Skip to content

Commit fb7b291

Browse files
alexmarkovCommit Bot
authored and
Commit Bot
committed
[vm] Reduce number of callbacks used in sync* functions
This change introduces separate stubs for suspending sync* functions at start and at yield/yield*. Suspend stub for yield/yield* no longer calls Dart callback (in order to make it faster). Also, ReturnSyncStar stub is removed - sync* functions now directly return false instead of going through the stub. TEST=ci Issue: #48378 Change-Id: Iee9a1f48cab2812cf0f9f0e4e6d8e847547e49f7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250420 Reviewed-by: Slava Egorov <[email protected]> Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 61caeca commit fb7b291

26 files changed

+2317
-2442
lines changed

runtime/vm/compiler/backend/il.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6851,10 +6851,6 @@ const Code& ReturnInstr::GetReturnStub(FlowGraphCompiler* compiler) const {
68516851
return Code::ZoneHandle(
68526852
compiler->zone(),
68536853
compiler->isolate_group()->object_store()->return_async_star_stub());
6854-
} else if (function.IsSyncGenerator()) {
6855-
return Code::ZoneHandle(
6856-
compiler->zone(),
6857-
compiler->isolate_group()->object_store()->return_sync_star_stub());
68586854
} else {
68596855
UNREACHABLE();
68606856
}
@@ -7357,8 +7353,11 @@ void SuspendInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
73577353
case StubId::kYieldAsyncStar:
73587354
stub = object_store->yield_async_star_stub();
73597355
break;
7360-
case StubId::kYieldSyncStar:
7361-
stub = object_store->yield_sync_star_stub();
7356+
case StubId::kSuspendSyncStarAtStart:
7357+
stub = object_store->suspend_sync_star_at_start_stub();
7358+
break;
7359+
case StubId::kSuspendSyncStarAtYield:
7360+
stub = object_store->suspend_sync_star_at_yield_stub();
73627361
break;
73637362
}
73647363
compiler->GenerateStubCall(source(), stub, UntaggedPcDescriptors::kOther,

runtime/vm/compiler/backend/il.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9620,7 +9620,8 @@ class SuspendInstr : public TemplateDefinition<1, Throws> {
96209620
enum class StubId {
96219621
kAwait,
96229622
kYieldAsyncStar,
9623-
kYieldSyncStar,
9623+
kSuspendSyncStarAtStart,
9624+
kSuspendSyncStarAtYield,
96249625
};
96259626

96269627
SuspendInstr(const InstructionSource& source,

runtime/vm/compiler/backend/il_arm.cc

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

487-
if (compiler->parsed_function().function().IsSuspendableFunction()) {
487+
if (compiler->parsed_function().function().IsAsyncFunction() ||
488+
compiler->parsed_function().function().IsAsyncGenerator()) {
488489
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
489490
const Code& stub = GetReturnStub(compiler);
490491
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/il_arm64.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
421421
ASSERT(result == CallingConventions::kReturnFpuReg);
422422
}
423423

424-
if (compiler->parsed_function().function().IsSuspendableFunction()) {
424+
if (compiler->parsed_function().function().IsAsyncFunction() ||
425+
compiler->parsed_function().function().IsAsyncGenerator()) {
425426
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
426427
const Code& stub = GetReturnStub(compiler);
427428
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/il_ia32.cc

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

236-
if (compiler->parsed_function().function().IsSuspendableFunction()) {
236+
if (compiler->parsed_function().function().IsAsyncFunction() ||
237+
compiler->parsed_function().function().IsAsyncGenerator()) {
237238
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
238239
const Code& stub = GetReturnStub(compiler);
239240
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/il_printer.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,8 +1394,11 @@ void SuspendInstr::PrintOperandsTo(BaseTextBuffer* f) const {
13941394
case StubId::kYieldAsyncStar:
13951395
name = "YieldAsyncStar";
13961396
break;
1397-
case StubId::kYieldSyncStar:
1398-
name = "YieldSyncStar";
1397+
case StubId::kSuspendSyncStarAtStart:
1398+
name = "SuspendSyncStarAtStart";
1399+
break;
1400+
case StubId::kSuspendSyncStarAtYield:
1401+
name = "SuspendSyncStarAtYield";
13991402
break;
14001403
}
14011404
f->Printf("%s(", name);

runtime/vm/compiler/backend/il_riscv.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ void ReturnInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
496496
ASSERT(result == CallingConventions::kReturnFpuReg);
497497
}
498498

499-
if (compiler->parsed_function().function().IsSuspendableFunction()) {
499+
if (compiler->parsed_function().function().IsAsyncFunction() ||
500+
compiler->parsed_function().function().IsAsyncGenerator()) {
500501
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
501502
const Code& stub = GetReturnStub(compiler);
502503
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/backend/il_x64.cc

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

341-
if (compiler->parsed_function().function().IsSuspendableFunction()) {
341+
if (compiler->parsed_function().function().IsAsyncFunction() ||
342+
compiler->parsed_function().function().IsAsyncGenerator()) {
342343
ASSERT(compiler->flow_graph().graph_entry()->NeedsFrame());
343344
const Code& stub = GetReturnStub(compiler);
344345
compiler->EmitJumpToStub(stub);

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ Fragment StreamingFlowGraphBuilder::InitSuspendableFunction(
608608
body += Drop();
609609
body += NullConstant();
610610
body += B->Suspend(TokenPosition::kNoSource,
611-
SuspendInstr::StubId::kYieldSyncStar);
611+
SuspendInstr::StubId::kSuspendSyncStarAtStart);
612612
body += Drop();
613613
// Clone context if there are any captured parameter variables, so
614614
// each invocation of .iterator would get its own copy of parameters.
@@ -686,7 +686,12 @@ Fragment StreamingFlowGraphBuilder::BuildFunctionBody(
686686
}
687687

688688
if (body.is_open()) {
689-
body += NullConstant();
689+
if (parsed_function()->function().IsSyncGenerator()) {
690+
// Return false from sync* function to indicate the end of iteration.
691+
body += Constant(Bool::False());
692+
} else {
693+
body += NullConstant();
694+
}
690695
body += Return(dart_function.end_token_pos());
691696
}
692697

@@ -4908,9 +4913,19 @@ Fragment StreamingFlowGraphBuilder::BuildReturnStatement(
49084913

49094914
bool inside_try_finally = try_finally_block() != nullptr;
49104915

4911-
Fragment instructions = tag == kNothing
4912-
? NullConstant()
4913-
: BuildExpression(); // read rest of expression.
4916+
Fragment instructions;
4917+
if (parsed_function()->function().IsSyncGenerator()) {
4918+
// Return false from sync* function to indicate the end of iteration.
4919+
instructions += Constant(Bool::False());
4920+
if (tag != kNothing) {
4921+
ASSERT(PeekTag() == kNullLiteral);
4922+
SkipExpression();
4923+
}
4924+
} else {
4925+
instructions +=
4926+
(tag == kNothing ? NullConstant()
4927+
: BuildExpression()); // read rest of expression.
4928+
}
49144929

49154930
if (instructions.is_open()) {
49164931
if (inside_try_finally) {
@@ -5265,8 +5280,9 @@ Fragment StreamingFlowGraphBuilder::BuildYieldStatement(
52655280
field = IG->object_store()->sync_star_iterator_current();
52665281
}
52675282
instructions += B->StoreInstanceFieldGuarded(field);
5268-
instructions += NullConstant();
5269-
instructions += B->Suspend(pos, SuspendInstr::StubId::kYieldSyncStar);
5283+
instructions += B->Constant(Bool::True());
5284+
instructions +=
5285+
B->Suspend(pos, SuspendInstr::StubId::kSuspendSyncStarAtYield);
52705286
instructions += Drop();
52715287
} else {
52725288
UNREACHABLE();

runtime/vm/compiler/runtime_api.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,6 @@ class Thread : public AllStatic {
12121212
static word return_async_not_future_stub_offset();
12131213
static word return_async_star_stub_offset();
12141214
static word return_async_stub_offset();
1215-
static word return_sync_star_stub_offset();
12161215
static word stack_overflow_shared_without_fpu_regs_entry_point_offset();
12171216
static word stack_overflow_shared_without_fpu_regs_stub_offset();
12181217
static word stack_overflow_shared_with_fpu_regs_entry_point_offset();
@@ -1262,8 +1261,7 @@ class Thread : public AllStatic {
12621261
static word suspend_state_return_async_star_entry_point_offset();
12631262

12641263
static word suspend_state_init_sync_star_entry_point_offset();
1265-
static word suspend_state_yield_sync_star_entry_point_offset();
1266-
static word suspend_state_return_sync_star_entry_point_offset();
1264+
static word suspend_state_suspend_sync_star_at_start_entry_point_offset();
12671265

12681266
static word suspend_state_handle_exception_entry_point_offset();
12691267

@@ -1300,9 +1298,8 @@ class ObjectStore : public AllStatic {
13001298
static word suspend_state_return_async_offset();
13011299
static word suspend_state_return_async_not_future_offset();
13021300
static word suspend_state_return_async_star_offset();
1303-
static word suspend_state_return_sync_star_offset();
1301+
static word suspend_state_suspend_sync_star_at_start_offset();
13041302
static word suspend_state_yield_async_star_offset();
1305-
static word suspend_state_yield_sync_star_offset();
13061303
};
13071304

13081305
class Isolate : public AllStatic {

0 commit comments

Comments
 (0)