|
| 1 | +//===----- CGCoroutine.cpp - Emit CIR Code for C++ coroutines -------------===// |
| 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 contains code dealing with C++ code generation of coroutines. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "CIRGenFunction.h" |
| 14 | +#include "mlir/Support/LLVM.h" |
| 15 | +#include "clang/AST/StmtCXX.h" |
| 16 | +#include "clang/Basic/TargetInfo.h" |
| 17 | +#include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 18 | + |
| 19 | +using namespace clang; |
| 20 | +using namespace clang::CIRGen; |
| 21 | + |
| 22 | +struct clang::CIRGen::CGCoroData { |
| 23 | + // Stores the __builtin_coro_id emitted in the function so that we can supply |
| 24 | + // it as the first argument to other builtins. |
| 25 | + cir::CallOp coroId = nullptr; |
| 26 | +}; |
| 27 | + |
| 28 | +// Defining these here allows to keep CGCoroData private to this file. |
| 29 | +CIRGenFunction::CGCoroInfo::CGCoroInfo() {} |
| 30 | +CIRGenFunction::CGCoroInfo::~CGCoroInfo() {} |
| 31 | + |
| 32 | +static void createCoroData(CIRGenFunction &cgf, |
| 33 | + CIRGenFunction::CGCoroInfo &curCoro, |
| 34 | + cir::CallOp coroId) { |
| 35 | + assert(!curCoro.data && "EmitCoroutineBodyStatement called twice?"); |
| 36 | + |
| 37 | + curCoro.data = std::make_unique<CGCoroData>(); |
| 38 | + curCoro.data->coroId = coroId; |
| 39 | +} |
| 40 | + |
| 41 | +cir::CallOp CIRGenFunction::emitCoroIDBuiltinCall(mlir::Location loc, |
| 42 | + mlir::Value nullPtr) { |
| 43 | + cir::IntType int32Ty = builder.getUInt32Ty(); |
| 44 | + |
| 45 | + const TargetInfo &ti = cgm.getASTContext().getTargetInfo(); |
| 46 | + unsigned newAlign = ti.getNewAlign() / ti.getCharWidth(); |
| 47 | + |
| 48 | + mlir::Operation *builtin = cgm.getGlobalValue(cgm.builtinCoroId); |
| 49 | + |
| 50 | + cir::FuncOp fnOp; |
| 51 | + if (!builtin) { |
| 52 | + fnOp = cgm.createCIRBuiltinFunction( |
| 53 | + loc, cgm.builtinCoroId, |
| 54 | + cir::FuncType::get({int32Ty, VoidPtrTy, VoidPtrTy, VoidPtrTy}, int32Ty), |
| 55 | + /*FD=*/nullptr); |
| 56 | + assert(fnOp && "should always succeed"); |
| 57 | + } else { |
| 58 | + fnOp = cast<cir::FuncOp>(builtin); |
| 59 | + } |
| 60 | + |
| 61 | + return builder.createCallOp(loc, fnOp, |
| 62 | + mlir::ValueRange{builder.getUInt32(newAlign, loc), |
| 63 | + nullPtr, nullPtr, nullPtr}); |
| 64 | +} |
| 65 | + |
| 66 | +mlir::LogicalResult |
| 67 | +CIRGenFunction::emitCoroutineBody(const CoroutineBodyStmt &s) { |
| 68 | + mlir::Location openCurlyLoc = getLoc(s.getBeginLoc()); |
| 69 | + cir::ConstantOp nullPtrCst = builder.getNullPtr(VoidPtrTy, openCurlyLoc); |
| 70 | + |
| 71 | + auto fn = mlir::cast<cir::FuncOp>(curFn); |
| 72 | + fn.setCoroutine(true); |
| 73 | + cir::CallOp coroId = emitCoroIDBuiltinCall(openCurlyLoc, nullPtrCst); |
| 74 | + createCoroData(*this, curCoro, coroId); |
| 75 | + |
| 76 | + assert(!cir::MissingFeatures::coroAllocBuiltinCall()); |
| 77 | + |
| 78 | + assert(!cir::MissingFeatures::coroBeginBuiltinCall()); |
| 79 | + |
| 80 | + assert(!cir::MissingFeatures::generateDebugInfo()); |
| 81 | + return mlir::success(); |
| 82 | +} |
0 commit comments