Skip to content

8349214: Improve size optimization flags for MSVC builds #23432

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

Closed
wants to merge 1 commit into from

Conversation

MBaesken
Copy link
Member

@MBaesken MBaesken commented Feb 4, 2025

Looks like the binary size optimization flags are not ideal when compiling with MSVC.
On other compilers (gcc/clang) the current size optimization flags lead in most cases to smaller libraries. On MSVC this seems to be not the case, the libs often get larger when optimizing for SIZE.

For example jvm.dll (build with VS2022) with current -Os size optimization
20M images/jdk/bin/server/jvm.dll

with -O1 set for size optimization
13M images/jdk/bin/server/jvm.dll

See the doc from MSVC about "optimize for size"
https://learn.microsoft.com/en-us/cpp/build/reference/o1-o2-minimize-size-maximize-speed?view=msvc-170
"The /O1 option sets the individual optimization options that create the smallest code in the majority of cases."
The Os option is only a part of the size minimization flag set :
/O1 (Minimize Size) equivalent to /Og /Os /Oy /Ob2 /GF /Gy


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8349214: Improve size optimization flags for MSVC builds (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/23432/head:pull/23432
$ git checkout pull/23432

Update a local copy of the PR:
$ git checkout pull/23432
$ git pull https://git.openjdk.org/jdk.git pull/23432/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 23432

View PR using the GUI difftool:
$ git pr show -t 23432

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/23432.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 4, 2025

👋 Welcome back mbaesken! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Feb 4, 2025

@MBaesken This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8349214: Improve size optimization flags for MSVC builds

Reviewed-by: djelinski, ihse

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 8 new commits pushed to the master branch:

  • bad39b6: 8348610: GenShen: TestShenandoahEvacuationInformationEvent failed with setRegions >= regionsFreed: expected 1 >= 57
  • 250ff86: 8349000: Performance improvement for Currency.isPastCutoverDate(String)
  • ee4caa4: 8349106: Change ChaCha20 intrinsic to use quarter-round parallel implementation on aarch64
  • b985347: 8348349: Refactor CDSConfig::is_dumping_heap()
  • beb43e2: 8349343: Add missing copyright messages in FFM benchmarks
  • e91a6ec: 8347489: RISC-V: Misaligned memory access with COH
  • d699aba: 8349135: Add tests for HttpRequest.Builder.copy()
  • 81126c2: 8349238: Some more FFM benchmarks are broken

Please see this link for an up-to-date comparison between the source branch of this pull request and the master branch.
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot changed the title JDK-8349214: Improve size optimization flags for MSVC builds 8349214: Improve size optimization flags for MSVC builds Feb 4, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 4, 2025
@openjdk
Copy link

openjdk bot commented Feb 4, 2025

@MBaesken The following label will be automatically applied to this pull request:

  • build

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the build build-dev@openjdk.org label Feb 4, 2025
@mlbridge
Copy link

mlbridge bot commented Feb 4, 2025

Webrevs

Copy link
Member

@djelinski djelinski left a comment

Choose a reason for hiding this comment

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

Makes sense.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Feb 4, 2025
@magicus
Copy link
Member

magicus commented Feb 4, 2025

What libraries are currently optimized for size on Windows, i.e. is affected by this change?

What testing have you performed to ensure no new compiler bugs show up? Changing optimization level introduces a risk of compiler-related bugs, similar to compiler upgrades. Note that this is not only about bugs in the compiler, but in our code where we use undefined behavior, which happily have worked out fine for one optimization level, but which results in bad code generated (correctly) by the compiler.

@djelinski
Copy link
Member

No libraries are optimized for size by default.

Optimization for size is only used by hotspot, and only when opt-size feature is enabled; I think opt-size may be enabled in the minimal variant.

Optimization level O1 is already used in many places, specifically in every place that uses HIGH or LOW optimization.

@MBaesken
Copy link
Member Author

MBaesken commented Feb 4, 2025

No libraries are optimized for size by default.

True, it is at the moment only for opt-size / jvm.dll .

In future I plan to create a PR for libsplashscreen using SIZE optimization, but currently this would lead to a larger (!) lib with MSVC because of the bad -Os , this does not make sense.

@TheShermanTanker
Copy link
Contributor

splashscreen already uses LOW for its optimization flags though. Is the intent to change it to SIZE for all compilers?

@MBaesken
Copy link
Member Author

MBaesken commented Feb 4, 2025

splashscreen already uses LOW for its optimization flags though. Is the intent to change it to SIZE for all compilers?

LOW uses NORM
https://github.com/openjdk/jdk/blob/master/make/common/native/Flags.gmk#L42
and this is already O1 on Windows/MSVC
C_O_FLAG_NORM="-O1"

so for Windows switching to SIZE won't change anything after this PR. So yes it will probably be SIZE for splashscreen for all compilers.

@magicus
Copy link
Member

magicus commented Feb 4, 2025

Sounds good then.

The optimization levels were kind of a mess that we "inherited" from the old build system, were they had just agglutinated over the years, probably dating back to Java 1.0, with nobody left around to remember why they were the way they were.

So it is definitely ripe for an overhaul. Anything you can do in this area is much appreciated, as long as it does not cause any regressions.

@MBaesken
Copy link
Member Author

MBaesken commented Feb 5, 2025

Thanks for the reviews !

/integrate

@openjdk
Copy link

openjdk bot commented Feb 5, 2025

Going to push as commit 40603a5.
Since your change was applied there have been 12 commits pushed to the master branch:

  • 0926949: 8347629: Test FailOverDirectExecutionControlTest.java fails with -Xcomp
  • a51e669: 8349200: [JMH] time.format.ZonedDateTimeFormatterBenchmark fails
  • 9d23de5: 8184352: Remove Sun provider information from KeyPairGenerator javadoc
  • d222c18: 8349107: Remove RMI finalizers
  • bad39b6: 8348610: GenShen: TestShenandoahEvacuationInformationEvent failed with setRegions >= regionsFreed: expected 1 >= 57
  • 250ff86: 8349000: Performance improvement for Currency.isPastCutoverDate(String)
  • ee4caa4: 8349106: Change ChaCha20 intrinsic to use quarter-round parallel implementation on aarch64
  • b985347: 8348349: Refactor CDSConfig::is_dumping_heap()
  • beb43e2: 8349343: Add missing copyright messages in FFM benchmarks
  • e91a6ec: 8347489: RISC-V: Misaligned memory access with COH
  • ... and 2 more: https://git.openjdk.org/jdk/compare/beae8843b9b2433af5e9fbe420b17e663cbdb960...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Feb 5, 2025
@openjdk openjdk bot closed this Feb 5, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Feb 5, 2025
@openjdk
Copy link

openjdk bot commented Feb 5, 2025

@MBaesken Pushed as commit 40603a5.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@MBaesken
Copy link
Member Author

MBaesken commented Feb 5, 2025

Sounds good then.

The optimization levels were kind of a mess that we "inherited" from the old build system, were they had just agglutinated over the years, probably dating back to Java 1.0, with nobody left around to remember why they were the way they were.

So it is definitely ripe for an overhaul. Anything you can do in this area is much appreciated, as long as it does not cause any regressions.

Thanks for the clarification !
Given that on Windows we compiled all the (JDK) native libraries with OPTIMIZATION setting LOW in fact with O1 (minimize size / create the smallest code in the majority of cases) for years, there is a high chance that size-optimization would work nicely for those (level LOW) too on platforms with gcc/clang ; but to be on the safe side we probably have to look into it on a per library level instead of just change it for all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build build-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants