Skip to content

Commit

Permalink
feat(compiler)!: Enable tail calls by default (#1589)
Browse files Browse the repository at this point in the history
* feat: Make Tail Calls Default

* fix: Disable Tail Calls In Near SmokeTest

* Update size of smallest grain program

---------

Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
  • Loading branch information
spotandjake and ospencer committed Mar 11, 2023
1 parent cbd46ee commit f6e5b00
Show file tree
Hide file tree
Showing 147 changed files with 1,034 additions and 1,403 deletions.
2 changes: 1 addition & 1 deletion cli/__test__/index.gr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* grainc-flags --no-gc --no-bulk-memory */
/* grainc-flags --no-gc --no-bulk-memory --no-wasm-tail-call */
module NearTest

include "runtime/unsafe/wasmi32"
Expand Down
2 changes: 1 addition & 1 deletion cli/__test__/nearEnv.gr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* grainc-flags --no-gc --no-bulk-memory */
/* grainc-flags --no-gc --no-bulk-memory --no-wasm-tail-call */
module NearEnv

provide foreign wasm storage_read: (
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -1391,16 +1391,16 @@ let call_lambda =
| Some(name) =>
let instr =
if (tail) {
if (Config.experimental_tail_call^) {
Expression.Call.make_return;
} else {
if (Config.no_tail_call^) {
(
(wasm_mod, name, args, retty) =>
Expression.Return.make(
wasm_mod,
Expression.Call.make(wasm_mod, name, args, retty),
)
);
} else {
Expression.Call.make_return;
};
} else {
Expression.Call.make;
Expand All @@ -1410,9 +1410,7 @@ let call_lambda =
| None =>
let instr =
if (tail) {
if (Config.experimental_tail_call^) {
Expression.Call_indirect.make_return;
} else {
if (Config.no_tail_call^) {
(
(wasm_mod, table, ptr, args, argty, retty) =>
Expression.Return.make(
Expand All @@ -1427,6 +1425,8 @@ let call_lambda =
),
)
);
} else {
Expression.Call_indirect.make_return;
};
} else {
Expression.Call_indirect.make;
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/utils/config.re
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,10 @@ let compilation_mode =
let statically_link =
toggle_flag(~names=["no-link"], ~doc="Disable static linking", true);

let experimental_tail_call =
let no_tail_call =
toggle_flag(
~names=["experimental-wasm-tail-call"],
~doc="Enables tail-call optimization",
~names=["no-wasm-tail-call"],
~doc="Disables tail-call optimization",
false,
);

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/utils/config.rei
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ let compilation_mode: ref(option(string));

let statically_link: ref(bool);

/** Enable tail-call optimizations */
/** Disables tail-call optimizations */

let experimental_tail_call: ref(bool);
let no_tail_call: ref(bool);

/** Whether to allow cyclic types. */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › modulo4
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $%_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $%_1113)
)
(i32.const -33)
(i32.const 35)
(return_call $%_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $%_1113)
)
(i32.const -33)
(i32.const 35)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › land4
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $&_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $&_1113)
)
(i32.const 1)
(i32.const 1)
(return_call $&_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $&_1113)
)
(i32.const 1)
(i32.const 1)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › lxor1
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $^_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $^_1113)
)
(i32.const 3)
(i32.const 3)
(return_call $^_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $^_1113)
)
(i32.const 3)
(i32.const 3)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › lor1
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $|_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $|_1113)
)
(i32.const 3)
(i32.const 3)
(return_call $|_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $|_1113)
)
(i32.const 3)
(i32.const 3)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › modulo6
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $%_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $%_1113)
)
(i32.const 35)
(i32.const 35)
(return_call $%_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $%_1113)
)
(i32.const 35)
(i32.const 35)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ basic functionality › precedence1
(block $do_backpatches.1
)
)
(return
(call $+_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1113)
)
(i32.const 7)
(local.get $6)
(return_call $+_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1113)
)
(i32.const 7)
(local.get $6)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › comp16
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $==_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $==_1113)
)
(i32.const 2147483646)
(i32.const 2147483646)
(return_call $==_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $==_1113)
)
(i32.const 2147483646)
(i32.const 2147483646)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › orshadow
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $+_1114
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1114)
)
(i32.const 3)
(i32.const 5)
(return_call $+_1114
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1114)
)
(i32.const 3)
(i32.const 5)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ basic functionality › precedence2
(block $do_backpatches.1
)
)
(return
(call $+_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1113)
)
(local.get $6)
(i32.const 7)
(return_call $+_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1113)
)
(local.get $6)
(i32.const 7)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ basic functionality › precedence3
(block $do_backpatches.1
)
)
(return
(call $+_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1113)
)
(i32.const 7)
(local.get $6)
(return_call $+_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $+_1113)
)
(i32.const 7)
(local.get $6)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › binop4
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $*_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $*_1113)
)
(i32.const 5)
(i32.const 7)
(return_call $*_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $*_1113)
)
(i32.const 5)
(i32.const 7)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ basic functionality › lsl1
(local $4 f32)
(local $5 f64)
(block $compile_block.1
(return
(call $<<_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $<<_1113)
)
(i32.const 15)
(i32.const 3)
(return_call $<<_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $<<_1113)
)
(i32.const 15)
(i32.const 3)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ basic functionality › unsafe_wasm_globals
(global.get $_F32_VAL_1145)
)
)
(return
(call $printF64_1146
(global.get $printF64_1146)
(global.get $_F64_VAL_1147)
)
(return_call $printF64_1146
(global.get $printF64_1146)
(global.get $_F64_VAL_1147)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,13 @@ basic functionality › comp22
(block $do_backpatches.17
)
)
(return
(call $isnt_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $isnt_1113)
)
(local.get $8)
(local.get $11)
(return_call $isnt_1113
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $isnt_1113)
)
(local.get $8)
(local.get $11)
)
)
)
Expand Down
Loading

0 comments on commit f6e5b00

Please sign in to comment.