1 //===----- CGCoroutine.cpp - Emit LLVM Code for C++ coroutines ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with C++ code generation of coroutines.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenFunction.h"
15 #include "clang/AST/StmtCXX.h"
17 using namespace clang;
18 using namespace CodeGen;
24 // Stores the llvm.coro.id emitted in the function so that we can supply it
25 // as the first argument to coro.begin, coro.alloc and coro.free intrinsics.
26 // Note: llvm.coro.id returns a token that cannot be directly expressed in a
28 llvm::CallInst *CoroId = nullptr;
29 // If coro.id came from the builtin, remember the expression to give better
30 // diagnostic. If CoroIdExpr is nullptr, the coro.id was created by
32 CallExpr const *CoroIdExpr = nullptr;
37 clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {}
38 CodeGenFunction::CGCoroInfo::~CGCoroInfo() {}
40 static void createCoroData(CodeGenFunction &CGF,
41 CodeGenFunction::CGCoroInfo &CurCoro,
42 llvm::CallInst *CoroId,
43 CallExpr const *CoroIdExpr = nullptr) {
45 if (CurCoro.Data->CoroIdExpr)
46 CGF.CGM.Error(CoroIdExpr->getLocStart(),
47 "only one __builtin_coro_id can be used in a function");
49 CGF.CGM.Error(CoroIdExpr->getLocStart(),
50 "__builtin_coro_id shall not be used in a C++ coroutine");
52 llvm_unreachable("EmitCoroutineBodyStatement called twice?");
57 CurCoro.Data = std::unique_ptr<CGCoroData>(new CGCoroData);
58 CurCoro.Data->CoroId = CoroId;
59 CurCoro.Data->CoroIdExpr = CoroIdExpr;
62 void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) {
63 auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
64 auto &TI = CGM.getContext().getTargetInfo();
65 unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth();
67 auto *CoroId = Builder.CreateCall(
68 CGM.getIntrinsic(llvm::Intrinsic::coro_id),
69 {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
70 createCoroData(*this, CurCoro, CoroId);
72 EmitScalarExpr(S.getAllocate());
73 // FIXME: Emit the rest of the coroutine.
74 EmitStmt(S.getDeallocate());
77 // Emit coroutine intrinsic and patch up arguments of the token type.
78 RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E,
80 SmallVector<llvm::Value *, 8> Args;
84 // The following three intrinsics take a token parameter referring to a token
85 // returned by earlier call to @llvm.coro.id. Since we cannot represent it in
86 // builtins, we patch it up here.
87 case llvm::Intrinsic::coro_alloc:
88 case llvm::Intrinsic::coro_begin:
89 case llvm::Intrinsic::coro_free: {
90 if (CurCoro.Data && CurCoro.Data->CoroId) {
91 Args.push_back(CurCoro.Data->CoroId);
94 CGM.Error(E->getLocStart(), "this builtin expect that __builtin_coro_id has"
95 " been used earlier in this function");
96 // Fallthrough to the next case to add TokenNone as the first argument.
98 // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first
100 case llvm::Intrinsic::coro_suspend:
101 Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext()));
104 for (auto &Arg : E->arguments())
105 Args.push_back(EmitScalarExpr(Arg));
107 llvm::Value *F = CGM.getIntrinsic(IID);
108 llvm::CallInst *Call = Builder.CreateCall(F, Args);
110 // If we see @llvm.coro.id remember it in the CoroData. We will update
111 // coro.alloc, coro.begin and coro.free intrinsics to refer to it.
112 if (IID == llvm::Intrinsic::coro_id) {
113 createCoroData(*this, CurCoro, Call, E);
115 return RValue::get(Call);