Flutter 3.50.0's First P0: A Store-to-Load Shortcut That Never Checked Dominance
Flutter 3.50.0's First Crash: Fixed, but Still No Hotfix
Flutter 3.50.0 shipped and immediately picked up a P0. The same code compiles on Dart 3.13.5, and on Dart 3.14.0 dart run runs it perfectly, but the moment you do an AOT compile, gen_snapshot throws linearscan.cc:3013: unreachable code. The reason: the control flow generated by the Dart front end and a back-end variable-load optimization developed an ill-fated entanglement, producing an invalid compiler intermediate representation.
The problematic code looks roughly like this, where Opt is a sealed class containing only two subtypes, Query and Item:
final Object value;
switch (opt) {
case Query(:final text):
print(text);
return;
case Item(value: final picked):
value = picked;
}
run(() {
committed = value;
});
Reading the source, nothing seems wrong: Query returns directly, Item assigns value before entering the closure, and since the two cases cover Opt completely, the final run can never execute with value unassigned.
But when the Dart CFE (Common Front End) lowers the pattern-matching switch to low-level conditional branches, even though the final Item branch is necessarily taken here, it still "obligingly" generates an if (opt is Item) check. From the back end's view of the control flow graph, that adds an extra path that says "the value is not an Item, skip the assignment, and continue on."
That path can never actually occur under Dart's sealed type semantics, but it remains present in the compiler's intermediate representation, so a back-end optimization cannot simply assume it doesn't exist. The Dart team also opened #64923 to track this CFE lowering issue.
Before Dart 3.14, commit 25ae146 made a change. It was originally meant to track effectively final variables, that is, variables that never change after initialization. Local variables captured by a closure are normally kept in a Context, and later reads fetch values back out of that Context. This change let immutable Context slots directly reuse the value written at initialization, saving one memory read, which is what's known as Store-to-Load Forwarding.
And in AOT mode, a variable like
final Object value;, declared without an initializer and notlate final, doesn't need to be written withnullfirst either.
Here's the catch: the old implementation carried a precondition. As long as it found the single initialization write, that write was assumed to happen before every read.
For the switch above, that assumption falls apart. In the control flow graph generated by the CFE, picked is only defined in the Item branch, and the extra fake path can bypass it. The compiler then replaces the read of the closure-captured variable value directly with picked, producing the bizarre situation of "using a value that hasn't been defined on the current path."
This corresponds exactly to Dominance in compiler theory: to replace a read with a value, you must ensure that every path reaching the read has already defined that value. Otherwise the generated IL is invalid.
The P0 was fixed quickly. A check was added temporarily to LoadFieldInstr::Canonicalize():
Definition* value = initializing_store->value()->definition();
if (IsDominatedBy(value)) {
return value;
}
That is, the read is only replaced when the initializing value dominates the current read. If the condition isn't satisfied, the original read is kept.
So this temporary fix keeps the original performance optimization in place and only adds the correctness check.
The fix has been merged, but Flutter 3.50.1 isn't out yet. However, the fix code has already entered Dart Stable, so Dart 3.14.1 should ship very soon.
So if this bug is blocking you right now, the interim workaround is to put the variable definition and the closure usage back into the same case:
switch (opt) {
case Query(:final text):
print(text);
case Item(value: final value):
run(() {
committed = value;
});
print(value);
}
All I can say is: the thing compiler people fear most is this kind of corner-case scrap that AI "optimizes" out for you.
Top 2 from juejin.cn, machine-translated. The original thread is authoritative.
I've developed several apps with Flutter.
Can't you just do committed = value directly, instead of run(() { committed = value })?