Skip to content

Commit 2ad557a

Browse files
alexmarkovCommit Bot
authored and
Commit Bot
committed
[vm] Cleanup more async-related code
This change continues cleanup of async implementation in the VM. TEST=ci Issue: #48378 Change-Id: Icdaeab18bcdc0d6974bc45841b630822cd1ac114 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/251441 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 9391343 commit 2ad557a

12 files changed

+44
-151
lines changed

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -698,22 +698,14 @@ Fragment StreamingFlowGraphBuilder::BuildFunctionBody(
698698
return body;
699699
}
700700

701-
Fragment StreamingFlowGraphBuilder::BuildEveryTimePrologue(
701+
Fragment StreamingFlowGraphBuilder::BuildRegularFunctionPrologue(
702702
const Function& dart_function,
703703
TokenPosition token_position,
704-
intptr_t type_parameters_offset) {
704+
LocalVariable* first_parameter) {
705705
Fragment F;
706706
F += CheckStackOverflowInPrologue(dart_function);
707707
F += DebugStepCheckInPrologue(dart_function, token_position);
708708
F += B->InitConstantParameters();
709-
return F;
710-
}
711-
712-
Fragment StreamingFlowGraphBuilder::BuildFirstTimePrologue(
713-
const Function& dart_function,
714-
LocalVariable* first_parameter,
715-
intptr_t type_parameters_offset) {
716-
Fragment F;
717709
F += SetupCapturedParameters(dart_function);
718710
F += ShortcutForUserDefinedEquals(dart_function, first_parameter);
719711
return F;
@@ -747,8 +739,7 @@ Fragment StreamingFlowGraphBuilder::ClearRawParameters(
747739
UncheckedEntryPointStyle StreamingFlowGraphBuilder::ChooseEntryPointStyle(
748740
const Function& dart_function,
749741
const Fragment& implicit_type_checks,
750-
const Fragment& first_time_prologue,
751-
const Fragment& every_time_prologue,
742+
const Fragment& regular_function_prologue,
752743
const Fragment& type_args_handling) {
753744
ASSERT(!dart_function.IsImplicitClosureFunction());
754745
if (!dart_function.MayHaveUncheckedEntryPoint() ||
@@ -761,17 +752,21 @@ UncheckedEntryPointStyle StreamingFlowGraphBuilder::ChooseEntryPointStyle(
761752
//
762753
// 1. There is a non-empty PrologueBuilder-prologue.
763754
//
764-
// 2. There is a non-empty "first-time" prologue.
755+
// 2. The regular function prologue has more than two instructions
756+
// (DebugStepCheck and CheckStackOverflow).
765757
//
766-
// 3. The "every-time" prologue has more than two instructions (DebugStepCheck
767-
// and CheckStackOverflow).
768-
//
769-
// TODO(#34162): For regular closures we can often avoid the
770-
// PrologueBuilder-prologue on non-dynamic invocations.
771758
if (!PrologueBuilder::HasEmptyPrologue(dart_function) ||
772-
!type_args_handling.is_empty() || !first_time_prologue.is_empty() ||
773-
!(every_time_prologue.entry == every_time_prologue.current ||
774-
every_time_prologue.current->previous() == every_time_prologue.entry)) {
759+
!type_args_handling.is_empty()) {
760+
return UncheckedEntryPointStyle::kSharedWithVariable;
761+
}
762+
Instruction* instr = regular_function_prologue.entry;
763+
if (instr != nullptr && instr->IsCheckStackOverflow()) {
764+
instr = instr->next();
765+
}
766+
if (instr != nullptr && instr->IsDebugStepCheck()) {
767+
instr = instr->next();
768+
}
769+
if (instr != nullptr) {
775770
return UncheckedEntryPointStyle::kSharedWithVariable;
776771
}
777772

@@ -782,15 +777,11 @@ FlowGraph* StreamingFlowGraphBuilder::BuildGraphOfFunction(
782777
bool is_constructor) {
783778
const Function& dart_function = parsed_function()->function();
784779

785-
intptr_t type_parameters_offset = 0;
786780
LocalVariable* first_parameter = nullptr;
787781
TokenPosition token_position = TokenPosition::kNoSource;
788782
{
789783
AlternativeReadingScope alt(&reader_);
790784
FunctionNodeHelper function_node_helper(this);
791-
function_node_helper.ReadUntilExcluding(
792-
FunctionNodeHelper::kTypeParameters);
793-
type_parameters_offset = ReaderOffset();
794785
function_node_helper.ReadUntilExcluding(
795786
FunctionNodeHelper::kPositionalParameters);
796787
intptr_t list_length = ReadListLength(); // read number of positionals.
@@ -811,15 +802,8 @@ FlowGraph* StreamingFlowGraphBuilder::BuildGraphOfFunction(
811802
BlockEntryInstr* instruction_cursor =
812803
flow_graph_builder_->BuildPrologue(normal_entry, &prologue_info);
813804

814-
// The 'every_time_prologue' runs first and is run when resuming from yield
815-
// points.
816-
const Fragment every_time_prologue = BuildEveryTimePrologue(
817-
dart_function, token_position, type_parameters_offset);
818-
819-
// The 'first_time_prologue' run after 'every_time_prologue' and is *not* run
820-
// when resuming from yield points.
821-
const Fragment first_time_prologue = BuildFirstTimePrologue(
822-
dart_function, first_parameter, type_parameters_offset);
805+
const Fragment regular_prologue = BuildRegularFunctionPrologue(
806+
dart_function, token_position, first_parameter);
823807

824808
// TODO(#34162): We can remove the default type handling (and
825809
// shorten the prologue type handling sequence) for non-dynamic invocations of
@@ -847,39 +831,36 @@ FlowGraph* StreamingFlowGraphBuilder::BuildGraphOfFunction(
847831
InitSuspendableFunction(dart_function) +
848832
BuildFunctionBody(dart_function, first_parameter, is_constructor);
849833

850-
auto extra_entry_point_style = ChooseEntryPointStyle(
851-
dart_function, implicit_type_checks, first_time_prologue,
852-
every_time_prologue, type_args_handling);
834+
auto extra_entry_point_style =
835+
ChooseEntryPointStyle(dart_function, implicit_type_checks,
836+
regular_prologue, type_args_handling);
853837

854838
Fragment function(instruction_cursor);
855839
FunctionEntryInstr* extra_entry = nullptr;
856840
switch (extra_entry_point_style) {
857841
case UncheckedEntryPointStyle::kNone: {
858-
function += every_time_prologue + first_time_prologue +
859-
type_args_handling + implicit_type_checks +
842+
function += regular_prologue + type_args_handling + implicit_type_checks +
860843
explicit_type_checks + body;
861844
break;
862845
}
863846
case UncheckedEntryPointStyle::kSeparate: {
864847
ASSERT(instruction_cursor == normal_entry);
865-
ASSERT(first_time_prologue.is_empty());
866848
ASSERT(type_args_handling.is_empty());
867849

868-
const Fragment prologue_copy = BuildEveryTimePrologue(
869-
dart_function, token_position, type_parameters_offset);
850+
const Fragment prologue_copy = BuildRegularFunctionPrologue(
851+
dart_function, token_position, first_parameter);
870852

871853
extra_entry = B->BuildSeparateUncheckedEntryPoint(
872854
normal_entry,
873-
/*normal_prologue=*/every_time_prologue + implicit_type_checks,
855+
/*normal_prologue=*/regular_prologue + implicit_type_checks,
874856
/*extra_prologue=*/prologue_copy,
875857
/*shared_prologue=*/explicit_type_checks,
876858
/*body=*/body);
877859
break;
878860
}
879861
case UncheckedEntryPointStyle::kSharedWithVariable: {
880862
Fragment prologue(normal_entry, instruction_cursor);
881-
prologue += every_time_prologue;
882-
prologue += first_time_prologue;
863+
prologue += regular_prologue;
883864
prologue += type_args_handling;
884865
prologue += explicit_type_checks;
885866
extra_entry = B->BuildSharedUncheckedEntryPoint(

runtime/vm/compiler/frontend/kernel_binary_flowgraph.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,12 @@ class StreamingFlowGraphBuilder : public KernelReaderHelper {
8282
bool constructor);
8383

8484
// Pieces of the prologue. They are all agnostic to the current Kernel offset.
85-
Fragment BuildEveryTimePrologue(const Function& dart_function,
86-
TokenPosition token_position,
87-
intptr_t type_parameters_offset);
88-
Fragment BuildFirstTimePrologue(const Function& dart_function,
89-
LocalVariable* first_parameter,
90-
intptr_t type_parameters_offset);
85+
Fragment BuildRegularFunctionPrologue(const Function& dart_function,
86+
TokenPosition token_position,
87+
LocalVariable* first_parameter);
9188
Fragment ClearRawParameters(const Function& dart_function);
9289
Fragment DebugStepCheckInPrologue(const Function& dart_function,
9390
TokenPosition position);
94-
Fragment SetAsyncStackTrace(const Function& dart_function);
9591
Fragment CheckStackOverflowInPrologue(const Function& dart_function);
9692
Fragment SetupCapturedParameters(const Function& dart_function);
9793
Fragment InitSuspendableFunction(const Function& dart_function);
@@ -102,8 +98,7 @@ class StreamingFlowGraphBuilder : public KernelReaderHelper {
10298
static UncheckedEntryPointStyle ChooseEntryPointStyle(
10399
const Function& dart_function,
104100
const Fragment& implicit_type_checks,
105-
const Fragment& first_time_prologue,
106-
const Fragment& every_time_prologue,
101+
const Fragment& regular_function_prologue,
107102
const Fragment& type_args_handling);
108103

109104
void loop_depth_inc();

runtime/vm/compiler/frontend/scope_builder.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ ScopeBuildingResult* ScopeBuilder::BuildScopes() {
148148
helper_.ReadUntilFunctionNode();
149149
function_node_helper.ReadUntilExcluding(
150150
FunctionNodeHelper::kPositionalParameters);
151-
current_function_async_marker_ = function_node_helper.async_marker_;
152151
// NOTE: FunctionNode is read further below the if.
153152

154153
intptr_t pos = 0;
@@ -368,7 +367,6 @@ ScopeBuildingResult* ScopeBuilder::BuildScopes() {
368367
scope_->InsertParameterAt(pos++, parsed_function_->receiver_var());
369368

370369
// Create all positional and named parameters.
371-
current_function_async_marker_ = FunctionNodeHelper::kSync;
372370
AddPositionalAndNamedParameters(
373371
pos, kTypeCheckEverythingNotCheckedInNonDynamicallyInvokedMethod,
374372
attrs);
@@ -393,7 +391,6 @@ ScopeBuildingResult* ScopeBuilder::BuildScopes() {
393391
// Callbacks and calls with handles need try/catch variables.
394392
if ((function.FfiCallbackTarget() != Function::null() ||
395393
function.FfiCSignatureContainsHandles())) {
396-
current_function_async_marker_ = FunctionNodeHelper::kSync;
397394
++depth_.try_;
398395
AddTryVariables();
399396
--depth_.try_;
@@ -1462,13 +1459,10 @@ void ScopeBuilder::HandleLocalFunction(intptr_t parent_kernel_offset) {
14621459
function_node_helper.ReadUntilExcluding(FunctionNodeHelper::kTypeParameters);
14631460

14641461
LocalScope* saved_function_scope = current_function_scope_;
1465-
FunctionNodeHelper::AsyncMarker saved_function_async_marker =
1466-
current_function_async_marker_;
14671462
DepthState saved_depth_state = depth_;
14681463
depth_ = DepthState(depth_.function_ + 1);
14691464
EnterScope(parent_kernel_offset);
14701465
current_function_scope_ = scope_;
1471-
current_function_async_marker_ = function_node_helper.async_marker_;
14721466
if (depth_.function_ == 1) {
14731467
FunctionScope function_scope = {offset, scope_};
14741468
result_->function_scopes.Add(function_scope);
@@ -1516,7 +1510,6 @@ void ScopeBuilder::HandleLocalFunction(intptr_t parent_kernel_offset) {
15161510
ExitScope(function_node_helper.position_, function_node_helper.end_position_);
15171511
depth_ = saved_depth_state;
15181512
current_function_scope_ = saved_function_scope;
1519-
current_function_async_marker_ = saved_function_async_marker;
15201513
}
15211514

15221515
void ScopeBuilder::EnterScope(intptr_t kernel_offset) {

runtime/vm/compiler/frontend/scope_builder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ class ScopeBuilder {
154154
TranslationHelper translation_helper_;
155155
Zone* zone_;
156156

157-
FunctionNodeHelper::AsyncMarker current_function_async_marker_;
158157
LocalScope* current_function_scope_;
159158
LocalScope* scope_;
160159
DepthState depth_;

runtime/vm/compiler/recognized_methods_list.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ namespace dart {
9797
0xc40903ac) \
9898
V(_SuspendState, _clone, SuspendState_clone, 0xae1a40a0) \
9999
V(_SuspendState, _createAsyncCallbacks, SuspendState_createAsyncCallbacks, \
100-
0x68be1bf3) \
100+
0x967521b1) \
101101
V(_SuspendState, _createAsyncStarCallback, \
102-
SuspendState_createAsyncStarCallback, 0xfa7537e4) \
102+
SuspendState_createAsyncStarCallback, 0xa50f923c) \
103103
V(_SuspendState, _resume, SuspendState_resume, 0x5d7a8489) \
104104
V(_IntegerImplementation, toDouble, IntegerToDouble, 0x97728b46) \
105105
V(_Double, _add, DoubleAdd, 0xea666327) \

runtime/vm/debugger.cc

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ ActivationFrame::ActivationFrame(const Closure& async_activation,
306306
pc_desc_(PcDescriptors::ZoneHandle()) {
307307
// Extract the function and the code from the asynchronous activation.
308308
function_ = async_activation.function();
309-
if (caller_closure_finder->IsCompactAsyncCallback(function_)) {
309+
if (caller_closure_finder->IsAsyncCallback(function_)) {
310310
const auto& suspend_state = SuspendState::Handle(
311311
caller_closure_finder->GetSuspendStateFromAsyncCallback(
312312
async_activation));
@@ -2944,16 +2944,6 @@ Breakpoint* Debugger::SetBreakpointAtActivation(const Instance& closure,
29442944
return bpt_location->AddPerClosure(this, closure, for_over_await);
29452945
}
29462946

2947-
Breakpoint* Debugger::SetBreakpointAtAsyncOp(const Function& async_op) {
2948-
const Script& script = Script::Handle(async_op.script());
2949-
BreakpointLocation* bpt_location =
2950-
SetBreakpoint(script, async_op.token_pos(), async_op.end_token_pos(), -1,
2951-
-1 /* no line/col */, async_op);
2952-
auto bpt = bpt_location->AddSingleShot(this);
2953-
bpt->set_is_synthetic_async(true);
2954-
return bpt;
2955-
}
2956-
29572947
Breakpoint* Debugger::BreakpointAtActivation(const Instance& closure) {
29582948
if (!closure.IsClosure()) {
29592949
return NULL;
@@ -4422,7 +4412,7 @@ void Debugger::MaybeAsyncStepInto(const Closure& async_op) {
44224412
void Debugger::AsyncStepInto(const Closure& async_op) {
44234413
Zone* zone = Thread::Current()->zone();
44244414
CallerClosureFinder caller_closure_finder(zone);
4425-
if (caller_closure_finder.IsCompactAsyncCallback(
4415+
if (caller_closure_finder.IsAsyncCallback(
44264416
Function::Handle(zone, async_op.function()))) {
44274417
const auto& suspend_state = SuspendState::Handle(
44284418
zone, caller_closure_finder.GetSuspendStateFromAsyncCallback(async_op));

runtime/vm/debugger.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,6 @@ class Debugger {
719719
intptr_t line_number,
720720
intptr_t column_number);
721721

722-
// Sets synthetic breakpoint at async_op to step over the synthetic part of
723-
// the stack trace.
724-
Breakpoint* SetBreakpointAtAsyncOp(const Function& async_op);
725-
726722
BreakpointLocation* BreakpointLocationAtLineCol(const String& script_url,
727723
intptr_t line_number,
728724
intptr_t column_number);

runtime/vm/object.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6996,13 +6996,6 @@ class Context : public Object {
69966996
static const intptr_t kBytesPerElement = kCompressedWordSize;
69976997
static const intptr_t kMaxElements = kSmiMax / kBytesPerElement;
69986998

6999-
static const intptr_t kAwaitJumpVarIndex = 0;
7000-
static const intptr_t kAsyncFutureIndex = 1;
7001-
static const intptr_t kControllerIndex = 1;
7002-
// Expected context index of chained futures in recognized async functions.
7003-
// These are used to unwind async stacks.
7004-
static const intptr_t kIsSyncIndex = 2;
7005-
70066999
struct ArrayTraits {
70077000
static intptr_t elements_start_offset() { return sizeof(UntaggedContext); }
70087001
static constexpr intptr_t kElementSize = kBytesPerElement;

runtime/vm/stack_trace.cc

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,6 @@ ClosurePtr CallerClosureFinder::GetCallerInFutureImpl(const Object& future) {
133133
return GetCallerInFutureListener(listener);
134134
}
135135

136-
ClosurePtr CallerClosureFinder::FindCallerInAsyncGenClosure(
137-
const Context& receiver_context) {
138-
// Get the async* _AsyncStarStreamController.
139-
context_entry_ = receiver_context.At(Context::kControllerIndex);
140-
return FindCallerInAsyncStarStreamController(context_entry_);
141-
}
142-
143136
ClosurePtr CallerClosureFinder::FindCallerInAsyncStarStreamController(
144137
const Object& async_star_stream_controller) {
145138
ASSERT(async_star_stream_controller.IsInstance());
@@ -246,7 +239,7 @@ ClosurePtr CallerClosureFinder::FindCallerFromSuspendState(
246239
}
247240
}
248241

249-
bool CallerClosureFinder::IsCompactAsyncCallback(const Function& function) {
242+
bool CallerClosureFinder::IsAsyncCallback(const Function& function) {
250243
parent_function_ = function.parent_function();
251244
auto kind = parent_function_.recognized_kind();
252245
return (kind == MethodRecognizer::kSuspendState_createAsyncCallbacks) ||
@@ -255,7 +248,7 @@ bool CallerClosureFinder::IsCompactAsyncCallback(const Function& function) {
255248

256249
SuspendStatePtr CallerClosureFinder::GetSuspendStateFromAsyncCallback(
257250
const Closure& closure) {
258-
ASSERT(IsCompactAsyncCallback(Function::Handle(closure.function())));
251+
ASSERT(IsAsyncCallback(Function::Handle(closure.function())));
259252
// Async/async* handler only captures the receiver (SuspendState).
260253
receiver_context_ = closure.context();
261254
RELEASE_ASSERT(receiver_context_.num_variables() == 1);
@@ -266,7 +259,7 @@ ClosurePtr CallerClosureFinder::FindCaller(const Closure& receiver_closure) {
266259
receiver_function_ = receiver_closure.function();
267260
receiver_context_ = receiver_closure.context();
268261

269-
if (IsCompactAsyncCallback(receiver_function_)) {
262+
if (IsAsyncCallback(receiver_function_)) {
270263
suspend_state_ = GetSuspendStateFromAsyncCallback(receiver_closure);
271264
return FindCallerFromSuspendState(suspend_state_);
272265
}
@@ -302,12 +295,6 @@ ClosurePtr CallerClosureFinder::FindCaller(const Closure& receiver_closure) {
302295
return Closure::null();
303296
}
304297

305-
ObjectPtr CallerClosureFinder::GetAsyncFuture(const Closure& receiver_closure) {
306-
// Closure -> Context -> _Future.
307-
receiver_context_ = receiver_closure.context();
308-
return receiver_context_.At(Context::kAsyncFutureIndex);
309-
}
310-
311298
ObjectPtr CallerClosureFinder::GetFutureFutureListener(const Object& future) {
312299
ASSERT(future.GetClassId() == future_impl_class.id());
313300
auto& listener = Object::Handle(
@@ -462,7 +449,7 @@ void StackTraceUtils::UnwindAwaiterChain(
462449
if (function.IsNull()) {
463450
continue;
464451
}
465-
if (caller_closure_finder->IsCompactAsyncCallback(function)) {
452+
if (caller_closure_finder->IsAsyncCallback(function)) {
466453
suspend_state =
467454
caller_closure_finder->GetSuspendStateFromAsyncCallback(closure);
468455
const uword pc = suspend_state.pc();

runtime/vm/stack_trace.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ class CallerClosureFinder {
2727
// Returns closure found either via the `result` Future, or the `callback`.
2828
ClosurePtr GetCallerInFutureListener(const Object& future_listener);
2929

30-
// Find caller closure from an async* function receiver context.
31-
// Returns either the `onData` or the Future awaiter.
32-
ClosurePtr FindCallerInAsyncGenClosure(const Context& receiver_context);
33-
3430
// Find caller closure from an _AsyncStarStreamController instance
3531
// corresponding to async* function.
3632
// Returns either the `onData` or the Future awaiter.
@@ -47,15 +43,12 @@ class CallerClosureFinder {
4743

4844
// Returns true if given closure function is a Future callback
4945
// corresponding to an async/async* function or async* body callback.
50-
bool IsCompactAsyncCallback(const Function& function);
46+
bool IsAsyncCallback(const Function& function);
5147

5248
// Returns SuspendState from the given callback which corresponds
5349
// to an async/async* function.
5450
SuspendStatePtr GetSuspendStateFromAsyncCallback(const Closure& closure);
5551

56-
// Finds the awaited Future from an async function receiver closure.
57-
ObjectPtr GetAsyncFuture(const Closure& receiver_closure);
58-
5952
// Get sdk/lib/async/future_impl.dart:_FutureListener.state.
6053
intptr_t GetFutureListenerState(const Object& future_listener);
6154

0 commit comments

Comments
 (0)