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
298 auto SwitchId = getSwitchCoroId();
299 SwitchLowering.ResumeSwitch = nullptr;
300 SwitchLowering.PromiseAlloca = SwitchId->getPromise();
301 SwitchLowering.ResumeEntryBlock = nullptr;
302
303 // Move final suspend to the last element in the CoroSuspends vector.
304 if (SwitchLowering.HasFinalSuspend &&
305 FinalSuspendIndex != CoroSuspends.size() - 1)
306 std::swap(a&: CoroSuspends[FinalSuspendIndex], b&: CoroSuspends.back());
307 break;
308 }
309 case Intrinsic::coro_id_async: {
310 ABI = coro::ABI::Async;
311 auto *AsyncId = getAsyncCoroId();
312 AsyncId->checkWellFormed();
313 AsyncLowering.Context = AsyncId->getStorage();
314 AsyncLowering.ContextArgNo = AsyncId->getStorageArgumentIndex();
315 AsyncLowering.ContextHeaderSize = AsyncId->getStorageSize();
316 AsyncLowering.ContextAlignment = AsyncId->getStorageAlignment().value();
317 AsyncLowering.AsyncFuncPointer = AsyncId->getAsyncFunctionPointer();
318 AsyncLowering.AsyncCC = F.getCallingConv();
319 break;
320 }
321 case Intrinsic::coro_id_retcon:
322 case Intrinsic::coro_id_retcon_once: {
323 ABI = IntrID == Intrinsic::coro_id_retcon ? coro::ABI::Retcon
324 : coro::ABI::RetconOnce;
325 auto ContinuationId = getRetconCoroId();
326 ContinuationId->checkWellFormed();
327 auto Prototype = ContinuationId->getPrototype();
328 RetconLowering.ResumePrototype = Prototype;
329 RetconLowering.Alloc = ContinuationId->getAllocFunction();
330 RetconLowering.Dealloc = ContinuationId->getDeallocFunction();
331 RetconLowering.ReturnBlock = nullptr;
332 RetconLowering.IsFrameInlineInStorage = false;
333 break;
334 }
335 default:
336 llvm_unreachable("coro.begin is not dependent on a coro.id call");
337 }
338}
339
340// If for some reason, we were not able to find coro.begin, bailout.
341void coro::Shape::invalidateCoroutine(
342 Function &F, SmallVectorImpl<CoroFrameInst *> &CoroFrames) {
343 assert(!CoroBegin);
344 {
345 // Replace coro.frame which are supposed to be lowered to the result of
346 // coro.begin with poison.
347 auto *Poison = PoisonValue::get(T: PointerType::get(C&: F.getContext(), AddressSpace: 0));
348 for (CoroFrameInst *CF : CoroFrames) {
349 CF->replaceAllUsesWith(V: Poison);
350 CF->eraseFromParent();
351 }
352 CoroFrames.clear();
353
354 // Replace all coro.suspend with poison and remove related coro.saves if
355 // present.
356 for (AnyCoroSuspendInst *CS : CoroSuspends) {
357 CS->replaceAllUsesWith(V: PoisonValue::get(T: CS->getType()));
358 if (auto *CoroSave = CS->getCoroSave())
359 CoroSave->eraseFromParent();
360 CS->eraseFromParent();
361 }
362 CoroSuspends.clear();
363
364 // Replace all coro.ends with unreachable instruction.
365 for (AnyCoroEndInst *CE : CoroEnds)
366 changeToUnreachable(I: CE);
367 }
368}
369
370void coro::SwitchABI::init() {
371 assert(Shape.ABI == coro::ABI::Switch);
372 {
373 for (auto *AnySuspend : Shape.CoroSuspends) {
374 auto Suspend = dyn_cast<CoroSuspendInst>(Val: AnySuspend);
375 if (!Suspend) {
376#ifndef NDEBUG
377 AnySuspend->dump();
378#endif
379 report_fatal_error(reason: "coro.id must be paired with coro.suspend");
380 }
381
382 if (!Suspend->getCoroSave())
383 createCoroSave(CoroBegin: Shape.CoroBegin, SuspendInst: Suspend);
384 }
385 }
386}
387
388void coro::AsyncABI::init() { assert(Shape.ABI == coro::ABI::Async); }
389
390void coro::AnyRetconABI::init() {
391 assert(Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce);
392 {
393 // Determine the result value types, and make sure they match up with
394 // the values passed to the suspends.
395 auto ResultTys = Shape.getRetconResultTypes();
396 auto ResumeTys = Shape.getRetconResumeTypes();
397
398 for (auto *AnySuspend : Shape.CoroSuspends) {
399 auto Suspend = dyn_cast<CoroSuspendRetconInst>(Val: AnySuspend);
400 if (!Suspend) {
401#ifndef NDEBUG
402 AnySuspend->dump();
403#endif
404 report_fatal_error(reason: "coro.id.retcon.* must be paired with "
405 "coro.suspend.retcon");
406 }
407
408 // Check that the argument types of the suspend match the results.
409 auto SI = Suspend->value_begin(), SE = Suspend->value_end();
410 auto RI = ResultTys.begin(), RE = ResultTys.end();
411 for (; SI != SE && RI != RE; ++SI, ++RI) {
412 auto SrcTy = (*SI)->getType();
413 if (SrcTy != *RI) {
414 // The optimizer likes to eliminate bitcasts leading into variadic
415 // calls, but that messes with our invariants. Re-insert the
416 // bitcast and ignore this type mismatch.
417 if (CastInst::isBitCastable(SrcTy, DestTy: *RI)) {
418 auto BCI = new BitCastInst(*SI, *RI, "", Suspend->getIterator());
419 SI->set(BCI);
420 continue;
421 }
422
423#ifndef NDEBUG
424 Suspend->dump();
425 Shape.RetconLowering.ResumePrototype->getFunctionType()->dump();
426#endif
427 report_fatal_error(reason: "argument to coro.suspend.retcon does not "
428 "match corresponding prototype function result");
429 }
430 }
431 if (SI != SE || RI != RE) {
432#ifndef NDEBUG
433 Suspend->dump();
434 Shape.RetconLowering.ResumePrototype->getFunctionType()->dump();
435#endif
436 report_fatal_error(reason: "wrong number of arguments to coro.suspend.retcon");
437 }
438
439 // Check that the result type of the suspend matches the resume types.
440 Type *SResultTy = Suspend->getType();
441 ArrayRef<Type *> SuspendResultTys;
442 if (SResultTy->isVoidTy()) {
443 // leave as empty array
444 } else if (auto SResultStructTy = dyn_cast<StructType>(Val: SResultTy)) {
445 SuspendResultTys = SResultStructTy->elements();
446 } else {
447 // forms an ArrayRef using SResultTy, be careful
448 SuspendResultTys = SResultTy;
449 }
450 if (SuspendResultTys.size() != ResumeTys.size()) {
451#ifndef NDEBUG
452 Suspend->dump();
453 Shape.RetconLowering.ResumePrototype->getFunctionType()->dump();
454#endif
455 report_fatal_error(reason: "wrong number of results from coro.suspend.retcon");
456 }
457 for (size_t I = 0, E = ResumeTys.size(); I != E; ++I) {
458 if (SuspendResultTys[I] != ResumeTys[I]) {
459#ifndef NDEBUG
460 Suspend->dump();
461 Shape.RetconLowering.ResumePrototype->getFunctionType()->dump();
462#endif
463 report_fatal_error(reason: "result from coro.suspend.retcon does not "
464 "match corresponding prototype function param");
465 }
466 }
467 }
468 }
469}
470
471void coro::Shape::cleanCoroutine(
472 SmallVectorImpl<CoroFrameInst *> &CoroFrames,
473 SmallVectorImpl<CoroSaveInst *> &UnusedCoroSaves) {
474 // The coro.frame intrinsic is always lowered to the result of coro.begin.
475 for (CoroFrameInst *CF : CoroFrames) {
476 CF->replaceAllUsesWith(V: CoroBegin);
477 CF->eraseFromParent();
478 }
479 CoroFrames.clear();
480
481 // Remove orphaned coro.saves.
482 for (CoroSaveInst *CoroSave : UnusedCoroSaves)
483 CoroSave->eraseFromParent();
484 UnusedCoroSaves.clear();
485}
486
487static void propagateCallAttrsFromCallee(CallInst *Call, Function *Callee) {
488 Call->setCallingConv(Callee->getCallingConv());
489 // TODO: attributes?
490}
491
492static void addCallToCallGraph(CallGraph *CG, CallInst *Call, Function *Callee){
493 if (CG)
494 (*CG)[Call->getFunction()]->addCalledFunction(Call, M: (*CG)[Callee]);
495}
496
497Value *coro::Shape::emitAlloc(IRBuilder<> &Builder, Value *Size,
498 CallGraph *CG) const {
499 switch (ABI) {
500 case coro::ABI::Switch:
501 llvm_unreachable("can't allocate memory in coro switch-lowering");
502
503 case coro::ABI::Retcon:
504 case coro::ABI::RetconOnce: {
505 auto Alloc = RetconLowering.Alloc;
506 Size = Builder.CreateIntCast(V: Size,
507 DestTy: Alloc->getFunctionType()->getParamType(i: 0),
508 /*is signed*/ isSigned: false);
509 auto *Call = Builder.CreateCall(Callee: Alloc, Args: Size);
510 propagateCallAttrsFromCallee(Call, Callee: Alloc);
511 addCallToCallGraph(CG, Call, Callee: Alloc);
512 return Call;
513 }
514 case coro::ABI::Async:
515 llvm_unreachable("can't allocate memory in coro async-lowering");
516 }
517 llvm_unreachable("Unknown coro::ABI enum");
518}
519
520void coro::Shape::emitDealloc(IRBuilder<> &Builder, Value *Ptr,
521 CallGraph *CG) const {
522 switch (ABI) {
523 case coro::ABI::Switch:
524 llvm_unreachable("can't allocate memory in coro switch-lowering");
525
526 case coro::ABI::Retcon:
527 case coro::ABI::RetconOnce: {
528 auto Dealloc = RetconLowering.Dealloc;
529 Ptr = Builder.CreateBitCast(V: Ptr,
530 DestTy: Dealloc->getFunctionType()->getParamType(i: 0));
531 auto *Call = Builder.CreateCall(Callee: Dealloc, Args: Ptr);
532 propagateCallAttrsFromCallee(Call, Callee: Dealloc);
533 addCallToCallGraph(CG, Call, Callee: Dealloc);
534 return;
535 }
536 case coro::ABI::Async:
537 llvm_unreachable("can't allocate memory in coro async-lowering");
538 }
539 llvm_unreachable("Unknown coro::ABI enum");
540}
541
542[[noreturn]] static void fail(const Instruction *I, const char *Reason,
543 Value *V) {
544#ifndef NDEBUG
545 I->dump();
546 if (V) {
547 errs() << " Value: ";
548 V->printAsOperand(llvm::errs());
549 errs() << '\n';
550 }
551#endif
552 report_fatal_error(reason: Reason);
553}
554
555/// Check that the given value is a well-formed prototype for the
556/// llvm.coro.id.retcon.* intrinsics.
557static void checkWFRetconPrototype(const AnyCoroIdRetconInst *I, Value *V) {
558 auto F = dyn_cast<Function>(Val: V->stripPointerCastsAndAliases());
559 if (!F)
560 fail(I, Reason: "llvm.coro.id.retcon.* prototype not a Function", V);
561
562 auto FT = F->getFunctionType();
563
564 if (isa<CoroIdRetconInst>(Val: I)) {
565 bool ResultOkay;
566 if (FT->getReturnType()->isPointerTy()) {
567 ResultOkay = true;
568 } else if (auto SRetTy = dyn_cast<StructType>(Val: FT->getReturnType())) {
569 ResultOkay = (!SRetTy->isOpaque() &&
570 SRetTy->getNumElements() > 0 &&
571 SRetTy->getElementType(N: 0)->isPointerTy());
572 } else {
573 ResultOkay = false;
574 }
575 if (!ResultOkay)
576 fail(I, Reason: "llvm.coro.id.retcon prototype must return pointer as first "
577 "result", V: F);
578
579 if (FT->getReturnType() !=
580 I->getFunction()->getFunctionType()->getReturnType())
581 fail(I, Reason: "llvm.coro.id.retcon prototype return type must be same as"
582 "current function return type", V: F);
583 } else {
584 // No meaningful validation to do here for llvm.coro.id.unique.once.
585 }
586
587 if (FT->getNumParams() == 0 || !FT->getParamType(i: 0)->isPointerTy())
588 fail(I, Reason: "llvm.coro.id.retcon.* prototype must take pointer as "
589 "its first parameter", V: F);
590}
591
592/// Check that the given value is a well-formed allocator.
593static void checkWFAlloc(const Instruction *I, Value *V) {
594 auto F = dyn_cast<Function>(Val: V->stripPointerCastsAndAliases());
595 if (!F)
596 fail(I, Reason: "llvm.coro.* allocator not a Function", V);
597
598 auto FT = F->getFunctionType();
599 if (!FT->getReturnType()->isPointerTy())
600 fail(I, Reason: "llvm.coro.* allocator must return a pointer", V: F);
601
602 if (FT->getNumParams() != 1 ||
603 !FT->getParamType(i: 0)->isIntegerTy())
604 fail(I, Reason: "llvm.coro.* allocator must take integer as only param", V: F);
605}
606
607/// Check that the given value is a well-formed deallocator.
608static void checkWFDealloc(const Instruction *I, Value *V) {
609 auto F = dyn_cast<Function>(Val: V->stripPointerCastsAndAliases());
610 if (!F)
611 fail(I, Reason: "llvm.coro.* deallocator not a Function", V);
612
613 auto FT = F->getFunctionType();
614 if (!FT->getReturnType()->isVoidTy())
615 fail(I, Reason: "llvm.coro.* deallocator must return void", V: F);
616
617 if (FT->getNumParams() != 1 ||
618 !FT->getParamType(i: 0)->isPointerTy())
619 fail(I, Reason: "llvm.coro.* deallocator must take pointer as only param", V: F);
620}
621
622static void checkConstantInt(const Instruction *I, Value *V,
623 const char *Reason) {
624 if (!isa<ConstantInt>(Val: V)) {
625 fail(I, Reason, V);
626 }
627}
628
629void AnyCoroIdRetconInst::checkWellFormed() const {
630 checkConstantInt(I: this, V: getArgOperand(i: SizeArg),
631 Reason: "size argument to coro.id.retcon.* must be constant");
632 checkConstantInt(I: this, V: getArgOperand(i: AlignArg),
633 Reason: "alignment argument to coro.id.retcon.* must be constant");
634 checkWFRetconPrototype(I: this, V: getArgOperand(i: PrototypeArg));
635 checkWFAlloc(I: this, V: getArgOperand(i: AllocArg));
636 checkWFDealloc(I: this, V: getArgOperand(i: DeallocArg));
637}
638
639static void checkAsyncFuncPointer(const Instruction *I, Value *V) {
640 auto *AsyncFuncPtrAddr =
641 dyn_cast<GlobalVariable>(Val: V->stripPointerCastsAndAliases());
642 if (!AsyncFuncPtrAddr)
643 fail(I, Reason: "llvm.coro.id.async async function pointer not a global", V);
644}
645
646void CoroIdAsyncInst::checkWellFormed() const {
647 checkConstantInt(I: this, V: getArgOperand(i: SizeArg),
648 Reason: "size argument to coro.id.async must be constant");
649 checkConstantInt(I: this, V: getArgOperand(i: AlignArg),
650 Reason: "alignment argument to coro.id.async must be constant");
651 checkConstantInt(I: this, V: getArgOperand(i: StorageArg),
652 Reason: "storage argument offset to coro.id.async must be constant");
653 checkAsyncFuncPointer(I: this, V: getArgOperand(i: AsyncFuncPtrArg));
654}
655
656static void checkAsyncContextProjectFunction(const Instruction *I,
657 Function *F) {
658 auto *FunTy = F->getFunctionType();
659 if (!FunTy->getReturnType()->isPointerTy())
660 fail(I,
661 Reason: "llvm.coro.suspend.async resume function projection function must "
662 "return a ptr type",
663 V: F);
664 if (FunTy->getNumParams() != 1 || !FunTy->getParamType(i: 0)->isPointerTy())
665 fail(I,
666 Reason: "llvm.coro.suspend.async resume function projection function must "
667 "take one ptr type as parameter",
668 V: F);
669}
670
671void CoroSuspendAsyncInst::checkWellFormed() const {
672 checkAsyncContextProjectFunction(I: this, F: getAsyncContextProjectionFunction());
673}
674
675void CoroAsyncEndInst::checkWellFormed() const {
676 auto *MustTailCallFunc = getMustTailCallFunction();
677 if (!MustTailCallFunc)
678 return;
679 auto *FnTy = MustTailCallFunc->getFunctionType();
680 if (FnTy->getNumParams() != (arg_size() - 3))
681 fail(I: this,
682 Reason: "llvm.coro.end.async must tail call function argument type must "
683 "match the tail arguments",
684 V: MustTailCallFunc);
685}
686