-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[CIR] Initial support for emitting coroutine body #161616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -428,6 +428,31 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, | |
return emitUnaryFPBuiltin<cir::ATanOp>(*this, *e); | ||
case Builtin::BI__builtin_elementwise_cos: | ||
return emitUnaryFPBuiltin<cir::CosOp>(*this, *e); | ||
case Builtin::BI__builtin_coro_id: | ||
case Builtin::BI__builtin_coro_promise: | ||
case Builtin::BI__builtin_coro_resume: | ||
case Builtin::BI__builtin_coro_noop: | ||
case Builtin::BI__builtin_coro_destroy: | ||
case Builtin::BI__builtin_coro_done: | ||
case Builtin::BI__builtin_coro_alloc: | ||
case Builtin::BI__builtin_coro_begin: | ||
case Builtin::BI__builtin_coro_end: | ||
case Builtin::BI__builtin_coro_suspend: | ||
case Builtin::BI__builtin_coro_align: | ||
llvm_unreachable("BI__builtin_coro_id like NYI"); | ||
|
||
|
||
case Builtin::BI__builtin_coro_frame: { | ||
cgm.errorNYI(e->getSourceRange(), "BI__builtin_coro_frame NYI"); | ||
assert(!cir::MissingFeatures::coroutineFrame()); | ||
return getUndefRValue(e->getType()); | ||
} | ||
case Builtin::BI__builtin_coro_free: | ||
case Builtin::BI__builtin_coro_size: { | ||
cgm.errorNYI(e->getSourceRange(), | ||
"BI__builtin_coro_free, BI__builtin_coro_size NYI"); | ||
assert(!cir::MissingFeatures::coroSizeBuiltinCall()); | ||
return getUndefRValue(e->getType()); | ||
} | ||
} | ||
|
||
// If this is an alias for a lib function (e.g. __builtin_sin), emit | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,85 @@ | ||||||||||||
//===----- CGCoroutine.cpp - Emit CIR Code for C++ coroutines -------------===// | ||||||||||||
// | ||||||||||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||||
// See https://llvm.org/LICENSE.txt for license information. | ||||||||||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||
// | ||||||||||||
//===----------------------------------------------------------------------===// | ||||||||||||
// | ||||||||||||
// This contains code dealing with C++ code generation of coroutines. | ||||||||||||
// | ||||||||||||
//===----------------------------------------------------------------------===// | ||||||||||||
|
||||||||||||
#include "CIRGenFunction.h" | ||||||||||||
#include "mlir/Support/LLVM.h" | ||||||||||||
#include "clang/AST/StmtCXX.h" | ||||||||||||
#include "clang/Basic/TargetInfo.h" | ||||||||||||
#include "clang/CIR/Dialect/IR/CIRTypes.h" | ||||||||||||
|
||||||||||||
using namespace clang; | ||||||||||||
using namespace clang::CIRGen; | ||||||||||||
|
||||||||||||
struct clang::CIRGen::CGCoroData { | ||||||||||||
// Stores the __builtin_coro_id emitted in the function so that we can supply | ||||||||||||
// it as the first argument to other builtins. | ||||||||||||
cir::CallOp coroId = nullptr; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
// Defining these here allows to keep CGCoroData private to this file. | ||||||||||||
CIRGenFunction::CGCoroInfo::CGCoroInfo() {} | ||||||||||||
CIRGenFunction::CGCoroInfo::~CGCoroInfo() {} | ||||||||||||
|
||||||||||||
static void createCoroData(CIRGenFunction &cgf, | ||||||||||||
CIRGenFunction::CGCoroInfo &curCoro, | ||||||||||||
cir::CallOp coroId) { | ||||||||||||
if (curCoro.data) { | ||||||||||||
llvm_unreachable("EmitCoroutineBodyStatement called twice?"); | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
|
if (curCoro.data) { | |
llvm_unreachable("EmitCoroutineBodyStatement called twice?"); | |
return; | |
} | |
assert(!curCoro.data && "EmitCoroutineBodyStatement called twice?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curCoro.data = std::unique_ptr<CGCoroData>(new CGCoroData); | |
curCoro.data = std::make_unique<CGCoroData>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The else
needs braces since the if
requires them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto fn = dyn_cast<cir::FuncOp>(curFn); | |
assert(fn && "other callables are NYI"); | |
auto fn = mlir::cast<cir::FuncOp>(curFn); |
I don't think they're just NYI. I don't think it's allowed. The other thing this can be is a global variable for which we are generating a ctor region. Using cast
makes the assertion part of the cast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Yes, before I rebased, curFn was a cir::FuncOp, so it was a quick change. Sorry about that.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1632,12 +1632,19 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) { | |
llvm::SMLoc loc = parser.getCurrentLocation(); | ||
mlir::Builder &builder = parser.getBuilder(); | ||
|
||
mlir::StringAttr builtinNameAttr = getBuiltinAttrName(state.name); | ||
mlir::StringAttr coroutineNameAttr = getCoroutineAttrName(state.name); | ||
mlir::StringAttr lambdaNameAttr = getLambdaAttrName(state.name); | ||
mlir::StringAttr noProtoNameAttr = getNoProtoAttrName(state.name); | ||
mlir::StringAttr visNameAttr = getSymVisibilityAttrName(state.name); | ||
mlir::StringAttr visibilityNameAttr = getGlobalVisibilityAttrName(state.name); | ||
mlir::StringAttr dsoLocalNameAttr = getDsoLocalAttrName(state.name); | ||
|
||
if (::mlir::succeeded(parser.parseOptionalKeyword(builtinNameAttr.strref()))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add an IR test to verify the parsing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
state.addAttribute(builtinNameAttr, parser.getBuilder().getUnitAttr()); | ||
if (::mlir::succeeded( | ||
parser.parseOptionalKeyword(coroutineNameAttr.strref()))) | ||
state.addAttribute(coroutineNameAttr, parser.getBuilder().getUnitAttr()); | ||
if (::mlir::succeeded(parser.parseOptionalKeyword(lambdaNameAttr.strref()))) | ||
state.addAttribute(lambdaNameAttr, parser.getBuilder().getUnitAttr()); | ||
if (parser.parseOptionalKeyword(noProtoNameAttr).succeeded()) | ||
|
@@ -1747,6 +1754,12 @@ mlir::Region *cir::FuncOp::getCallableRegion() { | |
} | ||
|
||
void cir::FuncOp::print(OpAsmPrinter &p) { | ||
if (getBuiltin()) | ||
p << " builtin"; | ||
|
||
if (getCoroutine()) | ||
p << " coroutine"; | ||
|
||
if (getLambda()) | ||
p << " lambda"; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done