Skip to content

Commit 1c4db44

Browse files
authored
flambda-backend: Port PR1202 and PR1205 to the ocaml/ subtree (#1211)
1 parent 577410e commit 1c4db44

16 files changed

+76
-36
lines changed

asmcomp/cmm_helpers.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,14 @@ let field_address ptr n dbg =
666666
then ptr
667667
else Cop(Cadda, [ptr; Cconst_int(n * size_addr, dbg)], dbg)
668668

669+
let get_field_gen_given_memory_chunk memory_chunk mut ptr n dbg =
670+
Cop (Cload (memory_chunk, mut), [field_address ptr n dbg], dbg)
671+
669672
let get_field_gen mut ptr n dbg =
670-
Cop(Cload (Word_val, mut), [field_address ptr n dbg], dbg)
673+
get_field_gen_given_memory_chunk Word_val mut ptr n dbg
671674

672675
let get_field_codepointer mut ptr n dbg =
673-
Cop (Cload (Word_int, mut), [field_address ptr n dbg], dbg)
676+
get_field_gen_given_memory_chunk Word_int mut ptr n dbg
674677

675678
let set_field ptr n newval init dbg =
676679
Cop(Cstore (Word_val, init), [field_address ptr n dbg; newval], dbg)

asmcomp/cmm_helpers.mli

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,20 @@ val remove_unit : expression -> expression
194194
val field_address : expression -> int -> Debuginfo.t -> expression
195195

196196
(** [get_field_gen mut ptr n dbg] returns an expression for the access to the
197-
[n]th field of the block pointed to by [ptr] *)
197+
[n]th field of the block pointed to by [ptr]. The [memory_chunk] used is
198+
always [Word_val]. *)
198199
val get_field_gen :
199200
Asttypes.mutable_flag -> expression -> int -> Debuginfo.t -> expression
200201

202+
(** Like [get_field_gen] but allows use of a different [memory_chunk]. *)
203+
val get_field_gen_given_memory_chunk :
204+
Cmm.memory_chunk ->
205+
Asttypes.mutable_flag ->
206+
expression ->
207+
int ->
208+
Debuginfo.t ->
209+
expression
210+
201211
(** [set_field ptr n newval init dbg] returns an expression for setting the
202212
[n]th field of the block pointed to by [ptr] to [newval] *)
203213
val set_field :

asmcomp/cmmgen.ml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,20 @@ let mut_from_env env ptr =
117117
else Asttypes.Mutable
118118
| _ -> Asttypes.Mutable
119119

120-
let get_field env ptr n dbg =
120+
let get_field env layout ptr n dbg =
121121
let mut = mut_from_env env ptr in
122-
get_field_gen mut ptr n dbg
122+
let memory_chunk =
123+
match layout with
124+
| Pvalue Pintval | Punboxed_int _ -> Word_int
125+
| Pvalue _ -> Word_val
126+
| Punboxed_float -> Double
127+
| Ptop ->
128+
Misc.fatal_errorf "get_field with Ptop: %a" Debuginfo.print_compact dbg
129+
| Pbottom ->
130+
Misc.fatal_errorf "get_field with Pbottom: %a" Debuginfo.print_compact
131+
dbg
132+
in
133+
get_field_gen_given_memory_chunk memory_chunk mut ptr n dbg
123134

124135
type rhs_kind =
125136
| RHS_block of Lambda.alloc_mode * int
@@ -869,8 +880,8 @@ and transl_prim_1 env p arg dbg =
869880
Popaque ->
870881
opaque (transl env arg) dbg
871882
(* Heap operations *)
872-
| Pfield n ->
873-
get_field env (transl env arg) n dbg
883+
| Pfield (n, layout) ->
884+
get_field env layout (transl env arg) n dbg
874885
| Pfloatfield (n,mode) ->
875886
let ptr = transl env arg in
876887
box_float dbg mode (floatfield n ptr dbg)

middle_end/clambda_primitives.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type primitive =
3636
| Pread_symbol of string
3737
(* Operations on heap blocks *)
3838
| Pmakeblock of int * mutable_flag * block_shape * alloc_mode
39-
| Pfield of int
39+
| Pfield of int * layout
4040
| Pfield_computed
4141
| Psetfield of int * immediate_or_pointer * initialization_or_assignment
4242
| Psetfield_computed of immediate_or_pointer * initialization_or_assignment

middle_end/clambda_primitives.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type primitive =
3636
| Pread_symbol of string
3737
(* Operations on heap blocks *)
3838
| Pmakeblock of int * mutable_flag * block_shape * alloc_mode
39-
| Pfield of int
39+
| Pfield of int * layout
4040
| Pfield_computed
4141
| Psetfield of int * immediate_or_pointer * initialization_or_assignment
4242
| Psetfield_computed of immediate_or_pointer * initialization_or_assignment

middle_end/closure/closure.ml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ let rec split_list n l =
5252

5353
let rec add_to_closure_env env_param pos cenv = function
5454
[] -> cenv
55-
| id :: rem ->
55+
| (id, kind) :: rem ->
5656
V.Map.add id
57-
(Uprim(P.Pfield pos, [Uvar env_param], Debuginfo.none))
57+
(Uprim(P.Pfield (pos, kind), [Uvar env_param], Debuginfo.none))
5858
(add_to_closure_env env_param (pos+1) cenv rem)
5959

6060
let is_gc_ignorable kind =
@@ -67,7 +67,12 @@ let is_gc_ignorable kind =
6767
| Pvalue (Pgenval | Pfloatval | Pboxedintval _ | Pvariant _ | Parrayval _) -> false
6868

6969
let split_closure_fv kinds fv =
70-
List.partition (fun id -> is_gc_ignorable (V.Map.find id kinds)) fv
70+
List.fold_right (fun id (not_scanned, scanned) ->
71+
let kind = V.Map.find id kinds in
72+
if is_gc_ignorable kind
73+
then ((id, kind) :: not_scanned, scanned)
74+
else (not_scanned, (id, kind)::scanned))
75+
fv ([], [])
7176

7277
(* Auxiliary for accessing globals. We change the name of the global
7378
to the name of the corresponding asm symbol. This is done here
@@ -532,10 +537,10 @@ let simplif_prim_pure ~backend fpc p (args, approxs) dbg =
532537
(Uprim(p, args, dbg), Value_tuple (mode, Array.of_list approxs))
533538
end
534539
(* Field access *)
535-
| Pfield n, _, [ Value_const(Uconst_ref(_, Some (Uconst_block(_, l)))) ]
540+
| Pfield (n, _), _, [ Value_const(Uconst_ref(_, Some (Uconst_block(_, l)))) ]
536541
when n < List.length l ->
537542
make_const (List.nth l n)
538-
| Pfield n, [ Uprim(P.Pmakeblock _, ul, _) ], [approx]
543+
| Pfield (n, _), [ Uprim(P.Pmakeblock _, ul, _) ], [approx]
539544
when n < List.length ul ->
540545
(* This case is particularly useful for removing allocations
541546
for optional parameters *)
@@ -943,7 +948,7 @@ let check_constant_result ulam approx =
943948
let glb =
944949
Uprim(P.Pread_symbol id, [], Debuginfo.none)
945950
in
946-
Uprim(P.Pfield i, [glb], Debuginfo.none), approx
951+
Uprim(P.Pfield (i, Lambda.layout_any_value), [glb], Debuginfo.none), approx
947952
end
948953
| _ -> (ulam, approx)
949954

@@ -1308,7 +1313,7 @@ let rec close ({ backend; fenv; cenv ; mutable_vars; kinds; catch_env } as env)
13081313
| Lprim(Pfield (n, _), [lam], loc) ->
13091314
let (ulam, approx) = close env lam in
13101315
let dbg = Debuginfo.from_location loc in
1311-
check_constant_result (Uprim(P.Pfield n, [ulam], dbg))
1316+
check_constant_result (Uprim(P.Pfield (n, Lambda.layout_any_value), [ulam], dbg))
13121317
(field_approx n approx)
13131318
| Lprim(Psetfield(n, is_ptr, init),
13141319
[Lprim(Pgetglobal cu, [], _); lam], loc) ->
@@ -1647,9 +1652,9 @@ and close_functions { backend; fenv; cenv; mutable_vars; kinds; catch_env } fun_
16471652
if !useless_env then [], [] else not_scanned_fv, scanned_fv in
16481653
let env = { backend; fenv; cenv; mutable_vars; kinds; catch_env } in
16491654
(Uclosure {
1650-
functions = clos ;
1651-
not_scanned_slots = List.map (close_var env) not_scanned_fv ;
1652-
scanned_slots = List.map (close_var env) scanned_fv
1655+
functions = clos;
1656+
not_scanned_slots = List.map (fun (id, _kind) -> close_var env id) not_scanned_fv;
1657+
scanned_slots = List.map (fun (id, _kind) -> close_var env id) scanned_fv
16531658
},
16541659
infos)
16551660

middle_end/convert_primitives.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let convert (prim : Lambda.primitive) : Clambda_primitives.primitive =
2828
Pmakeblock (tag, mutability, shape, mode)
2929
| Pmakefloatblock (mutability, mode) ->
3030
Pmakearray (Pfloatarray, mutability, mode)
31-
| Pfield (field, _) -> Pfield field
31+
| Pfield (field, _sem) -> Pfield (field, Pvalue Pgenval)
3232
| Pfield_computed _sem -> Pfield_computed
3333
| Psetfield (field, imm_or_pointer, init_or_assign) ->
3434
Psetfield (field, imm_or_pointer, init_or_assign)

middle_end/flambda/build_export_info.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ and descr_of_named (env : Env.t) (named : Flambda.named)
290290
Value_block (Tag.create_exn tag, Array.of_list approxs)
291291
in
292292
Value_id (Env.new_descr env descr)
293-
| Prim (Pfield i, [arg], _) ->
293+
| Prim (Pfield (i, _), [arg], _) ->
294294
begin match Env.get_descr env (Env.find_approx env arg) with
295295
| Some (Value_block (_, fields)) when Array.length fields > i -> fields.(i)
296296
| _ -> Value_unknown

middle_end/flambda/closure_conversion.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ let tupled_function_call_stub original_params unboxed_version ~closure_bound_var
9696
let _, body =
9797
List.fold_left (fun (pos, body) param ->
9898
let lam : Flambda.named =
99-
Prim (Pfield pos, [tuple_param_var], Debuginfo.none)
99+
Prim (Pfield (pos, Pvalue Pgenval), [tuple_param_var], Debuginfo.none)
100100
in
101101
pos + 1, Flambda.create_let param lam body)
102102
(0, call) params
@@ -733,9 +733,9 @@ let lambda_to_flambda ~backend ~compilation_unit ~size lam
733733
Flambda.create_let
734734
sym_v (Symbol block_symbol)
735735
(Flambda.create_let result_v
736-
(Prim (Pfield 0, [sym_v], Debuginfo.none))
736+
(Prim (Pfield (0, Pvalue Pgenval), [sym_v], Debuginfo.none))
737737
(Flambda.create_let value_v
738-
(Prim (Pfield pos, [result_v], Debuginfo.none))
738+
(Prim (Pfield (pos, Pvalue Pgenval), [result_v], Debuginfo.none))
739739
(Var value_v))))
740740
in
741741
let module_initializer : Flambda.program_body =

middle_end/flambda/extract_projections.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ let rec analyse_expr ~which_variables expr =
125125
when Variable.Map.mem move.closure which_variables ->
126126
projections :=
127127
Projection.Set.add (Move_within_set_of_closures move) !projections
128-
| Prim (Pfield field_index, [var], _dbg)
128+
| Prim (Pfield (field_index, Pvalue _), [var], _dbg)
129129
when Variable.Map.mem var which_variables ->
130130
projections :=
131131
Projection.Set.add (Field (field_index, var)) !projections

middle_end/flambda/flambda_to_clambda.ml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ and to_clambda_named t env var (named : Flambda.named) : Clambda.ulambda * Lambd
469469
Flambda.print_named named
470470
end
471471
| Read_symbol_field (symbol, field) ->
472-
Uprim (Pfield field, [to_clambda_symbol env symbol], Debuginfo.none),
472+
Uprim (Pfield (field, Pvalue Pgenval),
473+
[to_clambda_symbol env symbol], Debuginfo.none),
473474
Lambda.layout_any_value
474475
| Set_of_closures set_of_closures ->
475476
to_clambda_set_of_closures t env set_of_closures,
@@ -502,14 +503,20 @@ and to_clambda_named t env var (named : Flambda.named) : Clambda.ulambda * Lambd
502503
let fun_offset = get_fun_offset t closure_id in
503504
let var_offset = get_fv_offset t var in
504505
let pos = var_offset - fun_offset in
505-
Uprim (Pfield pos,
506+
Uprim (Pfield (pos, kind),
506507
[check_field t (check_closure t ulam (Expr (Var closure)))
507508
pos (Some named)],
508509
Debuginfo.none),
509510
kind
510-
| Prim (Pfield index, [block], dbg) ->
511+
| Prim (Pfield (index, layout), [block], dbg) ->
512+
begin match layout with
513+
| Pvalue _ -> ()
514+
| _ ->
515+
Misc.fatal_errorf "Pfield can only be of layout value %a"
516+
Flambda.print_named named
517+
end;
511518
let block, _block_layout = subst_var env block in
512-
Uprim (Pfield index, [check_field t block index None], dbg),
519+
Uprim (Pfield (index, layout), [check_field t block index None], dbg),
513520
Lambda.layout_field
514521
| Prim (Psetfield (index, maybe_ptr, init), [block; new_value], dbg) ->
515522
let block, _block_layout = subst_var env block in
@@ -649,7 +656,7 @@ and to_clambda_set_of_closures t env
649656
in
650657
let pos = var_offset - fun_offset in
651658
Env.add_subst env id
652-
(Uprim (Pfield pos, [Clambda.Uvar env_var], Debuginfo.none))
659+
(Uprim (Pfield (pos, spec_to.kind), [Clambda.Uvar env_var], Debuginfo.none))
653660
spec_to.kind
654661
in
655662
let env = Variable.Map.fold add_env_free_variable free_vars env in

middle_end/flambda/flambda_utils.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ let substitute_read_symbol_field_for_variables
567567
Expr (
568568
Flambda.create_let block (make_named t)
569569
(Flambda.create_let field
570-
(Prim (Pfield h, [block], Debuginfo.none))
570+
(Prim (Pfield (h, Pvalue Pgenval), [block], Debuginfo.none))
571571
(Var field)))
572572
in
573573
Flambda.create_let fresh_var (make_named path) expr
@@ -930,7 +930,7 @@ let projection_to_named (projection : Projection.t) : Flambda.named =
930930
| Project_closure project_closure -> Project_closure project_closure
931931
| Move_within_set_of_closures move -> Move_within_set_of_closures move
932932
| Field (field_index, var) ->
933-
Prim (Pfield field_index, [var], Debuginfo.none)
933+
Prim (Pfield (field_index, Pvalue Pgenval), [var], Debuginfo.none)
934934

935935
type specialised_to_same_as =
936936
| Not_specialised

middle_end/flambda/inline_and_simplify.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ and simplify_named env r (tree : Flambda.named) : Flambda.named * R.t =
10711071
in
10721072
begin match prim, args, args_approxs with
10731073
(* CR-someday mshinwell: Optimise [Pfield_computed]. *)
1074-
| Pfield field_index, [arg], [arg_approx] ->
1074+
| Pfield (field_index, _), [arg], [arg_approx] ->
10751075
let projection : Projection.t = Field (field_index, arg) in
10761076
begin match E.find_projection env ~projection with
10771077
| Some var ->

middle_end/flambda/lift_constants.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ let assign_symbols_and_collect_constant_definitions
8484
| Project_closure ({ closure_id } as project_closure) ->
8585
assign_existing_symbol (Symbol_utils.Flambda.for_closure closure_id);
8686
record_definition (AA.Project_closure project_closure)
87-
| Prim (Pfield index, [block], _) ->
87+
| Prim (Pfield (index, Pvalue _), [block], _) ->
8888
record_definition (AA.Field (block, index))
89+
| Prim (Pfield (_, _), [_], _) ->
90+
Misc.fatal_errorf "[Pfield] with kind not value is not expected to be\
91+
constant: @.%a@."
92+
Flambda.print_named named
8993
| Prim (Pfield _, _, _) ->
9094
Misc.fatal_errorf "[Pfield] with the wrong number of arguments"
9195
Flambda.print_named named

middle_end/flambda/ref_to_variables.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ let eliminate_ref_of_expr flam =
159159
flam
160160
and aux_named (named : Flambda.named) : Flambda.named =
161161
match named with
162-
| Prim(Pfield field, [v], _)
162+
| Prim(Pfield (field, _), [v], _)
163163
when convertible_variable v ->
164164
(match get_variable v field with
165165
| None -> Expr Proved_unreachable

middle_end/printclambda_primitives.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ let primitive ppf (prim:Clambda_primitives.primitive) =
7272
in
7373
let name = "make" ^ mode ^ mut in
7474
fprintf ppf "%s %i%a" name tag Printlambda.block_shape shape
75-
| Pfield n -> fprintf ppf "field %i" n
75+
| Pfield (n, layout) -> fprintf ppf "field%a %i" Printlambda.layout layout n
7676
| Pfield_computed -> fprintf ppf "field_computed"
7777
| Psetfield(n, ptr, init) ->
7878
let instr =

0 commit comments

Comments
 (0)