Skip to content

Commit 84a3d7e

Browse files
committed
Introduce Import_info
1 parent b03bf0b commit 84a3d7e

Some content is hidden

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

45 files changed

+747
-471
lines changed

backend/asmlibrarian.ml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,28 @@ let create_archive file_list lib_name =
6363
(fun file_name (unit, crc) ->
6464
Asmlink.check_consistency file_name unit crc)
6565
file_list descr_list;
66-
let cmis = Asmlink.extract_crc_interfaces () |> Array.of_list in
67-
let cmxs = Asmlink.extract_crc_implementations () |> Array.of_list in
66+
let cmis = Asmlink.extract_crc_interfaces () in
67+
let cmxs = Asmlink.extract_crc_implementations () in
68+
(* CR mshinwell: see comment in compilenv.ml
69+
let cmxs =
70+
Compilenv.ensure_sharing_between_cmi_and_cmx_imports cmis cmxs
71+
in
72+
*)
73+
let cmis = Array.of_list cmis in
74+
let cmxs = Array.of_list cmxs in
6875
let cmi_index = Compilation_unit.Name.Tbl.create 42 in
69-
Array.iteri
70-
(fun i (name, _crc) -> Compilation_unit.Name.Tbl.add cmi_index name i)
76+
Array.iteri (fun i import ->
77+
Compilation_unit.Name.Tbl.add cmi_index (Import_info.name import) i)
7178
cmis;
7279
let cmx_index = Compilation_unit.Tbl.create 42 in
73-
Array.iteri
74-
(fun i (name, _crc) -> Compilation_unit.Tbl.add cmx_index name i)
80+
Array.iteri (fun i import ->
81+
Compilation_unit.Tbl.add cmx_index (Import_info.cu import) i)
7582
cmxs;
7683
let genfns = Cmm_helpers.Generic_fns_tbl.make () in
77-
let mk_bitmap arr ix entries ~find =
84+
let mk_bitmap arr ix entries ~find ~get_name =
7885
let module B = Misc.Bitmap in
7986
let b = B.make (Array.length arr) in
80-
entries |> List.iter (fun (name, _crc) -> B.set b (find ix name));
87+
List.iter (fun import -> B.set b (find ix (get_name import))) entries;
8188
b
8289
in
8390
let units =
@@ -89,10 +96,12 @@ let create_archive file_list lib_name =
8996
li_force_link = unit.ui_force_link;
9097
li_imports_cmi =
9198
mk_bitmap cmis cmi_index unit.ui_imports_cmi
92-
~find:Compilation_unit.Name.Tbl.find;
99+
~find:Compilation_unit.Name.Tbl.find
100+
~get_name:Import_info.name;
93101
li_imports_cmx =
94102
mk_bitmap cmxs cmx_index unit.ui_imports_cmx
95-
~find:Compilation_unit.Tbl.find })
103+
~find:Compilation_unit.Tbl.find
104+
~get_name:Import_info.cu })
96105
descr_list
97106
in
98107
let infos =

backend/asmlink.ml

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ let cmx_required = ref ([] : CU.t list)
6060
let check_cmi_consistency file_name cmis =
6161
try
6262
Array.iter
63-
(fun (name, crco) ->
63+
(fun import ->
64+
let name = Import_info.name import in
65+
let crco = Import_info.crc_with_unit import in
6466
CU.Name.Tbl.replace interfaces name ();
6567
match crco with
6668
None -> ()
@@ -77,7 +79,9 @@ let check_cmi_consistency file_name cmis =
7779
let check_cmx_consistency file_name cmxs =
7880
try
7981
Array.iter
80-
(fun (name, crco) ->
82+
(fun import ->
83+
let name = Import_info.cu import in
84+
let crco = Import_info.crc import in
8185
implementations := name :: !implementations;
8286
match crco with
8387
None ->
@@ -110,13 +114,16 @@ let check_consistency ~unit cmis cmxs =
110114

111115
let extract_crc_interfaces () =
112116
CU.Name.Tbl.fold (fun name () crcs ->
113-
(name, Cmi_consistbl.find crc_interfaces name) :: crcs)
117+
let crc_with_unit = Cmi_consistbl.find crc_interfaces name in
118+
Import_info.create name ~crc_with_unit :: crcs)
114119
interfaces
115120
[]
116121

117122
let extract_crc_implementations () =
118123
Cmx_consistbl.extract !implementations crc_implementations
119-
|> List.map (fun (name, crco) -> name, Option.map snd crco)
124+
|> List.map (fun (cu, crc) ->
125+
let crc = Option.map (fun ((), crc) -> crc) crc in
126+
Import_info.create_normal cu ~crc)
120127

121128
(* Add C objects and options and "custom" info from a library descriptor.
122129
See bytecomp/bytelink.ml for comments on the order of C objects. *)
@@ -151,7 +158,8 @@ let is_required name =
151158
try ignore (Hashtbl.find missing_globals name); true
152159
with Not_found -> false
153160

154-
let add_required by (name, _crc) =
161+
let add_required by import =
162+
let name = Import_info.cu import in
155163
try
156164
let rq = Hashtbl.find missing_globals name in
157165
rq := by :: !rq
@@ -206,16 +214,16 @@ let scan_file ~shared genfns file (objfiles, tolink) =
206214
| Unit (file_name,info,crc) ->
207215
(* This is a .cmx file. It must be linked in any case. *)
208216
remove_required info.ui_unit;
209-
List.iter (fun (name, crc) ->
210-
add_required (file_name, None) (name, crc))
217+
List.iter (fun import ->
218+
add_required (file_name, None) import)
211219
info.ui_imports_cmx;
212220
let dynunit : Cmxs_format.dynunit option =
213221
if not shared then None else
214222
Some { dynu_name = info.ui_unit;
215223
dynu_crc = crc;
216224
dynu_defines = info.ui_defines;
217-
dynu_imports_cmi = info.ui_imports_cmi;
218-
dynu_imports_cmx = info.ui_imports_cmx }
225+
dynu_imports_cmi = info.ui_imports_cmi |> Array.of_list;
226+
dynu_imports_cmx = info.ui_imports_cmx |> Array.of_list }
219227
in
220228
let unit =
221229
{ name = info.ui_unit;
@@ -261,8 +269,8 @@ let scan_file ~shared genfns file (objfiles, tolink) =
261269
remove_required info.li_name;
262270
let req_by = (file_name, Some li_name) in
263271
info.li_imports_cmx |> Misc.Bitmap.iter (fun i ->
264-
let modname, digest = infos.lib_imports_cmx.(i) in
265-
add_required req_by (modname, digest));
272+
let import = infos.lib_imports_cmx.(i) in
273+
add_required req_by import);
266274
let imports_list tbl bits =
267275
List.init (Array.length tbl) (fun i ->
268276
if Misc.Bitmap.get bits i then Some tbl.(i) else None)
@@ -275,9 +283,11 @@ let scan_file ~shared genfns file (objfiles, tolink) =
275283
dynu_crc = info.li_crc;
276284
dynu_defines = info.li_defines;
277285
dynu_imports_cmi =
278-
imports_list infos.lib_imports_cmi info.li_imports_cmi;
286+
imports_list infos.lib_imports_cmi info.li_imports_cmi
287+
|> Array.of_list;
279288
dynu_imports_cmx =
280-
imports_list infos.lib_imports_cmx info.li_imports_cmx }
289+
imports_list infos.lib_imports_cmx info.li_imports_cmx
290+
|> Array.of_list }
281291
in
282292
let unit =
283293
{ name = info.li_name;

backend/asmlink.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ val call_linker_shared: string list -> string -> unit
2727

2828
val reset : unit -> unit
2929
val check_consistency: filepath -> Cmx_format.unit_infos -> Digest.t -> unit
30-
val extract_crc_interfaces: unit -> Cmx_format.import_info_cmi list
31-
val extract_crc_implementations: unit -> Cmx_format.import_info_cmx list
30+
val extract_crc_interfaces: unit -> Import_info.t list
31+
val extract_crc_implementations: unit -> Import_info.t list
3232

3333
type error =
3434
| File_not_found of filepath

backend/asmpackager.ml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ let check_units members =
7171
| PM_intf -> ()
7272
| PM_impl infos ->
7373
List.iter
74-
(fun (unit, _) ->
74+
(fun import ->
75+
let unit = Import_info.cu import in
7576
let name = CU.name unit in
7677
if List.mem name forbidden
7778
then raise(Error(Forward_reference(mb.pm_file, name))))
@@ -202,9 +203,9 @@ let get_approx ui : Clambda.value_approximation =
202203
let build_package_cmx members cmxfile =
203204
let unit_names =
204205
List.map (fun m -> m.pm_name) members in
205-
let filter ~get_name lst =
206-
List.filter (fun (name, _crc) ->
207-
not (List.mem (get_name name) unit_names)) lst in
206+
let filter lst =
207+
List.filter (fun import ->
208+
not (List.mem (Import_info.name import) unit_names)) lst in
208209
let union lst =
209210
List.fold_left
210211
(List.fold_left
@@ -247,10 +248,11 @@ let build_package_cmx members cmxfile =
247248
List.flatten (List.map (fun info -> info.ui_defines) units) @
248249
[ui.ui_unit];
249250
ui_imports_cmi =
250-
(modname, Some (ui.ui_unit, Env.crc_of_unit modname)) ::
251-
filter(Asmlink.extract_crc_interfaces()) ~get_name:(fun name -> name);
251+
(Import_info.create modname
252+
~crc_with_unit:(Some (ui.ui_unit, Env.crc_of_unit modname))) ::
253+
filter (Asmlink.extract_crc_interfaces ());
252254
ui_imports_cmx =
253-
filter(Asmlink.extract_crc_implementations()) ~get_name:CU.name;
255+
filter(Asmlink.extract_crc_implementations());
254256
ui_generic_fns =
255257
{ curry_fun =
256258
union(List.map (fun info -> info.ui_generic_fns.curry_fun) units);

file_formats/cmx_format.mli

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ type generic_fns =
4949
apply_fun: apply_fn list;
5050
send_fun: apply_fn list }
5151

52-
type import_info_cmi =
53-
Compilation_unit.Name.t * (Compilation_unit.t * Digest.t) option
54-
type import_info_cmx =
55-
Compilation_unit.t * Digest.t option
56-
5752
(* Symbols of function that pass certain checks for special properties. *)
5853
type checks =
5954
{
@@ -68,9 +63,9 @@ type unit_infos =
6863
(* All compilation units in the
6964
.cmx file (i.e. [ui_unit] and
7065
any produced via [Asmpackager]) *)
71-
mutable ui_imports_cmi: import_info_cmi list;
66+
mutable ui_imports_cmi: Import_info.t list;
7267
(* Interfaces imported *)
73-
mutable ui_imports_cmx: import_info_cmx list;
68+
mutable ui_imports_cmx: Import_info.t list;
7469
(* Infos imported *)
7570
mutable ui_generic_fns: generic_fns; (* Generic functions needed *)
7671
mutable ui_export_info: export_info;
@@ -80,8 +75,8 @@ type unit_infos =
8075
type unit_infos_raw =
8176
{ uir_unit: Compilation_unit.t;
8277
uir_defines: Compilation_unit.t list;
83-
uir_imports_cmi: import_info_cmi list;
84-
uir_imports_cmx: import_info_cmx list;
78+
uir_imports_cmi: Import_info.t array;
79+
uir_imports_cmx: Import_info.t array;
8580
uir_generic_fns: generic_fns;
8681
uir_export_info: export_info_raw;
8782
uir_checks: checks;
@@ -104,8 +99,8 @@ type lib_unit_info =
10499
li_imports_cmx : Bitmap.t } (* subset of lib_imports_cmx *)
105100

106101
type library_infos =
107-
{ lib_imports_cmi: import_info_cmi array;
108-
lib_imports_cmx: import_info_cmx array;
102+
{ lib_imports_cmi: Import_info.t array;
103+
lib_imports_cmx: Import_info.t array;
109104
lib_units: lib_unit_info list;
110105
lib_generic_fns: generic_fns;
111106
(* In the following fields the lists are reversed with respect to

middle_end/compilenv.ml

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ let read_unit_info filename =
173173
let ui = {
174174
ui_unit = uir.uir_unit;
175175
ui_defines = uir.uir_defines;
176-
ui_imports_cmi = uir.uir_imports_cmi;
177-
ui_imports_cmx = uir.uir_imports_cmx;
176+
ui_imports_cmi = uir.uir_imports_cmi |> Array.to_list;
177+
ui_imports_cmx = uir.uir_imports_cmx |> Array.to_list;
178178
ui_generic_fns = uir.uir_generic_fns;
179179
ui_export_info = export_info;
180180
ui_checks = uir.uir_checks;
@@ -226,8 +226,8 @@ let get_unit_info comp_unit ~cmx_name =
226226
(None, None)
227227
end
228228
in
229-
current_unit.ui_imports_cmx <-
230-
(comp_unit, crc) :: current_unit.ui_imports_cmx;
229+
let import = Import_info.create_normal comp_unit ~crc in
230+
current_unit.ui_imports_cmx <- import :: current_unit.ui_imports_cmx;
231231
CU.Name.Tbl.add global_infos_table cmx_name infos;
232232
infos
233233
end
@@ -337,6 +337,28 @@ let need_send_fun n mode =
337337

338338
(* Write the description of the current unit *)
339339

340+
(* CR mshinwell: let's think about this later, quadratic algorithm
341+
342+
let ensure_sharing_between_cmi_and_cmx_imports cmi_imports cmx_imports =
343+
(* If a [CU.t] in the .cmx imports also occurs in the .cmi imports, use
344+
the one in the .cmi imports, to increase sharing. (Such a [CU.t] in
345+
the .cmi imports may already have part of its value shared with the
346+
first [CU.Name.t] component in the .cmi imports, c.f.
347+
[Persistent_env.ensure_crc_sharing], so it's best to pick this [CU.t].) *)
348+
List.map (fun ((comp_unit, crc) as import) ->
349+
match
350+
List.find_map (function
351+
| _, None -> None
352+
| _, Some (comp_unit', _) ->
353+
if CU.equal comp_unit comp_unit' then Some comp_unit'
354+
else None)
355+
cmi_imports
356+
with
357+
| None -> import
358+
| Some comp_unit -> comp_unit, crc)
359+
cmx_imports
360+
*)
361+
340362
let write_unit_info info filename =
341363
let raw_export_info, sections =
342364
match info.ui_export_info with
@@ -351,8 +373,8 @@ let write_unit_info info filename =
351373
let raw_info = {
352374
uir_unit = info.ui_unit;
353375
uir_defines = info.ui_defines;
354-
uir_imports_cmi = info.ui_imports_cmi;
355-
uir_imports_cmx = info.ui_imports_cmx;
376+
uir_imports_cmi = Array.of_list info.ui_imports_cmi;
377+
uir_imports_cmx = Array.of_list info.ui_imports_cmx;
356378
uir_generic_fns = info.ui_generic_fns;
357379
uir_export_info = raw_export_info;
358380
uir_checks = info.ui_checks;

middle_end/compilenv.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ val require_global: Compilation_unit.t -> unit
133133

134134
val read_library_info: string -> library_infos
135135

136+
(* CR mshinwell: see comment in .ml
137+
val ensure_sharing_between_cmi_and_cmx_imports :
138+
(_ * (Compilation_unit.t * _) option) list ->
139+
(Compilation_unit.t * 'a) list ->
140+
(Compilation_unit.t * 'a) list
141+
*)
142+
136143
type error =
137144
Not_a_unit_info of string
138145
| Corrupted_unit_info of string

0 commit comments

Comments
 (0)