1 //===----- CGCoroutine.cpp - Emit LLVM Code for C++ coroutines ------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This contains code dealing with C++ code generation of coroutines. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenFunction.h" 15 #include "clang/AST/StmtCXX.h" 16 17 using namespace clang; 18 using namespace CodeGen; 19 20 namespace clang { 21 namespace CodeGen { 22 23 struct CGCoroData { 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 27 // builtin. 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 31 // EmitCoroutineBody. 32 CallExpr const *CoroIdExpr = nullptr; 33 }; 34 } 35 } 36 37 clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {} 38 CodeGenFunction::CGCoroInfo::~CGCoroInfo() {} 39 40 static void createCoroData(CodeGenFunction &CGF, 41 CodeGenFunction::CGCoroInfo &CurCoro, 42 llvm::CallInst *CoroId, 43 CallExpr const *CoroIdExpr = nullptr) { 44 if (CurCoro.Data) { 45 if (CurCoro.Data->CoroIdExpr) 46 CGF.CGM.Error(CoroIdExpr->getLocStart(), 47 "only one __builtin_coro_id can be used in a function"); 48 else if (CoroIdExpr) 49 CGF.CGM.Error(CoroIdExpr->getLocStart(), 50 "__builtin_coro_id shall not be used in a C++ coroutine"); 51 else 52 llvm_unreachable("EmitCoroutineBodyStatement called twice?"); 53 54 return; 55 } 56 57 CurCoro.Data = std::unique_ptr<CGCoroData>(new CGCoroData); 58 CurCoro.Data->CoroId = CoroId; 59 CurCoro.Data->CoroIdExpr = CoroIdExpr; 60 } 61 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(); 66 67 auto *CoroId = Builder.CreateCall( 68 CGM.getIntrinsic(llvm::Intrinsic::coro_id), 69 {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr}); 70 createCoroData(*this, CurCoro, CoroId); 71 72 EmitScalarExpr(S.getAllocate()); 73 // FIXME: Emit the rest of the coroutine. 74 EmitStmt(S.getDeallocate()); 75 } 76 77 // Emit coroutine intrinsic and patch up arguments of the token type. 78 RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E, 79 unsigned int IID) { 80 SmallVector<llvm::Value *, 8> Args; 81 switch (IID) { 82 default: 83 break; 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); 92 break; 93 } 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. 97 } 98 // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first 99 // argument. 100 case llvm::Intrinsic::coro_suspend: 101 Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext())); 102 break; 103 } 104 for (auto &Arg : E->arguments()) 105 Args.push_back(EmitScalarExpr(Arg)); 106 107 llvm::Value *F = CGM.getIntrinsic(IID); 108 llvm::CallInst *Call = Builder.CreateCall(F, Args); 109 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); 114 } 115 return RValue::get(Call); 116 } 117