LLVM 24.0.0git
CoroShape.h
Go to the documentation of this file.
1//===- CoroShape.h - Coroutine info for lowering --------------*- C++ -*---===//
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// This file declares the shape info struct that is required by many coroutine
9// utility methods.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_TRANSFORMS_COROUTINES_COROSHAPE_H
13#define LLVM_TRANSFORMS_COROUTINES_COROSHAPE_H
14
15#include "llvm/IR/IRBuilder.h"
16#include "llvm/IR/PassManager.h"
19
20namespace llvm {
21
22class CallGraph;
23
24namespace coro {
25
26enum class ABI {
27 /// The "resume-switch" lowering, where there are separate resume and
28 /// destroy functions that are shared between all suspend points. The
29 /// coroutine frame implicitly stores the resume and destroy functions,
30 /// the current index, and any promise value.
32
33 /// The "returned-continuation" lowering, where each suspend point creates a
34 /// single continuation function that is used for both resuming and
35 /// destroying. Does not support promises.
37
38 /// The "unique returned-continuation" lowering, where each suspend point
39 /// creates a single continuation function that is used for both resuming
40 /// and destroying. Does not support promises. The function is known to
41 /// suspend at most once during its execution, and the return value of
42 /// the continuation is void.
44
45 /// The "async continuation" lowering, where each suspend point creates a
46 /// single continuation function. The continuation function is available as an
47 /// intrinsic.
49};
50
51// Holds structural Coroutine Intrinsics for a particular function and other
52// values used during CoroSplit pass.
53struct Shape {
62
63 // Values invalidated by replaceSwiftErrorOps()
65
66 void clear() {
67 CoroBegin = nullptr;
68 CoroEnds.clear();
69 CoroIsInRampInsts.clear();
70 CoroSizes.clear();
71 CoroAligns.clear();
72 CoroSuspends.clear();
73 CoroAwaitSuspends.clear();
74 SymmetricTransfers.clear();
75
76 SwiftErrorOps.clear();
77
78 FramePtr = nullptr;
79 AllocaSpillBlock = nullptr;
80 }
81
82 // Scan the function and collect the above intrinsics for later processing
85 SmallVectorImpl<CoroSaveInst *> &UnusedCoroSaves);
86 // If for some reason, we were not able to find coro.begin, bailout.
87 LLVM_ABI void
90 // Perform ABI related initial transformation
92 // Remove orphaned and unnecessary intrinsics
93 LLVM_ABI void
95 SmallVectorImpl<CoroSaveInst *> &UnusedCoroSaves);
96
98
101 Value *FramePtr = nullptr;
103
117
125
138
139 union {
143 };
144
147 return cast<CoroIdInst>(CoroBegin->getId());
148 }
149
154
159
162 assert(SwitchLowering.IndexType && "index type not assigned");
163 return SwitchLowering.IndexType;
164 }
166 return ConstantInt::get(getIndexType(), Value);
167 }
168
171 assert(CoroBegin && "CoroBegin not assigned");
172 return PointerType::getUnqual(CoroBegin->getContext());
173 }
174
176 switch (ABI) {
178 return FunctionType::get(Type::getVoidTy(CoroBegin->getContext()),
179 PointerType::getUnqual(CoroBegin->getContext()),
180 /*IsVarArg=*/false);
183 return RetconLowering.ResumePrototype->getFunctionType();
184 case coro::ABI::Async:
185 // Not used. The function type depends on the active suspend.
186 return nullptr;
187 }
188
189 llvm_unreachable("Unknown coro::ABI enum");
190 }
191
194 auto FTy = CoroBegin->getFunction()->getFunctionType();
195
196 // The safety of all this is checked by checkWFRetconPrototype.
197 if (auto STy = dyn_cast<StructType>(FTy->getReturnType())) {
198 return STy->elements().slice(1);
199 } else {
200 return ArrayRef<Type *>();
201 }
202 }
203
206
207 // The safety of all this is checked by checkWFRetconPrototype.
208 auto FTy = RetconLowering.ResumePrototype->getFunctionType();
209 return FTy->params().slice(1);
210 }
211
213 switch (ABI) {
215 // Use the platform C calling convention so that resume/destroy
216 // function pointers stored in the coroutine frame are
217 // interoperable with other compilers.
218 return CallingConv::C;
219
222 return RetconLowering.ResumePrototype->getCallingConv();
223 case coro::ABI::Async:
224 return AsyncLowering.AsyncCC;
225 }
226 llvm_unreachable("Unknown coro::ABI enum");
227 }
228
230 if (ABI == coro::ABI::Switch)
231 return SwitchLowering.PromiseAlloca;
232 return nullptr;
233 }
234
236 if (auto *I = dyn_cast<Instruction>(FramePtr)) {
237 BasicBlock::iterator It = std::next(I->getIterator());
238 It.setHeadBit(true); // Copy pre-RemoveDIs behaviour.
239 return It;
240 }
241 return cast<Argument>(FramePtr)->getParent()->getEntryBlock().begin();
242 }
243
244 /// Allocate memory according to the rules of the active lowering.
245 ///
246 /// \param CG - if non-null, will be updated for the new call
248 CallGraph *CG) const;
249
250 /// Deallocate memory according to the rules of the active lowering.
251 ///
252 /// \param CG - if non-null, will be updated for the new call
253 LLVM_ABI void emitDealloc(IRBuilder<> &Builder, Value *Ptr,
254 CallGraph *CG) const;
255
256 Shape() = default;
257 explicit Shape(Function &F) {
259 SmallVector<CoroSaveInst *, 2> UnusedCoroSaves;
260
261 analyze(F, CoroFrames, UnusedCoroSaves);
262 if (!CoroBegin) {
263 invalidateCoroutine(F, CoroFrames);
264 return;
265 }
266 cleanCoroutine(CoroFrames, UnusedCoroSaves);
267 }
268};
269
270} // end namespace coro
271
272} // end namespace llvm
273
274#endif // LLVM_TRANSFORMS_COROUTINES_COROSHAPE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
an instruction to allocate memory on the stack
This represents either the llvm.coro.id.retcon or llvm.coro.id.retcon.once instruction.
Definition CoroInstr.h:238
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
The basic data container for the call graph of a Module of IR.
Definition CallGraph.h:72
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This class represents the llvm.coro.begin or llvm.coro.begin.custom.abi instructions.
Definition CoroInstr.h:479
This represents the llvm.coro.id.async instruction.
Definition CoroInstr.h:310
This represents the llvm.coro.id instruction.
Definition CoroInstr.h:148
Class to represent function types.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2893
Class to represent integer types.
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Multiway switch.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:282
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ 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
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallVector< CallInst *, 2 > SymmetricTransfers
Definition CoroShape.h:61
SmallVector< CoroAwaitSuspendInst *, 4 > CoroAwaitSuspends
Definition CoroShape.h:60
AsyncLoweringStorage AsyncLowering
Definition CoroShape.h:142
FunctionType * getResumeFunctionType() const
Definition CoroShape.h:175
IntegerType * getIndexType() const
Definition CoroShape.h:160
AnyCoroIdRetconInst * getRetconCoroId() const
Definition CoroShape.h:150
PointerType * getSwitchResumePointerType() const
Definition CoroShape.h:169
CoroIdInst * getSwitchCoroId() const
Definition CoroShape.h:145
LLVM_ABI void initABI()
SmallVector< CoroSizeInst *, 2 > CoroSizes
Definition CoroShape.h:57
CallingConv::ID getResumeFunctionCC() const
Definition CoroShape.h:212
coro::ABI ABI
Definition CoroShape.h:97
Shape(Function &F)
Definition CoroShape.h:257
ArrayRef< Type * > getRetconResumeTypes() const
Definition CoroShape.h:204
SmallVector< AnyCoroSuspendInst *, 4 > CoroSuspends
Definition CoroShape.h:59
uint64_t FrameSize
Definition CoroShape.h:100
LLVM_ABI Value * emitAlloc(IRBuilder<> &Builder, Value *Size, CallGraph *CG) const
Allocate memory according to the rules of the active lowering.
LLVM_ABI void cleanCoroutine(SmallVectorImpl< CoroFrameInst * > &CoroFrames, SmallVectorImpl< CoroSaveInst * > &UnusedCoroSaves)
ConstantInt * getIndex(uint64_t Value) const
Definition CoroShape.h:165
AllocaInst * getPromiseAlloca() const
Definition CoroShape.h:229
SwitchLoweringStorage SwitchLowering
Definition CoroShape.h:140
CoroBeginInst * CoroBegin
Definition CoroShape.h:54
BasicBlock::iterator getInsertPtAfterFramePtr() const
Definition CoroShape.h:235
ArrayRef< Type * > getRetconResultTypes() const
Definition CoroShape.h:192
SmallVector< CoroIsInRampInst *, 2 > CoroIsInRampInsts
Definition CoroShape.h:56
LLVM_ABI void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const
Deallocate memory according to the rules of the active lowering.
RetconLoweringStorage RetconLowering
Definition CoroShape.h:141
SmallVector< CoroAlignInst *, 2 > CoroAligns
Definition CoroShape.h:58
CoroIdAsyncInst * getAsyncCoroId() const
Definition CoroShape.h:155
SmallVector< AnyCoroEndInst *, 4 > CoroEnds
Definition CoroShape.h:55
SmallVector< CallInst *, 2 > SwiftErrorOps
Definition CoroShape.h:64
LLVM_ABI void invalidateCoroutine(Function &F, SmallVectorImpl< CoroFrameInst * > &CoroFrames)
BasicBlock * AllocaSpillBlock
Definition CoroShape.h:102
LLVM_ABI void analyze(Function &F, SmallVectorImpl< CoroFrameInst * > &CoroFrames, SmallVectorImpl< CoroSaveInst * > &UnusedCoroSaves)