Skip to content

Commit 9c60ae1

Browse files
committed
Add the opaque primitive
1 parent 109cafb commit 9c60ae1

File tree

11 files changed

+35
-2
lines changed

11 files changed

+35
-2
lines changed

asmcomp/cmmgen.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ and transl_ccall env prim args dbg =
16981698
and transl_prim_1 env p arg dbg =
16991699
match p with
17001700
(* Generic operations *)
1701-
Pidentity ->
1701+
Pidentity | Popaque ->
17021702
transl env arg
17031703
| Pignore ->
17041704
return_unit(remove_unit (transl env arg))

bytecomp/bytegen.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ let rec comp_expr env exp sz cont =
571571
in
572572
comp_init env sz decl_size
573573
end
574-
| Lprim(Pidentity, [arg]) ->
574+
| Lprim((Pidentity | Popaque), [arg]) ->
575575
comp_expr env arg sz cont
576576
| Lprim(Pignore, [arg]) ->
577577
comp_expr env arg sz (add_const_unit cont)

bytecomp/lambda.ml

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ type primitive =
133133
| Pbbswap of boxed_integer
134134
(* Integer to external pointer *)
135135
| Pint_as_pointer
136+
(* Inhibition of optimisation *)
137+
| Popaque
136138

137139
and comparison =
138140
Ceq | Cneq | Clt | Cgt | Cle | Cge

bytecomp/lambda.mli

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ type primitive =
136136
| Pbbswap of boxed_integer
137137
(* Integer to external pointer *)
138138
| Pint_as_pointer
139+
(* Inhibition of optimisation *)
140+
| Popaque
139141

140142
and comparison =
141143
Ceq | Cneq | Clt | Cgt | Cle | Cge

bytecomp/printlambda.ml

+2
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ let primitive ppf = function
260260
| Pbswap16 -> fprintf ppf "bswap16"
261261
| Pbbswap(bi) -> print_boxed_integer "bswap" ppf bi
262262
| Pint_as_pointer -> fprintf ppf "int_as_pointer"
263+
| Popaque -> fprintf ppf "opaque"
263264

264265
let name_of_primitive = function
265266
| Pidentity -> "Pidentity"
@@ -354,6 +355,7 @@ let name_of_primitive = function
354355
| Pbswap16 -> "Pbswap16"
355356
| Pbbswap _ -> "Pbbswap"
356357
| Pint_as_pointer -> "Pint_as_pointer"
358+
| Popaque -> "Popaque"
357359

358360
let function_attribute ppf { inline; is_a_functor } =
359361
if is_a_functor then

bytecomp/translcore.ml

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ let primitives_table = create_hashtable 57 [
297297
"%bswap_int64", Pbbswap(Pint64);
298298
"%bswap_native", Pbbswap(Pnativeint);
299299
"%int_as_pointer", Pint_as_pointer;
300+
"%opaque", Popaque;
300301
]
301302

302303
let prim_obj_dup =

otherlibs/threads/pervasives.ml

+4
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,10 @@ let (^^) (Format (fmt1, str1)) (Format (fmt2, str2)) =
597597
Format (CamlinternalFormatBasics.concat_fmt fmt1 fmt2,
598598
str1 ^ "%," ^ str2)
599599

600+
(* Optimisation *)
601+
602+
external opaque : 'a -> 'a = "%opaque"
603+
600604
(* Miscellaneous *)
601605

602606
external sys_exit : int -> 'a = "caml_sys_exit"

stdlib/pervasives.ml

+4
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ let ( ^^ ) (Format (fmt1, str1)) (Format (fmt2, str2)) =
502502
Format (CamlinternalFormatBasics.concat_fmt fmt1 fmt2,
503503
str1 ^ "%," ^ str2)
504504

505+
(* Optimisation *)
506+
507+
external opaque : 'a -> 'a = "%opaque"
508+
505509
(* Miscellaneous *)
506510

507511
external sys_exit : int -> 'a = "caml_sys_exit"

stdlib/pervasives.mli

+8
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,14 @@ val ( ^^ ) :
10981098
[f1], then results from [f2].
10991099
*)
11001100

1101+
(** {6 Optimisation} *)
1102+
1103+
external opaque : 'a -> 'a = "%opaque"
1104+
(** Semantically, [opaque v] behaves as the identity function; but when
1105+
used with Flambda mode, for the purposes of optimisation passes such
1106+
expression is side-effecting and yields an unknown result. This may
1107+
be used to prevent necessary code in applications such as benchmarks from
1108+
being optimised away. *)
11011109

11021110
(** {6 Program termination} *)
11031111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
let f x = opaque x
3+
4+
let () =
5+
assert(f f == f);
6+
assert(opaque 1 = 1);
7+
assert(opaque 1. = 1.)
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
All tests succeeded.

0 commit comments

Comments
 (0)