Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make patricia trees big-endian #3438

Merged
merged 7 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/arm64/emit.mlp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ let name_for_int_operation = function
| Ilsl -> "lsl"
| Ilsr -> "lsr"
| Iasr -> "asr"
| Iclz { arg_is_non_zero = _ } -> "clz"
| _ -> assert false

(* Decompose an integer constant into four 16-bit shifted fragments.
Expand Down Expand Up @@ -1000,6 +1001,9 @@ let emit_instr i =
` smulh {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`
| Lop(Intop (Imulh { signed = false })) ->
` umulh {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`
| Lop(Intop (Iclz _ as op)) ->
let instr = name_for_int_operation op in
` {emit_string instr} {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}\n`
| Lop(Intop op) ->
let instr = name_for_int_operation op in
` {emit_string instr} {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`
Expand Down
4 changes: 2 additions & 2 deletions backend/arm64/proc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ let assemble_file infile outfile =
let init () = ()

let operation_supported = function
| Cclz _ | Cctz _ | Cpopcnt
| Cctz _ | Cpopcnt
| Cprefetch _ | Catomic _
(* CR mslater: (float32) arm64 *)
| Cnegf Float32 | Cabsf Float32 | Caddf Float32
Expand All @@ -497,7 +497,7 @@ let operation_supported = function
Int_of_float Float32 | Float_of_int Float32 |
V128_of_scalar _ | Scalar_of_v128 _)
-> false (* Not implemented *)
| Cbswap _
| Cclz _ | Cbswap _
| Capply _ | Cextcall _ | Cload _ | Calloc _ | Cstore _
| Caddi | Csubi | Cmuli | Cmulhi _ | Cdivi | Cmodi
| Cand | Cor | Cxor | Clsl | Clsr | Casr
Expand Down
47 changes: 47 additions & 0 deletions middle_end/flambda2/algorithms/builtin_stubs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "caml/mlvalues.h"
#include "caml/fail.h"

#if defined(_MSC_VER)
#include <intrin.h>
#endif

// These are replaced with clz instructions by flambda2.
//
// Use weak symbols in order to allow compiler-libs and ocaml_intrinsics_kernel
// to be shared dependencies of the same program.

CAMLweakdef intnat caml_int_clz_tagged_to_untagged(value i) {
// Do not use Long_val(v1) conversion, instead preserving the tag.
// It guarantees that the input to builtin_clz / _BitScanReverse is
// non-zero, to guard against versions that are undefined for input 0. The
// tag does not change the number of leading zeros.
#if defined(__GNUC__) || defined(__clang__)
#if SIZEOF_PTR == SIZEOF_INT
return __builtin_clz(i);
#elif SIZEOF_PTR == SIZEOF_LONG
return __builtin_clzl(i);
#elif SIZEOF_PTR == SIZEOF_LONGLONG
return __builtin_clzll(i);
#else
#error "No builtin clz function available"
#endif
#elif defined(_MSC_VER)
unsigned long r = 0;
#ifdef SIZEOF_PTR == 8
_BitScanReverse64(&r, i);
r ^= 63;
#elif SIZEOF_PTR == 4
_BitScanReverse(&r, i);
r ^= 31;
#else
#error "No builtin bsr function available"
#endif
return r;
#else
#error "Unsupported compiler"
#endif
}

CAMLweakdef value caml_int_clz_tagged_to_tagged(value i) {
return Val_int(caml_int_clz_tagged_to_untagged(i));
}
13 changes: 13 additions & 0 deletions middle_end/flambda2/algorithms/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
(name flambda2_algorithms)
(wrapped true)
(instrumentation (backend bisect_ppx))
(foreign_stubs
(language c)
(names builtin_stubs)
(flags
((:include %{project_root}/oc_cflags.sexp)
(:include %{project_root}/sharedlib_cflags.sexp)
(:include %{project_root}/oc_cppflags.sexp))))
(ocamlopt_flags
(:standard -O3 -open Int_replace_polymorphic_compare))
(libraries ocamlcommon))

(install
(files
(dllflambda2_algorithms_stubs.so as stublibs/dllflambda2_algorithms_stubs.so))
(section lib)
(package ocaml))
Loading
Loading