Skip to content

Commit ed18bca

Browse files
committed
Allow metadata-only code in environments
1 parent 347d165 commit ed18bca

7 files changed

Lines changed: 51 additions & 27 deletions

File tree

middle_end/flambda2/from_lambda/closure_conversion.ml

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,21 +1606,33 @@ let close_program ~symbol_for_global ~big_endian ~module_ident
16061606
~handler_params:load_fields_handler_param ~handler:load_fields_body ~body
16071607
~is_exn_handler:false
16081608
in
1609+
let all_code =
1610+
Code_id.Map.mapi (fun code_id code_or_metadata ->
1611+
match (code_or_metadata : Code_or_metadata.t) with
1612+
| Code_present code -> code
1613+
| Metadata_only _ -> Misc.fatal_errorf "Metadata only for code ID %a" Code_id.print code_id)
1614+
(Acc.code acc)
1615+
in
16091616
let acc, body =
1610-
Code_id.Map.fold
1611-
(fun code_id code (acc, body) ->
1612-
let bound_symbols =
1613-
Bound_symbols.singleton (Bound_symbols.Pattern.code code_id)
1614-
in
1615-
let static_const = Static_const_or_code.create_code code in
1616-
let defining_expr =
1617-
Static_const_group.create [static_const] |> Named.create_static_consts
1618-
in
1619-
Let_with_acc.create acc
1620-
(Bound_pattern.symbols bound_symbols)
1621-
defining_expr ~body
1622-
|> Expr_with_acc.create_let)
1623-
(Acc.code acc) (acc, body)
1617+
let bound_symbols, static_consts =
1618+
Code_id.Map.fold
1619+
(fun code_id code (bound_symbols, static_consts) ->
1620+
let bound_symbols =
1621+
(Bound_symbols.Pattern.code code_id) :: bound_symbols
1622+
in
1623+
let static_consts =
1624+
(Static_const_or_code.create_code code) :: static_consts
1625+
in
1626+
(bound_symbols, static_consts))
1627+
all_code ([], [])
1628+
in
1629+
let defining_expr =
1630+
Static_const_group.create static_consts |> Named.create_static_consts
1631+
in
1632+
Let_with_acc.create acc
1633+
(Bound_pattern.symbols (Bound_symbols.create bound_symbols))
1634+
defining_expr ~body
1635+
|> Expr_with_acc.create_let
16241636
in
16251637
(* We must make sure there is always an outer [Let_symbol] binding so that
16261638
lifted constants not in the scope of any other [Let_symbol] binding get put
@@ -1670,5 +1682,5 @@ let close_program ~symbol_for_global ~big_endian ~module_ident
16701682
~module_symbol ~used_closure_vars:Unknown,
16711683
Exported_code.add_code
16721684
~keep_code:(fun _ -> false)
1673-
(Acc.code acc) Exported_code.empty,
1685+
all_code Exported_code.empty,
16741686
exported_offsets )

middle_end/flambda2/from_lambda/closure_conversion_aux.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ module Acc = struct
270270
type t =
271271
{ declared_symbols : (Symbol.t * Static_const.t) list;
272272
shareable_constants : Symbol.t Static_const.Map.t;
273-
code : Code.t Code_id.Map.t;
273+
code : Code_or_metadata.t Code_id.Map.t;
274274
free_names : Name_occurrences.t;
275275
continuation_applications : continuation_application Continuation.Map.t;
276276
cost_metrics : Cost_metrics.t;
@@ -325,7 +325,7 @@ module Acc = struct
325325
{ t with shareable_constants }
326326

327327
let add_code ~code_id ~code t =
328-
{ t with code = Code_id.Map.add code_id code t.code }
328+
{ t with code = Code_id.Map.add code_id (Code_or_metadata.create code) t.code }
329329

330330
let add_free_names free_names t =
331331
{ t with free_names = Name_occurrences.union free_names t.free_names }
@@ -629,8 +629,9 @@ module Let_with_acc = struct
629629
Cost_metrics.set_of_closures
630630
~find_code_characteristics:(fun code_id ->
631631
let code = Code_id.Map.find code_id code_mapping in
632-
{ cost_metrics = Code.cost_metrics code;
633-
params_arity = List.length (Code.params_arity code)
632+
let code_metadata = Code_or_metadata.code_metadata code in
633+
{ cost_metrics = Code_metadata.cost_metrics code_metadata;
634+
params_arity = List.length (Code_metadata.params_arity code_metadata)
634635
})
635636
set_of_closures
636637
| Rec_info _ -> Cost_metrics.zero

middle_end/flambda2/from_lambda/closure_conversion_aux.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module Acc : sig
162162

163163
val shareable_constants : t -> Symbol.t Static_const.Map.t
164164

165-
val code : t -> Code.t Code_id.Map.t
165+
val code : t -> Code_or_metadata.t Code_id.Map.t
166166

167167
val free_names : t -> Name_occurrences.t
168168

middle_end/flambda2/simplify/env/downwards_env.ml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type t =
4444
do_not_rebuild_terms : bool;
4545
closure_info : Closure_info.t;
4646
get_imported_code : unit -> Exported_code.t;
47-
all_code : Code.t Code_id.Map.t
47+
all_code : Code_or_metadata.t Code_id.Map.t
4848
}
4949

5050
let print_debuginfo ppf dbg =
@@ -90,7 +90,7 @@ let [@ocamlformat "disable"] print ppf { round; typing_env;
9090
CSE.print cse
9191
do_not_rebuild_terms
9292
Closure_info.print closure_info
93-
(Code_id.Map.print Code.print) all_code
93+
(Code_id.Map.print Code_or_metadata.print) all_code
9494

9595
let create ~round ~(resolver : resolver)
9696
~(get_imported_names : get_imported_names)
@@ -394,7 +394,7 @@ let mem_code t id =
394394

395395
let find_code_exn t id =
396396
match Code_id.Map.find id t.all_code with
397-
| code -> Code_or_metadata.create code
397+
| code_or_metadata -> code_or_metadata
398398
| exception Not_found -> (
399399
(* This [find_exn] call doesn't load any .cmx files, but in the majority of
400400
cases will succeed. *)
@@ -423,9 +423,18 @@ let define_code t ~code_id ~code =
423423
TE.add_to_code_age_relation t.typing_env ~new_code_id:code_id
424424
~old_code_id:(Code.newer_version_of code)
425425
in
426-
let all_code = Code_id.Map.add code_id code t.all_code in
426+
let all_code = Code_id.Map.add code_id (Code_or_metadata.create code) t.all_code in
427427
{ t with typing_env; all_code }
428428

429+
let define_code_metadata_only t ~code_id ~code =
430+
let code_or_metadata =
431+
Code_or_metadata.remember_only_metadata (Code_or_metadata.create code)
432+
in
433+
let all_code =
434+
Code_id.Map.add code_id code_or_metadata t.all_code
435+
in
436+
{ t with all_code }
437+
429438
let set_inlined_debuginfo t dbg = { t with inlined_debuginfo = dbg }
430439

431440
let get_inlined_debuginfo t = t.inlined_debuginfo

middle_end/flambda2/simplify/env/downwards_env.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ val create :
4141
unit_toplevel_return_continuation:Continuation.t ->
4242
t
4343

44-
val all_code : t -> Code.t Code_id.Map.t
44+
val all_code : t -> Code_or_metadata.t Code_id.Map.t
4545

4646
val resolver : t -> Compilation_unit.t -> Flambda2_types.Typing_env.t option
4747

@@ -153,6 +153,8 @@ val check_simple_is_bound : t -> Simple.t -> unit
153153

154154
val define_code : t -> code_id:Code_id.t -> code:Code.t -> t
155155

156+
val define_code_metadata_only : t -> code_id:Code_id.t -> code:Code.t -> t
157+
156158
val mem_code : t -> Code_id.t -> bool
157159

158160
(** This function raises if the code ID is unbound. *)

middle_end/flambda2/simplify_shared/closure_offsets.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ module Greedy = struct
497497
if Compilation_unit.is_current (Closure_id.get_compilation_unit c)
498498
then
499499
let code_metadata =
500-
Code_id.Map.find code_id all_code |> Code.code_metadata
500+
Code_id.Map.find code_id all_code |> Code_or_metadata.code_metadata
501501
in
502502
let module CM = Code_metadata in
503503
let is_tupled = CM.is_tupled code_metadata in

middle_end/flambda2/simplify_shared/closure_offsets.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ val create : unit -> t
4343
val add_set_of_closures :
4444
t ->
4545
is_phantom:bool ->
46-
all_code:Code.t Code_id.Map.t ->
46+
all_code:Code_or_metadata.t Code_id.Map.t ->
4747
Set_of_closures.t ->
4848
t
4949

0 commit comments

Comments
 (0)