3232#include " llvm/Analysis/CFG.h"
3333#include " llvm/Analysis/CallGraph.h"
3434#include " llvm/Analysis/ConstantFolding.h"
35+ #include " llvm/Analysis/DebugInfoCache.h"
3536#include " llvm/Analysis/LazyCallGraph.h"
3637#include " llvm/Analysis/OptimizationRemarkEmitter.h"
3738#include " llvm/Analysis/TargetTransformInfo.h"
@@ -79,15 +80,38 @@ using namespace llvm;
7980#define DEBUG_TYPE " coro-split"
8081
8182namespace {
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+
8299// / Collect (a known) subset of global debug info metadata potentially used by
83100// / the function \p F.
84101// /
85102// / This metadata set can be used to avoid cloning debug info not owned by \p F
86103// / and is shared among all potential clones \p F.
87- MetadataSetTy collectCommonDebugInfo (Function &F) {
104+ MetadataSetTy collectCommonDebugInfo (Function &F,
105+ const DebugInfoCache *DICache) {
88106 TimeTraceScope FunctionScope (" CollectCommonDebugInfo" );
89107
90108 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+
91115 DISubprogram *SPClonedWithinModule = CollectDebugInfoForCloning (
92116 F, CloneFunctionChangeType::LocalChangesOnly, DIFinder);
93117
@@ -1393,10 +1417,10 @@ namespace {
13931417struct SwitchCoroutineSplitter {
13941418 static void split (Function &F, coro::Shape &Shape,
13951419 SmallVectorImpl<Function *> &Clones,
1396- TargetTransformInfo &TTI) {
1420+ TargetTransformInfo &TTI, const DebugInfoCache *DICache ) {
13971421 assert (Shape.ABI == coro::ABI::Switch);
13981422
1399- MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F)};
1423+ MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F, DICache )};
14001424
14011425 // Create a resume clone by cloning the body of the original function,
14021426 // setting new entry block and replacing coro.suspend an appropriate value
@@ -1710,7 +1734,8 @@ CallInst *coro::createMustTailCall(DebugLoc Loc, Function *MustTailCallFn,
17101734
17111735void coro::AsyncABI::splitCoroutine (Function &F, coro::Shape &Shape,
17121736 SmallVectorImpl<Function *> &Clones,
1713- TargetTransformInfo &TTI) {
1737+ TargetTransformInfo &TTI,
1738+ const DebugInfoCache *DICache) {
17141739 assert (Shape.ABI == coro::ABI::Async);
17151740 assert (Clones.empty ());
17161741 // Reset various things that the optimizer might have decided it
@@ -1796,7 +1821,7 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
17961821
17971822 assert (Clones.size () == Shape.CoroSuspends .size ());
17981823
1799- MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F)};
1824+ MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F, DICache )};
18001825
18011826 for (auto [Idx, CS] : llvm::enumerate (Shape.CoroSuspends )) {
18021827 auto *Suspend = CS;
@@ -1809,7 +1834,8 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
18091834
18101835void coro::AnyRetconABI::splitCoroutine (Function &F, coro::Shape &Shape,
18111836 SmallVectorImpl<Function *> &Clones,
1812- TargetTransformInfo &TTI) {
1837+ TargetTransformInfo &TTI,
1838+ const DebugInfoCache *DICache) {
18131839 assert (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce);
18141840 assert (Clones.empty ());
18151841
@@ -1930,7 +1956,7 @@ void coro::AnyRetconABI::splitCoroutine(Function &F, coro::Shape &Shape,
19301956
19311957 assert (Clones.size () == Shape.CoroSuspends .size ());
19321958
1933- MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F)};
1959+ MetadataSetTy CommonDebugInfo{collectCommonDebugInfo (F, DICache )};
19341960
19351961 for (auto [Idx, CS] : llvm::enumerate (Shape.CoroSuspends )) {
19361962 auto Suspend = CS;
@@ -1984,13 +2010,15 @@ static bool hasSafeElideCaller(Function &F) {
19842010
19852011void coro::SwitchABI::splitCoroutine (Function &F, coro::Shape &Shape,
19862012 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);
19892016}
19902017
19912018static void doSplitCoroutine (Function &F, SmallVectorImpl<Function *> &Clones,
19922019 coro::BaseABI &ABI, TargetTransformInfo &TTI,
1993- bool OptimizeFrame) {
2020+ bool OptimizeFrame,
2021+ const DebugInfoCache *DICache) {
19942022 PrettyStackTraceFunction prettyStackTrace (F);
19952023
19962024 auto &Shape = ABI.Shape ;
@@ -2015,7 +2043,7 @@ static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
20152043 if (isNoSuspendCoroutine) {
20162044 handleNoSuspendCoroutine (Shape);
20172045 } else {
2018- ABI.splitCoroutine (F, Shape, Clones, TTI);
2046+ ABI.splitCoroutine (F, Shape, Clones, TTI, DICache );
20192047 }
20202048
20212049 // Replace all the swifterror operations in the original function.
@@ -2212,6 +2240,9 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
22122240 auto &FAM =
22132241 AM.getResult <FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager ();
22142242
2243+ const auto &MAMProxy = AM.getResult <ModuleAnalysisManagerCGSCCProxy>(C, CG);
2244+ const auto *DICache = MAMProxy.getCachedResult <DebugInfoCacheAnalysis>(M);
2245+
22152246 // Check for uses of llvm.coro.prepare.retcon/async.
22162247 SmallVector<Function *, 2 > PrepareFns;
22172248 addPrepareFunction (M, PrepareFns, " llvm.coro.prepare.retcon" );
@@ -2248,7 +2279,7 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
22482279
22492280 SmallVector<Function *, 4 > Clones;
22502281 auto &TTI = FAM.getResult <TargetIRAnalysis>(F);
2251- doSplitCoroutine (F, Clones, *ABI, TTI, OptimizeFrame);
2282+ doSplitCoroutine (F, Clones, *ABI, TTI, OptimizeFrame, DICache );
22522283 CurrentSCC = &updateCallGraphAfterCoroutineSplit (
22532284 *N, Shape, Clones, *CurrentSCC, CG, AM, UR, FAM);
22542285
0 commit comments