Skip to content

Commit 632f44e

Browse files
authored
[RemoveDIs][DebugInfo] Handle DPVAssign in most transforms (#78986)
This patch trivially updates various opt passes to handle DPVAssigns. In all cases, this means some combination of generifying existing code to handle DPValues and DbgAssignIntrinsics, iterating over DPValues where previously we did not, or duplicating code for DbgAssignIntrinsics to the equivalent DPValue function (in inlining and salvageDebugInfo).
1 parent 6ad4ed5 commit 632f44e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+227
-89
lines changed

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ static void cacheDIVar(FrameDataInfo &FrameData,
963963
if (DIVarCache.contains(V))
964964
continue;
965965

966-
auto CacheIt = [&DIVarCache, V](auto Container) {
966+
auto CacheIt = [&DIVarCache, V](const auto &Container) {
967967
auto *I = llvm::find_if(Container, [](auto *DDI) {
968968
return DDI->getExpression()->getNumElements() == 0;
969969
});
@@ -1868,7 +1868,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
18681868
// alias.
18691869
if (F->getSubprogram()) {
18701870
auto *CurDef = Def;
1871-
while (DIs.empty() && isa<LoadInst>(CurDef)) {
1871+
while (DIs.empty() && DPVs.empty() && isa<LoadInst>(CurDef)) {
18721872
auto *LdInst = cast<LoadInst>(CurDef);
18731873
// Only consider ptr to ptr same type load.
18741874
if (LdInst->getPointerOperandType() != LdInst->getType())
@@ -2966,7 +2966,7 @@ void coro::salvageDebugInfo(
29662966
Function *F = DPV.getFunction();
29672967
// Follow the pointer arithmetic all the way to the incoming
29682968
// function argument and convert into a DIExpression.
2969-
bool SkipOutermostLoad = DPV.getType() == DPValue::LocationType::Declare;
2969+
bool SkipOutermostLoad = DPV.isDbgDeclare();
29702970
Value *OriginalStorage = DPV.getVariableLocationOp(0);
29712971

29722972
auto SalvagedInfo = ::salvageDebugInfoImpl(

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,12 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) {
282282
Constant *FillVal = ConstantInt::get(ITy, Fill);
283283
StoreInst *S = Builder.CreateStore(FillVal, Dest, MI->isVolatile());
284284
S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
285-
for (auto *DAI : at::getAssignmentMarkers(S)) {
286-
if (llvm::is_contained(DAI->location_ops(), FillC))
287-
DAI->replaceVariableLocationOp(FillC, FillVal);
288-
}
285+
auto replaceOpForAssignmentMarkers = [FillC, FillVal](auto *DbgAssign) {
286+
if (llvm::is_contained(DbgAssign->location_ops(), FillC))
287+
DbgAssign->replaceVariableLocationOp(FillC, FillVal);
288+
};
289+
for_each(at::getAssignmentMarkers(S), replaceOpForAssignmentMarkers);
290+
for_each(at::getDPVAssignmentMarkers(S), replaceOpForAssignmentMarkers);
289291

290292
S->setAlignment(Alignment);
291293
if (isa<AtomicMemSetInst>(MI))

llvm/lib/Transforms/Scalar/ADCE.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,11 @@ ADCEChanged AggressiveDeadCodeElimination::removeDeadInstructions() {
549549
// like the rest of this loop does. Extending support to assignment tracking
550550
// is future work.
551551
for (DPValue &DPV : make_early_inc_range(I.getDbgValueRange())) {
552+
// Avoid removing a DPV that is linked to instructions because it holds
553+
// information about an existing store.
554+
if (DPV.isDbgAssign())
555+
if (!at::getAssignmentInsts(&DPV).empty())
556+
continue;
552557
if (AliveScopes.count(DPV.getDebugLoc()->getScope()))
553558
continue;
554559
I.dropOneDbgValue(&DPV);

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -488,27 +488,27 @@ static void shortenAssignment(Instruction *Inst, Value *OriginalDest,
488488
uint64_t DeadSliceSizeInBits = OldSizeInBits - NewSizeInBits;
489489
uint64_t DeadSliceOffsetInBits =
490490
OldOffsetInBits + (IsOverwriteEnd ? NewSizeInBits : 0);
491-
auto SetDeadFragExpr = [](DbgAssignIntrinsic *DAI,
491+
auto SetDeadFragExpr = [](auto *Assign,
492492
DIExpression::FragmentInfo DeadFragment) {
493493
// createFragmentExpression expects an offset relative to the existing
494494
// fragment offset if there is one.
495495
uint64_t RelativeOffset = DeadFragment.OffsetInBits -
496-
DAI->getExpression()
496+
Assign->getExpression()
497497
->getFragmentInfo()
498498
.value_or(DIExpression::FragmentInfo(0, 0))
499499
.OffsetInBits;
500500
if (auto NewExpr = DIExpression::createFragmentExpression(
501-
DAI->getExpression(), RelativeOffset, DeadFragment.SizeInBits)) {
502-
DAI->setExpression(*NewExpr);
501+
Assign->getExpression(), RelativeOffset, DeadFragment.SizeInBits)) {
502+
Assign->setExpression(*NewExpr);
503503
return;
504504
}
505505
// Failed to create a fragment expression for this so discard the value,
506506
// making this a kill location.
507507
auto *Expr = *DIExpression::createFragmentExpression(
508-
DIExpression::get(DAI->getContext(), std::nullopt),
508+
DIExpression::get(Assign->getContext(), std::nullopt),
509509
DeadFragment.OffsetInBits, DeadFragment.SizeInBits);
510-
DAI->setExpression(Expr);
511-
DAI->setKillLocation();
510+
Assign->setExpression(Expr);
511+
Assign->setKillLocation();
512512
};
513513

514514
// A DIAssignID to use so that the inserted dbg.assign intrinsics do not
@@ -526,32 +526,35 @@ static void shortenAssignment(Instruction *Inst, Value *OriginalDest,
526526
// returned by getAssignmentMarkers so save a copy of the markers to iterate
527527
// over.
528528
auto LinkedRange = at::getAssignmentMarkers(Inst);
529+
SmallVector<DPValue *> LinkedDPVAssigns = at::getDPVAssignmentMarkers(Inst);
529530
SmallVector<DbgAssignIntrinsic *> Linked(LinkedRange.begin(),
530531
LinkedRange.end());
531-
for (auto *DAI : Linked) {
532+
auto InsertAssignForOverlap = [&](auto *Assign) {
532533
std::optional<DIExpression::FragmentInfo> NewFragment;
533534
if (!at::calculateFragmentIntersect(DL, OriginalDest, DeadSliceOffsetInBits,
534-
DeadSliceSizeInBits, DAI,
535+
DeadSliceSizeInBits, Assign,
535536
NewFragment) ||
536537
!NewFragment) {
537538
// We couldn't calculate the intersecting fragment for some reason. Be
538539
// cautious and unlink the whole assignment from the store.
539-
DAI->setKillAddress();
540-
DAI->setAssignId(GetDeadLink());
541-
continue;
540+
Assign->setKillAddress();
541+
Assign->setAssignId(GetDeadLink());
542+
return;
542543
}
543544
// No intersect.
544545
if (NewFragment->SizeInBits == 0)
545-
continue;
546+
return;
546547

547548
// Fragments overlap: insert a new dbg.assign for this dead part.
548-
auto *NewAssign = cast<DbgAssignIntrinsic>(DAI->clone());
549-
NewAssign->insertAfter(DAI);
549+
auto *NewAssign = static_cast<decltype(Assign)>(Assign->clone());
550+
NewAssign->insertAfter(Assign);
550551
NewAssign->setAssignId(GetDeadLink());
551552
if (NewFragment)
552553
SetDeadFragExpr(NewAssign, *NewFragment);
553554
NewAssign->setKillAddress();
554-
}
555+
};
556+
for_each(Linked, InsertAssignForOverlap);
557+
for_each(LinkedDPVAssigns, InsertAssignForOverlap);
555558
}
556559

557560
static bool tryToShorten(Instruction *DeadI, int64_t &DeadStart,

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,11 +1589,14 @@ static void fixupDebugInfoPostExtraction(Function &OldFunc, Function &NewFunc,
15891589
for (auto &DPV : I.getDbgValueRange()) {
15901590
// Apply the two updates that dbg.values get: invalid operands, and
15911591
// variable metadata fixup.
1592-
// FIXME: support dbg.assign form of DPValues.
15931592
if (any_of(DPV.location_ops(), IsInvalidLocation)) {
15941593
DPVsToDelete.push_back(&DPV);
15951594
continue;
15961595
}
1596+
if (DPV.isDbgAssign() && IsInvalidLocation(DPV.getAddress())) {
1597+
DPVsToDelete.push_back(&DPV);
1598+
continue;
1599+
}
15971600
if (!DPV.getDebugLoc().getInlinedAt())
15981601
DPV.setVariable(GetUpdatedDIVariable(DPV.getVariable()));
15991602
DPV.setDebugLoc(DebugLoc::replaceInlinedAtSubprogram(DPV.getDebugLoc(),

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,13 +1789,15 @@ static at::StorageToVarsMap collectEscapedLocals(const DataLayout &DL,
17891789
continue;
17901790

17911791
// Find all local variables associated with the backing storage.
1792-
for (auto *DAI : at::getAssignmentMarkers(Base)) {
1792+
auto CollectAssignsForStorage = [&](auto *DbgAssign) {
17931793
// Skip variables from inlined functions - they are not local variables.
1794-
if (DAI->getDebugLoc().getInlinedAt())
1795-
continue;
1796-
LLVM_DEBUG(errs() << " > DEF : " << *DAI << "\n");
1797-
EscapedLocals[Base].insert(at::VarRecord(DAI));
1798-
}
1794+
if (DbgAssign->getDebugLoc().getInlinedAt())
1795+
return;
1796+
LLVM_DEBUG(errs() << " > DEF : " << *DbgAssign << "\n");
1797+
EscapedLocals[Base].insert(at::VarRecord(DbgAssign));
1798+
};
1799+
for_each(at::getAssignmentMarkers(Base), CollectAssignsForStorage);
1800+
for_each(at::getDPVAssignmentMarkers(Base), CollectAssignsForStorage);
17991801
}
18001802
return EscapedLocals;
18011803
}
@@ -1827,6 +1829,10 @@ static void fixupAssignments(Function::iterator Start, Function::iterator End) {
18271829
// attachment or use, replace it with a new version.
18281830
for (auto BBI = Start; BBI != End; ++BBI) {
18291831
for (Instruction &I : *BBI) {
1832+
for (DPValue &DPV : I.getDbgValueRange()) {
1833+
if (DPV.isDbgAssign())
1834+
DPV.setAssignId(GetNewID(DPV.getAssignID()));
1835+
}
18301836
if (auto *ID = I.getMetadata(LLVMContext::MD_DIAssignID))
18311837
I.setMetadata(LLVMContext::MD_DIAssignID, GetNewID(ID));
18321838
else if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
17531753

17541754
void llvm::ConvertDebugDeclareToDebugValue(DPValue *DPV, StoreInst *SI,
17551755
DIBuilder &Builder) {
1756-
assert(DPV->isAddressOfVariable());
1756+
assert(DPV->isAddressOfVariable() || DPV->isDbgAssign());
17571757
auto *DIVar = DPV->getVariable();
17581758
assert(DIVar && "Missing variable");
17591759
auto *DIExpr = DPV->getExpression();
@@ -2189,14 +2189,13 @@ void llvm::salvageDebugInfo(Instruction &I) {
21892189
salvageDebugInfoForDbgValues(I, DbgUsers, DPUsers);
21902190
}
21912191

2192-
/// Salvage the address component of \p DAI.
2193-
static void salvageDbgAssignAddress(DbgAssignIntrinsic *DAI) {
2194-
Instruction *I = dyn_cast<Instruction>(DAI->getAddress());
2192+
template <typename T> static void salvageDbgAssignAddress(T *Assign) {
2193+
Instruction *I = dyn_cast<Instruction>(Assign->getAddress());
21952194
// Only instructions can be salvaged at the moment.
21962195
if (!I)
21972196
return;
21982197

2199-
assert(!DAI->getAddressExpression()->getFragmentInfo().has_value() &&
2198+
assert(!Assign->getAddressExpression()->getFragmentInfo().has_value() &&
22002199
"address-expression shouldn't have fragment info");
22012200

22022201
// The address component of a dbg.assign cannot be variadic.
@@ -2210,16 +2209,16 @@ static void salvageDbgAssignAddress(DbgAssignIntrinsic *DAI) {
22102209
return;
22112210

22122211
DIExpression *SalvagedExpr = DIExpression::appendOpsToArg(
2213-
DAI->getAddressExpression(), Ops, 0, /*StackValue=*/false);
2212+
Assign->getAddressExpression(), Ops, 0, /*StackValue=*/false);
22142213
assert(!SalvagedExpr->getFragmentInfo().has_value() &&
22152214
"address-expression shouldn't have fragment info");
22162215

22172216
// Salvage succeeds if no additional values are required.
22182217
if (AdditionalValues.empty()) {
2219-
DAI->setAddress(NewV);
2220-
DAI->setAddressExpression(SalvagedExpr);
2218+
Assign->setAddress(NewV);
2219+
Assign->setAddressExpression(SalvagedExpr);
22212220
} else {
2222-
DAI->setKillAddress();
2221+
Assign->setKillAddress();
22232222
}
22242223
}
22252224

@@ -2293,10 +2292,19 @@ void llvm::salvageDebugInfoForDbgValues(
22932292
}
22942293
// Duplicate of above block for DPValues.
22952294
for (auto *DPV : DPUsers) {
2295+
if (DPV->isDbgAssign()) {
2296+
if (DPV->getAddress() == &I) {
2297+
salvageDbgAssignAddress(DPV);
2298+
Salvaged = true;
2299+
}
2300+
if (DPV->getValue() != &I)
2301+
continue;
2302+
}
2303+
22962304
// Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they
22972305
// are implicitly pointing out the value as a DWARF memory location
22982306
// description.
2299-
bool StackValue = DPV->getType() == DPValue::LocationType::Value;
2307+
bool StackValue = DPV->getType() != DPValue::LocationType::Declare;
23002308
auto DPVLocation = DPV->location_ops();
23012309
assert(
23022310
is_contained(DPVLocation, &I) &&
@@ -2330,7 +2338,7 @@ void llvm::salvageDebugInfoForDbgValues(
23302338
SalvagedExpr->getNumElements() <= MaxExpressionSize;
23312339
if (AdditionalValues.empty() && IsValidSalvageExpr) {
23322340
DPV->setExpression(SalvagedExpr);
2333-
} else if (DPV->getType() == DPValue::LocationType::Value &&
2341+
} else if (DPV->getType() != DPValue::LocationType::Declare &&
23342342
IsValidSalvageExpr &&
23352343
DPV->getNumVariableLocationOps() + AdditionalValues.size() <=
23362344
MaxDebugArgs) {
@@ -2340,8 +2348,7 @@ void llvm::salvageDebugInfoForDbgValues(
23402348
// currently only valid for stack value expressions.
23412349
// Also do not salvage if the resulting DIArgList would contain an
23422350
// unreasonably large number of values.
2343-
Value *Undef = UndefValue::get(I.getOperand(0)->getType());
2344-
DPV->replaceVariableLocationOp(I.getOperand(0), Undef);
2351+
DPV->setKillLocation();
23452352
}
23462353
LLVM_DEBUG(dbgs() << "SALVAGE: " << DPV << '\n');
23472354
Salvaged = true;

0 commit comments

Comments
 (0)