Skip to content

Commit 2691ae7

Browse files
committed
feat: Refactor to compcore
1 parent c79f8fe commit 2691ae7

File tree

4 files changed

+87
-64
lines changed

4 files changed

+87
-64
lines changed

compiler/src/codegen/compcore.re

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ open Grain_utils;
88
open Comp_utils;
99
open Comp_wasm_prim;
1010

11+
module StringSet = Set.Make(String);
12+
1113
let sources: ref(list((Expression.t, Grain_parsing.Location.t))) = ref([]);
1214

1315
/** Environment */
@@ -22,6 +24,7 @@ type codegen_env = {
2224
/* Allocated closures which need backpatching */
2325
backpatches: ref(list((Expression.t, closure_data))),
2426
required_imports: list(import),
27+
raw_resolutions: ref(StringSet.t),
2528
global_import_resolutions: Hashtbl.t(string, string),
2629
func_import_resolutions: Hashtbl.t(string, string),
2730
compilation_mode: Config.compilation_mode,
@@ -90,6 +93,7 @@ let init_codegen_env =
9093
},
9194
backpatches: ref([]),
9295
required_imports: [],
96+
raw_resolutions: ref(StringSet.empty),
9397
global_import_resolutions,
9498
func_import_resolutions,
9599
compilation_mode: Normal,
@@ -2782,6 +2786,14 @@ and compile_instr = (wasm_mod, env, instr) =>
27822786
compiled_args,
27832787
Type.create(Array.of_list(List.map(wasm_type, retty))),
27842788
);
2789+
} else if (StringSet.mem(func_name, env.raw_resolutions^)) {
2790+
// Deduplicated imports; call resolved name directly
2791+
Expression.Call.make(
2792+
wasm_mod,
2793+
resolved_name,
2794+
compiled_args,
2795+
Type.create(Array.of_list(List.map(wasm_type, retty))),
2796+
);
27852797
} else {
27862798
// Raw function resolved to Grain function; inject closure argument
27872799
let closure_global = resolve_global(~env, func_name);
@@ -3070,7 +3082,7 @@ let compute_table_size = (env, {function_table_elements}) => {
30703082
List.length(function_table_elements);
30713083
};
30723084

3073-
let compile_imports = (wasm_mod, env, {imports}) => {
3085+
let compile_imports = (wasm_mod, env, {imports}, import_map) => {
30743086
let compile_module_name = name =>
30753087
fun
30763088
| MImportWasm => name
@@ -3081,7 +3093,6 @@ let compile_imports = (wasm_mod, env, {imports}) => {
30813093
| (MImportGrain, MGlobalImport(_)) => "GRAIN$EXPORT$" ++ name
30823094
| _ => name
30833095
};
3084-
30853096
let compile_import = ({mimp_id, mimp_mod, mimp_name, mimp_type, mimp_kind}) => {
30863097
let module_name = compile_module_name(mimp_mod, mimp_kind);
30873098
let item_name = compile_import_name(mimp_name, mimp_kind, mimp_type);
@@ -3090,37 +3101,52 @@ let compile_imports = (wasm_mod, env, {imports}) => {
30903101
| MImportGrain => get_grain_imported_name(mimp_mod, mimp_id)
30913102
| MImportWasm => Ident.unique_name(mimp_id)
30923103
};
3093-
switch (mimp_kind, mimp_type) {
3094-
| (MImportGrain, MGlobalImport(ty, mut)) =>
3095-
Import.add_global_import(
3096-
wasm_mod,
3097-
internal_name,
3098-
module_name,
3099-
item_name,
3100-
wasm_type(ty),
3101-
mut,
3102-
)
3103-
| (_, MFuncImport(args, ret)) =>
3104-
let proc_list = l =>
3105-
Type.create @@ Array.of_list @@ List.map(wasm_type, l);
3106-
Import.add_function_import(
3107-
wasm_mod,
3108-
internal_name,
3109-
module_name,
3110-
item_name,
3111-
proc_list(args),
3112-
proc_list(ret),
3113-
);
3114-
| (_, MGlobalImport(typ, mut)) =>
3115-
let typ = wasm_type(typ);
3116-
Import.add_global_import(
3117-
wasm_mod,
3118-
internal_name,
3119-
module_name,
3120-
item_name,
3121-
typ,
3122-
mut,
3123-
);
3104+
let imp_key = (module_name, item_name, mimp_kind, mimp_type);
3105+
switch (Hashtbl.find_opt(import_map, imp_key)) {
3106+
| Some(name) when mimp_kind == MImportWasm =>
3107+
// Deduplicate wasm imports by resolving them to the previously imported name
3108+
let linked_name = linked_name(~env, internal_name);
3109+
switch (mimp_type) {
3110+
| MFuncImport(_, _) =>
3111+
Hashtbl.add(env.func_import_resolutions, linked_name, name)
3112+
| MGlobalImport(_, _) =>
3113+
Hashtbl.add(env.global_import_resolutions, linked_name, name)
3114+
};
3115+
env.raw_resolutions := StringSet.add(linked_name, env.raw_resolutions^);
3116+
| _ =>
3117+
Hashtbl.add(import_map, imp_key, internal_name);
3118+
switch (mimp_kind, mimp_type) {
3119+
| (MImportGrain, MGlobalImport(ty, mut)) =>
3120+
Import.add_global_import(
3121+
wasm_mod,
3122+
internal_name,
3123+
module_name,
3124+
item_name,
3125+
wasm_type(ty),
3126+
mut,
3127+
)
3128+
| (_, MFuncImport(args, ret)) =>
3129+
let proc_list = l =>
3130+
Type.create @@ Array.of_list @@ List.map(wasm_type, l);
3131+
Import.add_function_import(
3132+
wasm_mod,
3133+
internal_name,
3134+
module_name,
3135+
item_name,
3136+
proc_list(args),
3137+
proc_list(ret),
3138+
);
3139+
| (_, MGlobalImport(typ, mut)) =>
3140+
let typ = wasm_type(typ);
3141+
Import.add_global_import(
3142+
wasm_mod,
3143+
internal_name,
3144+
module_name,
3145+
item_name,
3146+
typ,
3147+
mut,
3148+
);
3149+
};
31243150
};
31253151
};
31263152

@@ -3472,12 +3498,14 @@ let compile_wasm_module =
34723498
Type.funcref,
34733499
);
34743500

3501+
let import_map = Hashtbl.create(10);
3502+
34753503
let compile_one = (dep_id, prog: mash_code) => {
34763504
let env = {...env, dep_id, compilation_mode: prog.compilation_mode};
3505+
ignore @@ compile_imports(wasm_mod, env, prog, import_map);
34773506
ignore @@ compile_globals(wasm_mod, env, prog);
34783507
ignore @@ compile_functions(wasm_mod, env, prog);
34793508
ignore @@ compile_exports(wasm_mod, env, prog);
3480-
ignore @@ compile_imports(wasm_mod, env, prog);
34813509
ignore @@ compile_tables(wasm_mod, env, prog);
34823510
};
34833511

@@ -3499,7 +3527,10 @@ let compile_wasm_module =
34993527

35003528
validate_module(~name?, wasm_mod);
35013529

3502-
Optimize_mod.optimize(wasm_mod);
3530+
switch (Config.profile^) {
3531+
| Some(Release) => Optimize_mod.optimize(wasm_mod)
3532+
| None => ()
3533+
};
35033534
wasm_mod;
35043535
};
35053536

compiler/src/codegen/linkedtree.re

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ let link = main_mashtree => {
109109
(resolved_module, import.mimp_name),
110110
);
111111
let import_name =
112-
Printf.sprintf(
113-
"%s_%d",
114-
Ident.unique_name(import.mimp_id),
115-
dep_id^,
116-
);
112+
internal_name(Ident.unique_name(import.mimp_id), dep_id^);
117113
Option.iter(
118114
global =>
119115
Hashtbl.add(global_import_resolutions, import_name, global),

compiler/src/codegen/optimize_mod.re

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -227,29 +227,25 @@ let optimize =
227227
~shrink_level=default_shrink_level,
228228
wasm_mod,
229229
) => {
230-
let passes =
231-
switch (Config.profile^) {
232-
| Some(Release) =>
233-
List.concat([
234-
default_global_optimization_pre_passes(
235-
~optimize_level,
236-
~shrink_level,
237-
wasm_mod,
238-
),
239-
default_function_optimization_passes(
240-
~optimize_level,
241-
~shrink_level,
242-
wasm_mod,
243-
),
244-
default_global_optimization_post_passes(
245-
~optimize_level,
246-
~shrink_level,
247-
wasm_mod,
248-
),
249-
])
250-
| None => [Passes.duplicate_import_elimination]
251-
};
252230
// Translation of https://github.com/WebAssembly/binaryen/blob/version_107/src/passes/pass.cpp#L441-L445
231+
let default_optimizations_passes =
232+
List.concat([
233+
default_global_optimization_pre_passes(
234+
~optimize_level,
235+
~shrink_level,
236+
wasm_mod,
237+
),
238+
default_function_optimization_passes(
239+
~optimize_level,
240+
~shrink_level,
241+
wasm_mod,
242+
),
243+
default_global_optimization_post_passes(
244+
~optimize_level,
245+
~shrink_level,
246+
wasm_mod,
247+
),
248+
]);
253249

254-
Module.run_passes(wasm_mod, passes);
250+
Module.run_passes(wasm_mod, default_optimizations_passes);
255251
};

compiler/test/suites/basic_functionality.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,6 @@ describe("basic functionality", ({test, testSkip}) => {
377377
~config_fn=smallestFileConfig,
378378
"smallest_grain_program",
379379
"",
380-
6507,
380+
6540,
381381
);
382382
});

0 commit comments

Comments
 (0)