LLVM  10.0.0
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"
17 
18 namespace llvm {
19 
20 class CallGraph;
21 class CallGraphSCC;
22 class PassRegistry;
23 
24 void initializeCoroEarlyLegacyPass(PassRegistry &);
25 void initializeCoroSplitLegacyPass(PassRegistry &);
26 void initializeCoroElideLegacyPass(PassRegistry &);
27 void initializeCoroCleanupLegacyPass(PassRegistry &);
28 
29 // CoroEarly pass marks every function that has coro.begin with a string
30 // attribute "coroutine.presplit"="0". CoroSplit pass processes the coroutine
31 // twice. First, it lets it go through complete IPO optimization pipeline as a
32 // single function. It forces restart of the pipeline by inserting an indirect
33 // call to an empty function "coro.devirt.trigger" which is devirtualized by
34 // CoroElide pass that triggers a restart of the pipeline by CGPassManager.
35 // When CoroSplit pass sees the same coroutine the second time, it splits it up,
36 // adds coroutine subfunctions to the SCC to be processed by IPO pipeline.
37 
38 #define CORO_PRESPLIT_ATTR "coroutine.presplit"
39 #define UNPREPARED_FOR_SPLIT "0"
40 #define PREPARED_FOR_SPLIT "1"
41 
42 #define CORO_DEVIRT_TRIGGER_FN "coro.devirt.trigger"
43 
44 namespace coro {
45 
46 bool declaresIntrinsics(const Module &M,
47  const std::initializer_list<StringRef>);
48 void replaceAllCoroAllocs(CoroBeginInst *CB, bool Replacement);
49 void replaceAllCoroFrees(CoroBeginInst *CB, Value *Replacement);
50 void replaceCoroFree(CoroIdInst *CoroId, bool Elide);
52  CallGraph &CG, CallGraphSCC &SCC);
53 
54 // Keeps data and helper functions for lowering coroutine intrinsics.
55 struct LowererBase {
61 
62  LowererBase(Module &M);
63  Value *makeSubFnCall(Value *Arg, int Index, Instruction *InsertPt);
64 };
65 
66 enum class ABI {
67  /// The "resume-switch" lowering, where there are separate resume and
68  /// destroy functions that are shared between all suspend points. The
69  /// coroutine frame implicitly stores the resume and destroy functions,
70  /// the current index, and any promise value.
71  Switch,
72 
73  /// The "returned-continuation" lowering, where each suspend point creates a
74  /// single continuation function that is used for both resuming and
75  /// destroying. Does not support promises.
76  Retcon,
77 
78  /// The "unique returned-continuation" lowering, where each suspend point
79  /// creates a single continuation function that is used for both resuming
80  /// and destroying. Does not support promises. The function is known to
81  /// suspend at most once during its execution, and the return value of
82  /// the continuation is void.
83  RetconOnce,
84 };
85 
86 // Holds structural Coroutine Intrinsics for a particular function and other
87 // values used during CoroSplit pass.
94 
95  // Field indexes for special fields in the switch lowering.
97  enum {
102  /// The index of the first spill field.
103  FirstSpill
104  };
105  };
106 
108 
112 
118  };
119 
126  };
127 
128  union {
131  };
132 
134  assert(ABI == coro::ABI::Switch);
135  return cast<CoroIdInst>(CoroBegin->getId());
136  }
137 
139  assert(ABI == coro::ABI::Retcon ||
140  ABI == coro::ABI::RetconOnce);
141  return cast<AnyCoroIdRetconInst>(CoroBegin->getId());
142  }
143 
145  assert(ABI == coro::ABI::Switch);
146  assert(FrameTy && "frame type not assigned");
147  return cast<IntegerType>(FrameTy->getElementType(SwitchFieldIndex::Index));
148  }
149  ConstantInt *getIndex(uint64_t Value) const {
150  return ConstantInt::get(getIndexType(), Value);
151  }
152 
154  assert(ABI == coro::ABI::Switch);
155  assert(FrameTy && "frame type not assigned");
156  return cast<PointerType>(FrameTy->getElementType(SwitchFieldIndex::Resume));
157  }
158 
160  switch (ABI) {
161  case coro::ABI::Switch: {
162  auto *FnPtrTy = getSwitchResumePointerType();
163  return cast<FunctionType>(FnPtrTy->getPointerElementType());
164  }
165  case coro::ABI::Retcon:
167  return RetconLowering.ResumePrototype->getFunctionType();
168  }
169  llvm_unreachable("Unknown coro::ABI enum");
170  }
171 
173  assert(ABI == coro::ABI::Retcon ||
174  ABI == coro::ABI::RetconOnce);
175  auto FTy = CoroBegin->getFunction()->getFunctionType();
176 
177  // The safety of all this is checked by checkWFRetconPrototype.
178  if (auto STy = dyn_cast<StructType>(FTy->getReturnType())) {
179  return STy->elements().slice(1);
180  } else {
181  return ArrayRef<Type*>();
182  }
183  }
184 
186  assert(ABI == coro::ABI::Retcon ||
187  ABI == coro::ABI::RetconOnce);
188 
189  // The safety of all this is checked by checkWFRetconPrototype.
190  auto FTy = RetconLowering.ResumePrototype->getFunctionType();
191  return FTy->params().slice(1);
192  }
193 
195  switch (ABI) {
196  case coro::ABI::Switch:
197  return CallingConv::Fast;
198 
199  case coro::ABI::Retcon:
201  return RetconLowering.ResumePrototype->getCallingConv();
202  }
203  llvm_unreachable("Unknown coro::ABI enum");
204  }
205 
206  unsigned getFirstSpillFieldIndex() const {
207  switch (ABI) {
208  case coro::ABI::Switch:
209  return SwitchFieldIndex::FirstSpill;
210 
211  case coro::ABI::Retcon:
213  return 0;
214  }
215  llvm_unreachable("Unknown coro::ABI enum");
216  }
217 
219  if (ABI == coro::ABI::Switch)
220  return SwitchLowering.PromiseAlloca;
221  return nullptr;
222  }
223 
224  /// Allocate memory according to the rules of the active lowering.
225  ///
226  /// \param CG - if non-null, will be updated for the new call
227  Value *emitAlloc(IRBuilder<> &Builder, Value *Size, CallGraph *CG) const;
228 
229  /// Deallocate memory according to the rules of the active lowering.
230  ///
231  /// \param CG - if non-null, will be updated for the new call
232  void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const;
233 
234  Shape() = default;
235  explicit Shape(Function &F) { buildFrom(F); }
236  void buildFrom(Function &F);
237 };
238 
240 
241 } // End namespace coro.
242 } // End namespace llvm
243 
244 #endif
The "returned-continuation" lowering, where each suspend point creates a single continuation function...
Instruction * FramePtr
Definition: CoroInternal.h:110
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
The "unique returned-continuation" lowering, where each suspend point creates a single continuation f...
void initializeCoroElideLegacyPass(PassRegistry &)
CoroBeginInst * CoroBegin
Definition: CoroInternal.h:89
This represents the llvm.coro.id instruction.
Definition: CoroInstr.h:113
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:351
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
PointerType * getSwitchResumePointerType() const
Definition: CoroInternal.h:153
Shape(Function &F)
Definition: CoroInternal.h:235
IntegerType * getIndexType() const
Definition: CoroInternal.h:144
SmallVector< CallInst *, 2 > SwiftErrorOps
Definition: CoroInternal.h:93
RetconLoweringStorage RetconLowering
Definition: CoroInternal.h:130
F(f)
FunctionType * getResumeFunctionType() const
Definition: CoroInternal.h:159
PointerType *const Int8Ptr
Definition: CoroInternal.h:58
Class to represent struct types.
Definition: DerivedTypes.h:238
ConstantInt * getIndex(uint64_t Value) const
Definition: CoroInternal.h:149
Class to represent function types.
Definition: DerivedTypes.h:108
FunctionType *const ResumeFnType
Definition: CoroInternal.h:59
CoroIdInst * getSwitchCoroId() const
Definition: CoroInternal.h:133
SmallVector< CoroSizeInst *, 2 > CoroSizes
Definition: CoroInternal.h:91
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
AnyCoroIdRetconInst * getRetconCoroId() const
Definition: CoroInternal.h:138
This represents either the llvm.coro.id.retcon or llvm.coro.id.retcon.once instruction.
Definition: CoroInstr.h:204
Class to represent pointers.
Definition: DerivedTypes.h:579
void replaceAllCoroFrees(CoroBeginInst *CB, Value *Replacement)
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
void initializeCoroEarlyLegacyPass(PassRegistry &)
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library...
Definition: Compiler.h:129
unsigned getFirstSpillFieldIndex() const
Definition: CoroInternal.h:206
Class to represent integer types.
Definition: DerivedTypes.h:40
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:59
AllocaInst * getPromiseAlloca() const
Definition: CoroInternal.h:218
LLVMContext & Context
Definition: CoroInternal.h:57
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
SwitchLoweringStorage SwitchLowering
Definition: CoroInternal.h:129
AnyCoroIdInst * getId() const
Definition: CoroInstr.h:309
StructType * FrameTy
Definition: CoroInternal.h:109
bool declaresIntrinsics(const Module &M, const std::initializer_list< StringRef >)
Definition: Coroutines.cpp:154
void replaceAllCoroAllocs(CoroBeginInst *CB, bool Replacement)
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
ArrayRef< Type * > getRetconResumeTypes() const
Definition: CoroInternal.h:185
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
A constant pointer value that points to null.
Definition: Constants.h:539
ConstantPointerNull *const NullPtr
Definition: CoroInternal.h:60
void buildCoroutineFrame(Function &F, Shape &Shape)
Definition: CoroFrame.cpp:1325
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:704
CallingConv::ID getResumeFunctionCC() const
Definition: CoroInternal.h:194
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:163
This class represents the llvm.coro.begin instruction.
Definition: CoroInstr.h:305
Value * makeSubFnCall(Value *Arg, int Index, Instruction *InsertPt)
Definition: Coroutines.cpp:108
SmallVector< AnyCoroSuspendInst *, 4 > CoroSuspends
Definition: CoroInternal.h:92
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:73
uint32_t Size
Definition: Profile.cpp:46
void replaceCoroFree(CoroIdInst *CoroId, bool Elide)
Definition: Coroutines.cpp:167
Multiway switch.
void initializeCoroCleanupLegacyPass(PassRegistry &)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector< CoroEndInst *, 4 > CoroEnds
Definition: CoroInternal.h:90
LLVM Value Representation.
Definition: Value.h:74
Fast - This calling convention attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:42
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
BasicBlock * AllocaSpillBlock
Definition: CoroInternal.h:111
void initializeCoroSplitLegacyPass(PassRegistry &)
void updateCallGraph(Function &Caller, ArrayRef< Function *> Funcs, CallGraph &CG, CallGraphSCC &SCC)
Definition: Coroutines.cpp:208
an instruction to allocate memory on the stack
Definition: Instructions.h:59
ArrayRef< Type * > getRetconResultTypes() const
Definition: CoroInternal.h:172