Skip to content

Commit

Permalink
fix(compiler): Remove number constant folding optimization (#1676)
Browse files Browse the repository at this point in the history
* fix(compiler): Remove number constant folding optimization

* snapshots

* add regression test
  • Loading branch information
ospencer committed Feb 17, 2023
1 parent b6651bb commit 181798d
Show file tree
Hide file tree
Showing 42 changed files with 1,410 additions and 199 deletions.
53 changes: 0 additions & 53 deletions compiler/src/middle_end/optimize_simple_expressions.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,16 @@ open Anftree;
open Grain_typed;
open Types;

let is_int =
fun
| Const_number(Const_number_int(_))
| Const_int32(_)
| Const_int64(_) => true
| _ => false;

let get_int =
fun
| Const_number(Const_number_int(n)) => n
| Const_int32(n) => Int64.of_int32(n)
| Const_int64(n) => n
| _ => failwith("Operand was not an integer");

let get_bool =
fun
| Const_bool(b) => b
| _ => failwith("Operand was not a boolean");

let in_valid_int_range = (op, x, y) =>
if (!is_int(x) || !is_int(y)) {
false;
} else {
let n = op(get_int(x), get_int(y));
/* Unboxed integers ("simple numbers") in Grain are stored double their value, so we need to check if the representation overflows */
let n = Int64.mul(n, 2L);
n < Int64.of_int32(Int32.max_int) && n > Int64.of_int32(Int32.min_int);
};

module ConstantFoldingArg: Anf_mapper.MapArgument = {
include Anf_mapper.DefaultMapArgument;

let leave_comp_expression = ({comp_desc: desc} as c) =>
switch (desc) {
| CApp(
({imm_desc: ImmId({name})}, _),
[{imm_desc: ImmConst(x)} as i, {imm_desc: ImmConst(y)}],
_,
) =>
let wrap_imm = imm => {
...c,
comp_desc: CImmExpr({...i, imm_desc: ImmConst(imm)}),
};
switch (name) {
// TODO(#1169): this should be expanded to make use of BigInts
/* in_valid_int_range check to make sure we don't overflow.
If we will overflow, don't optimize and allow the operation at runtime. */
| "+" when in_valid_int_range(Int64.add, x, y) =>
wrap_imm @@
Const_number(Const_number_int(Int64.add(get_int(x), get_int(y))))
| "-" when in_valid_int_range(Int64.sub, x, y) =>
wrap_imm @@
Const_number(Const_number_int(Int64.sub(get_int(x), get_int(y))))
| "*" when in_valid_int_range(Int64.mul, x, y) =>
wrap_imm @@
Const_number(Const_number_int(Int64.mul(get_int(x), get_int(y))))
| "<" => wrap_imm @@ Const_bool(get_int(x) < get_int(y))
| "<=" => wrap_imm @@ Const_bool(get_int(x) <= get_int(y))
| ">" => wrap_imm @@ Const_bool(get_int(x) > get_int(y))
| ">=" => wrap_imm @@ Const_bool(get_int(x) >= get_int(y))
| "==" => wrap_imm @@ Const_bool(x == y)
| _ => c
};
| CPrim1(Not, {imm_desc: ImmConst(Const_bool(b))} as i) => {
...c,
comp_desc: CImmExpr({...i, imm_desc: ImmConst(Const_bool(!b))}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ basic functionality › comp5
(module
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
(import \"_genv\" \"mem\" (memory $0 0))
(import \"_genv\" \"tbl\" (table $tbl 0 funcref))
(import \"_genv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_genv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$<\" (global $<_1111 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"<\" (func $<_1111 (param i32 i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
Expand All @@ -29,11 +35,37 @@ basic functionality › comp5
(local $3 i64)
(local $4 f32)
(local $5 f64)
(local $6 i32)
(return
(block $cleanup_locals.2 (result i32)
(block $cleanup_locals.6 (result i32)
(local.set $0
(block $compile_block.1 (result i32)
(i32.const 9)
(block $compile_block.5 (result i32)
(block $compile_store.2
(local.set $6
(call $<_1111
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $<_1111)
)
(i32.const 5)
(i32.const 7)
)
)
(block $do_backpatches.1
)
)
(if (result i32)
(i32.shr_u
(local.get $6)
(i32.const 31)
)
(block $compile_block.3 (result i32)
(i32.const 9)
)
(block $compile_block.4 (result i32)
(i32.const 11)
)
)
)
)
(local.get $0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
basic functionality › precedence1
(module
(type $none_=>_none (func))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
(type $none_=>_i32 (func (result i32)))
(import \"_genv\" \"mem\" (memory $0 0))
(import \"_genv\" \"tbl\" (table $tbl 0 funcref))
(import \"_genv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_genv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRef\" (global $GRAIN$EXPORT$decRef_0 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$*\" (global $*_1113 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$+\" (global $+_1110 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"*\" (func $*_1113 (param i32 i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"+\" (func $+_1110 (param i32 i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
Expand All @@ -29,11 +39,50 @@ basic functionality › precedence1
(local $3 i64)
(local $4 f32)
(local $5 f64)
(local $6 i32)
(return
(block $cleanup_locals.2 (result i32)
(block $cleanup_locals.4 (result i32)
(local.set $0
(block $compile_block.1 (result i32)
(i32.const 55)
(block $compile_block.3 (result i32)
(block $compile_store.2
(local.set $6
(tuple.extract 0
(tuple.make
(call $*_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $*_1113)
)
(i32.const 9)
(i32.const 13)
)
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(local.get $6)
)
)
)
)
(block $do_backpatches.1
)
)
(call $+_1110
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1110)
)
(i32.const 7)
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(local.get $6)
)
)
)
)
(drop
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(local.get $6)
)
)
(local.get $0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ basic functionality › comp16
(module
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
(import \"_genv\" \"mem\" (memory $0 0))
(import \"_genv\" \"tbl\" (table $tbl 0 funcref))
(import \"_genv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_genv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$==\" (global $==_1110 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"==\" (func $==_1110 (param i32 i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
Expand Down Expand Up @@ -33,7 +39,14 @@ basic functionality › comp16
(block $cleanup_locals.2 (result i32)
(local.set $0
(block $compile_block.1 (result i32)
(i32.const -2)
(call $==_1110
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $==_1110)
)
(i32.const 2147483646)
(i32.const 2147483646)
)
)
)
(local.get $0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ basic functionality › comp3
(module
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
(import \"_genv\" \"mem\" (memory $0 0))
(import \"_genv\" \"tbl\" (table $tbl 0 funcref))
(import \"_genv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_genv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$>=\" (global $>=_1111 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \">=\" (func $>=_1111 (param i32 i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
Expand All @@ -29,11 +35,37 @@ basic functionality › comp3
(local $3 i64)
(local $4 f32)
(local $5 f64)
(local $6 i32)
(return
(block $cleanup_locals.2 (result i32)
(block $cleanup_locals.6 (result i32)
(local.set $0
(block $compile_block.1 (result i32)
(i32.const 11)
(block $compile_block.5 (result i32)
(block $compile_store.2
(local.set $6
(call $>=_1111
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $>=_1111)
)
(i32.const 5)
(i32.const 7)
)
)
(block $do_backpatches.1
)
)
(if (result i32)
(i32.shr_u
(local.get $6)
(i32.const 31)
)
(block $compile_block.3 (result i32)
(i32.const 9)
)
(block $compile_block.4 (result i32)
(i32.const 11)
)
)
)
)
(local.get $0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ basic functionality › orshadow
(module
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
(import \"_genv\" \"mem\" (memory $0 0))
(import \"_genv\" \"tbl\" (table $tbl 0 funcref))
(import \"_genv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_genv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$+\" (global $+_1111 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"+\" (func $+_1111 (param i32 i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
Expand Down Expand Up @@ -33,7 +39,14 @@ basic functionality › orshadow
(block $cleanup_locals.2 (result i32)
(local.set $0
(block $compile_block.1 (result i32)
(i32.const 7)
(call $+_1111
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1111)
)
(i32.const 3)
(i32.const 5)
)
)
)
(local.get $0)
Expand Down
Loading

0 comments on commit 181798d

Please sign in to comment.