LLVM 22.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 void hidePromiseAlloca(CoroIdInst *CoroId, CoroBeginInst *CoroBegin);
34
35public:
36 Lowerer(Module &M)
37 : LowererBase(M), Builder(Context),
38 AnyResumeFnPtrTy(PointerType::getUnqual(Context)) {}
39 void lowerEarlyIntrinsics(Function &F);
40};
41}
42
43// Replace a direct call to coro.resume or coro.destroy with an indirect call to
44// an address returned by coro.subfn.addr intrinsic. This is done so that
45// CGPassManager recognizes devirtualization when CoroElide pass replaces a call
46// to coro.subfn.addr with an appropriate function address.
47void Lowerer::lowerResumeOrDestroy(CallBase &CB,
49 Value *ResumeAddr = makeSubFnCall(CB.getArgOperand(0), Index, &CB);
50 CB.setCalledOperand(ResumeAddr);
51 CB.setCallingConv(CallingConv::Fast);
52}
53
54// Coroutine promise field is always at the fixed offset from the beginning of
55// the coroutine frame. i8* coro.promise(i8*, i1 from) intrinsic adds an offset
56// to a passed pointer to move from coroutine frame to coroutine promise and
57// vice versa. Since we don't know exactly which coroutine frame it is, we build
58// a coroutine frame mock up starting with two function pointers, followed by a
59// properly aligned coroutine promise field.
60// TODO: Handle the case when coroutine promise alloca has align override.
61void Lowerer::lowerCoroPromise(CoroPromiseInst *Intrin) {
62 Value *Operand = Intrin->getArgOperand(0);
63 Align Alignment = Intrin->getAlignment();
64 Type *Int8Ty = Builder.getInt8Ty();
65
66 auto *SampleStruct =
67 StructType::get(Context, {AnyResumeFnPtrTy, AnyResumeFnPtrTy, Int8Ty});
68 const DataLayout &DL = TheModule.getDataLayout();
69 int64_t Offset = alignTo(
70 DL.getStructLayout(SampleStruct)->getElementOffset(2), Alignment);
71 if (Intrin->isFromPromise())
72 Offset = -Offset;
73
74 Builder.SetInsertPoint(Intrin);
75 Value *Replacement =
76 Builder.CreateConstInBoundsGEP1_32(Int8Ty, Operand, Offset);
77
78 Intrin->replaceAllUsesWith(Replacement);
79 Intrin->eraseFromParent();
80}
81
82// When a coroutine reaches final suspend point, it zeros out ResumeFnAddr in
83// the coroutine frame (it is UB to resume from a final suspend point).
84// The llvm.coro.done intrinsic is used to check whether a coroutine is
85// suspended at the final suspend point or not.
86void Lowerer::lowerCoroDone(IntrinsicInst *II) {
87 Value *Operand = II->getArgOperand(0);
88
89 // ResumeFnAddr is the first pointer sized element of the coroutine frame.
91 "resume function not at offset zero");
92 auto *FrameTy = Int8Ptr;
93
94 Builder.SetInsertPoint(II);
95 auto *Load = Builder.CreateLoad(FrameTy, Operand);
96 auto *Cond = Builder.CreateICmpEQ(Load, NullPtr);
97
98 II->replaceAllUsesWith(Cond);
99 II->eraseFromParent();
100}
101
103 Module &M = *NoopFn->getParent();
104 if (M.debug_compile_units().empty())
105 return;
106
107 DICompileUnit *CU = *M.debug_compile_units_begin();
108 DIBuilder DB(M, /*AllowUnresolved*/ false, CU);
109 std::array<Metadata *, 2> Params{nullptr, nullptr};
110 auto *SubroutineType =
111 DB.createSubroutineType(DB.getOrCreateTypeArray(Params));
112 StringRef Name = NoopFn->getName();
113 auto *SP = DB.createFunction(
114 CU, /*Name=*/Name, /*LinkageName=*/Name, /*File=*/ CU->getFile(),
115 /*LineNo=*/0, SubroutineType, /*ScopeLine=*/0, DINode::FlagArtificial,
116 DISubprogram::SPFlagDefinition);
117 NoopFn->setSubprogram(SP);
118 DB.finalize();
119}
120
121void Lowerer::lowerCoroNoop(IntrinsicInst *II) {
122 if (!NoopCoro) {
123 LLVMContext &C = Builder.getContext();
124 Module &M = *II->getModule();
125
126 // Create a noop.frame struct type.
127 auto *FnTy = FunctionType::get(Type::getVoidTy(C), Builder.getPtrTy(0),
128 /*isVarArg=*/false);
129 auto *FnPtrTy = Builder.getPtrTy(0);
130 StructType *FrameTy =
131 StructType::create({FnPtrTy, FnPtrTy}, "NoopCoro.Frame");
132
133 // Create a Noop function that does nothing.
135 FnTy, GlobalValue::LinkageTypes::PrivateLinkage,
136 M.getDataLayout().getProgramAddressSpace(), "__NoopCoro_ResumeDestroy",
137 &M);
138 NoopFn->setCallingConv(CallingConv::Fast);
140 auto *Entry = BasicBlock::Create(C, "entry", NoopFn);
141 ReturnInst::Create(C, Entry);
142
143 // Create a constant struct for the frame.
144 Constant* Values[] = {NoopFn, NoopFn};
145 Constant* NoopCoroConst = ConstantStruct::get(FrameTy, Values);
146 NoopCoro = new GlobalVariable(M, NoopCoroConst->getType(), /*isConstant=*/true,
147 GlobalVariable::PrivateLinkage, NoopCoroConst,
148 "NoopCoro.Frame.Const");
149 cast<GlobalVariable>(NoopCoro)->setNoSanitizeMetadata();
150 }
151
152 Builder.SetInsertPoint(II);
153 auto *NoopCoroVoidPtr = Builder.CreateBitCast(NoopCoro, Int8Ptr);
154 II->replaceAllUsesWith(NoopCoroVoidPtr);
155 II->eraseFromParent();
156}
157
158// Later middle-end passes will assume promise alloca dead after coroutine
159// suspend, leading to misoptimizations. We hide promise alloca using
160// coro.promise and will lower it back to alloca at CoroSplit.
161void Lowerer::hidePromiseAlloca(CoroIdInst *CoroId, CoroBeginInst *CoroBegin) {
162 auto *PA = CoroId->getPromise();
163 if (!PA || !CoroBegin)
164 return;
165 Builder.SetInsertPoint(*CoroBegin->getInsertionPointAfterDef());
166
167 auto *Alignment = Builder.getInt32(PA->getAlign().value());
168 auto *FromPromise = Builder.getInt1(false);
169 SmallVector<Value *, 3> Arg{CoroBegin, Alignment, FromPromise};
170 auto *PI = Builder.CreateIntrinsic(
171 Builder.getPtrTy(), Intrinsic::coro_promise, Arg, {}, "promise.addr");
172 PI->setCannotDuplicate();
173 // Remove lifetime markers, as these are only allowed on allocas.
174 for (User *U : make_early_inc_range(PA->users())) {
175 auto *I = cast<Instruction>(U);
176 if (I->isLifetimeStartOrEnd())
177 I->eraseFromParent();
178 }
179 PA->replaceUsesWithIf(PI, [CoroId](Use &U) {
180 bool IsBitcast = U == U.getUser()->stripPointerCasts();
181 bool IsCoroId = U.getUser() == CoroId;
182 return !IsBitcast && !IsCoroId;
183 });
184}
185
186// Prior to CoroSplit, calls to coro.begin needs to be marked as NoDuplicate,
187// as CoroSplit assumes there is exactly one coro.begin. After CoroSplit,
188// NoDuplicate attribute will be removed from coro.begin otherwise, it will
189// interfere with inlining.
190static void setCannotDuplicate(CoroIdInst *CoroId) {
191 for (User *U : CoroId->users())
192 if (auto *CB = dyn_cast<CoroBeginInst>(U))
193 CB->setCannotDuplicate();
194}
195
196void Lowerer::lowerEarlyIntrinsics(Function &F) {
197 CoroIdInst *CoroId = nullptr;
198 CoroBeginInst *CoroBegin = nullptr;
200 bool HasCoroSuspend = false;
201 for (Instruction &I : llvm::make_early_inc_range(instructions(F))) {
202 auto *CB = dyn_cast<CallBase>(&I);
203 if (!CB)
204 continue;
205
206 switch (CB->getIntrinsicID()) {
207 default:
208 continue;
209 case Intrinsic::coro_begin:
210 case Intrinsic::coro_begin_custom_abi:
211 if (CoroBegin)
213 "coroutine should have exactly one defining @llvm.coro.begin");
214 CoroBegin = cast<CoroBeginInst>(&I);
215 break;
216 case Intrinsic::coro_free:
217 CoroFrees.push_back(cast<CoroFreeInst>(&I));
218 break;
219 case Intrinsic::coro_suspend:
220 // Make sure that final suspend point is not duplicated as CoroSplit
221 // pass expects that there is at most one final suspend point.
222 if (cast<CoroSuspendInst>(&I)->isFinal())
223 CB->setCannotDuplicate();
224 HasCoroSuspend = true;
225 break;
226 case Intrinsic::coro_end_async:
227 case Intrinsic::coro_end:
228 // Make sure that fallthrough coro.end is not duplicated as CoroSplit
229 // pass expects that there is at most one fallthrough coro.end.
230 if (cast<AnyCoroEndInst>(&I)->isFallthrough())
231 CB->setCannotDuplicate();
232 break;
233 case Intrinsic::coro_noop:
234 lowerCoroNoop(cast<IntrinsicInst>(&I));
235 break;
236 case Intrinsic::coro_id:
237 if (auto *CII = cast<CoroIdInst>(&I)) {
238 if (CII->getInfo().isPreSplit()) {
239 assert(F.isPresplitCoroutine() &&
240 "The frontend uses Switch-Resumed ABI should emit "
241 "\"presplitcoroutine\" attribute for the coroutine.");
243 CII->setCoroutineSelf();
244 CoroId = cast<CoroIdInst>(&I);
245 }
246 }
247 break;
248 case Intrinsic::coro_id_retcon:
249 case Intrinsic::coro_id_retcon_once:
250 case Intrinsic::coro_id_async:
251 F.setPresplitCoroutine();
252 break;
253 case Intrinsic::coro_resume:
254 lowerResumeOrDestroy(*CB, CoroSubFnInst::ResumeIndex);
255 break;
256 case Intrinsic::coro_destroy:
257 lowerResumeOrDestroy(*CB, CoroSubFnInst::DestroyIndex);
258 break;
259 case Intrinsic::coro_promise:
260 lowerCoroPromise(cast<CoroPromiseInst>(&I));
261 break;
262 case Intrinsic::coro_done:
263 lowerCoroDone(cast<IntrinsicInst>(&I));
264 break;
265 }
266 }
267
268 if (CoroId) {
269 // Make sure that all CoroFree reference the coro.id intrinsic.
270 // Token type is not exposed through coroutine C/C++ builtins to plain C, so
271 // we allow specifying none and fixing it up here.
272 for (CoroFreeInst *CF : CoroFrees)
273 CF->setArgOperand(0, CoroId);
274
275 hidePromiseAlloca(CoroId, CoroBegin);
276 }
277
278 // Coroutine suspention could potentially lead to any argument modified
279 // outside of the function, hence arguments should not have noalias
280 // attributes.
281 if (HasCoroSuspend)
282 for (Argument &A : F.args())
283 if (A.hasNoAliasAttr())
284 A.removeAttr(Attribute::NoAlias);
285}
286
287static bool declaresCoroEarlyIntrinsics(const Module &M) {
288 // coro_suspend omitted as it is overloaded.
290 M, {Intrinsic::coro_id, Intrinsic::coro_id_retcon,
291 Intrinsic::coro_id_retcon_once, Intrinsic::coro_id_async,
292 Intrinsic::coro_destroy, Intrinsic::coro_done, Intrinsic::coro_end,
293 Intrinsic::coro_end_async, Intrinsic::coro_noop, Intrinsic::coro_free,
294 Intrinsic::coro_promise, Intrinsic::coro_resume});
295}
296
299 return PreservedAnalyses::all();
300
301 Lowerer L(M);
302 for (auto &F : M)
303 L.lowerEarlyIntrinsics(F);
304
307 return PA;
308}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static void setCannotDuplicate(CoroIdInst *CoroId)
static bool declaresCoroEarlyIntrinsics(const Module &M)
static void buildDebugInfoForNoopResumeDestroyFunc(Function *NoopFn)
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
Machine Check Debug Module
uint64_t IntrinsicInst * II
const SmallVectorImpl< MachineOperand > & Cond
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void setCallingConv(CallingConv::ID CC)
Value * getArgOperand(unsigned i) const
void setCannotDuplicate()
LLVM_ABI 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)
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
This class represents the llvm.coro.begin or llvm.coro.begin.custom.abi instructions.
Definition CoroInstr.h:449
This represents the llvm.coro.id instruction.
Definition CoroInstr.h:148
AllocaInst * getPromise() const
Definition CoroInstr.h:152
This represents the llvm.coro.promise instruction.
Definition CoroInstr.h:490
Align getAlignment() const
The required alignment of the promise.
Definition CoroInstr.h:502
bool isFromPromise() const
Are we translating from the frame to the promise (false) or from the promise to the frame (true)?
Definition CoroInstr.h:496
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
static Function * createWithDefaultAttr(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Creates a function with some attributes recorded in llvm.module.flags and the LLVMContext applied.
Definition Function.cpp:380
Module * getParent()
Get the module that this global value is contained inside of...
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition IRBuilder.h:497
Value * CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, const Twine &Name="")
Definition IRBuilder.h:1946
LLVM_ABI CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition IRBuilder.h:522
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2329
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2204
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition IRBuilder.h:1847
LLVMContext & getContext() const
Definition IRBuilder.h:203
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition IRBuilder.h:605
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:207
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition IRBuilder.h:552
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2780
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI std::optional< InstListType::iterator > getInsertionPointAfterDef()
Get the first insertion point at which the result of this instruction is defined.
A wrapper class for inspecting calls to intrinsic functions.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static LLVM_ABI 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:414
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition Type.cpp:620
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ Entry
Definition COFF.h:862
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
bool declaresIntrinsics(const Module &M, ArrayRef< Intrinsic::ID > List)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
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:626
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:155
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)