Guard auto-vectorization flag for GCC only - #3864
Conversation
|
Thanks! |
|
|
||
| # Always enable auto vectorization | ||
| QMAKE_CXXFLAGS+=-ftree-vectorize | ||
| # GCC does not auto-vectorize at -O2 (only from -O3 up); Clang already does, MSVC |
There was a problem hiding this comment.
I think this depends on the GCC version. So the comment is not accurate
There was a problem hiding this comment.
From Google AI:
GCC added an auto-vectorizer starting in GCC 4.0. Historically, automatic loop vectorization (-ftree-vectorize) was only implicitly enabled at -O3 (or manually via -O2 -ftree-vectorize). However, starting with GCC 12, it is enabled by default at -O2 as well.
GCC Auto-Vectorization Milestones
- GCC 4.0 and later: Introduced the tree-ssa auto-vectorizer, available only with
-O3or explicitly via-ftree-vectorizeat-O2. - GCC 12 and later: Enabled automatically at
-O2using a conservative"very cheap"cost model, while-O3continues to use a more aggressive"dynamic"cost model.
Optimization Level Summary
-O0/-O1: Auto-vectorization is off (unless forced manually with-ftree-vectorizeat-O1).-O2(GCC 12+): Enabled by default using-fvect-cost-model=very-cheap.-O2(GCC 11 and older): Disabled by default; requires adding-ftree-vectorizemanually.-O3(All versions since 4.0): Enabled by default using-fvect-cost-model=dynamic.
There was a problem hiding this comment.
For reference, I looked up what versions of GCC were used in the main distros:
| Distro | GCC default | GCC max available |
|---|---|---|
Ubuntu 20 (CI build for release/3_12) |
GCC 9 | GCC 10 |
Ubuntu 22 (CI build for main and 4.x) |
GCC 11 | GCC 12 |
| Ubuntu 24 | GCC 13 | GCC 14 |
| Debian 11 | GCC 10 | |
| Debian 12 | GCC 12 | |
| Debian 13 | GCC 14 | |
| RHEL 9 | GCC 11 | |
| RHEL 10 | GCC 14 |
d02f1e3 to
a32cec9
Compare
Wrap -ftree-vectorize in *-g++* qmake scope so the flag only reaches GCC compilers. MSVC ignores the unknown flag with D9002 warning, and Clang already auto-vectorizes at -O2. Closes jamulussoftware#3863 Signed-off-by: RawNuke <67506722+RawNuke@users.noreply.github.com>
a32cec9 to
2135b7b
Compare
|
Good catch, thanks. You are right that it depends on the GCC version, so the old comment was wrong to say GCC never vectorizes at -O2. I have corrected it: -ftree-vectorize is on at -O3, and also at -O2 from GCC 12 on. Setting it explicitly keeps vectorization on for GCC builds that would not enable it by default, and scoping to -g++ avoids the MSVC warning and the redundancy on Clang. Happy to adjust the wording further if needed. |
|
It looks like there are a lot more considerations to effectively use auto-vectorization with GCC. Here's my conversation with Google AI Mode about it:
When you explicitly add This behavior is dictated by how GCC handles its optimization flags: [2] The dynamic Cost Model DefaultHistorically, the dynamic cost model was hardcoded as the global default for the vectorizer whenever it was active. If a lower optimization level like How the Cost Model Defaults Evolved
Overriding the DefaultIf you are compiling code at
[1] https://wiki.gentoo.org
That is an important distinction: if you explicitly pass Complete Optimization Flag and Cost Model Matrix
Enforcing the Old Behavior on GCC 12+If you migrate to GCC 12 or newer but want the compiler to optimize exactly like your old Without appending Are you seeing unexpected vectorization behavior (or a lack thereof) in a project that was recently upgraded to GCC 12 or newer, or are you designing a universal Makefile/CMake template? [1] https://wiki.gentoo.org
To safely inject vectorization into the build system of an open-source audio tool like Jamulus across an unpredictable landscape of user distros and compiler versions, you need a highly standardized strategy. [1] 1. The Core GCC Version MappingTo ensure consistent cost-model behavior and prevent binary bloat on distributions running newer toolchains, use this explicit flag logic: If the distro builds with an old compiler (GCC < 12) or targets explicit optimization:
If the distro builds with GCC 12 or newer:
The Solution: Do not rely on implicit cost models. If you want aggressive, uniform auto-vectorization across all GCC versions at
2. The Audio Vectorization Blocker:
|
|
Thanks for digging into this. You are right that it is more nuanced than a one line comment can capture, especially the cost model at -O1 and at -O2 on older GCC. I am happy to trim the comment down to just the reason for the change, which is scoping the flag to GCC so it does not warn on MSVC or duplicate what Clang already does, and to drop the claims about specific optimization levels. If you would rather not carry the explicit flag at all given these considerations, I am fine closing this. Whatever you prefer. |
|
MY LLM WROTE: Measured, the matrix above is off in two cells: on GCC ≤ 11, So the explicit flag alone lands every GCC version in the same state (on, |
Closes #3863
Short description of changes
Wrap the
-ftree-vectorizeflag in a*-g++*qmake scope so it only reaches GCC compilers.Context: Fixes an issue?
Fixes #3863
What does this change do?
-ftree-vectorizeflag-O2)-ftree-vectorizeflag where it's neededChecklist