跪拜 Guibai
← All articles
Frontend · Flutter · Android

Flutter 3.50.0's First P0: A Store-to-Load Shortcut That Never Checked Dominance

By 恋猫de小郭 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

AOT builds on Flutter 3.50.0 die on the sort of sealed-class switch that is everywhere in modern Dart, and no hotfix has shipped yet, so affected teams have to reshape otherwise clean code. The crash is also a clean exhibit of a performance optimization quietly breaking a core compiler invariant and taking a whole release down with it.

Summary

Dart 3.14.0 introduces an AOT-only regression that lands straight in Flutter 3.50.0. The same sealed-class `switch` compiles cleanly on Dart 3.13.5 and runs fine through `dart run` on 3.14.0, but an AOT build makes `gen_snapshot` die with `linearscan.cc:3013: unreachable code`. The pattern: one `case` returns early, the other assigns a closure-captured variable before a closure runs.

Dart's front end lowers the pattern-matching switch while leaving a spurious `if (opt is Item)` guard and an extra control-flow path, even though the `Item` branch is logically guaranteed. A pre-3.14 change, commit 25ae146, added store-to-load forwarding for effectively final variables: reusing the initialization value instead of reading from the closure context. That optimization assumed the single initialization write always reaches every read. The extra switch path breaks the assumption, and the compiler substitutes a read with a value that was never defined on that path. Compiler theory names this failure mode: a dominance violation.

The fix adds an `IsDominatedBy` check to `LoadFieldInstr::Canonicalize()` so the substitution only happens when the initializing value dominates the read site. It is merged into Dart Stable and should reach Dart 3.14.1, while Flutter 3.50.1 is still pending. Until it lands, the workaround moves the variable definition and the closure's use into the same `case` branch.

Takeaways
— Flutter 3.50.0 ships a P0 where AOT compilation fails with `linearscan.cc:3013: unreachable code`; the same code compiles in Dart 3.13.5 and runs via `dart run` on 3.14.0.
— A sealed-class `switch` with one early-returning case and another that assigns a closure-captured variable triggers the crash.
— Dart's CFE lowering keeps an `if (opt is Item)` guard and an extra control-flow path even when the `Item` branch is logically guaranteed.
— Commit 25ae146, predating 3.14, added store-to-load forwarding for effectively final variables, reusing the initialization value instead of a context read.
— That optimization assumed the single initialization write precedes all reads; the extra path violates it, so the compiler used a value not defined on every path.
— Dart tracks the CFE lowering fault as issue #64923.
— A dominance check in `LoadFieldInstr::Canonicalize()` substitutes the read only when the initializing value dominates it, via `IsDominatedBy`, keeping the optimization where the invariant holds.
— The fix sits in Dart Stable ahead of Dart 3.14.1; until Flutter 3.50.1 ships, the workaround moves the variable definition and closure use into the same `case`.
Conclusions

A dominance violation sits at the root: the optimization held its single-initialization-write assumption implicitly rather than verifying it against the CFG, so the first CFE-generated junk path it met produced an invalid IL.

The temporary fix deliberately keeps store-to-load forwarding for the common case and only falls back when the value fails to dominate the read, a sensible costing tradeoff that preserves the speedup and closes the correctness hole.

Dart Stable already carries the fix while Flutter 3.50.x stays broken, so anyone pinned to Flutter's Dart SDK is left reshaping idiomatic code over a bug that is solved upstream.

The author's closing jab makes a practical point: AI assistants happily emit this exact compact sealed-class switch, and none of them surface what it does to the compiler's intermediate representation downstream.

Concepts & terms
Dominance
A relation in compiler theory: node A dominates node B when every execution path that reaches B passes through A first. The fix refuses to substitute a value into a read unless the defining instruction dominates the read site.
Store-to-Load Forwarding
A compiler optimization that replays a value from an earlier store at a later load, skipping a memory read. Here it replaced a closure-context read of `value` with the value written at initialization, without confirming every code path ran that store.
gen_snapshot
The Dart SDK's AOT snapshot generator. It produces the native machine image for release builds and is the tool that aborts with `linearscan.cc:3013: unreachable code` on this bug.
CFE (Common Front End)
Dart's shared compiler front end that parses source and lowers it into the intermediate representation used by both JIT and AOT backends. Its lowering of the pattern-matching switch produced the extra path that broke the optimizer's assumption.
From the discussion
Featured comments
茶树米

Can't you just do committed = value directly, instead of run(() { committed = value })?

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗