diff options
| author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2014-12-09 18:38:53 +0000 |
|---|---|---|
| committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2014-12-09 18:38:53 +0000 |
| commit | 5bf8fef58013e2c97180236fa6973faa40435d5f (patch) | |
| tree | d43dbdd5a9ee2cf0031cf3cd953d365a5e4e141d /llvm/bindings/go | |
| parent | b39e22bdc504ba77e2cd385538b6634808cda66e (diff) | |
| download | bcm5719-llvm-5bf8fef58013e2c97180236fa6973faa40435d5f.tar.gz bcm5719-llvm-5bf8fef58013e2c97180236fa6973faa40435d5f.zip | |
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
Diffstat (limited to 'llvm/bindings/go')
| -rw-r--r-- | llvm/bindings/go/llvm/DIBuilderBindings.cpp | 65 |
1 files changed, 41 insertions, 24 deletions
diff --git a/llvm/bindings/go/llvm/DIBuilderBindings.cpp b/llvm/bindings/go/llvm/DIBuilderBindings.cpp index cb7ac8ddd9c..9126320c0ef 100644 --- a/llvm/bindings/go/llvm/DIBuilderBindings.cpp +++ b/llvm/bindings/go/llvm/DIBuilderBindings.cpp @@ -18,10 +18,31 @@ using namespace llvm; +static Metadata *unwrapMetadata(LLVMValueRef VRef) { + Value *V = unwrap(VRef); + if (!V) + return nullptr; + if (auto *MD = dyn_cast<MetadataAsValue>(V)) + return MD->getMetadata(); + return ValueAsMetadata::get(V); +} + +static SmallVector<Metadata *, 8> unwrapMetadataArray(LLVMValueRef *Data, + size_t Length) { + SmallVector<Metadata *, 8> Elements; + for (size_t I = 0; I != Length; ++I) + Elements.push_back(unwrapMetadata(Data[I])); + return Elements; +} + namespace { template <typename T> T unwrapDI(LLVMValueRef v) { - return v ? T(unwrap<MDNode>(v)) : T(); + return T(cast_or_null<MDNode>(unwrapMetadata(v))); +} } + +static LLVMValueRef wrapDI(DIDescriptor N) { + return wrap(MetadataAsValue::get(N->getContext(), N)); } DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef) @@ -47,14 +68,14 @@ LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref, DIBuilder *D = unwrap(Dref); DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized, Flags, RuntimeVersion); - return wrap(CU); + return wrapDI(CU); } LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File, const char *Dir) { DIBuilder *D = unwrap(Dref); DIFile F = D->createFile(File, Dir); - return wrap(F); + return wrapDI(F); } LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref, @@ -64,7 +85,7 @@ LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref, DIBuilder *D = unwrap(Dref); DILexicalBlock LB = D->createLexicalBlock( unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Line, Column); - return wrap(LB); + return wrapDI(LB); } LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref, @@ -74,7 +95,7 @@ LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref, DIBuilder *D = unwrap(Dref); DILexicalBlockFile LBF = D->createLexicalBlockFile( unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Discriminator); - return wrap(LBF); + return wrapDI(LBF); } LLVMValueRef LLVMDIBuilderCreateFunction( @@ -87,7 +108,7 @@ LLVMValueRef LLVMDIBuilderCreateFunction( unwrapDI<DIDescriptor>(Scope), Name, LinkageName, unwrapDI<DIFile>(File), Line, unwrapDI<DICompositeType>(CompositeType), IsLocalToUnit, IsDefinition, ScopeLine, Flags, IsOptimized, unwrap<Function>(Func)); - return wrap(SP); + return wrapDI(SP); } LLVMValueRef LLVMDIBuilderCreateLocalVariable( @@ -98,7 +119,7 @@ LLVMValueRef LLVMDIBuilderCreateLocalVariable( DIVariable V = D->createLocalVariable( Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo); - return wrap(V); + return wrapDI(V); } LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref, @@ -107,7 +128,7 @@ LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref, unsigned Encoding) { DIBuilder *D = unwrap(Dref); DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding); - return wrap(T); + return wrapDI(T); } LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref, @@ -118,7 +139,7 @@ LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref, DIBuilder *D = unwrap(Dref); DIDerivedType T = D->createPointerType(unwrapDI<DIType>(PointeeType), SizeInBits, AlignInBits, Name); - return wrap(T); + return wrapDI(T); } LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, @@ -127,7 +148,7 @@ LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, DIBuilder *D = unwrap(Dref); DICompositeType CT = D->createSubroutineType( unwrapDI<DIFile>(File), unwrapDI<DITypeArray>(ParameterTypes)); - return wrap(CT); + return wrapDI(CT); } LLVMValueRef LLVMDIBuilderCreateStructType( @@ -139,7 +160,7 @@ LLVMValueRef LLVMDIBuilderCreateStructType( unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, SizeInBits, AlignInBits, Flags, unwrapDI<DIType>(DerivedFrom), unwrapDI<DIArray>(ElementTypes)); - return wrap(CT); + return wrapDI(CT); } LLVMValueRef LLVMDIBuilderCreateMemberType( @@ -150,7 +171,7 @@ LLVMValueRef LLVMDIBuilderCreateMemberType( DIDerivedType DT = D->createMemberType( unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI<DIType>(Ty)); - return wrap(DT); + return wrapDI(DT); } LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref, @@ -162,7 +183,7 @@ LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref, DICompositeType CT = D->createArrayType(SizeInBits, AlignInBits, unwrapDI<DIType>(ElementType), unwrapDI<DIArray>(Subscripts)); - return wrap(CT); + return wrapDI(CT); } LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty, @@ -172,40 +193,36 @@ LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty, DIDerivedType DT = D->createTypedef(unwrapDI<DIType>(Ty), Name, unwrapDI<DIFile>(File), Line, unwrapDI<DIDescriptor>(Context)); - return wrap(DT); + return wrapDI(DT); } LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo, int64_t Count) { DIBuilder *D = unwrap(Dref); DISubrange S = D->getOrCreateSubrange(Lo, Count); - return wrap(S); + return wrapDI(S); } LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref, LLVMValueRef *Data, size_t Length) { DIBuilder *D = unwrap(Dref); - Value **DataValue = unwrap(Data); - ArrayRef<Value *> Elements(DataValue, Length); - DIArray A = D->getOrCreateArray(Elements); - return wrap(A); + DIArray A = D->getOrCreateArray(unwrapMetadataArray(Data, Length)); + return wrapDI(A); } LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref, LLVMValueRef *Data, size_t Length) { DIBuilder *D = unwrap(Dref); - Value **DataValue = unwrap(Data); - ArrayRef<Value *> Elements(DataValue, Length); - DITypeArray A = D->getOrCreateTypeArray(Elements); - return wrap(A); + DITypeArray A = D->getOrCreateTypeArray(unwrapMetadataArray(Data, Length)); + return wrapDI(A); } LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr, size_t Length) { DIBuilder *D = unwrap(Dref); DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length)); - return wrap(Expr); + return wrapDI(Expr); } LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref, |

