Skip to content

Commit 06173dc

Browse files
Gburymshinwell
andauthored
Use cmx reachability to decide locality of symbols (#1246)
Co-authored-by: Mark Shinwell <mshinwell@pm.me>
1 parent d1fa776 commit 06173dc

25 files changed

+238
-157
lines changed

backend/cmm.ml

+5
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ type kind_for_unboxing =
225225

226226
type is_global = Global | Local
227227

228+
let equal_is_global g g' =
229+
match g, g' with
230+
| Local, Local | Global, Global -> true
231+
| Local, Global | Global, Local -> false
232+
228233
type symbol =
229234
{ sym_name : string;
230235
sym_global : is_global }

backend/cmm.mli

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ type kind_for_unboxing =
228228
| Boxed_float
229229

230230
type is_global = Global | Local
231+
val equal_is_global : is_global -> is_global -> bool
231232

232233
(* Symbols are marked with whether they are local or global,
233234
at both definition and use sites.

backend/cmm_helpers.ml

+18-13
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ let apply_function_sym arity result mode =
12081208
Compilenv.need_apply_fun arity result mode;
12091209
global_symbol (apply_function_name arity result mode)
12101210

1211-
let curry_function_sym function_kind arity result =
1211+
let curry_function_sym_name function_kind arity result =
12121212
match function_kind with
12131213
| Lambda.Curried { nlocal } ->
12141214
Compilenv.need_curry_fun function_kind arity result;
@@ -1233,6 +1233,11 @@ let curry_function_sym function_kind arity result =
12331233
^
12341234
match result with [| Val |] -> "" | _ -> "_R" ^ machtype_identifier result)
12351235

1236+
let curry_function_sym function_kind arity result =
1237+
{ sym_name = curry_function_sym_name function_kind arity result;
1238+
sym_global = Global
1239+
}
1240+
12361241
(* Big arrays *)
12371242

12381243
let bigarray_elt_size_in_bytes : Lambda.bigarray_kind -> int = function
@@ -2809,7 +2814,7 @@ let final_curry_function nlocal arity result =
28092814
let narity = List.length arity in
28102815
let fun_name =
28112816
global_symbol
2812-
(curry_function_sym (Lambda.Curried { nlocal }) arity result
2817+
(curry_function_sym_name (Lambda.Curried { nlocal }) arity result
28132818
^ "_"
28142819
^ Int.to_string (narity - 1))
28152820
in
@@ -2828,7 +2833,9 @@ let final_curry_function nlocal arity result =
28282833
}
28292834

28302835
let intermediate_curry_functions ~nlocal ~arity result =
2831-
let name1 = curry_function_sym (Lambda.Curried { nlocal }) arity result in
2836+
let name1 =
2837+
curry_function_sym_name (Lambda.Curried { nlocal }) arity result
2838+
in
28322839
let narity = List.length arity in
28332840
let dbg = placeholder_dbg in
28342841
let rec loop accumulated_args remaining_args num =
@@ -3756,9 +3763,8 @@ let emit_constant_closure symb fundecls clos_vars cont =
37563763
in
37573764
(Cint (infix_header pos) :: closure_symbol f2)
37583765
@ Csymbol_address
3759-
(global_symbol
3760-
(curry_function_sym arity.function_kind params_machtypes
3761-
return_machtype))
3766+
(curry_function_sym arity.function_kind params_machtypes
3767+
return_machtype)
37623768
:: Cint (closure_info ~arity ~startenv:(startenv - pos) ~is_last)
37633769
:: Csymbol_address
37643770
{ sym_name = f2.label; sym_global = symb.sym_global }
@@ -3776,11 +3782,10 @@ let emit_constant_closure symb fundecls clos_vars cont =
37763782
:: emit_others 3 remainder
37773783
| arity ->
37783784
Csymbol_address
3779-
(global_symbol
3780-
(curry_function_sym arity.function_kind
3781-
(List.map machtype_of_layout_changing_tagged_int_to_val
3782-
arity.params_layout)
3783-
(machtype_of_layout_changing_tagged_int_to_val arity.return_layout)))
3785+
(curry_function_sym arity.function_kind
3786+
(List.map machtype_of_layout_changing_tagged_int_to_val
3787+
arity.params_layout)
3788+
(machtype_of_layout_changing_tagged_int_to_val arity.return_layout))
37843789
:: Cint (closure_info ~arity ~startenv ~is_last)
37853790
:: Csymbol_address { sym_name = f1.label; sym_global = symb.sym_global }
37863791
:: emit_others 4 remainder)
@@ -3844,7 +3849,7 @@ let unit ~dbg = Cconst_int (1, dbg)
38443849

38453850
let var v = Cvar v
38463851

3847-
let symbol_from_string ~dbg sym = Cconst_symbol (global_symbol sym, dbg)
3852+
let symbol ~dbg sym = Cconst_symbol (sym, dbg)
38483853

38493854
let float ~dbg f = Cconst_float (f, dbg)
38503855

@@ -4136,7 +4141,7 @@ let gc_root_table syms =
41364141
let table_symbol = make_symbol ?compilation_unit:None "gc_roots" in
41374142
cdata
41384143
(define_symbol { sym_name = table_symbol; sym_global = Global }
4139-
@ List.map (fun s -> symbol_address (global_symbol s)) syms
4144+
@ List.map symbol_address syms
41404145
@ [cint 0n])
41414146

41424147
let cmm_arith_size (e : Cmm.expression) =

backend/cmm_helpers.mli

+4-5
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ val machtype_identifier : machtype -> string
475475
(** Get the symbol for the generic currying or tuplifying wrapper with [n]
476476
arguments, and ensure its presence in the set of defined symbols. *)
477477
val curry_function_sym :
478-
Lambda.function_kind -> machtype list -> machtype -> string
478+
Lambda.function_kind -> machtype list -> machtype -> Cmm.symbol
479479

480480
(** Bigarrays *)
481481

@@ -958,9 +958,8 @@ val unit : dbg:Debuginfo.t -> Cmm.expression
958958
(** Create an expression from a variable. *)
959959
val var : Backend_var.t -> Cmm.expression
960960

961-
(** Create an expression that gives the value of an object file symbol, such
962-
symbol's name being given by a string. *)
963-
val symbol_from_string : dbg:Debuginfo.t -> string -> Cmm.expression
961+
(** Create an expression that gives the value of an object file symbol. *)
962+
val symbol : dbg:Debuginfo.t -> Cmm.symbol -> Cmm.expression
964963

965964
(** Create a constant float expression. *)
966965
val float : dbg:Debuginfo.t -> float -> expression
@@ -1244,7 +1243,7 @@ val cfunction : fundecl -> phrase
12441243
val cdata : data_item list -> phrase
12451244

12461245
(** Create the gc root table from a list of root symbols. *)
1247-
val gc_root_table : string list -> phrase
1246+
val gc_root_table : Cmm.symbol list -> phrase
12481247

12491248
(* An estimate of the number of arithmetic instructions in a Cmm expression.
12501249
This is currently used in Flambda 2 to determine whether untagging an

backend/cmmgen.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ let rec transl env e =
481481
transl_fundecls (pos + 3) rem
482482
| arity ->
483483
Cconst_symbol
484-
(global_symbol (curry_function_sym
484+
((curry_function_sym
485485
arity.function_kind
486486
(List.map machtype_of_layout_changing_tagged_int_to_val
487487
arity.params_layout)

backend/cmmgen_state.ml

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ let is_local_function name =
7676
let clear_function_names () =
7777
Hashtbl.clear state.function_names
7878

79+
let add_structured_constant (sym : Cmm.symbol) cst =
80+
if not (Hashtbl.mem state.structured_constants sym.sym_name) then
81+
Hashtbl.replace state.structured_constants sym.sym_name (sym.sym_global, cst)
82+
7983
let set_local_structured_constants l =
8084
Hashtbl.clear state.structured_constants;
8185
List.iter

backend/cmmgen_state.mli

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ val is_local_function : Clambda.function_label -> bool
4141

4242
val clear_function_names : unit -> unit
4343

44+
val add_structured_constant : Cmm.symbol -> Clambda.ustructured_constant -> unit
45+
4446
val set_local_structured_constants : Clambda.preallocated_constant list -> unit
4547

4648
val add_global_structured_constant : string -> Clambda.ustructured_constant -> unit

backend/printcmm.ml

+10-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ let extcall_signature ppf (ty_res, ty_args) =
6363
fprintf ppf "->%a" machtype ty_res
6464
end
6565

66+
let is_global ppf = function
67+
| Global -> fprintf ppf "G"
68+
| Local -> fprintf ppf "L"
69+
70+
let symbol ppf s =
71+
fprintf ppf "%a:\"%s\"" is_global s.sym_global s.sym_name
72+
6673
let integer_comparison = function
6774
| Ceq -> "=="
6875
| Cne -> "!="
@@ -230,12 +237,13 @@ let operation d = function
230237
| Cbeginregion -> "beginregion"
231238
| Cendregion -> "endregion"
232239

240+
233241
let rec expr ppf = function
234242
| Cconst_int (n, _dbg) -> fprintf ppf "%i" n
235243
| Cconst_natint (n, _dbg) ->
236244
fprintf ppf "%s" (Nativeint.to_string n)
237245
| Cconst_float (n, _dbg) -> fprintf ppf "%F" n
238-
| Cconst_symbol (s, _dbg) -> fprintf ppf "\"%s\"" s.sym_name
246+
| Cconst_symbol (s, _dbg) -> fprintf ppf "%a:\"%s\"" is_global s.sym_global s.sym_name
239247
| Cvar id -> V.print ppf id
240248
| Clet(id, def, (Clet(_, _, _) as body)) ->
241249
let print_binding id ppf def =
@@ -401,7 +409,7 @@ let data_item ppf = function
401409
| Cint n -> fprintf ppf "int %s" (Nativeint.to_string n)
402410
| Csingle f -> fprintf ppf "single %F" f
403411
| Cdouble f -> fprintf ppf "double %F" f
404-
| Csymbol_address s -> fprintf ppf "addr \"%s\"" s.sym_name
412+
| Csymbol_address s -> fprintf ppf "addr %a:\"%s\"" is_global s.sym_global s.sym_name
405413
| Cstring s -> fprintf ppf "string \"%s\"" s
406414
| Cskip n -> fprintf ppf "skip %i" n
407415
| Calign n -> fprintf ppf "align %i" n

backend/printcmm.mli

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
open Format
1919

20+
val symbol : formatter -> Cmm.symbol -> unit
2021
val rec_flag : formatter -> Cmm.rec_flag -> unit
2122
val machtype_component : formatter -> Cmm.machtype_component -> unit
2223
val machtype : formatter -> Cmm.machtype -> unit

middle_end/flambda2/cmx/flambda_cmx.ml

+10-6
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,19 @@ let prepare_cmx ~module_symbol create_typing_env ~free_names_of_name
221221
|> Exported_offsets.reexport_value_slots
222222
(Name_occurrences.all_value_slots slots_used_in_typing_env)
223223
in
224-
Some
225-
(Flambda_cmx_format.create ~final_typing_env ~all_code ~exported_offsets
226-
~used_value_slots)
224+
let cmx =
225+
Flambda_cmx_format.create ~final_typing_env ~all_code ~exported_offsets
226+
~used_value_slots
227+
in
228+
reachable_names, Some cmx
227229

228230
let prepare_cmx_file_contents ~final_typing_env ~module_symbol ~used_value_slots
229231
~exported_offsets all_code =
230232
match final_typing_env with
231-
| None -> None
232-
| Some _ when Flambda_features.opaque () -> None
233+
| None ->
234+
Name_occurrences.singleton_symbol module_symbol Name_mode.normal, None
235+
| Some _ when Flambda_features.opaque () ->
236+
Name_occurrences.singleton_symbol module_symbol Name_mode.normal, None
233237
| Some final_typing_env ->
234238
let typing_env, canonicalise =
235239
TE.Pre_serializable.create final_typing_env ~used_value_slots
@@ -247,7 +251,7 @@ let prepare_cmx_file_contents ~final_typing_env ~module_symbol ~used_value_slots
247251
let prepare_cmx_from_approx ~approxs ~module_symbol ~exported_offsets
248252
~used_value_slots all_code =
249253
if Flambda_features.opaque ()
250-
then None
254+
then Name_occurrences.singleton_symbol module_symbol Name_mode.normal, None
251255
else
252256
let create_typing_env reachable_names =
253257
let approxs =

middle_end/flambda2/cmx/flambda_cmx.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ val prepare_cmx_file_contents :
4040
used_value_slots:Value_slot.Set.t ->
4141
exported_offsets:Exported_offsets.t ->
4242
Exported_code.t ->
43-
Flambda_cmx_format.t option
43+
Name_occurrences.t * Flambda_cmx_format.t option
4444

4545
val prepare_cmx_from_approx :
4646
approxs:Code_or_metadata.t Value_approximation.t Symbol.Map.t ->
4747
module_symbol:Symbol.t ->
4848
exported_offsets:Exported_offsets.t ->
4949
used_value_slots:Value_slot.Set.t ->
5050
Exported_code.t ->
51-
Flambda_cmx_format.t option
51+
Name_occurrences.t * Flambda_cmx_format.t option

middle_end/flambda2/flambda2.ml

+13-6
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,25 @@ let lambda_to_cmm ~ppf_dump:ppf ~prefixname ~filename:_ ~keep_symbol_tables
136136
in
137137
Compiler_hooks.execute Raw_flambda2 raw_flambda;
138138
print_rawflambda ppf raw_flambda;
139-
let flambda, offsets, cmx, all_code =
139+
let flambda, offsets, reachable_names, cmx, all_code =
140140
match mode, close_program_metadata with
141-
| Classic, Classic (code, cmx, offsets) ->
141+
| Classic, Classic (code, reachable_names, cmx, offsets) ->
142142
(if Flambda_features.inlining_report ()
143143
then
144144
let output_prefix = prefixname ^ ".cps_conv" in
145145
let inlining_tree =
146146
Inlining_report.output_then_forget_decisions ~output_prefix
147147
in
148148
Compiler_hooks.execute Inlining_tree inlining_tree);
149-
raw_flambda, offsets, cmx, code
149+
raw_flambda, offsets, reachable_names, cmx, code
150150
| Normal, Normal ->
151151
let round = 0 in
152-
let { Simplify.unit = flambda; exported_offsets; cmx; all_code } =
152+
let { Simplify.unit = flambda;
153+
exported_offsets;
154+
cmx;
155+
all_code;
156+
reachable_names
157+
} =
153158
Profile.record_call ~accumulate:true "simplify" (fun () ->
154159
Simplify.run ~cmx_loader ~round raw_flambda)
155160
in
@@ -163,13 +168,15 @@ let lambda_to_cmm ~ppf_dump:ppf ~prefixname ~filename:_ ~keep_symbol_tables
163168
Compiler_hooks.execute Flambda2 flambda;
164169
print_flambda "simplify" ppf flambda;
165170
print_flexpect "simplify" ppf ~raw_flambda flambda;
166-
flambda, exported_offsets, cmx, all_code
171+
flambda, exported_offsets, reachable_names, cmx, all_code
167172
in
168173
(match cmx with
169174
| None ->
170175
() (* Either opaque was passed, or there is no need to export offsets *)
171176
| Some cmx -> Compilenv.flambda2_set_export_info cmx);
172-
let cmm = Flambda2_to_cmm.To_cmm.unit flambda ~all_code ~offsets in
177+
let cmm =
178+
Flambda2_to_cmm.To_cmm.unit flambda ~all_code ~offsets ~reachable_names
179+
in
173180
if not keep_symbol_tables
174181
then (
175182
Compilenv.reset_info_tables ();

middle_end/flambda2/from_lambda/closure_conversion.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ module VB = Bound_var
3535
type 'a close_program_metadata =
3636
| Normal : [`Normal] close_program_metadata
3737
| Classic :
38-
(Exported_code.t * Flambda_cmx_format.t option * Exported_offsets.t)
38+
(Exported_code.t
39+
* Name_occurrences.t
40+
* Flambda_cmx_format.t option
41+
* Exported_offsets.t)
3942
-> [`Classic] close_program_metadata
4043

4144
type 'a close_program_result = Flambda_unit.t * 'a close_program_metadata
@@ -2580,7 +2583,7 @@ let close_program (type mode) ~(mode : mode Flambda_features.mode) ~big_endian
25802583
Slot_offsets.finalize_offsets (Acc.slot_offsets acc) ~get_code_metadata
25812584
~used_slots
25822585
in
2583-
let cmx =
2586+
let reachable_names, cmx =
25842587
Flambda_cmx.prepare_cmx_from_approx ~approxs:symbols_approximations
25852588
~module_symbol ~exported_offsets ~used_value_slots all_code
25862589
in
@@ -2589,4 +2592,4 @@ let close_program (type mode) ~(mode : mode Flambda_features.mode) ~big_endian
25892592
~toplevel_my_region ~body ~module_symbol
25902593
~used_value_slots:(Known used_value_slots)
25912594
in
2592-
unit, Classic (all_code, cmx, exported_offsets)
2595+
unit, Classic (all_code, reachable_names, cmx, exported_offsets)

middle_end/flambda2/from_lambda/closure_conversion.mli

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ val close_switch :
7373
type 'a close_program_metadata =
7474
| Normal : [`Normal] close_program_metadata
7575
| Classic :
76-
(Exported_code.t * Flambda_cmx_format.t option * Exported_offsets.t)
76+
(Exported_code.t
77+
* Name_occurrences.t
78+
* Flambda_cmx_format.t option
79+
* Exported_offsets.t)
7780
-> [`Classic] close_program_metadata
7881

7982
type 'a close_program_result = Flambda_unit.t * 'a close_program_metadata

middle_end/flambda2/simplify/simplify.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ type simplify_result =
2020
{ cmx : Flambda_cmx_format.t option;
2121
unit : Flambda_unit.t;
2222
all_code : Exported_code.t;
23-
exported_offsets : Exported_offsets.t
23+
exported_offsets : Exported_offsets.t;
24+
reachable_names : Name_occurrences.t
2425
}
2526

2627
let run ~cmx_loader ~round unit =
@@ -97,12 +98,12 @@ let run ~cmx_loader ~round unit =
9798
in
9899
Slot_offsets.finalize_offsets slot_offsets ~get_code_metadata ~used_slots
99100
in
100-
let cmx =
101+
let reachable_names, cmx =
101102
Flambda_cmx.prepare_cmx_file_contents ~final_typing_env ~module_symbol
102103
~used_value_slots ~exported_offsets all_code
103104
in
104105
let unit =
105106
FU.create ~return_continuation ~exn_continuation ~toplevel_my_region
106107
~module_symbol ~body ~used_value_slots:(Known used_value_slots)
107108
in
108-
{ cmx; unit; all_code; exported_offsets }
109+
{ cmx; unit; all_code; exported_offsets; reachable_names }

middle_end/flambda2/simplify/simplify.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ type simplify_result = private
2424
{ cmx : Flambda_cmx_format.t option;
2525
unit : Flambda_unit.t;
2626
all_code : Exported_code.t;
27-
exported_offsets : Exported_offsets.t
27+
exported_offsets : Exported_offsets.t;
28+
reachable_names : Name_occurrences.t
2829
}
2930

3031
val run :

0 commit comments

Comments
 (0)