LLVM 18.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, bool IsEntryPoint);
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 PointerType::getUnqual(FrameTy->getContext()),
189 /*IsVarArg=*/false);
190 case coro::ABI::Retcon:
191 case coro::ABI::RetconOnce:
192 return RetconLowering.ResumePrototype->getFunctionType();
193 case coro::ABI::Async:
194 // Not used. The function type depends on the active suspend.
195 return nullptr;
196 }
197
198 llvm_unreachable("Unknown coro::ABI enum");
199 }
200
202 assert(ABI == coro::ABI::Retcon ||
203 ABI == coro::ABI::RetconOnce);
204 auto FTy = CoroBegin->getFunction()->getFunctionType();
205
206 // The safety of all this is checked by checkWFRetconPrototype.
207 if (auto STy = dyn_cast<StructType>(FTy->getReturnType())) {
208 return STy->elements().slice(1);
209 } else {
210 return ArrayRef<Type*>();
211 }
212 }
213
215 assert(ABI == coro::ABI::Retcon ||
216 ABI == coro::ABI::RetconOnce);
217
218 // The safety of all this is checked by checkWFRetconPrototype.
219 auto FTy = RetconLowering.ResumePrototype->getFunctionType();
220 return FTy->params().slice(1);
221 }
222
224 switch (ABI) {
225 case coro::ABI::Switch:
226 return CallingConv::Fast;
227
228 case coro::ABI::Retcon:
229 case coro::ABI::RetconOnce:
230 return RetconLowering.ResumePrototype->getCallingConv();
231 case coro::ABI::Async:
232 return AsyncLowering.AsyncCC;
233 }
234 llvm_unreachable("Unknown coro::ABI enum");
235 }
236
238 if (ABI == coro::ABI::Switch)
239 return SwitchLowering.PromiseAlloca;
240 return nullptr;
241 }
242
244 if (auto *I = dyn_cast<Instruction>(FramePtr))
245 return I->getNextNode();
246 return &cast<Argument>(FramePtr)->getParent()->getEntryBlock().front();
247 }
248
249 /// Allocate memory according to the rules of the active lowering.
250 ///
251 /// \param CG - if non-null, will be updated for the new call
252 Value *emitAlloc(IRBuilder<> &Builder, Value *Size, CallGraph *CG) const;
253
254 /// Deallocate memory according to the rules of the active lowering.
255 ///
256 /// \param CG - if non-null, will be updated for the new call
257 void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const;
258
259 Shape() = default;
260 explicit Shape(Function &F, bool OptimizeFrame = false)
261 : OptimizeFrame(OptimizeFrame) {
262 buildFrom(F);
263 }
264 void buildFrom(Function &F);
265};
266
269 Function &F, Shape &Shape,
270 const std::function<bool(Instruction &)> &MaterializableCallback);
271CallInst *createMustTailCall(DebugLoc Loc, Function *MustTailCallFn,
273} // End namespace coro.
274} // End namespace llvm
275
276#endif
AMDGPU Lower Kernel Arguments
#define LLVM_LIBRARY_VISIBILITY
Definition: Compiler.h:131
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:202
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:60
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:418
AnyCoroIdInst * getId() const
Definition: CoroInstr.h:422
This represents the llvm.coro.id.async instruction.
Definition: CoroInstr.h:275
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:200
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2639
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:75
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:646
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:216
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:342
Multiway switch.
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 buildCoroutineFrame(Function &F, Shape &Shape, const std::function< bool(Instruction &)> &MaterializableCallback)
Definition: CoroFrame.cpp:2961
@ 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:2225
bool declaresAnyIntrinsic(const Module &M)
Definition: Coroutines.cpp:100
bool declaresIntrinsics(const Module &M, const std::initializer_list< StringRef >)
Definition: Coroutines.cpp:112
void salvageDebugInfo(SmallDenseMap< Argument *, AllocaInst *, 4 > &ArgToAllocaMap, DbgVariableIntrinsic *DVI, bool OptimizeFrame, bool IsEntryPoint)
Attempts to rewrite the location operand of debug intrinsics in terms of the coroutine frame pointer,...
Definition: CoroFrame.cpp:2801
void replaceCoroFree(CoroIdInst *CoroId, bool Elide)
Definition: Coroutines.cpp:125
CallInst * createMustTailCall(DebugLoc Loc, Function *MustTailCallFn, ArrayRef< Value * > Arguments, IRBuilder<> &)
Definition: CoroSplit.cpp:1742
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:50
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:243
SmallVector< CoroSizeInst *, 2 > CoroSizes
Definition: CoroInternal.h:79
CallingConv::ID getResumeFunctionCC() const
Definition: CoroInternal.h:223
ArrayRef< Type * > getRetconResumeTypes() const
Definition: CoroInternal.h:214
SmallVector< AnyCoroSuspendInst *, 4 > CoroSuspends
Definition: CoroInternal.h:81
Shape(Function &F, bool OptimizeFrame=false)
Definition: CoroInternal.h:260
SmallVector< CallInst *, 2 > SwiftErrorOps
Definition: CoroInternal.h:82
ConstantInt * getIndex(uint64_t Value) const
Definition: CoroInternal.h:174
AllocaInst * getPromiseAlloca() const
Definition: CoroInternal.h:237
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:201
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