Skip to content

Commit 5328c73

Browse files
authored
[DebugInfo] Strip more debug-intrinsic code from local utils (#149037)
SROA and a few other facilities use generic-lambdas and some overloaded functions to deal with both intrinsics and debug-records at the same time. As part of stripping out intrinsic support, delete a swathe of this code from things in the Utils directory. This is a large diff, but is mostly about removing functions that were duplicated during the migration to debug records. I've taken a few opportunities to replace comments about "intrinsics" with "records", and replace generic lambdas with plain lambdas (I believe this makes it more readable). All of this is chipping away at intrinsic-specific code until we get to removing parts of findDbgUsers, which is the final boss -- we can't remove that until almost everything else is gone.
1 parent 9ea27b8 commit 5328c73

File tree

8 files changed

+71
-545
lines changed

8 files changed

+71
-545
lines changed

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class BasicBlock;
3636
class BranchInst;
3737
class CallBase;
3838
class CallInst;
39-
class DbgVariableIntrinsic;
4039
class DIBuilder;
4140
class DomTreeUpdater;
4241
class Function;
@@ -275,44 +274,31 @@ LLVM_ABI CallInst *changeToCall(InvokeInst *II, DomTreeUpdater *DTU = nullptr);
275274
LLVM_ABI void InsertDebugValueAtStoreLoc(DbgVariableRecord *DVR, StoreInst *SI,
276275
DIBuilder &Builder);
277276

278-
/// Creates and inserts an llvm.dbg.value intrinsic before a store
279-
/// that has an associated llvm.dbg.value intrinsic.
280-
LLVM_ABI void InsertDebugValueAtStoreLoc(DbgVariableIntrinsic *DII,
281-
StoreInst *SI, DIBuilder &Builder);
282-
283-
/// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
284-
/// that has an associated llvm.dbg.declare intrinsic.
285-
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
286-
StoreInst *SI,
287-
DIBuilder &Builder);
277+
/// Inserts a dbg.value record before a store to an alloca'd value
278+
/// that has an associated dbg.declare record.
288279
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
289280
StoreInst *SI,
290281
DIBuilder &Builder);
291282

292-
/// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
293-
/// that has an associated llvm.dbg.declare intrinsic.
294-
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
295-
LoadInst *LI, DIBuilder &Builder);
283+
/// Inserts a dbg.value record before a load of an alloca'd value
284+
/// that has an associated dbg.declare record.
296285
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
297286
LoadInst *LI, DIBuilder &Builder);
298287

299-
/// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
300-
/// llvm.dbg.declare intrinsic.
301-
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
302-
PHINode *LI, DIBuilder &Builder);
288+
/// Inserts a dbg.value record after a phi that has an associated
289+
/// llvm.dbg.declare record.
303290
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
304291
PHINode *LI, DIBuilder &Builder);
305292

306-
/// Lowers llvm.dbg.declare intrinsics into appropriate set of
307-
/// llvm.dbg.value intrinsics.
293+
/// Lowers dbg.declare records into appropriate set of dbg.value records.
308294
LLVM_ABI bool LowerDbgDeclare(Function &F);
309295

310296
/// Propagate dbg.value intrinsics through the newly inserted PHIs.
311297
LLVM_ABI void
312298
insertDebugValuesForPHIs(BasicBlock *BB,
313299
SmallVectorImpl<PHINode *> &InsertedPHIs);
314300

315-
/// Replaces llvm.dbg.declare instruction when the address it
301+
/// Replaces dbg.declare record when the address it
316302
/// describes is replaced with a new value. If Deref is true, an
317303
/// additional DW_OP_deref is prepended to the expression. If Offset
318304
/// is non-zero, a constant displacement is added to the expression
@@ -321,10 +307,10 @@ LLVM_ABI bool replaceDbgDeclare(Value *Address, Value *NewAddress,
321307
DIBuilder &Builder, uint8_t DIExprFlags,
322308
int Offset);
323309

324-
/// Replaces multiple llvm.dbg.value instructions when the alloca it describes
310+
/// Replaces multiple dbg.value records when the alloca it describes
325311
/// is replaced with a new value. If Offset is non-zero, a constant displacement
326312
/// is added to the expression (after the mandatory Deref). Offset can be
327-
/// negative. New llvm.dbg.value instructions are inserted at the locations of
313+
/// negative. New dbg.value records are inserted at the locations of
328314
/// the instructions they replace.
329315
LLVM_ABI void replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
330316
DIBuilder &Builder, int Offset = 0);

llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
namespace llvm {
2525
class DominatorTree;
26-
class DbgVariableIntrinsic;
2726
class IntrinsicInst;
2827
class PostDominatorTree;
2928
class AllocaInst;
@@ -53,8 +52,6 @@ struct AllocaInfo {
5352
AllocaInst *AI;
5453
SmallVector<IntrinsicInst *, 2> LifetimeStart;
5554
SmallVector<IntrinsicInst *, 2> LifetimeEnd;
56-
SmallVector<DbgVariableIntrinsic *, 2> DbgVariableIntrinsics;
57-
// Non-intrinsic records of variable locations.
5855
SmallVector<DbgVariableRecord *, 2> DbgVariableRecords;
5956
};
6057

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,9 +3644,6 @@ Instruction *InstCombinerImpl::visitAllocSite(Instruction &MI) {
36443644
ConstantInt::get(Type::getInt1Ty(C->getContext()),
36453645
C->isFalseWhenEqual()));
36463646
} else if (auto *SI = dyn_cast<StoreInst>(I)) {
3647-
for (auto *DVI : DVIs)
3648-
if (DVI->isAddressOfVariable())
3649-
ConvertDebugDeclareToDebugValue(DVI, SI, *DIB);
36503647
for (auto *DVR : DVRs)
36513648
if (DVR->isAddressOfVariable())
36523649
ConvertDebugDeclareToDebugValue(DVR, SI, *DIB);

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,11 @@ calculateFragment(DILocalVariable *Variable,
315315
return UseFrag;
316316
}
317317

318-
static DebugVariable getAggregateVariable(DbgVariableIntrinsic *DVI) {
319-
return DebugVariable(DVI->getVariable(), std::nullopt,
320-
DVI->getDebugLoc().getInlinedAt());
321-
}
322318
static DebugVariable getAggregateVariable(DbgVariableRecord *DVR) {
323319
return DebugVariable(DVR->getVariable(), std::nullopt,
324320
DVR->getDebugLoc().getInlinedAt());
325321
}
326322

327-
/// Helpers for handling new and old debug info modes in migrateDebugInfo.
328-
/// These overloads unwrap a DbgInstPtr {Instruction* | DbgRecord*} union based
329-
/// on the \p Unused parameter type.
330323
DbgVariableRecord *UnwrapDbgInstPtr(DbgInstPtr P, DbgVariableRecord *Unused) {
331324
(void)Unused;
332325
return static_cast<DbgVariableRecord *>(cast<DbgRecord *>(P));
@@ -376,9 +369,6 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
376369
/// Map of aggregate variables to their fragment associated with OldAlloca.
377370
DenseMap<DebugVariable, std::optional<DIExpression::FragmentInfo>>
378371
BaseFragments;
379-
for (auto *DAI : at::getAssignmentMarkers(OldAlloca))
380-
BaseFragments[getAggregateVariable(DAI)] =
381-
DAI->getExpression()->getFragmentInfo();
382372
for (auto *DVR : at::getDVRAssignmentMarkers(OldAlloca))
383373
BaseFragments[getAggregateVariable(DVR)] =
384374
DVR->getExpression()->getFragmentInfo();
@@ -391,7 +381,7 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
391381
DIBuilder DIB(*OldInst->getModule(), /*AllowUnresolved*/ false);
392382
assert(OldAlloca->isStaticAlloca());
393383

394-
auto MigrateDbgAssign = [&](auto *DbgAssign) {
384+
auto MigrateDbgAssign = [&](DbgVariableRecord *DbgAssign) {
395385
LLVM_DEBUG(dbgs() << " existing dbg.assign is: " << *DbgAssign
396386
<< "\n");
397387
auto *Expr = DbgAssign->getExpression();
@@ -486,7 +476,6 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
486476
LLVM_DEBUG(dbgs() << "Created new assign: " << *NewAssign << "\n");
487477
};
488478

489-
for_each(MarkerRange, MigrateDbgAssign);
490479
for_each(DVRAssignMarkerRange, MigrateDbgAssign);
491480
}
492481

@@ -5119,36 +5108,13 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
51195108
}
51205109

51215110
// There isn't a shared interface to get the "address" parts out of a
5122-
// dbg.declare and dbg.assign, so provide some wrappers now for
5123-
// both debug intrinsics and records.
5124-
const Value *getAddress(const DbgVariableIntrinsic *DVI) {
5125-
if (const auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI))
5126-
return DAI->getAddress();
5127-
return cast<DbgDeclareInst>(DVI)->getAddress();
5128-
}
5129-
5130-
const Value *getAddress(const DbgVariableRecord *DVR) {
5131-
return DVR->getAddress();
5132-
}
5133-
5134-
bool isKillAddress(const DbgVariableIntrinsic *DVI) {
5135-
if (const auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI))
5136-
return DAI->isKillAddress();
5137-
return cast<DbgDeclareInst>(DVI)->isKillLocation();
5138-
}
5139-
5111+
// dbg.declare and dbg.assign, so provide some wrappers.
51405112
bool isKillAddress(const DbgVariableRecord *DVR) {
51415113
if (DVR->getType() == DbgVariableRecord::LocationType::Assign)
51425114
return DVR->isKillAddress();
51435115
return DVR->isKillLocation();
51445116
}
51455117

5146-
const DIExpression *getAddressExpression(const DbgVariableIntrinsic *DVI) {
5147-
if (const auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI))
5148-
return DAI->getAddressExpression();
5149-
return cast<DbgDeclareInst>(DVI)->getExpression();
5150-
}
5151-
51525118
const DIExpression *getAddressExpression(const DbgVariableRecord *DVR) {
51535119
if (DVR->getType() == DbgVariableRecord::LocationType::Assign)
51545120
return DVR->getAddressExpression();
@@ -5236,66 +5202,6 @@ static DIExpression *createOrReplaceFragment(const DIExpression *Expr,
52365202
return DIExpression::get(Expr->getContext(), Ops);
52375203
}
52385204

5239-
/// Insert a new dbg.declare.
5240-
/// \p Orig Original to copy debug loc and variable from.
5241-
/// \p NewAddr Location's new base address.
5242-
/// \p NewAddrExpr New expression to apply to address.
5243-
/// \p BeforeInst Insert position.
5244-
/// \p NewFragment New fragment (absolute, non-relative).
5245-
/// \p BitExtractAdjustment Offset to apply to any extract_bits op.
5246-
static void
5247-
insertNewDbgInst(DIBuilder &DIB, DbgDeclareInst *Orig, AllocaInst *NewAddr,
5248-
DIExpression *NewAddrExpr, Instruction *BeforeInst,
5249-
std::optional<DIExpression::FragmentInfo> NewFragment,
5250-
int64_t BitExtractAdjustment) {
5251-
if (NewFragment)
5252-
NewAddrExpr = createOrReplaceFragment(NewAddrExpr, *NewFragment,
5253-
BitExtractAdjustment);
5254-
if (!NewAddrExpr)
5255-
return;
5256-
5257-
DIB.insertDeclare(NewAddr, Orig->getVariable(), NewAddrExpr,
5258-
Orig->getDebugLoc(), BeforeInst->getIterator());
5259-
}
5260-
5261-
/// Insert a new dbg.assign.
5262-
/// \p Orig Original to copy debug loc, variable, value and value expression
5263-
/// from.
5264-
/// \p NewAddr Location's new base address.
5265-
/// \p NewAddrExpr New expression to apply to address.
5266-
/// \p BeforeInst Insert position.
5267-
/// \p NewFragment New fragment (absolute, non-relative).
5268-
/// \p BitExtractAdjustment Offset to apply to any extract_bits op.
5269-
static void
5270-
insertNewDbgInst(DIBuilder &DIB, DbgAssignIntrinsic *Orig, AllocaInst *NewAddr,
5271-
DIExpression *NewAddrExpr, Instruction *BeforeInst,
5272-
std::optional<DIExpression::FragmentInfo> NewFragment,
5273-
int64_t BitExtractAdjustment) {
5274-
// DIBuilder::insertDbgAssign will insert the #dbg_assign after NewAddr.
5275-
(void)BeforeInst;
5276-
5277-
// A dbg.assign puts fragment info in the value expression only. The address
5278-
// expression has already been built: NewAddrExpr.
5279-
DIExpression *NewFragmentExpr = Orig->getExpression();
5280-
if (NewFragment)
5281-
NewFragmentExpr = createOrReplaceFragment(NewFragmentExpr, *NewFragment,
5282-
BitExtractAdjustment);
5283-
if (!NewFragmentExpr)
5284-
return;
5285-
5286-
// Apply a DIAssignID to the store if it doesn't already have it.
5287-
if (!NewAddr->hasMetadata(LLVMContext::MD_DIAssignID)) {
5288-
NewAddr->setMetadata(LLVMContext::MD_DIAssignID,
5289-
DIAssignID::getDistinct(NewAddr->getContext()));
5290-
}
5291-
5292-
Instruction *NewAssign = cast<Instruction *>(DIB.insertDbgAssign(
5293-
NewAddr, Orig->getValue(), Orig->getVariable(), NewFragmentExpr, NewAddr,
5294-
NewAddrExpr, Orig->getDebugLoc()));
5295-
LLVM_DEBUG(dbgs() << "Created new assign intrinsic: " << *NewAssign << "\n");
5296-
(void)NewAssign;
5297-
}
5298-
52995205
/// Insert a new DbgRecord.
53005206
/// \p Orig Original to copy record type, debug loc and variable from, and
53015207
/// additionally value and value expression for dbg_assign records.
@@ -5457,12 +5363,12 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
54575363

54585364
// Migrate debug information from the old alloca to the new alloca(s)
54595365
// and the individual partitions.
5460-
auto MigrateOne = [&](auto *DbgVariable) {
5366+
auto MigrateOne = [&](DbgVariableRecord *DbgVariable) {
54615367
// Can't overlap with undef memory.
54625368
if (isKillAddress(DbgVariable))
54635369
return;
54645370

5465-
const Value *DbgPtr = getAddress(DbgVariable);
5371+
const Value *DbgPtr = DbgVariable->getAddress();
54665372
DIExpression::FragmentInfo VarFrag =
54675373
DbgVariable->getFragmentOrEntireVariable();
54685374
// Get the address expression constant offset if one exists and the ops
@@ -5543,7 +5449,6 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
55435449
if (SameVariableFragment(OldDII, DbgVariable))
55445450
OldDII->eraseFromParent();
55455451
};
5546-
for_each(findDbgDeclares(Fragment.Alloca), RemoveOne);
55475452
for_each(findDVRDeclares(Fragment.Alloca), RemoveOne);
55485453
for_each(findDVRValues(Fragment.Alloca), RemoveOne);
55495454
insertNewDbgInst(DIB, DbgVariable, Fragment.Alloca, NewExpr, &AI,
@@ -5553,10 +5458,8 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
55535458

55545459
// Migrate debug information from the old alloca to the new alloca(s)
55555460
// and the individual partitions.
5556-
for_each(findDbgDeclares(&AI), MigrateOne);
55575461
for_each(findDVRDeclares(&AI), MigrateOne);
55585462
for_each(findDVRValues(&AI), MigrateOne);
5559-
for_each(at::getAssignmentMarkers(&AI), MigrateOne);
55605463
for_each(at::getDVRAssignmentMarkers(&AI), MigrateOne);
55615464

55625465
return Changed;
@@ -5777,8 +5680,6 @@ bool SROA::deleteDeadInstructions(
57775680
// not be able to find it.
57785681
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
57795682
DeletedAllocas.insert(AI);
5780-
for (DbgDeclareInst *OldDII : findDbgDeclares(AI))
5781-
OldDII->eraseFromParent();
57825683
for (DbgVariableRecord *OldDII : findDVRDeclares(AI))
57835684
OldDII->eraseFromParent();
57845685
}

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,8 @@ void PruningFunctionCloner::CloneBlock(
576576
}
577577

578578
// Eagerly remap operands to the newly cloned instruction, except for PHI
579-
// nodes for which we defer processing until we update the CFG. Also defer
580-
// debug intrinsic processing because they may contain use-before-defs.
581-
if (!isa<PHINode>(NewInst) && !isa<DbgVariableIntrinsic>(NewInst)) {
579+
// nodes for which we defer processing until we update the CFG.
580+
if (!isa<PHINode>(NewInst)) {
582581
RemapInstruction(NewInst, VMap,
583582
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
584583

@@ -733,15 +732,6 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
733732
StartingInst = &StartingBB->front();
734733
}
735734

736-
// Collect debug intrinsics for remapping later.
737-
SmallVector<const DbgVariableIntrinsic *, 8> DbgIntrinsics;
738-
for (const auto &BB : *OldFunc) {
739-
for (const auto &I : BB) {
740-
if (const auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
741-
DbgIntrinsics.push_back(DVI);
742-
}
743-
}
744-
745735
// Clone the entry block, and anything recursively reachable from it.
746736
std::vector<const BasicBlock *> CloneWorklist;
747737
PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
@@ -899,21 +889,11 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
899889
// Restore attributes.
900890
NewFunc->setAttributes(Attrs);
901891

902-
// Remap debug intrinsic operands now that all values have been mapped.
903-
// Doing this now (late) preserves use-before-defs in debug intrinsics. If
892+
// Remap debug records operands now that all values have been mapped.
893+
// Doing this now (late) preserves use-before-defs in debug records. If
904894
// we didn't do this, ValueAsMetadata(use-before-def) operands would be
905895
// replaced by empty metadata. This would signal later cleanup passes to
906-
// remove the debug intrinsics, potentially causing incorrect locations.
907-
for (const auto *DVI : DbgIntrinsics) {
908-
if (DbgVariableIntrinsic *NewDVI =
909-
cast_or_null<DbgVariableIntrinsic>(VMap.lookup(DVI)))
910-
RemapInstruction(NewDVI, VMap,
911-
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
912-
TypeMapper, Materializer);
913-
}
914-
915-
// Do the same for DbgVariableRecords, touching all the instructions in the
916-
// cloned range of blocks.
896+
// remove the debug records, potentially causing incorrect locations.
917897
Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
918898
for (BasicBlock &BB : make_range(Begin, NewFunc->end())) {
919899
for (Instruction &I : BB) {

0 commit comments

Comments
 (0)