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

The embedded bitcode should always be prepared for LTO/ThinLTO #133250

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

DianQK
Copy link
Member

@DianQK DianQK commented Nov 20, 2024

Fixes #115344. Fixes #117220.

There are currently two methods for generating bitcode that used for LTO. One method involves using -C linker-plugin-lto to emit object files as bitcode, which is the typical setting used by cargo. The other method is through -C embed-bitcode=yes.

When using with -C embed-bitcode=yes -C lto=no, we run a complete non-LTO LLVM pipeline to obtain bitcode, then the bitcode is used for LTO. We run the Call Graph Profile Pass twice on the same module.

This PR is doing something similar to LLVM's buildFatLTODefaultPipeline, obtaining the bitcode for embedding after running buildThinLTOPreLinkDefaultPipeline.

r? nikic

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 20, 2024
@DianQK
Copy link
Member Author

DianQK commented Nov 20, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 20, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 20, 2024
[WIP] The embedded bitcode should always be prepared for LTO/ThinLTO

Fixes rust-lang#115344. Fixes rust-lang#117220.

r? ghost
@bors
Copy link
Contributor

bors commented Nov 20, 2024

⌛ Trying commit e91e0c7 with merge 5f7a0d8...

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 20, 2024

💔 Test failed - checks-actions

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 20, 2024
@rust-log-analyzer

This comment has been minimized.

@DianQK
Copy link
Member Author

DianQK commented Nov 21, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 21, 2024

⌛ Trying commit fff2da3 with merge 5a6a7f3...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 21, 2024
[WIP] The embedded bitcode should always be prepared for LTO/ThinLTO

Fixes rust-lang#115344. Fixes rust-lang#117220.

r? ghost
@bors
Copy link
Contributor

bors commented Nov 21, 2024

☀️ Try build successful - checks-actions
Build commit: 5a6a7f3 (5a6a7f3cfdb88d3331274cd6b5d737c993dd8a98)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (5a6a7f3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.5% [0.4%, 0.7%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.5% [-2.4%, -0.5%] 4
All ❌✅ (primary) 0.5% [0.4%, 0.7%] 2

Max RSS (memory usage)

Results (primary -0.2%, secondary 2.3%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.1% [2.0%, 2.3%] 2
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
-5.0% [-5.0%, -5.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-5.0%, 2.3%] 3

Cycles

Results (secondary -1.9%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 1
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.3%, secondary -2.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.3%, 0.3%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) 0.3% [0.3%, 0.3%] 4

Bootstrap: 796.195s -> 796.003s (-0.02%)
Artifact size: 335.92 MiB -> 335.87 MiB (-0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 21, 2024
@DianQK DianQK marked this pull request as ready for review November 24, 2024 14:06
@rustbot
Copy link
Collaborator

rustbot commented Nov 24, 2024

This PR modifies tests/run-make/. If this PR is trying to port a Makefile
run-make test to use rmake.rs, please update the
run-make port tracking issue
so we can track our progress. You can either modify the tracking issue
directly, or you can comment on the tracking issue and link this PR.

cc @jieyouxu

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@DianQK DianQK changed the title [WIP] The embedded bitcode should always be prepared for LTO/ThinLTO The embedded bitcode should always be prepared for LTO/ThinLTO Nov 24, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 1, 2024
The embedded bitcode should always be prepared for LTO/ThinLTO

Fixes rust-lang#115344. Fixes rust-lang#117220.

There are currently two methods for generating bitcode that used for LTO. One method involves using `-C linker-plugin-lto` to emit object files as bitcode, which is the typical setting used by cargo. The other method is through `-C embed-bitcode=yes`.

When using with `-C embed-bitcode=yes -C lto=no`, we run a complete non-LTO LLVM pipeline to obtain bitcode, then the bitcode is used for LTO. We run the Call Graph Profile Pass twice on the same module.

This PR is doing something similar to LLVM's `buildFatLTODefaultPipeline`, obtaining the bitcode for embedding after running `buildThinLTOPreLinkDefaultPipeline`.

r? nikic
@bors
Copy link
Contributor

bors commented Dec 2, 2024

☀️ Try build successful - checks-actions
Build commit: 99acc6a (99acc6a24c3447932825ce66c54d7ae1a4b0499e)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (99acc6a): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.4% [0.4%, 0.5%] 4
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-2.0%, -2.0%] 1
All ❌✅ (primary) 0.4% [0.4%, 0.5%] 4

Max RSS (memory usage)

Results (primary -0.7%, secondary 2.8%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.8% [2.6%, 3.1%] 2
Improvements ✅
(primary)
-0.7% [-0.7%, -0.7%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.7% [-0.7%, -0.7%] 1

Cycles

Results (primary -1.3%, secondary -1.9%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.3% [-1.3%, -1.3%] 1
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 1
All ❌✅ (primary) -1.3% [-1.3%, -1.3%] 1

Binary size

Results (primary 0.3%, secondary 0.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.4% [0.1%, 0.7%] 58
Regressions ❌
(secondary)
0.9% [0.0%, 5.3%] 21
Improvements ✅
(primary)
-0.2% [-0.4%, -0.1%] 8
Improvements ✅
(secondary)
-0.4% [-1.7%, -0.3%] 38
All ❌✅ (primary) 0.3% [-0.4%, 0.7%] 66

Bootstrap: 768.518s -> 768.387s (-0.02%)
Artifact size: 332.12 MiB -> 332.19 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 2, 2024
@DianQK
Copy link
Member Author

DianQK commented Dec 3, 2024

I think this is basically ready for review. Some potential adjustments could be addressed in subsequent PRs:

  • (Possibly in this PR) Rename ThinBitcode to PreLtoBitcode?
  • Refactor the code related to emit_pre_lto_bc
  • Reuse the embed bitcode for --emit=llvm-bc when using -C linker-plugin-lto?

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 3, 2024
@DianQK
Copy link
Member Author

DianQK commented Dec 9, 2024

I think this is basically ready for review. Some potential adjustments could be addressed in subsequent PRs:

  • (Possibly in this PR) Rename ThinBitcode to PreLtoBitcode?
  • Refactor the code related to emit_pre_lto_bc
  • Reuse the embed bitcode for --emit=llvm-bc when using -C linker-plugin-lto?

@rustbot review

Hmm, when passing -Z emit-thin-lto=false, it's different bitcode between the embed bitcode and the pre-LTO bitcode. I think all changes can be discussed later.

@DianQK
Copy link
Member Author

DianQK commented Dec 17, 2024

Two weeks ping

@DianQK
Copy link
Member Author

DianQK commented Dec 30, 2024

Two weeks ping again @nikic

@DianQK
Copy link
Member Author

DianQK commented Jan 7, 2025

ping
If there's anything that needs to be changed, please don't hesitate to let me know.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

This generally looks reasonable to me.

Is writing the bitcode out into the incremental directory the usual thing to do, as opposed to keeping it in memory until it is embedded?

compiler/rustc_codegen_llvm/src/back/write.rs Outdated Show resolved Hide resolved
compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Do the changes here also enable --emit=thin-llvm-bc? If yes, is it stable or unstable?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we can only emit it under the -Csave-temps flag that I haven't implemented yet.

@@ -508,6 +508,7 @@ impl FromStr for SplitDwarfKind {
pub enum OutputType {
Bitcode,
ThinLinkBitcode,
ThinBitcode,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add doc comments to Bitcode, ThinLinkBitcode and ThinBitcode to explain what the difference between them is...

@DianQK DianQK force-pushed the embed-bitcode-pgo branch 2 times, most recently from 71873c9 to d38c420 Compare January 8, 2025 14:15
@DianQK
Copy link
Member Author

DianQK commented Jan 8, 2025

I've rebased due to conflicts with #130060.

Is writing the bitcode out into the incremental directory the usual thing to do, as opposed to keeping it in memory until it is embedded?

I can put Option<ThinBuffer> in ModuleCodegen, but this might be a significant change. Perhaps I could use Option<Vec<u8>> first?

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 8, 2025
@bors
Copy link
Contributor

bors commented Jan 21, 2025

☔ The latest upstream changes (presumably #135335) made this pull request unmergeable. Please resolve the merge conflicts.

@DianQK DianQK force-pushed the embed-bitcode-pgo branch from 9cb6e23 to 816fc13 Compare February 6, 2025 14:18
@rustbot
Copy link
Collaborator

rustbot commented Feb 6, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@DianQK
Copy link
Member Author

DianQK commented Feb 6, 2025

Sorry for the late update. Pre-linked bitcode no longer needs to be saved to a file now.

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LTO Area: Link-time optimization (LTO) A-run-make Area: port run-make Makefiles to rmake.rs perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Combining LTO + PGO + lib/cdylib crashes on LLVM assertion Failed to compile a project with LTO + PGO
6 participants