Skip to content

Select SIMD code from compiler target flags - #367

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

dimpase wants to merge 7 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.

Configure selects AVX2, AVX-512F and the AVX-512 extensions supported by the compiler target, and lists the selected SIMD kernels. The --enable-avx2 and --enable-avx512f options and their --disable forms control the two groups independently. AVX-512F kernels also work on targets that lack AVX-512BW. The --enable-avx512bw and --disable-avx512bw options control the byte and word kernels within the AVX-512F group. Explicitly requesting an unavailable feature gives a configure error.

For Hensel lifting, configure also checks whether the installed FLINT fft_small vector operations compile with the selected flags. If they do not, msolve uses its polynomial multiplication fallback.

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 AVX2 nor AVX-512 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 AVX2 and AVX-512 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.
  • Cross-built with CFLAGS='-O2 -mavx2 -mfma -mavx512f'. Configure selected AVX-512F, and the 8-bit and 16-bit sparse-row kernels used their AVX2 implementations. --enable-avx512bw on this target produced the expected error.
  • --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.
  • All 97 tests passed on ARM macOS with NEON. Linux CI now checks F without BW, optional AVX-512 extensions and explicit disables. The fixed-seed Linux build exercises -march=native.

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

AI assistance: Codex helped prepare and validate this change.

@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>.

@dimpase

dimpase commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

@orlitzky - like this?

@orlitzky

orlitzky commented Oct 3, 2026

Copy link
Copy Markdown

@orlitzky - like this?

Yeah, thanks. It looks like avx512bw was also being included under the umbrella of avx512f? I don't have a new enough processor to confirm, but if so, we have a separate USE flag for avx512bw.

It would also be nice to have an AC_MSG_CHECKING ... AC_MSG_RESULT pair when the options are set to "auto" :)

@orlitzky

orlitzky commented Oct 3, 2026

Copy link
Copy Markdown

The intel docs say that _mm256_mullo_epi16 and _mm512_mullo_epi16 are part of avx512bw and I see that they're used in some but not all of the same places as the other avx512f stuff is. The least effort solution at the moment is probably to,

  1. Add --enable-avx512bw like the others and define HAVE_AVX512_BW.
  2. In the functions where both are needed, change the preprocessor conditionals to HAVE_AVX512_F && HAVE_AVX512_BW.
  3. Anywhere else, leave alone.

In the future, someone could improve the granularity by adding fallback C multiplication for the few avx512bw instructions, allowing people with avx512f but not avx512bw to reap most of the benefits.

@orlitzky

orlitzky commented Oct 3, 2026

Copy link
Copy Markdown

(And I think m4/ax_ext.m4 and m4/ax_gcc_x86_avx_xgetbv.m4 become redundant afterwards.)

I see that there's an issue (#71) and draft PR (#376) for runtime detection. I don't think this necessarily conflicts, we would just limit runtime detection to the "auto" case. Being able to disable the intrinsics is independently useful, and in the easy case (building for your own machine) it would remain much simpler to just enable them.

dimpase and others added 2 commits November 22, 2026 13:49
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>
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 force-pushed the honor-target-cflags branch from ec7007e to 195d192 Compare November 22, 2026 19:49
@mohabsafey

Copy link
Copy Markdown
Contributor

Thank you @dimpase Will try to have a look asap (but not before the end of the coming week)

Compile the vector operations used by Hensel lifting before enabling its
fft_small path. FLINT_HAVE_FFT_SMALL describes the installed library, but
its headers can require AVX2 and FMA that are absent from msolve's target
flags. Use the existing polynomial-multiplication fallback when the probe
fails, without adding target flags or running a configure test program.
Keep explicit HENSEL_NTT overrides available.

Validated against FLINT 3.6.0: all 97 tests pass on ARM macOS with the
detected fast path and with the configure result forced to the fallback.
The probe rejects baseline x86-64 and AVX2 without FMA, and accepts AVX2
with FMA, when cross-compiling at -O0, -O2 and -O3.

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

dimpase commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

5f0679a is meant to fix building failures detected by Windows CI (installed Flint AVX capabilities don't mean they are available on the testing host, too)

@mohabsafey

Copy link
Copy Markdown
Contributor

@dimpase thank you for this. I think the INSTALL and README files should (strongly) suggest CFLAGS="-O2 -march=native" as an option to ./configure. Also, I agree with the suggestion of @orlitzky (many thanks). One problem I find is that it seems that even with -march=native, the IFMA, AVX-512 BW/DQ, FMA3 and SSE2/SSSE3 paths stay out of compilation (according to claude). Reason is that their HAVE_* macros are no longer defined. Since we recently improved the efficiency in several ways including using these, I would like to make sure that the user who wants the best for his computer after compilation just gets it.
Are you working on this or could I try to propose some more concrete suggestions/commits on this PR? BTW, all apologies for the delay

dimpase and others added 2 commits November 25, 2026 13:56
Restore SSE2, SSSE3, FMA3 and the AVX-512 BW, DQ and IFMA guards without adding flags for the build host. Keep the AVX2 and AVX-512 opt-outs and document native builds as a local opt-in.

Co-authored-by: OpenAI Codex <codex@openai.com>
Resolve the overlap with algebraic-solving#410 by retaining the configure-time FLINT fft_small compile check, so the Hensel code still falls back when the target cannot compile those headers.

Co-authored-by: OpenAI Dot <support@openai.com>
@dimpase

dimpase commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@mohabsafey please have a look at the update - does it fix everything you asked for?

@mohabsafey

Copy link
Copy Markdown
Contributor

Many thanks. It seems that a target with AVX-512F but no BW (for example -mavx2 -mfma -mavx512f) makes configure stop with an error, although nobody asked for AVX-512. Could be fixed by checking AVX512BW as @orlitzky suggested? I also think that configure should displays something about the SIMD kernels it selects. Also, shouldn't the CI be adapted? Many thanks again @dimpase

A target such as -mavx2 -mfma -mavx512f can compile the foundation
kernels. Probe byte and word support separately and use the AVX2 or
scalar implementations where those 512-bit operations are unavailable.
Allow packagers to override BW selection with --enable/--disable-avx512bw.
Print the selected kernels at the end of configure.

Exercise the F-only target, optional extensions and explicit disables in
CI. Check the configuration and build each combination, run its tests
when the CPU supports it, and test the native target in the Linux
fixed-seed build.

Co-authored-by: Codex <codex@openai.com>
Signed-off-by: Dima Pasechnik <dima@pasechnik.info>
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.

3 participants