Skip to content

Commit dad797e

Browse files
osa1Commit Queue
authored andcommitted
[dart2wasm] Remove unused default states in br_tables in state machines
When generating the `br_table` for the state machines in `sync*` and `async` functions, we currently generate a default state (required by `br_table`) with an `unreachable` instruction. Instead, remove the default state and make the last state in the function the default state. This saves a few instructions per `sync*` and `async` functions. Issue: #60433 Change-Id: Ie89c3581304a97082628b150cc5604f9e4c2849e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/419540 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Ömer Ağacan <[email protected]>
1 parent bc76743 commit dad797e

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pkg/dart2wasm/lib/async.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,16 @@ class AsyncStateMachineCodeGenerator extends StateMachineCodeGenerator {
226226
// Switch on the target index.
227227
masterLoop = b.loop(const [], const []);
228228
labels = List.generate(targets.length, (_) => b.block()).reversed.toList();
229-
w.Label defaultLabel = b.block();
229+
230+
// There should be at least two states: inner and after targets for the
231+
// [FunctionNode].
232+
assert(labels.length >= 2);
233+
234+
// Use the last target label as the default `br_table` target.
235+
final brTableLabels = labels.sublist(0, labels.length - 1);
236+
final brTableDefaultLabel = labels.last;
230237
b.local_get(targetIndexLocal);
231-
b.br_table(labels, defaultLabel);
232-
b.end(); // defaultLabel
233-
b.unreachable();
238+
b.br_table(brTableLabels, brTableDefaultLabel);
234239

235240
// Initial state
236241
final StateTarget initialTarget = targets.first;

pkg/dart2wasm/lib/sync_star.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,16 @@ class SyncStarStateMachineCodeGenerator extends StateMachineCodeGenerator {
195195
// Switch on the target index.
196196
masterLoop = b.loop(const [], const [w.NumType.i32]);
197197
labels = List.generate(targets.length, (_) => b.block()).reversed.toList();
198-
w.Label defaultLabel = b.block();
198+
199+
// There should be at least two states: inner and after targets for the
200+
// [FunctionNode].
201+
assert(labels.length >= 2);
202+
203+
// Use the last target label as the default `br_table` target.
204+
final brTableLabels = labels.sublist(0, labels.length - 1);
205+
final brTableDefaultLabel = labels.last;
199206
b.local_get(targetIndexLocal);
200-
b.br_table(labels, defaultLabel);
201-
b.end(); // defaultLabel
202-
b.unreachable();
207+
b.br_table(brTableLabels, brTableDefaultLabel);
203208

204209
// Initial state, executed on first [moveNext] on the iterator.
205210
StateTarget initialTarget = targets.first;

0 commit comments

Comments
 (0)