blob: 2fdb1279ece97b9348734fe12a21a2a5d3ffe983 [file] [log] [blame]
//===----- CGCoroutine.cpp - Emit LLVM Code for C++ coroutines ------------===//
// The LLVM Compiler Infrastructure
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
// This contains code dealing with C++ code generation of coroutines.
#include "CodeGenFunction.h"
#include "clang/AST/StmtCXX.h"
using namespace clang;
using namespace CodeGen;
namespace clang {
namespace CodeGen {
struct CGCoroData {
// Stores the emitted in the function so that we can supply it
// as the first argument to coro.begin, coro.alloc and intrinsics.
// Note: returns a token that cannot be directly expressed in a
// builtin.
llvm::CallInst *CoroId = nullptr;
// If came from the builtin, remember the expression to give better
// diagnostic. If CoroIdExpr is nullptr, the was created by
// EmitCoroutineBody.
CallExpr const *CoroIdExpr = nullptr;
clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {}
CodeGenFunction::CGCoroInfo::~CGCoroInfo() {}
static void createCoroData(CodeGenFunction &CGF,
CodeGenFunction::CGCoroInfo &CurCoro,
llvm::CallInst *CoroId,
CallExpr const *CoroIdExpr = nullptr) {
if (CurCoro.Data) {
if (CurCoro.Data->CoroIdExpr)
"only one __builtin_coro_id can be used in a function");
else if (CoroIdExpr)
"__builtin_coro_id shall not be used in a C++ coroutine");
llvm_unreachable("EmitCoroutineBodyStatement called twice?");
CurCoro.Data = std::unique_ptr<CGCoroData>(new CGCoroData);
CurCoro.Data->CoroId = CoroId;
CurCoro.Data->CoroIdExpr = CoroIdExpr;
void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) {
auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
auto &TI = CGM.getContext().getTargetInfo();
unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth();
auto *CoroId = Builder.CreateCall(
{Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
createCoroData(*this, CurCoro, CoroId);
// FIXME: Emit the rest of the coroutine.
// Emit coroutine intrinsic and patch up arguments of the token type.
RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E,
unsigned int IID) {
SmallVector<llvm::Value *, 8> Args;
switch (IID) {
// The following three intrinsics take a token parameter referring to a token
// returned by earlier call to Since we cannot represent it in
// builtins, we patch it up here.
case llvm::Intrinsic::coro_alloc:
case llvm::Intrinsic::coro_begin:
case llvm::Intrinsic::coro_free: {
if (CurCoro.Data && CurCoro.Data->CoroId) {
CGM.Error(E->getLocStart(), "this builtin expect that __builtin_coro_id has"
" been used earlier in this function");
// Fallthrough to the next case to add TokenNone as the first argument.
// @llvm.coro.suspend takes a token parameter. Add token 'none' as the first
// argument.
case llvm::Intrinsic::coro_suspend:
for (auto &Arg : E->arguments())
llvm::Value *F = CGM.getIntrinsic(IID);
llvm::CallInst *Call = Builder.CreateCall(F, Args);
// If we see remember it in the CoroData. We will update
// coro.alloc, coro.begin and intrinsics to refer to it.
if (IID == llvm::Intrinsic::coro_id) {
createCoroData(*this, CurCoro, Call, E);
return RValue::get(Call);