LLVM 20.0.0git
CoroEarly.cpp
Go to the documentation of this file.
1//===- CoroEarly.cpp - Coroutine Early Function Pass ----------------------===//
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
10#include "CoroInternal.h"
11#include "llvm/IR/DIBuilder.h"
12#include "llvm/IR/Function.h"
13#include "llvm/IR/IRBuilder.h"
15#include "llvm/IR/Module.h"
17
18using namespace llvm;
19
20#define DEBUG_TYPE "coro-early"
21
22namespace {
23// Created on demand if the coro-early pass has work to do.
24class Lowerer : public coro::LowererBase {
25 IRBuilder<> Builder;
26 PointerType *const AnyResumeFnPtrTy;
27 Constant *NoopCoro = nullptr;
28
29 void lowerResumeOrDestroy(CallBase &CB, CoroSubFnInst::ResumeKind);
30 void lowerCoroPromise(CoroPromiseInst *Intrin);
31 void lowerCoroDone(IntrinsicInst *II);
32 void lowerCoroNoop(IntrinsicInst *II);
33
34public:
35 Lowerer(Module &M)
36 : LowererBase(M), Builder(Context),
37 AnyResumeFnPtrTy(PointerType::getUnqual(Context)) {}
38 void lowerEarlyIntrinsics(Function &F);
39};
40}
41
42// Replace a direct call to coro.resume or coro.destroy with an indirect call to
43// an address returned by coro.subfn.addr intrinsic. This is done so that
44// CGPassManager recognizes devirtualization when CoroElide pass replaces a call
45// to coro.subfn.addr with an appropriate function address.
46void Lowerer::lowerResumeOrDestroy(CallBase &CB,
48 Value *ResumeAddr = makeSubFnCall(CB.getArgOperand(0), Index, &CB);
49 CB.setCalledOperand(ResumeAddr);
51}
52
53// Coroutine promise field is always at the fixed offset from the beginning of
54// the coroutine frame. i8* coro.promise(i8*, i1 from) intrinsic adds an offset
55// to a passed pointer to move from coroutine frame to coroutine promise and
56// vice versa. Since we don't know exactly which coroutine frame it is, we build
57// a coroutine frame mock up starting with two function pointers, followed by a
58// properly aligned coroutine promise field.
59// TODO: Handle the case when coroutine promise alloca has align override.
60void Lowerer::lowerCoroPromise(CoroPromiseInst *Intrin) {
61 Value *Operand = Intrin->getArgOperand(0);
62 Align Alignment = Intrin->getAlignment();
63 Type *Int8Ty = Builder.getInt8Ty();
64
65 auto *SampleStruct =
66 StructType::get(Context, {AnyResumeFnPtrTy, AnyResumeFnPtrTy, Int8Ty});
67 const DataLayout &DL = TheModule.getDataLayout();
68 int64_t Offset = alignTo(
69 DL.getStructLayout(SampleStruct)->getElementOffset(2), Alignment);
70 if (Intrin->isFromPromise())
71 Offset = -Offset;
72
73 Builder.SetInsertPoint(Intrin);
74 Value *Replacement =
75 Builder.CreateConstInBoundsGEP1_32(Int8Ty, Operand, Offset);
76
77 Intrin->replaceAllUsesWith(Replacement);
78 Intrin->eraseFromParent();
79}
80
81// When a coroutine reaches final suspend point, it zeros out ResumeFnAddr in
82// the coroutine frame (it is UB to resume from a final suspend point).
83// The llvm.coro.done intrinsic is used to check whether a coroutine is
84// suspended at the final suspend point or not.
85void Lowerer::lowerCoroDone(IntrinsicInst *II) {
86 Value *Operand = II->getArgOperand(0);
87
88 // ResumeFnAddr is the first pointer sized element of the coroutine frame.
90 "resume function not at offset zero");
91 auto *FrameTy = Int8Ptr;
92
93 Builder.SetInsertPoint(II);
94 auto *Load = Builder.CreateLoad(FrameTy, Operand);
95 auto *Cond = Builder.CreateICmpEQ(Load, NullPtr);
96
97 II->replaceAllUsesWith(Cond);
98 II->eraseFromParent();
99}
100
102 Module &M = *NoopFn->getParent();
103 if (M.debug_compile_units().empty())
104 return;
105
106 DICompileUnit *CU = *M.debug_compile_units_begin();
107 DIBuilder DB(M, /*AllowUnresolved*/ false, CU);
108 std::array<Metadata *, 2> Params{nullptr, nullptr};
109 auto *SubroutineType =
110 DB.createSubroutineType(DB.getOrCreateTypeArray(Params));
111 StringRef Name = NoopFn->getName();
112 auto *SP = DB.createFunction(
113 CU, /*Name=*/Name, /*LinkageName=*/Name, /*File=*/ CU->getFile(),
114 /*LineNo=*/0, SubroutineType, /*ScopeLine=*/0, DINode::FlagArtificial,
115 DISubprogram::SPFlagDefinition);
116 NoopFn->setSubprogram(SP);
117 DB.finalize();
118}
119
120void Lowerer::lowerCoroNoop(IntrinsicInst *II) {
121 if (!NoopCoro) {
122 LLVMContext &C = Builder.getContext();
123 Module &M = *II->getModule();
124
125 // Create a noop.frame struct type.
126 auto *FnTy = FunctionType::get(Type::getVoidTy(C), Builder.getPtrTy(0),
127 /*isVarArg=*/false);
128 auto *FnPtrTy = Builder.getPtrTy(0);
129 StructType *FrameTy =
130 StructType::create({FnPtrTy, FnPtrTy}, "NoopCoro.Frame");
131
132 // Create a Noop function that does nothing.
133 Function *NoopFn =
134 Function::Create(FnTy, GlobalValue::LinkageTypes::PrivateLinkage,
135 "__NoopCoro_ResumeDestroy", &M);
136 NoopFn->setCallingConv(CallingConv::Fast);
138 auto *Entry = BasicBlock::Create(C, "entry", NoopFn);
139 ReturnInst::Create(C, Entry);
140
141 // Create a constant struct for the frame.
142 Constant* Values[] = {NoopFn, NoopFn};
143 Constant* NoopCoroConst = ConstantStruct::get(FrameTy, Values);
144 NoopCoro = new GlobalVariable(M, NoopCoroConst->getType(), /*isConstant=*/true,
145 GlobalVariable::PrivateLinkage, NoopCoroConst,
146 "NoopCoro.Frame.Const");
147 cast<GlobalVariable>(NoopCoro)->setNoSanitizeMetadata();
148 }
149
150 Builder.SetInsertPoint(II);
151 auto *NoopCoroVoidPtr = Builder.CreateBitCast(NoopCoro, Int8Ptr);
152 II->replaceAllUsesWith(NoopCoroVoidPtr);
153 II->eraseFromParent();
154}
155
156// Prior to CoroSplit, calls to coro.begin needs to be marked as NoDuplicate,
157// as CoroSplit assumes there is exactly one coro.begin. After CoroSplit,
158// NoDuplicate attribute will be removed from coro.begin otherwise, it will
159// interfere with inlining.
160static void setCannotDuplicate(CoroIdInst *CoroId) {
161 for (User *U : CoroId->users())
162 if (auto *CB = dyn_cast<CoroBeginInst>(U))
163 CB->setCannotDuplicate();
164}
165
166void Lowerer::lowerEarlyIntrinsics(Function &F) {
167 CoroIdInst *CoroId = nullptr;
169 bool HasCoroSuspend = false;
171 auto *CB = dyn_cast<CallBase>(&I);
172 if (!CB)
173 continue;
174
175 switch (CB->getIntrinsicID()) {
176 default:
177 continue;
178 case Intrinsic::coro_free:
179 CoroFrees.push_back(cast<CoroFreeInst>(&I));
180 break;
181 case Intrinsic::coro_suspend:
182 // Make sure that final suspend point is not duplicated as CoroSplit
183 // pass expects that there is at most one final suspend point.
184 if (cast<CoroSuspendInst>(&I)->isFinal())
185 CB->setCannotDuplicate();
186 HasCoroSuspend = true;
187 break;
188 case Intrinsic::coro_end_async:
189 case Intrinsic::coro_end:
190 // Make sure that fallthrough coro.end is not duplicated as CoroSplit
191 // pass expects that there is at most one fallthrough coro.end.
192 if (cast<AnyCoroEndInst>(&I)->isFallthrough())
193 CB->setCannotDuplicate();
194 break;
195 case Intrinsic::coro_noop:
196 lowerCoroNoop(cast<IntrinsicInst>(&I));
197 break;
198 case Intrinsic::coro_id:
199 if (auto *CII = cast<CoroIdInst>(&I)) {
200 if (CII->getInfo().isPreSplit()) {
201 assert(F.isPresplitCoroutine() &&
202 "The frontend uses Switch-Resumed ABI should emit "
203 "\"presplitcoroutine\" attribute for the coroutine.");
205 CII->setCoroutineSelf();
206 CoroId = cast<CoroIdInst>(&I);
207 }
208 }
209 break;
210 case Intrinsic::coro_id_retcon:
211 case Intrinsic::coro_id_retcon_once:
212 case Intrinsic::coro_id_async:
213 F.setPresplitCoroutine();
214 break;
215 case Intrinsic::coro_resume:
216 lowerResumeOrDestroy(*CB, CoroSubFnInst::ResumeIndex);
217 break;
218 case Intrinsic::coro_destroy:
219 lowerResumeOrDestroy(*CB, CoroSubFnInst::DestroyIndex);
220 break;
221 case Intrinsic::coro_promise:
222 lowerCoroPromise(cast<CoroPromiseInst>(&I));
223 break;
224 case Intrinsic::coro_done:
225 lowerCoroDone(cast<IntrinsicInst>(&I));
226 break;
227 }
228 }
229
230 // Make sure that all CoroFree reference the coro.id intrinsic.
231 // Token type is not exposed through coroutine C/C++ builtins to plain C, so
232 // we allow specifying none and fixing it up here.
233 if (CoroId)
234 for (CoroFreeInst *CF : CoroFrees)
235 CF->setArgOperand(0, CoroId);
236
237 // Coroutine suspention could potentially lead to any argument modified
238 // outside of the function, hence arguments should not have noalias
239 // attributes.
240 if (HasCoroSuspend)
241 for (Argument &A : F.args())
242 if (A.hasNoAliasAttr())
243 A.removeAttr(Attribute::NoAlias);
244}
245
246static bool declaresCoroEarlyIntrinsics(const Module &M) {
248 M, {"llvm.coro.id", "llvm.coro.id.retcon", "llvm.coro.id.retcon.once",
249 "llvm.coro.id.async", "llvm.coro.destroy", "llvm.coro.done",
250 "llvm.coro.end", "llvm.coro.end.async", "llvm.coro.noop",
251 "llvm.coro.free", "llvm.coro.promise", "llvm.coro.resume",
252 "llvm.coro.suspend"});
253}
254
257 return PreservedAnalyses::all();
258
259 Lowerer L(M);
260 for (auto &F : M)
261 L.lowerEarlyIntrinsics(F);
262
265 return PA;
266}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static void setCannotDuplicate(CoroIdInst *CoroId)
Definition: CoroEarly.cpp:160
static bool declaresCoroEarlyIntrinsics(const Module &M)
Definition: CoroEarly.cpp:246
static void buildDebugInfoForNoopResumeDestroyFunc(Function *NoopFn)
Definition: CoroEarly.cpp:101
std::string Name
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:212
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1120
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1411
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1294
void setCannotDuplicate()
Definition: InstrTypes.h:1928
Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
void setCalledOperand(Value *V)
Definition: InstrTypes.h:1385
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1378
This is an important base class in LLVM.
Definition: Constant.h:42
This represents the llvm.coro.free instruction.
Definition: CoroInstr.h:431
This represents the llvm.coro.id instruction.
Definition: CoroInstr.h:147
This represents the llvm.coro.promise instruction.
Definition: CoroInstr.h:489
Align getAlignment() const
The required alignment of the promise.
Definition: CoroInstr.h:501
bool isFromPromise() const
Are we translating from the frame to the promise (false) or from the promise to the frame (true)?
Definition: CoroInstr.h:495
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
Definition: Metadata.cpp:1870
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:173
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:92
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
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
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Class to represent struct types.
Definition: DerivedTypes.h:218
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:406
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:612
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
iterator_range< user_iterator > users()
Definition: Value.h:421
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
@ Entry
Definition: COFF.h:844
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:41
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
bool declaresIntrinsics(const Module &M, const std::initializer_list< StringRef >)
Definition: Coroutines.cpp:122
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CoroEarly.cpp:255