Semantic Diff

#9534 Keep module field reads before their variable is assigned

4 commits

2796a446117a261c37df1766640dabc473626473..pr-9534

a507341c7 · cknitt + Claude · Dec 04, 2026, 21:39 UTC

Keep a Some or tuple payload read before its variable is assigned

Why

The alias pass replaced the payload of a variable known to be Some(x), and a field of one known to be (x, _), with x itself. When x is assigned between the binding and the read, the read gets the new value. Since #9022 substituted Some(x) payloads, this is reachable through cross-module @inline bodies, which are stored after Lam_pass_sroa turned their refs into assigned variables. The pass now keeps the binding when the variable is assigned.

modifiedJS cross-module @inline

// Other.res@inlinelet f = (n: int, q: 'a, q2: 'a) => {  let s = ref(q)  let o = Some(s.contents)  for _ in 0 to n {    s := q2  }  switch o {  | Some(v) => Some((v, s.contents))  | None => None  }}@inlinelet g = (n: int, q: 'a, q2: 'a) => {  let s = ref(q)  let t = (s.contents, 1)  for _ in 0 to n {    s := q2  }  let (a, _) = t  (a, s.contents)} // Main.reslet viaAlias = () => Other.f(3, "a", "b")let viaField = () => Other.g(3, "a", "b")
js
Before both return ["b", "b"]
After both return ["a", "b"]
function viaAlias() {function viaAlias() { let s = "a"; let s = "a"; let o = Primitive_option.some(s); for (let _for = 0; _for <= 3; ++_for) { for (let _for = 0; _for <= 3; ++_for) { s = "b"; s = "b"; } } return [ return [ s, Primitive_option.valFromOption(o), s s ]; ];}}… viaField compiles to the same function viaField() { let s = "a"; let t_0 = s; for (let _for = 0; _for <= 3; ++_for) { s = "b"; } return [ t_0, s ];}

Review

  • Why also guard Js.Null.toOption(x) bindings? They record x the same way, and the unwrap and the null test read it later just like a Some(x) payload; no repro was found there, the guard keeps it sound.
9af8bb988 · cknitt + Claude · Dec 04, 2026, 17:58 UTC

Simplify bodies inlined while compiling to JavaScript

Why

A call to another module's function that also passes a parameter of the caller is inlined late, while compiling to JavaScript, after the alias pass has run. So the inlined body kept what that pass removes elsewhere: a Some(x) argument was bound and unwrapped again, and a function argument was called instead of inlined. The alias pass now also runs on such a body before it is compiled.

modifiedJS a literal Some next to a parameter

let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)
js
Before
After
function knownSome(x, f) {function knownSome(x, f) { let opt = x; let opt$1 = opt !== undefined ? Primitive_option.some(f(Primitive_option.valFromOption(opt))) : undefined; if (opt$1 !== undefined) { return opt$1; let opt = Primitive_option.some(f(x)); if (opt !== undefined) { return opt; } else { } else { return 0; return 0; } }}}

modifiedJS a function argument

let mapped = (opt: option<int>) => opt->Option.map(x => x + 1)
js
Before
After
function mapped(opt) {function mapped(opt) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.some((x => x + 1 | 0)(opt)); return Primitive_option.some(opt + 1 | 0); } }}}

Review

  • Why does the late pass get a fresh ident_tbl? The table was filled by collect_info before simplify_alias 3, simplify_lets, sroa and simplify_exits rewrote the program, so at codegen it can give a function body or a Some payload naming a variable those passes removed or turned into an assigned one: inlining a let-bound local function from it printed p + k with no k in scope. The body is now simplified knowing only the parameters it binds; the caller is still compiled with the real meta.
  • Which variables count as assigned there? The pass used to take only the ones lam assigns, but at codegen lam is just the inlined body, so a variable of the caller assigned by a call in it looked immutable and its read moved past that call. The pass now starts from meta.assignable as well. In the other direction, the pass may inline a further function that assigns a variable of its own, so the variables the body assigns are collected after the pass, before it is compiled.
  • What stays? Option.map still wraps its result with Primitive_option.some; that needs a mark on the result of the call.
93beda8c1 · cknitt + Claude · Dec 04, 2026, 18:18 UTC

Return Some of inlined functions without wrapping when the payload can't be undefined

Why

An inlined function such as Option.map is compiled for any 'b, so the Some(v) it returns is Primitive_option.some(v), in case v is itself None. The caller knows the result type. A Some(v) written at the call site is just v when the payload can't be undefined; the inlined copy now returns it the same way.

The translation of a full application whose own type is option<t>, where t can't be undefined, sets a new flag on the call, ap_result_not_nested. The beta reduction that inlines a call takes the call's flag and rewrites the callee's body for it before it substitutes the arguments: a Some in return position is built without wrapping, and a call in return position gets the flag, so that a wrapper of Option.map is inlined the same way. The flag is part of the call, so a flagged call is still a call to every other pass, and a self tail call stays a loop. An @inline body in a .cmj keeps its calls' flags, so another module inlines it the same way.

modifiedLambda result of a call

let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
lambda
Before
After
(function opt/1002 f/1003(function opt/1002 f/1003 (apply (field:map/4 global Stdlib_Option!) (apply #result_not_nested (field:map/4 global Stdlib_Option!) (#not_nested_option opt/1002) f/1003)) (#not_nested_option opt/1002) f/1003))

modifiedLambda body inlined at a flagged call

let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
lambda
Before Option.map's body, before its arguments are substituted
After
(if (#is_not_none opt/1002)(if (#is_not_none opt/1002) (some (apply f/1003 (#val_from_option_not_nest opt/1002))) (some_not_nest (apply f/1003 (#val_from_option_not_nest opt/1002))) undefined) undefined)

modifiedJS inlined Option.map

let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)let nested = (opt: option<int>) => opt->Option.map(x => x > 0 ? Some(x) : None)
js
Before
After
function mapped(opt, f) {function mapped(opt, f) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.some(f(opt)); return f(opt); } }}} function nested(opt) {function nested(opt) { if (opt === undefined) { if (opt === undefined) { return; return; } } let x = opt; let x = opt; return Primitive_option.some(x > 0 ? x : undefined); return Primitive_option.some(x > 0 ? x : undefined);}}

modifiedJS a chain of Option helpers

let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)
js
Before
After
function knownSome(x, f) {function knownSome(x, f) { let opt = Primitive_option.some(f(x)); let opt = f(x); if (opt !== undefined) { if (opt !== undefined) { return opt; return opt; } else { } else { return 0; return 0; } }}}

Review

  • Why is it sound to trust the caller's type? The rewrite trusts the type of the call itself, never the type an argument is used at, which a cast can change. The flag has its own place on the call, so the argument mark of #9504 is never read as one: isSome(magic(opt->Option.map(f))) leaves the cast call's wrapping alone. The body is rewritten before the arguments are substituted, so only its own Somes and calls are: a cast argument that the body returns, as in keep(magic(g(a)), true) with keep = (o, _) => o, keeps its wrapping. A switch at the call site on a value of the call's type already reads the payload directly, so a body that returned a wrapped or undefined payload under that type, through a cast or an external, would already break the caller; such a body inlined here now returns the payload as a Some written here would. A cast of the called function counts the same way, since the call's type comes from the function's type: with callOpt = (g: int => option<int>, a) => g(a), callOpt(magic(nestedPositive), a) inlines nestedPositive, of type int => option<option<int>>, at the call g(a), and returns its payload unwrapped.
  • Does the flag get in the way of other optimizations? No: a flagged call is still a call, so no pass needs to look through a mark. The tests' forwarding function, let x = e; f(x) and a tuple bound before a call compile without a temporary, and a self tail call is still a loop. Apart from the dropped Primitive_option.some calls, the generated JavaScript of the tests, the runtime and Belt doesn't change.
  • Why keep the flag in a .cmj? It is a fact about the call's value at the types of the module that wrote it, so it holds wherever the body is inlined. Without it, let v = (o, f) => Inline_export.mapPair(o, f) in the tests returned Primitive_option.some(f(o)): the call to Option.map in mapPair reads parameters, so it is inlined only after the body is exported.
  • Is a flagged call a marked argument? Yes: an argument whose own call is flagged is an option whose payload can't be undefined, by that call's type, so the inlined body unwraps the parameter it is bound to directly, as for an argument marked by its type.
  • What does it cost to compile? Each full application checks its result type the way #9504 checks arguments: abbreviations are read from their declarations first, and a built-in type such as int or unit is known to be no option without a lookup, which saves the lookups #9504 made for such arguments too. Compiling the runtime and Belt allocates 0.8% fewer words in all; Belt's list and sorted array modules 2.5% fewer; no module more than 0.15% more.
a3d2b7fc1 · cknitt + Claude · Dec 05, 2026, 07:36 UTC

Keep module field reads before their variable is assigned

Why

Two more places moved a read of a module field past an assignment to the variable holding the module, reachable through cross-module @inline bodies. The alias pass substituted a tuple element recorded as M.x with M a variable, and the alias check that lets single-use bindings be inlined accepted M.x for any variable M. Both now check the variable like any other.

modifiedJS cross-module @inline, module field

// Other.resmodule type S = {  let x: string}module A = {  let x = "a"}module B = {  let x = "b"}let get = (m: module(S)) => {  let module(M) = m  M.x}@inlinelet f = (n: int, q: module(S), q2: module(S)) => {  let s = ref(q)  let a = get(s.contents)  for _ in 0 to n {    s := q2  }  (a, get(s.contents))} // Main.reslet main = () => Other.f(3, module(Other.A), module(Other.B))
js
Before returns ["b", "b"]
After returns ["a", "b"]
function main() {function main() { let s = Other.A; let s = Other.A; let a = s.x; for (let _for = 0; _for <= 3; ++_for) { for (let _for = 0; _for <= 3; ++_for) { s = Other.B; s = Other.B; } } return [ return [ s.x, a, s.x s.x ]; ];}}

modifiedJS cross-module @inline, tuple of a module field

// Other.res, with S, A and B as above@inlinelet f = (n: int, q: module(S), q2: module(S)) => {  let s = ref(q)  let t = (    {      let module(M) = s.contents      M.x    },    1,  )  for _ in 0 to n {    s := q2  }  let (a, _) = t  let module(M2) = s.contents  (a, M2.x)} // Main.reslet main = () => Other.f(3, module(Other.A), module(Other.B))
js
Before returns ["b", "b"]
After returns ["a", "b"]
function main() {function main() { let s = Other.A; let s = Other.A; let t_0 = s.x; for (let _for = 0; _for <= 3; ++_for) { for (let _for = 0; _for <= 3; ++_for) { s = Other.B; s = Other.B; } } let M2 = s; let M2 = s; return [ return [ s.x, t_0, M2.x M2.x ]; ];}}