LLVM 17.0.0git
CoroInternal.h
Go to the documentation of this file.
1//===- CoroInternal.h - Internal Coroutine interfaces ---------*- 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// Common definitions/declarations used internally by coroutine lowering passes.
9//===----------------------------------------------------------------------===//
10
11#ifndef LLVM_LIB_TRANSFORMS_COROUTINES_COROINTERNAL_H
12#define LLVM_LIB_TRANSFORMS_COROUTINES_COROINTERNAL_H
13
14#include "CoroInstr.h"
15#include "llvm/IR/IRBuilder.h"
16
17namespace llvm {
18
19class CallGraph;
20
21namespace coro {
22
23bool declaresAnyIntrinsic(const Module &M);
24bool declaresIntrinsics(const Module &M,
25 const std::initializer_list<StringRef>);
26void replaceCoroFree(CoroIdInst *CoroId, bool Elide);
27
28/// Attempts to rewrite the location operand of debug intrinsics in terms of
29/// the coroutine frame pointer, folding pointer offsets into the DIExpression
30/// of the intrinsic.
31/// If the frame pointer is an Argument, store it into an alloca if
32/// OptimizeFrame is false.
35 DbgVariableIntrinsic *DVI, bool OptimizeFrame);
36
37// Keeps data and helper functions for lowering coroutine intrinsics.
44
46 Value *makeSubFnCall(Value *Arg, int Index, Instruction *InsertPt);
47};
48
49enum class ABI {
50 /// The "resume-switch" lowering, where there are separate resume and
51 /// destroy functions that are shared between all suspend points. The
52 /// coroutine frame implicitly stores the resume and destroy functions,
53 /// the current index, and any promise value.
54 Switch,
55
56 /// The "returned-continuation" lowering, where each suspend point creates a
57 /// single continuation function that is used for both resuming and
58 /// destroying. Does not support promises.
59 Retcon,
60
61 /// The "unique returned-continuation" lowering, where each suspend point
62 /// creates a single continuation function that is used for both resuming
63 /// and destroying. Does not support promises. The function is known to
64 /// suspend at most once during its execution, and the return value of
65 /// the continuation is void.
67
68 /// The "async continuation" lowering, where each suspend point creates a
69 /// single continuation function. The continuation function is available as an
70 /// intrinsic.
71 Async,
72};
73
74// Holds structural Coroutine Intrinsics for a particular function and other
75// values used during CoroSplit pass.
83
84 // Field indexes for special fields in the switch lowering.
86 enum {
88 Destroy
89
90 // The promise field is always at a fixed offset from the start of
91 // frame given its type, but the index isn't a constant for all
92 // possible frames.
93
94 // The switch-index field isn't at a fixed offset or index, either;
95 // we just work it in where it fits best.
96 };
97 };
98
100
106
107 /// This would only be true if optimization are enabled.
109
114 unsigned IndexField;
115 unsigned IndexAlign;
116 unsigned IndexOffset;
119 };
120
127 };
128
132 unsigned ContextArgNo;
135 uint64_t FrameOffset; // Start of the frame.
136 uint64_t ContextSize; // Includes frame size.
138
139 Align getContextAlignment() const { return Align(ContextAlignment); }
140 };
141
142 union {
146 };
147
149 assert(ABI == coro::ABI::Switch);
150 return cast<CoroIdInst>(CoroBegin->getId());
151 }
152
154 assert(ABI == coro::ABI::Retcon ||
155 ABI == coro::ABI::RetconOnce);
156 return cast<AnyCoroIdRetconInst>(CoroBegin->getId());
157 }
158
160 assert(ABI == coro::ABI::Async);
161 return cast<CoroIdAsyncInst>(CoroBegin->getId());
162 }
163
164 unsigned getSwitchIndexField() const {
165 assert(ABI == coro::ABI::Switch);
166 assert(FrameTy && "frame type not assigned");
167 return SwitchLowering.IndexField;
168 }
170 assert(ABI == coro::ABI::Switch);
171 assert(FrameTy && "frame type not assigned");
172 return cast<IntegerType>(FrameTy->getElementType(getSwitchIndexField()));
173 }
175 return ConstantInt::get(getIndexType(), Value);
176 }
177
179 assert(ABI == coro::ABI::Switch);
180 assert(FrameTy && "frame type not assigned");
181 return cast<PointerType>(FrameTy->getElementType(SwitchFieldIndex::Resume));
182 }
183
185 switch (ABI) {
186 case coro::ABI::Switch:
187 return FunctionType::get(Type::getVoidTy(FrameTy->getContext()),
188 FrameTy->getPointerTo(), /*IsVarArg*/false);
189 case coro::ABI::Retcon:
190 case coro::ABI::RetconOnce:
191 return RetconLowering.ResumePrototype->getFunctionType();
192 case coro::ABI::Async:
193 // Not used. The function type depends on the active suspend.
194 return nullptr;
195 }
196
197 llvm_unreachable("Unknown coro::ABI enum");
198 }
199
201 assert(ABI == coro::ABI::Retcon ||
202 ABI == coro::ABI::RetconOnce);
203 auto FTy = CoroBegin->getFunction()->getFunctionType();
204
205 // The safety of all this is checked by checkWFRetconPrototype.
206 if (auto STy = dyn_cast<StructType>(FTy->getReturnType())) {
207 return STy->elements().slice(1);
208 } else {
209 return ArrayRef<Type*>();
210 }
211 }
212
214 assert(ABI == coro::ABI::Retcon ||
215 ABI == coro::ABI::RetconOnce);
216
217 // The safety of all this is checked by checkWFRetconPrototype.
218 auto FTy = RetconLowering.ResumePrototype->getFunctionType();
219 return FTy->params().slice(1);
220 }
221
223 switch (ABI) {
224 case coro::ABI::Switch:
225 return CallingConv::Fast;
226
227 case coro::ABI::Retcon:
228 case coro::ABI::RetconOnce:
229 return RetconLowering.ResumePrototype->getCallingConv();
230 case coro::ABI::Async:
231 return AsyncLowering.AsyncCC;
232 }
233 llvm_unreachable("Unknown coro::ABI enum");
234 }
235
237 if (ABI == coro::ABI::Switch)
238 return SwitchLowering.PromiseAlloca;
239 return nullptr;
240 }
241
243 if (auto *I = dyn_cast<Instruction>(FramePtr))
244 return I->getNextNode();
245 return &cast<Argument>(FramePtr)->getParent()->getEntryBlock().front();
246 }
247
248 /// Allocate memory according to the rules of the active lowering.
249 ///
250 /// \param CG - if non-null, will be updated for the new call
251 Value *emitAlloc(IRBuilder<> &Builder, Value *Size, CallGraph *CG) const;
252
253 /// Deallocate memory according to the rules of the active lowering.
254 ///
255 /// \param CG - if non-null, will be updated for the new call
256 void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const;
257
258 Shape() = default;
259 explicit Shape(Function &F, bool OptimizeFrame = false)
260 : OptimizeFrame(OptimizeFrame) {
261 buildFrom(F);
262 }
263 void buildFrom(Function &F);
264};
265
268 Function &F, Shape &Shape,
269 const std::function<bool(Instruction &)> &MaterializableCallback);
270CallInst *createMustTailCall(DebugLoc Loc, Function *MustTailCallFn,
272} // End namespace coro.
273} // End namespace llvm
274
275#endif
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
AMDGPU Lower Kernel Arguments
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library,...
Definition: Compiler.h:126
uint64_t Align
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const unsigned FramePtr
an instruction to allocate memory on the stack
Definition: Instructions.h:58
This represents either the llvm.coro.id.retcon or llvm.coro.id.retcon.once instruction.
Definition: CoroInstr.h:204
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:72
This class represents a function call, abstracting a target machine's calling convention.
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
A constant pointer value that points to null.
Definition: Constants.h:533
This class represents the llvm.coro.begin instruction.
Definition: CoroInstr.h:420
AnyCoroIdInst * getId() const
Definition: CoroInstr.h:424
This represents the llvm.coro.id.async instruction.
Definition: CoroInstr.h:277
This represents the llvm.coro.id instruction.
Definition: CoroInstr.h:113
This is the common base class for debug info intrinsics for variables.
A debug info location.
Definition: DebugLoc.h:33
Class to represent function types.
Definition: DerivedTypes.h:103
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:174
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2570
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:74
Class to represent integer types.
Definition: DerivedTypes.h:40
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Class to represent pointers.
Definition: DerivedTypes.h:643
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
Class to represent struct types.
Definition: DerivedTypes.h:213
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:339
Multiway switch.
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
LLVM Value Representation.
Definition: Value.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void salvageDebugInfo(SmallDenseMap< Argument *, AllocaInst *, 4 > &ArgToAllocaMap, DbgVariableIntrinsic *DVI, bool OptimizeFrame)
Attempts to rewrite the location operand of debug intrinsics in terms of the coroutine frame pointer,...
Definition: CoroFrame.cpp:2832
void buildCoroutineFrame(Function &F, Shape &Shape, const std::function< bool(Instruction &)> &MaterializableCallback)
Definition: CoroFrame.cpp:2990
@ 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...
bool defaultMaterializable(Instruction &V)
Default materializable callback.
Definition: CoroFrame.cpp:2245
bool declaresAnyIntrinsic(const Module &M)
Definition: Coroutines.cpp:105
bool declaresIntrinsics(const Module &M, const std::initializer_list< StringRef >)
Definition: Coroutines.cpp:117
void replaceCoroFree(CoroIdInst *CoroId, bool Elide)
Definition: Coroutines.cpp:130
CallInst * createMustTailCall(DebugLoc Loc, Function *MustTailCallFn, ArrayRef< Value * > Arguments, IRBuilder<> &)
Definition: CoroSplit.cpp:1683
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
PointerType *const Int8Ptr
Definition: CoroInternal.h:41
ConstantPointerNull *const NullPtr
Definition: CoroInternal.h:43
Value * makeSubFnCall(Value *Arg, int Index, Instruction *InsertPt)
Definition: Coroutines.cpp:51
LLVMContext & Context
Definition: CoroInternal.h:40
FunctionType *const ResumeFnType
Definition: CoroInternal.h:42
AsyncLoweringStorage AsyncLowering
Definition: CoroInternal.h:145
FunctionType * getResumeFunctionType() const
Definition: CoroInternal.h:184
IntegerType * getIndexType() const
Definition: CoroInternal.h:169
StructType * FrameTy
Definition: CoroInternal.h:101
AnyCoroIdRetconInst * getRetconCoroId() const
Definition: CoroInternal.h:153
PointerType * getSwitchResumePointerType() const
Definition: CoroInternal.h:178
CoroIdInst * getSwitchCoroId() const
Definition: CoroInternal.h:148
Instruction * getInsertPtAfterFramePtr() const
Definition: CoroInternal.h:242
SmallVector< CoroSizeInst *, 2 > CoroSizes
Definition: CoroInternal.h:79
CallingConv::ID getResumeFunctionCC() const
Definition: CoroInternal.h:222
ArrayRef< Type * > getRetconResumeTypes() const
Definition: CoroInternal.h:213
SmallVector< AnyCoroSuspendInst *, 4 > CoroSuspends
Definition: CoroInternal.h:81
Shape(Function &F, bool OptimizeFrame=false)
Definition: CoroInternal.h:259
SmallVector< CallInst *, 2 > SwiftErrorOps
Definition: CoroInternal.h:82
ConstantInt * getIndex(uint64_t Value) const
Definition: CoroInternal.h:174
AllocaInst * getPromiseAlloca() const
Definition: CoroInternal.h:236
bool OptimizeFrame
This would only be true if optimization are enabled.
Definition: CoroInternal.h:108
SwitchLoweringStorage SwitchLowering
Definition: CoroInternal.h:143
CoroBeginInst * CoroBegin
Definition: CoroInternal.h:77
ArrayRef< Type * > getRetconResultTypes() const
Definition: CoroInternal.h:200
RetconLoweringStorage RetconLowering
Definition: CoroInternal.h:144
SmallVector< CoroAlignInst *, 2 > CoroAligns
Definition: CoroInternal.h:80
CoroIdAsyncInst * getAsyncCoroId() const
Definition: CoroInternal.h:159
SmallVector< AnyCoroEndInst *, 4 > CoroEnds
Definition: CoroInternal.h:78
BasicBlock * AllocaSpillBlock
Definition: CoroInternal.h:105
unsigned getSwitchIndexField() const
Definition: CoroInternal.h:164