Skip to content

Commit 729bf71

Browse files
riaqngoldfirere
andauthored
flambda-backend: Mode cross unboxed float (#2417)
* jkind mode cross unboxed float --------- Co-authored-by: Richard Eisenberg <[email protected]>
1 parent 628b3e9 commit 729bf71

File tree

3 files changed

+74
-54
lines changed

3 files changed

+74
-54
lines changed

testsuite/tests/typing-modal-kinds/basics.ml

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ module Hidden_int : sig type t : immediate val hide : int -> t end
3333
module Hidden_float_u : sig type t : float64 val hide : float# -> t end
3434
|}]
3535

36+
module Immediate : sig
37+
val id : ('a : immediate). 'a -> 'a
38+
val ignore : ('a : immediate). 'a -> unit
39+
val unique : ('a : immediate). unique_ 'a -> 'a
40+
end = struct
41+
let id x = x
42+
let ignore _ = ()
43+
let unique (unique_ x) = x
44+
end
45+
46+
[%%expect{|
47+
module Immediate :
48+
sig
49+
val id : ('a : immediate). 'a -> 'a
50+
val ignore : ('a : immediate). 'a -> unit
51+
val unique : ('a : immediate). unique_ 'a -> 'a
52+
end
53+
|}]
54+
3655
module Float_u : sig
3756
val id : ('a : float64). 'a -> 'a
3857
val ignore : ('a : float64). 'a -> unit
@@ -141,24 +160,17 @@ Line 1, characters 50-51:
141160
Error: This value escapes its region
142161
|}]
143162

144-
let float_u_escape = let local_ x : float# = #3.14 in x
163+
let float_u_escape () = let local_ x : float# = #3.14 in x
145164

146-
(* CR layouts v2.8: this should succeed *)
147165
[%%expect{|
148-
Line 1, characters 54-55:
149-
1 | let float_u_escape = let local_ x : float# = #3.14 in x
150-
^
151-
Error: This value escapes its region
166+
val float_u_escape : unit -> float# = <fun>
152167
|}]
153168

154-
let hidden_float_u_escape =
169+
let hidden_float_u_escape () =
155170
let local_ x : Hidden_float_u.t = Hidden_float_u.hide #3.14 in x
156171

157172
[%%expect{|
158-
Line 2, characters 65-66:
159-
2 | let local_ x : Hidden_float_u.t = Hidden_float_u.hide #3.14 in x
160-
^
161-
Error: This value escapes its region
173+
val hidden_float_u_escape : unit -> Hidden_float_u.t = <fun>
162174
|}]
163175

164176
let float_u_record_escape =
@@ -188,13 +200,9 @@ let foo () =
188200
(* [r.x] is allocated global and can escape. *)
189201
r.x
190202

191-
(* CR layouts v2.8: this should succeed *)
192203
[%%expect{|
193204
type r = { x : float; y : float; }
194-
Line 6, characters 2-5:
195-
6 | r.x
196-
^^^
197-
Error: This value escapes its region
205+
val foo : unit -> float = <fun>
198206
|}]
199207

200208
let function_escape = let local_ x : int -> int = fun y -> y in x
@@ -304,24 +312,17 @@ Line 1, characters 59-60:
304312
Error: Found a once value where a many value was expected
305313
|}]
306314

307-
let float_u_duplicate = let once_ x : float# = #3.14 in Float_u.id x
315+
let float_u_duplicate () = let once_ x : float# = #3.14 in Float_u.id x
308316

309-
(* CR layouts v2.8: this should succeed *)
310317
[%%expect{|
311-
Line 1, characters 67-68:
312-
1 | let float_u_duplicate = let once_ x : float# = #3.14 in Float_u.id x
313-
^
314-
Error: Found a once value where a many value was expected
318+
val float_u_duplicate : unit -> float# = <fun>
315319
|}]
316320

317-
let hidden_float_u_duplicate =
321+
let hidden_float_u_duplicate () =
318322
let once_ x : Hidden_float_u.t = Hidden_float_u.hide #3.14 in Float_u.id x
319323

320324
[%%expect{|
321-
Line 2, characters 75-76:
322-
2 | let once_ x : Hidden_float_u.t = Hidden_float_u.hide #3.14 in Float_u.id x
323-
^
324-
Error: Found a once value where a many value was expected
325+
val hidden_float_u_duplicate : unit -> Hidden_float_u.t = <fun>
325326
|}]
326327

327328
let float_u_record_duplicate =
@@ -493,29 +494,50 @@ Line 1, characters 51-52:
493494

494495
|}]
495496

496-
let float_u_unshare = let x : float# = #3.14 in Float_u.ignore x; Float_u.unique x
497+
(* CR layouts v2.8: The following should pass, even in principal mode, because the
498+
argument kind is known to cross mode. *)
499+
500+
let float_u_unshare () = let x : float# = #3.14 in Float_u.ignore x; Float_u.unique x
497501

498-
(* CR layouts v2.8: this should succeed *)
499502
[%%expect{|
500-
Line 1, characters 81-82:
501-
1 | let float_u_unshare = let x : float# = #3.14 in Float_u.ignore x; Float_u.unique x
502-
^
503+
val float_u_unshare : unit -> float# = <fun>
504+
|}, Principal{|
505+
Line 1, characters 84-85:
506+
1 | let float_u_unshare () = let x : float# = #3.14 in Float_u.ignore x; Float_u.unique x
507+
^
503508
Error: This value is used here as unique, but it has already been used:
504-
Line 1, characters 63-64:
505-
1 | let float_u_unshare = let x : float# = #3.14 in Float_u.ignore x; Float_u.unique x
506-
^
509+
Line 1, characters 66-67:
510+
1 | let float_u_unshare () = let x : float# = #3.14 in Float_u.ignore x; Float_u.unique x
511+
^
512+
513+
|}]
507514

515+
let imm_escape () = Immediate.id (local_ 42) [@nontail]
516+
517+
[%%expect{|
518+
val imm_escape : unit -> int = <fun>
519+
|}, Principal{|
520+
Line 1, characters 33-44:
521+
1 | let imm_escape () = Immediate.id (local_ 42) [@nontail]
522+
^^^^^^^^^^^
523+
Error: This value escapes its region
508524
|}]
509525

510-
let hidden_float_u_unshare =
526+
let hidden_float_u_unshare () =
511527
let x : Hidden_float_u.t = Hidden_float_u.hide #3.14 in
512528
Float_u.ignore x; Float_u.unique x
513529

514530
[%%expect{|
531+
val hidden_float_u_unshare : unit -> Hidden_float_u.t = <fun>
532+
|}, Principal{|
515533
Line 3, characters 35-36:
516534
3 | Float_u.ignore x; Float_u.unique x
517535
^
518-
Error: Found a shared value where a unique value was expected
536+
Error: This value is used here as unique, but it has already been used:
537+
Line 3, characters 17-18:
538+
3 | Float_u.ignore x; Float_u.unique x
539+
^
540+
519541
|}]
520542

521543
let float_u_record_unshare =

testsuite/tests/typing-modal-kinds/expected_mode.ml

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,13 @@ Line 1, characters 56-57:
115115
Error: This value escapes its region
116116
|}]
117117

118+
(* CR layouts v2.8: The following should pass, even in principal mode. *)
118119
let float_u_escape : local_ _ -> (float#, float#) Float_u.pair =
119120
fun x -> Float_u.mk_pair x x [@nontail]
120121

121122
[%%expect{|
123+
val float_u_escape : local_ float# -> (float#, float#) Float_u.pair = <fun>
124+
|}, Principal{|
122125
Line 2, characters 27-28:
123126
2 | fun x -> Float_u.mk_pair x x [@nontail]
124127
^
@@ -130,6 +133,10 @@ let hidden_float_u_escape :
130133
fun x -> Float_u.mk_pair x x [@nontail]
131134

132135
[%%expect{|
136+
val hidden_float_u_escape :
137+
local_ Hidden_float_u.t ->
138+
(Hidden_float_u.t, Hidden_float_u.t) Float_u.pair = <fun>
139+
|}, Principal{|
133140
Line 3, characters 27-28:
134141
3 | fun x -> Float_u.mk_pair x x [@nontail]
135142
^
@@ -238,19 +245,14 @@ Error: Found a once value where a many value was expected
238245
let float_u_duplicate : once_ _ -> float# = fun x -> x
239246

240247
[%%expect{|
241-
Line 1, characters 53-54:
242-
1 | let float_u_duplicate : once_ _ -> float# = fun x -> x
243-
^
244-
Error: Found a once value where a many value was expected
248+
val float_u_duplicate : once_ float# -> float# = <fun>
245249
|}]
246250

247251
let hidden_float_u_duplicate : once_ _ -> Hidden_float_u.t = fun x -> x
248252

249253
[%%expect{|
250-
Line 1, characters 70-71:
251-
1 | let hidden_float_u_duplicate : once_ _ -> Hidden_float_u.t = fun x -> x
252-
^
253-
Error: Found a once value where a many value was expected
254+
val hidden_float_u_duplicate : once_ Hidden_float_u.t -> Hidden_float_u.t =
255+
<fun>
254256
|}]
255257

256258
let float_u_record_duplicate : once_ _ -> float_u_record =
@@ -355,19 +357,14 @@ Error: Found a shared value where a unique value was expected
355357
let float_u_unshare : _ -> unique_ float# = fun x -> x
356358

357359
[%%expect{|
358-
Line 1, characters 53-54:
359-
1 | let float_u_unshare : _ -> unique_ float# = fun x -> x
360-
^
361-
Error: Found a shared value where a unique value was expected
360+
val float_u_unshare : float# -> unique_ float# = <fun>
362361
|}]
363362

364363
let hidden_float_u_unshare : _ -> unique_ Hidden_float_u.t = fun x -> x
365364

366365
[%%expect{|
367-
Line 1, characters 70-71:
368-
1 | let hidden_float_u_unshare : _ -> unique_ Hidden_float_u.t = fun x -> x
369-
^
370-
Error: Found a shared value where a unique value was expected
366+
val hidden_float_u_unshare : Hidden_float_u.t -> unique_ Hidden_float_u.t =
367+
<fun>
371368
|}]
372369

373370
let float_u_record_unshare : _ -> unique_ float_u_record =

typing/jkind.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@ module Jkind_desc = struct
717717

718718
let float64 =
719719
{ layout = Layout.float64;
720-
modes_upper_bounds = Modes.max;
720+
modes_upper_bounds =
721+
{ locality = Global; linearity = Many; uniqueness = Unique };
721722
externality_upper_bound = External
722723
}
723724

0 commit comments

Comments
 (0)