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

Let cc-rs choose the correct optimization flags for MSVC (/O2 instead of /Ox) #1596

Merged
merged 1 commit into from
Oct 13, 2023

Conversation

andoalon
Copy link
Contributor

@andoalon andoalon commented Mar 7, 2023

/Ox is widely misunderstood to be the flag to enable maximum optimization with MSVC, partly because it used to be called "Full optimzation" in the Visual Studio GUI (it has been changed and now it's called "Optimizations" or "Enable Most Speed Optimizations" which describes it better). The flag to enable maximum optimizaitions however is actually /O2 ("Maximize Speed"), which was and still is the default flag for release builds in Visual Studio.

https://learn.microsoft.com/en-us/cpp/build/reference/ox-full-optimization?view=msvc-170
https://stackoverflow.com/questions/5063334/what-is-the-difference-between-the-ox-and-o2-compiler-options

If the intention was indeed to have /O2 minus the flags /Ox is missing I would suggest adding a comment

@@ -619,7 +619,10 @@ fn cc(file: &Path, ext: &str, target: &Target, include_dir: &Path, out_file: &Pa
// run-time checking: (s)tack frame, (u)ninitialized variables
let _ = c.flag("/RTCsu");
} else {
let _ = c.flag("/Ox"); // Enable full optimization.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you agree that cc-rs does the right thing automatically, so that we can just remove both this line and the analogous "/Od" line above? I would prefer to do that, if cc-rs is automatically doing the right thing. (In general, I want to remove as much logic from build.rs that duplicates that cc-rs does as possible; see recent PRs that change build.rs to this effect.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't previously used cc-rs so I assumed manually specifying optimization flags was somehow necessary 😅
Well, cc-rs can determine the correct flag for each compiler: https://github.com/rust-lang/cc-rs/blob/2447a2ba5f455c00b1563193e125b60eebbd8ebe/src/lib.rs#L1748-L1749

I quickly checked the docs and there's this function that lets you manually specify the optimization level, but it also says that the OPT_LEVEL environment variable is used to determine the correct value otherwise (which is what is done manually here).

About "/Od", it is the default so no need to specify it: https://learn.microsoft.com/en-us/cpp/build/reference/od-disable-debug?view=msvc-170

It's not clear to me whether "/RTCsu" is on by default:
https://learn.microsoft.com/en-us/cpp/build/reference/rtc-run-time-error-checks?view=msvc-170

The Visual Studio IDE does enable it for debug builds but that might not be true when using the compiler directly. We might need to read a little more, or just keep it to be sure.

tl;dr
Yes, I totally agree, no optimization flags should be manually set unless there's a good reason for doing it. The "/RTCsu" situation is not 100% clear to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took another look, but I couldn't conclusively find anything indicating that /RTCsu is enabled by default. Therefore I left that part but removed the rest. If you think you don't need /RTCsu, then the whole if statement can be removed, otherwise I would say the change is ready to be merged

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andoalon I would be happy to have the /RTCsu removed.

I agree to license my contributions to each file under the terms given
at the top of each file I changed.
@andoalon andoalon force-pushed the fix-msvc-optimization-flag branch from 4f71e70 to 64e59de Compare October 12, 2023 12:52
@andoalon andoalon changed the title Use "real" full optimization flag with MSVC (/O2 instead of /Ox) Let cc-rs choose the correct optimization flags for MSVC (/O2 instead of /Ox) Oct 12, 2023
@codecov
Copy link

codecov bot commented Oct 12, 2023

Codecov Report

Merging #1596 (64e59de) into main (95948b3) will increase coverage by 3.82%.
Report is 1043 commits behind head on main.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #1596      +/-   ##
==========================================
+ Coverage   92.24%   96.06%   +3.82%     
==========================================
  Files         127      136       +9     
  Lines       18811    20532    +1721     
  Branches      196      217      +21     
==========================================
+ Hits        17352    19725    +2373     
+ Misses       1422      768     -654     
- Partials       37       39       +2     

see 67 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@briansmith
Copy link
Owner

briansmith commented Oct 12, 2023

I also investigated this and found the following: We are presently (before this PR) using /Ox for release builds instead of /O2.
According to what I read the difference between /Ox and /O2 is /Gs /GF /Gy. Our build.rs unconditionally enables /Gy which is function-level linking. /GF is irrelevant to us because we don't deal with strings in C. Not sue about /Gs but we unconditionally enable /GS presently.

I suggest, in addition to your changes, we should also remove /GS and /RTCsu and just go with the defaults for these. We should keep on unconditionally enabling /Gy function-level linking.

@briansmith briansmith merged commit 0841301 into briansmith:main Oct 13, 2023
123 checks passed
@briansmith
Copy link
Owner

Thanks!

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.

2 participants