Index: docs/LanguageExtensions.rst =================================================================== --- docs/LanguageExtensions.rst +++ docs/LanguageExtensions.rst @@ -1865,6 +1865,82 @@ Note that the compiler does not guarantee that non-temporal loads or stores will be used. +C++ Coroutines support builtins +-------------------------------- + +.. warning:: + This is a work in progress. Compatibility across Clang/LLVM releases is not + guaranteed. + +Clang provides experimental builtins to support C++ Coroutines as defined by +http://wg21.link/P0057. The following four are intended to be used by the +standard library to implement `std::experimental::coroutine_handle` type. + +**Syntax**: + +.. code-block:: c + + void __builtin_coro_resume(void *addr); + void __builtin_coro_destroy(void *addr); + bool __builtin_coro_done(void *addr); + void __builtin_coro_promise(void *addr, int alignment, bool from_promise) + +**Example of use**: + +.. code-block:: c++ + + template <> struct coroutine_handle { + void resume() const { __builtin_coro_resume(ptr); } + void destroy() const { __builtin_coro_destroy(ptr); } + bool done() const { return __builtin_coro_done(ptr); } + // ... + protected: + void *ptr; + }; + + template struct coroutine_handle : coroutine_handle<> { + // ... + Promise &promise() const { + return *reinterpret_cast( + __builtin_coro_promise(ptr, alignof(Promise), /*from-promise=*/false)); + } + static coroutine_handle from_promise(Promise &promise) { + coroutine_handle p; + p.ptr = __builtin_coro_promise(&promise, alignof(Promise), + /*from-promise=*/true); + return p; + } + }; + + +Other coroutine builtins are either for the internal clang use or for the use +during development of the coroutine feature. See `Coroutines in LLVM +`_ for +more information on their semantics. Note that builtins matching the intrinsics +that take token as the first parameter (llvm.coro.begin, llvm.coro.alloc, +llvm.coro.free and llvm.coro.suspend) omit the token parameter and fill it to +an appropriate value during the emission. + +**Syntax**: + +.. code-block:: c + + size_t __builtin_coro_size() + void *__builtin_coro_frame() + void *__builtin_coro_free(void* coro_frame) + + void *__builtin_coro_id(int align, void* promise, void *fnaddr, void *parts) + bool __builtin_coro_alloc() + void* __builtin_coro_begin(void* memory) + void __builtin_coro_end(void* coro_frame, bool unwind) + char __builtin_coro_suspend(bool final) + bool __builtin_coro_param(void* original, void* copy) + +Note that there is no builtin matching the `llvm.coro.save` intrinsic. LLVM +automatically will insert one if the first argument to `llvm.coro.suspend` is +token `none`. If a user calls `__builin_suspend`, clang will insert `token none` +as the first argument to the intrinsic. + Non-standard C++11 Attributes ============================= Index: include/clang/Basic/Builtins.def =================================================================== --- include/clang/Basic/Builtins.def +++ include/clang/Basic/Builtins.def @@ -1330,6 +1330,22 @@ BUILTIN(__builtin_nontemporal_store, "v.", "t") BUILTIN(__builtin_nontemporal_load, "v.", "t") +// Coroutine intrinsics. +BUILTIN(__builtin_coro_resume, "vv*", "") +BUILTIN(__builtin_coro_destroy, "vv*", "") +BUILTIN(__builtin_coro_done, "bv*", "n") +BUILTIN(__builtin_coro_promise, "v*v*IiIb", "n") + +BUILTIN(__builtin_coro_size, "z", "n") +BUILTIN(__builtin_coro_frame, "v*", "n") +BUILTIN(__builtin_coro_free, "v*v*", "n") + +BUILTIN(__builtin_coro_id, "v*Iiv*v*v*", "n") +BUILTIN(__builtin_coro_alloc, "b", "n") +BUILTIN(__builtin_coro_begin, "v*v*", "n") +BUILTIN(__builtin_coro_end, "vv*Ib", "n") +BUILTIN(__builtin_coro_suspend, "cIb", "n") +BUILTIN(__builtin_coro_param, "bv*v*", "n") // OpenCL v2.0 s6.13.16, s9.17.3.5 - Pipe functions. // We need the generic prototype, since the packet type could be anything. LANGBUILTIN(read_pipe, "i.", "tn", OCLC20_LANG) Index: lib/CodeGen/BackendUtil.cpp =================================================================== --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -41,6 +41,7 @@ #include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Target/TargetSubtargetInfo.h" +#include "llvm/Transforms/Coroutines.h" #include "llvm/Transforms/IPO.h" #include "llvm/Transforms/IPO/AlwaysInliner.h" #include "llvm/Transforms/IPO/PassManagerBuilder.h" @@ -401,6 +402,9 @@ addDataFlowSanitizerPass); } + if (LangOpts.Coroutines) + addCoroutinePassesToExtensionPoints(PMBuilder); + if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) { PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, addEfficiencySanitizerPass); Index: lib/CodeGen/CGBuiltin.cpp =================================================================== --- lib/CodeGen/CGBuiltin.cpp +++ lib/CodeGen/CGBuiltin.cpp @@ -2135,6 +2135,39 @@ break; } + case Builtin::BI__builtin_coro_size: { + auto & Context = getContext(); + auto SizeTy = Context.getSizeType(); + auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy)); + Value *F = CGM.getIntrinsic(Intrinsic::coro_size, T); + return RValue::get(Builder.CreateCall(F)); + } + + case Builtin::BI__builtin_coro_id: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_id); + case Builtin::BI__builtin_coro_promise: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_promise); + case Builtin::BI__builtin_coro_resume: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_resume); + case Builtin::BI__builtin_coro_frame: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_frame); + case Builtin::BI__builtin_coro_free: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_free); + case Builtin::BI__builtin_coro_destroy: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_destroy); + case Builtin::BI__builtin_coro_done: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_done); + case Builtin::BI__builtin_coro_alloc: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_alloc); + case Builtin::BI__builtin_coro_begin: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_begin); + case Builtin::BI__builtin_coro_end: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_end); + case Builtin::BI__builtin_coro_suspend: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_suspend); + case Builtin::BI__builtin_coro_param: + return EmitCoroutineIntrinsic(E, Intrinsic::coro_param); + // OpenCL v2.0 s6.13.16.2, Built-in pipe read and write functions case Builtin::BIread_pipe: case Builtin::BIwrite_pipe: { Index: lib/CodeGen/CGCoroutine.cpp =================================================================== --- /dev/null +++ lib/CodeGen/CGCoroutine.cpp @@ -0,0 +1,94 @@ +//===----- 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 "llvm/ADT/SmallVector.h" +#include "llvm/IR/Intrinsics.h" +#include "llvm/IR/Value.h" + +using namespace clang; +using namespace CodeGen; + +namespace clang { +namespace CodeGen { + +struct CGCoroData { + // More members to come in the future patches + + // Stores the llvm.coro.id emitted in the function so that we can supply it + // as the first argument to coro.begin, coro.alloc and coro.free intrinsics. + // Note: llvm.coro.id returns a token that cannot be directly expressed in a + // builtin. + llvm::CallInst *CoroId = nullptr; +}; +} +} +clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {} +CodeGenFunction::CGCoroInfo::~CGCoroInfo() {} + +static void createCoroDataIfNeeded(CodeGenFunction::CGCoroInfo& CurCoro) { + if (CurCoro.Data) + return; + + CurCoro.Data = std::unique_ptr(new CGCoroData); +} + +// Emit coroutine intrinsic and patch up arguments of the token type. +RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E, + unsigned int IID) { + SmallVector Args; + switch (IID) { + default: + break; + // The following three intrinsics take a token parameter referring to a token + // returned by earlier call to @llvm.coro.id. 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) { + Args.push_back(CurCoro.Data->CoroId); + break; + } + 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: + Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext())); + break; + } + for (auto &Arg : E->arguments()) + Args.push_back(EmitScalarExpr(Arg)); + + llvm::Value *F = CGM.getIntrinsic(IID); + llvm::CallInst *Call = Builder.CreateCall(F, Args); + + // If we see @llvm.coro.id remember it. We will update coro.alloc, coro.begin + // and coro.free intrinsics to refer to it. + if (IID == llvm::Intrinsic::coro_id) { + createCoroDataIfNeeded(CurCoro); + + if (CurCoro.Data->CoroId) { + CGM.Error(E->getLocStart(), + "only one __builtin_coro_id can be used in a function"); + } + else { + CurCoro.Data->CoroId = Call; + } + } + + return RValue::get(Call); +} Index: lib/CodeGen/CMakeLists.txt =================================================================== --- lib/CodeGen/CMakeLists.txt +++ lib/CodeGen/CMakeLists.txt @@ -3,6 +3,7 @@ BitReader BitWriter Core + Coroutines Coverage IPO IRReader @@ -42,6 +43,7 @@ CGCall.cpp CGClass.cpp CGCleanup.cpp + CGCoroutine.cpp CGDebugInfo.cpp CGDecl.cpp CGDeclCXX.cpp Index: lib/CodeGen/CodeGenFunction.h =================================================================== --- lib/CodeGen/CodeGenFunction.h +++ lib/CodeGen/CodeGenFunction.h @@ -88,6 +88,7 @@ class RegionCodeGenTy; class TargetCodeGenInfo; struct OMPTaskDataTy; +struct CGCoroData; /// The kind of evaluation to perform on values of a particular /// type. Basically, is the code in CGExprScalar, CGExprComplex, or @@ -155,6 +156,16 @@ QualType FnRetTy; llvm::Function *CurFn; + // Holds coroutine data if the current function is a coroutine. We use a + // wrapper to manage its lifetime, so that we don't have to define CGCoroData + // in this header. + struct CGCoroInfo { + std::unique_ptr Data; + CGCoroInfo(); + ~CGCoroInfo(); + }; + CGCoroInfo CurCoro; + /// CurGD - The GlobalDecl for the current function being compiled. GlobalDecl CurGD; @@ -2290,6 +2301,8 @@ void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S); void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S); + RValue EmitCoroutineIntrinsic(const CallExpr *E, unsigned int IID); + void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); Index: test/CodeGenCoroutines/Inputs/coro.h =================================================================== --- /dev/null +++ test/CodeGenCoroutines/Inputs/coro.h @@ -0,0 +1,37 @@ +void free(void *ptr); +void *malloc(unsigned int); + +#define CORO_SUSPEND_IMPL(IsFinal) \ + switch (__builtin_coro_suspend(IsFinal)) { \ + case 0: \ + if (IsFinal) \ + __builtin_trap(); \ + break; \ + case 1: \ + goto coro_Cleanup; \ + default: \ + goto coro_Suspend; \ + } + +#define CORO_SUSPEND() CORO_SUSPEND_IMPL(0) +#define CORO_FINAL_SUSPEND() CORO_SUSPEND_IMPL(1) + +#define CORO_BEGIN(AllocFunc) \ + void *coro_hdl = \ + (__builtin_coro_id(0, 0, 0, 0), \ + __builtin_coro_begin((__builtin_coro_alloc() \ + ? AllocFunc(__builtin_coro_size()) \ + : (void *)0))); + +#define CORO_END(FreeFunc) \ + coro_Cleanup : { \ + void *coro_mem = __builtin_coro_free(coro_hdl); \ + if (coro_mem) \ + FreeFunc(coro_mem); \ + } \ + coro_Suspend: \ + __builtin_coro_end(0, 0); \ + return coro_hdl; + +#define CORO_RESUME(hdl) __builtin_coro_resume(hdl) +#define CORO_DESTROY(hdl) __builtin_coro_destroy(hdl) Index: test/CodeGenCoroutines/O2-coro.c =================================================================== --- /dev/null +++ test/CodeGenCoroutines/O2-coro.c @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fcoroutines -emit-llvm %s -o - -O3 | FileCheck %s +#include "Inputs/coro.h" +void print(int); + +void* f() { + CORO_BEGIN(malloc); + + for (int i = 0;; ++i) { + print(i); + CORO_SUSPEND(); + } + + CORO_END(free); +} + +// CHECK-LABEL: @main +int main() { + void* coro = f(); + CORO_RESUME(coro); + CORO_RESUME(coro); + CORO_DESTROY(coro); +// CHECK: call void @print(i32 0) +// CHECK: call void @print(i32 1) +// CHECK: call void @print(i32 2) +} Index: test/CodeGenCoroutines/coro-builtins-err.c =================================================================== --- /dev/null +++ test/CodeGenCoroutines/coro-builtins-err.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc18.0.0 -fcoroutines -emit-llvm %s -o - -verify + +void f() { + __builtin_coro_alloc(); // expected-error {{this builtin expect that __builtin_coro_id}} + __builtin_coro_begin(0); // expected-error {{this builtin expect that __builtin_coro_id}} + __builtin_coro_free(0); // expected-error {{this builtin expect that __builtin_coro_id}} + + __builtin_coro_id(32, 0, 0, 0); + __builtin_coro_id(32, 0, 0, 0); // expected-error {{only one __builtin_coro_id}} +} Index: test/CodeGenCoroutines/coro-builtins.c =================================================================== --- /dev/null +++ test/CodeGenCoroutines/coro-builtins.c @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc18.0.0 -fcoroutines -emit-llvm %s -o - -disable-llvm-passes | FileCheck %s + +// CHECK-LABEL: f( +void f() { + // CHECK: %0 = call token @llvm.coro.id(i32 32, i8* null, i8* null, i8* null) + __builtin_coro_id(32, 0, 0, 0); + // CHECK-NEXT: %1 = call i1 @llvm.coro.alloc(token %0) + __builtin_coro_alloc(); + // CHECK-NEXT: %2 = call i64 @llvm.coro.size.i64() + __builtin_coro_size(); + // CHECK-NEXT: %3 = call i8* @llvm.coro.begin(token %0, i8* null) + __builtin_coro_begin(0); + // CHECK-NEXT: %4 = call i8* @llvm.coro.frame() + __builtin_coro_frame(); + // CHECK-NEXT: call void @llvm.coro.resume(i8* null) + __builtin_coro_resume(0); + // CHECK-NEXT: call void @llvm.coro.destroy(i8* null) + __builtin_coro_destroy(0); + // CHECK-NEXT: %5 = call i1 @llvm.coro.done(i8* null) + __builtin_coro_done(0); + // CHECK-NEXT: %6 = call i8* @llvm.coro.promise(i8* null, i32 32, i1 false) + __builtin_coro_promise(0, 32, 0); + // CHECK-NEXT: %7 = call i8* @llvm.coro.free(token %0, i8* null) + __builtin_coro_free(0); + // CHECK-NEXT: call void @llvm.coro.end(i8* null, i1 false) + __builtin_coro_end(0, 0); + // CHECK-NEXT: %8 = call i8 @llvm.coro.suspend(token none, i1 false) + __builtin_coro_suspend(0); + // CHECK-NEXT: %9 = call i1 @llvm.coro.param(i8* null, i8* null) + __builtin_coro_param(0, 0); +}