Semantic Diff

#9504 Unwrap option arguments of inlined functions directly when they can't be nested

3 commits

69e11bfe7acda5984c1bf6bb2e7b10c320cd7df6..pr-9504

a717faf33 · cknitt + Claude · Dec 01, 2026, 21:29 UTC

Unwrap option arguments of inlined functions directly when they can't be nested

Why

An inlined function such as Option.getOr is compiled for any 'a, so its body unwraps its option with Primitive_option.valFromOption, in case the payload is itself an option. The caller knows the payload type. A switch written at the call site reads the value directly when the payload can't be undefined; the inlined copy now does the same.

The translation of a call marks each option argument whose payload type can't be undefined with an identity primitive, Pnot_nested_option. When the call is inlined, the body is rewritten for that call: unwrapping a marked parameter becomes a plain read. The marks are dropped after the last inlining pass.

addedLambda Pnot_nested_option

let intOr = (opt: option<int>) => opt->Option.getOr(0)
lambda
Before
After
(function opt/1002(function opt/1002 (apply (field:getOr/6 global Stdlib_Option!) opt/1002 0)) (apply (field:getOr/6 global Stdlib_Option!) (#not_nested_option opt/1002) 0))

modifiedJS an inlined Option helper

let intOr = (opt: option<int>) => opt->Option.getOr(0)let nestedOr = (opt: option<option<int>>) => opt->Option.getOr(Some(0))
js
Before
After
function intOr(opt) {function intOr(opt) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.valFromOption(opt); return opt; } else { } else { return 0; return 0; } }}} function nestedOr(opt) {function nestedOr(opt) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.valFromOption(opt); return Primitive_option.valFromOption(opt); } else { } else { return 0; return 0; } }}}

Review

  • Why is the fact kept per call and not per variable? A variable can reach a call with a type of its own: f((Obj.magic(y): option<int>)) or a coercion translates to plain y. Recording y as not nested would also turn a correct switch y elsewhere into a plain read. So only the inlined body is rewritten, and y keeps valFromOption everywhere else.
  • What stays? Option.map still wraps its result with Primitive_option.some, since the body doesn't know the result type either. That needs a second mark, on the result, and is left for later.
88fa10878 · cknitt + Claude · Dec 02, 2026, 06:28 UTC

Keep known option values and cross-module inlining with marked arguments

Why

A review found three gaps in the first commit. Rewriting the unwrap to the bare parameter skipped the rule that reads the value a known option was built from, so let opt = Nullable.toOption(x) kept a wrapper variable. Each mark counted toward the inlining size, so small wrappers stopped being inlined. And calls to another module's function with a parameter as an argument are inlined only during JavaScript compilation, after the marks were gone, so opt->Option.getOr(default) kept valFromOption.

Now the unwrap becomes Pval_from_option_not_nest for a variable bound to a known option, the size ignores the mark, the marks stay on the arguments of calls to other modules' functions, and the inlining guard treats a marked parameter like a plain one.

modifiedJS another module's function with a parameter argument

let paramOr = (opt: option<int>, default) => opt->Option.getOr(default)
js
Before
After
function paramOr(opt, $$default) {function paramOr(opt, $$default) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.valFromOption(opt); return opt; } else { } else { return $$default; return $$default; } }}}

modifiedJS a variable bound to a nullable conversion

type r = {input: string}@send external exec: (string, string) => Nullable.t<r> = "exec"let r1 = (s: string) => {  let result = exec(s, s)->Nullable.toOption  (result->Option.map(x => x.input), result->Option.map(x => x.input ++ "a"))}
js
Before
After
function r1(s) {function r1(s) { let result = s.exec(s); let result = s.exec(s); let result$1 = result == null ? undefined : Primitive_option.some(result); return [ return [ !(result == null) ? Primitive_option.some(result$1.input) : undefined, !(result == null) ? Primitive_option.some(result$1.input + "a") : undefined !(result == null) ? Primitive_option.some(result.input) : undefined, !(result == null) ? Primitive_option.some(result.input + "a") : undefined ]; ];}}
ae67fffd5 · cknitt + Claude · Dec 02, 2026, 07:36 UTC

Keep option marks to the end and pass them through forwarding calls

Why

A second review found a simpler design and a regression. The marks no longer need removing: a mark compiles to nothing, and the passes after inlining don't look at the shape of call arguments. So the strip pass goes, and the unwrap always becomes Pval_from_option_not_nest, which the alias pass still resolves for a variable bound to a known option.

The regression: simple_beta_reduce refused a body whose call arguments were marked, so a small forwarding wrapper got temporaries for its arguments. It now substitutes through a mark. And an inlined body that passes a marked parameter on to another call marks it there too, so a generic wrapper of getOr reads the value directly as well.

modifiedJS a forwarding wrapper

@val external h: unit => option<int> = "h"@val external k: unit => int = "k"let w2 = (f, o: option<int>, d: int): int => f(o, d)let useW2 = f => w2(f, h(), k())
js
Before
After
function useW2(f) {function useW2(f) { let o = h(); let d = k(); return f(o, d); return f(h(), k()); }}

modifiedJS a generic wrapper

@inlinelet getOr = (opt, default) =>  switch opt {  | Some(v) => v  | None => default  }let wrapOr = (opt, default) => getOr(opt, default)let viaWrapper = (opt: option<int>) => wrapOr(opt, 0)
js
Before
After
function viaWrapper(opt) {function viaWrapper(opt) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.valFromOption(opt); return opt; } else { } else { return 0; return 0; } }}}