@@ -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
@@ -1142,6 +1143,47 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
11421143 for_each (DVRs, SalvageOne);
11431144 }
11441145
1146+ TinyPtrVector<DbgVariableRecord *> DVRDeclareValues =
1147+ findDVRDeclareValues (Def);
1148+ // Try best to find dbg.declare_value. If the spill is a temp, there may
1149+ // not be a direct dbg.declare_value. Walk up the load chain to find one
1150+ // from an alias.
1151+ if (F->getSubprogram ()) {
1152+ auto *CurDef = Def;
1153+ while (DVRDeclareValues.empty () && isa<LoadInst>(CurDef)) {
1154+ auto *LdInst = cast<LoadInst>(CurDef);
1155+ // Only consider ptr to ptr same type load.
1156+ if (LdInst->getPointerOperandType () != LdInst->getType ())
1157+ break ;
1158+ CurDef = LdInst->getPointerOperand ();
1159+ if (!isa<AllocaInst, LoadInst>(CurDef))
1160+ break ;
1161+ DVRDeclareValues = findDVRDeclareValues (CurDef);
1162+ }
1163+ }
1164+
1165+ auto SalvageOneCoro = [&](auto *DDI) {
1166+ // This dbg.declare_value is preserved for all coro-split function
1167+ // fragments. It will be unreachable in the main function, and
1168+ // processed by coro::salvageDebugInfo() by the Cloner. However, convert
1169+ // it to a dbg.declare to make sure future passes don't have to deal
1170+ // with a dbg.declare_value.
1171+ auto *VAM = ValueAsMetadata::get (CurrentReload);
1172+ Type *Ty = VAM->getValue ()->getType ();
1173+ // If the metadata type is not a pointer, emit a dbg.value instead.
1174+ DbgVariableRecord *NewDVR = new DbgVariableRecord (
1175+ ValueAsMetadata::get (CurrentReload), DDI->getVariable (),
1176+ DDI->getExpression (), DDI->getDebugLoc (),
1177+ Ty->isPointerTy () ? DbgVariableRecord::LocationType::Declare
1178+ : DbgVariableRecord::LocationType::Value);
1179+ Builder.GetInsertPoint ()->getParent ()->insertDbgRecordBefore (
1180+ NewDVR, Builder.GetInsertPoint ());
1181+ // This dbg.declare_value is for the main function entry point. It
1182+ // will be deleted in all coro-split functions.
1183+ coro::salvageDebugInfo (ArgToAllocaMap, *DDI, false /* UseEntryValue*/ );
1184+ };
1185+ for_each (DVRDeclareValues, SalvageOneCoro);
1186+
11451187 // If we have a single edge PHINode, remove it and replace it with a
11461188 // reload from the coroutine frame. (We already took care of multi edge
11471189 // PHINodes by normalizing them in the rewritePHIs function).
@@ -1925,7 +1967,7 @@ void coro::salvageDebugInfo(
19251967 Function *F = DVR.getFunction ();
19261968 // Follow the pointer arithmetic all the way to the incoming
19271969 // function argument and convert into a DIExpression.
1928- bool SkipOutermostLoad = DVR.isDbgDeclare ();
1970+ bool SkipOutermostLoad = DVR.isDbgDeclare () || DVR. isDbgDeclareValue () ;
19291971 Value *OriginalStorage = DVR.getVariableLocationOp (0 );
19301972
19311973 auto SalvagedInfo =
@@ -1939,10 +1981,11 @@ void coro::salvageDebugInfo(
19391981
19401982 DVR.replaceVariableLocationOp (OriginalStorage, Storage);
19411983 DVR.setExpression (Expr);
1942- // We only hoist dbg.declare today since it doesn't make sense to hoist
1943- // dbg.value since it does not have the same function wide guarantees that
1944- // dbg.declare does.
1945- if (DVR.getType () == DbgVariableRecord::LocationType::Declare) {
1984+ // We only hoist dbg.declare and dbg.declare_value today since it doesn't make
1985+ // sense to hoist dbg.value since it does not have the same function wide
1986+ // guarantees that dbg.declare does.
1987+ if (DVR.getType () == DbgVariableRecord::LocationType::Declare ||
1988+ DVR.getType () == DbgVariableRecord::LocationType::DeclareValue) {
19461989 std::optional<BasicBlock::iterator> InsertPt;
19471990 if (auto *I = dyn_cast<Instruction>(Storage)) {
19481991 InsertPt = I->getInsertionPointAfterDef ();
@@ -1957,6 +2000,19 @@ void coro::salvageDebugInfo(
19572000 InsertPt = F->getEntryBlock ().begin ();
19582001 if (InsertPt) {
19592002 DVR.removeFromParent ();
2003+ // If there is a dbg.declare_value being reinserted, insert it as a
2004+ // dbg.declare instead, so that subsequent passes don't have to deal with
2005+ // a dbg.declare_value.
2006+ if (DVR.getType () == DbgVariableRecord::LocationType::DeclareValue) {
2007+ auto *MD = DVR.getRawLocation ();
2008+ if (auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
2009+ Type *Ty = VAM->getValue ()->getType ();
2010+ if (Ty->isPointerTy ())
2011+ DVR.Type = DbgVariableRecord::LocationType::Declare;
2012+ else
2013+ DVR.Type = DbgVariableRecord::LocationType::Value;
2014+ }
2015+ }
19602016 (*InsertPt)->getParent ()->insertDbgRecordBefore (&DVR, *InsertPt);
19612017 }
19622018 }
0 commit comments