LLVM 20.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"
16#include "llvm/IR/IRBuilder.h"
17
18namespace llvm {
19
20class CallGraph;
21
22namespace coro {
23
24bool declaresAnyIntrinsic(const Module &M);
25bool declaresIntrinsics(const Module &M,
26 const std::initializer_list<StringRef>);
27void replaceCoroFree(CoroIdInst *CoroId, bool Elide);
28
29/// Attempts to rewrite the location operand of debug intrinsics in terms of
30/// the coroutine frame pointer, folding pointer offsets into the DIExpression
31/// of the intrinsic.
32/// If the frame pointer is an Argument, store it into an alloca to enhance the
33/// debugability.
36 DbgVariableIntrinsic &DVI, bool IsEntryPoint);
39 DbgVariableRecord &DVR, bool UseEntryValue);
40
41// Keeps data and helper functions for lowering coroutine intrinsics.
48
50 CallInst *makeSubFnCall(Value *Arg, int Index, Instruction *InsertPt);
51};
52
53enum class ABI {
54 /// The "resume-switch" lowering, where there are separate resume and
55 /// destroy functions that are shared between all suspend points. The
56 /// coroutine frame implicitly stores the resume and destroy functions,
57 /// the current index, and any promise value.
58 Switch,
59
60 /// The "returned-continuation" lowering, where each suspend point creates a
61 /// single continuation function that is used for both resuming and
62 /// destroying. Does not support promises.
63 Retcon,
64
65 /// The "unique returned-continuation" lowering, where each suspend point
66 /// creates a single continuation function that is used for both resuming
67 /// and destroying. Does not support promises. The function is known to
68 /// suspend at most once during its execution, and the return value of
69 /// the continuation is void.
71
72 /// The "async continuation" lowering, where each suspend point creates a
73 /// single continuation function. The continuation function is available as an
74 /// intrinsic.
75 Async,
76};
77
78// Holds structural Coroutine Intrinsics for a particular function and other
79// values used during CoroSplit pass.
89
90 // Field indexes for special fields in the switch lowering.
92 enum {
94 Destroy
95
96 // The promise field is always at a fixed offset from the start of
97 // frame given its type, but the index isn't a constant for all
98 // possible frames.
99
100 // The switch-index field isn't at a fixed offset or index, either;
101 // we just work it in where it fits best.
102 };
103 };
104
106
112
113 /// This would only be true if optimization are enabled.
115
120 unsigned IndexField;
121 unsigned IndexAlign;
122 unsigned IndexOffset;
125 };
126
133 };
134
138 unsigned ContextArgNo;
141 uint64_t FrameOffset; // Start of the frame.
142 uint64_t ContextSize; // Includes frame size.
144
145 Align getContextAlignment() const { return Align(ContextAlignment); }
146 };
147
148 union {
152 };
153
155 assert(ABI == coro::ABI::Switch);
156 return cast<CoroIdInst>(CoroBegin->getId());
157 }
158
160 assert(ABI == coro::ABI::Retcon ||
161 ABI == coro::ABI::RetconOnce);
162 return cast<AnyCoroIdRetconInst>(CoroBegin->getId());
163 }
164
166 assert(ABI == coro::ABI::Async);
167 return cast<CoroIdAsyncInst>(CoroBegin->getId());
168 }
169
170 unsigned getSwitchIndexField() const {
171 assert(ABI == coro::ABI::Switch);
172 assert(FrameTy && "frame type not assigned");
173 return SwitchLowering.IndexField;
174 }
176 assert(ABI == coro::ABI::Switch);
177 assert(FrameTy && "frame type not assigned");
178 return cast<IntegerType>(FrameTy->getElementType(getSwitchIndexField()));
179 }
181 return ConstantInt::get(getIndexType(), Value);
182 }
183
185 assert(ABI == coro::ABI::Switch);
186 assert(FrameTy && "frame type not assigned");
187 return cast<PointerType>(FrameTy->getElementType(SwitchFieldIndex::Resume));
188 }
189
191 switch (ABI) {
192 case coro::ABI::Switch:
193 return FunctionType::get(Type::getVoidTy(FrameTy->getContext()),
194 PointerType::getUnqual(FrameTy->getContext()),
195 /*IsVarArg=*/false);
196 case coro::ABI::Retcon:
197 case coro::ABI::RetconOnce:
198 return RetconLowering.ResumePrototype->getFunctionType();
199 case coro::ABI::Async:
200 // Not used. The function type depends on the active suspend.
201 return nullptr;
202 }
203
204 llvm_unreachable("Unknown coro::ABI enum");
205 }
206
208 assert(ABI == coro::ABI::Retcon ||
209 ABI == coro::ABI::RetconOnce);
210 auto FTy = CoroBegin->getFunction()->getFunctionType();
211
212 // The safety of all this is checked by checkWFRetconPrototype.
213 if (auto STy = dyn_cast<StructType>(FTy->getReturnType())) {
214 return STy->elements().slice(1);
215 } else {
216 return ArrayRef<Type*>();
217 }
218 }
219
221 assert(ABI == coro::ABI::Retcon ||
222 ABI == coro::ABI::RetconOnce);
223
224 // The safety of all this is checked by checkWFRetconPrototype.
225 auto FTy = RetconLowering.ResumePrototype->getFunctionType();
226 return FTy->params().slice(1);
227 }
228
230 switch (ABI) {
231 case coro::ABI::Switch:
232 return CallingConv::Fast;
233
234 case coro::ABI::Retcon:
235 case coro::ABI::RetconOnce:
236 return RetconLowering.ResumePrototype->getCallingConv();
237 case coro::ABI::Async:
238 return AsyncLowering.AsyncCC;
239 }
240 llvm_unreachable("Unknown coro::ABI enum");
241 }
242
244 if (ABI == coro::ABI::Switch)
245 return SwitchLowering.PromiseAlloca;
246 return nullptr;
247 }
248
250 if (auto *I = dyn_cast<Instruction>(FramePtr)) {
251 BasicBlock::iterator It = std::next(I->getIterator());
252 It.setHeadBit(true); // Copy pre-RemoveDIs behaviour.
253 return It;
254 }
255 return cast<Argument>(FramePtr)->getParent()->getEntryBlock().begin();
256 }
257
258 /// Allocate memory according to the rules of the active lowering.
259 ///
260 /// \param CG - if non-null, will be updated for the new call
261 Value *emitAlloc(IRBuilder<> &Builder, Value *Size, CallGraph *CG) const;
262
263 /// Deallocate memory according to the rules of the active lowering.
264 ///
265 /// \param CG - if non-null, will be updated for the new call
266 void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const;
267
268 Shape() = default;
269 explicit Shape(Function &F, bool OptimizeFrame = false)
270 : OptimizeFrame(OptimizeFrame) {
271 buildFrom(F);
272 }
273 void buildFrom(Function &F);
274};
275
278 Function &F, Shape &Shape, TargetTransformInfo &TTI,
279 const std::function<bool(Instruction &)> &MaterializableCallback);
280CallInst *createMustTailCall(DebugLoc Loc, Function *MustTailCallFn,
283} // End namespace coro.
284} // End namespace llvm
285
286#endif
AMDGPU Lower Kernel Arguments
#define LLVM_LIBRARY_VISIBILITY
Definition: Compiler.h:127
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())
This pass exposes codegen information to IR-level passes.
static const unsigned FramePtr
an instruction to allocate memory on the stack
Definition: Instructions.h:61
This represents either the llvm.coro.id.retcon or llvm.coro.id.retcon.once instruction.
Definition: CoroInstr.h:235
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:61
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:71
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:81
A constant pointer value that points to null.
Definition: Constants.h:549
This class represents the llvm.coro.begin instruction.
Definition: CoroInstr.h:451
AnyCoroIdInst * getId() const
Definition: CoroInstr.h:455
This represents the llvm.coro.id.async instruction.
Definition: CoroInstr.h:308
This represents the llvm.coro.id instruction.
Definition: CoroInstr.h:146
This is the common base class for debug info intrinsics for variables.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
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:214
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2686
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
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:1209
Class to represent struct types.
Definition: DerivedTypes.h:216
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:342
Multiway switch.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
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 IsEntryPoint)
Attempts to rewrite the location operand of debug intrinsics in terms of the coroutine frame pointer,...
Definition: CoroFrame.cpp:2930
@ 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:2278
bool declaresAnyIntrinsic(const Module &M)
Definition: Coroutines.cpp:103
bool declaresIntrinsics(const Module &M, const std::initializer_list< StringRef >)
Definition: Coroutines.cpp:115
void buildCoroutineFrame(Function &F, Shape &Shape, TargetTransformInfo &TTI, const std::function< bool(Instruction &)> &MaterializableCallback)
Definition: CoroFrame.cpp:3078
CallInst * createMustTailCall(DebugLoc Loc, Function *MustTailCallFn, TargetTransformInfo &TTI, ArrayRef< Value * > Arguments, IRBuilder<> &)
Definition: CoroSplit.cpp:1656
void replaceCoroFree(CoroIdInst *CoroId, bool Elide)
Definition: Coroutines.cpp:128
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:45
ConstantPointerNull *const NullPtr
Definition: CoroInternal.h:47
CallInst * makeSubFnCall(Value *Arg, int Index, Instruction *InsertPt)
Definition: Coroutines.cpp:50
LLVMContext & Context
Definition: CoroInternal.h:44
FunctionType *const ResumeFnType
Definition: CoroInternal.h:46
SmallVector< CallInst *, 2 > SymmetricTransfers
Definition: CoroInternal.h:88
SmallVector< CoroAwaitSuspendInst *, 4 > CoroAwaitSuspends
Definition: CoroInternal.h:87
AsyncLoweringStorage AsyncLowering
Definition: CoroInternal.h:151
FunctionType * getResumeFunctionType() const
Definition: CoroInternal.h:190
IntegerType * getIndexType() const
Definition: CoroInternal.h:175
StructType * FrameTy
Definition: CoroInternal.h:107
AnyCoroIdRetconInst * getRetconCoroId() const
Definition: CoroInternal.h:159
PointerType * getSwitchResumePointerType() const
Definition: CoroInternal.h:184
CoroIdInst * getSwitchCoroId() const
Definition: CoroInternal.h:154
SmallVector< CoroSizeInst *, 2 > CoroSizes
Definition: CoroInternal.h:83
CallingConv::ID getResumeFunctionCC() const
Definition: CoroInternal.h:229
ArrayRef< Type * > getRetconResumeTypes() const
Definition: CoroInternal.h:220
SmallVector< AnyCoroSuspendInst *, 4 > CoroSuspends
Definition: CoroInternal.h:85
Shape(Function &F, bool OptimizeFrame=false)
Definition: CoroInternal.h:269
SmallVector< CallInst *, 2 > SwiftErrorOps
Definition: CoroInternal.h:86
ConstantInt * getIndex(uint64_t Value) const
Definition: CoroInternal.h:180
AllocaInst * getPromiseAlloca() const
Definition: CoroInternal.h:243
bool OptimizeFrame
This would only be true if optimization are enabled.
Definition: CoroInternal.h:114
SwitchLoweringStorage SwitchLowering
Definition: CoroInternal.h:149
CoroBeginInst * CoroBegin
Definition: CoroInternal.h:81
BasicBlock::iterator getInsertPtAfterFramePtr() const
Definition: CoroInternal.h:249
ArrayRef< Type * > getRetconResultTypes() const
Definition: CoroInternal.h:207
RetconLoweringStorage RetconLowering
Definition: CoroInternal.h:150
SmallVector< CoroAlignInst *, 2 > CoroAligns
Definition: CoroInternal.h:84
CoroIdAsyncInst * getAsyncCoroId() const
Definition: CoroInternal.h:165
SmallVector< AnyCoroEndInst *, 4 > CoroEnds
Definition: CoroInternal.h:82
BasicBlock * AllocaSpillBlock
Definition: CoroInternal.h:111
unsigned getSwitchIndexField() const
Definition: CoroInternal.h:170