Skip to content

Commit 1ad7252

Browse files
authored
flambda-backend: Revert "Revert "Add a proper top and bottom layout" (#1169)" (#1191)
This reverts commit 5123d61.
1 parent dea4b3e commit 1ad7252

File tree

12 files changed

+121
-27
lines changed

12 files changed

+121
-27
lines changed

asmcomp/cmm_helpers.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,7 +2239,11 @@ let rec make_curry_apply result narity args_type args clos n =
22392239
:: args)
22402240
newclos (n - 1) )
22412241

2242-
let machtype_of_layout = function Lambda.Pvalue _ -> typ_val
2242+
let machtype_of_layout (layout : Lambda.layout) =
2243+
match layout with
2244+
| Ptop -> Misc.fatal_error "No machtype for layout [Ptop]"
2245+
| Pbottom -> Misc.fatal_error "No unique machtype for layout [Pbottom]"
2246+
| Pvalue _ -> typ_val
22432247

22442248
let final_curry_function nlocal arity result =
22452249
let last_arg = V.create_local "arg" in
@@ -3140,5 +3144,9 @@ let emit_preallocated_blocks preallocated_blocks cont =
31403144
let c1 = emit_gc_roots_table ~symbols cont in
31413145
List.fold_left preallocate_block c1 preallocated_blocks
31423146

3143-
let kind_of_layout (Lambda.Pvalue kind) = Vval kind
3144-
3147+
let kind_of_layout (layout : Lambda.layout) =
3148+
match layout with
3149+
| Ptop | Pbottom ->
3150+
(* This is incorrect but only used for unboxing *)
3151+
Vval Pgenval
3152+
| Pvalue kind -> Vval kind

asmcomp/cmmgen.ml

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,19 @@ let rec transl env e =
448448
| Ugeneric_apply(clos, args, args_layout, result_layout, kind, dbg) ->
449449
let clos = transl env clos in
450450
let args = List.map (transl env) args in
451-
let args_type = List.map machtype_of_layout args_layout in
452-
let return = machtype_of_layout result_layout in
453-
generic_apply (mut_from_env env clos) clos args args_type return kind dbg
451+
if List.mem Pbottom args_layout then
452+
(* [machtype_of_layout] will fail on Pbottom, convert it to a sequence
453+
and remove the call, preserving the execution order. *)
454+
List.fold_left2 (fun rest arg arg_layout ->
455+
if arg_layout = Pbottom then
456+
arg
457+
else
458+
Csequence(remove_unit arg, rest)
459+
) (Ctuple []) args args_layout
460+
else
461+
let args_type = List.map machtype_of_layout args_layout in
462+
let return = machtype_of_layout result_layout in
463+
generic_apply (mut_from_env env clos) clos args args_type return kind dbg
454464
| Usend(kind, met, obj, args, args_layout, result_layout, pos, dbg) ->
455465
let met = transl env met in
456466
let obj = transl env obj in
@@ -731,14 +741,23 @@ and transl_catch (kind : Cmm.value_kind) env nfail ids body handler dbg =
731741
each argument. *)
732742
let report args =
733743
List.iter2
734-
(fun (_id, Pvalue kind, u) c ->
735-
let strict =
736-
match kind with
737-
| Pfloatval | Pboxedintval _ -> false
738-
| Pintval | Pgenval | Pvariant _ | Parrayval _ -> true
739-
in
740-
u := join_unboxed_number_kind ~strict !u
741-
(is_unboxed_number_cmm ~strict c)
744+
(fun (id, (layout : Lambda.layout), u) c ->
745+
match layout with
746+
| Ptop ->
747+
Misc.fatal_errorf "Variable %a with layout [Ptop] can't be compiled"
748+
VP.print id
749+
| Pbottom ->
750+
Misc.fatal_errorf
751+
"Variable %a with layout [Pbottom] can't be compiled"
752+
VP.print id
753+
| Pvalue kind ->
754+
let strict =
755+
match kind with
756+
| Pfloatval | Pboxedintval _ -> false
757+
| Pintval | Pgenval | Pvariant _ | Parrayval _ -> true
758+
in
759+
u := join_unboxed_number_kind ~strict !u
760+
(is_unboxed_number_cmm ~strict c)
742761
)
743762
ids args
744763
in
@@ -1181,7 +1200,7 @@ and transl_unbox_sized size dbg env exp =
11811200
| Thirty_two -> transl_unbox_int dbg env Pint32 exp
11821201
| Sixty_four -> transl_unbox_int dbg env Pint64 exp
11831202

1184-
and transl_let env str (Pvalue kind : Lambda.layout) id exp transl_body =
1203+
and transl_let_value env str (kind : Lambda.value_kind) id exp transl_body =
11851204
let dbg = Debuginfo.none in
11861205
let cexp = transl env exp in
11871206
let unboxing =
@@ -1233,6 +1252,20 @@ and transl_let env str (Pvalue kind : Lambda.layout) id exp transl_body =
12331252
| Mutable, bn -> Clet_mut (v, typ_of_boxed_number bn, cexp, body)
12341253
end
12351254

1255+
and transl_let env str (layout : Lambda.layout) id exp transl_body =
1256+
match layout with
1257+
| Ptop ->
1258+
Misc.fatal_errorf "Variable %a with layout [Ptop] can't be compiled"
1259+
VP.print id
1260+
| Pbottom ->
1261+
let cexp = transl env exp in
1262+
(* N.B. [body] must still be traversed even if [exp] will never return:
1263+
there may be constant closures inside that need lifting out. *)
1264+
let _cbody : expression = transl_body env in
1265+
cexp
1266+
| Pvalue kind ->
1267+
transl_let_value env str kind id exp transl_body
1268+
12361269
and make_catch (kind : Cmm.value_kind) ncatch body handler dbg = match body with
12371270
| Cexit (nexit,[]) when nexit=ncatch -> handler
12381271
| _ -> ccatch (ncatch, [], body, handler, dbg, kind)

lambda/lambda.ml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ and value_kind =
255255
| Parrayval of array_kind
256256

257257
and layout =
258+
| Ptop
258259
| Pvalue of value_kind
260+
| Pbottom
259261

260262
and block_shape =
261263
value_kind list option
@@ -316,14 +318,30 @@ let rec equal_value_kind x y =
316318
| (Pgenval | Pfloatval | Pboxedintval _ | Pintval | Pvariant _
317319
| Parrayval _), _ -> false
318320

319-
let equal_layout (Pvalue x) (Pvalue y) = equal_value_kind x y
321+
let equal_layout x y =
322+
match x, y with
323+
| Pvalue x, Pvalue y -> equal_value_kind x y
324+
| Ptop, Ptop -> true
325+
| Pbottom, Pbottom -> true
326+
| _, _ -> false
320327

321-
let compatible_layout (Pvalue _) (Pvalue _) = true
328+
let compatible_layout x y =
329+
match x, y with
330+
| Pbottom, _
331+
| _, Pbottom -> true
332+
| Pvalue _, Pvalue _ -> true
333+
| Ptop, Ptop -> true
334+
| Ptop, _ | _, Ptop -> false
322335

323336
let must_be_value layout =
324337
match layout with
325338
| Pvalue v -> v
326-
(* | _ -> Misc.fatal_error "Layout is not a value" *)
339+
| Pbottom ->
340+
(* Here, we want to get the [value_kind] corresponding to
341+
a [Pbottom] layout. Anything will do, we return [Pgenval]
342+
as a default. *)
343+
Pgenval
344+
| _ -> Misc.fatal_error "Layout is not a value"
327345

328346
type structured_constant =
329347
Const_base of constant
@@ -601,10 +619,9 @@ let layout_lazy_contents = Pvalue Pgenval
601619
let layout_any_value = Pvalue Pgenval
602620
let layout_letrec = layout_any_value
603621

604-
let layout_top = Pvalue Pgenval
605-
let layout_bottom =
606-
(* CR pchambart: this should be an actual bottom *)
607-
Pvalue Pgenval
622+
(* CR ncourant: use [Ptop] or remove this as soon as possible. *)
623+
let layout_top = layout_any_value
624+
let layout_bottom = Pbottom
608625

609626
let default_function_attribute = {
610627
inline = Default_inline;

lambda/lambda.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ and value_kind =
212212
| Parrayval of array_kind
213213

214214
and layout =
215+
| Ptop
215216
| Pvalue of value_kind
217+
| Pbottom
216218

217219
and block_shape =
218220
value_kind list option

lambda/printlambda.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ and value_kind' ppf = function
9595
| Pvariant { consts; non_consts; } ->
9696
variant_kind value_kind' ppf ~consts ~non_consts
9797

98-
let layout ppf (Pvalue k) = value_kind ppf k
98+
let layout ppf layout =
99+
match layout with
100+
| Pvalue k -> value_kind ppf k
101+
| Ptop -> fprintf ppf "[top]"
102+
| Pbottom -> fprintf ppf "[bottom]"
99103

100104
let return_kind ppf (mode, kind) =
101105
let smode = alloc_mode mode in
@@ -109,6 +113,8 @@ let return_kind ppf (mode, kind) =
109113
| Pvalue (Pboxedintval bi) -> fprintf ppf ": %s%s@ " smode (boxed_integer_name bi)
110114
| Pvalue (Pvariant { consts; non_consts; }) ->
111115
variant_kind value_kind' ppf ~consts ~non_consts
116+
| Ptop -> fprintf ppf ": top@ "
117+
| Pbottom -> fprintf ppf ": bottom@ "
112118

113119
let field_kind ppf = function
114120
| Pgenval -> pp_print_string ppf "*"

middle_end/clambda_primitives.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ and value_kind = Lambda.value_kind =
142142
| Parrayval of array_kind
143143

144144
and layout = Lambda.layout =
145+
| Ptop
145146
| Pvalue of value_kind
147+
| Pbottom
146148

147149
and block_shape = Lambda.block_shape
148150
and boxed_integer = Primitive.boxed_integer =

middle_end/clambda_primitives.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ and value_kind = Lambda.value_kind =
145145
| Parrayval of array_kind
146146

147147
and layout = Lambda.layout =
148+
| Ptop
148149
| Pvalue of value_kind
150+
| Pbottom
149151

150152
and block_shape = Lambda.block_shape
151153
and boxed_integer = Primitive.boxed_integer =

middle_end/closure/closure.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ let rec add_to_closure_env env_param pos cenv = function
5959

6060
let is_gc_ignorable kind =
6161
match kind with
62+
| Ptop -> Misc.fatal_error "[Ptop] can't be stored in a closure."
63+
| Pbottom -> Misc.fatal_error "[Pbottom] should not be stored in a closure."
6264
| Pvalue Pintval -> true
6365
| Pvalue (Pgenval | Pfloatval | Pboxedintval _ | Pvariant _ | Parrayval _) -> false
6466

middle_end/flambda/closure_offsets.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ let add_closure_offsets
7070
in
7171
let gc_invisible_free_vars, gc_visible_free_vars =
7272
Variable.Map.partition (fun _ (free_var : Flambda.specialised_to) ->
73-
match free_var.kind with Pvalue Pintval -> true | Pvalue _ -> false)
73+
match free_var.kind with
74+
| Ptop ->
75+
Misc.fatal_error "[Ptop] can't be stored in a closure."
76+
| Pbottom ->
77+
Misc.fatal_error
78+
"[Pbottom] should have been eliminated as dead code \
79+
and not stored in a closure."
80+
| Pvalue Pintval -> true
81+
| Pvalue _ -> false)
7482
free_vars
7583
in
7684
let free_variable_offsets, free_variable_pos =

middle_end/flambda/flambda_to_clambda.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,11 @@ and to_clambda_set_of_closures t env
696696
let not_scanned_fv, scanned_fv =
697697
Variable.Map.partition (fun _ (free_var : Flambda.specialised_to) ->
698698
match free_var.kind with
699+
| Ptop -> Misc.fatal_error "[Ptop] can't be stored in a closure."
700+
| Pbottom ->
701+
Misc.fatal_error
702+
"[Pbottom] should have been eliminated as dead code \
703+
and not stored in a closure."
699704
| Pvalue Pintval -> true
700705
| Pvalue _ -> false)
701706
free_vars

middle_end/printclambda.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ let rec value_kind0 ppf kind =
5151
non_consts
5252

5353
let value_kind kind = Format.asprintf "%a" value_kind0 kind
54-
let layout (Lambda.Pvalue kind) = value_kind kind
54+
let layout (layout : Lambda.layout) =
55+
match layout with
56+
| Pvalue kind -> value_kind kind
57+
| Ptop -> ":top"
58+
| Pbottom -> ":bottom"
5559

5660
let rec structured_constant ppf = function
5761
| Uconst_float x -> fprintf ppf "%F" x

typing/typeopt.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,5 +411,10 @@ let value_kind_union (k1 : Lambda.value_kind) (k2 : Lambda.value_kind) =
411411
if Lambda.equal_value_kind k1 k2 then k1
412412
else Pgenval
413413

414-
let layout_union (Pvalue layout1) (Pvalue layout2) =
415-
Pvalue (value_kind_union layout1 layout2)
414+
let layout_union l1 l2 =
415+
match l1, l2 with
416+
| Pbottom, l
417+
| l, Pbottom -> l
418+
| Pvalue layout1, Pvalue layout2 ->
419+
Pvalue (value_kind_union layout1 layout2)
420+
| Ptop, _ | _, Ptop -> Ptop

0 commit comments

Comments
 (0)