Skip to content

Commit f1358b1

Browse files
Add support for llvm.dbg.declare_value in the CoroSplitter pass.
Make sure the CoroSplitter pass correctly handles llvm.dbg.declare_value intrinsics. Also, convert them to llvm.dbg.declares so that any subsquent passes do not need to be amended to support the llvm.dbg.declare_value intrinsic.
1 parent b427b5a commit f1358b1

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ static void cacheDIVar(FrameDataInfo &FrameData,
554554
DIVarCache.insert({V, (*I)->getVariable()});
555555
};
556556
CacheIt(findDVRDeclares(V));
557+
CacheIt(findDVRDeclareValues(V));
557558
}
558559
}
559560

@@ -1138,6 +1139,47 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
11381139
for_each(DVRs, SalvageOne);
11391140
}
11401141

1142+
TinyPtrVector<DbgVariableRecord *> DVRDeclareValues =
1143+
findDVRDeclareValues(Def);
1144+
// Try best to find dbg.declare_value. If the spill is a temp, there may
1145+
// not be a direct dbg.declare_value. Walk up the load chain to find one
1146+
// from an alias.
1147+
if (F->getSubprogram()) {
1148+
auto *CurDef = Def;
1149+
while (DVRDeclareValues.empty() && isa<LoadInst>(CurDef)) {
1150+
auto *LdInst = cast<LoadInst>(CurDef);
1151+
// Only consider ptr to ptr same type load.
1152+
if (LdInst->getPointerOperandType() != LdInst->getType())
1153+
break;
1154+
CurDef = LdInst->getPointerOperand();
1155+
if (!isa<AllocaInst, LoadInst>(CurDef))
1156+
break;
1157+
DVRDeclareValues = findDVRDeclareValues(CurDef);
1158+
}
1159+
}
1160+
1161+
auto SalvageOneCoro = [&](auto *DDI) {
1162+
// This dbg.declare_value is preserved for all coro-split function
1163+
// fragments. It will be unreachable in the main function, and
1164+
// processed by coro::salvageDebugInfo() by the Cloner. However, convert
1165+
// it to a dbg.declare to make sure future passes don't have to deal
1166+
// with a dbg.declare_value.
1167+
auto *VAM = ValueAsMetadata::get(CurrentReload);
1168+
Type *Ty = VAM->getValue()->getType();
1169+
// If the metadata type is not a pointer, emit a dbg.value instead.
1170+
DbgVariableRecord *NewDVR = new DbgVariableRecord(
1171+
ValueAsMetadata::get(CurrentReload), DDI->getVariable(),
1172+
DDI->getExpression(), DDI->getDebugLoc(),
1173+
Ty->isPointerTy() ? DbgVariableRecord::LocationType::Declare
1174+
: DbgVariableRecord::LocationType::Value);
1175+
Builder.GetInsertPoint()->getParent()->insertDbgRecordBefore(
1176+
NewDVR, Builder.GetInsertPoint());
1177+
// This dbg.declare_value is for the main function entry point. It
1178+
// will be deleted in all coro-split functions.
1179+
coro::salvageDebugInfo(ArgToAllocaMap, *DDI, false /*UseEntryValue*/);
1180+
};
1181+
for_each(DVRDeclareValues, SalvageOneCoro);
1182+
11411183
// If we have a single edge PHINode, remove it and replace it with a
11421184
// reload from the coroutine frame. (We already took care of multi edge
11431185
// PHINodes by normalizing them in the rewritePHIs function).
@@ -1921,7 +1963,7 @@ void coro::salvageDebugInfo(
19211963
Function *F = DVR.getFunction();
19221964
// Follow the pointer arithmetic all the way to the incoming
19231965
// function argument and convert into a DIExpression.
1924-
bool SkipOutermostLoad = DVR.isDbgDeclare();
1966+
bool SkipOutermostLoad = DVR.isDbgDeclare() || DVR.isDbgDeclareValue();
19251967
Value *OriginalStorage = DVR.getVariableLocationOp(0);
19261968

19271969
auto SalvagedInfo =
@@ -1935,10 +1977,11 @@ void coro::salvageDebugInfo(
19351977

19361978
DVR.replaceVariableLocationOp(OriginalStorage, Storage);
19371979
DVR.setExpression(Expr);
1938-
// We only hoist dbg.declare today since it doesn't make sense to hoist
1939-
// dbg.value since it does not have the same function wide guarantees that
1940-
// dbg.declare does.
1941-
if (DVR.getType() == DbgVariableRecord::LocationType::Declare) {
1980+
// We only hoist dbg.declare and dbg.declare_value today since it doesn't make
1981+
// sense to hoist dbg.value since it does not have the same function wide
1982+
// guarantees that dbg.declare does.
1983+
if (DVR.getType() == DbgVariableRecord::LocationType::Declare ||
1984+
DVR.getType() == DbgVariableRecord::LocationType::DeclareValue) {
19421985
std::optional<BasicBlock::iterator> InsertPt;
19431986
if (auto *I = dyn_cast<Instruction>(Storage)) {
19441987
InsertPt = I->getInsertionPointAfterDef();
@@ -1953,6 +1996,19 @@ void coro::salvageDebugInfo(
19531996
InsertPt = F->getEntryBlock().begin();
19541997
if (InsertPt) {
19551998
DVR.removeFromParent();
1999+
// If there is a dbg.declare_value being reinserted, insert it as a
2000+
// dbg.declare instead, so that subsequent passes don't have to deal with
2001+
// a dbg.declare_value.
2002+
if (DVR.getType() == DbgVariableRecord::LocationType::DeclareValue) {
2003+
auto *MD = DVR.getRawLocation();
2004+
if (auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
2005+
Type *Ty = VAM->getValue()->getType();
2006+
if (Ty->isPointerTy())
2007+
DVR.Type = DbgVariableRecord::LocationType::Declare;
2008+
else
2009+
DVR.Type = DbgVariableRecord::LocationType::Value;
2010+
}
2011+
}
19562012
(*InsertPt)->getParent()->insertDbgRecordBefore(&DVR, *InsertPt);
19572013
}
19582014
}

0 commit comments

Comments
 (0)