Skip to content

Commit f93e03f

Browse files
authored
Backend_impl / Compilenv changes for Flambda 2 (#97)
1 parent 0c1f620 commit f93e03f

File tree

8 files changed

+177
-23
lines changed

8 files changed

+177
-23
lines changed

dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
memtrace
122122
flambda2
123123
flambda2_to_cmm
124+
flambda2_backend_intf
124125
ocamloptcomp
125126
ocamlcommon
126127
stdlib)
@@ -139,6 +140,7 @@
139140
memtrace
140141
flambda2
141142
flambda2_to_cmm
143+
flambda2_backend_intf
142144
ocamloptcomp
143145
ocamlcommon
144146
stdlib

middle_end/compilenv.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ let get_global_info global_ident = (
217217
end
218218
)
219219

220+
let get_global_info' id =
221+
match get_global_info id with
222+
| None -> None
223+
| Some ui -> Some ui.ui_export_info
224+
220225
let cache_unit_info ui =
221226
Hashtbl.add global_infos_table ui.ui_name (Some ui)
222227

@@ -296,6 +301,10 @@ let set_export_info export_info =
296301
assert(Config.flambda);
297302
current_unit.ui_export_info <- Flambda1 export_info
298303

304+
let flambda2_set_export_info export_info =
305+
assert(Config.flambda2);
306+
current_unit.ui_export_info <- Flambda2 (Some export_info)
307+
299308
let approx_for_global comp_unit =
300309
let id = Compilation_unit.get_persistent_ident comp_unit in
301310
if (Compilation_unit.equal

middle_end/compilenv.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ val approx_for_global: Compilation_unit.t -> Export_info.t option
9595
(* Loads the exported information declaring the compilation_unit
9696
flambda-only *)
9797

98+
val get_global_info' : Ident.t -> Cmx_format.export_info option
99+
(* Middle-end-agnostic means of getting the export info found in the
100+
.cmx file of the given unit. *)
101+
102+
val flambda2_set_export_info : Flambda2.Flambda_cmx_format.t -> unit
103+
(* Set the export information for the current unit (Flambda 2 only). *)
104+
98105
val need_curry_fun: int -> unit
99106
val need_apply_fun: int -> unit
100107
val need_send_fun: int -> unit

middle_end/flambda2/backend_intf/dune

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(include_subdirs no)
2+
3+
(library
4+
(name flambda2_backend_intf)
5+
(wrapped false)
6+
(flags (:standard -principal -nostdlib -open Flambda2))
7+
(libraries stdlib ocamlcommon ocamlbytecomp ocamloptcomp
8+
flambda2_compilenv_deps flambda2)
9+
)
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* OCaml *)
4+
(* *)
5+
(* Pierre Chambart, OCamlPro *)
6+
(* Mark Shinwell and Leo White, Jane Street Europe *)
7+
(* *)
8+
(* Copyright 2013--2021 OCamlPro SAS *)
9+
(* Copyright 2014--2021 Jane Street Group LLC *)
10+
(* *)
11+
(* All rights reserved. This file is distributed under the terms of *)
12+
(* the GNU Lesser General Public License version 2.1, with the *)
13+
(* special exception on linking described in the file LICENSE. *)
14+
(* *)
15+
(**************************************************************************)
16+
17+
[@@@ocaml.warning "+a-30-40-41-42"]
18+
19+
module Flambda1_compilation_unit = Compilation_unit
20+
module Flambda1_linkage_name = Linkage_name
21+
22+
module Compilation_unit = struct
23+
include Flambda2_compilenv_deps.Compilation_unit
24+
25+
let of_flambda1_compilation_unit comp_unit =
26+
let ident = Flambda1_compilation_unit.get_persistent_ident comp_unit in
27+
let linkage_name =
28+
comp_unit
29+
|> Flambda1_compilation_unit.get_linkage_name
30+
|> Flambda1_linkage_name.to_string
31+
|> Flambda2_compilenv_deps.Linkage_name.create
32+
in
33+
create ident linkage_name
34+
end
35+
36+
module Linkage_name = Flambda2_compilenv_deps.Linkage_name
37+
module Symbol = Flambda2_compilenv_deps.Symbol
38+
39+
let symbol_for_module_block id =
40+
assert (Ident.global id);
41+
assert (not (Ident.is_predef id));
42+
let comp_unit =
43+
Compilenv.unit_for_global id
44+
|> Compilation_unit.of_flambda1_compilation_unit
45+
in
46+
Symbol.unsafe_create
47+
comp_unit
48+
(Linkage_name.create (Compilenv.symbol_for_global id))
49+
50+
let symbol_for_global' ?comp_unit id =
51+
if Ident.global id && not (Ident.is_predef id) then
52+
symbol_for_module_block id
53+
else
54+
let comp_unit =
55+
match comp_unit with
56+
| Some comp_unit -> comp_unit
57+
| None ->
58+
if Ident.is_predef id then Compilation_unit.predefined_exception ()
59+
else Compilation_unit.get_current_exn ()
60+
in
61+
Symbol.unsafe_create
62+
comp_unit
63+
(Linkage_name.create (Compilenv.symbol_for_global id))
64+
65+
let find_predef_exn name =
66+
let matches ident = String.equal (Ident.name ident) name in
67+
match List.find matches Predef.all_predef_exns with
68+
| exception Not_found ->
69+
Misc.fatal_errorf "Cannot find predef exn '%s'" name
70+
| ident -> ident
71+
72+
let division_by_zero =
73+
symbol_for_global'
74+
~comp_unit:(Compilation_unit.predefined_exception ())
75+
Predef.ident_division_by_zero
76+
77+
let invalid_argument =
78+
symbol_for_global'
79+
~comp_unit:(Compilation_unit.predefined_exception ())
80+
(find_predef_exn "Invalid_argument")
81+
82+
let all_predefined_exception_symbols =
83+
Predef.all_predef_exns
84+
|> List.map (fun ident ->
85+
symbol_for_global' ~comp_unit:(Compilation_unit.predefined_exception ())
86+
ident)
87+
|> Symbol.Set.of_list
88+
89+
let () =
90+
assert (Symbol.Set.mem division_by_zero all_predefined_exception_symbols);
91+
assert (Symbol.Set.mem invalid_argument all_predefined_exception_symbols)
92+
93+
let symbol_for_global' id : Symbol.t = symbol_for_global' id
94+
95+
let size_int = Arch.size_int
96+
let big_endian = Arch.big_endian
97+
98+
let max_sensible_number_of_arguments =
99+
Proc.max_arguments_for_tailcalls - 1
100+
101+
let set_global_info info = Compilenv.flambda2_set_export_info info
102+
103+
let get_global_info comp_unit =
104+
(* The Flambda simplifier should have returned the typing information
105+
for the predefined exception compilation unit before getting here. *)
106+
assert (not (Compilation_unit.is_predefined_exception comp_unit));
107+
if Compilation_unit.is_external_symbols comp_unit then None
108+
else
109+
let id =
110+
(* CR mshinwell: Unsure how to construct this properly. Also see CR
111+
in Closure_conversion about the linkage names of module blocks *)
112+
Compilation_unit.get_persistent_ident comp_unit
113+
in
114+
match Compilenv.get_global_info' id with
115+
| None | Some (Flambda2 None) -> None
116+
| Some (Flambda2 (Some info)) -> Some info
117+
| Some (Clambda _) ->
118+
(* CR mshinwell: This should be a user error, not a fatal error.
119+
Same below. *)
120+
Misc.fatal_errorf "The .cmx file for unit %a was compiled with \
121+
the Closure middle-end, not Flambda 2, and cannot be loaded"
122+
Compilation_unit.print comp_unit
123+
| Some (Flambda1 _) ->
124+
Misc.fatal_errorf "The .cmx file for unit %a was compiled with \
125+
the Flambda 1 middle-end, not Flambda 2, and cannot be loaded"
126+
Compilation_unit.print comp_unit
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* OCaml *)
4+
(* *)
5+
(* Pierre Chambart, OCamlPro *)
6+
(* Mark Shinwell and Leo White, Jane Street Europe *)
7+
(* *)
8+
(* Copyright 2013--2021 OCamlPro SAS *)
9+
(* Copyright 2014--2021 Jane Street Group LLC *)
10+
(* *)
11+
(* All rights reserved. This file is distributed under the terms of *)
12+
(* the GNU Lesser General Public License version 2.1, with the *)
13+
(* special exception on linking described in the file LICENSE. *)
14+
(* *)
15+
(**************************************************************************)
16+
17+
[@@@ocaml.warning "+a-30-40-41-42"]
18+
19+
include Flambda_backend_intf.S

middle_end/flambda2/flambda_backend_intf.mli

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,18 @@
1616

1717
[@@@ocaml.warning "+a-4-9-30-40-41-42"]
1818

19-
(* XXX This shouldn't be needed once the Symbol refactoring is in; then we
20-
can use the same Compilation unit and Symbol types for both Flambda 1 and 2.
21-
*)
22-
23-
(** Knowledge that the middle end needs about the backend. *)
19+
(** Knowledge that the Flambda 2 middle end needs about the backend. *)
2420

2521
module type S = sig
2622
(** Compute the symbol for the given identifier. *)
2723
val symbol_for_global' : (Ident.t -> Symbol.t)
2824

29-
(*
30-
(** If the given approximation is that of a symbol (Value_symbol) or an
31-
external (Value_extern), attempt to find a more informative
32-
approximation from a previously-written compilation artifact. In the
33-
native code backend, for example, this might consult a .cmx file. *)
34-
val really_import_approx : Simple_value_approx.t -> Simple_value_approx.t
35-
*)
36-
3725
val all_predefined_exception_symbols : Symbol.Set.t
3826

3927
val division_by_zero : Symbol.t
4028

4129
val invalid_argument : Symbol.t
4230

43-
(*
44-
val import_symbol : Symbol.t -> Simple_value_approx.t
45-
*)
46-
47-
val closure_symbol : Closure_id.t -> Symbol.t
48-
4931
(** The natural size of an integer on the target architecture
5032
(cf. [Arch.size_int] in the native code backend). *)
5133
val size_int : int
@@ -60,5 +42,7 @@ module type S = sig
6042
val max_sensible_number_of_arguments : int
6143

6244
val set_global_info : Flambda_cmx_format.t -> unit
63-
val get_global_info : Compilation_unit.t -> Flambda_cmx_format.t option
45+
val get_global_info
46+
: Flambda2_compilenv_deps.Compilation_unit.t
47+
-> Flambda_cmx_format.t option
6448
end

middle_end/flambda2/to_cmm/un_cps.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,9 +1511,7 @@ let unit (middle_end_result : Flambda_middle_end.middle_end_result) =
15111511
offsets *)
15121512
| Some cmx ->
15131513
let _cmx = Flambda_cmx_format.with_exported_offsets cmx offsets in
1514-
(* CR mshinwell: wire this in *)
1515-
Misc.fatal_error "To do"
1516-
(* Compilenv.set_global_info (Flambda (Some cmx)) *)
1514+
Compilenv.flambda2_set_export_info cmx
15171515
end;
15181516
let used_closure_vars = Flambda_unit.used_closure_vars unit in
15191517
let dummy_k = Continuation.create () in

0 commit comments

Comments
 (0)