32
32
#include " llvm/Analysis/CFG.h"
33
33
#include " llvm/Analysis/CallGraph.h"
34
34
#include " llvm/Analysis/ConstantFolding.h"
35
+ #include " llvm/Analysis/DebugInfoCache.h"
35
36
#include " llvm/Analysis/LazyCallGraph.h"
36
37
#include " llvm/Analysis/OptimizationRemarkEmitter.h"
37
38
#include " llvm/Analysis/TargetTransformInfo.h"
@@ -79,15 +80,38 @@ using namespace llvm;
79
80
#define DEBUG_TYPE " coro-split"
80
81
81
82
namespace {
83
+ const DebugInfoFinder *cachedDIFinder (Function &F,
84
+ const DebugInfoCache *DICache) {
85
+ if (!DICache)
86
+ return nullptr ;
87
+
88
+ auto *SP = F.getSubprogram ();
89
+ auto *CU = SP ? SP->getUnit () : nullptr ;
90
+ if (!CU)
91
+ return nullptr ;
92
+
93
+ if (auto Found = DICache->Result .find (CU); Found != DICache->Result .end ())
94
+ return &Found->getSecond ();
95
+
96
+ return nullptr ;
97
+ }
98
+
82
99
// / Collect (a known) subset of global debug info metadata potentially used by
83
100
// / the function \p F.
84
101
// /
85
102
// / This metadata set can be used to avoid cloning debug info not owned by \p F
86
103
// / and is shared among all potential clones \p F.
87
- MetadataSetTy collectCommonDebugInfo (Function &F) {
104
+ MetadataSetTy collectCommonDebugInfo (Function &F,
105
+ const DebugInfoCache *DICache) {
88
106
TimeTraceScope FunctionScope (" CollectCommonDebugInfo" );
89
107
90
108
DebugInfoFinder DIFinder;
109
+
110
+ // Copy DIFinder from cache which is primed on F's compile unit when available
111
+ auto *PrimedDIFinder = cachedDIFinder (F, DICache);
112
+ if (PrimedDIFinder)
113
+ DIFinder = *PrimedDIFinder;
114
+
91
115
DISubprogram *SPClonedWithinModule = CollectDebugInfoForCloning (
92
116
F, CloneFunctionChangeType::LocalChangesOnly, DIFinder);
93
117
@@ -1393,10 +1417,10 @@ namespace {
1393
1417
struct SwitchCoroutineSplitter {
1394
1418
static void split (Function &F, coro::Shape &Shape,
1395
1419
SmallVectorImpl<Function *> &Clones,
1396
- TargetTransformInfo &TTI) {
1420
+ TargetTransformInfo &TTI, const DebugInfoCache *DICache ) {
1397
1421
assert (Shape.ABI == coro::ABI::Switch);
1398
1422
1399
- MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F)};
1423
+ MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F, DICache )};
1400
1424
1401
1425
// Create a resume clone by cloning the body of the original function,
1402
1426
// setting new entry block and replacing coro.suspend an appropriate value
@@ -1710,7 +1734,8 @@ CallInst *coro::createMustTailCall(DebugLoc Loc, Function *MustTailCallFn,
1710
1734
1711
1735
void coro::AsyncABI::splitCoroutine (Function &F, coro::Shape &Shape,
1712
1736
SmallVectorImpl<Function *> &Clones,
1713
- TargetTransformInfo &TTI) {
1737
+ TargetTransformInfo &TTI,
1738
+ const DebugInfoCache *DICache) {
1714
1739
assert (Shape.ABI == coro::ABI::Async);
1715
1740
assert (Clones.empty ());
1716
1741
// Reset various things that the optimizer might have decided it
@@ -1796,7 +1821,7 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
1796
1821
1797
1822
assert (Clones.size () == Shape.CoroSuspends .size ());
1798
1823
1799
- MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F)};
1824
+ MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F, DICache )};
1800
1825
1801
1826
for (auto [Idx, CS] : llvm::enumerate (Shape.CoroSuspends )) {
1802
1827
auto *Suspend = CS;
@@ -1809,7 +1834,8 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
1809
1834
1810
1835
void coro::AnyRetconABI::splitCoroutine (Function &F, coro::Shape &Shape,
1811
1836
SmallVectorImpl<Function *> &Clones,
1812
- TargetTransformInfo &TTI) {
1837
+ TargetTransformInfo &TTI,
1838
+ const DebugInfoCache *DICache) {
1813
1839
assert (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce);
1814
1840
assert (Clones.empty ());
1815
1841
@@ -1930,7 +1956,7 @@ void coro::AnyRetconABI::splitCoroutine(Function &F, coro::Shape &Shape,
1930
1956
1931
1957
assert (Clones.size () == Shape.CoroSuspends .size ());
1932
1958
1933
- MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F)};
1959
+ MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F, DICache )};
1934
1960
1935
1961
for (auto [Idx, CS] : llvm::enumerate (Shape.CoroSuspends )) {
1936
1962
auto Suspend = CS;
@@ -1984,13 +2010,15 @@ static bool hasSafeElideCaller(Function &F) {
1984
2010
1985
2011
void coro::SwitchABI::splitCoroutine (Function &F, coro::Shape &Shape,
1986
2012
SmallVectorImpl<Function *> &Clones,
1987
- TargetTransformInfo &TTI) {
1988
- SwitchCoroutineSplitter::split (F, Shape, Clones, TTI);
2013
+ TargetTransformInfo &TTI,
2014
+ const DebugInfoCache *DICache) {
2015
+ SwitchCoroutineSplitter::split (F, Shape, Clones, TTI, DICache);
1989
2016
}
1990
2017
1991
2018
static void doSplitCoroutine (Function &F, SmallVectorImpl<Function *> &Clones,
1992
2019
coro::BaseABI &ABI, TargetTransformInfo &TTI,
1993
- bool OptimizeFrame) {
2020
+ bool OptimizeFrame,
2021
+ const DebugInfoCache *DICache) {
1994
2022
PrettyStackTraceFunction prettyStackTrace (F);
1995
2023
1996
2024
auto &Shape = ABI.Shape ;
@@ -2015,7 +2043,7 @@ static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
2015
2043
if (isNoSuspendCoroutine) {
2016
2044
handleNoSuspendCoroutine (Shape);
2017
2045
} else {
2018
- ABI.splitCoroutine (F, Shape, Clones, TTI);
2046
+ ABI.splitCoroutine (F, Shape, Clones, TTI, DICache );
2019
2047
}
2020
2048
2021
2049
// Replace all the swifterror operations in the original function.
@@ -2212,6 +2240,9 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2212
2240
auto &FAM =
2213
2241
AM.getResult <FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager ();
2214
2242
2243
+ const auto &MAMProxy = AM.getResult <ModuleAnalysisManagerCGSCCProxy>(C, CG);
2244
+ const auto *DICache = MAMProxy.getCachedResult <DebugInfoCacheAnalysis>(M);
2245
+
2215
2246
// Check for uses of llvm.coro.prepare.retcon/async.
2216
2247
SmallVector<Function *, 2 > PrepareFns;
2217
2248
addPrepareFunction (M, PrepareFns, " llvm.coro.prepare.retcon" );
@@ -2248,7 +2279,7 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2248
2279
2249
2280
SmallVector<Function *, 4 > Clones;
2250
2281
auto &TTI = FAM.getResult <TargetIRAnalysis>(F);
2251
- doSplitCoroutine (F, Clones, *ABI, TTI, OptimizeFrame);
2282
+ doSplitCoroutine (F, Clones, *ABI, TTI, OptimizeFrame, DICache );
2252
2283
CurrentSCC = &updateCallGraphAfterCoroutineSplit (
2253
2284
*N, Shape, Clones, *CurrentSCC, CG, AM, UR, FAM);
2254
2285
0 commit comments