Index: llvm/include/llvm/AsmParser/LLParser.h =================================================================== --- llvm/include/llvm/AsmParser/LLParser.h +++ llvm/include/llvm/AsmParser/LLParser.h @@ -97,6 +97,12 @@ SmallVector InstsWithTBAATag; + /// DIAssignID metadata does not support temporary RAUW so we cannot use + /// the normal metadata forward reference resolution method. Instead, + /// non-temporary DIAssignID are attached to instructions (recorded here) + /// then replaced later. + DenseMap> TempDIAssignIDAttachments; + // Type resolution handling data structures. The location is set when we // have processed a use of the type but not a definition yet. StringMap > NamedTypes; Index: llvm/include/llvm/IR/DebugInfo.h =================================================================== --- llvm/include/llvm/IR/DebugInfo.h +++ llvm/include/llvm/IR/DebugInfo.h @@ -21,7 +21,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/TinyPtrVector.h" #include "llvm/ADT/iterator_range.h" -#include "llvm/IR/DebugInfoMetadata.h" +#include "llvm/IR/IntrinsicInst.h" namespace llvm { @@ -159,6 +159,52 @@ SmallPtrSet NodesSeen; }; +/// Assignment Tracking (at). +namespace at { +using AssignmentInstRange = + iterator_range::iterator>; +/// Return a range of instructions (typically just one) that have \p ID +/// as an attachment. +AssignmentInstRange getAssignmentInsts(DIAssignID *ID); +/// Return a range of instructions (typically just one) that perform the +/// assignment that \p DAI encodes. +inline AssignmentInstRange getAssignmentInsts(const DbgAssignIntrinsic *DAI) { + return getAssignmentInsts(cast(DAI->getAssignId())); +} + +/// Wrapper around Value's User iterator that dereferences to a +/// DbgAssignIntrinsic ptr rather than a User ptr. +class DbgAssignIt + : public iterator_adaptor_base::iterator_category, + DbgAssignIntrinsic *, std::ptrdiff_t, + DbgAssignIntrinsic **, + DbgAssignIntrinsic *&> { +public: + DbgAssignIt(Value::user_iterator It) : iterator_adaptor_base(It) {} + DbgAssignIntrinsic *operator*() const { return cast(*I); } +}; +using AssignmentMarkerRange = iterator_range; +/// Return a list of dbg.assign intrinsics which use \ID as an operand. +AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID); +/// Return a list of dbg.assign intrinsics for which \p Inst performs the +/// assignment they encode. +inline AssignmentMarkerRange getAssignmentMarkers(const Instruction *Inst) { + if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID)) + return getAssignmentMarkers(cast(ID)); + else + return make_range(Value::user_iterator(), Value::user_iterator()); +} + +/// Replace all uses (and attachments) of \p Old with \p New. +void RAUW(DIAssignID *Old, DIAssignID *New); + +/// Remove all Assignment Tracking related intrinsics and metadata from \p F. +void deleteAll(Function *F); + +} // end namespace at + /// Return true if assignment tracking is enabled. bool getEnableAssignmentTracking(); } // end namespace llvm Index: llvm/include/llvm/IR/Instruction.h =================================================================== --- llvm/include/llvm/IR/Instruction.h +++ llvm/include/llvm/IR/Instruction.h @@ -529,6 +529,10 @@ void getAllMetadataImpl(SmallVectorImpl> &) const; + // Update the LLVMContext ID-to-Instruction(s) mapping. If \p ID is nullptr + // then clear the mapping for this instruction. + void updateDIAssignIDMapping(DIAssignID *ID); + public: //===--------------------------------------------------------------------===// // Predicates and helper methods. Index: llvm/include/llvm/IR/IntrinsicInst.h =================================================================== --- llvm/include/llvm/IR/IntrinsicInst.h +++ llvm/include/llvm/IR/IntrinsicInst.h @@ -90,6 +90,7 @@ case Intrinsic::assume: case Intrinsic::sideeffect: case Intrinsic::pseudoprobe: + case Intrinsic::dbg_assign: case Intrinsic::dbg_declare: case Intrinsic::dbg_value: case Intrinsic::dbg_label: @@ -124,6 +125,7 @@ case Intrinsic::dbg_value: case Intrinsic::dbg_addr: case Intrinsic::dbg_label: + case Intrinsic::dbg_assign: return true; default: return false; @@ -226,10 +228,12 @@ bool hasArgList() const { return isa(getRawLocation()); } - /// Does this describe the address of a local variable. True for dbg.addr - /// and dbg.declare, but not dbg.value, which describes its value. + /// Does this describe the address of a local variable. True for dbg.addr and + /// dbg.declare, but not dbg.value, which describes its value, or dbg.assign, + /// which describes a combination of the variable's value and address. bool isAddressOfVariable() const { - return getIntrinsicID() != Intrinsic::dbg_value; + return getIntrinsicID() != Intrinsic::dbg_value && + getIntrinsicID() != Intrinsic::dbg_assign; } void setUndef() { @@ -281,6 +285,14 @@ /// is described. Optional getFragmentSizeInBits() const; + // Get the FragmentInfo for the variable. Note that dbg.assign intrinsics + // track two DIExpressions, but only the first expression has FragmentInfo. + // Using this function rather than going through DIExpression directly helps + // to abstract/hide that hack. FIXME: Move fragment info out of DIExpression. + Optional getFragment() const { + return getExpression()->getFragmentInfo(); + } + /// \name Casting methods /// @{ static bool classof(const IntrinsicInst *I) { @@ -288,6 +300,7 @@ case Intrinsic::dbg_declare: case Intrinsic::dbg_value: case Intrinsic::dbg_addr: + case Intrinsic::dbg_assign: return true; default: return false; @@ -297,7 +310,7 @@ return isa(V) && classof(cast(V)); } /// @} -private: +protected: void setArgOperand(unsigned i, Value *v) { DbgInfoIntrinsic::setArgOperand(i, v); } @@ -358,7 +371,50 @@ /// \name Casting methods /// @{ static bool classof(const IntrinsicInst *I) { - return I->getIntrinsicID() == Intrinsic::dbg_value; + return I->getIntrinsicID() == Intrinsic::dbg_value || + I->getIntrinsicID() == Intrinsic::dbg_assign; + } + static bool classof(const Value *V) { + return isa(V) && classof(cast(V)); + } + /// @} +}; + +/// This represents the llvm.dbg.assign instruction. +class DbgAssignIntrinsic : public DbgValueInst { + enum Operands { + OpValue, + OpVar, + OpExpr, + OpAssignID, + OpAddress, + OpAddressExpr, + }; + +public: + Value *getAddress() const; + MetadataAsValue *getAssignIdMetadataAsValue() const { + return cast(getArgOperand(OpAssignID)); + } + Metadata *getAssignId() const { + return getAssignIdMetadataAsValue()->getMetadata(); + } + DIExpression *getAddressExpression() const { + return cast( + cast(getArgOperand(OpAddressExpr))->getMetadata()); + } + void setAddressExpression(DIExpression *NewExpr) { + setArgOperand(OpAddressExpr, + MetadataAsValue::get(NewExpr->getContext(), NewExpr)); + } + void setAssignId(DIAssignID *New); + void setAddress(Value *V); + void setValue(Value *V); + void replaceUsesOfWith(Value *From, Value *To); + /// \name Casting methods + /// @{ + static bool classof(const IntrinsicInst *I) { + return I->getIntrinsicID() == Intrinsic::dbg_assign; } static bool classof(const Value *V) { return isa(V) && classof(cast(V)); Index: llvm/include/llvm/IR/Intrinsics.td =================================================================== --- llvm/include/llvm/IR/Intrinsics.td +++ llvm/include/llvm/IR/Intrinsics.td @@ -958,6 +958,13 @@ [llvm_metadata_ty, llvm_metadata_ty, llvm_metadata_ty]>; + def int_dbg_assign : DefaultAttrsIntrinsic<[], + [llvm_metadata_ty, + llvm_metadata_ty, + llvm_metadata_ty, + llvm_metadata_ty, + llvm_metadata_ty, + llvm_metadata_ty]>; def int_dbg_label : DefaultAttrsIntrinsic<[], [llvm_metadata_ty]>; } Index: llvm/lib/AsmParser/LLParser.cpp =================================================================== --- llvm/lib/AsmParser/LLParser.cpp +++ llvm/lib/AsmParser/LLParser.cpp @@ -796,7 +796,18 @@ // See if this was forward referenced, if so, handle it. auto FI = ForwardRefMDNodes.find(MetadataID); if (FI != ForwardRefMDNodes.end()) { - FI->second.first->replaceAllUsesWith(Init); + auto *ToReplace = FI->second.first.get(); + // DIAssignID has its own special forward-reference "replacement" for + // attachments (the temporary attachments are never actually attached). + if (isa(Init)) { + for (auto *Inst : TempDIAssignIDAttachments[ToReplace]) { + assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID) && + "Inst already has DIAssignID?"); + Inst->setMetadata(LLVMContext::MD_DIAssignID, Init); + } + } + + ToReplace->replaceAllUsesWith(Init); ForwardRefMDNodes.erase(FI); assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work"); @@ -1907,7 +1918,11 @@ if (parseMetadataAttachment(MDK, N)) return true; - Inst.setMetadata(MDK, N); + if (MDK == LLVMContext::MD_DIAssignID) + TempDIAssignIDAttachments[N].push_back(&Inst); + else + Inst.setMetadata(MDK, N); + if (MDK == LLVMContext::MD_tbaa) InstsWithTBAATag.push_back(&Inst); Index: llvm/lib/IR/DebugInfo.cpp =================================================================== --- llvm/lib/IR/DebugInfo.cpp +++ llvm/lib/IR/DebugInfo.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm-c/DebugInfo.h" +#include "LLVMContextImpl.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/STLExtras.h" @@ -37,6 +38,7 @@ #include using namespace llvm; +using namespace llvm::at; using namespace llvm::dwarf; static cl::opt @@ -1622,3 +1624,57 @@ return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind; } } + +AssignmentInstRange at::getAssignmentInsts(DIAssignID *ID) { + assert(ID && "Expected non-null ID"); + LLVMContext &Ctx = ID->getContext(); + auto &Map = Ctx.pImpl->AssignmentIDToInstrs; + + auto MapIt = Map.find(ID); + if (MapIt == Map.end()) + return make_range(nullptr, nullptr); + + return make_range(MapIt->second.begin(), MapIt->second.end()); +} + +AssignmentMarkerRange at::getAssignmentMarkers(DIAssignID *ID) { + assert(ID && "Expected non-null ID"); + LLVMContext &Ctx = ID->getContext(); + + auto *IDAsValue = MetadataAsValue::getIfExists(Ctx, ID); + + // The ID is only used wrapped in MetadataAsValue(ID), so lets check that + // one of those already exists first. + if (!IDAsValue) + return make_range(Value::user_iterator(), Value::user_iterator()); + + return make_range(IDAsValue->user_begin(), IDAsValue->user_end()); + ; +} + +void at::RAUW(DIAssignID *Old, DIAssignID *New) { + // Replace MetadataAsValue uses. + if (auto *OldIDAsValue = + MetadataAsValue::getIfExists(Old->getContext(), Old)) { + auto *NewIDAsValue = MetadataAsValue::get(Old->getContext(), New); + OldIDAsValue->replaceAllUsesWith(NewIDAsValue); + } + + // Replace attachments. + for (auto *I : getAssignmentInsts(Old)) + I->setMetadata(LLVMContext::MD_DIAssignID, New); +} + +void at::deleteAll(Function *F) { + SmallVector ToDelete; + for (BasicBlock &BB : *F) { + for (Instruction &I : BB) { + if (auto *DAI = dyn_cast(&I)) + ToDelete.push_back(DAI); + else + I.setMetadata(LLVMContext::MD_DIAssignID, nullptr); + } + } + for (auto *DAI : ToDelete) + DAI->eraseFromParent(); +} Index: llvm/lib/IR/Instruction.cpp =================================================================== --- llvm/lib/IR/Instruction.cpp +++ llvm/lib/IR/Instruction.cpp @@ -55,6 +55,10 @@ // instructions in a BasicBlock are deleted). if (isUsedByMetadata()) ValueAsMetadata::handleRAUW(this, UndefValue::get(getType())); + + // Explicitly remove DIAssignID metadata to clear up ID -> Instruction(s) + // mapping in LLVMContext. + setMetadata(LLVMContext::MD_DIAssignID, nullptr); } Index: llvm/lib/IR/IntrinsicInst.cpp =================================================================== --- llvm/lib/IR/IntrinsicInst.cpp +++ llvm/lib/IR/IntrinsicInst.cpp @@ -79,10 +79,20 @@ void DbgVariableIntrinsic::replaceVariableLocationOp(Value *OldValue, Value *NewValue) { + bool DbgAssignAddrReplaced = [this, OldValue, NewValue]() -> bool { + auto *DAI = dyn_cast(this); + if (!DAI || OldValue != DAI->getAddress()) + return false; + DAI->setAddress(NewValue); + return true; + }(); + (void)DbgAssignAddrReplaced; + assert(NewValue && "Values must be non-null"); auto Locations = location_ops(); auto OldIt = find(Locations, OldValue); - assert(OldIt != Locations.end() && "OldValue must be a current location"); + assert((OldIt != Locations.end() || DbgAssignAddrReplaced) && + "OldValue must be a current location"); if (!hasArgList()) { Value *NewOperand = isa(NewValue) ? NewValue @@ -139,6 +149,40 @@ return getVariable()->getSizeInBits(); } +Value *DbgAssignIntrinsic::getAddress() const { + Value *Op = getArgOperand(OpAddress); + auto *MD = cast(Op)->getMetadata(); + if (auto *V = dyn_cast(MD)) + return V->getValue(); + + // When the value goes to null, it gets replaced by an empty MDNode. + assert(!cast(MD)->getNumOperands() && "Expected an empty MDNode"); + return nullptr; +} + +void DbgAssignIntrinsic::setAssignId(DIAssignID *New) { + setOperand(OpAssignID, MetadataAsValue::get(getContext(), New)); +} + +void DbgAssignIntrinsic::setAddress(Value *V) { + assert(V->getType()->isPointerTy() && + "Destination Component must be a pointer type"); + setOperand(OpAddress, + MetadataAsValue::get(getContext(), ValueAsMetadata::get(V))); +} + +void DbgAssignIntrinsic::setValue(Value *V) { + setOperand(OpValue, + MetadataAsValue::get(getContext(), ValueAsMetadata::get(V))); +} + +void DbgAssignIntrinsic::replaceUsesOfWith(Value *From, Value *To) { + if (getValue() == From) + setValue(To); + if (getAddress() == From) + setAddress(To); +} + int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef NameTable, StringRef Name) { assert(Name.startswith("llvm.")); Index: llvm/lib/IR/LLVMContextImpl.h =================================================================== --- llvm/lib/IR/LLVMContextImpl.h +++ llvm/lib/IR/LLVMContextImpl.h @@ -1487,6 +1487,11 @@ /// Collection of metadata used in this context. DenseMap ValueMetadata; + // Map DIAssignID -> Instructions with that attachment. + // Managed by Instruction via Instruction::updateDIAssignIDMapping. + // Query using the at:: functions defined in DebugInfo.h. + DenseMap> AssignmentIDToInstrs; + /// Collection of per-GlobalObject sections used in this context. DenseMap GlobalObjectSections; Index: llvm/lib/IR/Metadata.cpp =================================================================== --- llvm/lib/IR/Metadata.cpp +++ llvm/lib/IR/Metadata.cpp @@ -1329,6 +1329,36 @@ } } +void Instruction::updateDIAssignIDMapping(DIAssignID *ID) { + auto &IDToInstrs = getContext().pImpl->AssignmentIDToInstrs; + if (auto *CurrentID = + cast_or_null(getMetadata(LLVMContext::MD_DIAssignID))) { + // Nothing to do if the ID isn't changing. + if (ID == CurrentID) + return; + + // Unmap this instruction from its current ID. + auto InstrsIt = IDToInstrs.find(CurrentID); + assert(InstrsIt != IDToInstrs.end() && "bit out of sync with map 1"); + + auto &InstVec = InstrsIt->second; + auto *InstIt = std::find(InstVec.begin(), InstVec.end(), this); + if (InstIt != InstVec.end()) { + // The vector contains a ptr to this. + // If this is the only element in the vector, remove the ID:vector + // enrty, otherwise just remove the instruction from vector. + if (InstVec.size() == 1) + IDToInstrs.erase(InstrsIt); + else + InstVec.erase(InstIt); + } + } + + // Map this instruction to the new ID. + if (ID) + IDToInstrs[ID].push_back(this); +} + void Instruction::setMetadata(unsigned KindID, MDNode *Node) { if (!Node && !hasMetadata()) return; @@ -1339,6 +1369,15 @@ return; } + // Update DIAssignID to Instruction(s) mapping. + if (KindID == LLVMContext::MD_DIAssignID) { + // The DIAssignID tracking infrastructure doesn't support RAUWing temporary + // nodes with DIAssignIDs. The cast_or_null below would also catch this, but + // having a dedicated assert helps make this obvious. + assert((!Node || !Node->isTemporary()) && "Temporary DIAssignIDs are invalid"); + updateDIAssignIDMapping(cast_or_null(Node)); + } + Value::setMetadata(KindID, Node); } Index: llvm/lib/IR/Verifier.cpp =================================================================== --- llvm/lib/IR/Verifier.cpp +++ llvm/lib/IR/Verifier.cpp @@ -69,6 +69,7 @@ #include "llvm/IR/ConstantRange.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/DebugInfo.h" #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DebugLoc.h" #include "llvm/IR/DerivedTypes.h" @@ -4424,6 +4425,20 @@ isa(I) || isa(I) || isa(I); AssertDI(ExpectedInstTy, "!DIAssignID attached to unexpected instruction kind", I, MD); + + // Iterate over the MetadataAsValue uses of the DIAssignID - these should + // only be found as DbgAssignIntrinsic operands. + if (auto *AsValue = MetadataAsValue::getIfExists(Context, MD)) { + for (auto *User : AsValue->users()) { + AssertDI(isa(User), + "!DIAssignID should only be used by llvm.dbg.assign intrinsics", + MD, User); + } + } + // All of the dbg.assign intrinsics should be in the same function as I. + for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(&I)) + AssertDI(DAI->getFunction() == I.getFunction(), + "dbg.assign not in same function as inst", DAI, &I); } void Verifier::visitAnnotationMetadata(MDNode *Annotation) { @@ -5646,6 +5661,24 @@ "invalid llvm.dbg." + Kind + " intrinsic expression", &DII, DII.getRawExpression()); + if (auto *DAI = dyn_cast(&DII)) { + AssertDI(isa(DAI->getAssignId()), + "invalid llvm.dbg.assign intrinsic ID (4th arg)", &DII, + DAI->getAssignId()); + auto *Addr = cast(DII.getArgOperand(4))->getMetadata(); + AssertDI(isa(Addr), + "invalid llvm.dbg.assign intrinsic address (5th arg)", &DII, Addr); + auto *AddrExpr = cast(DII.getArgOperand(5))->getMetadata(); + AssertDI(isa(AddrExpr), + "invalid llvm.dbg.assign intrinsic address expression (6th arg)", + &DII, AddrExpr); + + // All of the linked instructions should be in the same function as DII. + for (Instruction *I : at::getAssignmentInsts(DAI)) + AssertDI(DAI->getFunction() == I->getFunction(), + "inst not in same function as dbg.assign", I, DAI); + } + // Ignore broken !dbg attachments; they're checked elsewhere. if (MDNode *N = DII.getDebugLoc().getAsMDNode()) if (!isa(N)) Index: llvm/test/DebugInfo/Generic/assignment-tracking/roundtrip.ll =================================================================== --- llvm/test/DebugInfo/Generic/assignment-tracking/roundtrip.ll +++ llvm/test/DebugInfo/Generic/assignment-tracking/roundtrip.ll @@ -2,17 +2,81 @@ ; 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() +;; Roundtrip test (text -> bitcode -> text) for DIAssignID metadata and +;; llvm.dbg.assign intrinsics. +;; DIAssignID attachment only. +; CHECK-LABEL: @fun() +; CHECK: %local = alloca i32, align 4, !DIAssignID ![[ID1:[0-9]+]] define dso_local void @fun() !dbg !7 { entry: %local = alloca i32, align 4, !DIAssignID !14 ret void, !dbg !13 } +;; Unlinked llvm.dbg.assign. +; CHECK-DAG: @fun2() +; CHECK: llvm.dbg.assign(metadata i32 undef, metadata ![[VAR2:[0-9]+]], metadata !DIExpression(), metadata ![[ID2:[0-9]+]], metadata i32 undef, metadata !DIExpression()), !dbg ![[DBG2:[0-9]+]] +define dso_local void @fun2() !dbg !15 { +entry: + %local = alloca i32, align 4 + call void @llvm.dbg.assign(metadata i32 undef, metadata !16, metadata !DIExpression(), metadata !18, metadata i32 undef, metadata !DIExpression()), !dbg !17 + ret void, !dbg !17 +} + +;; An llvm.dbg.assign linked to an alloca. +; CHECK-LABEL: @fun3() +; CHECK: %local = alloca i32, align 4, !DIAssignID ![[ID3:[0-9]+]] +; CHECK-NEXT: llvm.dbg.assign(metadata i32 undef, metadata ![[VAR3:[0-9]+]], metadata !DIExpression(), metadata ![[ID3]], metadata i32 undef, metadata !DIExpression()), !dbg ![[DBG3:[0-9]+]] +define dso_local void @fun3() !dbg !19 { +entry: + %local = alloca i32, align 4, !DIAssignID !22 + call void @llvm.dbg.assign(metadata i32 undef, metadata !20, metadata !DIExpression(), metadata !22, metadata i32 undef, metadata !DIExpression()), !dbg !21 + ret void, !dbg !21 +} + +;; Check that using a DIAssignID as an operand before using it as an attachment +;; works (the order of the alloca and dbg.assign has been swapped). +; CHECK-LABEL: @fun4() +; CHECK: llvm.dbg.assign(metadata i32 undef, metadata ![[VAR4:[0-9]+]], metadata !DIExpression(), metadata ![[ID4:[0-9]+]], metadata i32 undef, metadata !DIExpression()), !dbg ![[DBG4:[0-9]+]] +; CHECK-NEXT: %local = alloca i32, align 4, !DIAssignID ![[ID4]] +define dso_local void @fun4() !dbg !23 { +entry: + call void @llvm.dbg.assign(metadata i32 undef, metadata !24, metadata !DIExpression(), metadata !26, metadata i32 undef, metadata !DIExpression()), !dbg !25 + %local = alloca i32, align 4, !DIAssignID !26 + ret void, !dbg !25 +} + +;; Check that the value and address operands print correctly. +;; There are currently no plans to support DIArgLists for the address component. +; CHECK-LABEL: @fun5 +; CHECK: %local = alloca i32, align 4, !DIAssignID ![[ID5:[0-9]+]] +; CHECK-NEXT: llvm.dbg.assign(metadata i32 %v, metadata ![[VAR5:[0-9]+]], metadata !DIExpression(), metadata ![[ID5]], metadata i32* %local, metadata !DIExpression()), !dbg ![[DBG5:[0-9]+]] +; CHECK-NEXT: llvm.dbg.assign(metadata !DIArgList(i32 %v, i32 1), metadata ![[VAR5]], metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_minus, DW_OP_stack_value), metadata ![[ID5]], metadata i32* %local, metadata !DIExpression()), !dbg ![[DBG5]] +define dso_local void @fun5(i32 %v) !dbg !27 { +entry: + %local = alloca i32, align 4, !DIAssignID !30 + call void @llvm.dbg.assign(metadata i32 %v, metadata !28, metadata !DIExpression(), metadata !30, metadata i32* %local, metadata !DIExpression()), !dbg !29 + call void @llvm.dbg.assign(metadata !DIArgList(i32 %v, i32 1), metadata !28, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_minus, DW_OP_stack_value), metadata !30, metadata i32* %local, metadata !DIExpression()), !dbg !29 + ret void +} + +; CHECK-DAG: ![[ID1]] = distinct !DIAssignID() +; CHECK-DAG: ![[ID2]] = distinct !DIAssignID() +; CHECK-DAG: ![[VAR2]] = !DILocalVariable(name: "local2", +; CHECK-DAG: ![[DBG2]] = !DILocation(line: 2 +; CHECK-DAG: ![[ID3]] = distinct !DIAssignID() +; CHECK-DAG: ![[VAR3]] = !DILocalVariable(name: "local3", +; CHECK-DAG: ![[DBG3]] = !DILocation(line: 3, +; CHECK-DAG: ![[ID4]] = distinct !DIAssignID() +; CHECK-DAG: ![[VAR4]] = !DILocalVariable(name: "local4", +; CHECK-DAG: ![[DBG4]] = !DILocation(line: 4, +; CHECK-DAG: ![[ID5]] = distinct !DIAssignID() +; CHECK-DAG: ![[VAR5]] = !DILocalVariable(name: "local5", +; CHECK-DAG: ![[DBG5]] = !DILocation(line: 5, + +declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata) + !llvm.dbg.cu = !{!0} !llvm.module.flags = !{!3, !4, !5} !llvm.ident = !{!6} @@ -29,5 +93,23 @@ !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) +!13 = !DILocation(line: 1, column: 1, scope: !7) !14 = distinct !DIAssignID() +!15 = distinct !DISubprogram(name: "fun2", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!16 = !DILocalVariable(name: "local2", scope: !15, file: !1, line: 2, type: !11) +!17 = !DILocation(line: 2, column: 1, scope: !15) +!18 = distinct !DIAssignID() +!19 = distinct !DISubprogram(name: "fun3", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!20 = !DILocalVariable(name: "local3", scope: !19, file: !1, line: 2, type: !11) +!21 = !DILocation(line: 3, column: 1, scope: !19) +!22 = distinct !DIAssignID() +!23 = distinct !DISubprogram(name: "fun4", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!24 = !DILocalVariable(name: "local4", scope: !23, file: !1, line: 2, type: !11) +!25 = !DILocation(line: 4, column: 1, scope: !23) +!26 = distinct !DIAssignID() +!27 = distinct !DISubprogram(name: "fun5", scope: !1, file: !1, line: 1, type: !31, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!28 = !DILocalVariable(name: "local5", scope: !27, file: !1, line: 2, type: !11) +!29 = !DILocation(line: 5, column: 1, scope: !27) +!30 = distinct !DIAssignID() +!31 = !DISubroutineType(types: !32) +!32 = !{null, !11} Index: llvm/unittests/IR/DebugInfoTest.cpp =================================================================== --- llvm/unittests/IR/DebugInfoTest.cpp +++ llvm/unittests/IR/DebugInfoTest.cpp @@ -329,4 +329,130 @@ EXPECT_EQ(MDExp->getNumElements(), 0u); } +TEST(AssignmentTrackingTest, Utils) { + // Test the assignment tracking utils defined in DebugInfo.h namespace at {}. + // This includes: + // getAssignmentInsts + // getAssignmentMarkers + // RAUW + // deleteAll + // + // The input IR includes two functions, fun1 and fun2. Both contain an alloca + // with a DIAssignID tag. fun1's alloca is linked to two llvm.dbg.assign + // intrinsics, one of which is for an inlined variable and appears before the + // alloca. + + LLVMContext C; + std::unique_ptr M = parseIR(C, R"( + define dso_local void @fun1() !dbg !7 { + entry: + call void @llvm.dbg.assign(metadata i32 undef, metadata !10, metadata !DIExpression(), metadata !12, metadata i32 undef, metadata !DIExpression()), !dbg !13 + %local = alloca i32, align 4, !DIAssignID !12 + call void @llvm.dbg.assign(metadata i32 undef, metadata !16, metadata !DIExpression(), metadata !12, metadata i32 undef, metadata !DIExpression()), !dbg !15 + ret void, !dbg !15 + } + + define dso_local void @fun2() !dbg !17 { + entry: + %local = alloca i32, align 4, !DIAssignID !20 + call void @llvm.dbg.assign(metadata i32 undef, metadata !18, metadata !DIExpression(), metadata !20, metadata i32 undef, metadata !DIExpression()), !dbg !19 + ret void, !dbg !19 + } + + declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata) + + !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: "fun1", 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: "local3", scope: !7, file: !1, line: 2, type: !11) + !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) + !12 = distinct !DIAssignID() + !13 = !DILocation(line: 5, column: 1, scope: !14, inlinedAt: !15) + !14 = distinct !DISubprogram(name: "inline", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) + !15 = !DILocation(line: 3, column: 1, scope: !7) + !16 = !DILocalVariable(name: "local1", scope: !7, file: !1, line: 2, type: !11) + !17 = distinct !DISubprogram(name: "fun2", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) + !18 = !DILocalVariable(name: "local2", scope: !17, file: !1, line: 2, type: !11) + !19 = !DILocation(line: 4, column: 1, scope: !17) + !20 = distinct !DIAssignID() + )"); + + // Check the test IR isn't malformed. + ASSERT_TRUE(M); + + Function &Fun1 = *M->getFunction("fun1"); + Instruction &Alloca = *Fun1.getEntryBlock().getFirstNonPHIOrDbg(); + + // 1. Check the Instruction <-> Intrinsic mappings work in fun1. + // + // Check there are two llvm.dbg.assign intrinsics linked to Alloca. + auto CheckFun1Mapping = [&Alloca]() { + auto Markers = at::getAssignmentMarkers(&Alloca); + EXPECT_TRUE(std::distance(Markers.begin(), Markers.end()) == 2); + // Check those two entries are distinct. + DbgAssignIntrinsic *First = *Markers.begin(); + DbgAssignIntrinsic *Second = *++Markers.begin(); + EXPECT_NE(First, Second); + + // Check that we can get back to Alloca from each llvm.dbg.assign. + for (auto *DAI : Markers) { + auto Insts = at::getAssignmentInsts(DAI); + // Check there is exactly one instruction linked to each intrinsic. Use + // ASSERT_TRUE because we're going to dereference the begin iterator. + ASSERT_TRUE(std::distance(Insts.begin(), Insts.end()) == 1); + EXPECT_FALSE(Insts.empty()); + // Check the linked instruction is Alloca. + Instruction *LinkedInst = *Insts.begin(); + EXPECT_EQ(LinkedInst, &Alloca); + } + }; + CheckFun1Mapping(); + + // 2. Check DIAssignID RAUW replaces attachments and uses. + // + DIAssignID *Old = + cast_or_null(Alloca.getMetadata(LLVMContext::MD_DIAssignID)); + DIAssignID *New = DIAssignID::getDistinct(C); + ASSERT_TRUE(Old && New && New != Old); + at::RAUW(Old, New); + // Check fun1's alloca and intrinsics have been updated and the mapping still + // works. + EXPECT_EQ(New, cast_or_null( + Alloca.getMetadata(LLVMContext::MD_DIAssignID))); + CheckFun1Mapping(); + + // Check that fun2's alloca and intrinsic have not not been updated. + Instruction &Fun2Alloca = + *M->getFunction("fun2")->getEntryBlock().getFirstNonPHIOrDbg(); + DIAssignID *Fun2ID = cast_or_null( + Fun2Alloca.getMetadata(LLVMContext::MD_DIAssignID)); + EXPECT_NE(New, Fun2ID); + auto Fun2Markers = at::getAssignmentMarkers(&Fun2Alloca); + ASSERT_TRUE(std::distance(Fun2Markers.begin(), Fun2Markers.end()) == 1); + auto Fun2Insts = at::getAssignmentInsts(*Fun2Markers.begin()); + ASSERT_TRUE(std::distance(Fun2Insts.begin(), Fun2Insts.end()) == 1); + EXPECT_EQ(*Fun2Insts.begin(), &Fun2Alloca); + + // 3. Check that deleting works and applies only to the target function. + at::deleteAll(&Fun1); + // There should now only be the alloca and ret in fun1. + EXPECT_EQ(Fun1.begin()->size(), 2); + // fun2's alloca should have the same DIAssignID and remain linked to its + // llvm.dbg.assign. + EXPECT_EQ(Fun2ID, cast_or_null( + Fun2Alloca.getMetadata(LLVMContext::MD_DIAssignID))); + EXPECT_FALSE(at::getAssignmentMarkers(&Fun2Alloca).empty()); +} + } // end namespace