Skip to content

Commit

Permalink
[NFC][Coro] Add helpers for coro cloning with a TimeTraceScope (#112948)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
artempyanykh authored Oct 30, 2024
1 parent 0167a92 commit 1c2824e
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions llvm/lib/Transforms/Coroutines/CoroSplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Coroutines/ABI.h"
#include "llvm/Transforms/Coroutines/CoroInstr.h"
Expand Down Expand Up @@ -118,7 +119,6 @@ class CoroCloner {

TargetTransformInfo &TTI;

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

public:
/// Create a clone for a switch lowering.
static Function *createClone(Function &OrigF, const Twine &Suffix,
coro::Shape &Shape, Kind FKind,
TargetTransformInfo &TTI) {
TimeTraceScope FunctionScope("CoroCloner");

CoroCloner Cloner(OrigF, Suffix, Shape, FKind, TTI);
Cloner.create();
return Cloner.getFunction();
}

/// Create a clone for a continuation lowering.
static Function *createClone(Function &OrigF, const Twine &Suffix,
coro::Shape &Shape, Function *NewF,
AnyCoroSuspendInst *ActiveSuspend,
TargetTransformInfo &TTI) {
TimeTraceScope FunctionScope("CoroCloner");

CoroCloner Cloner(OrigF, Suffix, Shape, NewF, ActiveSuspend, TTI);
Cloner.create();
return Cloner.getFunction();
}

Function *getFunction() const {
assert(NewF != nullptr && "declaration not yet set");
return NewF;
Expand Down Expand Up @@ -1466,13 +1490,16 @@ struct SwitchCoroutineSplitter {
TargetTransformInfo &TTI) {
assert(Shape.ABI == coro::ABI::Switch);

// Create a resume clone by cloning the body of the original function,
// setting new entry block and replacing coro.suspend an appropriate value
// to force resume or cleanup pass for every suspend point.
createResumeEntryBlock(F, Shape);
auto *ResumeClone =
createClone(F, ".resume", Shape, CoroCloner::Kind::SwitchResume, TTI);
auto *DestroyClone =
createClone(F, ".destroy", Shape, CoroCloner::Kind::SwitchUnwind, TTI);
auto *CleanupClone =
createClone(F, ".cleanup", Shape, CoroCloner::Kind::SwitchCleanup, TTI);
auto *ResumeClone = CoroCloner::createClone(
F, ".resume", Shape, CoroCloner::Kind::SwitchResume, TTI);
auto *DestroyClone = CoroCloner::createClone(
F, ".destroy", Shape, CoroCloner::Kind::SwitchUnwind, TTI);
auto *CleanupClone = CoroCloner::createClone(
F, ".cleanup", Shape, CoroCloner::Kind::SwitchCleanup, TTI);

postSplitCleanup(*ResumeClone);
postSplitCleanup(*DestroyClone);
Expand Down Expand Up @@ -1562,17 +1589,6 @@ struct SwitchCoroutineSplitter {
}

private:
// Create a resume clone by cloning the body of the original function, setting
// new entry block and replacing coro.suspend an appropriate value to force
// resume or cleanup pass for every suspend point.
static Function *createClone(Function &F, const Twine &Suffix,
coro::Shape &Shape, CoroCloner::Kind FKind,
TargetTransformInfo &TTI) {
CoroCloner Cloner(F, Suffix, Shape, FKind, TTI);
Cloner.create();
return Cloner.getFunction();
}

// Create an entry block for a resume function with a switch that will jump to
// suspend points.
static void createResumeEntryBlock(Function &F, coro::Shape &Shape) {
Expand Down Expand Up @@ -1872,7 +1888,8 @@ void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape,
auto *Suspend = Shape.CoroSuspends[Idx];
auto *Clone = Clones[Idx];

CoroCloner(F, "resume." + Twine(Idx), Shape, Clone, Suspend, TTI).create();
CoroCloner::createClone(F, "resume." + Twine(Idx), Shape, Clone, Suspend,
TTI);
}
}

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

CoroCloner(F, "resume." + Twine(i), Shape, Clone, Suspend, TTI).create();
CoroCloner::createClone(F, "resume." + Twine(i), Shape, Clone, Suspend,
TTI);
}
}

Expand Down

0 comments on commit 1c2824e

Please sign in to comment.