Skip to content

[DebugInfo] Strip more debug-intrinsic code from local utils #149037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions llvm/include/llvm/Transforms/Utils/Local.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class BasicBlock;
class BranchInst;
class CallBase;
class CallInst;
class DbgVariableIntrinsic;
class DIBuilder;
class DomTreeUpdater;
class Function;
Expand Down Expand Up @@ -275,44 +274,31 @@ LLVM_ABI CallInst *changeToCall(InvokeInst *II, DomTreeUpdater *DTU = nullptr);
LLVM_ABI void InsertDebugValueAtStoreLoc(DbgVariableRecord *DVR, StoreInst *SI,
DIBuilder &Builder);

/// Creates and inserts an llvm.dbg.value intrinsic before a store
/// that has an associated llvm.dbg.value intrinsic.
LLVM_ABI void InsertDebugValueAtStoreLoc(DbgVariableIntrinsic *DII,
StoreInst *SI, DIBuilder &Builder);

/// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
/// that has an associated llvm.dbg.declare intrinsic.
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
StoreInst *SI,
DIBuilder &Builder);
/// Inserts a dbg.value record before a store to an alloca'd value
/// that has an associated dbg.declare record.
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
StoreInst *SI,
DIBuilder &Builder);

/// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
/// that has an associated llvm.dbg.declare intrinsic.
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
LoadInst *LI, DIBuilder &Builder);
/// Inserts a dbg.value record before a load of an alloca'd value
/// that has an associated dbg.declare record.
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
LoadInst *LI, DIBuilder &Builder);

/// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
/// llvm.dbg.declare intrinsic.
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
PHINode *LI, DIBuilder &Builder);
/// Inserts a dbg.value record after a phi that has an associated
/// llvm.dbg.declare record.
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
PHINode *LI, DIBuilder &Builder);

/// Lowers llvm.dbg.declare intrinsics into appropriate set of
/// llvm.dbg.value intrinsics.
/// Lowers dbg.declare records into appropriate set of dbg.value records.
LLVM_ABI bool LowerDbgDeclare(Function &F);

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

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

/// Replaces multiple llvm.dbg.value instructions when the alloca it describes
/// Replaces multiple dbg.value records when the alloca it describes
/// is replaced with a new value. If Offset is non-zero, a constant displacement
/// is added to the expression (after the mandatory Deref). Offset can be
/// negative. New llvm.dbg.value instructions are inserted at the locations of
/// negative. New dbg.value records are inserted at the locations of
/// the instructions they replace.
LLVM_ABI void replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
DIBuilder &Builder, int Offset = 0);
Expand Down
3 changes: 0 additions & 3 deletions llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

namespace llvm {
class DominatorTree;
class DbgVariableIntrinsic;
class IntrinsicInst;
class PostDominatorTree;
class AllocaInst;
Expand Down Expand Up @@ -53,8 +52,6 @@ struct AllocaInfo {
AllocaInst *AI;
SmallVector<IntrinsicInst *, 2> LifetimeStart;
SmallVector<IntrinsicInst *, 2> LifetimeEnd;
SmallVector<DbgVariableIntrinsic *, 2> DbgVariableIntrinsics;
// Non-intrinsic records of variable locations.
SmallVector<DbgVariableRecord *, 2> DbgVariableRecords;
};

Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3644,9 +3644,6 @@ Instruction *InstCombinerImpl::visitAllocSite(Instruction &MI) {
ConstantInt::get(Type::getInt1Ty(C->getContext()),
C->isFalseWhenEqual()));
} else if (auto *SI = dyn_cast<StoreInst>(I)) {
for (auto *DVI : DVIs)
if (DVI->isAddressOfVariable())
ConvertDebugDeclareToDebugValue(DVI, SI, *DIB);
for (auto *DVR : DVRs)
if (DVR->isAddressOfVariable())
ConvertDebugDeclareToDebugValue(DVR, SI, *DIB);
Expand Down
107 changes: 4 additions & 103 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,11 @@ calculateFragment(DILocalVariable *Variable,
return UseFrag;
}

static DebugVariable getAggregateVariable(DbgVariableIntrinsic *DVI) {
return DebugVariable(DVI->getVariable(), std::nullopt,
DVI->getDebugLoc().getInlinedAt());
}
static DebugVariable getAggregateVariable(DbgVariableRecord *DVR) {
return DebugVariable(DVR->getVariable(), std::nullopt,
DVR->getDebugLoc().getInlinedAt());
}

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

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

for_each(MarkerRange, MigrateDbgAssign);
for_each(DVRAssignMarkerRange, MigrateDbgAssign);
}

Expand Down Expand Up @@ -5119,36 +5108,13 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
}

// There isn't a shared interface to get the "address" parts out of a
// dbg.declare and dbg.assign, so provide some wrappers now for
// both debug intrinsics and records.
const Value *getAddress(const DbgVariableIntrinsic *DVI) {
if (const auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI))
return DAI->getAddress();
return cast<DbgDeclareInst>(DVI)->getAddress();
}

const Value *getAddress(const DbgVariableRecord *DVR) {
return DVR->getAddress();
}

bool isKillAddress(const DbgVariableIntrinsic *DVI) {
if (const auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI))
return DAI->isKillAddress();
return cast<DbgDeclareInst>(DVI)->isKillLocation();
}

// dbg.declare and dbg.assign, so provide some wrappers.
bool isKillAddress(const DbgVariableRecord *DVR) {
if (DVR->getType() == DbgVariableRecord::LocationType::Assign)
return DVR->isKillAddress();
return DVR->isKillLocation();
}

const DIExpression *getAddressExpression(const DbgVariableIntrinsic *DVI) {
if (const auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI))
return DAI->getAddressExpression();
return cast<DbgDeclareInst>(DVI)->getExpression();
}

const DIExpression *getAddressExpression(const DbgVariableRecord *DVR) {
if (DVR->getType() == DbgVariableRecord::LocationType::Assign)
return DVR->getAddressExpression();
Expand Down Expand Up @@ -5236,66 +5202,6 @@ static DIExpression *createOrReplaceFragment(const DIExpression *Expr,
return DIExpression::get(Expr->getContext(), Ops);
}

/// Insert a new dbg.declare.
/// \p Orig Original to copy debug loc and variable from.
/// \p NewAddr Location's new base address.
/// \p NewAddrExpr New expression to apply to address.
/// \p BeforeInst Insert position.
/// \p NewFragment New fragment (absolute, non-relative).
/// \p BitExtractAdjustment Offset to apply to any extract_bits op.
static void
insertNewDbgInst(DIBuilder &DIB, DbgDeclareInst *Orig, AllocaInst *NewAddr,
DIExpression *NewAddrExpr, Instruction *BeforeInst,
std::optional<DIExpression::FragmentInfo> NewFragment,
int64_t BitExtractAdjustment) {
if (NewFragment)
NewAddrExpr = createOrReplaceFragment(NewAddrExpr, *NewFragment,
BitExtractAdjustment);
if (!NewAddrExpr)
return;

DIB.insertDeclare(NewAddr, Orig->getVariable(), NewAddrExpr,
Orig->getDebugLoc(), BeforeInst->getIterator());
}

/// Insert a new dbg.assign.
/// \p Orig Original to copy debug loc, variable, value and value expression
/// from.
/// \p NewAddr Location's new base address.
/// \p NewAddrExpr New expression to apply to address.
/// \p BeforeInst Insert position.
/// \p NewFragment New fragment (absolute, non-relative).
/// \p BitExtractAdjustment Offset to apply to any extract_bits op.
static void
insertNewDbgInst(DIBuilder &DIB, DbgAssignIntrinsic *Orig, AllocaInst *NewAddr,
DIExpression *NewAddrExpr, Instruction *BeforeInst,
std::optional<DIExpression::FragmentInfo> NewFragment,
int64_t BitExtractAdjustment) {
// DIBuilder::insertDbgAssign will insert the #dbg_assign after NewAddr.
(void)BeforeInst;

// A dbg.assign puts fragment info in the value expression only. The address
// expression has already been built: NewAddrExpr.
DIExpression *NewFragmentExpr = Orig->getExpression();
if (NewFragment)
NewFragmentExpr = createOrReplaceFragment(NewFragmentExpr, *NewFragment,
BitExtractAdjustment);
if (!NewFragmentExpr)
return;

// Apply a DIAssignID to the store if it doesn't already have it.
if (!NewAddr->hasMetadata(LLVMContext::MD_DIAssignID)) {
NewAddr->setMetadata(LLVMContext::MD_DIAssignID,
DIAssignID::getDistinct(NewAddr->getContext()));
}

Instruction *NewAssign = cast<Instruction *>(DIB.insertDbgAssign(
NewAddr, Orig->getValue(), Orig->getVariable(), NewFragmentExpr, NewAddr,
NewAddrExpr, Orig->getDebugLoc()));
LLVM_DEBUG(dbgs() << "Created new assign intrinsic: " << *NewAssign << "\n");
(void)NewAssign;
}

/// Insert a new DbgRecord.
/// \p Orig Original to copy record type, debug loc and variable from, and
/// additionally value and value expression for dbg_assign records.
Expand Down Expand Up @@ -5457,12 +5363,12 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {

// Migrate debug information from the old alloca to the new alloca(s)
// and the individual partitions.
auto MigrateOne = [&](auto *DbgVariable) {
auto MigrateOne = [&](DbgVariableRecord *DbgVariable) {
// Can't overlap with undef memory.
if (isKillAddress(DbgVariable))
return;

const Value *DbgPtr = getAddress(DbgVariable);
const Value *DbgPtr = DbgVariable->getAddress();
DIExpression::FragmentInfo VarFrag =
DbgVariable->getFragmentOrEntireVariable();
// Get the address expression constant offset if one exists and the ops
Expand Down Expand Up @@ -5543,7 +5449,6 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
if (SameVariableFragment(OldDII, DbgVariable))
OldDII->eraseFromParent();
};
for_each(findDbgDeclares(Fragment.Alloca), RemoveOne);
for_each(findDVRDeclares(Fragment.Alloca), RemoveOne);
for_each(findDVRValues(Fragment.Alloca), RemoveOne);
insertNewDbgInst(DIB, DbgVariable, Fragment.Alloca, NewExpr, &AI,
Expand All @@ -5553,10 +5458,8 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {

// Migrate debug information from the old alloca to the new alloca(s)
// and the individual partitions.
for_each(findDbgDeclares(&AI), MigrateOne);
for_each(findDVRDeclares(&AI), MigrateOne);
for_each(findDVRValues(&AI), MigrateOne);
for_each(at::getAssignmentMarkers(&AI), MigrateOne);
for_each(at::getDVRAssignmentMarkers(&AI), MigrateOne);

return Changed;
Expand Down Expand Up @@ -5777,8 +5680,6 @@ bool SROA::deleteDeadInstructions(
// not be able to find it.
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
DeletedAllocas.insert(AI);
for (DbgDeclareInst *OldDII : findDbgDeclares(AI))
OldDII->eraseFromParent();
for (DbgVariableRecord *OldDII : findDVRDeclares(AI))
OldDII->eraseFromParent();
}
Expand Down
30 changes: 5 additions & 25 deletions llvm/lib/Transforms/Utils/CloneFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,8 @@ void PruningFunctionCloner::CloneBlock(
}

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

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

// Collect debug intrinsics for remapping later.
SmallVector<const DbgVariableIntrinsic *, 8> DbgIntrinsics;
for (const auto &BB : *OldFunc) {
for (const auto &I : BB) {
if (const auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
DbgIntrinsics.push_back(DVI);
}
}

// Clone the entry block, and anything recursively reachable from it.
std::vector<const BasicBlock *> CloneWorklist;
PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
Expand Down Expand Up @@ -899,21 +889,11 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
// Restore attributes.
NewFunc->setAttributes(Attrs);

// Remap debug intrinsic operands now that all values have been mapped.
// Doing this now (late) preserves use-before-defs in debug intrinsics. If
// Remap debug records operands now that all values have been mapped.
// Doing this now (late) preserves use-before-defs in debug records. If
// we didn't do this, ValueAsMetadata(use-before-def) operands would be
// replaced by empty metadata. This would signal later cleanup passes to
// remove the debug intrinsics, potentially causing incorrect locations.
for (const auto *DVI : DbgIntrinsics) {
if (DbgVariableIntrinsic *NewDVI =
cast_or_null<DbgVariableIntrinsic>(VMap.lookup(DVI)))
RemapInstruction(NewDVI, VMap,
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
TypeMapper, Materializer);
}

// Do the same for DbgVariableRecords, touching all the instructions in the
// cloned range of blocks.
// remove the debug records, potentially causing incorrect locations.
Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
for (BasicBlock &BB : make_range(Begin, NewFunc->end())) {
for (Instruction &I : BB) {
Expand Down
Loading
Loading