llvm.org GIT mirror llvm / 3b0fe4e
AsmParser/Bitcode: Add support for MDLocation This adds assembly and bitcode support for `MDLocation`. The assembly side is rather big, since this is the first `MDNode` subclass (that isn't `MDTuple`). Part of PR21433. (If you're wondering where the mountains of testcase updates are, we don't need them until I update `DILocation` and `DebugLoc` to actually use this class.) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225830 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 4 years ago
15 changed file(s) with 284 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
28822882 More information about specific metadata nodes recognized by the
28832883 optimizers and code generator is found below.
28842884
2885 Specialized Metadata Nodes
2886 ^^^^^^^^^^^^^^^^^^^^^^^^^^
2887
2888 Specialized metadata nodes are custom data structures in metadata (as opposed
2889 to generic tuples). Their fields are labelled, and can be specified in any
2890 order.
2891
2892 MDLocation
2893 """"""""""
2894
2895 ``MDLocation`` nodes represent source debug locations. The ``scope:`` field is
2896 mandatory.
2897
2898 .. code-block:: llvm
2899
2900 !0 = !MDLocation(line: 2900, column: 42, scope: !1, inlinedAt: !2)
2901
28852902 '``tbaa``' Metadata
28862903 ^^^^^^^^^^^^^^^^^^^
28872904
141141 METADATA_NAME = 4, // STRING: [values]
142142 METADATA_DISTINCT_NODE = 5, // DISTINCT_NODE: [n x md num]
143143 METADATA_KIND = 6, // [n x [id, name]]
144 // 7 is unused.
144 METADATA_LOCATION = 7, // [distinct, line, col, scope, inlined-at?]
145145 METADATA_OLD_NODE = 8, // OLD_NODE: [n x (type num, value num)]
146146 METADATA_OLD_FN_NODE = 9, // OLD_FN_NODE: [n x (type num, value num)]
147147 METADATA_NAMED_NODE = 10, // NAMED_NODE: [n x mdnodes]
586586 return TokError("unexpected type in metadata definition");
587587
588588 bool IsDistinct = EatIfPresent(lltok::kw_distinct);
589 if (ParseToken(lltok::exclaim, "Expected '!' here") ||
590 ParseMDTuple(Init, IsDistinct))
589 if (Lex.getKind() == lltok::MetadataVar) {
590 if (ParseSpecializedMDNode(Init, IsDistinct))
591 return true;
592 } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
593 ParseMDTuple(Init, IsDistinct))
591594 return true;
592595
593596 // See if this was forward referenced, if so, handle it.
29012904 /// MDNode:
29022905 /// ::= !{ ... }
29032906 /// ::= !7
2907 /// ::= !MDLocation(...)
29042908 bool LLParser::ParseMDNode(MDNode *&N) {
2909 if (Lex.getKind() == lltok::MetadataVar)
2910 return ParseSpecializedMDNode(N);
2911
29052912 return ParseToken(lltok::exclaim, "expected '!' here") ||
29062913 ParseMDNodeTail(N);
29072914 }
29142921 // !42
29152922 return ParseMDNodeID(N);
29162923 }
2924
2925 bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
2926 MDUnsignedField &Result) {
2927 if (Result.Seen)
2928 return Error(Loc,
2929 "field '" + Name + "' cannot be specified more than once");
2930
2931 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
2932 return TokError("expected unsigned integer");
2933 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(Result.Max + 1ull);
2934
2935 if (Val64 > Result.Max)
2936 return TokError("value for '" + Name + "' too large, limit is " +
2937 Twine(Result.Max));
2938 Result.assign(Val64);
2939 Lex.Lex();
2940 return false;
2941 }
2942
2943 bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
2944 if (Result.Seen)
2945 return Error(Loc,
2946 "field '" + Name + "' cannot be specified more than once");
2947
2948 Metadata *MD;
2949 if (ParseMetadata(MD, nullptr))
2950 return true;
2951
2952 Result.assign(MD);
2953 return false;
2954 }
2955
2956 template
2957 bool LLParser::ParseMDFieldsImpl(ParserTy parseField) {
2958 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
2959 Lex.Lex();
2960
2961 if (ParseToken(lltok::lparen, "expected '(' here"))
2962 return true;
2963 if (EatIfPresent(lltok::rparen))
2964 return false;
2965
2966 do {
2967 if (Lex.getKind() != lltok::LabelStr)
2968 return TokError("expected field label here");
2969
2970 if (parseField())
2971 return true;
2972 } while (EatIfPresent(lltok::comma));
2973
2974 return ParseToken(lltok::rparen, "expected ')' here");
2975 }
2976
2977 bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
2978 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
2979 #define DISPATCH_TO_PARSER(CLASS) \
2980 if (Lex.getStrVal() == #CLASS) \
2981 return Parse##CLASS(N, IsDistinct);
2982
2983 DISPATCH_TO_PARSER(MDLocation);
2984 #undef DISPATCH_TO_PARSER
2985
2986 return TokError("expected metadata type");
2987 }
2988
2989 #define PARSE_MD_FIELD(NAME) \
2990 do { \
2991 if (Lex.getStrVal() == #NAME) { \
2992 LocTy Loc = Lex.getLoc(); \
2993 Lex.Lex(); \
2994 if (ParseMDField(Loc, #NAME, NAME)) \
2995 return true; \
2996 return false; \
2997 } \
2998 } while (0)
2999
3000 /// ParseMDLocationFields:
3001 /// ::= !MDLocation(line: 43, column: 8, scope: !5, inlinedAt: !6)
3002 bool LLParser::ParseMDLocation(MDNode *&Result, bool IsDistinct) {
3003 MDUnsignedField line(0, ~0u >> 8);
3004 MDUnsignedField column(0, ~0u >> 24);
3005 MDField scope;
3006 MDField inlinedAt;
3007 if (ParseMDFieldsImpl([&]() -> bool {
3008 PARSE_MD_FIELD(line);
3009 PARSE_MD_FIELD(column);
3010 PARSE_MD_FIELD(scope);
3011 PARSE_MD_FIELD(inlinedAt);
3012 return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");
3013 }))
3014 return true;
3015
3016 if (!scope.Seen)
3017 return TokError("missing required field 'scope'");
3018
3019 auto get = (IsDistinct ? MDLocation::getDistinct : MDLocation::get);
3020 Result = get(Context, line.Val, column.Val, scope.Val, inlinedAt.Val);
3021 return false;
3022 }
3023 #undef PARSE_MD_FIELD
29173024
29183025 /// ParseMetadataAsValue
29193026 /// ::= metadata i32 %local
29593066 /// ::= !42
29603067 /// ::= !{...}
29613068 /// ::= !"string"
3069 /// ::= !MDLocation(...)
29623070 bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
3071 if (Lex.getKind() == lltok::MetadataVar) {
3072 MDNode *N;
3073 if (ParseSpecializedMDNode(N))
3074 return true;
3075 MD = N;
3076 return false;
3077 }
3078
29633079 // ValueAsMetadata:
29643080 //
29653081 if (Lex.getKind() != lltok::exclaim)
7777 "Ordering not defined for this ValID kind yet");
7878 return StrVal < RHS.StrVal;
7979 }
80 };
81
82 /// Structure to represent an optional metadata field.
83 template struct MDFieldImpl {
84 typedef MDFieldImpl ImplTy;
85 FieldTy Val;
86 bool Seen;
87
88 void assign(FieldTy Val) {
89 Seen = true;
90 this->Val = Val;
91 }
92
93 explicit MDFieldImpl(FieldTy Default) : Val(Default), Seen(false) {}
94 };
95 template struct MDUnsignedField : public MDFieldImpl {
96 typedef typename MDUnsignedField::ImplTy ImplTy;
97 NumTy Max;
98
99 MDUnsignedField(NumTy Default = 0,
100 NumTy Max = std::numeric_limits::max())
101 : ImplTy(Default), Max(Max) {}
102 };
103 struct MDField : public MDFieldImpl {
104 MDField() : ImplTy(nullptr) {}
80105 };
81106
82107 class LLParser {
391416 bool ParseMDNodeTail(MDNode *&MD);
392417 bool ParseMDNodeVector(SmallVectorImpl &MDs);
393418 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
419
420 bool ParseMDField(LocTy Loc, StringRef Name,
421 MDUnsignedField &Result);
422 bool ParseMDField(LocTy Loc, StringRef Name, MDField &Result);
423 template bool ParseMDFieldsImpl(ParserTy parseField);
424 bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
425 bool ParseMDLocation(MDNode *&Result, bool IsDistinct);
394426
395427 // Function Parsing.
396428 struct ArgInfo {
12661266 NextMDValueNo++);
12671267 break;
12681268 }
1269 case bitc::METADATA_LOCATION: {
1270 if (Record.size() != 5)
1271 return Error("Invalid record");
1272
1273 auto get = Record[0] ? MDLocation::getDistinct : MDLocation::get;
1274 unsigned Line = Record[1];
1275 unsigned Column = Record[2];
1276 MDNode *Scope = cast(MDValueList.getValueFwdRef(Record[3]));
1277 Metadata *InlinedAt =
1278 Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr;
1279 MDValueList.AssignValue(get(Context, Line, Column, Scope, InlinedAt),
1280 NextMDValueNo++);
1281 break;
1282 }
12691283 case bitc::METADATA_STRING: {
12701284 std::string String(Record.begin(), Record.end());
12711285 llvm::UpgradeMDStringConstant(String);
778778 Record.clear();
779779 }
780780
781 static void WriteMDLocation(const MDLocation *N, const ValueEnumerator &VE,
782 BitstreamWriter &Stream,
783 SmallVectorImpl &Record,
784 unsigned Abbrev) {
785 Record.push_back(N->isDistinct());
786 Record.push_back(N->getLine());
787 Record.push_back(N->getColumn());
788 Record.push_back(VE.getMetadataID(N->getScope()));
789
790 // Always emit the inlined-at location, even though it's optional.
791 if (Metadata *InlinedAt = N->getInlinedAt())
792 Record.push_back(VE.getMetadataID(InlinedAt) + 1);
793 else
794 Record.push_back(0);
795
796 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
797 Record.clear();
798 }
799
781800 static void WriteModuleMetadata(const Module *M,
782801 const ValueEnumerator &VE,
783802 BitstreamWriter &Stream) {
797816 MDSAbbrev = Stream.EmitAbbrev(Abbv);
798817 }
799818
819 unsigned LocAbbrev = 0;
820 if (VE.hasMDLocation()) {
821 // Abbrev for METADATA_LOCATION.
822 //
823 // Assume the column is usually under 128, and always output the inlined-at
824 // location (it's never more expensive than building an array size 1).
825 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
826 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
827 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
828 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
829 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
830 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
831 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
832 LocAbbrev = Stream.EmitAbbrev(Abbv);
833 }
834
800835 unsigned NameAbbrev = 0;
801836 if (!M->named_metadata_empty()) {
802837 // Abbrev for METADATA_NAME.
809844
810845 SmallVector Record;
811846 for (const Metadata *MD : MDs) {
847 if (const MDLocation *Loc = dyn_cast(MD)) {
848 WriteMDLocation(Loc, VE, Stream, Record, LocAbbrev);
849 continue;
850 }
812851 if (const MDNode *N = dyn_cast(MD)) {
813852 WriteMDNode(N, VE, Stream, Record);
814853 continue;
281281 return V.first->getType()->isIntOrIntVectorTy();
282282 }
283283
284 ValueEnumerator::ValueEnumerator(const Module &M) : HasMDString(false) {
284 ValueEnumerator::ValueEnumerator(const Module &M)
285 : HasMDString(false), HasMDLocation(false) {
285286 if (shouldPreserveBitcodeUseListOrder())
286287 UseListOrders = predictUseListOrder(M);
287288
546547 EnumerateValue(C->getValue());
547548
548549 HasMDString |= isa(MD);
550 HasMDLocation |= isa(MD);
549551
550552 // Replace the dummy ID inserted above with the correct one. MDValueMap may
551553 // have changed by inserting operands, so we need a fresh lookup here.
6464 typedef DenseMap MetadataMapType;
6565 MetadataMapType MDValueMap;
6666 bool HasMDString;
67 bool HasMDLocation;
6768
6869 typedef DenseMap AttributeGroupMapType;
6970 AttributeGroupMapType AttributeGroupMap;
110111 unsigned getMetadataID(const Metadata *V) const;
111112
112113 bool hasMDString() const { return HasMDString; }
114 bool hasMDLocation() const { return HasMDLocation; }
113115
114116 unsigned getTypeID(Type *T) const {
115117 TypeMapType::const_iterator I = TypeMap.find(T);
0 ; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
1
2 ; CHECK: :[[@LINE+1]]:18: error: invalid field 'bad'
3 !0 = !MDLocation(bad: 0)
0 ; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
1
2 !0 = !{}
3
4 ; CHECK: :[[@LINE+1]]:38: error: field 'line' cannot be specified more than once
5 !1 = !MDLocation(line: 3, scope: !0, line: 3)
0 ; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
1
2 !0 = !{}
3
4 ; CHECK-NOT: error
5 !1 = !MDLocation(column: 255, scope: !0)
6
7 ; CHECK: :[[@LINE+1]]:26: error: value for 'column' too large, limit is 255
8 !2 = !MDLocation(column: 256, scope: !0)
0 ; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
1
2 !0 = !{}
3
4 ; CHECK-NOT: error
5 !1 = !MDLocation(line: 16777215, scope: !0)
6
7 ; CHECK: :[[@LINE+1]]:24: error: value for 'line' too large, limit is 16777215
8 !2 = !MDLocation(line: 16777216, scope: !0)
0 ; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
1
2 ; CHECK: :[[@LINE+1]]:6: error: expected metadata type
3 !0 = !Invalid(field: 0)
0 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s
1 ; RUN: verify-uselistorder %s
2
3 ; CHECK: !named = !{!0, !1, !1, !2, !2, !3, !3}
4 !named = !{!0, !1, !2, !3, !4, !5, !6}
5
6 ; CHECK: !0 = !{}
7 !0 = !{}
8
9 ; CHECK-NEXT: !1 = !MDLocation(line: 3, column: 7, scope: !0)
10 !1 = !MDLocation(line: 3, column: 7, scope: !0)
11 !2 = !MDLocation(scope: !0, column: 7, line: 3)
12
13 ; CHECK-NEXT: !2 = !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !1)
14 !3 = !MDLocation(scope: !0, inlinedAt: !1, column: 7, line: 3)
15 !4 = !MDLocation(column: 7, line: 3, scope: !0, inlinedAt: !1)
16
17 ; CHECK-NEXT: !3 = !MDLocation(scope: !0)
18 !5 = !MDLocation(scope: !0)
19 !6 = !MDLocation(scope: !0, column: 0, line: 0)
6161 syn keyword llvmError getresult begin end
6262
6363 " Misc syntax.
64 syn match llvmNoName /[%@]\d\+\>/
64 syn match llvmNoName /[%@!]\d\+\>/
6565 syn match llvmNumber /-\?\<\d\+\>/
6666 syn match llvmFloat /-\?\<\d\+\.\d*\(e[+-]\d\+\)\?\>/
6767 syn match llvmFloat /\<0x\x\+\>/
7171 syn region llvmString start=/"/ skip=/\\"/ end=/"/
7272 syn match llvmLabel /[-a-zA-Z$._][-a-zA-Z$._0-9]*:/
7373 syn match llvmIdentifier /[%@][-a-zA-Z$._][-a-zA-Z$._0-9]*/
74
75 " Named metadata and specialized metadata keywords.
76 syn match llvmIdentifier /![-a-zA-Z$._][-a-zA-Z$._0-9]*\ze\s*$/
77 syn match llvmIdentifier /![-a-zA-Z$._][-a-zA-Z$._0-9]*\ze\s*[=!]/
78 syn match llvmType /!\zs\a\+\ze\s*(/
7479
7580 " Syntax-highlight dejagnu test commands.
7681 syn match llvmSpecialComment /;\s*RUN:.*$/