1//===- Coroutines.cpp -----------------------------------------------------===//
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//
9// This file implements the common infrastructure for Coroutine Passes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CoroInternal.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Analysis/CallGraph.h"
17#include "llvm/IR/Attributes.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/InstIterator.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/IR/Intrinsics.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/Type.h"
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Transforms/Coroutines/ABI.h"
30#include "llvm/Transforms/Coroutines/CoroInstr.h"
31#include "llvm/Transforms/Coroutines/CoroShape.h"
32#include "llvm/Transforms/Utils/Local.h"
33#include <cassert>
34#include <cstddef>
35#include <utility>
36
37using namespace llvm;
38
39// Construct the lowerer base class and initialize its members.
40coro::LowererBase::LowererBase(Module &M)
41 : TheModule(M), Context(M.getContext()),
42 Int8Ptr(PointerType::get(C&: Context, AddressSpace: 0)),
43 ResumeFnType(FunctionType::get(Result: Type::getVoidTy(C&: Context), Params: Int8Ptr,
44 /*isVarArg=*/false)),
45 NullPtr(ConstantPointerNull::get(T: Int8Ptr)) {}
46
47// Creates a call to llvm.coro.subfn.addr to obtain a resume function address.
48// It generates the following:
49//
50// call ptr @llvm.coro.subfn.addr(ptr %Arg, i8 %index)
51
52CallInst *coro::LowererBase::makeSubFnCall(Value *Arg, int Index,
53 Instruction *InsertPt) {
54 auto *IndexVal = ConstantInt::get(Ty: Type::getInt8Ty(C&: Context), V: Index);
55 auto *Fn =
56 Intrinsic::getOrInsertDeclaration(M: &TheModule, id: Intrinsic::coro_subfn_addr);
57
58 assert(Index >= CoroSubFnInst::IndexFirst &&
59 Index < CoroSubFnInst::IndexLast &&
60 "makeSubFnCall: Index value out of range");
61 return CallInst::Create(Func: Fn, Args: {Arg, IndexVal}, NameStr: "", InsertBefore: InsertPt->getIterator());
62}
63
64// We can only efficiently check for non-overloaded intrinsics.
65// The following intrinsics are absent for that reason:
66// coro_align, coro_size, coro_suspend_async, coro_suspend_retcon
67static Intrinsic::ID NonOverloadedCoroIntrinsics[] = {
68 Intrinsic::coro_alloc,
69 Intrinsic::coro_async_context_alloc,
70 Intrinsic::coro_async_context_dealloc,
71 Intrinsic::coro_async_resume,
72 Intrinsic::coro_async_size_replace,
73 Intrinsic::coro_await_suspend_bool,
74 Intrinsic::coro_await_suspend_handle,
75 Intrinsic::coro_await_suspend_void,
76 Intrinsic::coro_begin,
77 Intrinsic::coro_begin_custom_abi,
78 Intrinsic::coro_destroy,
79 Intrinsic::coro_done,
80 Intrinsic::coro_end,
81 Intrinsic::coro_end_async,
82 Intrinsic::coro_frame,
83 Intrinsic::coro_free,
84 Intrinsic::coro_id,
85 Intrinsic::coro_id_async,
86 Intrinsic::coro_id_retcon,
87 Intrinsic::coro_id_retcon_once,
88 Intrinsic::coro_noop,
89 Intrinsic::coro_prepare_async,
90 Intrinsic::coro_prepare_retcon,
91 Intrinsic::coro_promise,
92 Intrinsic::coro_resume,
93 Intrinsic::coro_save,
94 Intrinsic::coro_subfn_addr,
95 Intrinsic::coro_suspend,
96 Intrinsic::coro_is_in_ramp,
97};
98
99bool coro::isSuspendBlock(BasicBlock *BB) {
100 return isa<AnyCoroSuspendInst>(Val: BB->front());
101}
102
103bool coro::declaresAnyIntrinsic(const Module &M) {
104 return declaresIntrinsics(M, List: NonOverloadedCoroIntrinsics);
105}
106
107// Checks whether the module declares any of the listed intrinsics.
108bool coro::declaresIntrinsics(const Module &M, ArrayRef<Intrinsic::ID> List) {
109#ifndef NDEBUG
110 for (Intrinsic::ID ID : List)
111 assert(!Intrinsic::isOverloaded(ID) &&
112 "Only non-overloaded intrinsics supported");
113#endif
114
115 for (Intrinsic::ID ID : List)
116 if (Intrinsic::getDeclarationIfExists(M: &M, id: ID))
117 return true;
118 return false;
119}
120
121// Replace all coro.frees associated with the provided frame with 'null' and
122// erase all associated coro.deads
123void coro::elideCoroFree(Value *FramePtr) {
124 SmallVector<CoroFreeInst *, 4> CoroFrees;
125 SmallVector<CoroDeadInst *, 4> CoroDeads;
126 for (User *U : FramePtr->users()) {
127 if (auto *CF = dyn_cast<CoroFreeInst>(Val: U))
128 CoroFrees.push_back(Elt: CF);
129 else if (auto *CD = dyn_cast<CoroDeadInst>(Val: U))
130 CoroDeads.push_back(Elt: CD);
131 }
132
133 Value *Replacement =
134 ConstantPointerNull::get(T: PointerType::get(C&: FramePtr->getContext(), AddressSpace: 0));
135 for (CoroFreeInst *CF : CoroFrees) {
136 CF->replaceAllUsesWith(V: Replacement);
137 CF->eraseFromParent();
138 }
139
140 for (auto *CD : CoroDeads)
141 CD->eraseFromParent();
142}
143
144void coro::suppressCoroAllocs(CoroIdInst *CoroId) {
145 SmallVector<CoroAllocInst *, 4> CoroAllocs;
146 for (User *U : CoroId->users())
147 if (auto *CA = dyn_cast<CoroAllocInst>(Val: U))
148 CoroAllocs.push_back(Elt: CA);
149
150 if (CoroAllocs.empty())
151 return;
152
153 coro::suppressCoroAllocs(Context&: CoroId->getContext(), CoroAllocs);
154}
155
156// Replacing llvm.coro.alloc with false will suppress dynamic
157// allocation as it is expected for the frontend to generate the code that
158// looks like:
159// id = coro.id(...)
160// mem = coro.alloc(id) ? malloc(coro.size()) : 0;
161// coro.begin(id, mem)
162void coro::suppressCoroAllocs(LLVMContext &Context,
163 ArrayRef<CoroAllocInst *> CoroAllocs) {
164 auto *False = ConstantInt::getFalse(Context);
165 for (auto *CA : CoroAllocs) {
166 CA->replaceAllUsesWith(V: False);
167 CA->eraseFromParent();
168 }
169}
170
171static CoroSaveInst *createCoroSave(CoroBeginInst *CoroBegin,
172 CoroSuspendInst *SuspendInst) {
173 Module *M = SuspendInst->getModule();
174 auto *Fn = Intrinsic::getOrInsertDeclaration(M, id: Intrinsic::coro_save);
175 auto *SaveInst = cast<CoroSaveInst>(
176 Val: CallInst::Create(Func: Fn, Args: CoroBegin, NameStr: "", InsertBefore: SuspendInst->getIterator()));
177 assert(!SuspendInst->getCoroSave());
178 SuspendInst->setArgOperand(i: 0, v: SaveInst);
179 return SaveInst;
180}
181
182// Collect "interesting" coroutine intrinsics.
183void coro::Shape::analyze(Function &F,
184 SmallVectorImpl<CoroFrameInst *> &CoroFrames,
185 SmallVectorImpl<CoroSaveInst *> &UnusedCoroSaves) {
186 clear();
187
188 bool HasFinalSuspend = false;
189 bool HasUnwindCoroEnd = false;
190 size_t FinalSuspendIndex = 0;
191
192 for (Instruction &I : instructions(F)) {
193 // FIXME: coro_await_suspend_* are not proper `IntrinisicInst`s
194 // because they might be invoked
195 if (auto AWS = dyn_cast<CoroAwaitSuspendInst>(Val: &I)) {
196 CoroAwaitSuspends.push_back(Elt: AWS);
197 } else if (auto II = dyn_cast<IntrinsicInst>(Val: &I)) {
198 switch (II->getIntrinsicID()) {
199 default:
200 continue;
201 case Intrinsic::coro_size:
202 CoroSizes.push_back(Elt: cast<CoroSizeInst>(Val: II));
203 break;
204 case Intrinsic::coro_align:
205 CoroAligns.push_back(Elt: cast<CoroAlignInst>(Val: II));
206 break;
207 case Intrinsic::coro_frame:
208 CoroFrames.push_back(Elt: cast<CoroFrameInst>(Val: II));
209 break;
210 case Intrinsic::coro_save:
211 // After optimizations, coro_suspends using this coro_save might have
212 // been removed, remember orphaned coro_saves to remove them later.
213 if (II->use_empty())
214 UnusedCoroSaves.push_back(Elt: cast<CoroSaveInst>(Val: II));
215 break;
216 case Intrinsic::coro_suspend_async: {
217 auto *Suspend = cast<CoroSuspendAsyncInst>(Val: II);
218 Suspend->checkWellFormed();
219 CoroSuspends.push_back(Elt: Suspend);
220 break;
221 }
222 case Intrinsic::coro_suspend_retcon: {
223 auto Suspend = cast<CoroSuspendRetconInst>(Val: II);
224 CoroSuspends.push_back(Elt: Suspend);
225 break;
226 }
227 case Intrinsic::coro_suspend: {
228 auto Suspend = cast<CoroSuspendInst>(Val: II);
229 CoroSuspends.push_back(Elt: Suspend);
230 if (Suspend->isFinal()) {
231 if (HasFinalSuspend)
232 report_fatal_error(
233 reason: "Only one suspend point can be marked as final");
234 HasFinalSuspend = true;
235 FinalSuspendIndex = CoroSuspends.size() - 1;
236 }
237 break;
238 }
239 case Intrinsic::coro_begin:
240 case Intrinsic::coro_begin_custom_abi: {
241 auto CB = cast<CoroBeginInst>(Val: II);
242
243 // Ignore coro id's that aren't pre-split.
244 auto Id = dyn_cast<CoroIdInst>(Val: CB->getId());
245 if (Id && !Id->getInfo().isPreSplit())
246 break;
247
248 if (CoroBegin)
249 report_fatal_error(
250 reason: "coroutine should have exactly one defining @llvm.coro.begin");
251 CB->addRetAttr(Kind: Attribute::NonNull);
252 CB->addRetAttr(Kind: Attribute::NoAlias);
253 CB->removeFnAttr(Kind: Attribute::NoDuplicate);
254 CoroBegin = CB;
255 break;
256 }
257 case Intrinsic::coro_end_async:
258 case Intrinsic::coro_end:
259 CoroEnds.push_back(Elt: cast<AnyCoroEndInst>(Val: II));
260 if (auto *AsyncEnd = dyn_cast<CoroAsyncEndInst>(Val: II)) {
261 AsyncEnd->checkWellFormed();
262 }
263
264 if (CoroEnds.back()->isUnwind())
265 HasUnwindCoroEnd = true;
266
267 if (CoroEnds.back()->isFallthrough() && isa<CoroEndInst>(Val: II)) {
268 // Make sure that the fallthrough coro.end is the first element in the
269 // CoroEnds vector.
270 // Note: I don't think this is neccessary anymore.
271 if (CoroEnds.size() > 1) {
272 if (CoroEnds.front()->isFallthrough())
273 report_fatal_error(
274 reason: "Only one coro.end can be marked as fallthrough");
275 std::swap(a&: CoroEnds.front(), b&: CoroEnds.back());
276 }
277 }
278 break;
279 case Intrinsic::coro_is_in_ramp:
280 CoroIsInRampInsts.push_back(Elt: cast<CoroIsInRampInst>(Val: II));
281 break;
282 }
283 }
284 }
285
286 // If there is no CoroBegin then this is not a coroutine.
287 if (!CoroBegin)
288 return;
289
290 // Determination of ABI and initializing lowering info
291 auto Id = CoroBegin->getId();
292 switch (auto IntrID = Id->getIntrinsicID()) {
293 case Intrinsic::coro_id: {
294 ABI = coro::ABI::Switch;
295 SwitchLowering.HasFinalSuspend = HasFinalSuspend;
296 SwitchLowering.HasUnwindCoroEnd = HasUnwindCoroEnd;
297 SwitchLowering.HasCoroElideNoAllocVariant = false;
298
299 auto SwitchId = getSwitchCoroId();
300 SwitchLowering.ResumeSwitch = nullptr;
301 SwitchLowering.PromiseAlloca = SwitchId->getPromise();
302 SwitchLowering.ResumeEntryBlock = nullptr;
303
304 // Move final suspend to the last element in the CoroSuspends vector.
305 if (SwitchLowering.HasFinalSuspend &&
306 FinalSuspendIndex != CoroSuspends.size() - 1)
307 std::swap(a&: CoroSuspends[FinalSuspendIndex], b&: CoroSuspends.back());
308 break;
309 }
310 case Intrinsic::coro_id_async: {
311 ABI = coro::ABI::Async;
312 auto *AsyncId = getAsyncCoroId();
313 AsyncId->checkWellFormed();
314 AsyncLowering.Context = AsyncId->getStorage();
315 AsyncLowering.ContextArgNo = AsyncId->getStorageArgumentIndex();
316 AsyncLowering.ContextHeaderSize = AsyncId->getStorageSize();
317 AsyncLowering.ContextAlignment = AsyncId->getStorageAlignment().value();
318 AsyncLowering.AsyncFuncPointer = AsyncId->getAsyncFunctionPointer();
319 AsyncLowering.AsyncCC = F.getCallingConv();
320 break;
321 }
322 case Intrinsic::coro_id_retcon:
323 case Intrinsic::coro_id_retcon_once: {
324 ABI = IntrID == Intrinsic::coro_id_retcon ? coro::ABI::Retcon
325 : coro::ABI::RetconOnce;
326 auto ContinuationId = getRetconCoroId();
327 ContinuationId->checkWellFormed();
328 auto Prototype = ContinuationId->getPrototype();
329 RetconLowering.ResumePrototype = Prototype;
330 RetconLowering.Alloc = ContinuationId->getAllocFunction();
331 RetconLowering.Dealloc = ContinuationId->getDeallocFunction();
332 RetconLowering.ReturnBlock = nullptr;
333 RetconLowering.IsFrameInlineInStorage = false;
334 break;
335 }
336 default:
337 llvm_unreachable("coro.begin is not dependent on a coro.id call");
338 }
339}
340
341// If for some reason, we were not able to find coro.begin, bailout.
342void coro::Shape::invalidateCoroutine(
343 Function &F, SmallVectorImpl<CoroFrameInst *> &CoroFrames) {
344 assert(!CoroBegin);
345 {
346 // Replace coro.frame which are supposed to be lowered to the result of
347 // coro.begin with poison.
348 auto *Poison = PoisonValue::get(T: PointerType::get(C&: F.getContext(), AddressSpace: 0));
349 for (CoroFrameInst *CF : CoroFrames) {
350 CF->replaceAllUsesWith(V: Poison);
351 CF->eraseFromParent();
352 }
353 CoroFrames.clear();
354
355 // Replace all coro.suspend with poison and remove related coro.saves if
356 // present.
357 for (AnyCoroSuspendInst *CS : CoroSuspends) {
358 CS->replaceAllUsesWith(V: PoisonValue::get(T: CS->getType()));
359 if (auto *CoroSave = CS->getCoroSave())
360 CoroSave->eraseFromParent();
361 CS->eraseFromParent();
362 }
363 CoroSuspends.clear();
364
365 // Replace all coro.ends with unreachable instruction.
366 for (AnyCoroEndInst *CE : CoroEnds)
367 changeToUnreachable(I: CE);
368 }
369}
370
371void coro::SwitchABI::init() {
372 assert(Shape.ABI == coro::ABI::Switch);
373 {
374 for (auto *AnySuspend : Shape.CoroSuspends) {
375 auto Suspend = dyn_cast<CoroSuspendInst>(Val: AnySuspend);
376 if (!Suspend) {
377#ifndef NDEBUG
378 AnySuspend->dump();
379#endif
380 report_fatal_error(reason: "coro.id must be paired with coro.suspend");
381 }
382
383 if (!Suspend->getCoroSave())
384 createCoroSave(CoroBegin: Shape.CoroBegin, SuspendInst: Suspend);
385 }
386 }
387}
388
389void coro::AsyncABI::init() { assert(Shape.ABI == coro::ABI::Async); }
390
391void coro::AnyRetconABI::init() {
392 assert(Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce);
393 {
394 // Determine the result value types, and make sure they match up with
395 // the values passed to the suspends.
396 auto ResultTys = Shape.getRetconResultTypes();
397 auto ResumeTys = Shape.getRetconResumeTypes();
398
399 for (auto *AnySuspend : Shape.CoroSuspends) {
400 auto Suspend = dyn_cast<CoroSuspendRetconInst>(Val: AnySuspend);
401 if (!Suspend) {
402#ifndef NDEBUG
403 AnySuspend->dump();
404#endif
405 report_fatal_error(reason: "coro.id.retcon.* must be paired with "
406 "coro.suspend.retcon");
407 }
408
409 // Check that the argument types of the suspend match the results.
410 auto SI = Suspend->value_begin(), SE = Suspend->value_end();
411 auto RI = ResultTys.begin(), RE = ResultTys.end();
412 for (; SI != SE && RI != RE; ++SI, ++RI) {
413 auto SrcTy = (*SI)->getType();
414 if (SrcTy != *RI) {
415 // The optimizer likes to eliminate bitcasts leading into variadic
416 // calls, but that messes with our invariants. Re-insert the
417 // bitcast and ignore this type mismatch.
418 if (CastInst::isBitCastable(SrcTy, DestTy: *RI)) {
419 auto BCI = new BitCastInst(*SI, *RI, "", Suspend->getIterator());
420 SI->set(BCI);
421 continue;
422 }
423
424#ifndef NDEBUG
425 Suspend->dump();
426 Shape.RetconLowering.ResumePrototype->getFunctionType()->dump();
427#endif
428 report_fatal_error(reason: "argument to coro.suspend.retcon does not "
429 "match corresponding prototype function result");
430 }
431 }
432 if (SI != SE || RI != RE) {
433#ifndef NDEBUG
434 Suspend->dump();
435 Shape.RetconLowering.ResumePrototype->getFunctionType()->dump();
436#endif
437 report_fatal_error(reason: "wrong number of arguments to coro.suspend.retcon");
438 }
439
440 // Check that the result type of the suspend matches the resume types.
441 Type *SResultTy = Suspend->getType();
442 ArrayRef<Type *> SuspendResultTys;
443 if (SResultTy->isVoidTy()) {
444 // leave as empty array
445 } else if (auto SResultStructTy = dyn_cast<StructType>(Val: SResultTy)) {
446 SuspendResultTys = SResultStructTy->elements();
447 } else {
448 // forms an ArrayRef using SResultTy, be careful
449 SuspendResultTys = SResultTy;
450 }
451 if (SuspendResultTys.size() != ResumeTys.size()) {
452#ifndef NDEBUG
453 Suspend->dump();
454 Shape.RetconLowering.ResumePrototype->getFunctionType()->dump();
455#endif
456 report_fatal_error(reason: "wrong number of results from coro.suspend.retcon");
457 }
458 for (size_t I = 0, E = ResumeTys.size(); I != E; ++I) {
459 if (SuspendResultTys[I] != ResumeTys[I]) {
460#ifndef NDEBUG
461 Suspend->dump();
462 Shape.RetconLowering.ResumePrototype->getFunctionType()->dump();
463#endif
464 report_fatal_error(reason: "result from coro.suspend.retcon does not "
465 "match corresponding prototype function param");
466 }
467 }
468 }
469 }
470}
471
472void coro::Shape::cleanCoroutine(
473 SmallVectorImpl<CoroFrameInst *> &CoroFrames,
474 SmallVectorImpl<CoroSaveInst *> &UnusedCoroSaves) {
475 // The coro.frame intrinsic is always lowered to the result of coro.begin.
476 for (CoroFrameInst *CF : CoroFrames) {
477 CF->replaceAllUsesWith(V: CoroBegin);
478 CF->eraseFromParent();
479 }
480 CoroFrames.clear();
481
482 // Remove orphaned coro.saves.
483 for (CoroSaveInst *CoroSave : UnusedCoroSaves)
484 CoroSave->eraseFromParent();
485 UnusedCoroSaves.clear();
486}
487
488static void propagateCallAttrsFromCallee(CallInst *Call, Function *Callee) {
489 Call->setCallingConv(Callee->getCallingConv());
490 // TODO: attributes?
491}
492
493static void addCallToCallGraph(CallGraph *CG, CallInst *Call, Function *Callee){
494 if (CG)
495 (*CG)[Call->getFunction()]->addCalledFunction(Call, M: (*CG)[Callee]);
496}
497
498Value *coro::Shape::emitAlloc(IRBuilder<> &Builder, Value *Size,
499 CallGraph *CG) const {
500 switch (ABI) {
501 case coro::ABI::Switch:
502 llvm_unreachable("can't allocate memory in coro switch-lowering");
503
504 case coro::ABI::Retcon:
505 case coro::ABI::RetconOnce: {
506 auto Alloc = RetconLowering.Alloc;
507 Size = Builder.CreateIntCast(V: Size,
508 DestTy: Alloc->getFunctionType()->getParamType(i: 0),
509 /*is signed*/ isSigned: false);
510 auto *Call = Builder.CreateCall(Callee: Alloc, Args: Size);
511 propagateCallAttrsFromCallee(Call, Callee: Alloc);
512 addCallToCallGraph(CG, Call, Callee: Alloc);
513 return Call;
514 }
515 case coro::ABI::Async:
516 llvm_unreachable("can't allocate memory in coro async-lowering");
517 }
518 llvm_unreachable("Unknown coro::ABI enum");
519}
520
521void coro::Shape::emitDealloc(IRBuilder<> &Builder, Value *Ptr,
522 CallGraph *CG) const {
523 switch (ABI) {
524 case coro::ABI::Switch:
525 llvm_unreachable("can't allocate memory in coro switch-lowering");
526
527 case coro::ABI::Retcon:
528 case coro::ABI::RetconOnce: {
529 auto Dealloc = RetconLowering.Dealloc;
530 Ptr = Builder.CreateBitCast(V: Ptr,
531 DestTy: Dealloc->getFunctionType()->getParamType(i: 0));
532 auto *Call = Builder.CreateCall(Callee: Dealloc, Args: Ptr);
533 propagateCallAttrsFromCallee(Call, Callee: Dealloc);
534 addCallToCallGraph(CG, Call, Callee: Dealloc);
535 return;
536 }
537 case coro::ABI::Async:
538 llvm_unreachable("can't allocate memory in coro async-lowering");
539 }
540 llvm_unreachable("Unknown coro::ABI enum");
541}
542
543[[noreturn]] static void fail(const Instruction *I, const char *Reason,
544 Value *V) {
545#ifndef NDEBUG
546 I->dump();
547 if (V) {
548 errs() << " Value: ";
549 V->printAsOperand(llvm::errs());
550 errs() << '\n';
551 }
552#endif
553 report_fatal_error(reason: Reason);
554}
555
556/// Check that the given value is a well-formed prototype for the
557/// llvm.coro.id.retcon.* intrinsics.
558static void checkWFRetconPrototype(const AnyCoroIdRetconInst *I, Value *V) {
559 auto F = dyn_cast<Function>(Val: V->stripPointerCastsAndAliases());
560 if (!F)
561 fail(I, Reason: "llvm.coro.id.retcon.* prototype not a Function", V);
562
563 auto FT = F->getFunctionType();
564
565 if (isa<CoroIdRetconInst>(Val: I)) {
566 bool ResultOkay;
567 if (FT->getReturnType()->isPointerTy()) {
568 ResultOkay = true;
569 } else if (auto SRetTy = dyn_cast<StructType>(Val: FT->getReturnType())) {
570 ResultOkay = (!SRetTy->isOpaque() &&
571 SRetTy->getNumElements() > 0 &&
572 SRetTy->getElementType(N: 0)->isPointerTy());
573 } else {
574 ResultOkay = false;
575 }
576 if (!ResultOkay)
577 fail(I, Reason: "llvm.coro.id.retcon prototype must return pointer as first "
578 "result", V: F);
579
580 if (FT->getReturnType() !=
581 I->getFunction()->getFunctionType()->getReturnType())
582 fail(I, Reason: "llvm.coro.id.retcon prototype return type must be same as"
583 "current function return type", V: F);
584 } else {
585 // No meaningful validation to do here for llvm.coro.id.unique.once.
586 }
587
588 if (FT->getNumParams() == 0 || !FT->getParamType(i: 0)->isPointerTy())
589 fail(I, Reason: "llvm.coro.id.retcon.* prototype must take pointer as "
590 "its first parameter", V: F);
591}
592
593/// Check that the given value is a well-formed allocator.
594static void checkWFAlloc(const Instruction *I, Value *V) {
595 auto F = dyn_cast<Function>(Val: V->stripPointerCastsAndAliases());
596 if (!F)
597 fail(I, Reason: "llvm.coro.* allocator not a Function", V);
598
599 auto FT = F->getFunctionType();
600 if (!FT->getReturnType()->isPointerTy())
601 fail(I, Reason: "llvm.coro.* allocator must return a pointer", V: F);
602
603 if (FT->getNumParams() != 1 ||
604 !FT->getParamType(i: 0)->isIntegerTy())
605 fail(I, Reason: "llvm.coro.* allocator must take integer as only param", V: F);
606}
607
608/// Check that the given value is a well-formed deallocator.
609static void checkWFDealloc(const Instruction *I, Value *V) {
610 auto F = dyn_cast<Function>(Val: V->stripPointerCastsAndAliases());
611 if (!F)
612 fail(I, Reason: "llvm.coro.* deallocator not a Function", V);
613
614 auto FT = F->getFunctionType();
615 if (!FT->getReturnType()->isVoidTy())
616 fail(I, Reason: "llvm.coro.* deallocator must return void", V: F);
617
618 if (FT->getNumParams() != 1 ||
619 !FT->getParamType(i: 0)->isPointerTy())
620 fail(I, Reason: "llvm.coro.* deallocator must take pointer as only param", V: F);
621}
622
623static void checkConstantInt(const Instruction *I, Value *V,
624 const char *Reason) {
625 if (!isa<ConstantInt>(Val: V)) {
626 fail(I, Reason, V);
627 }
628}
629
630void AnyCoroIdRetconInst::checkWellFormed() const {
631 checkConstantInt(I: this, V: getArgOperand(i: SizeArg),
632 Reason: "size argument to coro.id.retcon.* must be constant");
633 checkConstantInt(I: this, V: getArgOperand(i: AlignArg),
634 Reason: "alignment argument to coro.id.retcon.* must be constant");
635 checkWFRetconPrototype(I: this, V: getArgOperand(i: PrototypeArg));
636 checkWFAlloc(I: this, V: getArgOperand(i: AllocArg));
637 checkWFDealloc(I: this, V: getArgOperand(i: DeallocArg));
638}
639
640static void checkAsyncFuncPointer(const Instruction *I, Value *V) {
641 auto *AsyncFuncPtrAddr =
642 dyn_cast<GlobalVariable>(Val: V->stripPointerCastsAndAliases());
643 if (!AsyncFuncPtrAddr)
644 fail(I, Reason: "llvm.coro.id.async async function pointer not a global", V);
645}
646
647void CoroIdAsyncInst::checkWellFormed() const {
648 checkConstantInt(I: this, V: getArgOperand(i: SizeArg),
649 Reason: "size argument to coro.id.async must be constant");
650 checkConstantInt(I: this, V: getArgOperand(i: AlignArg),
651 Reason: "alignment argument to coro.id.async must be constant");
652 checkConstantInt(I: this, V: getArgOperand(i: StorageArg),
653 Reason: "storage argument offset to coro.id.async must be constant");
654 checkAsyncFuncPointer(I: this, V: getArgOperand(i: AsyncFuncPtrArg));
655}
656
657static void checkAsyncContextProjectFunction(const Instruction *I,
658 Function *F) {
659 auto *FunTy = F->getFunctionType();
660 if (!FunTy->getReturnType()->isPointerTy())
661 fail(I,
662 Reason: "llvm.coro.suspend.async resume function projection function must "
663 "return a ptr type",
664 V: F);
665 if (FunTy->getNumParams() != 1 || !FunTy->getParamType(i: 0)->isPointerTy())
666 fail(I,
667 Reason: "llvm.coro.suspend.async resume function projection function must "
668 "take one ptr type as parameter",
669 V: F);
670}
671
672void CoroSuspendAsyncInst::checkWellFormed() const {
673 checkAsyncContextProjectFunction(I: this, F: getAsyncContextProjectionFunction());
674}
675
676void CoroAsyncEndInst::checkWellFormed() const {
677 auto *MustTailCallFunc = getMustTailCallFunction();
678 if (!MustTailCallFunc)
679 return;
680 auto *FnTy = MustTailCallFunc->getFunctionType();
681 if (FnTy->getNumParams() != (arg_size() - 3))
682 fail(I: this,
683 Reason: "llvm.coro.end.async must tail call function argument type must "
684 "match the tail arguments",
685 V: MustTailCallFunc);
686}
687