Skip to content

Commit 1c2824e

Browse files
authored
[NFC][Coro] Add helpers for coro cloning with a TimeTraceScope (#112948)
A helper (2 overloads) that consolidates corocloner creation and the actual cloning. The helpers create a TimeTraceScope to make it easier to see how long the cloning takes. Extracted from #109032 (commit 1)
1 parent 0167a92 commit 1c2824e

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

+38-20
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "llvm/Support/Casting.h"
6161
#include "llvm/Support/Debug.h"
6262
#include "llvm/Support/PrettyStackTrace.h"
63+
#include "llvm/Support/TimeProfiler.h"
6364
#include "llvm/Support/raw_ostream.h"
6465
#include "llvm/Transforms/Coroutines/ABI.h"
6566
#include "llvm/Transforms/Coroutines/CoroInstr.h"
@@ -118,7 +119,6 @@ class CoroCloner {
118119

119120
TargetTransformInfo &TTI;
120121

121-
public:
122122
/// Create a cloner for a switch lowering.
123123
CoroCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape,
124124
Kind FKind, TargetTransformInfo &TTI)
@@ -140,6 +140,30 @@ class CoroCloner {
140140
assert(ActiveSuspend && "need active suspend point for continuation");
141141
}
142142

143+
public:
144+
/// Create a clone for a switch lowering.
145+
static Function *createClone(Function &OrigF, const Twine &Suffix,
146+
coro::Shape &Shape, Kind FKind,
147+
TargetTransformInfo &TTI) {
148+
TimeTraceScope FunctionScope("CoroCloner");
149+
150+
CoroCloner Cloner(OrigF, Suffix, Shape, FKind, TTI);
151+
Cloner.create();
152+
return Cloner.getFunction();
153+
}
154+
155+
/// Create a clone for a continuation lowering.
156+
static Function *createClone(Function &OrigF, const Twine &Suffix,
157+
coro::Shape &Shape, Function *NewF,
158+
AnyCoroSuspendInst *ActiveSuspend,
159+
TargetTransformInfo &TTI) {
160+
TimeTraceScope FunctionScope("CoroCloner");
161+
162+
CoroCloner Cloner(OrigF, Suffix, Shape, NewF, ActiveSuspend, TTI);
163+
Cloner.create();
164+
return Cloner.getFunction();
165+
}
166+
143167
Function *getFunction() const {
144168
assert(NewF != nullptr && "declaration not yet set");
145169
return NewF;
@@ -1466,13 +1490,16 @@ struct SwitchCoroutineSplitter {
14661490
TargetTransformInfo &TTI) {
14671491
assert(Shape.ABI == coro::ABI::Switch);
14681492

1493+
// Create a resume clone by cloning the body of the original function,
1494+
// setting new entry block and replacing coro.suspend an appropriate value
1495+
// to force resume or cleanup pass for every suspend point.
14691496
createResumeEntryBlock(F, Shape);
1470-
auto *ResumeClone =
1471-
createClone(F, ".resume", Shape, CoroCloner::Kind::SwitchResume, TTI);
1472-
auto *DestroyClone =
1473-
createClone(F, ".destroy", Shape, CoroCloner::Kind::SwitchUnwind, TTI);
1474-
auto *CleanupClone =
1475-
createClone(F, ".cleanup", Shape, CoroCloner::Kind::SwitchCleanup, TTI);
1497+
auto *ResumeClone = CoroCloner::createClone(
1498+
F, ".resume", Shape, CoroCloner::Kind::SwitchResume, TTI);
1499+
auto *DestroyClone = CoroCloner::createClone(
1500+
F, ".destroy", Shape, CoroCloner::Kind::SwitchUnwind, TTI);
1501+
auto *CleanupClone = CoroCloner::createClone(
1502+
F, ".cleanup", Shape, CoroCloner::Kind::SwitchCleanup, TTI);
14761503

14771504
postSplitCleanup(*ResumeClone);
14781505
postSplitCleanup(*DestroyClone);
@@ -1562,17 +1589,6 @@ struct SwitchCoroutineSplitter {
15621589
}
15631590

15641591
private:
1565-
// Create a resume clone by cloning the body of the original function, setting
1566-
// new entry block and replacing coro.suspend an appropriate value to force
1567-
// resume or cleanup pass for every suspend point.
1568-
static Function *createClone(Function &F, const Twine &Suffix,
1569-
coro::Shape &Shape, CoroCloner::Kind FKind,
1570-
TargetTransformInfo &TTI) {
1571-
CoroCloner Cloner(F, Suffix, Shape, FKind, TTI);
1572-
Cloner.create();
1573-
return Cloner.getFunction();
1574-
}
1575-
15761592
// Create an entry block for a resume function with a switch that will jump to
15771593
// suspend points.
15781594
static void createResumeEntryBlock(Function &F, coro::Shape &Shape) {
@@ -1872,7 +1888,8 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
18721888
auto *Suspend = Shape.CoroSuspends[Idx];
18731889
auto *Clone = Clones[Idx];
18741890

1875-
CoroCloner(F, "resume." + Twine(Idx), Shape, Clone, Suspend, TTI).create();
1891+
CoroCloner::createClone(F, "resume." + Twine(Idx), Shape, Clone, Suspend,
1892+
TTI);
18761893
}
18771894
}
18781895

@@ -2001,7 +2018,8 @@ void coro::AnyRetconABI::splitCoroutine(Function &F, coro::Shape &Shape,
20012018
auto Suspend = Shape.CoroSuspends[i];
20022019
auto Clone = Clones[i];
20032020

2004-
CoroCloner(F, "resume." + Twine(i), Shape, Clone, Suspend, TTI).create();
2021+
CoroCloner::createClone(F, "resume." + Twine(i), Shape, Clone, Suspend,
2022+
TTI);
20052023
}
20062024
}
20072025

0 commit comments

Comments
 (0)