Skip to content

Commit dea485e

Browse files
alanechangantalszgoldfirere
authored
flambda-backend: Unboxed ints (int32, int64, and nativeint) (#2113)
* Add frontend support for `nativeint#`, `int32#`, and `int64#` Bootstrap Update error messages in tests Update comments in `float_u.mli` Update `float_u` copyright headers Add missing unboxed int layout support Add unboxed integer layouts to the parser Add `Nativeint_u` module Minor documentation improvements to a `float#` test First passing unboxed nativeint test! Throw in the remaining `Nativeint_u` tests for the first test file Add tests that `nativeint#` is banned for `-layouts{_beta,}` Nativeint stdlib tests Layout `word` basics tests `nativeint#` parsing test Broken C tests for nativeint Failing `nativeint#` allocations tests Add `int64#` and `int32#` libraries and tests (broken!) Fix incorrect constant definition Update some test output Bootstrap `make alldepend` Apply `backend/` changes to `ocaml/asmcomp` Fix the stdlib compilation for unboxed integers Fix layout parsing Update some now-working tests Fix bits32 and bits64 tests so they're only as broken as word tests Update .c tests to use the right types Unboxed integer tests shouldn't return `double`s Use `succ` Unboxed integers don't allocate under flambda2 Add `[@inlined]` and `[@inline available]` to the `int64` modules Add `[@inlined]` and `[@inline available]` to the other int modules Add `[@inlined]` attrs to the calls to `unsigned_div` in int modules This is needed to prevent unboxed ints from allocating Update `StdlibModules` and run `make alldepend` Bootstrap Respond to review: Fix attribute names Respond to review: Remove "only some # types" check from the parser Update ocaml/stdlib/int32_u.mli Co-authored-by: Richard Eisenberg <rae@richarde.dev> Update ocaml/stdlib/int32_u.mli Co-authored-by: Richard Eisenberg <rae@richarde.dev> Proposed rewording of comments about "modulo" Update ocaml/stdlib/int64_u.mli Co-authored-by: Richard Eisenberg <rae@richarde.dev> * allow layout annot for new sorts * changes to unbox int tests * other test changes * add missing dune install * bootstrap * remove bad tests * remove incorrect comment * better comments and fix tests * rename param * revert backend change: no more typ_int32&typ_int64 * tests around join * remove XXX * Fix boxed/unboxed record error * move things to beta * add unboxed conversion functions * completely remove typ_int64 from cmm --------- Co-authored-by: Antal Spector-Zabusky <antal.b.sz@gmail.com> Co-authored-by: Richard Eisenberg <rae@richarde.dev>
1 parent c228d93 commit dea485e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+17188
-9534
lines changed

asmcomp/cmm_helpers.ml

-2
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,6 @@ module Extended_machtype = struct
910910

911911
let typ_any_int = [| Extended_machtype_component.Any_int |]
912912

913-
let typ_int64 = [| Extended_machtype_component.Any_int |]
914-
915913
let typ_float = [| Extended_machtype_component.Float |]
916914

917915
let typ_void = [||]

asmcomp/cmm_helpers.mli

-2
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,6 @@ module Extended_machtype : sig
311311

312312
val typ_any_int : t
313313

314-
val typ_int64 : t
315-
316314
val typ_float : t
317315

318316
val typ_void : t

asmcomp/cmmgen.ml

+4
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,16 @@ let exttype_of_sort (s : Jkind.Sort.const) =
355355
match s with
356356
| Value -> XInt
357357
| Float64 -> XFloat
358+
| Word -> XInt
359+
| Bits32 -> XInt32
360+
| Bits64 -> XInt64
358361
| Void -> Misc.fatal_error "Cmmgen.exttype_of_sort: void encountered"
359362

360363
let machtype_of_sort (s : Jkind.Sort.const) =
361364
match s with
362365
| Value -> typ_val
363366
| Float64 -> typ_float
367+
| Word | Bits32 | Bits64 -> typ_int
364368
| Void -> Misc.fatal_error "Cmmgen.machtype_of_sort: void encountered"
365369

366370
let is_unboxed_number_cmm ~strict cmm =

boot/menhir/parser.ml

+9,504-9,363
Large diffs are not rendered by default.

boot/ocamlc

10.9 KB
Binary file not shown.

boot/ocamllex

0 Bytes
Binary file not shown.

lambda/lambda.ml

+6
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@ let layout_module_field = Pvalue Pgenval
729729
let layout_functor = Pvalue Pgenval
730730
let layout_boxed_float = Pvalue Pfloatval
731731
let layout_unboxed_float = Punboxed_float
732+
let layout_unboxed_nativeint = Punboxed_int Pnativeint
733+
let layout_unboxed_int32 = Punboxed_int Pint32
734+
let layout_unboxed_int64 = Punboxed_int Pint64
732735
let layout_string = Pvalue Pgenval
733736
let layout_boxedint bi = Pvalue (Pboxedintval bi)
734737

@@ -1560,6 +1563,9 @@ let layout_of_native_repr : Primitive.native_repr -> _ = function
15601563
begin match s with
15611564
| Value -> layout_any_value
15621565
| Float64 -> layout_unboxed_float
1566+
| Word -> layout_unboxed_nativeint
1567+
| Bits32 -> layout_unboxed_int32
1568+
| Bits64 -> layout_unboxed_int64
15631569
| Void -> assert false
15641570
end
15651571

lambda/matching.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ let check_record_field_jkind lbl =
123123
| Float64, (Record_boxed _ | Record_inlined _
124124
| Record_unboxed | Record_float) ->
125125
raise (Error (lbl.lbl_loc, Illegal_record_field Float64))
126-
| (Any | Void) as c, _ -> raise (Error (lbl.lbl_loc, Illegal_record_field c))
126+
| (Any | Void | Word | Bits32 | Bits64) as c, _ ->
127+
(* CR layouts v2.1: support unboxed ints here *)
128+
raise (Error (lbl.lbl_loc, Illegal_record_field c))
127129

128130
(*
129131
Compatibility predicate that considers potential rebindings of constructors

lambda/translcore.ml

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ let check_record_field_sort loc sort repres =
7373
raise (Error (loc, Illegal_record_field Float64))
7474
| Void, _ ->
7575
raise (Error (loc, Illegal_record_field Void))
76+
| (Word | Bits32 | Bits64 as const), _ ->
77+
(* CR layouts v2.1: support unboxed ints here *)
78+
raise (Error (loc, Illegal_record_field const))
7679

7780
(* Forward declaration -- to be filled in by Translmod.transl_module *)
7881
let transl_module =

lambda/translprim.ml

+6
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,12 @@ let lookup_primitive loc poly pos p =
460460
| "%perform" -> Primitive (Pperform, 1)
461461
| "%resume" -> Primitive (Presume, 3)
462462
| "%dls_get" -> Primitive (Pdls_get, 1)
463+
| "%unbox_nativeint" -> Primitive(Punbox_int Pnativeint, 1)
464+
| "%box_nativeint" -> Primitive(Pbox_int (Pnativeint, mode), 1)
465+
| "%unbox_int32" -> Primitive(Punbox_int Pint32, 1)
466+
| "%box_int32" -> Primitive(Pbox_int (Pint32, mode), 1)
467+
| "%unbox_int64" -> Primitive(Punbox_int Pint64, 1)
468+
| "%box_int64" -> Primitive(Pbox_int (Pint64, mode), 1)
463469
| s when String.length s > 0 && s.[0] = '%' ->
464470
raise(Error(loc, Unknown_builtin_primitive s))
465471
| _ -> External p

parsing/parser.mly

+19-20
Original file line numberDiff line numberDiff line change
@@ -999,13 +999,15 @@ let unboxed_float sloc sign (f, m) =
999999

10001000
(* Unboxed float type *)
10011001

1002-
let assert_unboxed_float_type ~loc =
1002+
let assert_unboxed_type ~loc =
10031003
Language_extension.(
10041004
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Stable)
10051005

1006-
let unboxed_float_type sloc tys =
1007-
assert_unboxed_float_type ~loc:(make_loc sloc);
1008-
Ptyp_constr (mkloc (Lident "float#") (make_loc sloc), tys)
1006+
(* Invariant: [lident] must end with an [Lident] that ends with a ["#"]. *)
1007+
let unboxed_type sloc lident tys =
1008+
let loc = make_loc sloc in
1009+
assert_unboxed_type ~loc;
1010+
Ptyp_constr (mkloc lident loc, tys)
10091011
%}
10101012

10111013
/* Tokens */
@@ -4408,23 +4410,11 @@ atomic_type:
44084410
| UNDERSCORE
44094411
{ Ptyp_any }
44104412
| tys = actual_type_parameters
4411-
tid = mkrhs(type_longident)
4412-
HASH_SUFFIX
4413-
{ match tid.txt with
4414-
| Lident "float" ->
4415-
let ident_start = fst $loc(tid) in
4416-
let hash_end = snd $loc($3) in
4417-
unboxed_float_type (ident_start, hash_end) tys
4418-
| _ ->
4419-
(* CR layouts v2.1: We should avoid [not_expecting] in long-lived
4420-
code. When we support unboxed types other than float, we should
4421-
consider moving this check into the typechecker.
4422-
*)
4423-
not_expecting $sloc "Unboxed type other than float#"
4424-
}
4413+
tid = mkrhs(type_unboxed_longident)
4414+
{ unboxed_type $loc(tid) tid.txt tys }
44254415
| tys = actual_type_parameters
44264416
tid = mkrhs(type_longident)
4427-
{ Ptyp_constr(tid, tys) } %prec below_HASH
4417+
{ Ptyp_constr(tid, tys) }
44284418
| LESS meth_list GREATER
44294419
{ let (f, c) = $2 in Ptyp_object (f, c) }
44304420
| LESS GREATER
@@ -4676,8 +4666,17 @@ val_longident:
46764666
label_longident:
46774667
mk_longident(mod_longident, LIDENT) { $1 }
46784668
;
4669+
type_trailing_no_hash:
4670+
LIDENT { $1 } %prec below_HASH
4671+
;
4672+
type_trailing_hash:
4673+
LIDENT HASH_SUFFIX { $1 ^ "#" }
4674+
;
46794675
type_longident:
4680-
mk_longident(mod_ext_longident, LIDENT) { $1 }
4676+
mk_longident(mod_ext_longident, type_trailing_no_hash) { $1 }
4677+
;
4678+
type_unboxed_longident:
4679+
mk_longident(mod_ext_longident, type_trailing_hash) { $1 }
46814680
;
46824681
46834682
mod_longident:

stdlib/.depend

+30
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,16 @@ stdlib__Int32.cmx : int32.ml \
522522
stdlib__Int32.cmi
523523
stdlib__Int32.cmi : int32.mli \
524524
stdlib.cmi
525+
stdlib__Int32_u.cmo : int32_u.ml \
526+
stdlib.cmi \
527+
stdlib__Int32.cmi \
528+
stdlib__Int32_u.cmi
529+
stdlib__Int32_u.cmx : int32_u.ml \
530+
stdlib.cmx \
531+
stdlib__Int32.cmx \
532+
stdlib__Int32_u.cmi
533+
stdlib__Int32_u.cmi : int32_u.mli \
534+
stdlib.cmi
525535
stdlib__Int64.cmo : int64.ml \
526536
stdlib.cmi \
527537
stdlib__Int64.cmi
@@ -530,6 +540,16 @@ stdlib__Int64.cmx : int64.ml \
530540
stdlib__Int64.cmi
531541
stdlib__Int64.cmi : int64.mli \
532542
stdlib.cmi
543+
stdlib__Int64_u.cmo : int64_u.ml \
544+
stdlib.cmi \
545+
stdlib__Int64.cmi \
546+
stdlib__Int64_u.cmi
547+
stdlib__Int64_u.cmx : int64_u.ml \
548+
stdlib.cmx \
549+
stdlib__Int64.cmx \
550+
stdlib__Int64_u.cmi
551+
stdlib__Int64_u.cmi : int64_u.mli \
552+
stdlib.cmi
533553
stdlib__Lazy.cmo : lazy.ml \
534554
stdlib.cmi \
535555
stdlib__Obj.cmi \
@@ -645,6 +665,16 @@ stdlib__Nativeint.cmx : nativeint.ml \
645665
stdlib__Nativeint.cmi
646666
stdlib__Nativeint.cmi : nativeint.mli \
647667
stdlib.cmi
668+
stdlib__Nativeint_u.cmo : nativeint_u.ml \
669+
stdlib.cmi \
670+
stdlib__Nativeint.cmi \
671+
stdlib__Nativeint_u.cmi
672+
stdlib__Nativeint_u.cmx : nativeint_u.ml \
673+
stdlib.cmx \
674+
stdlib__Nativeint.cmx \
675+
stdlib__Nativeint_u.cmi
676+
stdlib__Nativeint_u.cmi : nativeint_u.mli \
677+
stdlib.cmi
648678
stdlib__Obj.cmo : obj.ml \
649679
stdlib__Sys.cmi \
650680
stdlib.cmi \

stdlib/Makefile

+7-7
Original file line numberDiff line numberDiff line change
@@ -208,27 +208,27 @@ stdlib.cmx: stdlib.ml
208208
-pp "$(AWK) -f ./expand_module_aliases.awk" -c $<
209209

210210

211-
# special cases to add the extension flag when compiling float_u
211+
# special cases to add the extension flag when compiling float_u etc.
212212
# CR layouts: eventually these can be just [-extension layouts]
213-
stdlib__Float_u.cmi:
213+
stdlib__%_u.cmi:
214214
$(CAMLC) $(COMPFLAGS) -extension layouts_alpha \
215215
-o $@ -c $(filter %.mli, $^)
216216

217-
stdlib__Float_u.cmo:
217+
stdlib__%_u.cmo:
218218
$(CAMLC) $(COMPFLAGS) -extension layouts_alpha \
219219
-o $@ -c $(filter %.ml, $^)
220220

221-
stdlib__Float_u.cmx:
221+
stdlib__%_u.cmx:
222222
$(CAMLOPT) $(COMPFLAGS) $(OPTCOMPFLAGS) -extension layouts_alpha \
223223
-o $@ -c $(filter %.ml, $^)
224224

225-
float_u.cmi: %.mli
225+
%_u.cmi: %_u.mli
226226
$(CAMLC) $(COMPFLAGS) -extension layouts_alpha -c $<
227227

228-
float_u.cmo: %.ml
228+
%_u.cmo: %_u.ml
229229
$(CAMLC) $(COMPFLAGS) -extension layouts_alpha -c $<
230230

231-
float_u.cmx: %.ml
231+
%_u.cmx: %_u.ml
232232
$(CAMLOPT) $(COMPFLAGS) $(OPTCOMPFLAGS) -extension layouts_alpha -c $<
233233

234234
%.cmi: %.mli

stdlib/StdlibModules

+3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ STDLIB_MODULE_BASENAMES = \
6161
float \
6262
float_u \
6363
int32 \
64+
int32_u \
6465
int64 \
66+
int64_u \
6567
nativeint \
68+
nativeint_u \
6669
lexing \
6770
parsing \
6871
set \

stdlib/dune

+18
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@
142142
int.mli
143143
int32.ml
144144
int32.mli
145+
int32_u.ml
146+
int32_u.mli
145147
int64.ml
146148
int64.mli
149+
int64_u.ml
150+
int64_u.mli
147151
lazy.ml
148152
lazy.mli
149153
lexing.ml
@@ -162,6 +166,8 @@
162166
mutex.mli
163167
nativeint.ml
164168
nativeint.mli
169+
nativeint_u.ml
170+
nativeint_u.mli
165171
obj.ml
166172
obj.mli
167173
oo.ml
@@ -301,9 +307,15 @@
301307
.stdlib.objs/byte/stdlib__Int32.cmi
302308
.stdlib.objs/byte/stdlib__Int32.cmt
303309
.stdlib.objs/byte/stdlib__Int32.cmti
310+
.stdlib.objs/byte/stdlib__Int32_u.cmi
311+
.stdlib.objs/byte/stdlib__Int32_u.cmt
312+
.stdlib.objs/byte/stdlib__Int32_u.cmti
304313
.stdlib.objs/byte/stdlib__Int64.cmi
305314
.stdlib.objs/byte/stdlib__Int64.cmt
306315
.stdlib.objs/byte/stdlib__Int64.cmti
316+
.stdlib.objs/byte/stdlib__Int64_u.cmi
317+
.stdlib.objs/byte/stdlib__Int64_u.cmt
318+
.stdlib.objs/byte/stdlib__Int64_u.cmti
307319
.stdlib.objs/byte/stdlib__Lazy.cmi
308320
.stdlib.objs/byte/stdlib__Lazy.cmt
309321
.stdlib.objs/byte/stdlib__Lazy.cmti
@@ -331,6 +343,9 @@
331343
.stdlib.objs/byte/stdlib__Nativeint.cmi
332344
.stdlib.objs/byte/stdlib__Nativeint.cmt
333345
.stdlib.objs/byte/stdlib__Nativeint.cmti
346+
.stdlib.objs/byte/stdlib__Nativeint_u.cmi
347+
.stdlib.objs/byte/stdlib__Nativeint_u.cmt
348+
.stdlib.objs/byte/stdlib__Nativeint_u.cmti
334349
.stdlib.objs/byte/stdlib__Obj.cmi
335350
.stdlib.objs/byte/stdlib__Obj.cmt
336351
.stdlib.objs/byte/stdlib__Obj.cmti
@@ -446,6 +461,7 @@
446461
.stdlib.objs/native/stdlib__Bigarray.cmx
447462
.stdlib.objs/native/stdlib__Array.cmx
448463
.stdlib.objs/native/stdlib__Int32.cmx
464+
.stdlib.objs/native/stdlib__Int32_u.cmx
449465
.stdlib.objs/native/stdlib__Lexing.cmx
450466
.stdlib.objs/native/stdlib__ArrayLabels.cmx
451467
.stdlib.objs/native/stdlib__Obj.cmx
@@ -466,6 +482,7 @@
466482
.stdlib.objs/native/stdlib__Ephemeron.cmx
467483
.stdlib.objs/native/stdlib__String.cmx
468484
.stdlib.objs/native/stdlib__Nativeint.cmx
485+
.stdlib.objs/native/stdlib__Nativeint_u.cmx
469486
.stdlib.objs/native/stdlib__Set.cmx
470487
.stdlib.objs/native/stdlib__ListLabels.cmx
471488
.stdlib.objs/native/stdlib__Bool.cmx
@@ -474,6 +491,7 @@
474491
.stdlib.objs/native/stdlib__Random.cmx
475492
.stdlib.objs/native/stdlib__Int.cmx
476493
.stdlib.objs/native/stdlib__Int64.cmx
494+
.stdlib.objs/native/stdlib__Int64_u.cmx
477495
.stdlib.objs/native/stdlib__Scanf.cmx
478496
.stdlib.objs/native/stdlib__Printf.cmx
479497
.stdlib.objs/native/stdlib__Complex.cmx

stdlib/float_u.ml

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
(* *)
44
(* OCaml *)
55
(* *)
6-
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
7-
(* Nicolas Ojeda Bar, LexiFi *)
6+
(* Chris Casinghino, Jane Street, New York *)
87
(* *)
9-
(* Copyright 2018 Institut National de Recherche en Informatique et *)
10-
(* en Automatique. *)
8+
(* Copyright 2023 Jane Street Group LLC *)
119
(* *)
1210
(* All rights reserved. This file is distributed under the terms of *)
1311
(* the GNU Lesser General Public License version 2.1, with the *)

0 commit comments

Comments
 (0)