Skip to content

cmake inconsistently overriding -O3 (sometimes) #31491

Closed

Description

@fanquake

Using master @ d73f37d.

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.

Activity

  1. added this to the 29.0 milestone on Dec 13, 2024
  2. hebasto commented on Dec 13, 2024

    @hebasto
    Member

    Optimization levels and debugging options are tied to a "build type/configuration" in CMake. If a user wants to override any of these, they should use the CMAKE_CXX_FLAGS_<CONFIG> variables. In depends, the $(host)_{release,debug}_CXXFLAGS variables handle this. However, they are not overridable by the user at the moment.

  3. fanquake commented on Dec 13, 2024

    @fanquake
    MemberAuthor

    If a user wants to override any of these, they should use the CMAKE_CXX_FLAGS_ variables.

    Any user would expect this to be handled by the toolchain file, which is meant to pass everything required through to CMake. Regardless of that, the fact that the behaviour here is inconsistent depending on whether or not you pass -DCMAKE_BUILD_TYPE=RelWithDebug explictly, is confusing.

  4. fanquake commented on Dec 13, 2024

    @fanquake
    MemberAuthor

    If a user wants to override any of these, they should use the CMAKE_CXX_FLAGS_ variables.

    Does this actually work in this case though? If I do:

    cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS_RELEASE="-O3"
    <snip>
    C++ compiler flags .................... -O2 -std=c++20 -fPIC -fno-extended-identifiers -fdebug-prefix-map=/root/ci_scratch/src=. -fmacro-prefix-map=/root/ci_scratch/src=. -fstack-reuse=none -Wall -Wextra -Wformat -Wformat-security -Wvla -Wredundant-decls -Wdate-time -Wduplicated-branches -Wduplicated-cond -Wlogical-op -Woverloaded-virtual -Wsuggest-override -Wimplicit-fallthrough -Wunreachable-code -Wbidi-chars=any -Wundef -Wno-unused-parameter -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -Wstack-protector -fstack-protector-all -fstack-clash-protection -mbranch-protection=standard

    I still get -O2 ?

  5. theuni commented on Dec 13, 2024

    @theuni
    Member

    I still get -O2 ?

    I'm seeing the same. This definitely worked at some point, I assume there's been some regression since merge?

  6. theuni commented on Dec 13, 2024

    @theuni
    Member

    Ah, I see. It's:
    replace_cxx_flag_in_config(Release -O3 -O2)

    I suppose that should only happen if the -O3 isn't coming from an explicitly set CMAKE_CXX_FLAGS_RELEASE.

  7. fanquake commented on Dec 16, 2024

    @fanquake
    MemberAuthor

    I suppose that should only happen if the -O3 isn't coming from an explicitly set CMAKE_CXX_FLAGS_RELEASE.

    Possibly, but I think the way this has been implented is confusing (also not properly documented), and the goals are unclear. Are we trying to prevent compilation with -O3, or just trying to make things as non-standard (CMake) as possible. Why even do the replacement if you are going to let it be overridden (using the standard flags)? The current code, and that behaviour are just going to trip up everyone that expects 'Release' to already be a certain thing.

  8. fanquake commented on Jan 6, 2025

    @fanquake
    MemberAuthor

    @hebasto Given that your suggestion of

    > If a user wants to override any of these, they should use the CMAKE_CXX_FLAGS_

    doesn't work. What should users do? Are we disallowing -O3 wholesale? If this is the case, it should probably be documented (with the rationale), and likely warned about at configure time (if they have set -O3 via CMAKE_CXX_FLAGS_ or any other way), rather than sliently swapping users flags out from under them.

    I suppose that should only happen if the -O3 isn't coming from an explicitly set CMAKE_CXX_FLAGS_RELEASE.

    This would be better than the current approach of silently changing user flags, but thinking about this more, I'm also not sure we want to be adding even more special cases, without it being clear what outcome we are trying to acheive.

  9. theuni commented on Jan 6, 2025

    @theuni
    Member

    I'd say this actually isn't a special case, rather it's fixing a logical bug in the current impl.

    Currently, we essentially do: echo "$CMAKE_CXX_FLAGS_RELEASE" | sed "s/-O3/-O2/".

    Instead, I think we only want to do that if we're not overriding the user. Essentially the same as: https://github.com/bitcoin/bitcoin/blob/28.x/configure.ac#L298

    So basically the user will always get -O2, unless they change the optims by setting them in the supported/documented variable: -DCMAKE_CXX_FLAGS_RELEASE=-O3

    See here for the fix: theuni@4f8f7de

    I can PR ^^ if you're good with it.

  10. theuni commented on Jan 6, 2025

    @theuni
    Member

    Note that this does not address the depends problem. The issue there is that the "-Ox" flags are jumbled in with the rest of the flags.

    To fix that, we need to grab the "-O" values out of the passed-in CXXFLAGS and tack the last one onto CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT. I'll push a commit for that to my branch.

  11. theuni commented on Jan 6, 2025

    @theuni
    Member

    Unfortunately it seems like the behavior around $config_FLAGS is just broken in depends. It uses _INIT values which are always appended-to.

    So at the moment, the $config setting always takes precedence over what's set in depends, even if not overriding C(XX)FLAGS.

    @hebasto Have you experimented with using regular cache variables as opposed to the _INIT ones?

  12. fanquake commented on Jan 10, 2025

    @fanquake
    MemberAuthor

    Instead, I think we only want to do that if we're not overriding the user. Essentially the same as: https://github.com/bitcoin/bitcoin/blob/28.x/configure.ac#L298

    So should we also be documenting that our -DCMAKE_BUILD_TYPE=Release doesn't give -O3 (given it's the expected CMake default)? As users explicitly setting that are expecting -O3, without having to also provide it via CMAKE_CXX_FLAGS_RELEASE, but still wont get it via master or your branch above.

  13. 6 remaining items

  14. removed this from the 29.0 milestone on Mar 11, 2025
  15. sipa commented on Apr 24, 2025

    @sipa
    Member

    See also #32337.

  16. fanquake commented on Apr 25, 2025

    @fanquake
    MemberAuthor

    @hebasto can you followup with all the questions asked in this thread:

    #31491 (comment)

    #31491 (comment)

    It'd be good to get some clarification on which behaviour is meant to work.

  17. maflcko commented on Apr 25, 2025

    @maflcko
    Member

    I guess it could make sense to append -O2 in the case of -DCMAKE_BUILD_TYPE=RelWithDebInfo reported in OP as well, to avoid the unexpected -O3 there? (The output reading a bit odd seems like a style issue that can be ignored for now)

  18. hebasto commented on Apr 26, 2025

    @hebasto
    Member

    @hebasto can you followup with all the questions asked in this thread:

    #31491 (comment)

    It'd be good to get some clarification on which behaviour is meant to work.

    @theuni

    @hebasto Have you experimented with using regular cache variables as opposed to the _INIT ones?

    Cache variables are designed to be set by the user. With the suggested approach, there is no way to distinguish whether the variable is set on the command line by the user or in a toolchain file.

  19. hebasto commented on Apr 26, 2025

    @hebasto
    Member

    @theuni

    Ah, I see. It's: replace_cxx_flag_in_config(Release -O3 -O2)

    I suppose that should only happen if the -O3 isn't coming from an explicitly set CMAKE_CXX_FLAGS_RELEASE.

    @fanquake

    @hebasto Given that your suggestion of

    > If a user wants to override any of these, they should use the CMAKE_CXX_FLAGS_

    doesn't work. What should users do? Are we disallowing -O3 wholesale? If this is the case, it should probably be documented (with the rationale), and likely warned about at configure time (if they have set -O3 via CMAKE_CXX_FLAGS_ or any other way), rather than sliently swapping users flags out from under them.

    Fixed in #32356.

  20. hebasto commented on Apr 26, 2025

    @hebasto
    Member

    Using master @ d73f37d.

    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?).

    I cannot reproduce the output for the second cmake invocation.

  21. added a commit that references this issue on May 8, 2025
  22. maflcko commented on May 9, 2025

    @maflcko
    Member

    Anything left to be done here? It looks like the initial reports were about some depends-specific issues. However, the discussion then went into a general discussion about O3 and release notes? The -DCMAKE_CXX_FLAGS_type thing should be fixed by now.

    It could make sense to file a new issue, or at least change the title to clarify that this is a depends-related report. Assuming that this isn't already covered by #31920 or #30813

  23. fanquake commented on May 9, 2025

    @fanquake
    MemberAuthor

    Given the amount of discussion here, we can re-open new issues.

  24. added a commit that references this issue on Jun 28, 2026
  25. added a commit that references this issue on Jun 30, 2026
  26. locked and limited conversation to collaborators on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions