Skip to content

Commit 4075e8b

Browse files
alexmarkovCommit Bot
authored and
Commit Bot
committed
[vm] New async*/yield/yield* implementation based on suspend/resume stubs
TEST=ci Issue: #48378 Change-Id: I0c2ca9269b2c8f008a79c139a0ce10231996732d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/242923 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 0d5d34a commit 4075e8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1289
-766
lines changed

pkg/kernel/binary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ type TryFinally extends Statement {
13711371
type YieldStatement extends Statement {
13721372
Byte tag = 77;
13731373
FileOffset fileOffset;
1374-
Byte flags (isYieldStar);
1374+
Byte flags (isYieldStar, isNative);
13751375
Expression expression;
13761376
}
13771377

pkg/vm/lib/transformations/continuation.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,16 @@ class RecursiveContinuationRewriter extends RemovingTransformer {
158158
return node;
159159
}
160160
case AsyncMarker.AsyncStar:
161-
return new AsyncStarFunctionRewriter(
162-
helper, node, staticTypeContext, desugarAsync)
163-
.rewrite();
161+
if (desugarAsync) {
162+
return new AsyncStarFunctionRewriter(
163+
helper, node, staticTypeContext, desugarAsync)
164+
.rewrite();
165+
} else {
166+
node.transformOrRemoveChildren(new RecursiveContinuationRewriter(
167+
helper, staticTypeContext, desugarAsync,
168+
desugarAwaitFor: true));
169+
return node;
170+
}
164171
}
165172
}
166173

runtime/vm/code_descriptors.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class ExceptionHandlerList : public ZoneAllocated {
7777
};
7878

7979
explicit ExceptionHandlerList(const Function& function)
80-
: list_(), has_async_handler_(function.IsCompactAsyncFunction()) {}
80+
: list_(),
81+
has_async_handler_(function.IsCompactAsyncFunction() ||
82+
function.IsCompactAsyncStarFunction()) {}
8183

8284
intptr_t Length() const { return list_.length(); }
8385

runtime/vm/compiler/backend/flow_graph_compiler_arm.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ void FlowGraphCompiler::EmitPrologue() {
376376
} else if (parsed_function().suspend_state_var() != nullptr) {
377377
// Initialize synthetic :suspend_state variable early
378378
// as it may be accessed by GC and exception handling before
379-
// InitAsync stub is called.
379+
// InitSuspendableFunction stub is called.
380380
const intptr_t slot_index =
381381
compiler::target::frame_layout.FrameSlotForVariable(
382382
parsed_function().suspend_state_var());

runtime/vm/compiler/backend/flow_graph_compiler_arm64.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ void FlowGraphCompiler::EmitPrologue() {
366366
} else if (parsed_function().suspend_state_var() != nullptr) {
367367
// Initialize synthetic :suspend_state variable early
368368
// as it may be accessed by GC and exception handling before
369-
// InitAsync stub is called.
369+
// InitSuspendableFunction stub is called.
370370
const intptr_t slot_index =
371371
compiler::target::frame_layout.FrameSlotForVariable(
372372
parsed_function().suspend_state_var());

runtime/vm/compiler/backend/flow_graph_compiler_ia32.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ void FlowGraphCompiler::EmitPrologue() {
453453
} else if (parsed_function().suspend_state_var() != nullptr) {
454454
// Initialize synthetic :suspend_state variable early
455455
// as it may be accessed by GC and exception handling before
456-
// InitAsync stub is called.
456+
// InitSuspendableFunction stub is called.
457457
const intptr_t slot_index =
458458
compiler::target::frame_layout.FrameSlotForVariable(
459459
parsed_function().suspend_state_var());

runtime/vm/compiler/backend/flow_graph_compiler_riscv.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ void FlowGraphCompiler::EmitPrologue() {
360360
} else if (parsed_function().suspend_state_var() != nullptr) {
361361
// Initialize synthetic :suspend_state variable early
362362
// as it may be accessed by GC and exception handling before
363-
// InitAsync stub is called.
363+
// InitSuspendableFunction stub is called.
364364
const intptr_t slot_index =
365365
compiler::target::frame_layout.FrameSlotForVariable(
366366
parsed_function().suspend_state_var());

runtime/vm/compiler/backend/flow_graph_compiler_x64.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ void FlowGraphCompiler::EmitPrologue() {
372372
} else if (parsed_function().suspend_state_var() != nullptr) {
373373
// Initialize synthetic :suspend_state variable early
374374
// as it may be accessed by GC and exception handling before
375-
// InitAsync stub is called.
375+
// InitSuspendableFunction stub is called.
376376
const intptr_t slot_index =
377377
compiler::target::frame_layout.FrameSlotForVariable(
378378
parsed_function().suspend_state_var());

runtime/vm/compiler/backend/il.cc

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6901,16 +6901,25 @@ void RawStoreFieldInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
69016901
}
69026902

69036903
const Code& ReturnInstr::GetReturnStub(FlowGraphCompiler* compiler) const {
6904-
ASSERT(compiler->parsed_function().function().IsCompactAsyncFunction());
6905-
if (!value()->Type()->CanBeFuture()) {
6906-
return Code::ZoneHandle(compiler->zone(),
6907-
compiler->isolate_group()
6908-
->object_store()
6909-
->return_async_not_future_stub());
6910-
}
6911-
return Code::ZoneHandle(
6912-
compiler->zone(),
6913-
compiler->isolate_group()->object_store()->return_async_stub());
6904+
const Function& function = compiler->parsed_function().function();
6905+
ASSERT(function.IsSuspendableFunction());
6906+
if (function.IsCompactAsyncFunction()) {
6907+
if (!value()->Type()->CanBeFuture()) {
6908+
return Code::ZoneHandle(compiler->zone(),
6909+
compiler->isolate_group()
6910+
->object_store()
6911+
->return_async_not_future_stub());
6912+
}
6913+
return Code::ZoneHandle(
6914+
compiler->zone(),
6915+
compiler->isolate_group()->object_store()->return_async_stub());
6916+
} else if (function.IsCompactAsyncStarFunction()) {
6917+
return Code::ZoneHandle(
6918+
compiler->zone(),
6919+
compiler->isolate_group()->object_store()->return_async_star_stub());
6920+
} else {
6921+
UNREACHABLE();
6922+
}
69146923
}
69156924

69166925
void NativeReturnInstr::EmitReturnMoves(FlowGraphCompiler* compiler) {
@@ -7230,10 +7239,12 @@ LocationSummary* Call1ArgStubInstr::MakeLocationSummary(Zone* zone,
72307239
LocationSummary(zone, kNumInputs, kNumTemps, LocationSummary::kCall);
72317240
switch (stub_id_) {
72327241
case StubId::kInitAsync:
7242+
case StubId::kInitAsyncStar:
72337243
locs->set_in(0, Location::RegisterLocation(
72347244
InitSuspendableFunctionStubABI::kTypeArgsReg));
72357245
break;
7236-
case StubId::kAwaitAsync:
7246+
case StubId::kAwait:
7247+
case StubId::kYieldAsyncStar:
72377248
locs->set_in(0, Location::RegisterLocation(SuspendStubABI::kArgumentReg));
72387249
break;
72397250
}
@@ -7248,15 +7259,21 @@ void Call1ArgStubInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
72487259
case StubId::kInitAsync:
72497260
stub = object_store->init_async_stub();
72507261
break;
7251-
case StubId::kAwaitAsync:
7252-
stub = object_store->await_async_stub();
7262+
case StubId::kAwait:
7263+
stub = object_store->await_stub();
7264+
break;
7265+
case StubId::kInitAsyncStar:
7266+
stub = object_store->init_async_star_stub();
7267+
break;
7268+
case StubId::kYieldAsyncStar:
7269+
stub = object_store->yield_async_star_stub();
72537270
break;
72547271
}
72557272
compiler->GenerateStubCall(source(), stub, UntaggedPcDescriptors::kOther,
72567273
locs(), deopt_id(), env());
72577274

72587275
#if defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_IA32)
7259-
if (stub_id_ == StubId::kAwaitAsync) {
7276+
if ((stub_id_ == StubId::kAwait) || (stub_id_ == StubId::kYieldAsyncStar)) {
72607277
// On x86 (X64 and IA32) mismatch between calls and returns
72617278
// significantly regresses performance. So suspend stub
72627279
// does not return directly to the caller. Instead, a small

runtime/vm/compiler/backend/il.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9567,8 +9567,10 @@ class SimdOpInstr : public Definition {
95679567
class Call1ArgStubInstr : public TemplateDefinition<1, Throws> {
95689568
public:
95699569
enum class StubId {
9570+
kAwait,
95709571
kInitAsync,
9571-
kAwaitAsync,
9572+
kInitAsyncStar,
9573+
kYieldAsyncStar,
95729574
};
95739575

95749576
Call1ArgStubInstr(const InstructionSource& source,

runtime/vm/compiler/backend/il_arm.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
484484
ASSERT(result == CallingConventions::kReturnFpuReg);
485485
}
486486

487-
if (compiler->parsed_function().function().IsCompactAsyncFunction()) {
487+
if (compiler->parsed_function().function().IsSuspendableFunction()) {
488488
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
489489
const Code& stub = GetReturnStub(compiler);
490490
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/il_arm64.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
411411
ASSERT(result == CallingConventions::kReturnFpuReg);
412412
}
413413

414-
if (compiler->parsed_function().function().IsCompactAsyncFunction()) {
414+
if (compiler->parsed_function().function().IsSuspendableFunction()) {
415415
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
416416
const Code& stub = GetReturnStub(compiler);
417417
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/il_ia32.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
233233
Register result = locs()->in(0).reg();
234234
ASSERT(result == EAX);
235235

236-
if (compiler->parsed_function().function().IsCompactAsyncFunction()) {
236+
if (compiler->parsed_function().function().IsSuspendableFunction()) {
237237
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
238238
const Code& stub = GetReturnStub(compiler);
239239
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/il_printer.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,8 +1362,14 @@ void Call1ArgStubInstr::PrintOperandsTo(BaseTextBuffer* f) const {
13621362
case StubId::kInitAsync:
13631363
name = "InitAsync";
13641364
break;
1365-
case StubId::kAwaitAsync:
1366-
name = "AwaitAsync";
1365+
case StubId::kAwait:
1366+
name = "Await";
1367+
break;
1368+
case StubId::kInitAsyncStar:
1369+
name = "InitAsyncStar";
1370+
break;
1371+
case StubId::kYieldAsyncStar:
1372+
name = "YieldAsyncStar";
13671373
break;
13681374
}
13691375
f->Printf("%s(", name);

runtime/vm/compiler/backend/il_riscv.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
464464
ASSERT(result == CallingConventions::kReturnFpuReg);
465465
}
466466

467-
if (compiler->parsed_function().function().IsCompactAsyncFunction()) {
467+
if (compiler->parsed_function().function().IsSuspendableFunction()) {
468468
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
469469
const Code& stub = GetReturnStub(compiler);
470470
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/il_x64.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
338338
ASSERT(result == CallingConventions::kReturnFpuReg);
339339
}
340340

341-
if (compiler->parsed_function().function().IsCompactAsyncFunction()) {
341+
if (compiler->parsed_function().function().IsSuspendableFunction()) {
342342
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
343343
const Code& stub = GetReturnStub(compiler);
344344
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/range_analysis.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,7 @@ void LoadFieldInstr::InferRange(RangeAnalysis* analysis, Range* range) {
28142814
case Slot::Kind::kFunctionType_parameter_types:
28152815
case Slot::Kind::kFunctionType_type_parameters:
28162816
case Slot::Kind::kInstance_native_fields_array:
2817-
case Slot::Kind::kSuspendState_future:
2817+
case Slot::Kind::kSuspendState_function_data:
28182818
case Slot::Kind::kSuspendState_then_callback:
28192819
case Slot::Kind::kSuspendState_error_callback:
28202820
case Slot::Kind::kTypedDataView_typed_data:

runtime/vm/compiler/backend/slot.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ bool Slot::IsImmutableLengthSlot() const {
243243
case Slot::Kind::kFunctionType_named_parameter_names:
244244
case Slot::Kind::kFunctionType_parameter_types:
245245
case Slot::Kind::kFunctionType_type_parameters:
246-
case Slot::Kind::kSuspendState_future:
246+
case Slot::Kind::kSuspendState_function_data:
247247
case Slot::Kind::kSuspendState_then_callback:
248248
case Slot::Kind::kSuspendState_error_callback:
249249
case Slot::Kind::kType_arguments:

runtime/vm/compiler/backend/slot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ParsedFunction;
7373
V(ImmutableLinkedHashBase, UntaggedLinkedHashBase, index, \
7474
TypedDataUint32Array, VAR) \
7575
V(Instance, UntaggedInstance, native_fields_array, Dynamic, VAR) \
76-
V(SuspendState, UntaggedSuspendState, future, Dynamic, VAR) \
76+
V(SuspendState, UntaggedSuspendState, function_data, Dynamic, VAR) \
7777
V(SuspendState, UntaggedSuspendState, then_callback, Closure, VAR) \
7878
V(SuspendState, UntaggedSuspendState, error_callback, Closure, VAR) \
7979
V(Type, UntaggedType, arguments, TypeArguments, FINAL) \

0 commit comments

Comments
 (0)