Skip to content

Commit 51170e7

Browse files
authored
feat(compiler): Name globals in wasm output (#1184)
* feat(compiler): Name globals in wasm output * snapshots
1 parent 694e6cf commit 51170e7

File tree

328 files changed

+1242
-1301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

328 files changed

+1242
-1301
lines changed

compiler/src/codegen/compcore.re

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ type codegen_env = {
2020
stack_size,
2121
/* Allocated closures which need backpatching */
2222
backpatches: ref(list((Expression.t, closure_data))),
23-
imported_funcs: Ident.tbl(Ident.tbl(int32)),
24-
imported_globals: Ident.tbl(Ident.tbl(string)),
2523
required_imports: list(import),
2624
};
2725

@@ -283,20 +281,9 @@ let init_codegen_env = name => {
283281
stack_size_f64: 0,
284282
},
285283
backpatches: ref([]),
286-
imported_funcs: Ident.empty,
287-
imported_globals: Ident.empty,
288284
required_imports: [],
289285
};
290286

291-
let lookup_ext_global = (env, modname, itemname) =>
292-
Ident.find_same(itemname, Ident.find_same(modname, env.imported_globals));
293-
294-
let var_of_ext_global = (env, modname, itemname) =>
295-
lookup_ext_global(env, modname, itemname);
296-
297-
let lookup_ext_func = (env, modname, itemname) =>
298-
Ident.find_same(itemname, Ident.find_same(modname, env.imported_funcs));
299-
300287
/** Static runtime values */
301288

302289
// Static pointer to the runtime heap
@@ -3436,8 +3423,8 @@ let compile_imports = (wasm_mod, env, {imports}) => {
34363423
let compile_exports = (wasm_mod, env, {imports, exports, globals}) => {
34373424
let compile_export = (i, export) => {
34383425
switch (export) {
3439-
| GlobalExport({ex_global_name, ex_global_index}) =>
3440-
let internal_name = Printf.sprintf("global_%ld", ex_global_index);
3426+
| GlobalExport({ex_global_name}) =>
3427+
let internal_name = Ident.unique_name(ex_global_name);
34413428
let exported_name = "GRAIN$EXPORT$" ++ Ident.name(ex_global_name);
34423429
ignore @@
34433430
Export.add_global_export(wasm_mod, internal_name, exported_name);
@@ -3483,7 +3470,7 @@ let compile_exports = (wasm_mod, env, {imports, exports, globals}) => {
34833470
ignore @@
34843471
Export.add_global_export(
34853472
wasm_mod,
3486-
Printf.sprintf("global_%d", List.length(globals) + 1),
3473+
Ident.name(table_size),
34873474
Ident.name(table_size),
34883475
);
34893476
};
@@ -3511,11 +3498,11 @@ let compile_globals = (wasm_mod, env, {globals} as prog) => {
35113498
| Types.StackAllocated(WasmF32) => const_float32(0.)
35123499
| Types.StackAllocated(WasmF64) => const_float64(0.);
35133500
List.iter(
3514-
((i, ty)) =>
3501+
((id, ty)) =>
35153502
ignore @@
35163503
Global.add_global(
35173504
wasm_mod,
3518-
Printf.sprintf("global_%ld", i),
3505+
Ident.unique_name(id),
35193506
wasm_type(ty),
35203507
true,
35213508
Expression.Const.make(wasm_mod, initial_value(ty)),
@@ -3525,7 +3512,7 @@ let compile_globals = (wasm_mod, env, {globals} as prog) => {
35253512
ignore @@
35263513
Global.add_global(
35273514
wasm_mod,
3528-
Printf.sprintf("global_%d", 1 + List.length(globals)),
3515+
Ident.name(table_size),
35293516
Type.int32,
35303517
false,
35313518
Expression.Const.make(
@@ -3638,49 +3625,14 @@ let validate_module = (~name=?, wasm_mod: Module.t) =>
36383625
raise(WasmRunnerError(wasm_mod, name, "WARNING: Invalid module"))
36393626
};
36403627

3641-
let prepare = (env, {imports}) => {
3642-
let process_import =
3643-
(acc_env, idx, {mimp_mod, mimp_name, mimp_type, mimp_kind}) => {
3644-
let idx_name = Transl_anf.global_name(idx);
3645-
let register = (name, tbl) => {
3646-
let tbl =
3647-
switch (Ident.find_same_opt(mimp_mod, tbl)) {
3648-
| None => Ident.add(mimp_mod, Ident.empty, tbl)
3649-
| Some(_) => tbl
3650-
};
3651-
Ident.add(
3652-
mimp_mod,
3653-
Ident.add(mimp_name, name, Ident.find_same(mimp_mod, tbl)),
3654-
tbl,
3655-
);
3656-
};
3657-
3658-
let (imported_funcs, imported_globals) =
3659-
switch (mimp_type) {
3660-
| MFuncImport(_) => (
3661-
register(Int32.of_int(idx), acc_env.imported_funcs),
3662-
acc_env.imported_globals,
3663-
)
3664-
| MGlobalImport(_) => (
3665-
acc_env.imported_funcs,
3666-
register(idx_name, acc_env.imported_globals),
3667-
)
3668-
};
3669-
{...acc_env, imported_funcs, imported_globals};
3670-
};
3671-
3628+
let prepare = env => {
36723629
let required_imports =
36733630
if (Env.is_runtime_mode()) {
36743631
List.concat([required_global_imports, required_function_imports]);
36753632
} else {
36763633
runtime_imports;
36773634
};
3678-
let new_env =
3679-
List_utils.fold_lefti(process_import, env, runtime_global_imports);
3680-
let new_env =
3681-
List_utils.fold_lefti(process_import, new_env, runtime_function_imports);
3682-
let new_env = List_utils.fold_lefti(process_import, new_env, imports);
3683-
{...new_env, required_imports};
3635+
{...env, required_imports};
36843636
};
36853637

36863638
let compile_wasm_module = (~env=?, ~name=?, prog) => {
@@ -3690,7 +3642,7 @@ let compile_wasm_module = (~env=?, ~name=?, prog) => {
36903642
| None => init_codegen_env(name)
36913643
| Some(e) => e
36923644
};
3693-
let env = prepare(env, prog);
3645+
let env = prepare(env);
36943646
let wasm_mod = Module.create();
36953647
if (Config.source_map^) {
36963648
ignore @@

compiler/src/codegen/compcore.rei

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ type codegen_env = {
99
stack_size,
1010
/* Allocated closures which need backpatching */
1111
backpatches: ref(list((Expression.t, closure_data))),
12-
imported_funcs: Ident.tbl(Ident.tbl(int32)),
13-
imported_globals: Ident.tbl(Ident.tbl(string)),
1412
required_imports: list(import),
1513
};
1614

compiler/src/codegen/mashtree.re

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,7 @@ type export =
466466
ex_function_name: string,
467467
ex_function_internal_name: string,
468468
})
469-
| GlobalExport({
470-
ex_global_name: Ident.t,
471-
ex_global_index: int32,
472-
});
469+
| GlobalExport({ex_global_name: Ident.t});
473470

474471
[@deriving sexp]
475472
type mash_function = {
@@ -497,7 +494,7 @@ type mash_program = {
497494
exports: list(export),
498495
main_body: block,
499496
main_body_stack_size: stack_size,
500-
globals: list((int32, Types.allocation_type)),
497+
globals: list((Ident.t, Types.allocation_type)),
501498
function_table_elements: list(string),
502499
signature: Cmi_format.cmi_infos,
503500
type_metadata: list(Types.type_metadata),

compiler/src/codegen/transl_anf.re

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,11 @@ let set_global_imports = imports => {
9090
/** Global index (index of global variables) */
9191

9292
let global_table =
93-
ref(Ident.empty: Ident.tbl((bool, int32, Types.allocation_type)));
94-
let global_index = ref(0);
93+
ref(Ident.empty: Ident.tbl((bool, Types.allocation_type)));
9594

9695
let get_globals = () => {
9796
Ident.fold_all(
98-
(_, (_, slot, ty), acc) => [(slot, ty), ...acc],
97+
(id, (_, ty), acc) => [(id, ty), ...acc],
9998
global_table^,
10099
[],
101100
);
@@ -104,9 +103,9 @@ let get_globals = () => {
104103
let global_exports = () => {
105104
let tbl = global_table^;
106105
Ident.fold_all(
107-
(ex_global_name, (exported, ex_global_index, _), acc) =>
106+
(ex_global_name, (exported, _), acc) =>
108107
if (exported) {
109-
[GlobalExport({ex_global_name, ex_global_index}), ...acc];
108+
[GlobalExport({ex_global_name: ex_global_name}), ...acc];
110109
} else {
111110
acc;
112111
},
@@ -117,28 +116,23 @@ let global_exports = () => {
117116

118117
let reset_global = () => {
119118
global_table := Ident.empty;
120-
global_index := 0;
121119
};
122120

123-
let next_global = (exported, id, ty: Types.allocation_type) =>
124-
/* RIP Hygiene (this behavior works as expected until we have more metaprogramming constructs) */
121+
let get_global = (exported, id, ty: Types.allocation_type) =>
125122
switch (Ident.find_same_opt(id, global_table^)) {
126-
| Some((_, ret, _)) => Int32.to_int(ret)
123+
| Some(_) => id
127124
| None =>
128-
let ret = global_index^;
129-
global_table :=
130-
Ident.add(id, (exported, Int32.of_int(ret), ty), global_table^);
131-
global_index := ret + 1;
132-
ret;
125+
global_table := Ident.add(id, (exported, ty), global_table^);
126+
id;
133127
};
134128

135-
let global_name = slot => Printf.sprintf("global_%d", slot);
129+
let global_name = id => Ident.unique_name(id);
136130

137131
let find_id = (id, env) =>
138132
try(Ident.find_same(id, env.ce_binds)) {
139133
| Not_found =>
140-
let (_, slot, alloc) = Ident.find_same(id, global_table^);
141-
MGlobalBind(global_name(Int32.to_int(slot)), alloc);
134+
let (_, alloc) = Ident.find_same(id, global_table^);
135+
MGlobalBind(global_name(id), alloc);
142136
};
143137

144138
let worklist_reset = () => Queue.clear(compilation_worklist);
@@ -761,8 +755,8 @@ let compile_wrapper =
761755
{func_idx, arity: Int32.of_int(arity + 1), variables: []};
762756
};
763757

764-
let next_global = (~exported=false, id, ty) => {
765-
let ret = next_global(exported, id, ty);
758+
let get_global = (~exported=false, id, ty) => {
759+
let ret = get_global(exported, id, ty);
766760
global_name(ret);
767761
};
768762

@@ -994,7 +988,7 @@ and compile_anf_expr = (env, a) =>
994988
switch (global) {
995989
| Global({exported}) => (
996990
env,
997-
MGlobalBind(next_global(~exported, id, alloc), alloc),
991+
MGlobalBind(get_global(~exported, id, alloc), alloc),
998992
)
999993
| Nonglobal => (
1000994
next_env,
@@ -1206,7 +1200,7 @@ let lift_imports = (env, imports) => {
12061200
| WasmFunction(mod_, name) =>
12071201
let exported = imp_exported == Global({exported: true});
12081202
let glob =
1209-
next_global(~exported, imp_use_id, Types.StackAllocated(WasmI32));
1203+
get_global(~exported, imp_use_id, Types.StackAllocated(WasmI32));
12101204
let new_mod = {
12111205
mimp_mod: Ident.create_persistent(mod_),
12121206
mimp_name: Ident.create_persistent(name),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
open Grain_middle_end;
22

3-
let global_name: int => string;
3+
let global_name: Grain_typed.Ident.t => string;
44
let transl_anf_program: Anftree.anf_program => Mashtree.mash_program;

compiler/test/__snapshots__/arrays.0f9e7d37.0.snapshot

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ arrays › array_access
1414
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $wimport_GRAIN$MODULE$runtime/gc_incRef (param i32 i32) (result i32)))
1515
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $wimport_GRAIN$MODULE$runtime/gc_decRef (param i32 i32) (result i32)))
1616
(import \"GRAIN$MODULE$runtime/exception\" \"printException\" (func $wimport_GRAIN$MODULE$runtime/exception_printException (param i32 i32) (result i32)))
17-
(global $global_0 (mut i32) (i32.const 0))
18-
(global $global_2 i32 (i32.const 0))
17+
(global $x_1131 (mut i32) (i32.const 0))
18+
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
1919
(export \"memory\" (memory $0))
2020
(export \"_gmain\" (func $_gmain))
2121
(export \"_start\" (func $_start))
22-
(export \"GRAIN$TABLE_SIZE\" (global $global_2))
22+
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
2323
(func $_gmain (; has Stack IR ;) (result i32)
2424
(local $0 i32)
2525
(local $1 i32)
@@ -48,13 +48,13 @@ arrays › array_access
4848
(local.get $0)
4949
(i32.const 7)
5050
)
51-
(global.set $global_0
51+
(global.set $x_1131
5252
(tuple.extract 0
5353
(tuple.make
5454
(local.get $0)
5555
(call $wimport_GRAIN$MODULE$runtime/gc_decRef
5656
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$decRef)
57-
(global.get $global_0)
57+
(global.get $x_1131)
5858
)
5959
)
6060
)
@@ -68,7 +68,7 @@ arrays › array_access
6868
(i32.const 0)
6969
(i32.load offset=4
7070
(local.tee $1
71-
(global.get $global_0)
71+
(global.get $x_1131)
7272
)
7373
)
7474
)

compiler/test/__snapshots__/arrays.24453e6e.0.snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ arrays › array1_trailing
77
(import \"_grainEnv\" \"relocBase\" (global $wimport__grainEnv_relocBase i32))
88
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$malloc (mut i32)))
99
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $wimport_GRAIN$MODULE$runtime/gc_malloc (param i32 i32) (result i32)))
10-
(global $global_1 i32 (i32.const 0))
10+
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
1111
(export \"memory\" (memory $0))
1212
(export \"_gmain\" (func $_gmain))
1313
(export \"_start\" (func $_start))
14-
(export \"GRAIN$TABLE_SIZE\" (global $global_1))
14+
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
1515
(func $_gmain (; has Stack IR ;) (result i32)
1616
(local $0 i32)
1717
(i32.store

compiler/test/__snapshots__/arrays.28fcc534.0.snapshot

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ arrays › array_access4
1414
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $wimport_GRAIN$MODULE$runtime/gc_incRef (param i32 i32) (result i32)))
1515
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $wimport_GRAIN$MODULE$runtime/gc_decRef (param i32 i32) (result i32)))
1616
(import \"GRAIN$MODULE$runtime/exception\" \"printException\" (func $wimport_GRAIN$MODULE$runtime/exception_printException (param i32 i32) (result i32)))
17-
(global $global_0 (mut i32) (i32.const 0))
18-
(global $global_2 i32 (i32.const 0))
17+
(global $x_1131 (mut i32) (i32.const 0))
18+
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
1919
(export \"memory\" (memory $0))
2020
(export \"_gmain\" (func $_gmain))
2121
(export \"_start\" (func $_start))
22-
(export \"GRAIN$TABLE_SIZE\" (global $global_2))
22+
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
2323
(func $_gmain (; has Stack IR ;) (result i32)
2424
(local $0 i32)
2525
(local $1 i32)
@@ -48,13 +48,13 @@ arrays › array_access4
4848
(local.get $0)
4949
(i32.const 7)
5050
)
51-
(global.set $global_0
51+
(global.set $x_1131
5252
(tuple.extract 0
5353
(tuple.make
5454
(local.get $0)
5555
(call $wimport_GRAIN$MODULE$runtime/gc_decRef
5656
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$decRef)
57-
(global.get $global_0)
57+
(global.get $x_1131)
5858
)
5959
)
6060
)
@@ -68,7 +68,7 @@ arrays › array_access4
6868
(i32.const 0)
6969
(i32.load offset=4
7070
(local.tee $1
71-
(global.get $global_0)
71+
(global.get $x_1131)
7272
)
7373
)
7474
)

compiler/test/__snapshots__/arrays.4c8c9f91.0.snapshot

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ arrays › array_access2
1414
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $wimport_GRAIN$MODULE$runtime/gc_incRef (param i32 i32) (result i32)))
1515
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $wimport_GRAIN$MODULE$runtime/gc_decRef (param i32 i32) (result i32)))
1616
(import \"GRAIN$MODULE$runtime/exception\" \"printException\" (func $wimport_GRAIN$MODULE$runtime/exception_printException (param i32 i32) (result i32)))
17-
(global $global_0 (mut i32) (i32.const 0))
18-
(global $global_2 i32 (i32.const 0))
17+
(global $x_1131 (mut i32) (i32.const 0))
18+
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
1919
(export \"memory\" (memory $0))
2020
(export \"_gmain\" (func $_gmain))
2121
(export \"_start\" (func $_start))
22-
(export \"GRAIN$TABLE_SIZE\" (global $global_2))
22+
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
2323
(func $_gmain (; has Stack IR ;) (result i32)
2424
(local $0 i32)
2525
(local $1 i32)
@@ -48,13 +48,13 @@ arrays › array_access2
4848
(local.get $0)
4949
(i32.const 7)
5050
)
51-
(global.set $global_0
51+
(global.set $x_1131
5252
(tuple.extract 0
5353
(tuple.make
5454
(local.get $0)
5555
(call $wimport_GRAIN$MODULE$runtime/gc_decRef
5656
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$decRef)
57-
(global.get $global_0)
57+
(global.get $x_1131)
5858
)
5959
)
6060
)
@@ -68,7 +68,7 @@ arrays › array_access2
6868
(i32.const 0)
6969
(i32.load offset=4
7070
(local.tee $1
71-
(global.get $global_0)
71+
(global.get $x_1131)
7272
)
7373
)
7474
)

0 commit comments

Comments
 (0)