96 : TheModule(M),
Context(M.getContext()),
115 "makeSubFnCall: Index value out of range");
126 static const char *
const CoroIntrinsics[] = {
135 "llvm.coro.id.retcon",
136 "llvm.coro.id.retcon.once",
139 "llvm.coro.prepare.retcon",
144 "llvm.coro.subfn.addr",
146 "llvm.coro.suspend.retcon",
155 const std::initializer_list<StringRef>
List) {
170 if (
auto CF = dyn_cast<CoroFreeInst>(U))
173 if (CoroFrees.
empty())
178 : CoroFrees.
front()->getFrame();
181 CF->replaceAllUsesWith(Replacement);
182 CF->eraseFromParent();
194 if (
auto *Call = dyn_cast<CallBase>(&
I)) {
211 auto *ParentNode = CG[&ParentFunc];
212 ParentNode->removeAllCalledFunctions();
219 Nodes.push_back(Callee);
250 bool HasFinalSuspend =
false;
251 size_t FinalSuspendIndex = 0;
257 if (
auto II = dyn_cast<IntrinsicInst>(&
I)) {
258 switch (II->getIntrinsicID()) {
261 case Intrinsic::coro_size:
262 CoroSizes.
push_back(cast<CoroSizeInst>(II));
264 case Intrinsic::coro_frame:
265 CoroFrames.
push_back(cast<CoroFrameInst>(II));
267 case Intrinsic::coro_save:
271 UnusedCoroSaves.
push_back(cast<CoroSaveInst>(II));
273 case Intrinsic::coro_suspend_retcon: {
274 auto Suspend = cast<CoroSuspendRetconInst>(II);
275 CoroSuspends.push_back(Suspend);
278 case Intrinsic::coro_suspend: {
279 auto Suspend = cast<CoroSuspendInst>(II);
280 CoroSuspends.push_back(Suspend);
281 if (Suspend->isFinal()) {
284 "Only one suspend point can be marked as final");
285 HasFinalSuspend =
true;
286 FinalSuspendIndex = CoroSuspends.size() - 1;
290 case Intrinsic::coro_begin: {
291 auto CB = cast<CoroBeginInst>(II);
295 if (Id && !Id->getInfo().isPreSplit())
300 "coroutine should have exactly one defining @llvm.coro.begin");
304 Attribute::NoDuplicate);
308 case Intrinsic::coro_end:
309 CoroEnds.push_back(cast<CoroEndInst>(II));
310 if (CoroEnds.back()->isFallthrough()) {
313 if (CoroEnds.size() > 1) {
314 if (CoroEnds.front()->isFallthrough())
316 "Only one coro.end can be marked as fallthrough");
317 std::swap(CoroEnds.front(), CoroEnds.back());
331 CF->replaceAllUsesWith(
Undef);
332 CF->eraseFromParent();
339 CS->eraseFromParent();
340 if (
auto *CoroSave = CS->getCoroSave())
341 CoroSave->eraseFromParent();
351 auto Id = CoroBegin->getId();
352 switch (
auto IdIntrinsic =
Id->getIntrinsicID()) {
353 case Intrinsic::coro_id: {
354 auto SwitchId = cast<CoroIdInst>(
Id);
356 this->SwitchLowering.HasFinalSuspend = HasFinalSuspend;
357 this->SwitchLowering.ResumeSwitch =
nullptr;
358 this->SwitchLowering.PromiseAlloca = SwitchId->getPromise();
359 this->SwitchLowering.ResumeEntryBlock =
nullptr;
361 for (
auto AnySuspend : CoroSuspends) {
370 if (!Suspend->getCoroSave())
376 case Intrinsic::coro_id_retcon:
377 case Intrinsic::coro_id_retcon_once: {
378 auto ContinuationId = cast<AnyCoroIdRetconInst>(
Id);
379 ContinuationId->checkWellFormed();
380 this->
ABI = (IdIntrinsic == Intrinsic::coro_id_retcon
383 auto Prototype = ContinuationId->getPrototype();
384 this->RetconLowering.ResumePrototype =
Prototype;
385 this->RetconLowering.Alloc = ContinuationId->getAllocFunction();
386 this->RetconLowering.Dealloc = ContinuationId->getDeallocFunction();
387 this->RetconLowering.ReturnBlock =
nullptr;
388 this->RetconLowering.IsFrameInlineInStorage =
false;
392 auto ResultTys = getRetconResultTypes();
393 auto ResumeTys = getRetconResumeTypes();
395 for (
auto AnySuspend : CoroSuspends) {
402 "coro.suspend.retcon");
406 auto SI = Suspend->value_begin(), SE = Suspend->value_end();
407 auto RI = ResultTys.begin(), RE = ResultTys.end();
408 for (;
SI != SE && RI != RE; ++
SI, ++RI) {
409 auto SrcTy = (*SI)->getType();
425 "match corresponding prototype function result");
428 if (
SI != SE || RI != RE) {
437 Type *SResultTy = Suspend->getType();
441 }
else if (
auto SResultStructTy = dyn_cast<StructType>(SResultTy)) {
442 SuspendResultTys = SResultStructTy->elements();
445 SuspendResultTys = SResultTy;
447 if (SuspendResultTys.
size() != ResumeTys.size()) {
454 for (
size_t I = 0,
E = ResumeTys.size();
I !=
E; ++
I) {
455 if (SuspendResultTys[
I] != ResumeTys[
I]) {
461 "match corresponding prototype function param");
474 CF->replaceAllUsesWith(CoroBegin);
475 CF->eraseFromParent();
480 SwitchLowering.HasFinalSuspend &&
481 FinalSuspendIndex != CoroSuspends.size() - 1)
482 std::swap(CoroSuspends[FinalSuspendIndex], CoroSuspends.back());
486 CoroSave->eraseFromParent();
496 (*CG)[Call->
getFunction()]->addCalledFunction(Call, (*CG)[Callee]);
507 auto Alloc = RetconLowering.Alloc;
509 Alloc->getFunctionType()->getParamType(0),
528 auto Dealloc = RetconLowering.Dealloc;
530 Dealloc->getFunctionType()->getParamType(0));
531 auto *Call = Builder.
CreateCall(Dealloc, Ptr);
545 errs() <<
" Value: ";
558 fail(I,
"llvm.coro.id.retcon.* prototype not a Function", V);
560 auto FT =
F->getFunctionType();
562 if (isa<CoroIdRetconInst>(I)) {
564 if (FT->getReturnType()->isPointerTy()) {
566 }
else if (
auto SRetTy = dyn_cast<StructType>(FT->getReturnType())) {
567 ResultOkay = (!SRetTy->isOpaque() &&
568 SRetTy->getNumElements() > 0 &&
569 SRetTy->getElementType(0)->isPointerTy());
574 fail(I,
"llvm.coro.id.retcon prototype must return pointer as first " 577 if (FT->getReturnType() !=
579 fail(I,
"llvm.coro.id.retcon prototype return type must be same as" 580 "current function return type",
F);
585 if (FT->getNumParams() == 0 || !FT->getParamType(0)->isPointerTy())
586 fail(I,
"llvm.coro.id.retcon.* prototype must take pointer as " 587 "its first parameter",
F);
594 fail(I,
"llvm.coro.* allocator not a Function", V);
596 auto FT =
F->getFunctionType();
597 if (!FT->getReturnType()->isPointerTy())
598 fail(I,
"llvm.coro.* allocator must return a pointer",
F);
600 if (FT->getNumParams() != 1 ||
601 !FT->getParamType(0)->isIntegerTy())
602 fail(I,
"llvm.coro.* allocator must take integer as only param",
F);
609 fail(I,
"llvm.coro.* deallocator not a Function", V);
611 auto FT =
F->getFunctionType();
612 if (!FT->getReturnType()->isVoidTy())
613 fail(I,
"llvm.coro.* deallocator must return void",
F);
615 if (FT->getNumParams() != 1 ||
616 !FT->getParamType(0)->isPointerTy())
617 fail(I,
"llvm.coro.* deallocator must take pointer as only param",
F);
621 const char *Reason) {
622 if (!isa<ConstantInt>(V)) {
629 "size argument to coro.id.retcon.* must be constant");
631 "alignment argument to coro.id.retcon.* must be constant");
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
This represents the llvm.coro.suspend.retcon instruction.
The "returned-continuation" lowering, where each suspend point creates a single continuation function...
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
The "unique returned-continuation" lowering, where each suspend point creates a single continuation f...
void initializeCoroElideLegacyPass(PassRegistry &)
CoroBeginInst * CoroBegin
This represents the llvm.coro.id instruction.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
void push_back(const T &Elt)
A global registry used in conjunction with static constructors to make pluggable components (like tar...
static void buildCGN(CallGraph &CG, CallGraphNode *Node)
This class represents a function call, abstracting a target machine's calling convention.
EP_ScalarOptimizerLate - This extension point allows adding optimization passes after most of the mai...
The two locations do not alias at all.
static void addCoroutineSCCPasses(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM)
LLVMContext & getContext() const
All values hold a context through their type.
virtual void add(Pass *P)=0
Add a pass to the queue of passes to run.
void setArgOperand(unsigned i, Value *v)
A node in the call graph for a module.
void dump() const
Support for debugging, callable in GDB: V->dump()
static bool isCoroutineIntrinsicName(StringRef Name)
Attribute unwrap(LLVMAttributeRef Attr)
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
This represents the llvm.coro.suspend instruction.
This file contains the simple types necessary to represent the attributes associated with functions a...
void buildFrom(Function &F)
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
Class to represent function types.
EP_EnabledOnOptLevel0 - This extension point allows adding passes that should not be disabled by O0 o...
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
FunctionType *const ResumeFnType
SmallVector< CoroSizeInst *, 2 > CoroSizes
Value * emitAlloc(IRBuilder<> &Builder, Value *Size, CallGraph *CG) const
Allocate memory according to the rules of the active lowering.
static void checkConstantInt(const Instruction *I, Value *V, const char *Reason)
static void addCoroutineOptimizerLastPasses(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM)
This class represents a no-op cast from one type to another.
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Pass * createCoroEarlyLegacyPass()
Lower coroutine intrinsics that are not needed by later passes.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
This represents either the llvm.coro.id.retcon or llvm.coro.id.retcon.once instruction.
static void addCoroutineScalarOptimizerPasses(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM)
static void checkWFDealloc(const Instruction *I, Value *V)
Check that the given value is a well-formed deallocator.
bool isVoidTy() const
Return true if this is 'void'.
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
bool isLeaf(ID id)
Returns true if the intrinsic is a leaf, i.e.
CallGraphNode * getCallsExternalNode() const
The instances of the Type class are immutable: once they are created, they are never changed...
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void initializeCoroEarlyLegacyPass(PassRegistry &)
static bool isBitCastable(Type *SrcTy, Type *DestTy)
Check whether a bitcast between these types is valid.
This represents the llvm.coro.end instruction.
static void addCallToCallGraph(CallGraph *CG, CallInst *Call, Function *Callee)
void setCallingConv(CallingConv::ID CC)
ModulePass * createBarrierNoopPass()
createBarrierNoopPass - This pass is purely a module pass barrier in a pass manager.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
This represents the llvm.coro.save instruction.
void initialize(ArrayRef< CallGraphNode *> NewNodes)
const Function * getFunction() const
Return the function this instruction belongs to.
static void checkWFAlloc(const Instruction *I, Value *V)
Check that the given value is a well-formed allocator.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
EP_OptimizerLast – This extension point allows adding passes that run after everything else...
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
EP_EarlyAsPossible - This extension point allows adding passes before any other transformations, allowing them to see the code as it is coming out of the frontend.
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
This represents the llvm.coro.free instruction.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Function * getFunction() const
Returns the function that this call graph node represents.
EP_CGSCCOptimizerLate - This extension point allows adding CallGraphSCC passes at the end of the main...
bool declaresIntrinsics(const Module &M, const std::initializer_list< StringRef >)
static void addCoroutineOpt0Passes(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM)
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
void initializeCoroutines(PassRegistry &)
Initialize all passes linked into the Coroutines library.
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
static CoroSaveInst * createCoroSave(CoroBeginInst *CoroBegin, CoroSuspendInst *SuspendInst)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
A constant pointer value that points to null.
Type * getReturnType() const
void checkWellFormed() const
#define LLVM_ATTRIBUTE_NORETURN
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.
static void addCoroutineEarlyPasses(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM)
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
struct LLVMOpaquePassManager * LLVMPassManagerRef
This class represents the llvm.coro.begin instruction.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Value * makeSubFnCall(Value *Arg, int Index, Instruction *InsertPt)
SmallVector< AnyCoroSuspendInst *, 4 > CoroSuspends
amdgpu Simplify well known AMD library false FunctionCallee Callee
iterator_range< user_iterator > users()
static void clear(coro::Shape &Shape)
The basic data container for the call graph of a Module of IR.
LLVM_NODISCARD bool empty() const
Pass * createCoroSplitLegacyPass()
Split up coroutines into multiple functions driving their state machines.
void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const
Deallocate memory according to the rules of the active lowering.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
void replaceCoroFree(CoroIdInst *CoroId, bool Elide)
static LLVM_ATTRIBUTE_NORETURN void fail(const Instruction *I, const char *Reason, Value *V)
void initializeCoroCleanupLegacyPass(PassRegistry &)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector< CoroEndInst *, 4 > CoroEnds
Pass * createCoroElideLegacyPass()
Analyze coroutines use sites, devirtualize resume/destroy calls and elide heap allocation for corouti...
This represents the llvm.coro.frame instruction.
LLVM Value Representation.
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
CallGraphNode * getOrInsertFunction(const Function *F)
Similar to operator[], but this will insert a new CallGraphNode for F if one does not already exist...
void addCoroutinePassesToExtensionPoints(PassManagerBuilder &Builder)
Add all coroutine passes to appropriate extension points.
StringRef - Represent a constant reference to a string, i.e.
inst_range instructions(Function *F)
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
int lookupLLVMIntrinsicByName(ArrayRef< const char *> NameTable, StringRef Name)
Looks up Name in NameTable via binary search.
BasicBlock * AllocaSpillBlock
void initializeCoroSplitLegacyPass(PassRegistry &)
void addExtension(ExtensionPointTy Ty, ExtensionFn Fn)
static void checkWFRetconPrototype(const AnyCoroIdRetconInst *I, Value *V)
Check that the given value is a well-formed prototype for the llvm.coro.id.retcon.
Pass * createCoroCleanupLegacyPass()
Lower all remaining coroutine intrinsics.
unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
static IntegerType * getInt8Ty(LLVMContext &C)
CoroSaveInst * getCoroSave() const
void updateCallGraph(Function &Caller, ArrayRef< Function *> Funcs, CallGraph &CG, CallGraphSCC &SCC)
static void propagateCallAttrsFromCallee(CallInst *Call, Function *Callee)
void addCalledFunction(CallBase *Call, CallGraphNode *M)
Adds a function to the list of functions called by this one.