Index: llvm/include/llvm-c/DebugInfo.h =================================================================== --- llvm/include/llvm-c/DebugInfo.h +++ llvm/include/llvm-c/DebugInfo.h @@ -169,7 +169,8 @@ LLVMDICommonBlockMetadataKind, LLVMDIStringTypeMetadataKind, LLVMDIGenericSubrangeMetadataKind, - LLVMDIArgListMetadataKind + LLVMDIArgListMetadataKind, + LLVMDIAssignIDMetadataKind, }; typedef unsigned LLVMMetadataKind; Index: llvm/include/llvm/Bitcode/LLVMBitCodes.h =================================================================== --- llvm/include/llvm/Bitcode/LLVMBitCodes.h +++ llvm/include/llvm/Bitcode/LLVMBitCodes.h @@ -346,7 +346,8 @@ // info. METADATA_COMMON_BLOCK = 44, // [distinct, scope, name, variable,...] METADATA_GENERIC_SUBRANGE = 45, // [distinct, count, lo, up, stride] - METADATA_ARG_LIST = 46 // [n x [type num, value num]] + METADATA_ARG_LIST = 46, // [n x [type num, value num]] + METADATA_ASSIGN_ID = 47, // [distinct, ...] }; // The constants block (CONSTANTS_BLOCK_ID) describes emission for each Index: llvm/include/llvm/IR/DebugInfoMetadata.h =================================================================== --- llvm/include/llvm/IR/DebugInfoMetadata.h +++ llvm/include/llvm/IR/DebugInfoMetadata.h @@ -210,6 +210,7 @@ case DIImportedEntityKind: case DIModuleKind: case DIGenericSubrangeKind: + case DIAssignIDKind: return true; } } @@ -290,6 +291,37 @@ } }; +/// Assignment ID +/// Used to link stores (as an attachment) and dbg.assigns (as an operand). +class DIAssignID : public MDNode { + friend class LLVMContextImpl; + friend class MDNode; + + DIAssignID(LLVMContext &C, StorageType Storage) + : MDNode(C, DIAssignIDKind, Storage, None) {} + + ~DIAssignID() { dropAllReferences(); } + + static DIAssignID *getImpl(LLVMContext &Context, StorageType Storage, + bool ShouldCreate = true); + + TempDIAssignID cloneImpl() const { return getTemporary(getContext()); } + +public: + // This node has no operands to replace. + void replaceOperandWith(unsigned I, Metadata *New) = delete; + static DIAssignID *getDistinct(LLVMContext &Context) { + return getImpl(Context, Distinct); + } + static TempDIAssignID getTemporary(LLVMContext &Context) { + return TempDIAssignID(getImpl(Context, Temporary)); + } + + static bool classof(const Metadata *MD) { + return MD->getMetadataID() == DIAssignIDKind; + } +}; + /// Array subrange. /// /// TODO: Merge into node for DW_TAG_array_type, which should have a custom Index: llvm/include/llvm/IR/FixedMetadataKinds.def =================================================================== --- llvm/include/llvm/IR/FixedMetadataKinds.def +++ llvm/include/llvm/IR/FixedMetadataKinds.def @@ -42,3 +42,4 @@ LLVM_FIXED_MD_KIND(MD_vcall_visibility, "vcall_visibility", 28) LLVM_FIXED_MD_KIND(MD_noundef, "noundef", 29) LLVM_FIXED_MD_KIND(MD_annotation, "annotation", 30) +LLVM_FIXED_MD_KIND(MD_DIAssignID, "DIAssignID", 31) Index: llvm/include/llvm/IR/Metadata.def =================================================================== --- llvm/include/llvm/IR/Metadata.def +++ llvm/include/llvm/IR/Metadata.def @@ -110,6 +110,7 @@ HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DILabel) HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIObjCProperty) HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIImportedEntity) +HANDLE_SPECIALIZED_MDNODE_LEAF(DIAssignID) HANDLE_SPECIALIZED_MDNODE_BRANCH(DIMacroNode) HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIMacro) HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIMacroFile) Index: llvm/lib/AsmParser/LLParser.cpp =================================================================== --- llvm/lib/AsmParser/LLParser.cpp +++ llvm/lib/AsmParser/LLParser.cpp @@ -4413,6 +4413,24 @@ return false; } +/// parseDIAssignID: +/// ::= distinct !DIAssignID() +bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) { + if (!IsDistinct) + return Lex.Error("missing 'distinct', required for !DIAssignID()"); + + Lex.Lex(); + + // Now eat the parens. + if (parseToken(lltok::lparen, "expected '(' here")) + return true; + if (parseToken(lltok::rparen, "expected ')' here")) + return true; + + Result = DIAssignID::getDistinct(Context); + return false; +} + /// parseGenericDINode: /// ::= !GenericDINode(tag: 15, header: "...", operands: {...}) bool LLParser::parseGenericDINode(MDNode *&Result, bool IsDistinct) { Index: llvm/lib/Bitcode/Reader/MetadataLoader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/MetadataLoader.cpp +++ llvm/lib/Bitcode/Reader/MetadataLoader.cpp @@ -870,6 +870,7 @@ case bitc::METADATA_TEMPLATE_VALUE: case bitc::METADATA_GLOBAL_VAR: case bitc::METADATA_LOCAL_VAR: + case bitc::METADATA_ASSIGN_ID: case bitc::METADATA_LABEL: case bitc::METADATA_EXPRESSION: case bitc::METADATA_OBJC_PROPERTY: @@ -1953,6 +1954,18 @@ break; } + case bitc::METADATA_ASSIGN_ID: { + if (Record.size() != 1) + return error("Invalid DIAssignID record."); + + IsDistinct = Record[0] & 1; + if (!IsDistinct) + return error("Invalid DIAssignID record. Must be distinct"); + + MetadataList.assignValue(DIAssignID::getDistinct(Context), NextMetadataNo); + NextMetadataNo++; + break; + } case bitc::METADATA_LOCAL_VAR: { // 10th field is for the obseleted 'inlinedAt:' field. if (Record.size() < 8 || Record.size() > 10) Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -338,6 +338,8 @@ unsigned Abbrev); void writeDIModule(const DIModule *N, SmallVectorImpl &Record, unsigned Abbrev); + void writeDIAssignID(const DIAssignID *N, SmallVectorImpl &Record, + unsigned Abbrev); void writeDITemplateTypeParameter(const DITemplateTypeParameter *N, SmallVectorImpl &Record, unsigned Abbrev); @@ -1922,6 +1924,15 @@ Record.clear(); } +void ModuleBitcodeWriter::writeDIAssignID(const DIAssignID *N, + SmallVectorImpl &Record, + unsigned Abbrev) { + // There are no arguments for this metadata type. + Record.push_back(N->isDistinct()); + Stream.EmitRecord(bitc::METADATA_ASSIGN_ID, Record, Abbrev); + Record.clear(); +} + void ModuleBitcodeWriter::writeDITemplateTypeParameter( const DITemplateTypeParameter *N, SmallVectorImpl &Record, unsigned Abbrev) { Index: llvm/lib/IR/AsmWriter.cpp =================================================================== --- llvm/lib/IR/AsmWriter.cpp +++ llvm/lib/IR/AsmWriter.cpp @@ -1858,6 +1858,12 @@ Out << ")"; } +static void writeDIAssignID(raw_ostream &Out, const DIAssignID *DL, + AsmWriterContext &WriterCtx) { + Out << "!DIAssignID()"; + MDFieldPrinter Printer(Out, WriterCtx); +} + static void writeDISubrange(raw_ostream &Out, const DISubrange *N, AsmWriterContext &WriterCtx) { Out << "!DISubrange("; Index: llvm/lib/IR/DebugInfoMetadata.cpp =================================================================== --- llvm/lib/IR/DebugInfoMetadata.cpp +++ llvm/lib/IR/DebugInfoMetadata.cpp @@ -1079,6 +1079,11 @@ DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements)); } +DIAssignID *DIAssignID::getImpl(LLVMContext &Context, StorageType Storage, + bool ShouldCreate) { + return new (0u) DIAssignID(Context, Storage); +} + unsigned DIExpression::ExprOperand::getSize() const { uint64_t Op = getOp(); Index: llvm/lib/IR/Verifier.cpp =================================================================== --- llvm/lib/IR/Verifier.cpp +++ llvm/lib/IR/Verifier.cpp @@ -462,6 +462,7 @@ void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty); void visitDereferenceableMetadata(Instruction &I, MDNode *MD); void visitProfMetadata(Instruction &I, MDNode *MD); + void visitDIAssignIDMetadata(Instruction &I, MDNode *MD); void visitAnnotationMetadata(MDNode *Annotation); void visitAliasScopeMetadata(const MDNode *MD); void visitAliasScopeListMetadata(const MDNode *MD); @@ -1451,6 +1452,11 @@ AssertDI(!isa(Ty), "invalid type", &N, N.getType()); } +void Verifier::visitDIAssignID(const DIAssignID &N) { + AssertDI(!N.getNumOperands(), "DIAssignID has no arguments", &N); + AssertDI(N.isDistinct(), "DIAssignID must be distinct", &N); +} + void Verifier::visitDILabel(const DILabel &N) { if (auto *S = N.getRawScope()) AssertDI(isa(S), "invalid scope", &N, S); @@ -4412,6 +4418,14 @@ } } +void Verifier::visitDIAssignIDMetadata(Instruction &I, MDNode *MD) { + assert(I.hasMetadata(LLVMContext::MD_DIAssignID)); + bool ExpectedInstTy = + isa(I) || isa(I) || isa(I); + AssertDI(ExpectedInstTy, + "!DIAssignID attached to unexpected instruction kind", I, MD); +} + void Verifier::visitAnnotationMetadata(MDNode *Annotation) { Assert(isa(Annotation), "annotation must be a tuple"); Assert(Annotation->getNumOperands() >= 1, @@ -4635,6 +4649,9 @@ if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof)) visitProfMetadata(I, MD); + if (MDNode *MD = I.getMetadata(LLVMContext::MD_DIAssignID)) + visitDIAssignIDMetadata(I, MD); + if (MDNode *Annotation = I.getMetadata(LLVMContext::MD_annotation)) visitAnnotationMetadata(Annotation); Index: llvm/test/DebugInfo/Generic/assignment-tracking/roundtrip.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/Generic/assignment-tracking/roundtrip.ll @@ -0,0 +1,33 @@ +; RUN: opt %s -verify -experimental-assignment-tracking \ +; RUN: | opt -verify -S -experimental-assignment-tracking \ +; RUN: | FileCheck %s + +;; Roundtrip test (text -> bitcode -> text) for DIAssignID attachments. + +; CHECK: %local = alloca i32, align 4, !DIAssignID ![[ID:[0-9]+]] +; CHECK-DAG: ![[ID]] = distinct !DIAssignID() + +define dso_local void @fun() !dbg !7 { +entry: + %local = alloca i32, align 4, !DIAssignID !14 + ret void, !dbg !13 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 14.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "test.c", directory: "/") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 14.0.0"} +!7 = distinct !DISubprogram(name: "fun", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{null} +!10 = !DILocalVariable(name: "local", scope: !7, file: !1, line: 2, type: !11) +!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!13 = !DILocation(line: 3, column: 1, scope: !7) +!14 = distinct !DIAssignID()