@@ -555,6 +555,7 @@ static void cacheDIVar(FrameDataInfo &FrameData,
555555 };
556556 CacheIt (findDbgDeclares (V));
557557 CacheIt (findDVRDeclares (V));
558+ CacheIt (findDVRDeclareValues (V));
558559 }
559560}
560561
@@ -1156,6 +1157,47 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
11561157 for_each (DVRs, SalvageOne);
11571158 }
11581159
1160+ TinyPtrVector<DbgVariableRecord *> DVRDeclareValues =
1161+ findDVRDeclareValues (Def);
1162+ // Try best to find dbg.declare_value. If the spill is a temp, there may
1163+ // not be a direct dbg.declare_value. Walk up the load chain to find one
1164+ // from an alias.
1165+ if (F->getSubprogram ()) {
1166+ auto *CurDef = Def;
1167+ while (DVRDeclareValues.empty () && isa<LoadInst>(CurDef)) {
1168+ auto *LdInst = cast<LoadInst>(CurDef);
1169+ // Only consider ptr to ptr same type load.
1170+ if (LdInst->getPointerOperandType () != LdInst->getType ())
1171+ break ;
1172+ CurDef = LdInst->getPointerOperand ();
1173+ if (!isa<AllocaInst, LoadInst>(CurDef))
1174+ break ;
1175+ DVRDeclareValues = findDVRDeclareValues (CurDef);
1176+ }
1177+ }
1178+
1179+ auto SalvageOneCoro = [&](auto *DDI) {
1180+ // This dbg.declare_value is preserved for all coro-split function
1181+ // fragments. It will be unreachable in the main function, and
1182+ // processed by coro::salvageDebugInfo() by the Cloner. However, convert
1183+ // it to a dbg.declare to make sure future passes don't have to deal
1184+ // with a dbg.declare_value.
1185+ auto *VAM = ValueAsMetadata::get (CurrentReload);
1186+ Type *Ty = VAM->getValue ()->getType ();
1187+ // If the metadata type is not a pointer, emit a dbg.value instead.
1188+ DbgVariableRecord *NewDVR = new DbgVariableRecord (
1189+ ValueAsMetadata::get (CurrentReload), DDI->getVariable (),
1190+ DDI->getExpression (), DDI->getDebugLoc (),
1191+ Ty->isPointerTy () ? DbgVariableRecord::LocationType::Declare
1192+ : DbgVariableRecord::LocationType::Value);
1193+ Builder.GetInsertPoint ()->getParent ()->insertDbgRecordBefore (
1194+ NewDVR, Builder.GetInsertPoint ());
1195+ // This dbg.declare_value is for the main function entry point. It
1196+ // will be deleted in all coro-split functions.
1197+ coro::salvageDebugInfo (ArgToAllocaMap, *DDI, false /* UseEntryValue*/ );
1198+ };
1199+ for_each (DVRDeclareValues, SalvageOneCoro);
1200+
11591201 // If we have a single edge PHINode, remove it and replace it with a
11601202 // reload from the coroutine frame. (We already took care of multi edge
11611203 // PHINodes by normalizing them in the rewritePHIs function).
@@ -1982,7 +2024,7 @@ void coro::salvageDebugInfo(
19822024 Function *F = DVR.getFunction ();
19832025 // Follow the pointer arithmetic all the way to the incoming
19842026 // function argument and convert into a DIExpression.
1985- bool SkipOutermostLoad = DVR.isDbgDeclare ();
2027+ bool SkipOutermostLoad = DVR.isDbgDeclare () || DVR. isDbgDeclareValue () ;
19862028 Value *OriginalStorage = DVR.getVariableLocationOp (0 );
19872029
19882030 auto SalvagedInfo =
@@ -1996,10 +2038,11 @@ void coro::salvageDebugInfo(
19962038
19972039 DVR.replaceVariableLocationOp (OriginalStorage, Storage);
19982040 DVR.setExpression (Expr);
1999- // We only hoist dbg.declare today since it doesn't make sense to hoist
2000- // dbg.value since it does not have the same function wide guarantees that
2001- // dbg.declare does.
2002- if (DVR.getType () == DbgVariableRecord::LocationType::Declare) {
2041+ // We only hoist dbg.declare and dbg.declare_value today since it doesn't make
2042+ // sense to hoist dbg.value since it does not have the same function wide
2043+ // guarantees that dbg.declare does.
2044+ if (DVR.getType () == DbgVariableRecord::LocationType::Declare ||
2045+ DVR.getType () == DbgVariableRecord::LocationType::DeclareValue) {
20032046 std::optional<BasicBlock::iterator> InsertPt;
20042047 if (auto *I = dyn_cast<Instruction>(Storage)) {
20052048 InsertPt = I->getInsertionPointAfterDef ();
@@ -2014,6 +2057,19 @@ void coro::salvageDebugInfo(
20142057 InsertPt = F->getEntryBlock ().begin ();
20152058 if (InsertPt) {
20162059 DVR.removeFromParent ();
2060+ // If there is a dbg.declare_value being reinserted, insert it as a
2061+ // dbg.declare instead, so that subsequent passes don't have to deal with
2062+ // a dbg.declare_value.
2063+ if (DVR.getType () == DbgVariableRecord::LocationType::DeclareValue) {
2064+ auto *MD = DVR.getRawLocation ();
2065+ if (auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
2066+ Type *Ty = VAM->getValue ()->getType ();
2067+ if (Ty->isPointerTy ())
2068+ DVR.Type = DbgVariableRecord::LocationType::Declare;
2069+ else
2070+ DVR.Type = DbgVariableRecord::LocationType::Value;
2071+ }
2072+ }
20172073 (*InsertPt)->getParent ()->insertDbgRecordBefore (&DVR, *InsertPt);
20182074 }
20192075 }
0 commit comments