lean4-htt/tests/elab/compile_coalesce_rc.lean
Henrik Böving c71a0ea9a5
perf: coalescing of RC ops within one basic block (#13136)
This PR introduces coalescing of RC operations to the RC optimizer.
Whenever we perform multiple `inc`s for a single value within one basic
block it is legal to instead perform all of these `inc`s at once at the
first `inc` side. This is the case because the value will stay alive
until at least the last `inc` and was thus never observable with `RC=1`.
Hence, this change of `inc` location never destroys reuse opportunities.
2026-03-27 10:13:04 +00:00

41 lines
833 B
Text

module
/-! This test demonstrates basic coalescing capabilities of the RC optimizer -/
public section
@[extern "foo"]
opaque foo (x : String) (n : Nat) : Nat
/--
trace: [Compiler.coalesceRc] size: 18
def test x : tobj :=
let _x.1 := 0;
inc[3][ref] x;
let a := foo x _x.1;
let _x.2 := 1;
let _x.3 := foo x _x.2;
let a := Nat.add a _x.3;
dec _x.3;
dec a;
let _x.4 := 2;
let _x.5 := foo x _x.4;
let a := Nat.add a _x.5;
dec _x.5;
dec a;
let _x.6 := 3;
let _x.7 := foo x _x.6;
let a := Nat.add a _x.7;
dec _x.7;
dec a;
return a
-/
#guard_msgs in
set_option trace.Compiler.coalesceRc true in
def test (x : String) : Nat :=
let a := foo x 0
let a := a + foo x 1
let a := a + foo x 2
let a := a + foo x 3
a