LLVM 20.0.0git
CoroCloner.h
Go to the documentation of this file.
1//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7// Helper class for splitting a coroutine into separate functions. For example
8// the returned-continuation coroutine is split into separate continuation
9// functions.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_LIB_TRANSFORMS_COROUTINES_COROCLONER_H
13#define LLVM_LIB_TRANSFORMS_COROUTINES_COROCLONER_H
14
15#include "llvm/IR/Function.h"
16#include "llvm/IR/IRBuilder.h"
21
22namespace llvm {
23
24namespace coro {
25
26enum class CloneKind {
27 /// The shared resume function for a switch lowering.
29
30 /// The shared unwind function for a switch lowering.
32
33 /// The shared cleanup function for a switch lowering.
35
36 /// An individual continuation function.
38
39 /// An async resume function.
40 Async,
41};
42
44protected:
46 const Twine &Suffix;
51 // Common module-level metadata that's shared between all coroutine clones and
52 // doesn't need to be cloned itself.
54
56 Function *NewF = nullptr;
57 Value *NewFramePtr = nullptr;
58
59 /// The active suspend instruction; meaningful only for continuation and async
60 /// ABIs.
62
63 /// Create a cloner for a continuation lowering.
74 assert(NewF && "need existing function for continuation");
75 assert(ActiveSuspend && "need active suspend point for continuation");
76 }
77
78public:
83 Builder(OrigF.getContext()), TTI(TTI),
85
86 virtual ~BaseCloner() {}
87
88 /// Create a clone for a continuation lowering.
96 TimeTraceScope FunctionScope("BaseCloner");
97
100 Cloner.create();
101 return Cloner.getFunction();
102 }
103
105 assert(NewF != nullptr && "declaration not yet set");
106 return NewF;
107 }
108
109 virtual void create();
110
111protected:
113 switch (FKind) {
114 case CloneKind::Async:
117 return false;
120 return true;
121 }
122 llvm_unreachable("Unknown ClonerKind enum");
123 }
124
125 void replaceEntryBlock();
128 void replaceCoroSuspends();
129 void replaceCoroEnds();
131 void salvageDebugInfo();
132 void handleFinalSuspend();
133};
134
135class SwitchCloner : public BaseCloner {
136protected:
137 /// Create a cloner for a switch lowering.
142
143 void create() override;
144
145public:
146 /// Create a clone for a switch lowering.
152 TimeTraceScope FunctionScope("SwitchCloner");
153
155 Cloner.create();
156 return Cloner.getFunction();
157 }
158};
159
160} // end namespace coro
161
162} // end namespace llvm
163
164#endif // LLVM_LIB_TRANSFORMS_COROUTINES_COROCLONER_H
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2705
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Definition: TimeProfiler.h:180
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
static Function * createClone(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, Function *NewF, AnyCoroSuspendInst *ActiveSuspend, TargetTransformInfo &TTI, const MetadataSetTy &CommonDebugInfo)
Create a clone for a continuation lowering.
Definition: CoroCloner.h:89
AnyCoroSuspendInst * ActiveSuspend
The active suspend instruction; meaningful only for continuation and async ABIs.
Definition: CoroCloner.h:61
Value * deriveNewFramePointer()
Derive the value of the new frame pointer.
Definition: CoroSplit.cpp:762
const MetadataSetTy & CommonDebugInfo
Definition: CoroCloner.h:53
BaseCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, CloneKind FKind, TargetTransformInfo &TTI, const MetadataSetTy &CommonDebugInfo)
Definition: CoroCloner.h:79
TargetTransformInfo & TTI
Definition: CoroCloner.h:50
coro::Shape & Shape
Definition: CoroCloner.h:47
ValueToValueMapTy VMap
Definition: CoroCloner.h:55
bool isSwitchDestroyFunction()
Definition: CoroCloner.h:112
const Twine & Suffix
Definition: CoroCloner.h:46
Function * getFunction() const
Definition: CoroCloner.h:104
void replaceRetconOrAsyncSuspendUses()
Replace uses of the active llvm.coro.suspend.retcon/async call with the arguments to the continuation...
Definition: CoroSplit.cpp:488
virtual void create()
Clone the body of the original function into a resume function of some sort.
Definition: CoroSplit.cpp:897
BaseCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, Function *NewF, AnyCoroSuspendInst *ActiveSuspend, TargetTransformInfo &TTI, const MetadataSetTy &CommonDebugInfo)
Create a cloner for a continuation lowering.
Definition: CoroCloner.h:64
void create() override
Clone the body of the original function into a resume function of some sort.
Definition: CoroSplit.cpp:1127
static Function * createClone(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, CloneKind FKind, TargetTransformInfo &TTI, const MetadataSetTy &CommonDebugInfo)
Create a clone for a switch lowering.
Definition: CoroCloner.h:147
SwitchCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, CloneKind FKind, TargetTransformInfo &TTI, const MetadataSetTy &CommonDebugInfo)
Create a cloner for a switch lowering.
Definition: CoroCloner.h:138
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Async
The "async continuation" lowering, where each suspend point creates a single continuation function.
@ RetconOnce
The "unique returned-continuation" lowering, where each suspend point creates a single continuation f...
@ Retcon
The "returned-continuation" lowering, where each suspend point creates a single continuation function...
@ Switch
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
@ Async
An async resume function.
@ SwitchCleanup
The shared cleanup function for a switch lowering.
@ SwitchResume
The shared resume function for a switch lowering.
@ SwitchUnwind
The shared unwind function for a switch lowering.
@ Continuation
An individual continuation function.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
coro::ABI ABI
Definition: CoroShape.h:107