Skip to content

Commit 8d23754

Browse files
committed
Revert "[DebugInfo] Strip more debug-intrinsic code from local utils (llvm#149037)"
This reverts commit 5328c73.
1 parent 3cadca3 commit 8d23754

File tree

8 files changed

+407
-90
lines changed

8 files changed

+407
-90
lines changed

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

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

277-
/// Inserts a dbg.value record before a store to an alloca'd value
278-
/// that has an associated dbg.declare record.
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);
279288
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
280289
StoreInst *SI,
281290
DIBuilder &Builder);
282291

283-
/// Inserts a dbg.value record before a load of an alloca'd value
284-
/// that has an associated dbg.declare record.
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);
285296
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
286297
LoadInst *LI, DIBuilder &Builder);
287298

288-
/// Inserts a dbg.value record after a phi that has an associated
289-
/// llvm.dbg.declare record.
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);
290303
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
291304
PHINode *LI, DIBuilder &Builder);
292305

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

296310
/// Propagate dbg.value intrinsics through the newly inserted PHIs.
297311
LLVM_ABI void
298312
insertDebugValuesForPHIs(BasicBlock *BB,
299313
SmallVectorImpl<PHINode *> &InsertedPHIs);
300314

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

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

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

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

2424
namespace llvm {
2525
class DominatorTree;
26+
class DbgVariableIntrinsic;
2627
class IntrinsicInst;
2728
class PostDominatorTree;
2829
class AllocaInst;
@@ -52,6 +53,8 @@ struct AllocaInfo {
5253
AllocaInst *AI;
5354
SmallVector<IntrinsicInst *, 2> LifetimeStart;
5455
SmallVector<IntrinsicInst *, 2> LifetimeEnd;
56+
SmallVector<DbgVariableIntrinsic *, 2> DbgVariableIntrinsics;
57+
// Non-intrinsic records of variable locations.
5558
SmallVector<DbgVariableRecord *, 2> DbgVariableRecords;
5659
};
5760

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,6 +3644,9 @@ 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);
36473650
for (auto *DVR : DVRs)
36483651
if (DVR->isAddressOfVariable())
36493652
ConvertDebugDeclareToDebugValue(DVR, SI, *DIB);

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,18 @@ 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+
}
318322
static DebugVariable getAggregateVariable(DbgVariableRecord *DVR) {
319323
return DebugVariable(DVR->getVariable(), std::nullopt,
320324
DVR->getDebugLoc().getInlinedAt());
321325
}
322326

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.
323330
DbgVariableRecord *UnwrapDbgInstPtr(DbgInstPtr P, DbgVariableRecord *Unused) {
324331
(void)Unused;
325332
return static_cast<DbgVariableRecord *>(cast<DbgRecord *>(P));
@@ -369,6 +376,9 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
369376
/// Map of aggregate variables to their fragment associated with OldAlloca.
370377
DenseMap<DebugVariable, std::optional<DIExpression::FragmentInfo>>
371378
BaseFragments;
379+
for (auto *DAI : at::getAssignmentMarkers(OldAlloca))
380+
BaseFragments[getAggregateVariable(DAI)] =
381+
DAI->getExpression()->getFragmentInfo();
372382
for (auto *DVR : at::getDVRAssignmentMarkers(OldAlloca))
373383
BaseFragments[getAggregateVariable(DVR)] =
374384
DVR->getExpression()->getFragmentInfo();
@@ -381,7 +391,7 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
381391
DIBuilder DIB(*OldInst->getModule(), /*AllowUnresolved*/ false);
382392
assert(OldAlloca->isStaticAlloca());
383393

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

489+
for_each(MarkerRange, MigrateDbgAssign);
479490
for_each(DVRAssignMarkerRange, MigrateDbgAssign);
480491
}
481492

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

51105121
// There isn't a shared interface to get the "address" parts out of a
5111-
// dbg.declare and dbg.assign, so provide some wrappers.
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+
51125140
bool isKillAddress(const DbgVariableRecord *DVR) {
51135141
if (DVR->getType() == DbgVariableRecord::LocationType::Assign)
51145142
return DVR->isKillAddress();
51155143
return DVR->isKillLocation();
51165144
}
51175145

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+
51185152
const DIExpression *getAddressExpression(const DbgVariableRecord *DVR) {
51195153
if (DVR->getType() == DbgVariableRecord::LocationType::Assign)
51205154
return DVR->getAddressExpression();
@@ -5211,6 +5245,66 @@ static DIExpression *createOrReplaceFragment(const DIExpression *Expr,
52115245
return DIExpression::get(Expr->getContext(), Ops);
52125246
}
52135247

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

53865480
// Migrate debug information from the old alloca to the new alloca(s)
53875481
// and the individual partitions.
5388-
auto MigrateOne = [&](DbgVariableRecord *DbgVariable) {
5482+
auto MigrateOne = [&](auto *DbgVariable) {
53895483
// Can't overlap with undef memory.
53905484
if (isKillAddress(DbgVariable))
53915485
return;
53925486

5393-
const Value *DbgPtr = DbgVariable->getAddress();
5487+
const Value *DbgPtr = getAddress(DbgVariable);
53945488
DIExpression::FragmentInfo VarFrag =
53955489
DbgVariable->getFragmentOrEntireVariable();
53965490
// Get the address expression constant offset if one exists and the ops
@@ -5478,6 +5572,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
54785572
if (SameVariableFragment(OldDII, DbgVariable))
54795573
OldDII->eraseFromParent();
54805574
};
5575+
for_each(findDbgDeclares(Fragment.Alloca), RemoveOne);
54815576
for_each(findDVRDeclares(Fragment.Alloca), RemoveOne);
54825577
for_each(findDVRValues(Fragment.Alloca), RemoveOne);
54835578
insertNewDbgInst(DIB, DbgVariable, Fragment.Alloca, NewExpr, &AI,
@@ -5487,8 +5582,10 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
54875582

54885583
// Migrate debug information from the old alloca to the new alloca(s)
54895584
// and the individual partitions.
5585+
for_each(findDbgDeclares(&AI), MigrateOne);
54905586
for_each(findDVRDeclares(&AI), MigrateOne);
54915587
for_each(findDVRValues(&AI), MigrateOne);
5588+
for_each(at::getAssignmentMarkers(&AI), MigrateOne);
54925589
for_each(at::getDVRAssignmentMarkers(&AI), MigrateOne);
54935590

54945591
return Changed;
@@ -5709,6 +5806,8 @@ bool SROA::deleteDeadInstructions(
57095806
// not be able to find it.
57105807
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
57115808
DeletedAllocas.insert(AI);
5809+
for (DbgDeclareInst *OldDII : findDbgDeclares(AI))
5810+
OldDII->eraseFromParent();
57125811
for (DbgVariableRecord *OldDII : findDVRDeclares(AI))
57135812
OldDII->eraseFromParent();
57145813
}

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,9 @@ 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.
580-
if (!isa<PHINode>(NewInst)) {
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)) {
581582
RemapInstruction(NewInst, VMap,
582583
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
583584

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

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+
735745
// Clone the entry block, and anything recursively reachable from it.
736746
std::vector<const BasicBlock *> CloneWorklist;
737747
PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
@@ -889,11 +899,21 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
889899
// Restore attributes.
890900
NewFunc->setAttributes(Attrs);
891901

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
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
894904
// we didn't do this, ValueAsMetadata(use-before-def) operands would be
895905
// replaced by empty metadata. This would signal later cleanup passes to
896-
// remove the debug records, potentially causing incorrect locations.
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.
897917
Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
898918
for (BasicBlock &BB : make_range(Begin, NewFunc->end())) {
899919
for (Instruction &I : BB) {

0 commit comments

Comments
 (0)