Skip to content

Commit 5f6b7fe

Browse files
authored
flambda-backend: Move zero alloc annotations into a separate file (#2474)
* Move zero alloc annotations into a separate file * Add header to files
1 parent b22815e commit 5f6b7fe

File tree

12 files changed

+132
-64
lines changed

12 files changed

+132
-64
lines changed

.depend

+17-6
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@ utils/ccomp.cmx : \
2929
utils/ccomp.cmi
3030
utils/ccomp.cmi :
3131
utils/clflags.cmo : \
32+
utils/zero_alloc_annotations.cmi \
3233
utils/profile.cmi \
3334
utils/numbers.cmi \
3435
utils/misc.cmi \
3536
utils/config.cmi \
3637
utils/arg_helper.cmi \
3738
utils/clflags.cmi
3839
utils/clflags.cmx : \
40+
utils/zero_alloc_annotations.cmx \
3941
utils/profile.cmx \
4042
utils/numbers.cmx \
4143
utils/misc.cmx \
4244
utils/config.cmx \
4345
utils/arg_helper.cmx \
4446
utils/clflags.cmi
4547
utils/clflags.cmi : \
48+
utils/zero_alloc_annotations.cmi \
4649
utils/profile.cmi \
4750
utils/misc.cmi
4851
utils/compilation_unit.cmo : \
@@ -282,6 +285,11 @@ utils/warnings.cmx : \
282285
utils/misc.cmx \
283286
utils/warnings.cmi
284287
utils/warnings.cmi :
288+
utils/zero_alloc_annotations.cmo : \
289+
utils/zero_alloc_annotations.cmi
290+
utils/zero_alloc_annotations.cmx : \
291+
utils/zero_alloc_annotations.cmi
292+
utils/zero_alloc_annotations.cmi :
285293
utils/zero_alloc_utils.cmo : \
286294
utils/zero_alloc_utils.cmi
287295
utils/zero_alloc_utils.cmx : \
@@ -401,6 +409,7 @@ parsing/attr_helper.cmi : \
401409
parsing/asttypes.cmi
402410
parsing/builtin_attributes.cmo : \
403411
utils/zero_alloc_utils.cmi \
412+
utils/zero_alloc_annotations.cmi \
404413
utils/warnings.cmi \
405414
parsing/parsetree.cmi \
406415
utils/misc.cmi \
@@ -414,6 +423,7 @@ parsing/builtin_attributes.cmo : \
414423
parsing/builtin_attributes.cmi
415424
parsing/builtin_attributes.cmx : \
416425
utils/zero_alloc_utils.cmx \
426+
utils/zero_alloc_annotations.cmx \
417427
utils/warnings.cmx \
418428
parsing/parsetree.cmi \
419429
utils/misc.cmx \
@@ -690,20 +700,23 @@ typing/annot.cmi : \
690700
typing/btype.cmo : \
691701
typing/types.cmi \
692702
typing/path.cmi \
703+
parsing/parsetree.cmi \
693704
utils/local_store.cmi \
694705
typing/ident.cmi \
695706
parsing/asttypes.cmi \
696707
typing/btype.cmi
697708
typing/btype.cmx : \
698709
typing/types.cmx \
699710
typing/path.cmx \
711+
parsing/parsetree.cmi \
700712
utils/local_store.cmx \
701713
typing/ident.cmx \
702714
parsing/asttypes.cmi \
703715
typing/btype.cmi
704716
typing/btype.cmi : \
705717
typing/types.cmi \
706718
typing/path.cmi \
719+
parsing/parsetree.cmi \
707720
typing/jkind.cmi \
708721
parsing/asttypes.cmi
709722
typing/cmt2annot.cmo : \
@@ -1449,8 +1462,7 @@ typing/printtyp.cmi : \
14491462
parsing/location.cmi \
14501463
typing/ident.cmi \
14511464
typing/errortrace.cmi \
1452-
typing/env.cmi \
1453-
parsing/asttypes.cmi
1465+
typing/env.cmi
14541466
typing/printtyped.cmo : \
14551467
utils/zero_alloc_utils.cmi \
14561468
typing/types.cmi \
@@ -1932,6 +1944,7 @@ typing/typedecl.cmi : \
19321944
parsing/parsetree.cmi \
19331945
parsing/longident.cmi \
19341946
parsing/location.cmi \
1947+
utils/language_extension.cmi \
19351948
typing/jkind.cmi \
19361949
typing/includecore.cmi \
19371950
typing/ident.cmi \
@@ -7055,6 +7068,7 @@ toplevel/genprintval.cmo : \
70557068
typing/env.cmi \
70567069
typing/datarepr.cmi \
70577070
typing/ctype.cmi \
7071+
utils/clflags.cmi \
70587072
typing/btype.cmi \
70597073
parsing/asttypes.cmi \
70607074
toplevel/genprintval.cmi
@@ -7076,6 +7090,7 @@ toplevel/genprintval.cmx : \
70767090
typing/env.cmx \
70777091
typing/datarepr.cmx \
70787092
typing/ctype.cmx \
7093+
utils/clflags.cmx \
70797094
typing/btype.cmx \
70807095
parsing/asttypes.cmi \
70817096
toplevel/genprintval.cmi
@@ -7264,7 +7279,6 @@ toplevel/topprinters.cmo : \
72647279
typing/mode.cmi \
72657280
typing/ident.cmi \
72667281
typing/ctype.cmi \
7267-
parsing/asttypes.cmi \
72687282
toplevel/topprinters.cmi
72697283
toplevel/topprinters.cmx : \
72707284
typing/types.cmx \
@@ -7273,7 +7287,6 @@ toplevel/topprinters.cmx : \
72737287
typing/mode.cmx \
72747288
typing/ident.cmx \
72757289
typing/ctype.cmx \
7276-
parsing/asttypes.cmi \
72777290
toplevel/topprinters.cmi
72787291
toplevel/topprinters.cmi : \
72797292
typing/types.cmi
@@ -7412,7 +7425,6 @@ toplevel/byte/trace.cmo : \
74127425
bytecomp/meta.cmi \
74137426
parsing/longident.cmi \
74147427
typing/ctype.cmi \
7415-
parsing/asttypes.cmi \
74167428
toplevel/byte/trace.cmi
74177429
toplevel/byte/trace.cmx : \
74187430
typing/types.cmx \
@@ -7425,7 +7437,6 @@ toplevel/byte/trace.cmx : \
74257437
bytecomp/meta.cmx \
74267438
parsing/longident.cmx \
74277439
typing/ctype.cmx \
7428-
parsing/asttypes.cmi \
74297440
toplevel/byte/trace.cmi
74307441
toplevel/byte/trace.cmi : \
74317442
typing/types.cmi \

Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -1466,14 +1466,14 @@ tools/ocamldep$(EXE): OC_BYTECODE_LINKFLAGS += -compat-32
14661466
ocamlprof_LIBRARIES =
14671467
ocamlprof_MODULES = \
14681468
config build_path_prefix_map misc identifiable numbers arg_helper \
1469-
local_store load_path clflags terminfo warnings location longident \
1470-
docstrings syntaxerr ast_helper camlinternalMenhirLib parser pprintast \
1471-
lexer parse ocamlprof
1469+
local_store load_path zero_alloc_annotations clflags terminfo warnings \
1470+
location longident docstrings syntaxerr ast_helper camlinternalMenhirLib \
1471+
parser pprintast lexer parse ocamlprof
14721472

14731473
ocamlcp_ocamloptp_MODULES = \
14741474
config build_path_prefix_map misc profile warnings identifiable numbers \
1475-
arg_helper local_store load_path clflags terminfo location ccomp compenv \
1476-
main_args ocamlcp_common
1475+
arg_helper local_store load_path zero_alloc_annotations clflags terminfo \
1476+
location ccomp compenv main_args ocamlcp_common zero_alloc_annotations
14771477

14781478
ocamlcp_LIBRARIES =
14791479
ocamlcp_MODULES = $(ocamlcp_ocamloptp_MODULES) ocamlcp
@@ -1490,7 +1490,7 @@ ocamlmklib_MODULES = config build_path_prefix_map misc ocamlmklib
14901490
ocamlmktop_LIBRARIES =
14911491
ocamlmktop_MODULES = \
14921492
config build_path_prefix_map misc identifiable numbers arg_helper \
1493-
local_store load_path clflags profile ccomp ocamlmktop
1493+
local_store load_path zero_alloc_annotations clflags profile ccomp ocamlmktop
14941494

14951495
# Reading cmt files
14961496

compilerlibs/Makefile.compilerlibs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ UTILS = \
3434
utils/arg_helper.cmo \
3535
utils/local_store.cmo \
3636
utils/load_path.cmo \
37+
utils/zero_alloc_annotations.cmo \
3738
utils/clflags.cmo \
3839
utils/debug.cmo \
3940
utils/language_extension_kernel.cmo \

dune

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
local_store target_system compilation_unit import_info linkage_name symbol
7878
lazy_backtrack diffing diffing_with_keys
7979
language_extension_kernel language_extension
80-
zero_alloc_utils
80+
zero_alloc_utils zero_alloc_annotations
8181

8282
;; PARSING
8383
location longident docstrings printast syntaxerr ast_helper
@@ -248,6 +248,7 @@
248248
(identifiable.mli as compiler-libs/identifiable.mli)
249249
(numbers.mli as compiler-libs/numbers.mli)
250250
(arg_helper.mli as compiler-libs/arg_helper.mli)
251+
(zero_alloc_annotations.mli as compiler-libs/zero_alloc_annotations.mli)
251252
(clflags.mli as compiler-libs/clflags.mli)
252253
(language_extension.mli as compiler-libs/language_extension.mli)
253254
(profile.mli as compiler-libs/profile.mli)

otherlibs/dynlink/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ COMPILERLIBS_SOURCES=\
8484
utils/arg_helper.ml \
8585
utils/local_store.ml \
8686
utils/load_path.ml \
87+
utils/zero_alloc_annotations.ml \
8788
utils/clflags.ml \
8889
utils/debug.ml \
8990
utils/language_extension_kernel.ml \

otherlibs/dynlink/dune

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
identifiable
5555
numbers
5656
arg_helper
57+
zero_alloc_annotations
5758
clflags
5859
debug
5960
language_extension_kernel
@@ -148,6 +149,7 @@
148149
(copy_files ../../utils/identifiable.ml)
149150
(copy_files ../../utils/numbers.ml)
150151
(copy_files ../../utils/arg_helper.ml)
152+
(copy_files ../../utils/zero_alloc_annotations.ml)
151153
(copy_files ../../utils/clflags.ml)
152154
(copy_files ../../utils/debug.ml)
153155
(copy_files ../../utils/language_extension_kernel.ml)
@@ -214,6 +216,7 @@
214216
(copy_files ../../utils/identifiable.mli)
215217
(copy_files ../../utils/numbers.mli)
216218
(copy_files ../../utils/arg_helper.mli)
219+
(copy_files ../../utils/zero_alloc_annotations.mli)
217220
(copy_files ../../utils/clflags.mli)
218221
(copy_files ../../utils/debug.mli)
219222
(copy_files ../../utils/language_extension_kernel.mli)

parsing/builtin_attributes.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,10 @@ let parse_attribute_with_ident_payload attr ~name ~f =
603603
let zero_alloc_attribute (attr : Parsetree.attribute) =
604604
parse_attribute_with_ident_payload attr
605605
~name:"zero_alloc" ~f:(function
606-
| "check" -> Clflags.zero_alloc_check := Clflags.Annotations.Check_default
607-
| "check_opt" -> Clflags.zero_alloc_check := Clflags.Annotations.Check_opt_only
608-
| "check_all" -> Clflags.zero_alloc_check := Clflags.Annotations.Check_all
609-
| "check_none" -> Clflags.zero_alloc_check := Clflags.Annotations.No_check
606+
| "check" -> Clflags.zero_alloc_check := Zero_alloc_annotations.Check_default
607+
| "check_opt" -> Clflags.zero_alloc_check := Zero_alloc_annotations.Check_opt_only
608+
| "check_all" -> Clflags.zero_alloc_check := Zero_alloc_annotations.Check_all
609+
| "check_none" -> Clflags.zero_alloc_check := Zero_alloc_annotations.No_check
610610
| "all" ->
611611
Clflags.zero_alloc_check_assert_all := true
612612
| _ ->

tools/Makefile

+6-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ ocamldep.opt$(EXE): $(call byte2native, $(OCAMLDEP))
8383
# The profiler
8484

8585
OCAMLPROF=config.cmo build_path_prefix_map.cmo misc.cmo identifiable.cmo \
86-
numbers.cmo arg_helper.cmo clflags.cmo debug.cmo terminfo.cmo \
86+
numbers.cmo arg_helper.cmo zero_alloc_annotations.cmo clflags.cmo \
87+
debug.cmo terminfo.cmo \
8788
warnings.cmo location.cmo longident.cmo docstrings.cmo \
8889
syntaxerr.cmo ast_helper.cmo \
8990
language_extension_kernel.cmo language_extension.cmo \
@@ -101,7 +102,7 @@ opt.opt: profiling.cmx
101102
OCAMLCP = config.cmo build_path_prefix_map.cmo misc.cmo profile.cmo \
102103
warnings.cmo identifiable.cmo numbers.cmo arg_helper.cmo \
103104
language_extension_kernel.cmo language_extension.cmo \
104-
clflags.cmo local_store.cmo \
105+
zero_alloc_annotations.cmo clflags.cmo local_store.cmo \
105106
terminfo.cmo location.cmo load_path.cmo ccomp.cmo compenv.cmo \
106107
main_args.cmo
107108

@@ -136,8 +137,9 @@ ocamlmklib.opt$(EXE): $(call byte2native, $(OCAMLMKLIB))
136137
# To make custom toplevels
137138

138139
OCAMLMKTOP=config.cmo build_path_prefix_map.cmo misc.cmo \
139-
identifiable.cmo numbers.cmo arg_helper.cmo clflags.cmo \
140-
local_store.cmo load_path.cmo profile.cmo ccomp.cmo ocamlmktop.cmo
140+
identifiable.cmo numbers.cmo arg_helper.cmo zero_alloc_annotations.cmo \
141+
clflags.cmo local_store.cmo load_path.cmo profile.cmo ccomp.cmo \
142+
ocamlmktop.cmo
141143

142144
ocamlmktop$(EXE): $(OCAMLMKTOP)
143145
ocamlmktop.opt$(EXE): $(call byte2native, $(OCAMLMKTOP))

utils/clflags.ml

+1-33
Original file line numberDiff line numberDiff line change
@@ -648,39 +648,7 @@ let create_usage_msg program =
648648
let print_arguments program =
649649
Arg.usage !arg_spec (create_usage_msg program)
650650

651-
module Annotations = struct
652-
type t = Check_default | Check_all | Check_opt_only | No_check
653-
654-
let all = [ Check_default; Check_all; Check_opt_only; No_check ]
655-
656-
let to_string = function
657-
| Check_default -> "default"
658-
| Check_all -> "all"
659-
| Check_opt_only -> "opt"
660-
| No_check -> "none"
661-
662-
let equal t1 t2 =
663-
match t1, t2 with
664-
| Check_default, Check_default -> true
665-
| Check_all, Check_all -> true
666-
| No_check, No_check -> true
667-
| Check_opt_only, Check_opt_only -> true
668-
| (Check_default | Check_all | Check_opt_only | No_check), _ -> false
669-
670-
let of_string v =
671-
let f t =
672-
if String.equal (to_string t) v then Some t else None
673-
in
674-
List.find_map f all
675-
676-
let doc =
677-
"\n\ The argument specifies which annotations to check: \n\
678-
\ \"opt\" means attributes with \"opt\" payload and is intended for debugging;\n\
679-
\ \"default\" means attributes without \"opt\" payload; \n\
680-
\ \"all\" covers both \"opt\" and \"default\" and is intended for optimized builds."
681-
end
682-
683-
let zero_alloc_check = ref Annotations.Check_default (* -zero-alloc-check *)
651+
let zero_alloc_check = ref Zero_alloc_annotations.Check_default (* -zero-alloc-check *)
684652
let zero_alloc_check_assert_all = ref false (* -zero-alloc-check-assert-all *)
685653

686654
let no_auto_include_otherlibs = ref false (* -no-auto-include-otherlibs *)

utils/clflags.mli

+2-10
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,8 @@ val print_arguments : string -> unit
293293
(* [reset_arguments ()] clear all declared arguments *)
294294
val reset_arguments : unit -> unit
295295

296-
(* [Annotations] specifies which zero_alloc attributes to check. *)
297-
module Annotations : sig
298-
type t = Check_default | Check_all | Check_opt_only | No_check
299-
val all : t list
300-
val to_string : t -> string
301-
val of_string : string -> t option
302-
val equal : t -> t -> bool
303-
val doc : string
304-
end
305-
val zero_alloc_check : Annotations.t ref
296+
(* [zero_alloc_check] specifies which zero_alloc attributes to check. *)
297+
val zero_alloc_check : Zero_alloc_annotations.t ref
306298
val zero_alloc_check_assert_all : bool ref
307299

308300
val no_auto_include_otherlibs : bool ref

utils/zero_alloc_annotations.ml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
(******************************************************************************
2+
* flambda-backend *
3+
* Greta Yorsh, Jane Street *
4+
* -------------------------------------------------------------------------- *
5+
* MIT License *
6+
* *
7+
* Copyright (c) 2024 Jane Street Group LLC *
8+
* opensource-contacts@janestreet.com *
9+
* *
10+
* Permission is hereby granted, free of charge, to any person obtaining a *
11+
* copy of this software and associated documentation files (the "Software"), *
12+
* to deal in the Software without restriction, including without limitation *
13+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
14+
* and/or sell copies of the Software, and to permit persons to whom the *
15+
* Software is furnished to do so, subject to the following conditions: *
16+
* *
17+
* The above copyright notice and this permission notice shall be included *
18+
* in all copies or substantial portions of the Software. *
19+
* *
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
23+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
25+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
26+
* DEALINGS IN THE SOFTWARE. *
27+
******************************************************************************)
28+
type t = Check_default | Check_all | Check_opt_only | No_check
29+
30+
let all = [ Check_default; Check_all; Check_opt_only; No_check ]
31+
32+
let to_string = function
33+
| Check_default -> "default"
34+
| Check_all -> "all"
35+
| Check_opt_only -> "opt"
36+
| No_check -> "none"
37+
38+
let equal t1 t2 =
39+
match t1, t2 with
40+
| Check_default, Check_default -> true
41+
| Check_all, Check_all -> true
42+
| No_check, No_check -> true
43+
| Check_opt_only, Check_opt_only -> true
44+
| (Check_default | Check_all | Check_opt_only | No_check), _ -> false
45+
46+
let of_string v =
47+
let f t =
48+
if String.equal (to_string t) v then Some t else None
49+
in
50+
List.find_map f all
51+
52+
let doc =
53+
"\n\ The argument specifies which annotations to check: \n\
54+
\ \"opt\" means attributes with \"opt\" payload and is intended for debugging;\n\
55+
\ \"default\" means attributes without \"opt\" payload; \n\
56+
\ \"all\" covers both \"opt\" and \"default\" and is intended for optimized builds."

0 commit comments

Comments
 (0)