bitcoin: cmake inconsistently overriding `-O3` (sometimes)
Using master @ d73f37dda221835b5109ede1b84db2dc7c4b74a1.
The following seems incorrect, because
make -C depends/ NO_QT=1 NO_WALLET=1 NO_ZMQ=1 -j18 CFLAGS="-O3" CXXFLAGS="-O3"
cmake -B build --toolchain /root/ci_scratch/depends/aarch64-unknown-linux-gnu/toolchain.cmake
<snip>
C++ compiler flags .................... -O3 -O2 -g
Linker flags .......................... -O3 -O2 -g
is overriding the user-provided -O3.
If you configure by providing an explicit -DCMAKE_BUILD_TYPE=RelWithDebInfo, you get:
cmake -B build --toolchain /root/ci_scratch/depends/aarch64-unknown-linux-gnu/toolchain.cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo
C++ compiler flags .................... -O3
Linker flags .......................... -O3
No -O2 at all, so the user provided -O3 is used (also missing -g for debug info?).
If you configure with -DCMAKE_BUILD_TYPE=Release:
cmake -B build --toolchain /root/ci_scratch/depends/aarch64-unknown-linux-gnu/toolchain.cmake -DCMAKE_BUILD_TYPE=Release
C++ compiler flags .................... -O3 -O2
Linker flags .......................... -O3 -O2
Now we are back to (inconsistently) overriding the user.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 25 (25 by maintainers)
See also #32337.
Is it the case that the primary danger for us would be the compiler optimising out undefined behaviour? If so, then our compiler warnings and
native_asanci test which has theundefinedsanitizer enabled should (in theory) catch said instances of UB?I think I might have the opposite view here, in that for our software specifically; that we should actively downgrade to “known good” configuration when users who are self-compiling will likely by-default choose
Release, as you mention.As far as I know, we can’t claim we fully understand the implications of
-O3on our codebase in terms of optimizations like:In particular we will of course be most worried about
-O3“optimizing” away cryptographic security checks, or inadvertently creating new side-channel attacks, etc.For someone who knows what they are doing and really wants (to test?)
-O3, it doesn’t seem a lot of work to use our almost currently-documented workaround:I also benchmarked
O2vsO3using benchcoin (where theO3build also included anO3build of libsecp), which found a partial IBD (block 800k -> 855k) using-O3to be at most 4% faster when running with a 32GB dbcache:And with a default dbcache of 450MB only a meagre 2% faster.
In summary, for such a small potential maximum benefit, accepting the unknowns don’t personally feel worth it to me. I think if the automatic downgrade and workaround can be documented, and a fix provided for the current
-DCMAKE_CXX_FLAGS_RELEASEbug, we would be in a nice conservative position, where advanced users can still buildReleasewith-O3too, if they really want to, but otherwise we will override the flag forRelease.PS. The binaries for the benchcoin run are available here in case anyone would like to run their own tests with them.
PPS. It seems that Linus as recently as 2022 still does not want to entertain the option of an
-O3Kernel: https://lore.kernel.org/lkml/CA+55aFz2sNBbZyg-_i8_Ldr2e8o9dfvdSfHHuRzVtP2VMAUWPg@mail.gmail.com/ outside of manually setting special build flags (much like our current “workaround” described above).Ah, I see. It’s:
replace_cxx_flag_in_config(Release -O3 -O2)I suppose that should only happen if the
-O3isn’t coming from an explicitly setCMAKE_CXX_FLAGS_RELEASE.