LLVM 22.0.0git
CoroCloner.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8// Helper class for splitting a coroutine into separate functions. For example
9// the returned-continuation coroutine is split into separate continuation
10// functions.
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TRANSFORMS_COROUTINES_COROCLONER_H
14#define LLVM_LIB_TRANSFORMS_COROUTINES_COROCLONER_H
15
16#include "llvm/IR/Function.h"
17#include "llvm/IR/IRBuilder.h"
22
23namespace llvm::coro {
24
25enum class CloneKind {
26 /// The shared resume function for a switch lowering.
28
29 /// The shared unwind function for a switch lowering.
31
32 /// The shared cleanup function for a switch lowering.
34
35 /// An individual continuation function.
37
38 /// An async resume function.
40};
41
43protected:
45 const Twine &Suffix;
50
52 Function *NewF = nullptr;
53 Value *NewFramePtr = nullptr;
54
55 /// The active suspend instruction; meaningful only for continuation and async
56 /// ABIs.
58
59 /// Create a cloner for a continuation lowering.
66 Builder(OrigF.getContext()), TTI(TTI), NewF(NewF),
68 assert(Shape.ABI == ABI::Retcon || Shape.ABI == ABI::RetconOnce ||
69 Shape.ABI == ABI::Async);
70 assert(NewF && "need existing function for continuation");
71 assert(ActiveSuspend && "need active suspend point for continuation");
72 }
73
74public:
79
80 virtual ~BaseCloner() {}
81
82 /// Create a clone for a continuation lowering.
87 assert(Shape.ABI == ABI::Retcon || Shape.ABI == ABI::RetconOnce ||
88 Shape.ABI == ABI::Async);
89 TimeTraceScope FunctionScope("BaseCloner");
90
92 Cloner.create();
93 return Cloner.getFunction();
94 }
95
97 assert(NewF != nullptr && "declaration not yet set");
98 return NewF;
99 }
100
101 virtual void create();
102
103protected:
105 switch (FKind) {
106 case CloneKind::Async:
109 return false;
112 return true;
113 }
114 llvm_unreachable("Unknown ClonerKind enum");
115 }
116
117 void replaceEntryBlock();
120 void replaceCoroSuspends();
121 void replaceCoroEnds();
122 void replaceCoroIsInRamp();
124 void salvageDebugInfo();
125 void handleFinalSuspend();
126};
127
128class SwitchCloner : public BaseCloner {
129protected:
130 /// Create a cloner for a switch lowering.
134
135 void create() override;
136
137public:
138 /// Create a clone for a switch lowering.
142 assert(Shape.ABI == ABI::Switch);
143 TimeTraceScope FunctionScope("SwitchCloner");
144
146 Cloner.create();
147 return Cloner.getFunction();
148 }
149};
150
151} // end namespace llvm::coro
152
153#endif // LLVM_LIB_TRANSFORMS_COROUTINES_COROCLONER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
AnyCoroSuspendInst * ActiveSuspend
The active suspend instruction; meaningful only for continuation and async ABIs.
Definition CoroCloner.h:57
Value * deriveNewFramePointer()
Derive the value of the new frame pointer.
TargetTransformInfo & TTI
Definition CoroCloner.h:49
BaseCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, Function *NewF, AnyCoroSuspendInst *ActiveSuspend, TargetTransformInfo &TTI)
Create a cloner for a continuation lowering.
Definition CoroCloner.h:60
coro::Shape & Shape
Definition CoroCloner.h:46
static Function * createClone(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, Function *NewF, AnyCoroSuspendInst *ActiveSuspend, TargetTransformInfo &TTI)
Create a clone for a continuation lowering.
Definition CoroCloner.h:83
BaseCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, CloneKind FKind, TargetTransformInfo &TTI)
Definition CoroCloner.h:75
ValueToValueMapTy VMap
Definition CoroCloner.h:51
const Twine & Suffix
Definition CoroCloner.h:45
Function * getFunction() const
Definition CoroCloner.h:96
void replaceRetconOrAsyncSuspendUses()
Replace uses of the active llvm.coro.suspend.retcon/async call with the arguments to the continuation...
virtual void create()
Clone the body of the original function into a resume function of some sort.
static Function * createClone(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, CloneKind FKind, TargetTransformInfo &TTI)
Create a clone for a switch lowering.
Definition CoroCloner.h:139
void create() override
Clone the body of the original function into a resume function of some sort.
SwitchCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape, CloneKind FKind, TargetTransformInfo &TTI)
Create a cloner for a switch lowering.
Definition CoroCloner.h:131
#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.
Definition CoroShape.h:48
@ RetconOnce
The "unique returned-continuation" lowering, where each suspend point creates a single continuation f...
Definition CoroShape.h:43
@ Retcon
The "returned-continuation" lowering, where each suspend point creates a single continuation function...
Definition CoroShape.h:36
@ Switch
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
Definition CoroShape.h:31
@ Async
An async resume function.
Definition CoroCloner.h:39
@ SwitchCleanup
The shared cleanup function for a switch lowering.
Definition CoroCloner.h:33
@ SwitchResume
The shared resume function for a switch lowering.
Definition CoroCloner.h:27
@ SwitchUnwind
The shared unwind function for a switch lowering.
Definition CoroCloner.h:30
@ Continuation
An individual continuation function.
Definition CoroCloner.h:36
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy