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 bycollect_infobeforesimplify_alias 3,simplify_lets,sroaandsimplify_exitsrewrote the program, so at codegen it can give a function body or aSomepayload naming a variable those passes removed or turned into an assigned one: inlining a let-bound local function from it printedp + kwith nokin scope. The body is now simplified knowing only the parameters it binds; the caller is still compiled with the realmeta. - Which variables count as assigned there? The pass used to take only the ones
lamassigns, but at codegenlamis 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 frommeta.assignableas 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.mapstill wraps its result withPrimitive_option.some; that needs a mark on the result of the call.