@@ -964,12 +964,17 @@ static void cacheDIVar(FrameDataInfo &FrameData,
964
964
continue ;
965
965
966
966
SmallVector<DbgDeclareInst *, 1 > DDIs;
967
- findDbgDeclares (DDIs, V);
968
- auto *I = llvm::find_if (DDIs, [](DbgDeclareInst *DDI) {
969
- return DDI->getExpression ()->getNumElements () == 0 ;
970
- });
971
- if (I != DDIs.end ())
972
- DIVarCache.insert ({V, (*I)->getVariable ()});
967
+ SmallVector<DPValue *, 1 > DPVs;
968
+ findDbgDeclares (DDIs, V, &DPVs);
969
+ auto CacheIt = [&DIVarCache, V](auto &Container) {
970
+ auto *I = llvm::find_if (Container, [](auto *DDI) {
971
+ return DDI->getExpression ()->getNumElements () == 0 ;
972
+ });
973
+ if (I != Container.end ())
974
+ DIVarCache.insert ({V, (*I)->getVariable ()});
975
+ };
976
+ CacheIt (DDIs);
977
+ CacheIt (DPVs);
973
978
}
974
979
}
975
980
@@ -1121,15 +1126,25 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
1121
1126
" Coroutine with switch ABI should own Promise alloca" );
1122
1127
1123
1128
SmallVector<DbgDeclareInst *, 1 > DIs;
1124
- findDbgDeclares (DIs, PromiseAlloca);
1125
- if (DIs.empty ())
1129
+ SmallVector<DPValue *, 1 > DPVs;
1130
+ findDbgDeclares (DIs, PromiseAlloca, &DPVs);
1131
+
1132
+ DILocalVariable *PromiseDIVariable = nullptr ;
1133
+ DILocation *DILoc = nullptr ;
1134
+ if (!DIs.empty ()) {
1135
+ DbgDeclareInst *PromiseDDI = DIs.front ();
1136
+ PromiseDIVariable = PromiseDDI->getVariable ();
1137
+ DILoc = PromiseDDI->getDebugLoc ().get ();
1138
+ } else if (!DPVs.empty ()) {
1139
+ DPValue *PromiseDPV = DPVs.front ();
1140
+ PromiseDIVariable = PromiseDPV->getVariable ();
1141
+ DILoc = PromiseDPV->getDebugLoc ().get ();
1142
+ } else {
1126
1143
return ;
1144
+ }
1127
1145
1128
- DbgDeclareInst *PromiseDDI = DIs.front ();
1129
- DILocalVariable *PromiseDIVariable = PromiseDDI->getVariable ();
1130
1146
DILocalScope *PromiseDIScope = PromiseDIVariable->getScope ();
1131
1147
DIFile *DFile = PromiseDIScope->getFile ();
1132
- DILocation *DILoc = PromiseDDI->getDebugLoc ().get ();
1133
1148
unsigned LineNum = PromiseDIVariable->getLine ();
1134
1149
1135
1150
DICompositeType *FrameDITy = DBuilder.createStructType (
@@ -1243,7 +1258,7 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
1243
1258
auto *FrameDIVar = DBuilder.createAutoVariable (PromiseDIScope, " __coro_frame" ,
1244
1259
DFile, LineNum, FrameDITy,
1245
1260
true , DINode::FlagArtificial);
1246
- assert (FrameDIVar->isValidLocationForIntrinsic (PromiseDDI-> getDebugLoc () ));
1261
+ assert (FrameDIVar->isValidLocationForIntrinsic (DILoc ));
1247
1262
1248
1263
// Subprogram would have ContainedNodes field which records the debug
1249
1264
// variables it contained. So we need to add __coro_frame to the
@@ -1261,9 +1276,17 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
1261
1276
7 , (MDTuple::get (F.getContext (), RetainedNodesVec)));
1262
1277
}
1263
1278
1264
- DBuilder.insertDeclare (Shape.FramePtr , FrameDIVar,
1265
- DBuilder.createExpression (), DILoc,
1266
- Shape.getInsertPtAfterFramePtr ());
1279
+ if (UseNewDbgInfoFormat) {
1280
+ DPValue *NewDPV = new DPValue (ValueAsMetadata::get (Shape.FramePtr ),
1281
+ FrameDIVar, DBuilder.createExpression (),
1282
+ DILoc, DPValue::LocationType::Declare);
1283
+ BasicBlock::iterator It = Shape.getInsertPtAfterFramePtr ();
1284
+ It->getParent ()->insertDPValueBefore (NewDPV, It);
1285
+ } else {
1286
+ DBuilder.insertDeclare (Shape.FramePtr , FrameDIVar,
1287
+ DBuilder.createExpression (), DILoc,
1288
+ &*Shape.getInsertPtAfterFramePtr ());
1289
+ }
1267
1290
}
1268
1291
1269
1292
// Build a struct that will keep state for an active coroutine.
@@ -1771,7 +1794,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1771
1794
if (auto *Arg = dyn_cast<Argument>(Def)) {
1772
1795
// For arguments, we will place the store instruction right after
1773
1796
// the coroutine frame pointer instruction, i.e. coro.begin.
1774
- InsertPt = Shape.getInsertPtAfterFramePtr ()-> getIterator () ;
1797
+ InsertPt = Shape.getInsertPtAfterFramePtr ();
1775
1798
1776
1799
// If we're spilling an Argument, make sure we clear 'nocapture'
1777
1800
// from the coroutine function.
@@ -1788,7 +1811,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1788
1811
if (!DT.dominates (CB, I)) {
1789
1812
// If it is not dominated by CoroBegin, then spill should be
1790
1813
// inserted immediately after CoroFrame is computed.
1791
- InsertPt = Shape.getInsertPtAfterFramePtr ()-> getIterator () ;
1814
+ InsertPt = Shape.getInsertPtAfterFramePtr ();
1792
1815
} else if (auto *II = dyn_cast<InvokeInst>(I)) {
1793
1816
// If we are spilling the result of the invoke instruction, split
1794
1817
// the normal edge and insert the spill in the new block.
@@ -1843,7 +1866,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1843
1866
SpillAlignment, E.first ->getName () + Twine (" .reload" ));
1844
1867
1845
1868
SmallVector<DbgDeclareInst *, 1 > DIs;
1846
- findDbgDeclares (DIs, Def);
1869
+ SmallVector<DPValue *, 1 > DPVs;
1870
+ findDbgDeclares (DIs, Def, &DPVs);
1847
1871
// Try best to find dbg.declare. If the spill is a temp, there may not
1848
1872
// be a direct dbg.declare. Walk up the load chain to find one from an
1849
1873
// alias.
@@ -1858,24 +1882,36 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1858
1882
if (!isa<AllocaInst, LoadInst>(CurDef))
1859
1883
break ;
1860
1884
DIs.clear ();
1861
- findDbgDeclares (DIs, CurDef);
1885
+ DPVs.clear ();
1886
+ findDbgDeclares (DIs, CurDef, &DPVs);
1862
1887
}
1863
1888
}
1864
1889
1865
- for (DbgDeclareInst *DDI : DIs ) {
1890
+ auto SalvageOne = [&]( auto *DDI) {
1866
1891
bool AllowUnresolved = false ;
1867
1892
// This dbg.declare is preserved for all coro-split function
1868
1893
// fragments. It will be unreachable in the main function, and
1869
1894
// processed by coro::salvageDebugInfo() by CoroCloner.
1870
- DIBuilder (*CurrentBlock->getParent ()->getParent (), AllowUnresolved)
1871
- .insertDeclare (CurrentReload, DDI->getVariable (),
1872
- DDI->getExpression (), DDI->getDebugLoc (),
1873
- &*Builder.GetInsertPoint ());
1895
+ if (UseNewDbgInfoFormat) {
1896
+ DPValue *NewDPV =
1897
+ new DPValue (ValueAsMetadata::get (CurrentReload),
1898
+ DDI->getVariable (), DDI->getExpression (),
1899
+ DDI->getDebugLoc (), DPValue::LocationType::Declare);
1900
+ Builder.GetInsertPoint ()->getParent ()->insertDPValueBefore (
1901
+ NewDPV, Builder.GetInsertPoint ());
1902
+ } else {
1903
+ DIBuilder (*CurrentBlock->getParent ()->getParent (), AllowUnresolved)
1904
+ .insertDeclare (CurrentReload, DDI->getVariable (),
1905
+ DDI->getExpression (), DDI->getDebugLoc (),
1906
+ &*Builder.GetInsertPoint ());
1907
+ }
1874
1908
// This dbg.declare is for the main function entry point. It
1875
1909
// will be deleted in all coro-split functions.
1876
- coro::salvageDebugInfo (ArgToAllocaMap, DDI, Shape.OptimizeFrame ,
1910
+ coro::salvageDebugInfo (ArgToAllocaMap, * DDI, Shape.OptimizeFrame ,
1877
1911
false /* UseEntryValue*/ );
1878
- }
1912
+ };
1913
+ for_each (DIs, SalvageOne);
1914
+ for_each (DPVs, SalvageOne);
1879
1915
}
1880
1916
1881
1917
// If we have a single edge PHINode, remove it and replace it with a
@@ -1893,6 +1929,10 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1893
1929
// Replace all uses of CurrentValue in the current instruction with
1894
1930
// reload.
1895
1931
U->replaceUsesOfWith (Def, CurrentReload);
1932
+ // Instructions are added to Def's user list if the attached
1933
+ // debug records use Def. Update those now.
1934
+ for (auto &DPV : U->getDbgValueRange ())
1935
+ DPV.replaceVariableLocationOp (Def, CurrentReload, true );
1896
1936
}
1897
1937
}
1898
1938
@@ -1943,9 +1983,12 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1943
1983
G->setName (Alloca->getName () + Twine (" .reload.addr" ));
1944
1984
1945
1985
SmallVector<DbgVariableIntrinsic *, 4 > DIs;
1946
- findDbgUsers (DIs, Alloca);
1986
+ SmallVector<DPValue *> DPValues;
1987
+ findDbgUsers (DIs, Alloca, &DPValues);
1947
1988
for (auto *DVI : DIs)
1948
1989
DVI->replaceUsesOfWith (Alloca, G);
1990
+ for (auto *DPV : DPValues)
1991
+ DPV->replaceVariableLocationOp (Alloca, G);
1949
1992
1950
1993
for (Instruction *I : UsersToUpdate) {
1951
1994
// It is meaningless to retain the lifetime intrinsics refer for the
@@ -1959,7 +2002,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1959
2002
I->replaceUsesOfWith (Alloca, G);
1960
2003
}
1961
2004
}
1962
- Builder.SetInsertPoint (Shape.getInsertPtAfterFramePtr ());
2005
+ Builder.SetInsertPoint (&* Shape.getInsertPtAfterFramePtr ());
1963
2006
for (const auto &A : FrameData.Allocas ) {
1964
2007
AllocaInst *Alloca = A.Alloca ;
1965
2008
if (A.MayWriteBeforeCoroBegin ) {
@@ -2020,7 +2063,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
2020
2063
isa<BitCastInst>(Inst);
2021
2064
});
2022
2065
if (HasAccessingPromiseBeforeCB) {
2023
- Builder.SetInsertPoint (Shape.getInsertPtAfterFramePtr ());
2066
+ Builder.SetInsertPoint (&* Shape.getInsertPtAfterFramePtr ());
2024
2067
auto *G = GetFramePointer (PA);
2025
2068
auto *Value = Builder.CreateLoad (PA->getAllocatedType (), PA);
2026
2069
Builder.CreateStore (Value, G);
@@ -2802,21 +2845,16 @@ static void collectFrameAlloca(AllocaInst *AI, coro::Shape &Shape,
2802
2845
Visitor.getMayWriteBeforeCoroBegin ());
2803
2846
}
2804
2847
2805
- void coro::salvageDebugInfo (
2806
- SmallDenseMap<Argument *, AllocaInst *, 4 > &ArgToAllocaMap,
2807
- DbgVariableIntrinsic *DVI, bool OptimizeFrame, bool UseEntryValue) {
2808
- Function *F = DVI->getFunction ();
2848
+ static std::optional<std::pair<Value &, DIExpression &>>
2849
+ salvageDebugInfoImpl (SmallDenseMap<Argument *, AllocaInst *, 4 > &ArgToAllocaMap,
2850
+ bool OptimizeFrame, bool UseEntryValue, Function *F,
2851
+ Value *Storage, DIExpression *Expr,
2852
+ bool SkipOutermostLoad) {
2809
2853
IRBuilder<> Builder (F->getContext ());
2810
2854
auto InsertPt = F->getEntryBlock ().getFirstInsertionPt ();
2811
2855
while (isa<IntrinsicInst>(InsertPt))
2812
2856
++InsertPt;
2813
2857
Builder.SetInsertPoint (&F->getEntryBlock (), InsertPt);
2814
- DIExpression *Expr = DVI->getExpression ();
2815
- // Follow the pointer arithmetic all the way to the incoming
2816
- // function argument and convert into a DIExpression.
2817
- bool SkipOutermostLoad = !isa<DbgValueInst>(DVI);
2818
- Value *Storage = DVI->getVariableLocationOp (0 );
2819
- Value *OriginalStorage = Storage;
2820
2858
2821
2859
while (auto *Inst = dyn_cast_or_null<Instruction>(Storage)) {
2822
2860
if (auto *LdInst = dyn_cast<LoadInst>(Inst)) {
@@ -2848,7 +2886,7 @@ void coro::salvageDebugInfo(
2848
2886
SkipOutermostLoad = false ;
2849
2887
}
2850
2888
if (!Storage)
2851
- return ;
2889
+ return std::nullopt ;
2852
2890
2853
2891
auto *StorageAsArg = dyn_cast<Argument>(Storage);
2854
2892
const bool IsSwiftAsyncArg =
@@ -2884,8 +2922,30 @@ void coro::salvageDebugInfo(
2884
2922
Expr = DIExpression::prepend (Expr, DIExpression::DerefBefore);
2885
2923
}
2886
2924
2887
- DVI->replaceVariableLocationOp (OriginalStorage, Storage);
2888
- DVI->setExpression (Expr);
2925
+ return {{*Storage, *Expr}};
2926
+ }
2927
+
2928
+ void coro::salvageDebugInfo (
2929
+ SmallDenseMap<Argument *, AllocaInst *, 4 > &ArgToAllocaMap,
2930
+ DbgVariableIntrinsic &DVI, bool OptimizeFrame, bool UseEntryValue) {
2931
+
2932
+ Function *F = DVI.getFunction ();
2933
+ // Follow the pointer arithmetic all the way to the incoming
2934
+ // function argument and convert into a DIExpression.
2935
+ bool SkipOutermostLoad = !isa<DbgValueInst>(DVI);
2936
+ Value *OriginalStorage = DVI.getVariableLocationOp (0 );
2937
+
2938
+ auto SalvagedInfo = ::salvageDebugInfoImpl (
2939
+ ArgToAllocaMap, OptimizeFrame, UseEntryValue, F, OriginalStorage,
2940
+ DVI.getExpression (), SkipOutermostLoad);
2941
+ if (!SalvagedInfo)
2942
+ return ;
2943
+
2944
+ Value *Storage = &SalvagedInfo->first ;
2945
+ DIExpression *Expr = &SalvagedInfo->second ;
2946
+
2947
+ DVI.replaceVariableLocationOp (OriginalStorage, Storage);
2948
+ DVI.setExpression (Expr);
2889
2949
// We only hoist dbg.declare today since it doesn't make sense to hoist
2890
2950
// dbg.value since it does not have the same function wide guarantees that
2891
2951
// dbg.declare does.
@@ -2896,7 +2956,44 @@ void coro::salvageDebugInfo(
2896
2956
else if (isa<Argument>(Storage))
2897
2957
InsertPt = F->getEntryBlock ().begin ();
2898
2958
if (InsertPt)
2899
- DVI->moveBefore (*(*InsertPt)->getParent (), *InsertPt);
2959
+ DVI.moveBefore (*(*InsertPt)->getParent (), *InsertPt);
2960
+ }
2961
+ }
2962
+
2963
+ void coro::salvageDebugInfo (
2964
+ SmallDenseMap<Argument *, AllocaInst *, 4 > &ArgToAllocaMap, DPValue &DPV,
2965
+ bool OptimizeFrame, bool UseEntryValue) {
2966
+
2967
+ Function *F = DPV.getFunction ();
2968
+ // Follow the pointer arithmetic all the way to the incoming
2969
+ // function argument and convert into a DIExpression.
2970
+ bool SkipOutermostLoad = DPV.getType () == DPValue::LocationType::Declare;
2971
+ Value *OriginalStorage = DPV.getVariableLocationOp (0 );
2972
+
2973
+ auto SalvagedInfo = ::salvageDebugInfoImpl (
2974
+ ArgToAllocaMap, OptimizeFrame, UseEntryValue, F, OriginalStorage,
2975
+ DPV.getExpression (), SkipOutermostLoad);
2976
+ if (!SalvagedInfo)
2977
+ return ;
2978
+
2979
+ Value *Storage = &SalvagedInfo->first ;
2980
+ DIExpression *Expr = &SalvagedInfo->second ;
2981
+
2982
+ DPV.replaceVariableLocationOp (OriginalStorage, Storage);
2983
+ DPV.setExpression (Expr);
2984
+ // We only hoist dbg.declare today since it doesn't make sense to hoist
2985
+ // dbg.value since it does not have the same function wide guarantees that
2986
+ // dbg.declare does.
2987
+ if (DPV.getType () == DPValue::LocationType::Declare) {
2988
+ std::optional<BasicBlock::iterator> InsertPt;
2989
+ if (auto *I = dyn_cast<Instruction>(Storage))
2990
+ InsertPt = I->getInsertionPointAfterDef ();
2991
+ else if (isa<Argument>(Storage))
2992
+ InsertPt = F->getEntryBlock ().begin ();
2993
+ if (InsertPt) {
2994
+ DPV.removeFromParent ();
2995
+ (*InsertPt)->getParent ()->insertDPValueBefore (&DPV, *InsertPt);
2996
+ }
2900
2997
}
2901
2998
}
2902
2999
@@ -3087,10 +3184,15 @@ void coro::buildCoroutineFrame(
3087
3184
for (auto &Iter : FrameData.Spills ) {
3088
3185
auto *V = Iter.first ;
3089
3186
SmallVector<DbgValueInst *, 16 > DVIs;
3090
- findDbgValues (DVIs, V);
3187
+ SmallVector<DPValue *, 16 > DPVs;
3188
+ findDbgValues (DVIs, V, &DPVs);
3091
3189
for (DbgValueInst *DVI : DVIs)
3092
3190
if (Checker.isDefinitionAcrossSuspend (*V, DVI))
3093
3191
FrameData.Spills [V].push_back (DVI);
3192
+ // Add the instructions which carry debug info that is in the frame.
3193
+ for (DPValue *DPV : DPVs)
3194
+ if (Checker.isDefinitionAcrossSuspend (*V, DPV->Marker ->MarkedInstr ))
3195
+ FrameData.Spills [V].push_back (DPV->Marker ->MarkedInstr );
3094
3196
}
3095
3197
3096
3198
LLVM_DEBUG (dumpSpills (" Spills" , FrameData.Spills ));
0 commit comments