Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: Allow specifying optimization level in COMMON_OPT #3748

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

mmuetzel
Copy link
Contributor

@mmuetzel mmuetzel commented Aug 30, 2022

When building with make a default optimization level of -O2 is used.
See:
https://github.com/xianyi/OpenBLAS/blob/00534523ad999d89945d23b7df0eafc69c31f1b3/Makefile.system#L1551-L1557

By default, cmake uses -O3 for Release builds currently:
https://github.com/Kitware/CMake/blob/v3.24.1/Modules/Compiler/GNU.cmake#L59

This means binaries are built with different optimization levels depending on whether make or cmake is used.

Additionally, it is currently not possible to override the optimization level with, e.g., -DCOMMON_OPT=-O2 because it appears before the CMAKE_${Language}_FLAGS_RELEASE flags (which effectively overrides the flag in COMMON_OPT).

See also: #3740

The proposed change sets the default compiler optimization level that is used with cmake to -O2 (i.e., the same that is used by make).
Additionally, it allows overriding the compiler optimization level with flags in COMMON_OPT, CCOMMON_OPT, and FCOMMON_OPT.

@brada4
Copy link
Contributor

brada4 commented Aug 31, 2022

Maybe it is worth playing warning, my rationale is that -Os has a practical purpose always when used, but say -Ofast guarantees numeric faults.

@mmuetzel
Copy link
Contributor Author

Maybe it is worth playing warning, my rationale is that -Os has a practical purpose always when used, but say -Ofast guarantees numeric faults.

I'm not sure I understand your comment.
The proposed change doesn't touch any flags supplied by the user in the COMMON_OPT variables. And I'd argue that this should stay as it is. The build system should assume that a user knows what they are doing when setting custom flags...

@brada4
Copy link
Contributor

brada4 commented Aug 31, 2022

PR is not wrong, just that probably needs some discussion/consideration.

@mmuetzel
Copy link
Contributor Author

PR is not wrong, just that probably needs some discussion/consideration.

I'm happy to discuss. But I don't understand which changes you are proposing.

@mmuetzel
Copy link
Contributor Author

I'm not sure why one of the checks of this PR was failing:
https://github.com/xianyi/OpenBLAS/runs/8095576089?check_suite_focus=true

IIUC, that check uses make to build OpenBLAS. Afaict, this PR doesn't touch the Makefile rules.

@martin-frbg
Copy link
Collaborator

I'm not sure why one of the checks of this PR was failing:
Me neither, that cross-build check has been failing more or less randomly lately and I have not managed to reproduce this locally.

On the actual topic of your PR, I am unsure if it would be useful to change the well-defined CMAKE default behaviour for Release builds years after adding cmake support. Perhaps it might be more useful to just handle the case where the user declared no build type at all (as a novice user might), which currently would result in no -O option at all (I think) ?

@mmuetzel
Copy link
Contributor Author

mmuetzel commented Sep 1, 2022

Afaict, it is not completely uncommon that projects override the flags in CMAKE_${Language}_FLAGS_${CMAKE_BUILD_TYPE}. But I understand your point of view.
We could just remove the block that modifies these flags from the PR.

At the same time, it might be surprising for users if optimization flags that they pass with the COMMON_OPT variables don't actually have any effect. (At least, it was for me.)

Should the build rules check if there are any -O flags in the COMMON_OPT variables and remove them from cmake's default flags conditional on that? That would be possible. But it would be pretty involved (and maybe more error prone?).

@mmuetzel
Copy link
Contributor Author

mmuetzel commented Sep 1, 2022

I updated the PR to leave cmake's default optimization level alone unless the user explicitly specifies optimization flags in any of the COMMON_OPT variables.

The default optimization level might be overridden by default settings of cmake.
@brada4
Copy link
Contributor

brada4 commented Sep 2, 2022

I think we set release_with_debuginfo for novice user case which roughly matches makefile behaviour.
-frecursive is a paramount, it is obscure name for thread safety in fortran....

@mmuetzel mmuetzel changed the title cmake: Use same default optimization level as Makefiles. cmake: Allow specifying optimization level in COMMON_OPT Sep 20, 2022
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