clang 22.0.0git
CIRGenCoroutine.cpp
Go to the documentation of this file.
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"
18
19using namespace clang;
20using namespace clang::CIRGen;
21
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.
31
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
41cir::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
66mlir::LogicalResult
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
77
79
81 return mlir::success();
82}
static void createCoroData(CIRGenFunction &cgf, CIRGenFunction::CGCoroInfo &curCoro, cir::CallOp coroId)
__device__ __2f16 float __ockl_bool s
cir::CallOp emitCoroIDBuiltinCall(mlir::Location loc, mlir::Value nullPtr)
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
mlir::Operation * curFn
The current function or global initializer that is generated code for.
mlir::LogicalResult emitCoroutineBody(const CoroutineBodyStmt &s)
Represents the body of a coroutine.
Definition StmtCXX.h:320
Exposes information about the current target.
Definition TargetInfo.h:226
unsigned getNewAlign() const
Return the largest alignment for which a suitably-sized allocation with 'operator new(size_t)' is gua...
Definition TargetInfo.h:761
unsigned getCharWidth() const
Definition TargetInfo.h:517
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool coroAllocBuiltinCall()
static bool coroBeginBuiltinCall()
static bool generateDebugInfo()
std::unique_ptr< CGCoroData > data
cir::PointerType VoidPtrTy
void* in address space 0