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,39 @@ 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
+ auto Found = DICache->Result .find (CU);
94
+ if (Found == DICache->Result .end ())
95
+ return nullptr ;
96
+
97
+ return &Found->getSecond ();
98
+ }
99
+
82
100
// / Collect (a known) subset of global debug info metadata potentially used by
83
101
// / the function \p F.
84
102
// /
85
103
// / This metadata set can be used to avoid cloning debug info not owned by \p F
86
104
// / and is shared among all potential clones \p F.
87
- void collectGlobalDebugInfo (Function &F, MetadataSetTy &GlobalDebugInfo) {
105
+ void collectGlobalDebugInfo (Function &F, MetadataSetTy &GlobalDebugInfo,
106
+ const DebugInfoCache *DICache) {
88
107
TimeTraceScope FunctionScope (" CollectGlobalDebugInfo" );
89
108
90
109
DebugInfoFinder DIFinder;
110
+
111
+ // Copy DIFinder from cache which is primed on F's compile unit when available
112
+ auto *PrimedDIFinder = cachedDIFinder (F, DICache);
113
+ if (PrimedDIFinder)
114
+ DIFinder = *PrimedDIFinder;
115
+
91
116
DISubprogram *SPClonedWithinModule = CollectDebugInfoForCloning (
92
117
F, CloneFunctionChangeType::LocalChangesOnly, DIFinder);
93
118
@@ -1394,11 +1419,11 @@ namespace {
1394
1419
struct SwitchCoroutineSplitter {
1395
1420
static void split (Function &F, coro::Shape &Shape,
1396
1421
SmallVectorImpl<Function *> &Clones,
1397
- TargetTransformInfo &TTI) {
1422
+ TargetTransformInfo &TTI, const DebugInfoCache *DICache ) {
1398
1423
assert (Shape.ABI == coro::ABI::Switch);
1399
1424
1400
1425
MetadataSetTy GlobalDebugInfo;
1401
- collectGlobalDebugInfo (F, GlobalDebugInfo);
1426
+ collectGlobalDebugInfo (F, GlobalDebugInfo, DICache );
1402
1427
1403
1428
// Create a resume clone by cloning the body of the original function,
1404
1429
// setting new entry block and replacing coro.suspend an appropriate value
@@ -1712,7 +1737,8 @@ CallInst *coro::createMustTailCall(DebugLoc Loc, Function *MustTailCallFn,
1712
1737
1713
1738
void coro::AsyncABI::splitCoroutine (Function &F, coro::Shape &Shape,
1714
1739
SmallVectorImpl<Function *> &Clones,
1715
- TargetTransformInfo &TTI) {
1740
+ TargetTransformInfo &TTI,
1741
+ const DebugInfoCache *DICache) {
1716
1742
assert (Shape.ABI == coro::ABI::Async);
1717
1743
assert (Clones.empty ());
1718
1744
// Reset various things that the optimizer might have decided it
@@ -1799,7 +1825,7 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
1799
1825
assert (Clones.size () == Shape.CoroSuspends .size ());
1800
1826
1801
1827
MetadataSetTy GlobalDebugInfo;
1802
- collectGlobalDebugInfo (F, GlobalDebugInfo);
1828
+ collectGlobalDebugInfo (F, GlobalDebugInfo, DICache );
1803
1829
1804
1830
for (auto [Idx, CS] : llvm::enumerate (Shape.CoroSuspends )) {
1805
1831
auto *Suspend = CS;
@@ -1812,7 +1838,8 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
1812
1838
1813
1839
void coro::AnyRetconABI::splitCoroutine (Function &F, coro::Shape &Shape,
1814
1840
SmallVectorImpl<Function *> &Clones,
1815
- TargetTransformInfo &TTI) {
1841
+ TargetTransformInfo &TTI,
1842
+ const DebugInfoCache *DICache) {
1816
1843
assert (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce);
1817
1844
assert (Clones.empty ());
1818
1845
@@ -1934,7 +1961,7 @@ void coro::AnyRetconABI::splitCoroutine(Function &F, coro::Shape &Shape,
1934
1961
assert (Clones.size () == Shape.CoroSuspends .size ());
1935
1962
1936
1963
MetadataSetTy GlobalDebugInfo;
1937
- collectGlobalDebugInfo (F, GlobalDebugInfo);
1964
+ collectGlobalDebugInfo (F, GlobalDebugInfo, DICache );
1938
1965
1939
1966
for (auto [Idx, CS] : llvm::enumerate (Shape.CoroSuspends )) {
1940
1967
auto Suspend = CS;
@@ -1988,13 +2015,15 @@ static bool hasSafeElideCaller(Function &F) {
1988
2015
1989
2016
void coro::SwitchABI::splitCoroutine (Function &F, coro::Shape &Shape,
1990
2017
SmallVectorImpl<Function *> &Clones,
1991
- TargetTransformInfo &TTI) {
1992
- SwitchCoroutineSplitter::split (F, Shape, Clones, TTI);
2018
+ TargetTransformInfo &TTI,
2019
+ const DebugInfoCache *DICache) {
2020
+ SwitchCoroutineSplitter::split (F, Shape, Clones, TTI, DICache);
1993
2021
}
1994
2022
1995
2023
static void doSplitCoroutine (Function &F, SmallVectorImpl<Function *> &Clones,
1996
2024
coro::BaseABI &ABI, TargetTransformInfo &TTI,
1997
- bool OptimizeFrame) {
2025
+ bool OptimizeFrame,
2026
+ const DebugInfoCache *DICache) {
1998
2027
PrettyStackTraceFunction prettyStackTrace (F);
1999
2028
2000
2029
auto &Shape = ABI.Shape ;
@@ -2019,7 +2048,7 @@ static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
2019
2048
if (isNoSuspendCoroutine) {
2020
2049
handleNoSuspendCoroutine (Shape);
2021
2050
} else {
2022
- ABI.splitCoroutine (F, Shape, Clones, TTI);
2051
+ ABI.splitCoroutine (F, Shape, Clones, TTI, DICache );
2023
2052
}
2024
2053
2025
2054
// Replace all the swifterror operations in the original function.
@@ -2216,6 +2245,9 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2216
2245
auto &FAM =
2217
2246
AM.getResult <FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager ();
2218
2247
2248
+ const auto &MAMProxy = AM.getResult <ModuleAnalysisManagerCGSCCProxy>(C, CG);
2249
+ const auto *DICache = MAMProxy.getCachedResult <DebugInfoCacheAnalysis>(M);
2250
+
2219
2251
// Check for uses of llvm.coro.prepare.retcon/async.
2220
2252
SmallVector<Function *, 2 > PrepareFns;
2221
2253
addPrepareFunction (M, PrepareFns, " llvm.coro.prepare.retcon" );
@@ -2252,7 +2284,7 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2252
2284
2253
2285
SmallVector<Function *, 4 > Clones;
2254
2286
auto &TTI = FAM.getResult <TargetIRAnalysis>(F);
2255
- doSplitCoroutine (F, Clones, *ABI, TTI, OptimizeFrame);
2287
+ doSplitCoroutine (F, Clones, *ABI, TTI, OptimizeFrame, DICache );
2256
2288
CurrentSCC = &updateCallGraphAfterCoroutineSplit (
2257
2289
*N, Shape, Clones, *CurrentSCC, CG, AM, UR, FAM);
2258
2290
0 commit comments