Skip to content

Commit a88d308

Browse files
authored
Attribute [@@@zero_alloc check] to turn the check on (#1294)
1 parent 382b573 commit a88d308

16 files changed

+42
-12
lines changed

backend/checkmach.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ end
786786
module Spec_zero_alloc : Spec = struct
787787
let property = Cmm.Zero_alloc
788788

789-
let enabled () = !Flambda_backend_flags.zero_alloc_check
789+
let enabled () = !Clflags.zero_alloc_check
790790

791791
(* Compact the mapping from function name to Value.t to reduce size of Checks
792792
in cmx and memory consumption Compilenv. Different components have

backend/checks.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let reset t =
2424
t.enabled <- false
2525

2626
let merge src ~into:dst =
27-
if !Flambda_backend_flags.zero_alloc_check
27+
if !Clflags.zero_alloc_check
2828
then (
2929
let join _key b1 b2 = Some (b1 || b2) in
3030
dst.nor <- String.Map.union join dst.nor src.nor;

driver/flambda_backend_args.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,8 @@ module Flambda_backend_options_impl = struct
742742

743743
let heap_reduction_threshold x =
744744
Flambda_backend_flags.heap_reduction_threshold := x
745-
let zero_alloc_check = set' Flambda_backend_flags.zero_alloc_check
745+
746+
let zero_alloc_check = set' Clflags.zero_alloc_check
746747
let dcheckmach = set' Flambda_backend_flags.dump_checkmach
747748

748749
let disable_poll_insertion = set' Flambda_backend_flags.disable_poll_insertion
@@ -972,7 +973,7 @@ module Extra_params = struct
972973
set_int_option' Flambda_backend_flags.reorder_blocks_random
973974
| "basic-block-sections" -> set' Flambda_backend_flags.basic_block_sections
974975
| "heap-reduction-threshold" -> set_int' Flambda_backend_flags.heap_reduction_threshold
975-
| "zero-alloc-check" -> set' Flambda_backend_flags.zero_alloc_check
976+
| "zero-alloc-check" -> set' Clflags.zero_alloc_check
976977
| "dump-checkmach" -> set' Flambda_backend_flags.dump_checkmach
977978
| "poll-insertion" -> set' Flambda_backend_flags.disable_poll_insertion
978979
| "long-frames" -> set' Flambda_backend_flags.allow_long_frames

driver/flambda_backend_flags.ml

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ let dasm_comments = ref false (* -dasm-comments *)
2828

2929
let default_heap_reduction_threshold = 500_000_000 / (Sys.word_size / 8)
3030
let heap_reduction_threshold = ref default_heap_reduction_threshold (* -heap-reduction-threshold *)
31-
let zero_alloc_check = ref false (* -zero-alloc-check *)
3231
let dump_checkmach = ref false (* -dcheckmach *)
3332

3433
let disable_poll_insertion = ref (not Config.poll_insertion)

driver/flambda_backend_flags.mli

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ val dasm_comments : bool ref
2929

3030
val default_heap_reduction_threshold : int
3131
val heap_reduction_threshold : int ref
32-
val zero_alloc_check : bool ref
3332
val dump_checkmach : bool ref
3433

3534
val disable_poll_insertion : bool ref

ocaml/parsing/builtin_attributes.ml

+24-1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ let parse_int_payload attr =
435435
"A constant payload of type int was expected";
436436
None
437437

438+
let parse_ident_payload attr =
439+
match ident_of_payload attr.attr_payload with
440+
| Some i -> Some i
441+
| None ->
442+
warn_payload attr.attr_loc attr.attr_name.txt
443+
"A constant payload of type ident was expected";
444+
None
445+
438446
let clflags_attribute_without_payload attr ~name clflags_ref =
439447
when_attribute_is [name; "ocaml." ^ name] attr ~f:(fun () ->
440448
match parse_empty_payload attr with
@@ -490,6 +498,20 @@ let inline_attribute attr =
490498
Clflags.Float_arg_helper.parse s err_msg Clflags.inline_threshold
491499
| None -> warn_payload attr.attr_loc attr.attr_name.txt err_msg)
492500

501+
let parse_attribute_with_ident_payload attr ~name ~f =
502+
when_attribute_is [name; "ocaml." ^ name] attr ~f:(fun () ->
503+
match parse_ident_payload attr with
504+
| Some i -> f i
505+
| None -> ())
506+
507+
let zero_alloc_attribute (attr : Parsetree.attribute) =
508+
parse_attribute_with_ident_payload attr
509+
~name:"zero_alloc" ~f:(function
510+
| "check" -> Clflags.zero_alloc_check := true
511+
| _ ->
512+
warn_payload attr.attr_loc attr.attr_name.txt
513+
"Only 'check' is supported")
514+
493515
let afl_inst_ratio_attribute attr =
494516
clflags_attribute_with_int_payload attr
495517
~name:"afl_inst_ratio" Clflags.afl_inst_ratio
@@ -508,7 +530,8 @@ let parse_standard_implementation_attributes attr =
508530
inline_attribute attr;
509531
afl_inst_ratio_attribute attr;
510532
flambda_o3_attribute attr;
511-
flambda_oclassic_attribute attr
533+
flambda_oclassic_attribute attr;
534+
zero_alloc_attribute attr
512535

513536
let has_local_opt attrs =
514537
has_attribute ["ocaml.local_opt"; "local_opt"] attrs

ocaml/stdlib/camlinternalAtomic.ml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
(**************************************************************************)
1515

1616
[@@@ocaml.flambda_o3]
17+
[@@@ocaml.zero_alloc check]
1718

1819
(* CamlinternalAtomic is a dependency of Stdlib, so it is compiled with
1920
-nopervasives. *)

ocaml/stdlib/camlinternalFormatBasics.ml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
(* special exception on linking described in the file LICENSE. *)
1414
(* *)
1515
(**************************************************************************)
16+
[@@@ocaml.zero_alloc check]
1617

1718
(* Padding position. *)
1819
type padty =

ocaml/stdlib/std_exit.ml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
open! Stdlib
1818

1919
[@@@ocaml.flambda_o3]
20+
[@@@ocaml.zero_alloc check]
2021

2122
(* Ensure that [at_exit] functions are called at the end of every program *)
2223

ocaml/stdlib/stdlib.ml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
[@@@ocaml.warning "-49"]
1818
[@@@ocaml.flambda_o3]
19+
[@@@ocaml.zero_alloc check]
1920

2021
(* Exceptions *)
2122

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Fatal error: exception Stdlib.Exit
2-
Raised at Stdlib.open_in_gen in file "stdlib.ml", line 408, characters 28-54
2+
Raised at Stdlib.open_in_gen in file "stdlib.ml", line 409, characters 28-54
33
Called from Pr2195 in file "pr2195.ml", line 24, characters 6-19
44
Re-raised at Pr2195 in file "pr2195.ml", line 29, characters 4-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Fatal error: exception Stdlib.Exit
2-
Raised at Stdlib.open_in_gen in file "stdlib.ml", line 408, characters 28-54
3-
Called from Stdlib.open_in in file "stdlib.ml", line 413, characters 2-45
2+
Raised at Stdlib.open_in_gen in file "stdlib.ml", line 409, characters 28-54
3+
Called from Stdlib.open_in in file "stdlib.ml", line 414, characters 2-45
44
Called from Pr2195 in file "pr2195.ml", line 24, characters 6-19
55
Re-raised at Pr2195 in file "pr2195.ml", line 29, characters 4-41

ocaml/testsuite/tests/lib-dynlink-initializers/test10_main.byte.reference

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Error: Failure("Plugin error")
2-
Raised at Stdlib.failwith in file "stdlib.ml", line 32, characters 17-33
2+
Raised at Stdlib.failwith in file "stdlib.ml", line 33, characters 17-33
33
Called from Test10_plugin.g in file "test10_plugin.ml", line 3, characters 2-21
44
Called from Test10_plugin.f in file "test10_plugin.ml", line 6, characters 2-6
55
Called from Test10_plugin in file "test10_plugin.ml", line 10, characters 2-6

ocaml/testsuite/tests/lib-dynlink-initializers/test10_main.native.reference

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Error: Failure("Plugin error")
2-
Raised at Stdlib.failwith in file "stdlib.ml", line 32, characters 17-33
2+
Raised at Stdlib.failwith in file "stdlib.ml", line 33, characters 17-33
33
Called from Test10_plugin.g in file "test10_plugin.ml" (inlined), line 2, characters 15-38
44
Called from Test10_plugin.f in file "test10_plugin.ml", line 6, characters 2-6
55
Called from Test10_plugin in file "test10_plugin.ml", line 10, characters 2-6

ocaml/utils/clflags.ml

+2
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,5 @@ let create_usage_msg program =
632632

633633
let print_arguments program =
634634
Arg.usage !arg_spec (create_usage_msg program)
635+
636+
let zero_alloc_check = ref false (* -zero-alloc-check *)

ocaml/utils/clflags.mli

+2
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,5 @@ val print_arguments : string -> unit
283283

284284
(* [reset_arguments ()] clear all declared arguments *)
285285
val reset_arguments : unit -> unit
286+
287+
val zero_alloc_check : bool ref

0 commit comments

Comments
 (0)