LLVM 23.0.0git
Coroutines.cpp
Go to the documentation of this file.
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"
15#include "llvm/ADT/StringRef.h"
17#include "llvm/IR/Attributes.h"
18#include "llvm/IR/Constants.h"
20#include "llvm/IR/Function.h"
24#include "llvm/IR/Intrinsics.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/Type.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.
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
53 Instruction *InsertPt) {
54 auto *IndexVal = ConstantInt::get(Type::getInt8Ty(Context), Index);
55 auto *Fn =
56 Intrinsic::getOrInsertDeclaration(&TheModule, Intrinsic::coro_subfn_addr);
57
60 "makeSubFnCall: Index value out of range");
61 return CallInst::Create(Fn, {Arg, IndexVal}, "", 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
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
102
106
107// Checks whether the module declares any of the listed intrinsics.
109#ifndef NDEBUG
110 for (Intrinsic::ID ID : List)
112 "Only non-overloaded intrinsics supported");
113#endif
114
115 for (Intrinsic::ID ID : List)
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
126 for (User *U : FramePtr->users()) {
127 if (auto *CF = dyn_cast<CoroFreeInst>(U))
128 CoroFrees.push_back(CF);
129 else if (auto *CD = dyn_cast<CoroDeadInst>(U))
130 CoroDeads.push_back(CD);
131 }
132
133 Value *Replacement =
135 for (CoroFreeInst *CF : CoroFrees) {
136 CF->replaceAllUsesWith(Replacement);
137 CF->eraseFromParent();
138 }
139
140 for (auto *CD : CoroDeads)
141 CD->eraseFromParent();
142}
143
146 for (User *U : CoroId->users())
147 if (auto *CA = dyn_cast<CoroAllocInst>(U))
148 CoroAllocs.push_back(CA);
149
150 if (CoroAllocs.empty())
151 return;
152
153 coro::suppressCoroAllocs(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)
163 ArrayRef<CoroAllocInst *> CoroAllocs) {
164 auto *False = ConstantInt::getFalse(Context);
165 for (auto *CA : CoroAllocs) {
166 CA->replaceAllUsesWith(False);
167 CA->eraseFromParent();
168 }
169}
170
172 CoroSuspendInst *SuspendInst) {
173 Module *M = SuspendInst->getModule();
174 auto *Fn = Intrinsic::getOrInsertDeclaration(M, Intrinsic::coro_save);
175 auto *SaveInst = cast<CoroSaveInst>(
176 CallInst::Create(Fn, CoroBegin, "", SuspendInst->getIterator()));
177 assert(!SuspendInst->getCoroSave());
178 SuspendInst->setArgOperand(0, SaveInst);
179 return SaveInst;
180}
181
182// Collect "interesting" coroutine intrinsics.
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>(&I)) {
196 CoroAwaitSuspends.push_back(AWS);
197 } else if (auto II = dyn_cast<IntrinsicInst>(&I)) {
198 switch (II->getIntrinsicID()) {
199 default:
200 continue;
201 case Intrinsic::coro_size:
202 CoroSizes.push_back(cast<CoroSizeInst>(II));
203 break;
204 case Intrinsic::coro_align:
206 break;
207 case Intrinsic::coro_frame:
208 CoroFrames.push_back(cast<CoroFrameInst>(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(cast<CoroSaveInst>(II));
215 break;
216 case Intrinsic::coro_suspend_async: {
217 auto *Suspend = cast<CoroSuspendAsyncInst>(II);
218 Suspend->checkWellFormed();
219 CoroSuspends.push_back(Suspend);
220 break;
221 }
222 case Intrinsic::coro_suspend_retcon: {
223 auto Suspend = cast<CoroSuspendRetconInst>(II);
224 CoroSuspends.push_back(Suspend);
225 break;
226 }
227 case Intrinsic::coro_suspend: {
228 auto Suspend = cast<CoroSuspendInst>(II);
229 CoroSuspends.push_back(Suspend);
230 if (Suspend->isFinal()) {
231 if (HasFinalSuspend)
233 "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>(II);
242
243 // Ignore coro id's that aren't pre-split.
244 auto Id = dyn_cast<CoroIdInst>(CB->getId());
245 if (Id && !Id->getInfo().isPreSplit())
246 break;
247
248 if (CoroBegin)
250 "coroutine should have exactly one defining @llvm.coro.begin");
251 CB->addRetAttr(Attribute::NonNull);
252 CB->addRetAttr(Attribute::NoAlias);
253 CB->removeFnAttr(Attribute::NoDuplicate);
254 CoroBegin = CB;
255 break;
256 }
257 case Intrinsic::coro_end_async:
258 case Intrinsic::coro_end:
259 CoroEnds.push_back(cast<AnyCoroEndInst>(II));
260 if (auto *AsyncEnd = dyn_cast<CoroAsyncEndInst>(II)) {
261 AsyncEnd->checkWellFormed();
262 }
263
264 if (CoroEnds.back()->isUnwind())
265 HasUnwindCoroEnd = true;
266
267 if (CoroEnds.back()->isFallthrough() && isa<CoroEndInst>(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())
274 "Only one coro.end can be marked as fallthrough");
275 std::swap(CoroEnds.front(), CoroEnds.back());
276 }
277 }
278 break;
279 case Intrinsic::coro_is_in_ramp:
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: {
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(CoroSuspends[FinalSuspendIndex], CoroSuspends.back());
307 break;
308 }
309 case Intrinsic::coro_id_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
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.
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(PointerType::get(F.getContext(), 0));
348 for (CoroFrameInst *CF : CoroFrames) {
349 CF->replaceAllUsesWith(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(PoisonValue::get(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)
367 }
368}
369
372 {
373 for (auto *AnySuspend : Shape.CoroSuspends) {
374 auto Suspend = dyn_cast<CoroSuspendInst>(AnySuspend);
375 if (!Suspend) {
376#ifndef NDEBUG
377 AnySuspend->dump();
378#endif
379 report_fatal_error("coro.id must be paired with coro.suspend");
380 }
381
382 if (!Suspend->getCoroSave())
383 createCoroSave(Shape.CoroBegin, Suspend);
384 }
385 }
386}
387
389
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>(AnySuspend);
400 if (!Suspend) {
401#ifndef NDEBUG
402 AnySuspend->dump();
403#endif
404 report_fatal_error("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, *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("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("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>(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("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("result from coro.suspend.retcon does not "
464 "match corresponding prototype function param");
465 }
466 }
467 }
468 }
469}
470
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(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
488 Call->setCallingConv(Callee->getCallingConv());
489 // TODO: attributes?
490}
491
493 if (CG)
494 (*CG)[Call->getFunction()]->addCalledFunction(Call, (*CG)[Callee]);
495}
496
498 CallGraph *CG) const {
499 switch (ABI) {
501 llvm_unreachable("can't allocate memory in coro switch-lowering");
502
505 auto Alloc = RetconLowering.Alloc;
506 Size = Builder.CreateIntCast(Size,
507 Alloc->getFunctionType()->getParamType(0),
508 /*is signed*/ false);
509 auto *Call = Builder.CreateCall(Alloc, Size);
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
521 CallGraph *CG) const {
522 switch (ABI) {
524 llvm_unreachable("can't allocate memory in coro switch-lowering");
525
528 auto Dealloc = RetconLowering.Dealloc;
529 Ptr = Builder.CreateBitCast(Ptr,
530 Dealloc->getFunctionType()->getParamType(0));
531 auto *Call = Builder.CreateCall(Dealloc, Ptr);
533 addCallToCallGraph(CG, Call, 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);
553}
554
555/// Check that the given value is a well-formed prototype for the
556/// llvm.coro.id.retcon.* intrinsics.
558 auto F = dyn_cast<Function>(V->stripPointerCasts());
559 if (!F)
560 fail(I, "llvm.coro.id.retcon.* prototype not a Function", V);
561
562 auto FT = F->getFunctionType();
563
565 bool ResultOkay;
566 if (FT->getReturnType()->isPointerTy()) {
567 ResultOkay = true;
568 } else if (auto SRetTy = dyn_cast<StructType>(FT->getReturnType())) {
569 ResultOkay = (!SRetTy->isOpaque() &&
570 SRetTy->getNumElements() > 0 &&
571 SRetTy->getElementType(0)->isPointerTy());
572 } else {
573 ResultOkay = false;
574 }
575 if (!ResultOkay)
576 fail(I, "llvm.coro.id.retcon prototype must return pointer as first "
577 "result", F);
578
579 if (FT->getReturnType() !=
580 I->getFunction()->getFunctionType()->getReturnType())
581 fail(I, "llvm.coro.id.retcon prototype return type must be same as"
582 "current function return type", 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(0)->isPointerTy())
588 fail(I, "llvm.coro.id.retcon.* prototype must take pointer as "
589 "its first parameter", 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>(V->stripPointerCasts());
595 if (!F)
596 fail(I, "llvm.coro.* allocator not a Function", V);
597
598 auto FT = F->getFunctionType();
599 if (!FT->getReturnType()->isPointerTy())
600 fail(I, "llvm.coro.* allocator must return a pointer", F);
601
602 if (FT->getNumParams() != 1 ||
603 !FT->getParamType(0)->isIntegerTy())
604 fail(I, "llvm.coro.* allocator must take integer as only param", 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>(V->stripPointerCasts());
610 if (!F)
611 fail(I, "llvm.coro.* deallocator not a Function", V);
612
613 auto FT = F->getFunctionType();
614 if (!FT->getReturnType()->isVoidTy())
615 fail(I, "llvm.coro.* deallocator must return void", F);
616
617 if (FT->getNumParams() != 1 ||
618 !FT->getParamType(0)->isPointerTy())
619 fail(I, "llvm.coro.* deallocator must take pointer as only param", F);
620}
621
622static void checkConstantInt(const Instruction *I, Value *V,
623 const char *Reason) {
624 if (!isa<ConstantInt>(V)) {
625 fail(I, Reason, V);
626 }
627}
628
630 checkConstantInt(this, getArgOperand(SizeArg),
631 "size argument to coro.id.retcon.* must be constant");
632 checkConstantInt(this, getArgOperand(AlignArg),
633 "alignment argument to coro.id.retcon.* must be constant");
634 checkWFRetconPrototype(this, getArgOperand(PrototypeArg));
635 checkWFAlloc(this, getArgOperand(AllocArg));
636 checkWFDealloc(this, getArgOperand(DeallocArg));
637}
638
639static void checkAsyncFuncPointer(const Instruction *I, Value *V) {
640 auto *AsyncFuncPtrAddr = dyn_cast<GlobalVariable>(V->stripPointerCasts());
641 if (!AsyncFuncPtrAddr)
642 fail(I, "llvm.coro.id.async async function pointer not a global", V);
643}
644
646 checkConstantInt(this, getArgOperand(SizeArg),
647 "size argument to coro.id.async must be constant");
648 checkConstantInt(this, getArgOperand(AlignArg),
649 "alignment argument to coro.id.async must be constant");
650 checkConstantInt(this, getArgOperand(StorageArg),
651 "storage argument offset to coro.id.async must be constant");
652 checkAsyncFuncPointer(this, getArgOperand(AsyncFuncPtrArg));
653}
654
656 Function *F) {
657 auto *FunTy = F->getFunctionType();
658 if (!FunTy->getReturnType()->isPointerTy())
659 fail(I,
660 "llvm.coro.suspend.async resume function projection function must "
661 "return a ptr type",
662 F);
663 if (FunTy->getNumParams() != 1 || !FunTy->getParamType(0)->isPointerTy())
664 fail(I,
665 "llvm.coro.suspend.async resume function projection function must "
666 "take one ptr type as parameter",
667 F);
668}
669
673
675 auto *MustTailCallFunc = getMustTailCallFunction();
676 if (!MustTailCallFunc)
677 return;
678 auto *FnTy = MustTailCallFunc->getFunctionType();
679 if (FnTy->getNumParams() != (arg_size() - 3))
680 fail(this,
681 "llvm.coro.end.async must tail call function argument type must "
682 "match the tail arguments",
683 MustTailCallFunc);
684}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
This file contains the simple types necessary to represent the attributes associated with functions a...
static void fail(const SDLoc &DL, SelectionDAG &DAG, const Twine &Msg, SDValue Val={})
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Intrinsic::ID NonOverloadedCoroIntrinsics[]
static void checkWFDealloc(const Instruction *I, Value *V)
Check that the given value is a well-formed deallocator.
static void checkConstantInt(const Instruction *I, Value *V, const char *Reason)
static void checkWFRetconPrototype(const AnyCoroIdRetconInst *I, Value *V)
Check that the given value is a well-formed prototype for the llvm.coro.id.retcon.
static void propagateCallAttrsFromCallee(CallInst *Call, Function *Callee)
static void checkAsyncContextProjectFunction(const Instruction *I, Function *F)
static CoroSaveInst * createCoroSave(CoroBeginInst *CoroBegin, CoroSuspendInst *SuspendInst)
static void checkWFAlloc(const Instruction *I, Value *V)
Check that the given value is a well-formed allocator.
static void addCallToCallGraph(CallGraph *CG, CallInst *Call, Function *Callee)
static void checkAsyncFuncPointer(const Instruction *I, Value *V)
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
This file defines the SmallVector class.
static const unsigned FramePtr
This represents either the llvm.coro.id.retcon or llvm.coro.id.retcon.once instruction.
Definition CoroInstr.h:237
LLVM_ABI void checkWellFormed() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Instruction & front() const
Definition BasicBlock.h:484
This class represents a no-op cast from one type to another.
Value * getArgOperand(unsigned i) const
void setArgOperand(unsigned i, Value *v)
unsigned arg_size() const
The basic data container for the call graph of a Module of IR.
Definition CallGraph.h:72
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI bool isBitCastable(Type *SrcTy, Type *DestTy)
Check whether a bitcast between these types is valid.
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
A constant pointer value that points to null.
Definition Constants.h:701
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Function * getMustTailCallFunction() const
Definition CoroInstr.h:754
LLVM_ABI void checkWellFormed() const
This class represents the llvm.coro.begin or llvm.coro.begin.custom.abi instructions.
Definition CoroInstr.h:475
This represents the llvm.coro.frame instruction.
Definition CoroInstr.h:420
This represents the llvm.coro.free instruction.
Definition CoroInstr.h:444
LLVM_ABI void checkWellFormed() const
This represents the llvm.coro.id instruction.
Definition CoroInstr.h:148
This represents the llvm.coro.save instruction.
Definition CoroInstr.h:504
Function * getAsyncContextProjectionFunction() const
Definition CoroInstr.h:605
LLVM_ABI void checkWellFormed() const
This represents the llvm.coro.suspend instruction.
Definition CoroInstr.h:557
CoroSaveInst * getCoroSave() const
Definition CoroInstr.h:561
Class to represent function types.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2835
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Class to represent pointers.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:311
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
LLVM Value Representation.
Definition Value.h:75
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
iterator_range< user_iterator > users()
Definition Value.h:426
void init() override
coro::Shape & Shape
Definition ABI.h:60
void init() override
self_iterator getIterator()
Definition ilist_node.h:123
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
LLVM_ABI bool isOverloaded(ID id)
Returns true if the intrinsic can be overloaded.
@ Async
The "async continuation" lowering, where each suspend point creates a single continuation function.
Definition CoroShape.h:48
@ RetconOnce
The "unique returned-continuation" lowering, where each suspend point creates a single continuation f...
Definition CoroShape.h:43
@ Retcon
The "returned-continuation" lowering, where each suspend point creates a single continuation function...
Definition CoroShape.h:36
@ Switch
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
Definition CoroShape.h:31
bool declaresAnyIntrinsic(const Module &M)
bool isSuspendBlock(BasicBlock *BB)
void suppressCoroAllocs(CoroIdInst *CoroId)
Replaces all @llvm.coro.alloc intrinsics calls associated with a given call @llvm....
void elideCoroFree(Value *FramePtr)
bool declaresIntrinsics(const Module &M, ArrayRef< Intrinsic::ID > List)
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI unsigned changeToUnreachable(Instruction *I, 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...
Definition Local.cpp:2528
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
PointerType *const Int8Ptr
ConstantPointerNull *const NullPtr
CallInst * makeSubFnCall(Value *Arg, int Index, Instruction *InsertPt)
FunctionType *const ResumeFnType
SmallVector< CoroAwaitSuspendInst *, 4 > CoroAwaitSuspends
Definition CoroShape.h:60
AsyncLoweringStorage AsyncLowering
Definition CoroShape.h:141
AnyCoroIdRetconInst * getRetconCoroId() const
Definition CoroShape.h:149
CoroIdInst * getSwitchCoroId() const
Definition CoroShape.h:144
SmallVector< CoroSizeInst *, 2 > CoroSizes
Definition CoroShape.h:57
coro::ABI ABI
Definition CoroShape.h:97
SmallVector< AnyCoroSuspendInst *, 4 > CoroSuspends
Definition CoroShape.h:59
LLVM_ABI Value * emitAlloc(IRBuilder<> &Builder, Value *Size, CallGraph *CG) const
Allocate memory according to the rules of the active lowering.
LLVM_ABI void cleanCoroutine(SmallVectorImpl< CoroFrameInst * > &CoroFrames, SmallVectorImpl< CoroSaveInst * > &UnusedCoroSaves)
SwitchLoweringStorage SwitchLowering
Definition CoroShape.h:139
CoroBeginInst * CoroBegin
Definition CoroShape.h:54
SmallVector< CoroIsInRampInst *, 2 > CoroIsInRampInsts
Definition CoroShape.h:56
LLVM_ABI void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const
Deallocate memory according to the rules of the active lowering.
RetconLoweringStorage RetconLowering
Definition CoroShape.h:140
SmallVector< CoroAlignInst *, 2 > CoroAligns
Definition CoroShape.h:58
CoroIdAsyncInst * getAsyncCoroId() const
Definition CoroShape.h:154
SmallVector< AnyCoroEndInst *, 4 > CoroEnds
Definition CoroShape.h:55
LLVM_ABI void invalidateCoroutine(Function &F, SmallVectorImpl< CoroFrameInst * > &CoroFrames)
LLVM_ABI void analyze(Function &F, SmallVectorImpl< CoroFrameInst * > &CoroFrames, SmallVectorImpl< CoroSaveInst * > &UnusedCoroSaves)