Skip to content

Select SIMD code from compiler target flags - #367

Open
dimpase wants to merge 2 commits into
algebraic-solving:masterfrom
dimpase:honor-target-cflags
Open

dimpase wants to merge 2 commits into
algebraic-solving:masterfrom
dimpase:honor-target-cflags

Conversation

@dimpase

@dimpase dimpase commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

AX_EXT probes the build machine's CPU and adds SIMD flags based on that machine. Distribution and cross builds need the selected code path to follow the compiler target without appending machine-derived flags to CFLAGS.

Select AVX2 and AVX-512 code from the compiler target by default. Add --enable-avx2 and --enable-avx512f, so packagers can explicitly enable or disable either code path. An explicit enable checks that the compiler target supports the required intrinsics and fails at configure time with a clear message if it does not. The AVX-512 implementation requires both AVX-512F and AVX-512BW. The two options can be disabled independently, including AVX2 while retaining AVX-512.

This is compile-time selection; it does not add runtime CPU dispatch.

Validation

  • Baseline CFLAGS='-O2 -pipe -march=x86-64': build and all 68 tests passed; neither SIMD implementation enabled.
  • CFLAGS='-O2 -pipe -mavx2': build and all 68 tests passed with AVX2 enabled.
  • CFLAGS='-O2 -pipe -mavx2' --disable-avx2: build and all 68 tests passed with both SIMD implementations disabled.
  • CFLAGS='-O2 -pipe -mavx512f -mavx512bw' --disable-avx2 --enable-avx512f: build passed with AVX-512 enabled and AVX2 disabled. The binary was not run because this host lacks AVX-512.
  • CFLAGS='-O2 -pipe -mavx512f' fails during configure with an AVX-512BW requirement message; --disable-avx512f allows that target.
  • --enable-avx2 with a baseline x86-64 compiler target fails during configure with an AVX2 requirement message.
  • Baseline make distcheck passed before the option follow-up.

Signed-off-by: Dima Pasechnik dima@pasechnik.info

AI assistance: Codex helped prepare and validate this change.

Configure AVX2 and AVX-512 code using the compiler target macros instead of probing the build machine. Remove the now-unused host-derived SIMD and CPU extension flags from the Makefiles so CFLAGS controls the selected implementation.

Signed-off-by: Dima Pasechnik <dima@pasechnik.info>
Co-authored-by: Codex <codex@openai.com>
@dimpase

dimpase commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

@mohabsafey - This should make life much easier for distributions. The normally don't want arch-dependent flags of the build host to leak into binaries they build - something that potentially leads to annoying runtime errors.
Instead, this makes the compiler and other tools' settings the default.

You can still build using the host arch, by passing -march=native to ./configure.

@dimpase

dimpase commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor Author

You may wonder why one CI run on this PR is failing - this is reproducible with the random seed of that very run, namely:

SEED=1790795968 bash test/diff/diff_cyclic5-16.sh

fails both ways, with and without this PR branch.

@dimpase

dimpase commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

@orlitzky - this came from an attempt to make a Gentoo ebuild for msolve.

@orlitzky

orlitzky commented Oct 2, 2026

Copy link
Copy Markdown

This is an improvement as-is (we don't want machine CFLAGS added by surprise), but I would go one step further and disconnect the machine-specific code from the compiler's optimization flags. The macros like __AVX2__ are only defined when e.g. -mavx2 is in the user's CFLAGS... they are a good guess, but occasionally it is nice to be able to switch off the optimizations without the code, or the other way around.

Gentoo has USE flags for these (cpu_flags_x86_avx2 and cpu_flags_x86_avx512f) that end users will typically enable on machines that support them, but can be disabled for whatever weird reasons. This is independent from CFLAGS which we try to leave alone / ignore.

The best solution from that perspective would be,

  • Don't append any CFLAGS at all (already done in this PR)
  • Add --enable-avx2 and --enable-avx512f flags to ./configure that default to on if __AVX2__ and __AVX512F__ are defined, but can be overridden with --disable-<flag>.

Let packagers override the compiler-target defaults with --disable-avx2
and --disable-avx512f without changing CFLAGS. Check that enabled code
can compile with the selected target, including the AVX-512BW intrinsics
used by the AVX-512 implementation. Include the intrinsic headers when
AVX-512 is enabled independently of AVX2.

Co-authored-by: Codex <codex@openai.com>
Signed-off-by: Dima Pasechnik <dima@pasechnik.info>
@dimpase

dimpase commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

@orlitzky - like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants