Skip to content

Commit

Permalink
fix(compiler): Do not inline aliased mutables (grain-lang#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
peblair authored Sep 19, 2021
1 parent d3b60a4 commit a50430a
Show file tree
Hide file tree
Showing 22 changed files with 605 additions and 98 deletions.
28 changes: 28 additions & 0 deletions compiler/src/middle_end/analyze_mutable_vars.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
open Anftree;
open Grain_typed;

let mutable_vars = ref(Ident.Set.empty);

let is_mutable = id => Ident.Set.mem(id, mutable_vars^);

module MutableVarsArg = {
include Anf_iterator.DefaultIterArgument;

let enter_anf_expression = ({anf_desc}) => {
switch (anf_desc) {
| AELet(_, _, Mutable, binds, _) =>
List.iter(
((id, _)) => {mutable_vars := Ident.Set.add(id, mutable_vars^)},
binds,
)
| _ => ()
};
};
};

module MutableVarsIter = Anf_iterator.MakeIter(MutableVarsArg);

let analyze = prog => {
mutable_vars := Ident.Set.empty;
MutableVarsIter.iter_anf_program(prog);
};
3 changes: 3 additions & 0 deletions compiler/src/middle_end/analyze_mutable_vars.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let is_mutable: Grain_typed.Ident.t => bool;

let analyze: Anftree.anf_program => unit;
1 change: 1 addition & 0 deletions compiler/src/middle_end/optimize.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let analysis_passes = [
Analyze_tail_calls.analyze,
Analyze_inline_wasm.analyze,
Analyze_closure_scoped_vars.analyze,
Analyze_mutable_vars.analyze,
];

let optimization_passes = [
Expand Down
8 changes: 7 additions & 1 deletion compiler/src/middle_end/optimize_constants.re
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ module ConstantPropagationArg: Anf_mapper.MapArgument = {
Int64.compare(n, Literals.simple_number_max) < 0
&& Int64.compare(n, Literals.simple_number_min) > 0 =>
add_constant(id, ImmConst(Const_number(c')))
| {comp_desc: CImmExpr({imm_desc})} => add_constant(id, imm_desc)
| {comp_desc: CImmExpr({imm_desc})} =>
switch (imm_desc) {
// We don't substitute mutable variables, since we do pass-by-value, not pass-by-reference
| ImmId(rhs_id) when Analyze_mutable_vars.is_mutable(rhs_id) =>
()
| _ => add_constant(id, imm_desc)
}
| _ => ()
},
binds,
Expand Down
25 changes: 23 additions & 2 deletions compiler/test/__snapshots__/let_mut.00e05fe2.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let mut › let-mut_division1
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local.set $1
(tuple.extract 0
(tuple.make
Expand All @@ -37,6 +38,20 @@ let mut › let-mut_division1
)
)
(local.set $2
(tuple.extract 0
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $0)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(i32.const 0)
)
)
)
)
(local.set $3
(tuple.extract 0
(tuple.make
(call_indirect (type $i32_i32_i32_=>_i32)
Expand All @@ -53,7 +68,7 @@ let mut › let-mut_division1
)
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $0)
(local.get $2)
)
(i32.const 39)
(i32.load offset=8
Expand All @@ -72,7 +87,7 @@ let mut › let-mut_division1
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $2)
(local.get $3)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
Expand All @@ -99,6 +114,12 @@ let mut › let-mut_division1
(local.get $2)
)
)
(drop
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(local.get $3)
)
)
(drop
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
Expand Down
27 changes: 24 additions & 3 deletions compiler/test/__snapshots__/let_mut.1176df90.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let mut › let-mut_multiplication2
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local.set $1
(tuple.extract 0
(tuple.make
Expand All @@ -38,6 +39,20 @@ let mut › let-mut_multiplication2
)
)
(local.set $2
(tuple.extract 0
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $0)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(i32.const 0)
)
)
)
)
(local.set $3
(tuple.extract 0
(tuple.make
(call_indirect (type $i32_i32_i32_=>_i32)
Expand All @@ -54,7 +69,7 @@ let mut › let-mut_multiplication2
)
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $0)
(local.get $2)
)
(i32.const 39)
(i32.load offset=8
Expand All @@ -68,7 +83,7 @@ let mut › let-mut_multiplication2
)
)
)
(local.set $3
(local.set $4
(tuple.extract 0
(tuple.make
(block (result i32)
Expand All @@ -77,7 +92,7 @@ let mut › let-mut_multiplication2
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $2)
(local.get $3)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
Expand Down Expand Up @@ -122,6 +137,12 @@ let mut › let-mut_multiplication2
(local.get $3)
)
)
(drop
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(local.get $4)
)
)
(local.get $1)
)
(func $_start (; has Stack IR ;)
Expand Down
23 changes: 22 additions & 1 deletion compiler/test/__snapshots__/let_mut.3307d5a7.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let mut › let-mut3
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local.set $0
(tuple.extract 0
(tuple.make
Expand Down Expand Up @@ -66,6 +67,20 @@ let mut › let-mut3
)
)
)
(local.set $3
(tuple.extract 0
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $2)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(i32.const 0)
)
)
)
)
(call_indirect (type $i32_i32_=>_i32)
(local.tee $0
(tuple.extract 0
Expand All @@ -80,7 +95,7 @@ let mut › let-mut3
)
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $2)
(local.get $3)
)
(i32.load offset=8
(local.get $0)
Expand All @@ -103,6 +118,12 @@ let mut › let-mut3
(local.get $2)
)
)
(drop
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(local.get $3)
)
)
(drop
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
Expand Down
27 changes: 24 additions & 3 deletions compiler/test/__snapshots__/let_mut.43f6980c.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let mut › let-mut_division3
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local.set $1
(tuple.extract 0
(tuple.make
Expand All @@ -38,6 +39,20 @@ let mut › let-mut_division3
)
)
(local.set $2
(tuple.extract 0
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $0)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(i32.const 0)
)
)
)
)
(local.set $3
(tuple.extract 0
(tuple.make
(call_indirect (type $i32_i32_i32_=>_i32)
Expand All @@ -54,7 +69,7 @@ let mut › let-mut_division3
)
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $0)
(local.get $2)
)
(i32.const 39)
(i32.load offset=8
Expand All @@ -68,7 +83,7 @@ let mut › let-mut_division3
)
)
)
(local.set $3
(local.set $4
(tuple.extract 0
(tuple.make
(block (result i32)
Expand All @@ -77,7 +92,7 @@ let mut › let-mut_division3
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $2)
(local.get $3)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
Expand Down Expand Up @@ -122,6 +137,12 @@ let mut › let-mut_division3
(local.get $3)
)
)
(drop
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(local.get $4)
)
)
(local.get $1)
)
(func $_start (; has Stack IR ;)
Expand Down
27 changes: 24 additions & 3 deletions compiler/test/__snapshots__/let_mut.48249b50.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let mut › let-mut5
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local.set $1
(tuple.extract 0
(tuple.make
Expand All @@ -38,6 +39,20 @@ let mut › let-mut5
)
)
(local.set $2
(tuple.extract 0
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $0)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(i32.const 0)
)
)
)
)
(local.set $3
(tuple.extract 0
(tuple.make
(call_indirect (type $i32_i32_i32_=>_i32)
Expand All @@ -54,7 +69,7 @@ let mut › let-mut5
)
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $0)
(local.get $2)
)
(i32.const 3)
(i32.load offset=8
Expand All @@ -68,7 +83,7 @@ let mut › let-mut5
)
)
)
(local.set $3
(local.set $4
(tuple.extract 0
(tuple.make
(block (result i32)
Expand All @@ -77,7 +92,7 @@ let mut › let-mut5
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_incRef_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$incRef_0)
(local.get $2)
(local.get $3)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
Expand Down Expand Up @@ -122,6 +137,12 @@ let mut › let-mut5
(local.get $3)
)
)
(drop
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(local.get $4)
)
)
(local.get $1)
)
(func $_start (; has Stack IR ;)
Expand Down
Loading

0 comments on commit a50430a

Please sign in to comment.