Repository navigation
Description
When the optimizer inlines a function whose body is a single primitive or a single call on its parameters, it puts each argument where the body uses that parameter. The arguments then run in the body's order instead of left to right:
let minus = (a: int, b: int) => b - a
let effA = () => {
Console.log("A")
1
}
let effB = () => {
Console.log("B")
2
}
let x = minus(effA(), effB())let x = (console.log("B"), 2) - (console.log("A"), 1) | 0;This logs B before A. When one argument reads mutable state and another changes it, the result is wrong. Around await, the read moves in front of the await:
let r = ref(0)
let f = async (p: promise<int>) => minus(await p, r.contents)
let main = async () => {
let pending = f(Promise.resolve(1))
r := 10
Console.log(await pending)
}async function f(p) {
return r.contents - await p | 0;
}f should read r.contents after p resolves, so main should log 9; it logs -1.
The same happens:
- without
await: withbump = () => { r := r.contents + 10; 1 },minus(bump(), r.contents)compiles tor.contents - bump(), andminus(r.contents, bump())tobump() - r.contents; - with a local
ref, which becomes a mutable variable:minus(s.contents, bumpS())compiles tobumpS() - s; - when the body is a call:
let flip = (a, b) => sub(b, a)reorders the same way; - with an unused argument, which runs before the others: with
first = (a: int, _b: int) => Int.toString(a),first(effA(), effB())compiles toeffB();followed byeffA().toString().
These cases go through Lam_beta_reduce_util.simple_beta_reduce, the fast path that Lam_beta_reduce.propagate_beta_reduce and propagate_beta_reduce_with_map try first. It substitutes each argument for its parameter in the body and puts the unused arguments in front, without checking that this keeps the call order. Its header comment says "since in ocaml, the apply order is not specified". #8572 fixed the order of the let-bindings in the slow path, and its test helper (equalish(x, copy(y))) does not take the fast path.
Falling back to the slow path is not enough on its own. propagate_beta_reduce_with_map also substitutes an argument used at most once when Lam_analysis.no_side_effects holds, and a read of a mutable field or of a mutable variable has no side effects but sees the other arguments' effects. With only the fast path changed, minus(r.contents, bump()) compiles to let b = bump(); return b - r.contents.
Reproduced with 11.1.4, 12.0.0-alpha.5, 12.3.1, 13.0.0-alpha.6 and master (c10223e).
Activity
Evaluate the arguments of an inlined call left to right
Evaluate the arguments of an inlined call left to right (#9177)