Skip to content

Commit 3806693

Browse files
[Clang] Introduce [[clang::coro_inplace_task]]
1 parent 58004e5 commit 3806693

25 files changed

+496
-134
lines changed

clang/include/clang/AST/ExprCXX.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5082,18 +5082,19 @@ class CoroutineSuspendExpr : public Expr {
50825082
enum SubExpr { Operand, Common, Ready, Suspend, Resume, Count };
50835083

50845084
Stmt *SubExprs[SubExpr::Count];
5085-
OpaqueValueExpr *OpaqueValue = nullptr;
5085+
OpaqueValueExpr *CommonExprOpaqueValue = nullptr;
5086+
OpaqueValueExpr *InplaceCallOpaqueValue = nullptr;
50865087

50875088
public:
50885089
// These types correspond to the three C++ 'await_suspend' return variants
50895090
enum class SuspendReturnType { SuspendVoid, SuspendBool, SuspendHandle };
50905091

50915092
CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, Expr *Operand,
50925093
Expr *Common, Expr *Ready, Expr *Suspend, Expr *Resume,
5093-
OpaqueValueExpr *OpaqueValue)
5094+
OpaqueValueExpr *CommonExprOpaqueValue)
50945095
: Expr(SC, Resume->getType(), Resume->getValueKind(),
50955096
Resume->getObjectKind()),
5096-
KeywordLoc(KeywordLoc), OpaqueValue(OpaqueValue) {
5097+
KeywordLoc(KeywordLoc), CommonExprOpaqueValue(CommonExprOpaqueValue) {
50975098
SubExprs[SubExpr::Operand] = Operand;
50985099
SubExprs[SubExpr::Common] = Common;
50995100
SubExprs[SubExpr::Ready] = Ready;
@@ -5128,7 +5129,16 @@ class CoroutineSuspendExpr : public Expr {
51285129
}
51295130

51305131
/// getOpaqueValue - Return the opaque value placeholder.
5131-
OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; }
5132+
OpaqueValueExpr *getCommonExprOpaqueValue() const {
5133+
return CommonExprOpaqueValue;
5134+
}
5135+
5136+
OpaqueValueExpr *getInplaceCallOpaqueValue() const {
5137+
return InplaceCallOpaqueValue;
5138+
}
5139+
void setInplaceCallOpaqueValue(OpaqueValueExpr *E) {
5140+
InplaceCallOpaqueValue = E;
5141+
}
51325142

51335143
Expr *getReadyExpr() const {
51345144
return static_cast<Expr*>(SubExprs[SubExpr::Ready]);
@@ -5194,9 +5204,9 @@ class CoawaitExpr : public CoroutineSuspendExpr {
51945204
public:
51955205
CoawaitExpr(SourceLocation CoawaitLoc, Expr *Operand, Expr *Common,
51965206
Expr *Ready, Expr *Suspend, Expr *Resume,
5197-
OpaqueValueExpr *OpaqueValue, bool IsImplicit = false)
5207+
OpaqueValueExpr *CommonExprOpaqueValue, bool IsImplicit = false)
51985208
: CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Operand, Common,
5199-
Ready, Suspend, Resume, OpaqueValue) {
5209+
Ready, Suspend, Resume, CommonExprOpaqueValue) {
52005210
CoawaitBits.IsImplicit = IsImplicit;
52015211
}
52025212

@@ -5275,9 +5285,9 @@ class CoyieldExpr : public CoroutineSuspendExpr {
52755285
public:
52765286
CoyieldExpr(SourceLocation CoyieldLoc, Expr *Operand, Expr *Common,
52775287
Expr *Ready, Expr *Suspend, Expr *Resume,
5278-
OpaqueValueExpr *OpaqueValue)
5288+
OpaqueValueExpr *CommonExprOpaqueValue)
52795289
: CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Operand, Common,
5280-
Ready, Suspend, Resume, OpaqueValue) {}
5290+
Ready, Suspend, Resume, CommonExprOpaqueValue) {}
52815291
CoyieldExpr(SourceLocation CoyieldLoc, QualType Ty, Expr *Operand,
52825292
Expr *Common)
52835293
: CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Ty, Operand,

clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,14 @@ def CoroDisableLifetimeBound : InheritableAttr {
12171217
let SimpleHandler = 1;
12181218
}
12191219

1220+
def CoroInplaceTask : InheritableAttr {
1221+
let Spellings = [Clang<"coro_inplace_task">];
1222+
let Subjects = SubjectList<[CXXRecord]>;
1223+
let LangOpts = [CPlusPlus];
1224+
let Documentation = [CoroInplaceTaskDoc];
1225+
let SimpleHandler = 1;
1226+
}
1227+
12201228
// OSObject-based attributes.
12211229
def OSConsumed : InheritableParamAttr {
12221230
let Spellings = [Clang<"os_consumed">];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8013,6 +8013,25 @@ but do not pass them to the underlying coroutine or pass them by value.
80138013
}];
80148014
}
80158015

8016+
def CoroInplaceTaskDoc : Documentation {
8017+
let Category = DocCatDecl;
8018+
let Content = [{
8019+
The ``[[clang::coro_inplace_task]]`` is a class attribute which can be applied
8020+
to a coroutine return type.
8021+
8022+
When a coroutine function that returns such a type calls another coroutine function,
8023+
the compiler performs heap allocation elision when the following conditions are all met:
8024+
- callee coroutine function returns a type that is annotated with ``[[clang::coro_inplace_task]]``.
8025+
- The callee coroutine function is inlined.
8026+
- In caller coroutine, the return value of the callee is a prvalue or an xvalue, and
8027+
- The temporary expression containing the callee coroutine object is immediately co_awaited.
8028+
8029+
The behavior is undefined if any of the following condition was met:
8030+
- the caller coroutine is destroyed earlier than the callee coroutine.
8031+
8032+
}];
8033+
}
8034+
80168035
def CountedByDocs : Documentation {
80178036
let Category = DocCatField;
80188037
let Content = [{

clang/lib/CodeGen/CGBlocks.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,8 @@ llvm::Type *CodeGenModule::getGenericBlockLiteralType() {
11541154
}
11551155

11561156
RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
1157-
ReturnValueSlot ReturnValue) {
1157+
ReturnValueSlot ReturnValue,
1158+
llvm::CallBase **CallOrInvoke) {
11581159
const auto *BPT = E->getCallee()->getType()->castAs<BlockPointerType>();
11591160
llvm::Value *BlockPtr = EmitScalarExpr(E->getCallee());
11601161
llvm::Type *GenBlockTy = CGM.getGenericBlockLiteralType();
@@ -1211,7 +1212,7 @@ RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
12111212
CGCallee Callee(CGCalleeInfo(), Func);
12121213

12131214
// And call the block.
1214-
return EmitCall(FnInfo, Callee, ReturnValue, Args);
1215+
return EmitCall(FnInfo, Callee, ReturnValue, Args, CallOrInvoke);
12151216
}
12161217

12171218
Address CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable) {

clang/lib/CodeGen/CGCUDARuntime.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ CGCUDARuntime::~CGCUDARuntime() {}
2525

2626
RValue CGCUDARuntime::EmitCUDAKernelCallExpr(CodeGenFunction &CGF,
2727
const CUDAKernelCallExpr *E,
28-
ReturnValueSlot ReturnValue) {
28+
ReturnValueSlot ReturnValue,
29+
llvm::CallBase **CallOrInvoke) {
2930
llvm::BasicBlock *ConfigOKBlock = CGF.createBasicBlock("kcall.configok");
3031
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("kcall.end");
3132

@@ -35,7 +36,7 @@ RValue CGCUDARuntime::EmitCUDAKernelCallExpr(CodeGenFunction &CGF,
3536

3637
eval.begin(CGF);
3738
CGF.EmitBlock(ConfigOKBlock);
38-
CGF.EmitSimpleCallExpr(E, ReturnValue);
39+
CGF.EmitSimpleCallExpr(E, ReturnValue, CallOrInvoke);
3940
CGF.EmitBranch(ContBlock);
4041

4142
CGF.EmitBlock(ContBlock);

clang/lib/CodeGen/CGCUDARuntime.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/IR/GlobalValue.h"
2222

2323
namespace llvm {
24+
class CallBase;
2425
class Function;
2526
class GlobalVariable;
2627
}
@@ -82,9 +83,10 @@ class CGCUDARuntime {
8283
CGCUDARuntime(CodeGenModule &CGM) : CGM(CGM) {}
8384
virtual ~CGCUDARuntime();
8485

85-
virtual RValue EmitCUDAKernelCallExpr(CodeGenFunction &CGF,
86-
const CUDAKernelCallExpr *E,
87-
ReturnValueSlot ReturnValue);
86+
virtual RValue
87+
EmitCUDAKernelCallExpr(CodeGenFunction &CGF, const CUDAKernelCallExpr *E,
88+
ReturnValueSlot ReturnValue,
89+
llvm::CallBase **CallOrInvoke = nullptr);
8890

8991
/// Emits a kernel launch stub.
9092
virtual void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) = 0;

clang/lib/CodeGen/CGCXXABI.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,11 @@ class CGCXXABI {
485485
llvm::PointerUnion<const CXXDeleteExpr *, const CXXMemberCallExpr *>;
486486

487487
/// Emit the ABI-specific virtual destructor call.
488-
virtual llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
489-
const CXXDestructorDecl *Dtor,
490-
CXXDtorType DtorType,
491-
Address This,
492-
DeleteOrMemberCallExpr E) = 0;
488+
virtual llvm::Value *
489+
EmitVirtualDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor,
490+
CXXDtorType DtorType, Address This,
491+
DeleteOrMemberCallExpr E,
492+
llvm::CallBase **CallOrInvoke) = 0;
493493

494494
virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
495495
GlobalDecl GD,

clang/lib/CodeGen/CGClass.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,15 +2191,11 @@ static bool canEmitDelegateCallArgs(CodeGenFunction &CGF,
21912191
return true;
21922192
}
21932193

2194-
void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
2195-
CXXCtorType Type,
2196-
bool ForVirtualBase,
2197-
bool Delegating,
2198-
Address This,
2199-
CallArgList &Args,
2200-
AggValueSlot::Overlap_t Overlap,
2201-
SourceLocation Loc,
2202-
bool NewPointerIsChecked) {
2194+
void CodeGenFunction::EmitCXXConstructorCall(
2195+
const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase,
2196+
bool Delegating, Address This, CallArgList &Args,
2197+
AggValueSlot::Overlap_t Overlap, SourceLocation Loc,
2198+
bool NewPointerIsChecked, llvm::CallBase **CallOrInvoke) {
22032199
const CXXRecordDecl *ClassDecl = D->getParent();
22042200

22052201
if (!NewPointerIsChecked)
@@ -2247,7 +2243,7 @@ void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
22472243
const CGFunctionInfo &Info = CGM.getTypes().arrangeCXXConstructorCall(
22482244
Args, D, Type, ExtraArgs.Prefix, ExtraArgs.Suffix, PassPrototypeArgs);
22492245
CGCallee Callee = CGCallee::forDirect(CalleePtr, GlobalDecl(D, Type));
2250-
EmitCall(Info, Callee, ReturnValueSlot(), Args, nullptr, false, Loc);
2246+
EmitCall(Info, Callee, ReturnValueSlot(), Args, CallOrInvoke, false, Loc);
22512247

22522248
// Generate vtable assumptions if we're constructing a complete object
22532249
// with a vtable. We don't do this for base subobjects for two reasons:

clang/lib/CodeGen/CGCoroutine.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
#include "CGCleanup.h"
1414
#include "CodeGenFunction.h"
15-
#include "llvm/ADT/ScopeExit.h"
15+
#include "clang/AST/ExprCXX.h"
1616
#include "clang/AST/StmtCXX.h"
1717
#include "clang/AST/StmtVisitor.h"
18+
#include "llvm/ADT/ScopeExit.h"
19+
#include "llvm/IR/Intrinsics.h"
1820

1921
using namespace clang;
2022
using namespace CodeGen;
@@ -223,12 +225,22 @@ static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Co
223225
CoroutineSuspendExpr const &S,
224226
AwaitKind Kind, AggValueSlot aggSlot,
225227
bool ignoreResult, bool forLValue) {
226-
auto *E = S.getCommonExpr();
228+
auto &Builder = CGF.Builder;
227229

228-
auto CommonBinder =
229-
CodeGenFunction::OpaqueValueMappingData::bind(CGF, S.getOpaqueValue(), E);
230-
auto UnbindCommonOnExit =
231-
llvm::make_scope_exit([&] { CommonBinder.unbind(CGF); });
230+
// If S.getInplaceCallOpaqueValue() is null, we don't have a nested opaque
231+
// value for common expression.
232+
std::optional<CodeGenFunction::OpaqueValueMapping> OperandMapping;
233+
if (auto *CallOV = S.getInplaceCallOpaqueValue()) {
234+
auto *CE = cast<CallExpr>(CallOV->getSourceExpr());
235+
// TODO: don't use the intrisic coro_safe_elide in the next version.
236+
LValue CallResult = CGF.EmitCallExprLValue(CE, nullptr);
237+
OperandMapping.emplace(CGF, CallOV, CallResult);
238+
llvm::Value *Value = CallResult.getPointer(CGF);
239+
auto SafeElide = CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_safe_elide);
240+
Builder.CreateCall(SafeElide, Value);
241+
}
242+
CodeGenFunction::OpaqueValueMapping BindCommon(CGF,
243+
S.getCommonExprOpaqueValue());
232244

233245
auto Prefix = buildSuspendPrefixStr(Coro, Kind);
234246
BasicBlock *ReadyBlock = CGF.createBasicBlock(Prefix + Twine(".ready"));
@@ -241,7 +253,6 @@ static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Co
241253
// Otherwise, emit suspend logic.
242254
CGF.EmitBlock(SuspendBlock);
243255

244-
auto &Builder = CGF.Builder;
245256
llvm::Function *CoroSave = CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_save);
246257
auto *NullPtr = llvm::ConstantPointerNull::get(CGF.CGM.Int8PtrTy);
247258
auto *SaveCall = Builder.CreateCall(CoroSave, {NullPtr});
@@ -256,7 +267,8 @@ static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Co
256267

257268
SmallVector<llvm::Value *, 3> SuspendIntrinsicCallArgs;
258269
SuspendIntrinsicCallArgs.push_back(
259-
CGF.getOrCreateOpaqueLValueMapping(S.getOpaqueValue()).getPointer(CGF));
270+
CGF.getOrCreateOpaqueLValueMapping(S.getCommonExprOpaqueValue())
271+
.getPointer(CGF));
260272

261273
SuspendIntrinsicCallArgs.push_back(CGF.CurCoro.Data->CoroBegin);
262274
SuspendIntrinsicCallArgs.push_back(SuspendWrapper);
@@ -455,7 +467,7 @@ CodeGenFunction::generateAwaitSuspendWrapper(Twine const &CoroName,
455467
Builder.CreateLoad(GetAddrOfLocalVar(&FrameDecl));
456468

457469
auto AwaiterBinder = CodeGenFunction::OpaqueValueMappingData::bind(
458-
*this, S.getOpaqueValue(), AwaiterLValue);
470+
*this, S.getCommonExprOpaqueValue(), AwaiterLValue);
459471

460472
auto *SuspendRet = EmitScalarExpr(S.getSuspendExpr());
461473

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5442,24 +5442,25 @@ RValue CodeGenFunction::EmitRValueForField(LValue LV,
54425442
//===--------------------------------------------------------------------===//
54435443

54445444
RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
5445-
ReturnValueSlot ReturnValue) {
5445+
ReturnValueSlot ReturnValue,
5446+
llvm::CallBase **CallOrInvoke) {
54465447
// Builtins never have block type.
54475448
if (E->getCallee()->getType()->isBlockPointerType())
5448-
return EmitBlockCallExpr(E, ReturnValue);
5449+
return EmitBlockCallExpr(E, ReturnValue, CallOrInvoke);
54495450

54505451
if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
5451-
return EmitCXXMemberCallExpr(CE, ReturnValue);
5452+
return EmitCXXMemberCallExpr(CE, ReturnValue, CallOrInvoke);
54525453

54535454
if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E))
5454-
return EmitCUDAKernelCallExpr(CE, ReturnValue);
5455+
return EmitCUDAKernelCallExpr(CE, ReturnValue, CallOrInvoke);
54555456

54565457
// A CXXOperatorCallExpr is created even for explicit object methods, but
54575458
// these should be treated like static function call.
54585459
if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
54595460
if (const auto *MD =
54605461
dyn_cast_if_present<CXXMethodDecl>(CE->getCalleeDecl());
54615462
MD && MD->isImplicitObjectMemberFunction())
5462-
return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
5463+
return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue, CallOrInvoke);
54635464

54645465
CGCallee callee = EmitCallee(E->getCallee());
54655466

@@ -5472,14 +5473,17 @@ RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
54725473
return EmitCXXPseudoDestructorExpr(callee.getPseudoDestructorExpr());
54735474
}
54745475

5475-
return EmitCall(E->getCallee()->getType(), callee, E, ReturnValue);
5476+
return EmitCall(E->getCallee()->getType(), callee, E, ReturnValue,
5477+
/*Chain=*/nullptr, CallOrInvoke);
54765478
}
54775479

54785480
/// Emit a CallExpr without considering whether it might be a subclass.
54795481
RValue CodeGenFunction::EmitSimpleCallExpr(const CallExpr *E,
5480-
ReturnValueSlot ReturnValue) {
5482+
ReturnValueSlot ReturnValue,
5483+
llvm::CallBase **CallOrInvoke) {
54815484
CGCallee Callee = EmitCallee(E->getCallee());
5482-
return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue);
5485+
return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue,
5486+
/*Chain=*/nullptr, CallOrInvoke);
54835487
}
54845488

54855489
// Detect the unusual situation where an inline version is shadowed by a
@@ -5683,8 +5687,9 @@ LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
56835687
llvm_unreachable("bad evaluation kind");
56845688
}
56855689

5686-
LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
5687-
RValue RV = EmitCallExpr(E);
5690+
LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E,
5691+
llvm::CallBase **CallOrInvoke) {
5692+
RValue RV = EmitCallExpr(E, ReturnValueSlot(), CallOrInvoke);
56885693

56895694
if (!RV.isScalar())
56905695
return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
@@ -5807,9 +5812,11 @@ LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
58075812
AlignmentSource::Decl);
58085813
}
58095814

5810-
RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee,
5811-
const CallExpr *E, ReturnValueSlot ReturnValue,
5812-
llvm::Value *Chain) {
5815+
RValue CodeGenFunction::EmitCall(QualType CalleeType,
5816+
const CGCallee &OrigCallee, const CallExpr *E,
5817+
ReturnValueSlot ReturnValue,
5818+
llvm::Value *Chain,
5819+
llvm::CallBase **CallOrInvoke) {
58135820
// Get the actual function type. The callee type will always be a pointer to
58145821
// function type or a block pointer type.
58155822
assert(CalleeType->isFunctionPointerType() &&
@@ -6020,8 +6027,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee
60206027
Address(Handle, Handle->getType(), CGM.getPointerAlign()));
60216028
Callee.setFunctionPointer(Stub);
60226029
}
6023-
llvm::CallBase *CallOrInvoke = nullptr;
6024-
RValue Call = EmitCall(FnInfo, Callee, ReturnValue, Args, &CallOrInvoke,
6030+
llvm::CallBase *LocalCallOrInvoke = nullptr;
6031+
RValue Call = EmitCall(FnInfo, Callee, ReturnValue, Args, &LocalCallOrInvoke,
60256032
E == MustTailCall, E->getExprLoc());
60266033

60276034
// Generate function declaration DISuprogram in order to be used
@@ -6030,11 +6037,13 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee
60306037
if (auto *CalleeDecl = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
60316038
FunctionArgList Args;
60326039
QualType ResTy = BuildFunctionArgList(CalleeDecl, Args);
6033-
DI->EmitFuncDeclForCallSite(CallOrInvoke,
6040+
DI->EmitFuncDeclForCallSite(LocalCallOrInvoke,
60346041
DI->getFunctionType(CalleeDecl, ResTy, Args),
60356042
CalleeDecl);
60366043
}
60376044
}
6045+
if (CallOrInvoke)
6046+
*CallOrInvoke = LocalCallOrInvoke;
60386047

60396048
return Call;
60406049
}

0 commit comments

Comments
 (0)