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 | dad20b2ae2544708d6a33abdb9bddd0a329f50e0 (patch) | |
tree | fdb7968b3e5073b9ca72d4ca118217a955dd6752 /lib/AsmParser | |
parent | db7b69e3a634c5fdff0eceeee2a41ee49c3270a2 (diff) |
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.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/AsmParser')
-rw-r--r-- | lib/AsmParser/LLParser.cpp | 135 | ||||
-rw-r--r-- | lib/AsmParser/LLParser.h | 19 |
2 files changed, 92 insertions, 62 deletions
diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index a682f97ceb2..ca4ba6e07f3 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -62,8 +62,6 @@ bool LLParser::ValidateEndOfModule() { NumberedMetadata[SlotNo] == nullptr) return Error(MDList[i].Loc, "use of undefined metadata '!" + Twine(SlotNo) + "'"); - assert(!NumberedMetadata[SlotNo]->isFunctionLocal() && - "Unexpected function-local metadata"); Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]); } } @@ -169,6 +167,10 @@ bool LLParser::ValidateEndOfModule() { "use of undefined metadata '!" + Twine(ForwardRefMDNodes.begin()->first) + "'"); + // Resolve metadata cycles. + for (auto &N : NumberedMetadata) + if (auto *G = cast_or_null<GenericMDNode>(N)) + G->resolveCycles(); // Look for intrinsic functions and CallInst that need to be upgraded for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) @@ -561,12 +563,12 @@ bool LLParser::ParseMDNodeID(MDNode *&Result) { if (Result) return false; // Otherwise, create MDNode forward reference. - MDNode *FwdNode = MDNode::getTemporary(Context, None); + MDNodeFwdDecl *FwdNode = MDNode::getTemporary(Context, None); ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc()); if (NumberedMetadata.size() <= MID) NumberedMetadata.resize(MID+1); - NumberedMetadata[MID] = FwdNode; + NumberedMetadata[MID].reset(FwdNode); Result = FwdNode; return false; } @@ -609,23 +611,18 @@ bool LLParser::ParseStandaloneMetadata() { LocTy TyLoc; Type *Ty = nullptr; - SmallVector<Value *, 16> Elts; + MDNode *Init; if (ParseUInt32(MetadataID) || ParseToken(lltok::equal, "expected '=' here") || ParseType(Ty, TyLoc) || ParseToken(lltok::exclaim, "Expected '!' here") || - ParseToken(lltok::lbrace, "Expected '{' here") || - ParseMDNodeVector(Elts, nullptr) || - ParseToken(lltok::rbrace, "expected end of metadata node")) + ParseMDNode(Init)) return true; - MDNode *Init = MDNode::get(Context, Elts); - // See if this was forward referenced, if so, handle it. - std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator - FI = ForwardRefMDNodes.find(MetadataID); + auto FI = ForwardRefMDNodes.find(MetadataID); if (FI != ForwardRefMDNodes.end()) { - MDNode *Temp = FI->second.first; + auto *Temp = FI->second.first; Temp->replaceAllUsesWith(Init); MDNode::deleteTemporary(Temp); ForwardRefMDNodes.erase(FI); @@ -637,7 +634,7 @@ bool LLParser::ParseStandaloneMetadata() { if (NumberedMetadata[MetadataID] != nullptr) return TokError("Metadata id is already used"); - NumberedMetadata[MetadataID] = Init; + NumberedMetadata[MetadataID].reset(Init); } return false; @@ -1527,18 +1524,15 @@ bool LLParser::ParseInstructionMetadata(Instruction *Inst, if (ParseToken(lltok::exclaim, "expected '!' here")) return true; - // This code is similar to that of ParseMetadataValue, however it needs to + // This code is similar to that of ParseMetadata, however it needs to // have special-case code for a forward reference; see the comments on // ForwardRefInstMetadata for details. Also, MDStrings are not supported // at the top level here. if (Lex.getKind() == lltok::lbrace) { - ValID ID; - if (ParseMetadataListValue(ID, PFS)) + MDNode *N; + if (ParseMDNode(N)) return true; - assert(ID.Kind == ValID::t_MDNode); - if (ID.MDNodeVal->isFunctionLocal()) - return Error(Loc, "unexpected function-local metadata"); - Inst->setMetadata(MDK, ID.MDNodeVal); + Inst->setMetadata(MDK, N); } else { unsigned NodeID = 0; if (ParseMDNodeID(Node, NodeID)) @@ -2395,7 +2389,7 @@ bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) { ID.Kind = ValID::t_LocalName; break; case lltok::exclaim: // !42, !{...}, or !"foo" - return ParseMetadataValue(ID, PFS); + return ParseMetadataAsValue(ID, PFS); case lltok::APSInt: ID.APSIntVal = Lex.getAPSIntVal(); ID.Kind = ValID::t_APSInt; @@ -2935,45 +2929,69 @@ bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) { return false; } -bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) { - assert(Lex.getKind() == lltok::lbrace); - Lex.Lex(); +bool LLParser::ParseMDNode(MDNode *&MD) { + SmallVector<Metadata *, 16> Elts; + if (ParseMDNodeVector(Elts, nullptr)) + return true; - SmallVector<Value*, 16> Elts; - if (ParseMDNodeVector(Elts, PFS) || - ParseToken(lltok::rbrace, "expected end of metadata node")) + MD = MDNode::get(Context, Elts); + return false; +} + +bool LLParser::ParseMDNodeOrLocal(Metadata *&MD, PerFunctionState *PFS) { + SmallVector<Metadata *, 16> Elts; + if (ParseMDNodeVector(Elts, PFS)) return true; - ID.MDNodeVal = MDNode::get(Context, Elts); - ID.Kind = ValID::t_MDNode; + // Check for function-local metadata masquerading as an MDNode. + if (PFS && Elts.size() == 1 && Elts[0] && isa<LocalAsMetadata>(Elts[0])) { + MD = Elts[0]; + return false; + } + + MD = MDNode::get(Context, Elts); + return false; +} + +bool LLParser::ParseMetadataAsValue(ValID &ID, PerFunctionState *PFS) { + Metadata *MD; + if (ParseMetadata(MD, PFS)) + return true; + + ID.Kind = ValID::t_Metadata; + ID.MetadataVal = MetadataAsValue::get(Context, MD); return false; } -/// ParseMetadataValue +/// ParseMetadata /// ::= !42 /// ::= !{...} /// ::= !"string" -bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) { +bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) { assert(Lex.getKind() == lltok::exclaim); Lex.Lex(); // MDNode: // !{ ... } if (Lex.getKind() == lltok::lbrace) - return ParseMetadataListValue(ID, PFS); + return ParseMDNodeOrLocal(MD, PFS); // Standalone metadata reference // !42 if (Lex.getKind() == lltok::APSInt) { - if (ParseMDNodeID(ID.MDNodeVal)) return true; - ID.Kind = ValID::t_MDNode; + MDNode *N; + if (ParseMDNodeID(N)) + return true; + MD = N; return false; } // MDString: // ::= '!' STRINGCONSTANT - if (ParseMDString(ID.MDStringVal)) return true; - ID.Kind = ValID::t_MDString; + MDString *S; + if (ParseMDString(S)) + return true; + MD = S; return false; } @@ -3006,15 +3024,10 @@ bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2))); return false; } - case ValID::t_MDNode: + case ValID::t_Metadata: if (!Ty->isMetadataTy()) return Error(ID.Loc, "metadata value must have metadata type"); - V = ID.MDNodeVal; - return false; - case ValID::t_MDString: - if (!Ty->isMetadataTy()) - return Error(ID.Loc, "metadata value must have metadata type"); - V = ID.MDStringVal; + V = ID.MetadataVal; return false; case ValID::t_GlobalName: V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); @@ -4668,13 +4681,16 @@ int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { //===----------------------------------------------------------------------===// /// ParseMDNodeVector -/// ::= Element (',' Element)* +/// ::= { Element (',' Element)* } /// Element /// ::= 'null' | TypeAndValue -bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts, +bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts, PerFunctionState *PFS) { + assert(Lex.getKind() == lltok::lbrace); + Lex.Lex(); + // Check for an empty list. - if (Lex.getKind() == lltok::rbrace) + if (EatIfPresent(lltok::rbrace)) return false; bool IsLocal = false; @@ -4688,13 +4704,26 @@ bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts, continue; } + Type *Ty = nullptr; + if (ParseType(Ty)) + return true; + + if (Ty->isMetadataTy()) { + // No function-local metadata here. + Metadata *MD = nullptr; + if (ParseMetadata(MD, nullptr)) + return true; + Elts.push_back(MD); + continue; + } + Value *V = nullptr; - if (ParseTypeAndValue(V, PFS)) return true; - Elts.push_back(V); + if (ParseValue(Ty, V, PFS)) + return true; + assert(V && "Expected valid value"); + Elts.push_back(ValueAsMetadata::get(V)); - if (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal()) - return TokError("unexpected nested function-local metadata"); - if (!V->getType()->isMetadataTy() && !isa<Constant>(V)) { + if (isa<LocalAsMetadata>(Elts.back())) { assert(PFS && "Unexpected function-local metadata without PFS"); if (Elts.size() > 1) return TokError("unexpected function-local metadata"); @@ -4702,7 +4731,7 @@ bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts, } } while (EatIfPresent(lltok::comma)); - return false; + return ParseToken(lltok::rbrace, "expected end of metadata node"); } //===----------------------------------------------------------------------===// diff --git a/lib/AsmParser/LLParser.h b/lib/AsmParser/LLParser.h index aa62bcc8daf..d32e58e83f8 100644 --- a/lib/AsmParser/LLParser.h +++ b/lib/AsmParser/LLParser.h @@ -52,8 +52,7 @@ namespace llvm { t_EmptyArray, // No value: [] t_Constant, // Value in ConstantVal. t_InlineAsm, // Value in StrVal/StrVal2/UIntVal. - t_MDNode, // Value in MDNodeVal. - t_MDString, // Value in MDStringVal. + t_Metadata, // Value in MetadataVal. t_ConstantStruct, // Value in ConstantStructElts. t_PackedConstantStruct // Value in ConstantStructElts. } Kind; @@ -64,8 +63,7 @@ namespace llvm { APSInt APSIntVal; APFloat APFloatVal; Constant *ConstantVal; - MDNode *MDNodeVal; - MDString *MDStringVal; + MetadataAsValue *MetadataVal; Constant **ConstantStructElts; ValID() : Kind(t_LocalID), APFloatVal(0.0) {} @@ -115,8 +113,8 @@ namespace llvm { StringMap<std::pair<Type*, LocTy> > NamedTypes; std::vector<std::pair<Type*, LocTy> > NumberedTypes; - std::vector<TrackingVH<MDNode> > NumberedMetadata; - std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes; + std::vector<TrackingMDNodeRef> NumberedMetadata; + std::map<unsigned, std::pair<MDNodeFwdDecl *, LocTy>> ForwardRefMDNodes; // Global Value reference information. std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; @@ -382,9 +380,12 @@ namespace llvm { bool ParseGlobalTypeAndValue(Constant *&V); bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts); bool parseOptionalComdat(Comdat *&C); - bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS); - bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS); - bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS); + bool ParseMetadataAsValue(ValID &ID, PerFunctionState *PFS); + bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS); + bool ParseMDNode(MDNode *&MD); + bool ParseMDNodeOrLocal(Metadata *&MD, PerFunctionState *PFS); + bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &, + PerFunctionState *PFS); bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS); // Function Parsing. |