forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjkind.ml
2103 lines (1843 loc) · 76.2 KB
/
jkind.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Chris Casinghino, Jane Street, New York *)
(* *)
(* Copyright 2021 Jane Street Group LLC *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
open Mode
open Jkind_types
[@@@warning "+9"]
(* A *sort* is the information the middle/back ends need to be able to
compile a manipulation (storing, passing, etc) of a runtime value. *)
module Sort = struct
include Jkind_types.Sort
module Flat = struct
type t =
| Var of Var.id
| Base of base
end
end
type sort = Sort.t
type type_expr = Types.type_expr
(* A *layout* of a type describes the way values of that type are stored at
runtime, including details like width, register convention, calling
convention, etc. A layout may be *representable* or *unrepresentable*. The
middle/back ends are unable to cope with values of types with an
unrepresentable layout. The only unrepresentable layout is `any`, which is
the top of the layout lattice. *)
module Layout = struct
open Jkind_types.Layout
type nonrec 'sort t = 'sort t =
| Sort of 'sort
| Product of 'sort t list
| Any
module Const = struct
type t = Const.t =
| Any
| Base of Sort.base
| Product of t list
let max = Any
let rec equal c1 c2 =
match c1, c2 with
| Base b1, Base b2 -> Sort.equal_base b1 b2
| Any, Any -> true
| Product cs1, Product cs2 -> List.equal equal cs1 cs2
| (Base _ | Any | Product _), _ -> false
module Static = struct
let value = Base Sort.Value
let void = Base Sort.Void
let float64 = Base Sort.Float64
let float32 = Base Sort.Float32
let word = Base Sort.Word
let bits32 = Base Sort.Bits32
let bits64 = Base Sort.Bits64
let vec128 = Base Sort.Vec128
let of_base : Sort.base -> t = function
| Value -> value
| Void -> void
| Float64 -> float64
| Float32 -> float32
| Word -> word
| Bits32 -> bits32
| Bits64 -> bits64
| Vec128 -> vec128
end
include Static
let rec get_sort : t -> Sort.Const.t option = function
| Any -> None
| Base b -> Some (Base b)
| Product ts ->
Option.map
(fun x -> Sort.Const.Product x)
(Misc.Stdlib.List.map_option get_sort ts)
let of_sort s =
let rec of_sort : Sort.t -> _ = function
| Var _ -> None
| Base b -> Some (Static.of_base b)
| Product sorts ->
Option.map
(fun x -> Product x)
(* [Sort.get] is deep, so no need to repeat it here *)
(Misc.Stdlib.List.map_option of_sort sorts)
in
of_sort (Sort.get s)
let of_flat_sort : Sort.Flat.t -> _ = function
| Var _ -> None
| Base b -> Some (Static.of_base b)
let rec of_sort_const : Sort.Const.t -> t = function
| Base b -> Base b
| Product consts -> Product (List.map of_sort_const consts)
let to_string t =
let rec to_string nested (t : t) =
match t with
| Any -> "any"
| Base b -> Sort.to_string_base b
| Product ts ->
String.concat ""
[ (if nested then "(" else "");
String.concat " & " (List.map (to_string true) ts);
(if nested then ")" else "") ]
in
to_string false t
module Debug_printers = struct
open Format
let t ppf t = fprintf ppf "%s" (to_string t)
end
end
module Debug_printers = struct
open Format
let rec t format_sort ppf = function
| Any -> fprintf ppf "Any"
| Sort s -> fprintf ppf "Sort %a" format_sort s
| Product ts ->
fprintf ppf "Product [ %a ]"
(pp_print_list
~pp_sep:(fun ppf () -> Format.fprintf ppf ";@ ")
(t format_sort))
ts
end
let rec of_const (const : Const.t) : _ t =
match const with
| Any -> Any
| Base b -> Sort (Sort.of_base b)
| Product cs -> Product (List.map of_const cs)
let rec to_sort = function
| Any -> None
| Sort s -> Some s
| Product ts -> to_product_sort ts
and to_product_sort ts =
Option.map
(fun x -> Sort.Product x)
(Misc.Stdlib.List.map_option to_sort ts)
let rec get : Sort.t t -> Sort.Flat.t t =
let rec flatten_sort : Sort.t -> Sort.Flat.t t = function
| Var v -> Sort (Var (Sort.Var.get_id v))
| Base b ->
Sort (Base b)
(* No need to call [Sort.get] here, because one [get] is deep. *)
| Product sorts -> Product (List.map flatten_sort sorts)
in
function
| Any -> Any
| Sort s -> flatten_sort (Sort.get s)
| Product ts -> Product (List.map get ts)
let rec get_const of_sort : _ t -> Const.t option = function
| Any -> Some Any
| Sort s -> of_sort s
| Product layouts ->
Option.map
(fun x -> Layout.Const.Product x)
(Misc.Stdlib.List.map_option (get_const of_sort) layouts)
let get_flat_const t = get_const Const.of_flat_sort t
let get_const t = get_const Const.of_sort t
let sort_equal_result ~allow_mutation result =
match (result : Sort.equate_result) with
| (Equal_mutated_first | Equal_mutated_second | Equal_mutated_both)
when not allow_mutation ->
Misc.fatal_errorf "Jkind.equal: Performed unexpected mutation"
| Unequal -> false
| Equal_no_mutation | Equal_mutated_first | Equal_mutated_second
| Equal_mutated_both ->
true
let rec equate_or_equal ~allow_mutation t1 t2 =
match t1, t2 with
| Sort s1, Sort s2 ->
sort_equal_result ~allow_mutation (Sort.equate_tracking_mutation s1 s2)
| Product ts, Sort sort | Sort sort, Product ts -> (
match to_product_sort ts with
| None -> false
| Some sort' ->
sort_equal_result ~allow_mutation
(Sort.equate_tracking_mutation sort sort'))
| Product ts1, Product ts2 ->
List.equal (equate_or_equal ~allow_mutation) ts1 ts2
| Any, Any -> true
| (Any | Sort _ | Product _), _ -> false
let rec sub t1 t2 : Misc.Le_result.t =
match t1, t2 with
| Any, Any -> Equal
| _, Any -> Less
| Any, _ -> Not_le
| Sort s1, Sort s2 -> if Sort.equate s1 s2 then Equal else Not_le
| Product ts1, Product ts2 ->
if List.compare_lengths ts1 ts2 = 0
then Misc.Le_result.combine_list (List.map2 sub ts1 ts2)
else Not_le
| Product ts1, Sort s2 -> (
match to_product_sort ts1 with
| None -> Not_le
| Some s1 -> if Sort.equate s1 s2 then Equal else Not_le)
| Sort s1, Product ts2 -> (
match to_product_sort ts2 with
| None -> Not_le
| Some s2 -> if Sort.equate s1 s2 then Equal else Not_le)
let rec intersection t1 t2 =
match t1, t2 with
| _, Any -> Some t1
| Any, _ -> Some t2
| Sort s1, Sort s2 -> if Sort.equate s1 s2 then Some t1 else None
| Product ts1, Product ts2 ->
if List.compare_lengths ts1 ts2 = 0
then
let components = List.map2 intersection ts1 ts2 in
Option.map
(fun x -> Product x)
(Misc.Stdlib.List.some_if_all_elements_are_some components)
else None
| (Product ts as t), Sort sort | Sort sort, (Product ts as t) -> (
match to_product_sort ts with
| None -> None
| Some sort' -> if Sort.equate sort sort' then Some t else None)
let of_new_sort_var () =
let sort = Sort.new_var () in
Sort sort, sort
let rec default_to_value_and_get : _ Layout.t -> Const.t = function
| Any -> Any
| Sort s -> Const.of_sort_const (Sort.default_to_value_and_get s)
| Product p -> Product (List.map default_to_value_and_get p)
let format ppf layout =
let open Format in
let rec pp_element ~nested ppf : _ Layout.t -> unit = function
| Any -> fprintf ppf "any"
| Sort s -> Sort.format ppf s
| Product ts ->
let pp_sep ppf () = Format.fprintf ppf " & " in
Misc.pp_nested_list ~nested ~pp_element ~pp_sep ppf ts
in
pp_element ~nested:false ppf layout
end
module Externality = Jkind_axis.Externality
module Nullability = Jkind_axis.Nullability
module Modes = Jkind_axis.Of_lattice (Alloc.Const)
module History = struct
include Jkind_intf.History
let is_imported t =
match t.history with Creation Imported -> true | _ -> false
(* CR layouts: Anything that returns false here could probably just be removed,
but let's keep the info around at least during development. *)
let is_informative t =
match t.history with Creation Imported -> false | _ -> true
let update_reason t reason = { t with history = Creation reason }
let with_warning t = { t with has_warned = true }
let has_warned t = t.has_warned
end
(*********************************)
(* Main type declarations *)
type +'d const = (type_expr, 'd) Jkind_types.Const.t
type 'd t = (type_expr, 'd) Jkind_types.t
type jkind_l = (allowed * disallowed) t
type packed = Pack : 'd t -> packed [@@unboxed]
include Allowance.Magic_allow_disallow (struct
type (_, _, 'd) sided = 'd t
let disallow_right ({ jkind = { layout = _; _ }; _ } as t) = t
let disallow_left ({ jkind = { layout = _; _ }; _ } as t) = t
let allow_right ({ jkind = { layout = _; _ }; _ } as t) = t
let allow_left ({ jkind = { layout = _; _ }; _ } as t) = t
end)
let terrible_relax_l ({ jkind = { layout = _; _ }; _ } as t) = t
let fresh_jkind jkind ~annotation ~why =
{ jkind; annotation; history = Creation why; has_warned = false }
(******************************)
(*** user errors ***)
module Error = struct
type t =
| Insufficient_level :
{ jkind : Parsetree.jkind_annotation;
required_layouts_level : Language_extension.maturity
}
-> t
| Unknown_jkind of Parsetree.jkind_annotation
| Multiple_jkinds of
{ from_annotation : Parsetree.jkind_annotation;
from_attribute : Builtin_attributes.jkind_attribute Location.loc
}
exception User_error of Location.t * t
end
let raise ~loc err = raise (Error.User_error (loc, err))
module Const = struct
open Jkind_types.Layout_and_axes
type +'d t = 'd const
include Allowance.Magic_allow_disallow (struct
type (_, _, 'd) sided = 'd t
let disallow_left ({ layout = _; _ } as t) = t
let disallow_right ({ layout = _; _ } as t) = t
let allow_left ({ layout = _; _ } as t) = t
let allow_right ({ layout = _; _ } as t) = t
end)
let max =
{ layout = Layout.Const.max;
modes_upper_bounds = Modes.max;
externality_upper_bound = Externality.max;
nullability_upper_bound = Nullability.max
}
let equal_after_all_inference_is_done t1 t2 =
Layout_and_axes.equal_after_all_inference_is_done Layout.Const.equal t1 t2
module Builtin = struct
type nonrec +'d t =
{ jkind : 'd t;
name : string
}
let mk_jkind ~mode_crossing ~nullability (layout : Layout.Const.t) =
let modes_upper_bounds, externality_upper_bound =
match mode_crossing with
| true -> Modes.min, Externality.min
| false -> Modes.max, Externality.max
in
{ layout;
modes_upper_bounds;
externality_upper_bound;
nullability_upper_bound = nullability
}
let any =
{ jkind = mk_jkind Any ~mode_crossing:false ~nullability:Maybe_null;
name = "any"
}
let any_non_null =
{ jkind = mk_jkind Any ~mode_crossing:false ~nullability:Non_null;
name = "any_non_null"
}
let value_or_null =
{ jkind =
mk_jkind (Base Value) ~mode_crossing:false ~nullability:Maybe_null;
name = "value_or_null"
}
let value =
{ jkind = mk_jkind (Base Value) ~mode_crossing:false ~nullability:Non_null;
name = "value"
}
let immutable_data =
{ jkind =
{ layout = Base Value;
modes_upper_bounds =
{ linearity = Linearity.Const.min;
contention = Contention.Const.min;
portability = Portability.Const.min;
uniqueness = Uniqueness.Const.max;
areality = Locality.Const.max
};
externality_upper_bound = Externality.max;
nullability_upper_bound = Nullability.Non_null
};
name = "immutable_data"
}
let mutable_data =
{ jkind =
{ layout = Base Value;
modes_upper_bounds =
{ linearity = Linearity.Const.min;
contention = Contention.Const.max;
portability = Portability.Const.min;
uniqueness = Uniqueness.Const.max;
areality = Locality.Const.max
};
externality_upper_bound = Externality.max;
nullability_upper_bound = Nullability.Non_null
};
name = "mutable_data"
}
(* CR layouts v3: change to [or_null] when separability is implemented. *)
let void =
{ jkind = mk_jkind (Base Void) ~mode_crossing:false ~nullability:Non_null;
name = "void"
}
let immediate =
{ jkind = mk_jkind (Base Value) ~mode_crossing:true ~nullability:Non_null;
name = "immediate"
}
(* [immediate64] describes types that are stored directly (no indirection)
on 64-bit platforms but indirectly on 32-bit platforms. The key question:
along which modes should a [immediate64] cross? As of today, all of them,
but the reasoning for each is independent and somewhat subtle:
* Locality: This is fine, because we do not have stack-allocation on
32-bit platforms. Thus mode-crossing is sound at any type on 32-bit,
including immediate64 types.
* Linearity: This is fine, because linearity matters only for function
types, and an immediate64 cannot be a function type and cannot store
one either.
* Uniqueness: This is fine, because uniqueness matters only for
in-place update, and no record supporting in-place update is an
immediate64. ([@@unboxed] records do not support in-place update.)
* Syncness: This is fine, because syncness matters only for function
types, and an immediate64 cannot be a function type and cannot store
one either.
* Contention: This is fine, because contention matters only for
types with mutable fields, and an immediate64 does not have immutable
fields.
In practice, the functor that creates immediate64s,
[Stdlib.Sys.Immediate64.Make], will require these conditions on its
argument. But the arguments that we expect here will have no trouble
meeting the conditions.
*)
let immediate64 =
{ jkind = { immediate.jkind with externality_upper_bound = External64 };
name = "immediate64"
}
(* CR layouts v3: change to [Maybe_null] when separability is implemented. *)
let float64 =
{ jkind =
mk_jkind (Base Float64) ~mode_crossing:true ~nullability:Non_null;
name = "float64"
}
(* CR layouts v3: change to [Maybe_null] when separability is implemented. *)
let float32 =
{ jkind =
mk_jkind (Base Float32) ~mode_crossing:true ~nullability:Non_null;
name = "float32"
}
(* CR layouts v3: change to [Maybe_null] when separability is implemented. *)
let word =
{ jkind = mk_jkind (Base Word) ~mode_crossing:true ~nullability:Non_null;
name = "word"
}
(* CR layouts v3: change to [Maybe_null] when separability is implemented. *)
let bits32 =
{ jkind = mk_jkind (Base Bits32) ~mode_crossing:true ~nullability:Non_null;
name = "bits32"
}
(* CR layouts v3: change to [Maybe_null] when separability is implemented. *)
let bits64 =
{ jkind = mk_jkind (Base Bits64) ~mode_crossing:true ~nullability:Non_null;
name = "bits64"
}
(* CR layouts v3: change to [Maybe_null] when separability is implemented. *)
let vec128 =
{ jkind = mk_jkind (Base Vec128) ~mode_crossing:true ~nullability:Non_null;
name = "vec128"
}
let all =
[ any;
any_non_null;
value_or_null;
value;
immutable_data;
mutable_data;
void;
immediate;
immediate64;
float64;
float32;
word;
bits32;
bits64;
vec128 ]
(* CR layouts v3.0: remove this hack once [or_null] is out of [Alpha]. *)
let all_non_null =
[ any;
{ any_non_null with name = "any" };
{ value_or_null with name = "value" };
value;
immutable_data;
mutable_data;
void;
immediate;
immediate64;
float64;
float32;
word;
bits32;
bits64;
vec128 ]
let of_attribute : Builtin_attributes.jkind_attribute -> _ t = function
| Immediate -> immediate
| Immediate64 -> immediate64
end
module To_out_jkind_const : sig
(** Convert a [t] into a [Outcometree.out_jkind_const].
The jkind is written in terms of the built-in jkind that requires the least amount
of modes after the mod. For example,
[value mod global many unique portable uncontended external_ non_null] could be
written in terms of [value] (as it appears above), or in terms of [immediate]
(which would just be [immediate]). Since the latter requires less modes to be
printed, it is chosen. *)
val convert : allow_null:bool -> 'd t -> Outcometree.out_jkind_const
end = struct
type printable_jkind =
{ base : string;
modal_bounds : string list
}
module Bounds = struct
type t =
{ alloc_bounds : Alloc.Const.t;
externality_bound : Externality.t;
nullability_bound : Nullability.t
}
let of_jkind jkind =
{ alloc_bounds = jkind.modes_upper_bounds;
externality_bound = jkind.externality_upper_bound;
nullability_bound = jkind.nullability_upper_bound
}
end
let get_modal_bound ~le ~print ~base actual =
match le actual base with
| true -> (
match le base actual with
| true -> `Valid None
| false -> `Valid (Some (Format.asprintf "%a" print actual)))
| false -> `Invalid
let get_modal_bounds ~(base : Bounds.t) (actual : Bounds.t) =
[ get_modal_bound ~le:Locality.Const.le ~print:Locality.Const.print
~base:base.alloc_bounds.areality actual.alloc_bounds.areality;
get_modal_bound ~le:Uniqueness.Const.le ~print:Uniqueness.Const.print
~base:base.alloc_bounds.uniqueness actual.alloc_bounds.uniqueness;
get_modal_bound ~le:Linearity.Const.le ~print:Linearity.Const.print
~base:base.alloc_bounds.linearity actual.alloc_bounds.linearity;
get_modal_bound ~le:Contention.Const.le ~print:Contention.Const.print
~base:base.alloc_bounds.contention actual.alloc_bounds.contention;
get_modal_bound ~le:Portability.Const.le ~print:Portability.Const.print
~base:base.alloc_bounds.portability actual.alloc_bounds.portability;
get_modal_bound ~le:Externality.le ~print:Externality.print
~base:base.externality_bound actual.externality_bound;
get_modal_bound ~le:Nullability.le ~print:Nullability.print
~base:base.nullability_bound actual.nullability_bound ]
|> List.rev
|> List.fold_left
(fun acc mode ->
match acc, mode with
| _, `Invalid | None, _ -> None
| acc, `Valid None -> acc
| Some acc, `Valid (Some mode) -> Some (mode :: acc))
(Some [])
(** Write [actual] in terms of [base] *)
let convert_with_base ~(base : _ Builtin.t) actual =
let matching_layouts =
Layout.Const.equal base.jkind.layout actual.layout
in
let modal_bounds =
get_modal_bounds
~base:(Bounds.of_jkind base.jkind)
(Bounds.of_jkind actual)
in
match matching_layouts, modal_bounds with
| true, Some modal_bounds -> Some { base = base.name; modal_bounds }
| false, _ | _, None -> None
(** Select the out_jkind_const with the least number of modal bounds to print *)
let rec select_simplest = function
| a :: b :: tl ->
let simpler =
if List.length a.modal_bounds < List.length b.modal_bounds
then a
else b
in
select_simplest (simpler :: tl)
| [out] -> Some out
| [] -> None
let convert ~allow_null jkind =
(* For each primitive jkind, we try to print the jkind in terms of it (this is
possible if the primitive is a subjkind of it). We then choose the "simplest". The
"simplest" is taken to mean the one with the least number of modes that need to
follow the [mod]. *)
let simplest =
(* CR layouts v3.0: remove this hack once [or_null] is out of [Alpha]. *)
(if allow_null then Builtin.all else Builtin.all_non_null)
|> List.filter_map (fun base -> convert_with_base ~base jkind)
|> select_simplest
in
let printable_jkind =
match simplest with
| Some simplest -> simplest
| None -> (
(* CR layouts v2.8: sometimes there is no valid way to build a jkind from a
built-in abbreviation. For now, we just pretend that the layout name is a valid
jkind abbreviation whose modal bounds are all max, even though this is a
lie. *)
let out_jkind_verbose =
convert_with_base
~base:
{ jkind =
{ layout = jkind.layout;
modes_upper_bounds = Modes.max;
externality_upper_bound = Externality.max;
nullability_upper_bound = Nullability.Non_null
};
name = Layout.Const.to_string jkind.layout
}
jkind
in
match out_jkind_verbose with
| Some out_jkind -> out_jkind
| None ->
(* If we fail, try again with nullable jkinds. *)
let out_jkind_verbose =
convert_with_base
~base:
{ jkind =
{ layout = jkind.layout;
modes_upper_bounds = Modes.max;
externality_upper_bound = Externality.max;
nullability_upper_bound = Nullability.max
};
name = Layout.Const.to_string jkind.layout
}
jkind
in
(* convert_with_base is guaranteed to succeed since the layout matches and the
modal bounds are all max *)
Option.get out_jkind_verbose)
in
match printable_jkind with
| { base; modal_bounds = _ :: _ as modal_bounds } ->
Outcometree.Ojkind_const_mod
(Ojkind_const_abbreviation base, modal_bounds)
| { base; modal_bounds = [] } ->
Outcometree.Ojkind_const_abbreviation base
end
let to_out_jkind_const jkind =
let allow_null = Language_extension.(is_at_least Layouts Alpha) in
To_out_jkind_const.convert ~allow_null jkind
let format ppf jkind = to_out_jkind_const jkind |> !Oprint.out_jkind_const ppf
let jkind_of_product_annotations jkinds =
let folder (layouts, mode_ub, ext_ub, null_ub)
{ layout;
modes_upper_bounds;
externality_upper_bound;
nullability_upper_bound
} =
( layout :: layouts,
Modes.join mode_ub modes_upper_bounds,
Externality.join ext_ub externality_upper_bound,
Nullability.join null_ub nullability_upper_bound )
in
let layouts, mode_ub, ext_ub, null_ub =
List.fold_left folder
([], Modes.min, Externality.min, Nullability.min)
jkinds
in
{ layout = Layout.Const.Product (List.rev layouts);
modes_upper_bounds = mode_ub;
externality_upper_bound = ext_ub;
nullability_upper_bound = null_ub
}
let rec of_user_written_annotation_unchecked_level :
type l r.
(l * r) History.annotation_context ->
Parsetree.jkind_annotation ->
(l * r) t =
fun context jkind ->
match jkind.pjkind_desc with
| Abbreviation name ->
(* CR layouts v2.8: move this to predef *)
(match name with
(* CR layouts v3.0: remove this hack once non-null jkinds are out of alpha.
It is confusing, but preserves backwards compatibility for arrays. *)
| "any" when Language_extension.(is_at_least Layouts Alpha) ->
Builtin.any.jkind
| "any" -> Builtin.any_non_null.jkind
| "any_non_null" -> Builtin.any_non_null.jkind
| "value_or_null" -> Builtin.value_or_null.jkind
| "value" -> Builtin.value.jkind
| "void" -> Builtin.void.jkind
| "immediate64" -> Builtin.immediate64.jkind
| "immediate" -> Builtin.immediate.jkind
| "float64" -> Builtin.float64.jkind
| "float32" -> Builtin.float32.jkind
| "word" -> Builtin.word.jkind
| "bits32" -> Builtin.bits32.jkind
| "bits64" -> Builtin.bits64.jkind
| "vec128" -> Builtin.vec128.jkind
| _ -> raise ~loc:jkind.pjkind_loc (Unknown_jkind jkind))
|> allow_left |> allow_right
| Mod (jkind, modifiers) ->
let base = of_user_written_annotation_unchecked_level context jkind in
(* for each mode, lower the corresponding modal bound to be that mode *)
let parsed_modifiers = Typemode.transl_modifier_annots modifiers in
let parsed_modes : Alloc.Const.Option.t =
{ areality = parsed_modifiers.locality;
linearity = parsed_modifiers.linearity;
uniqueness = parsed_modifiers.uniqueness;
portability = parsed_modifiers.portability;
contention = parsed_modifiers.contention
}
in
{ layout = base.layout;
modes_upper_bounds =
Alloc.Const.meet base.modes_upper_bounds
(Alloc.Const.Option.value ~default:Alloc.Const.max parsed_modes);
nullability_upper_bound =
Nullability.meet base.nullability_upper_bound
(Option.value ~default:Nullability.max parsed_modifiers.nullability);
externality_upper_bound =
Externality.meet base.externality_upper_bound
(Option.value ~default:Externality.max parsed_modifiers.externality)
}
| Product ts ->
let jkinds =
List.map (of_user_written_annotation_unchecked_level context) ts
in
jkind_of_product_annotations jkinds
| Default | With _ | Kind_of _ -> Misc.fatal_error "XXX unimplemented"
(* The [annotation_context] parameter can be used to allow annotations / kinds
in different contexts to be enabled with different extension settings.
At some points in time, we will not care about the context, and so this
parameter might effectively be unused.
*)
(* CR layouts: When everything is stable, remove this function. *)
let get_required_layouts_level (_context : 'd History.annotation_context)
(jkind : 'd t) =
let rec scan_layout (l : Layout.Const.t) : Language_extension.maturity =
match l, jkind.nullability_upper_bound with
| (Base (Float64 | Float32 | Word | Bits32 | Bits64 | Vec128) | Any), _
| Base Value, Non_null ->
Stable
| Product layouts, _ ->
List.fold_left
(fun m l -> Language_extension.Maturity.max m (scan_layout l))
Language_extension.Stable layouts
| Base Void, _ | Base Value, Maybe_null -> Alpha
in
scan_layout jkind.layout
let of_user_written_annotation ~context (annot : Parsetree.jkind_annotation) =
let const = of_user_written_annotation_unchecked_level context annot in
let required_layouts_level = get_required_layouts_level context const in
if not (Language_extension.is_at_least Layouts required_layouts_level)
then
raise ~loc:annot.pjkind_loc
(Insufficient_level { jkind = annot; required_layouts_level });
const
end
module Desc = struct
type 'd t = (Sort.Flat.t Layout.t, 'd) Layout_and_axes.t
let get_const t = Layout_and_axes.map_option Layout.get_flat_const t
(* CR layouts v2.8: This will probably need to be overhauled with
[with]-types. See also [Printtyp.out_jkind_of_desc], which uses the same
algorithm. *)
let format ppf t =
let open Format in
let rec format_desc ~nested ppf
(desc : (Sort.Flat.t Layout.t, _) Layout_and_axes.t) =
match desc.layout with
| Sort (Var n) -> fprintf ppf "'s%d" (Sort.Var.get_print_number n)
(* Analyze a product before calling [get_const]: the machinery in
[Const.format] works better for atomic layouts, not products. *)
| Product lays ->
let pp_sep ppf () = fprintf ppf "@ & " in
Misc.pp_nested_list ~nested ~pp_element:format_desc ~pp_sep ppf
(List.map (fun layout -> { desc with layout }) lays)
| _ -> (
match get_const desc with
| Some c -> Const.format ppf c
| None -> assert false (* handled above *))
in
format_desc ~nested:false ppf t
end
module Jkind_desc = struct
open Jkind_types.Layout_and_axes
let of_const t = Layout_and_axes.map Layout.of_const t
let add_nullability_crossing t =
{ t with nullability_upper_bound = Nullability.min }
let add_portability_and_contention_crossing ~from t =
let new_portability =
Portability.Const.meet t.modes_upper_bounds.portability
from.modes_upper_bounds.portability
in
let new_contention =
Contention.Const.meet t.modes_upper_bounds.contention
from.modes_upper_bounds.contention
in
let added_crossings =
(not
(Portability.Const.le t.modes_upper_bounds.portability new_portability))
|| not
(Contention.Const.le t.modes_upper_bounds.contention new_contention)
in
( { t with
modes_upper_bounds =
{ t.modes_upper_bounds with
portability = new_portability;
contention = new_contention
}
},
added_crossings )
let max = of_const Const.max
let equate_or_equal ~allow_mutation
{ layout = lay1;
modes_upper_bounds = modes1;
externality_upper_bound = ext1;
nullability_upper_bound = null1
}
{ layout = lay2;
modes_upper_bounds = modes2;
externality_upper_bound = ext2;
nullability_upper_bound = null2
} =
Layout.equate_or_equal ~allow_mutation lay1 lay2
&& Modes.equal modes1 modes2
&& Externality.equal ext1 ext2
&& Nullability.equal null1 null2
let sub t1 t2 = Layout_and_axes.sub Layout.sub t1 t2
let intersection
{ layout = lay1;
modes_upper_bounds = modes1;
externality_upper_bound = ext1;
nullability_upper_bound = null1
}
{ layout = lay2;
modes_upper_bounds = modes2;
externality_upper_bound = ext2;
nullability_upper_bound = null2
} =
Option.bind (Layout.intersection lay1 lay2) (fun layout ->
Some
{ layout;
modes_upper_bounds = Modes.meet modes1 modes2;
externality_upper_bound = Externality.meet ext1 ext2;
nullability_upper_bound = Nullability.meet null1 null2
})
let of_new_sort_var nullability_upper_bound =
let layout, sort = Layout.of_new_sort_var () in
( { layout;
modes_upper_bounds = Modes.max;
externality_upper_bound = Externality.max;
nullability_upper_bound
},
sort )
module Builtin = struct
let any = max
let value_or_null = of_const Const.Builtin.value_or_null.jkind
let value = of_const Const.Builtin.value.jkind
let void = of_const Const.Builtin.void.jkind
let immediate = of_const Const.Builtin.immediate.jkind
end
let product jkinds =
(* CR layouts v7.1: Here we throw away the history of the component
jkinds. This is not great. We should, as part of a broader pass on error
messages around product kinds, zip them up into some kind of product
history. *)
let folder (layouts, annotations, mode_ub, ext_ub, null_ub)
{ jkind =
{ layout;
modes_upper_bounds;
externality_upper_bound;
nullability_upper_bound
};
annotation;
history = _;
has_warned = _
} =
( layout :: layouts,
annotation :: annotations,
Modes.join mode_ub modes_upper_bounds,
Externality.join ext_ub externality_upper_bound,
Nullability.join null_ub nullability_upper_bound )
in
let layouts, annotations, mode_ub, ext_ub, null_ub =
List.fold_left folder
([], [], Modes.min, Externality.min, Nullability.min)
jkinds
in
let layouts = List.rev layouts in
let annotations = List.rev annotations in
let annotations = Misc.Stdlib.Monad.Option.all annotations in
let annotation =
Option.map
(fun annotations ->
Parsetree.
{ pjkind_loc = Location.none; pjkind_desc = Product annotations })
annotations
in
( { layout : _ Layout.t = Product layouts;
modes_upper_bounds = mode_ub;
externality_upper_bound = ext_ub;