-
Notifications
You must be signed in to change notification settings - Fork 715
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
Let cc-rs choose the correct optimization flags for MSVC (/O2
instead of /Ox
)
#1596
Conversation
@@ -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. |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
4f71e70
to
64e59de
Compare
/O2
instead of /Ox
)/O2
instead of /Ox
)
Codecov Report
@@ 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 |
I also investigated this and found the following: We are presently (before this PR) using I suggest, in addition to your changes, we should also remove |
Thanks! |
/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