Skip to content

Makefile: Add compile-time detection for compiler and host platform feature support #1144

@hanno-becker

Description

@hanno-becker

Context: The idea of auto.mk is to optimize the build for the platform on which mlkem-native is built. This is done by automatically adding various AVX2 feature flags.

Issues: There are issues at multiple levels.

  1. On x86_64, auto.mk makes two assumptions about the compilation host:
    i. It assumes that the host compiler supports various feature flags: -mavx2 -mbmi2 -mpopcnt -maes
    ii. It assumes that the host CPU supports the aforementioned AVX2 features.
  2. On AArch64, auto.mk does not deal with the Armv8.4-a+SHA3 extension required for parts of the FIPS202 backend. However, the analogous way of dealing with this, by adding -march=armv8.4-a+sha3 to CFLAGS, would suffer from the same issues as in point 1.

Task:

  1. Add a new test/mk/compiler.mk which detects whether the architectural features used by mlkem-native are supported by the host compiler.
    i. For x86_64, detect whether the compiler supports AVX2, SSE2, and BMI2.
    ii. For AArch64, detect whether the compiler supports the SHA3 extension.

    The detected compiler support should be captured in Makefile variables MLK_COMPILER_SUPPORTS_XXX. It should not be reflected in any CFLAGS. This is to avoid creating a dependency of the mlkem-native sources on our own build system.

    The feature detection for the compiler should be done by try-compilation.

  2. Extend test/mk/auto.mk to check whether the host CPU (the CPU on which mlkem-native is compiled) supports the aforementioned features. This should at least support any combination of {x86_64, AArch64} x {MacOS, Linux}. The results should be captured in MLK_HOST_SUPPORTS_XXX, following the same naming as MLK_COMPILER_SUPPORTS_XXX. Then, if for any given feature XXX, both MLK_COMPILER_SUPPORTS_XXX and MLK_HOST_SUPPORTS_XXX are set, the CFLAGS should be amended with the appropriate architecture flag. The setting of MLK_FORCE_XXX directives should be kept. For platforms other than the ones listed above, the CFLAGS should not be further amended -- for those, one relies on the default compiler configuration being tailored to the target CPU.

  3. Add a make host_info target to the main Makefile which summarizes the host-compiler/CPU feature support. This will require unconditionally importing auto.mk (unguarded by AUTO=0/1), but instead guarding the extension of the CFLAGS by AUTO=1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions