Skip to content

Commit 17374b4

Browse files
gretay-jspoechsel
authored andcommitted
flambda-backend: Add [@@Builtin] attribute to Primitives (#11 part 1)
1 parent 85127ad commit 17374b4

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

asmcomp/cmm_helpers.ml

+1
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ let int64_native_prim name arity ~alloc =
13581358
let rec make_args = function 0 -> [] | n -> u64 :: make_args (n - 1) in
13591359
Primitive.make ~name ~native_name:(name ^ "_native")
13601360
~alloc
1361+
~c_builtin:false
13611362
~native_repr_args:(make_args arity)
13621363
~native_repr_res:u64
13631364

typing/primitive.ml

+12-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type description =
3030
{ prim_name: string; (* Name of primitive or C function *)
3131
prim_arity: int; (* Number of arguments *)
3232
prim_alloc: bool; (* Does it allocates or raise? *)
33+
prim_c_builtin: bool; (* Is the compiler allowed to replace it? *)
3334
prim_native_name: string; (* Name of C function for the nat. code gen. *)
3435
prim_native_repr_args: native_repr list;
3536
prim_native_repr_res: native_repr }
@@ -69,14 +70,17 @@ let simple ~name ~arity ~alloc =
6970
{prim_name = name;
7071
prim_arity = arity;
7172
prim_alloc = alloc;
73+
prim_c_builtin = false;
7274
prim_native_name = "";
7375
prim_native_repr_args = make_native_repr_args arity Same_as_ocaml_repr;
7476
prim_native_repr_res = Same_as_ocaml_repr}
7577

76-
let make ~name ~alloc ~native_name ~native_repr_args ~native_repr_res =
78+
let make ~name ~alloc ~c_builtin
79+
~native_name ~native_repr_args ~native_repr_res =
7780
{prim_name = name;
7881
prim_arity = List.length native_repr_args;
7982
prim_alloc = alloc;
83+
prim_c_builtin = c_builtin;
8084
prim_native_name = native_name;
8185
prim_native_repr_args = native_repr_args;
8286
prim_native_repr_res = native_repr_res}
@@ -98,6 +102,10 @@ let parse_declaration valdecl ~native_repr_args ~native_repr_res =
98102
Attr_helper.has_no_payload_attribute ["noalloc"; "ocaml.noalloc"]
99103
valdecl.pval_attributes
100104
in
105+
let builtin_attribute =
106+
Attr_helper.has_no_payload_attribute ["builtin"; "ocaml.builtin"]
107+
valdecl.pval_attributes
108+
in
101109
if old_style_float &&
102110
not (List.for_all is_ocaml_repr native_repr_args &&
103111
is_ocaml_repr native_repr_res) then
@@ -131,6 +139,7 @@ let parse_declaration valdecl ~native_repr_args ~native_repr_res =
131139
{prim_name = name;
132140
prim_arity = arity;
133141
prim_alloc = not noalloc;
142+
prim_c_builtin = builtin_attribute;
134143
prim_native_name = native_name;
135144
prim_native_repr_args = native_repr_args;
136145
prim_native_repr_res = native_repr_res}
@@ -155,6 +164,7 @@ let rec add_native_repr_attributes ty attrs =
155164
let oattr_unboxed = { oattr_name = "unboxed" }
156165
let oattr_untagged = { oattr_name = "untagged" }
157166
let oattr_noalloc = { oattr_name = "noalloc" }
167+
let oattr_builtin = { oattr_name = "builtin" }
158168

159169
let print p osig_val_decl =
160170
let prims =
@@ -169,6 +179,7 @@ let print p osig_val_decl =
169179
let all_unboxed = for_all is_unboxed in
170180
let all_untagged = for_all is_untagged in
171181
let attrs = if p.prim_alloc then [] else [oattr_noalloc] in
182+
let attrs = if p.prim_c_builtin then oattr_builtin::attrs else attrs in
172183
let attrs =
173184
if all_unboxed then
174185
oattr_unboxed :: attrs

typing/primitive.mli

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ type description = private
2929
{ prim_name: string; (* Name of primitive or C function *)
3030
prim_arity: int; (* Number of arguments *)
3131
prim_alloc: bool; (* Does it allocates or raise? *)
32+
prim_c_builtin: bool;
33+
(* When [prim_c_builtin] is true, the native compiler is allowed to rewrite
34+
calls to the external C function that implements this primitive,
35+
based on its name [prim_name], into a predetermined instruction sequence.
36+
[prim_c_builtin] is ignored on compiler primitives
37+
whose name [prim_name] starts with %. *)
3238
prim_native_name: string; (* Name of C function for the nat. code gen. *)
3339
prim_native_repr_args: native_repr list;
3440
prim_native_repr_res: native_repr }
@@ -44,6 +50,7 @@ val simple
4450
val make
4551
: name:string
4652
-> alloc:bool
53+
-> c_builtin:bool
4754
-> native_name:string
4855
-> native_repr_args: native_repr list
4956
-> native_repr_res: native_repr

0 commit comments

Comments
 (0)