Skip to content

Commit 9997e03

Browse files
authored
[RemoveDIs] Update DIBuilder to conditionally insert DbgRecords (#84739)
Have DIBuilder conditionally insert either debug intrinsics or DbgRecord depending on the module's IsNewDbgInfoFormat flag. The insertion methods now return a `DbgInstPtr` (a `PointerUnion<Instruction *, DbgRecord *>`). Add a unittest for both modes (I couldn't find an existing test testing insertion behaviours specifically). This patch changes the existing assumption that DbgRecords are only ever inserted if there's an instruction to insert-before because clang currently inserts debug intrinsics while CodeGening (like any other instruction) meaning it'll try inserting to the end of a block without a terminator. We already have machinery in place to maintain the DbgRecords when a terminator is removed - these become "trailing DbgRecords" which are re-attached when a new instruction is inserted. All I've done is allow this state to occur while inserting DbgRecords too, i.e., it's not only removing terminators that causes this valid transient state, but inserting DbgRecords into incomplete blocks too. The C API will be updated in follow up patches. --- Note: this doesn't mean clang is emitting DbgRecords yet, because the modules it creates are still always in the old debug mode. That will come in a future patch.
1 parent ce1fd92 commit 9997e03

File tree

9 files changed

+358
-187
lines changed

9 files changed

+358
-187
lines changed

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ namespace llvm {
3838
class Module;
3939
class Value;
4040
class DbgAssignIntrinsic;
41+
class DbgRecord;
42+
43+
using DbgInstPtr = PointerUnion<Instruction *, DbgRecord *>;
4144

4245
class DIBuilder {
4346
Module &M;
@@ -90,13 +93,17 @@ namespace llvm {
9093
void trackIfUnresolved(MDNode *N);
9194

9295
/// Internal helper for insertDeclare.
93-
Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
94-
DIExpression *Expr, const DILocation *DL,
95-
BasicBlock *InsertBB, Instruction *InsertBefore);
96+
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
97+
DIExpression *Expr, const DILocation *DL,
98+
BasicBlock *InsertBB, Instruction *InsertBefore);
9699

97100
/// Internal helper for insertLabel.
98-
Instruction *insertLabel(DILabel *LabelInfo, const DILocation *DL,
99-
BasicBlock *InsertBB, Instruction *InsertBefore);
101+
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
102+
BasicBlock *InsertBB, Instruction *InsertBefore);
103+
104+
/// Internal helper. Track metadata if untracked and insert \p DPV.
105+
void insertDPValue(DPValue *DPV, BasicBlock *InsertBB,
106+
Instruction *InsertBefore, bool InsertAtHead = false);
100107

101108
/// Internal helper with common code used by insertDbg{Value,Addr}Intrinsic.
102109
Instruction *insertDbgIntrinsic(llvm::Function *Intrinsic, llvm::Value *Val,
@@ -106,10 +113,11 @@ namespace llvm {
106113
Instruction *InsertBefore);
107114

108115
/// Internal helper for insertDbgValueIntrinsic.
109-
Instruction *
110-
insertDbgValueIntrinsic(llvm::Value *Val, DILocalVariable *VarInfo,
111-
DIExpression *Expr, const DILocation *DL,
112-
BasicBlock *InsertBB, Instruction *InsertBefore);
116+
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
117+
DILocalVariable *VarInfo,
118+
DIExpression *Expr, const DILocation *DL,
119+
BasicBlock *InsertBB,
120+
Instruction *InsertBefore);
113121

114122
public:
115123
/// Construct a builder for a module.
@@ -921,9 +929,9 @@ namespace llvm {
921929
/// \param Expr A complex location expression.
922930
/// \param DL Debug info location.
923931
/// \param InsertAtEnd Location for the new intrinsic.
924-
Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
925-
DIExpression *Expr, const DILocation *DL,
926-
BasicBlock *InsertAtEnd);
932+
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
933+
DIExpression *Expr, const DILocation *DL,
934+
BasicBlock *InsertAtEnd);
927935

928936
/// Insert a new llvm.dbg.assign intrinsic call.
929937
/// \param LinkedInstr Instruction with a DIAssignID to link with the new
@@ -939,59 +947,56 @@ namespace llvm {
939947
/// \param DL Debug info location, usually: (line: 0,
940948
/// column: 0, scope: var-decl-scope). See
941949
/// getDebugValueLoc.
942-
DbgAssignIntrinsic *insertDbgAssign(Instruction *LinkedInstr, Value *Val,
943-
DILocalVariable *SrcVar,
944-
DIExpression *ValExpr, Value *Addr,
945-
DIExpression *AddrExpr,
946-
const DILocation *DL);
950+
DbgInstPtr insertDbgAssign(Instruction *LinkedInstr, Value *Val,
951+
DILocalVariable *SrcVar, DIExpression *ValExpr,
952+
Value *Addr, DIExpression *AddrExpr,
953+
const DILocation *DL);
947954

948955
/// Insert a new llvm.dbg.declare intrinsic call.
949956
/// \param Storage llvm::Value of the variable
950957
/// \param VarInfo Variable's debug info descriptor.
951958
/// \param Expr A complex location expression.
952959
/// \param DL Debug info location.
953960
/// \param InsertBefore Location for the new intrinsic.
954-
Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
955-
DIExpression *Expr, const DILocation *DL,
956-
Instruction *InsertBefore);
961+
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
962+
DIExpression *Expr, const DILocation *DL,
963+
Instruction *InsertBefore);
957964

958965
/// Insert a new llvm.dbg.label intrinsic call.
959966
/// \param LabelInfo Label's debug info descriptor.
960967
/// \param DL Debug info location.
961968
/// \param InsertBefore Location for the new intrinsic.
962-
Instruction *insertLabel(DILabel *LabelInfo, const DILocation *DL,
963-
Instruction *InsertBefore);
969+
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
970+
Instruction *InsertBefore);
964971

965972
/// Insert a new llvm.dbg.label intrinsic call.
966973
/// \param LabelInfo Label's debug info descriptor.
967974
/// \param DL Debug info location.
968975
/// \param InsertAtEnd Location for the new intrinsic.
969-
Instruction *insertLabel(DILabel *LabelInfo, const DILocation *DL,
970-
BasicBlock *InsertAtEnd);
976+
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
977+
BasicBlock *InsertAtEnd);
971978

972979
/// Insert a new llvm.dbg.value intrinsic call.
973980
/// \param Val llvm::Value of the variable
974981
/// \param VarInfo Variable's debug info descriptor.
975982
/// \param Expr A complex location expression.
976983
/// \param DL Debug info location.
977984
/// \param InsertAtEnd Location for the new intrinsic.
978-
Instruction *insertDbgValueIntrinsic(llvm::Value *Val,
979-
DILocalVariable *VarInfo,
980-
DIExpression *Expr,
981-
const DILocation *DL,
982-
BasicBlock *InsertAtEnd);
985+
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
986+
DILocalVariable *VarInfo,
987+
DIExpression *Expr, const DILocation *DL,
988+
BasicBlock *InsertAtEnd);
983989

984990
/// Insert a new llvm.dbg.value intrinsic call.
985991
/// \param Val llvm::Value of the variable
986992
/// \param VarInfo Variable's debug info descriptor.
987993
/// \param Expr A complex location expression.
988994
/// \param DL Debug info location.
989995
/// \param InsertBefore Location for the new intrinsic.
990-
Instruction *insertDbgValueIntrinsic(llvm::Value *Val,
991-
DILocalVariable *VarInfo,
992-
DIExpression *Expr,
993-
const DILocation *DL,
994-
Instruction *InsertBefore);
996+
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
997+
DILocalVariable *VarInfo,
998+
DIExpression *Expr, const DILocation *DL,
999+
Instruction *InsertBefore);
9951000

9961001
/// Replace the vtable holder in the given type.
9971002
///

llvm/lib/IR/BasicBlock.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,6 @@ void BasicBlock::spliceDebugInfoEmptyBlock(BasicBlock::iterator Dest,
754754
// occur when a block is optimised away and the terminator has been moved
755755
// somewhere else.
756756
if (Src->empty()) {
757-
assert(Dest != end() &&
758-
"Transferring trailing DPValues to another trailing position");
759757
DPMarker *SrcTrailingDPValues = Src->getTrailingDPValues();
760758
if (!SrcTrailingDPValues)
761759
return;
@@ -1040,15 +1038,10 @@ void BasicBlock::insertDPValueAfter(DbgRecord *DPV, Instruction *I) {
10401038

10411039
void BasicBlock::insertDPValueBefore(DbgRecord *DPV,
10421040
InstListType::iterator Where) {
1043-
// We should never directly insert at the end of the block, new DPValues
1044-
// shouldn't be generated at times when there's no terminator.
1045-
assert(Where != end());
1046-
assert(Where->getParent() == this);
1047-
if (!Where->DbgMarker)
1048-
createMarker(Where);
1041+
assert(Where == end() || Where->getParent() == this);
10491042
bool InsertAtHead = Where.getHeadBit();
1050-
createMarker(&*Where);
1051-
Where->DbgMarker->insertDPValue(DPV, InsertAtHead);
1043+
DPMarker *M = createMarker(Where);
1044+
M->insertDPValue(DPV, InsertAtHead);
10521045
}
10531046

10541047
DPMarker *BasicBlock::getNextMarker(Instruction *I) {

llvm/lib/IR/DIBuilder.cpp

Lines changed: 92 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -925,35 +925,47 @@ DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
925925
File, Line, Col);
926926
}
927927

928-
Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
929-
DIExpression *Expr, const DILocation *DL,
930-
Instruction *InsertBefore) {
928+
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
929+
DIExpression *Expr, const DILocation *DL,
930+
Instruction *InsertBefore) {
931931
return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(),
932932
InsertBefore);
933933
}
934934

935-
Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
936-
DIExpression *Expr, const DILocation *DL,
937-
BasicBlock *InsertAtEnd) {
935+
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
936+
DIExpression *Expr, const DILocation *DL,
937+
BasicBlock *InsertAtEnd) {
938938
// If this block already has a terminator then insert this intrinsic before
939939
// the terminator. Otherwise, put it at the end of the block.
940940
Instruction *InsertBefore = InsertAtEnd->getTerminator();
941941
return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore);
942942
}
943943

944-
DbgAssignIntrinsic *
945-
DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
946-
DILocalVariable *SrcVar, DIExpression *ValExpr,
947-
Value *Addr, DIExpression *AddrExpr,
948-
const DILocation *DL) {
944+
DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
945+
DILocalVariable *SrcVar,
946+
DIExpression *ValExpr, Value *Addr,
947+
DIExpression *AddrExpr,
948+
const DILocation *DL) {
949+
auto *Link = cast_or_null<DIAssignID>(
950+
LinkedInstr->getMetadata(LLVMContext::MD_DIAssignID));
951+
assert(Link && "Linked instruction must have DIAssign metadata attached");
952+
953+
if (M.IsNewDbgInfoFormat) {
954+
DPValue *DPV = DPValue::createDPVAssign(Val, SrcVar, ValExpr, Link, Addr,
955+
AddrExpr, DL);
956+
BasicBlock *InsertBB = LinkedInstr->getParent();
957+
// Insert after LinkedInstr.
958+
BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator());
959+
Instruction *InsertBefore = NextIt == InsertBB->end() ? nullptr : &*NextIt;
960+
insertDPValue(DPV, InsertBB, InsertBefore, true);
961+
return DPV;
962+
}
963+
949964
LLVMContext &Ctx = LinkedInstr->getContext();
950965
Module *M = LinkedInstr->getModule();
951966
if (!AssignFn)
952967
AssignFn = Intrinsic::getDeclaration(M, Intrinsic::dbg_assign);
953968

954-
auto *Link = LinkedInstr->getMetadata(LLVMContext::MD_DIAssignID);
955-
assert(Link && "Linked instruction must have DIAssign metadata attached");
956-
957969
std::array<Value *, 6> Args = {
958970
MetadataAsValue::get(Ctx, ValueAsMetadata::get(Val)),
959971
MetadataAsValue::get(Ctx, SrcVar),
@@ -971,35 +983,36 @@ DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
971983
return DVI;
972984
}
973985

974-
Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
975-
Instruction *InsertBefore) {
986+
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
987+
Instruction *InsertBefore) {
976988
return insertLabel(LabelInfo, DL,
977989
InsertBefore ? InsertBefore->getParent() : nullptr,
978990
InsertBefore);
979991
}
980992

981-
Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
982-
BasicBlock *InsertAtEnd) {
993+
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
994+
BasicBlock *InsertAtEnd) {
983995
return insertLabel(LabelInfo, DL, InsertAtEnd, nullptr);
984996
}
985997

986-
Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
987-
DILocalVariable *VarInfo,
988-
DIExpression *Expr,
989-
const DILocation *DL,
990-
Instruction *InsertBefore) {
991-
Instruction *DVI = insertDbgValueIntrinsic(
998+
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V,
999+
DILocalVariable *VarInfo,
1000+
DIExpression *Expr,
1001+
const DILocation *DL,
1002+
Instruction *InsertBefore) {
1003+
DbgInstPtr DVI = insertDbgValueIntrinsic(
9921004
V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
9931005
InsertBefore);
994-
cast<CallInst>(DVI)->setTailCall();
1006+
if (DVI.is<Instruction *>())
1007+
cast<CallInst>(DVI.get<Instruction *>())->setTailCall();
9951008
return DVI;
9961009
}
9971010

998-
Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
999-
DILocalVariable *VarInfo,
1000-
DIExpression *Expr,
1001-
const DILocation *DL,
1002-
BasicBlock *InsertAtEnd) {
1011+
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V,
1012+
DILocalVariable *VarInfo,
1013+
DIExpression *Expr,
1014+
const DILocation *DL,
1015+
BasicBlock *InsertAtEnd) {
10031016
return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr);
10041017
}
10051018

@@ -1023,24 +1036,37 @@ static Function *getDeclareIntrin(Module &M) {
10231036
return Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
10241037
}
10251038

1026-
Instruction *DIBuilder::insertDbgValueIntrinsic(
1039+
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(
10271040
llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr,
10281041
const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) {
1042+
if (M.IsNewDbgInfoFormat) {
1043+
DPValue *DPV = DPValue::createDPValue(Val, VarInfo, Expr, DL);
1044+
insertDPValue(DPV, InsertBB, InsertBefore);
1045+
return DPV;
1046+
}
1047+
10291048
if (!ValueFn)
10301049
ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
10311050
return insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertBB,
10321051
InsertBefore);
10331052
}
10341053

1035-
Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
1036-
DIExpression *Expr, const DILocation *DL,
1037-
BasicBlock *InsertBB,
1038-
Instruction *InsertBefore) {
1054+
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
1055+
DIExpression *Expr, const DILocation *DL,
1056+
BasicBlock *InsertBB,
1057+
Instruction *InsertBefore) {
10391058
assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
10401059
assert(DL && "Expected debug loc");
10411060
assert(DL->getScope()->getSubprogram() ==
10421061
VarInfo->getScope()->getSubprogram() &&
10431062
"Expected matching subprograms");
1063+
1064+
if (M.IsNewDbgInfoFormat) {
1065+
DPValue *DPV = DPValue::createDPVDeclare(Storage, VarInfo, Expr, DL);
1066+
insertDPValue(DPV, InsertBB, InsertBefore);
1067+
return DPV;
1068+
}
1069+
10441070
if (!DeclareFn)
10451071
DeclareFn = getDeclareIntrin(M);
10461072

@@ -1055,6 +1081,23 @@ Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10551081
return B.CreateCall(DeclareFn, Args);
10561082
}
10571083

1084+
void DIBuilder::insertDPValue(DPValue *DPV, BasicBlock *InsertBB,
1085+
Instruction *InsertBefore, bool InsertAtHead) {
1086+
assert(InsertBefore || InsertBB);
1087+
trackIfUnresolved(DPV->getVariable());
1088+
trackIfUnresolved(DPV->getExpression());
1089+
if (DPV->isDbgAssign())
1090+
trackIfUnresolved(DPV->getAddressExpression());
1091+
1092+
BasicBlock::iterator InsertPt;
1093+
if (InsertBB && InsertBefore)
1094+
InsertPt = InsertBefore->getIterator();
1095+
else if (InsertBB)
1096+
InsertPt = InsertBB->end();
1097+
InsertPt.setHeadBit(InsertAtHead);
1098+
InsertBB->insertDPValueBefore(DPV, InsertPt);
1099+
}
1100+
10581101
Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
10591102
Value *V, DILocalVariable *VarInfo,
10601103
DIExpression *Expr,
@@ -1081,18 +1124,28 @@ Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
10811124
return B.CreateCall(IntrinsicFn, Args);
10821125
}
10831126

1084-
Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1085-
BasicBlock *InsertBB,
1086-
Instruction *InsertBefore) {
1127+
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1128+
BasicBlock *InsertBB,
1129+
Instruction *InsertBefore) {
10871130
assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
10881131
assert(DL && "Expected debug loc");
10891132
assert(DL->getScope()->getSubprogram() ==
10901133
LabelInfo->getScope()->getSubprogram() &&
10911134
"Expected matching subprograms");
1135+
1136+
trackIfUnresolved(LabelInfo);
1137+
if (M.IsNewDbgInfoFormat) {
1138+
DPLabel *DPL = new DPLabel(LabelInfo, DL);
1139+
if (InsertBB && InsertBefore)
1140+
InsertBB->insertDPValueBefore(DPL, InsertBefore->getIterator());
1141+
else if (InsertBB)
1142+
InsertBB->insertDPValueBefore(DPL, InsertBB->end());
1143+
return DPL;
1144+
}
1145+
10921146
if (!LabelFn)
10931147
LabelFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_label);
10941148

1095-
trackIfUnresolved(LabelInfo);
10961149
Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)};
10971150

10981151
IRBuilder<> B(DL->getContext());

0 commit comments

Comments
 (0)