Skip to content

Commit a8f6aae

Browse files
authored
flambda-backend: Fix bug in Clambda_primitives.result_layout. (#1833)
1 parent 4b2a6f6 commit a8f6aae

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

lambda/lambda.ml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,18 @@ let structured_constant_layout = function
14871487
| Const_block _ | Const_immstring _ -> Pvalue Pgenval
14881488
| Const_float_array _ | Const_float_block _ -> Pvalue (Parrayval Pfloatarray)
14891489

1490+
let layout_of_native_repr : Primitive.native_repr -> _ = function
1491+
| Untagged_int -> layout_int
1492+
| Unboxed_vector v -> layout_boxed_vector v
1493+
| Unboxed_float -> layout_boxed_float
1494+
| Unboxed_integer bi -> layout_boxedint bi
1495+
| Same_as_ocaml_repr s ->
1496+
begin match s with
1497+
| Value -> layout_any_value
1498+
| Float64 -> layout_unboxed_float
1499+
| Void -> assert false
1500+
end
1501+
14901502
let primitive_result_layout (p : primitive) =
14911503
match p with
14921504
| Popaque layout | Pobj_magic layout -> layout
@@ -1504,17 +1516,7 @@ let primitive_result_layout (p : primitive) =
15041516
| Paddfloat _ | Psubfloat _ | Pmulfloat _ | Pdivfloat _
15051517
| Pbox_float _ -> layout_boxed_float
15061518
| Punbox_float -> Punboxed_float
1507-
| Pccall { prim_native_repr_res = _, Untagged_int; _} -> layout_int
1508-
| Pccall { prim_native_repr_res = _, Unboxed_vector v; _} -> layout_boxed_vector v
1509-
| Pccall { prim_native_repr_res = _, Unboxed_float; _} -> layout_boxed_float
1510-
| Pccall { prim_native_repr_res = _, Same_as_ocaml_repr s; _} ->
1511-
begin match s with
1512-
| Value -> layout_any_value
1513-
| Float64 -> layout_unboxed_float
1514-
| Void -> assert false
1515-
end
1516-
| Pccall { prim_native_repr_res = _, Unboxed_integer bi; _} ->
1517-
layout_boxedint bi
1519+
| Pccall { prim_native_repr_res = _, repr_res } -> layout_of_native_repr repr_res
15181520
| Praise _ -> layout_bottom
15191521
| Psequor | Psequand | Pnot
15201522
| Pnegint | Paddint | Psubint | Pmulint

lambda/lambda.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ val equal_boxed_vector_size : boxed_vector -> boxed_vector -> bool
309309

310310
val must_be_value : layout -> value_kind
311311

312+
(* This is the layout of ocaml values used as arguments to or returned from
313+
primitives for this [native_repr]. So the legacy [Unboxed_float] - which is
314+
a float that is unboxed before being passed to a C function - is mapped to
315+
[layout_any_value], while [Same_as_ocaml_repr Float64] is mapped to
316+
[layout_unboxed_float].
317+
*)
318+
val layout_of_native_repr : Primitive.native_repr -> layout
319+
312320
type structured_constant =
313321
Const_base of constant
314322
| Const_block of int * structured_constant list

middle_end/clambda_primitives.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,6 @@ let result_layout (p : primitive) =
211211
match p with
212212
| Punbox_float -> Lambda.Punboxed_float
213213
| Punbox_int bi -> Lambda.Punboxed_int bi
214+
| Pccall {prim_native_repr_res = (_, repr_res); _} ->
215+
Lambda.layout_of_native_repr repr_res
214216
| _ -> Lambda.layout_any_value

testsuite/tests/typing-layouts-float64/c_api.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ let sum_of_one_to_seven =
5959
in
6060
print_floatu "Function with many args, sum_of_one_to_seven" f
6161

62+
(* Non-inlined eta expansion *)
63+
let[@inline never] sin_U_U' x = sin_U_U x
6264

65+
let sin_seven =
66+
let f = sin_U_U' (of_float 7.) in
67+
print_floatu "Test U -> U eta expansion, sin seven" f

testsuite/tests/typing-layouts-float64/c_api.reference

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Test U -> B, sin four: -0.76
44
Test (B[@unboxed]) -> U, sin five: -0.96
55
Test U -> (B[@unboxed]), sin six: -0.28
66
Function with many args, sum_of_one_to_seven: 28.00
7+
Test U -> U eta expansion, sin seven: 0.66

0 commit comments

Comments
 (0)