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

Runtime detection of CPU features #53

Merged
merged 12 commits into from Apr 30, 2020
18 changes: 0 additions & 18 deletions README.md
Expand Up @@ -41,21 +41,3 @@ Unix:
```OCaml
let () = Mirage_crypto_rng_unix.initialize ()
```

#### Illegal instructions

```
Program terminated with signal SIGILL, Illegal instruction.
#0 _mm_aeskeygenassist_si128 (__C=<optimized out>, __X=...)
```

`Mirage_crypto` has CPU acceleration support (`SSE2`+`AES-NI`), but no run-time
autodetection yet. You compiled the library with acceleration, but you are using
it on a machine that does not support it.

The environment variable `MIRAGE_CRYPTO_ACCELERATE` can be used to override
detection:

- `MIRAGE_CRYPTO_ACCELERATE=false dune build` force-disables non-portable code.
- `MIRAGE_CRYPTO_ACCELERATE=true dune build` force-enables non-portable code.
- Otherwise, it matches the capabilities of the build machine.
9 changes: 1 addition & 8 deletions config/cfg.ml
@@ -1,14 +1,7 @@
let evar = "MIRAGE_CRYPTO_ACCELERATE"
let flags = ["-DACCELERATE"; "-mssse3"; "-maes"; "-mpclmul"]
let accelerate_flags = ["-mssse3"; "-maes"; "-mpclmul"]
let std_flags = ["--std=c99"; "-Wall"; "-Wextra"; "-Wpedantic"; "-O3"]

let _ =
let accelerate_flags = match Sys.getenv evar with
| "true" -> flags
| "false" -> []
| _ -> flags
| exception Not_found -> flags
in
let ent_flags =
let c = Configurator.V1.create "mirage-crypto" in
let arch = Configurator.V1.Process.run c "uname" ["-m"] in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just two lines below, the accelerate_flags should be passed -- i.e only on x86 platform, not on arm (this is as far as I can tell what cpuid would have done as well)... or am I misguided and ARM nowadays supports -mssse3 -maes -mpclmul?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering the same. Fixed !
Should it be passed for amd64 and x86 ? It seems only x86_64 was selected in mirage-crypto.h. I removed that check so we have only one place to worry about this.

Expand Down
1 change: 0 additions & 1 deletion src/dune
Expand Up @@ -13,5 +13,4 @@
(include_subdirs unqualified)

(rule
(deps (env_var MIRAGE_CRYPTO_ACCELERATE))
(action (with-stdout-to cflags.sexp (run ../config/cfg.exe))))
2 changes: 1 addition & 1 deletion src/native/mirage_crypto.h
Expand Up @@ -7,7 +7,7 @@
#include <caml/mlvalues.h>
#include <caml/bigarray.h>

#if defined (__x86_64__) && defined (ACCELERATE)
#if defined (__x86_64__)
#include <x86intrin.h>
#define __mc_ACCELERATE__
#endif
Expand Down