3030#include " llvm/Analysis/CFG.h"
3131#include " llvm/Analysis/CallGraph.h"
3232#include " llvm/Analysis/ConstantFolding.h"
33+ #include " llvm/Analysis/DebugInfoCache.h"
3334#include " llvm/Analysis/LazyCallGraph.h"
3435#include " llvm/Analysis/OptimizationRemarkEmitter.h"
3536#include " llvm/Analysis/TargetTransformInfo.h"
@@ -84,15 +85,39 @@ using namespace llvm;
8485
8586namespace {
8687
88+ const DebugInfoFinder *cachedDIFinder (Function &F,
89+ const DebugInfoCache *DICache) {
90+ if (!DICache)
91+ return nullptr ;
92+
93+ auto *SP = F.getSubprogram ();
94+ auto *CU = SP ? SP->getUnit () : nullptr ;
95+ if (!CU)
96+ return nullptr ;
97+
98+ auto Found = DICache->Result .find (CU);
99+ if (Found == DICache->Result .end ())
100+ return nullptr ;
101+
102+ return &Found->getSecond ();
103+ }
104+
87105// / Collect (a known) subset of global debug info metadata potentially used by
88106// / the function \p F.
89107// /
90108// / This metadata set can be used to avoid cloning debug info not owned by \p F
91109// / and is shared among all potential clones \p F.
92- void collectGlobalDebugInfo (Function &F, MetadataSetTy &GlobalDebugInfo) {
110+ void collectGlobalDebugInfo (Function &F, MetadataSetTy &GlobalDebugInfo,
111+ const DebugInfoCache *DICache) {
93112 TimeTraceScope FunctionScope (" CollectGlobalDebugInfo" );
94113
95114 DebugInfoFinder DIFinder;
115+
116+ // Copy DIFinder from cache which is primed on F's compile unit when available
117+ auto *PrimedDIFinder = cachedDIFinder (F, DICache);
118+ if (PrimedDIFinder)
119+ DIFinder = *PrimedDIFinder;
120+
96121 DISubprogram *SPClonedWithinModule = ProcessSubprogramAttachment (
97122 F, CloneFunctionChangeType::LocalChangesOnly, DIFinder);
98123
@@ -1516,11 +1541,11 @@ namespace {
15161541struct SwitchCoroutineSplitter {
15171542 static void split (Function &F, coro::Shape &Shape,
15181543 SmallVectorImpl<Function *> &Clones,
1519- TargetTransformInfo &TTI) {
1544+ TargetTransformInfo &TTI, const DebugInfoCache *DICache ) {
15201545 assert (Shape.ABI == coro::ABI::Switch);
15211546
15221547 MetadataSetTy GlobalDebugInfo;
1523- collectGlobalDebugInfo (F, GlobalDebugInfo);
1548+ collectGlobalDebugInfo (F, GlobalDebugInfo, DICache );
15241549
15251550 // Create a resume clone by cloning the body of the original function,
15261551 // setting new entry block and replacing coro.suspend an appropriate value
@@ -1834,7 +1859,8 @@ CallInst *coro::createMustTailCall(DebugLoc Loc, Function *MustTailCallFn,
18341859
18351860void coro::AsyncABI::splitCoroutine (Function &F, coro::Shape &Shape,
18361861 SmallVectorImpl<Function *> &Clones,
1837- TargetTransformInfo &TTI) {
1862+ TargetTransformInfo &TTI,
1863+ const DebugInfoCache *DICache) {
18381864 assert (Shape.ABI == coro::ABI::Async);
18391865 assert (Clones.empty ());
18401866 // Reset various things that the optimizer might have decided it
@@ -1921,7 +1947,7 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
19211947 assert (Clones.size () == Shape.CoroSuspends .size ());
19221948
19231949 MetadataSetTy GlobalDebugInfo;
1924- collectGlobalDebugInfo (F, GlobalDebugInfo);
1950+ collectGlobalDebugInfo (F, GlobalDebugInfo, DICache );
19251951
19261952 for (size_t Idx = 0 , End = Shape.CoroSuspends .size (); Idx != End; ++Idx) {
19271953 auto *Suspend = Shape.CoroSuspends [Idx];
@@ -1934,7 +1960,8 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
19341960
19351961void coro::AnyRetconABI::splitCoroutine (Function &F, coro::Shape &Shape,
19361962 SmallVectorImpl<Function *> &Clones,
1937- TargetTransformInfo &TTI) {
1963+ TargetTransformInfo &TTI,
1964+ const DebugInfoCache *DICache) {
19381965 assert (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce);
19391966 assert (Clones.empty ());
19401967
@@ -2055,7 +2082,7 @@ void coro::AnyRetconABI::splitCoroutine(Function &F, coro::Shape &Shape,
20552082 assert (Clones.size () == Shape.CoroSuspends .size ());
20562083
20572084 MetadataSetTy GlobalDebugInfo;
2058- collectGlobalDebugInfo (F, GlobalDebugInfo);
2085+ collectGlobalDebugInfo (F, GlobalDebugInfo, DICache );
20592086
20602087 for (size_t i = 0 , e = Shape.CoroSuspends .size (); i != e; ++i) {
20612088 auto Suspend = Shape.CoroSuspends [i];
@@ -2109,13 +2136,15 @@ static bool hasSafeElideCaller(Function &F) {
21092136
21102137void coro::SwitchABI::splitCoroutine (Function &F, coro::Shape &Shape,
21112138 SmallVectorImpl<Function *> &Clones,
2112- TargetTransformInfo &TTI) {
2113- SwitchCoroutineSplitter::split (F, Shape, Clones, TTI);
2139+ TargetTransformInfo &TTI,
2140+ const DebugInfoCache *DICache) {
2141+ SwitchCoroutineSplitter::split (F, Shape, Clones, TTI, DICache);
21142142}
21152143
21162144static void doSplitCoroutine (Function &F, SmallVectorImpl<Function *> &Clones,
21172145 coro::BaseABI &ABI, TargetTransformInfo &TTI,
2118- bool OptimizeFrame) {
2146+ bool OptimizeFrame,
2147+ const DebugInfoCache *DICache) {
21192148 PrettyStackTraceFunction prettyStackTrace (F);
21202149
21212150 auto &Shape = ABI.Shape ;
@@ -2140,7 +2169,7 @@ static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
21402169 if (isNoSuspendCoroutine) {
21412170 handleNoSuspendCoroutine (Shape);
21422171 } else {
2143- ABI.splitCoroutine (F, Shape, Clones, TTI);
2172+ ABI.splitCoroutine (F, Shape, Clones, TTI, DICache );
21442173 }
21452174
21462175 // Replace all the swifterror operations in the original function.
@@ -2337,6 +2366,9 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
23372366 auto &FAM =
23382367 AM.getResult <FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager ();
23392368
2369+ const auto &MAMProxy = AM.getResult <ModuleAnalysisManagerCGSCCProxy>(C, CG);
2370+ const auto *DICache = MAMProxy.getCachedResult <DebugInfoCacheAnalysis>(M);
2371+
23402372 // Check for uses of llvm.coro.prepare.retcon/async.
23412373 SmallVector<Function *, 2 > PrepareFns;
23422374 addPrepareFunction (M, PrepareFns, " llvm.coro.prepare.retcon" );
@@ -2373,7 +2405,7 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
23732405
23742406 SmallVector<Function *, 4 > Clones;
23752407 auto &TTI = FAM.getResult <TargetIRAnalysis>(F);
2376- doSplitCoroutine (F, Clones, *ABI, TTI, OptimizeFrame);
2408+ doSplitCoroutine (F, Clones, *ABI, TTI, OptimizeFrame, DICache );
23772409 CurrentSCC = &updateCallGraphAfterCoroutineSplit (
23782410 *N, Shape, Clones, *CurrentSCC, CG, AM, UR, FAM);
23792411
0 commit comments