Skip to content

Commit 529d66b

Browse files
authored
flambda-backend: Compile refactor (#1096)
1 parent df798c1 commit 529d66b

File tree

7 files changed

+78
-94
lines changed

7 files changed

+78
-94
lines changed

asmcomp/asmpackager.ml

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -111,36 +111,26 @@ let make_package_object ~ppf_dump members targetobj targetname coercion
111111
let compilation_unit = CU.create for_pack_prefix modname in
112112
let prefixname = Filename.remove_extension objtemp in
113113
let required_globals = CU.Set.empty in
114-
let program, middle_end =
115-
if Config.flambda then
116-
let main_module_block_size, code =
117-
Translmod.transl_package_flambda components coercion
118-
in
119-
let code = Simplif.simplify_lambda code in
120-
let program =
121-
{ Lambda.
122-
code;
123-
main_module_block_size;
124-
compilation_unit;
125-
required_globals;
126-
}
127-
in
128-
program, Flambda_middle_end.lambda_to_clambda
129-
else
130-
let main_module_block_size, code =
131-
Translmod.transl_store_package components
132-
compilation_unit coercion
133-
in
134-
let code = Simplif.simplify_lambda code in
135-
let program =
136-
{ Lambda.
137-
code;
138-
main_module_block_size;
139-
compilation_unit;
140-
required_globals;
141-
}
142-
in
143-
program, Closure_middle_end.lambda_to_clambda
114+
let transl_style : Translmod.compilation_unit_style =
115+
if Config.flambda || Config.flambda2 then Plain_block
116+
else Set_individual_fields
117+
in
118+
let main_module_block_size, code =
119+
Translmod.transl_package components compilation_unit coercion
120+
~style:transl_style
121+
in
122+
let code = Simplif.simplify_lambda code in
123+
let program =
124+
{ Lambda.
125+
code;
126+
main_module_block_size;
127+
compilation_unit;
128+
required_globals;
129+
}
130+
in
131+
let middle_end =
132+
if Config.flambda then Flambda_middle_end.lambda_to_clambda
133+
else Closure_middle_end.lambda_to_clambda
144134
in
145135
Asmgen.compile_implementation ~backend
146136
~prefixname

bytecomp/bytepackager.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ let build_global_target ~ppf_dump oc target_name members pos coercion =
161161
| PM_intf -> None
162162
| PM_impl _ -> Some (m.pm_name |> unit_of_name))
163163
members in
164-
let lam = Translmod.transl_package components compilation_unit coercion in
164+
let _size, lam =
165+
Translmod.transl_package components compilation_unit coercion
166+
~style:Set_global_to_block
167+
in
165168
if !Clflags.dump_rawlambda then
166169
Format.fprintf ppf_dump "%a@." Printlambda.lambda lam;
167170
let lam = Simplif.simplify_lambda lam in

driver/compile.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let interface ~source_file ~output_prefix =
3333
let to_bytecode i Typedtree.{structure; coercion; _} =
3434
(structure, coercion)
3535
|> Profile.(record transl)
36-
(Translmod.transl_implementation i.module_name)
36+
(Translmod.transl_implementation i.module_name ~style:Set_global_to_block)
3737
|> Profile.(record ~accumulate:true generate)
3838
(fun { Lambda.code = lambda; required_globals } ->
3939
lambda

driver/optcompile.ml

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,13 @@ let interface ~source_file ~output_prefix =
3030
~hook_typed_tree:(fun _ -> ())
3131
info
3232

33-
let (|>>) (x, y) f = (x, f y)
34-
3533
(** Native compilation backend for .ml files. *)
3634

37-
let flambda i backend Typedtree.{structure; coercion; _} =
35+
let compile i ~backend ~middle_end ~transl_style
36+
Typedtree.{structure; coercion; _} =
3837
(structure, coercion)
3938
|> Profile.(record transl)
40-
(Translmod.transl_implementation_flambda i.module_name)
41-
|> Profile.(record generate)
42-
(fun {Lambda.compilation_unit; main_module_block_size;
43-
required_globals; code } ->
44-
((compilation_unit, main_module_block_size), code)
45-
|>> print_if i.ppf_dump Clflags.dump_rawlambda Printlambda.lambda
46-
|>> Simplif.simplify_lambda
47-
|>> print_if i.ppf_dump Clflags.dump_lambda Printlambda.lambda
48-
|> (fun ((compilation_unit, main_module_block_size), code) ->
49-
let program : Lambda.program =
50-
{ Lambda.
51-
compilation_unit;
52-
main_module_block_size;
53-
required_globals;
54-
code;
55-
}
56-
in
57-
Asmgen.compile_implementation
58-
~backend
59-
~prefixname:i.output_prefix
60-
~middle_end:Flambda_middle_end.lambda_to_clambda
61-
~ppf_dump:i.ppf_dump
62-
program);
63-
Compilenv.save_unit_info (cmx i))
64-
65-
let clambda i backend Typedtree.{structure; coercion; _} =
66-
Clflags.set_oclassic ();
67-
(structure, coercion)
68-
|> Profile.(record transl)
69-
(Translmod.transl_store_implementation i.module_name)
39+
(Translmod.transl_implementation i.module_name ~style:transl_style)
7040
|> print_if i.ppf_dump Clflags.dump_rawlambda Printlambda.program
7141
|> Profile.(record generate)
7242
(fun program ->
@@ -76,10 +46,19 @@ let clambda i backend Typedtree.{structure; coercion; _} =
7646
|> Asmgen.compile_implementation
7747
~backend
7848
~prefixname:i.output_prefix
79-
~middle_end:Closure_middle_end.lambda_to_clambda
49+
~middle_end
8050
~ppf_dump:i.ppf_dump;
8151
Compilenv.save_unit_info (cmx i))
8252

53+
let flambda i backend typed =
54+
compile i typed ~backend ~transl_style:Plain_block
55+
~middle_end:Flambda_middle_end.lambda_to_clambda
56+
57+
let clambda i backend typed =
58+
Clflags.set_oclassic ();
59+
compile i typed ~backend ~transl_style:Set_individual_fields
60+
~middle_end:Closure_middle_end.lambda_to_clambda
61+
8362
(* Emit assembly directly from Linear IR *)
8463
let emit i =
8564
Compilenv.reset i.module_name;

lambda/translmod.ml

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,12 @@ let required_globals ~flambda body =
910910

911911
(* Compile an implementation *)
912912

913-
let transl_implementation_flambda compilation_unit (str, cc) =
913+
type compilation_unit_style =
914+
| Plain_block
915+
| Set_global_to_block
916+
| Set_individual_fields
917+
918+
let transl_implementation_plain_block compilation_unit (str, cc) =
914919
reset_labels ();
915920
primitive_declarations := [];
916921
Translprim.clear_used_primitives ();
@@ -928,9 +933,9 @@ let transl_implementation_flambda compilation_unit (str, cc) =
928933
required_globals = required_globals ~flambda:true body;
929934
code = body }
930935

931-
let transl_implementation module_name (str, cc) =
936+
let transl_implementation_set_global module_name (str, cc) =
932937
let implementation =
933-
transl_implementation_flambda module_name (str, cc)
938+
transl_implementation_plain_block module_name (str, cc)
934939
in
935940
let code =
936941
Lprim (Psetglobal implementation.compilation_unit, [implementation.code],
@@ -1522,7 +1527,7 @@ let transl_store_phrases module_name str =
15221527
in
15231528
transl_store_gen ~scopes module_name (str,Tcoerce_none) true
15241529

1525-
let transl_store_implementation compilation_unit (str, restr) =
1530+
let transl_implementation_set_fields compilation_unit (str, restr) =
15261531
let s = !transl_store_subst in
15271532
transl_store_subst := Ident.Map.empty;
15281533
let scopes = enter_compilation_unit ~scopes:empty_scopes compilation_unit in
@@ -1535,6 +1540,12 @@ let transl_store_implementation compilation_unit (str, restr) =
15351540
compilation_unit;
15361541
required_globals = required_globals ~flambda:true code }
15371542

1543+
let transl_implementation compilation_unit impl ~style =
1544+
match style with
1545+
| Plain_block -> transl_implementation_plain_block compilation_unit impl
1546+
| Set_global_to_block -> transl_implementation_set_global compilation_unit impl
1547+
| Set_individual_fields -> transl_implementation_set_fields compilation_unit impl
1548+
15381549
(* Compile a toplevel phrase *)
15391550

15401551
let toploop_unit = Compilation_unit.of_string "Toploop"
@@ -1736,7 +1747,7 @@ let get_component = function
17361747
None -> Lconst const_unit
17371748
| Some id -> Lprim(Pgetglobal id, [], Loc_unknown)
17381749

1739-
let transl_package_flambda component_names coercion =
1750+
let transl_package_plain_block component_names coercion =
17401751
let size =
17411752
match coercion with
17421753
| Tcoerce_none -> List.length component_names
@@ -1751,13 +1762,9 @@ let transl_package_flambda component_names coercion =
17511762
List.map get_component component_names,
17521763
Loc_unknown))
17531764

1754-
let transl_package component_names target_name coercion =
1755-
let components =
1756-
Lprim(Pmakeblock(0, Immutable, None, alloc_heap),
1757-
List.map get_component component_names, Loc_unknown) in
1758-
Lprim(Psetglobal target_name,
1759-
[apply_coercion Loc_unknown Strict coercion components],
1760-
Loc_unknown)
1765+
let transl_package_set_global component_names target_name coercion =
1766+
let size, block = transl_package_plain_block component_names coercion in
1767+
size, Lprim(Psetglobal target_name, [block], Loc_unknown)
17611768
(*
17621769
let components =
17631770
match coercion with
@@ -1774,7 +1781,7 @@ let transl_package component_names target_name coercion =
17741781
Lprim(Psetglobal target_name, [Lprim(Pmakeblock(0, Immutable), components)])
17751782
*)
17761783

1777-
let transl_store_package component_names target_name coercion =
1784+
let transl_package_set_fields component_names target_name coercion =
17781785
let rec make_sequence fn pos arg =
17791786
match arg with
17801787
[] -> lambda_unit
@@ -1819,6 +1826,15 @@ let transl_store_package component_names target_name coercion =
18191826
*)
18201827
| _ -> assert false
18211828

1829+
let transl_package component_names target_name coercion ~style =
1830+
match style with
1831+
| Plain_block ->
1832+
transl_package_plain_block component_names coercion
1833+
| Set_global_to_block ->
1834+
transl_package_set_global component_names target_name coercion
1835+
| Set_individual_fields ->
1836+
transl_package_set_fields component_names target_name coercion
1837+
18221838
(* Error report *)
18231839

18241840
open Format

lambda/translmod.mli

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,21 @@
1919
open Typedtree
2020
open Lambda
2121

22+
type compilation_unit_style =
23+
| Plain_block (* Flambda *)
24+
| Set_global_to_block (* Bytecode *)
25+
| Set_individual_fields (* Closure *)
26+
2227
val transl_implementation:
23-
Compilation_unit.t -> structure * module_coercion -> Lambda.program
28+
Compilation_unit.t -> structure * module_coercion
29+
-> style:compilation_unit_style -> Lambda.program
2430
val transl_store_phrases: Compilation_unit.t -> structure -> int * lambda
25-
val transl_store_implementation:
26-
Compilation_unit.t -> structure * module_coercion -> Lambda.program
27-
28-
val transl_implementation_flambda:
29-
Compilation_unit.t -> structure * module_coercion -> Lambda.program
3031

3132
val transl_toplevel_definition: structure -> lambda
33+
3234
val transl_package:
3335
Compilation_unit.t option list -> Compilation_unit.t -> module_coercion
34-
-> lambda
35-
val transl_store_package:
36-
Compilation_unit.t option list -> Compilation_unit.t -> module_coercion
37-
-> int * lambda
38-
39-
val transl_package_flambda:
40-
Compilation_unit.t option list -> module_coercion -> int * lambda
36+
-> style:compilation_unit_style -> int * lambda
4137

4238
val toplevel_name: Ident.t -> string
4339
val nat_toplevel_name: Ident.t -> Compilation_unit.t * int

toplevel/native/topeval.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ let execute_phrase print_outcome ppf phr =
207207
if Config.flambda then
208208
let { Lambda.compilation_unit; main_module_block_size = size;
209209
required_globals; code = res } =
210-
Translmod.transl_implementation_flambda phrase_comp_unit
211-
(str, Tcoerce_none)
210+
Translmod.transl_implementation phrase_comp_unit (str, Tcoerce_none)
211+
~style:Plain_block
212212
in
213213
remember compilation_unit 0 sg';
214214
compilation_unit, close_phrase res, required_globals, size

0 commit comments

Comments
 (0)