Skip to content

Avoid unnecessary LLVM passes when thin LTO buffer is null - #166084

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
nnethercote:unnecessary-llvm-passes
Nov 18, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
nnethercote:unnecessary-llvm-passes

Conversation

@nnethercote

@nnethercote nnethercote commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

LLVMRustOptimize runs three passes when NeedThinLTOBufferPasses is true: CanonicalizeAliasesPass, NameAnonGlobalPass and AssignGUIDPass. These passes are only needed when the module's bitcode or summary will be written.

This commit updates how NeedThinLTOBufferPasses is initialized in LLVMRustOptimize; instead of being initialized as always true, it's now only true if a thin LTO buffer is provided. (Subsequent existing code in the function may change NeedThinLTOBufferPasses to false.) Avoiding the unnecessary passes improves compile times a little, e.g. on debug builds (which are O0 + PreLinkNoLTO).

That requires a change to the passes added by LLVMRustModuleSerialize, because it currently relies on LLVMRustOptimize having run CanonicalizeAliasesPass and NameAnonGlobalPass, and this is no longer true in some cases such as --emit=llvm-bc. The passes are idempotent so it doesn't hurt if they are run twice in other cases.

Non-LTO --emit=llvm-ir,asm output changes in two ways: unnamed globals stay unnamed (or are named __unnamed by LLVM) instead of being named anon (this requires updating some tests) and !guid metadata disappears.

r? @nikic

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 17, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

@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 17, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 17, 2026
Avoid unnecessary LLVM passes when thin LTO buffer is null
@rust-bors

rust-bors Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 7fa4664 (7fa4664be5888ec23fca3c044a78def597144e89)
Base parent: 5df8c02 (5df8c02afdf6b882bc301442cea70f026e3d3974)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (7fa4664): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.4% [-0.6%, -0.3%] 23
Improvements ✅
(secondary)
-1.0% [-4.4%, -0.3%] 19
All ❌✅ (primary) -0.4% [-0.6%, -0.3%] 23

Max RSS (memory usage)

Results (secondary -2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.4%, 0.8%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.6% [-5.0%, -4.1%] 4
All ❌✅ (primary) - - 0

Cycles

Results (primary 2.2%, secondary -2.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
1.6% [0.4%, 3.4%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.2% [-10.2%, -0.6%] 7
All ❌✅ (primary) 2.2% [2.2%, 2.2%] 1

Binary size

Results (primary -0.6%, secondary -1.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.6% [-2.8%, -0.1%] 67
Improvements ✅
(secondary)
-1.2% [-7.9%, -0.1%] 35
All ❌✅ (primary) -0.6% [-2.8%, -0.1%] 67

Bootstrap: 467.993s -> 465.25s (-0.59%)
Artifact size: 406.20 MiB -> 406.24 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 17, 2026
@rust-bors

rust-bors Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@rust-timer: 🔑 Insufficient privileges: not in review users

@nnethercote

Copy link
Copy Markdown
Contributor Author

cc @bjorn3 @dianqk @cuviper

@nnethercote
nnethercote marked this pull request as ready for review November 17, 2026 05:25
@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 Nov 17, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member

Insufficient privileges: not in review users

This is my mistake oops, fixed in rust-lang/bors#892

@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors rollup=never rustc-perf

@nnethercote

Copy link
Copy Markdown
Contributor Author

coercions has the strongest signal, and notably the cycles and wall-time (and max-rss) improvements are larger than the instruction count improvements.

@nikic nikic left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

`LLVMRustOptimize` runs three passes when `NeedThinLTOBufferPasses` is
true: `CanonicalizeAliasesPass`, `NameAnonGlobalPass` and
`AssignGUIDPass`. These passes are only needed when the module's bitcode
or summary will be written.

This commit updates how `NeedThinLTOBufferPasses` is initialized in
`LLVMRustOptimize`; instead of being initialized as always true, it's
now only true if a thin LTO buffer is provided. (Subsequent existing
code in the function may change `NeedThinLTOBufferPasses` to false.)
Avoiding the unnecessary passes improves compile times a little, e.g. on
debug builds (which are `O0` + `PreLinkNoLTO`).

That requires a change to the passes added by `LLVMRustModuleSerialize`,
because it currently relies on `LLVMRustOptimize` having run
`CanonicalizeAliasesPass` and `NameAnonGlobalPass`, and this is no
longer true in some cases such as `--emit=llvm-bc`. The passes are
idempotent so it doesn't hurt if they are run twice in other cases.

Non-LTO `--emit=llvm-ir,asm` output changes in two ways: unnamed globals
stay unnamed (or are named `__unnamed` by LLVM) instead of being named
`anon` (this requires updating some tests) and `!guid` metadata
disappears.
@nnethercote
nnethercote force-pushed the unnecessary-llvm-passes branch from 08946c8 to ce8a6c8 Compare November 18, 2026 00:38
@nnethercote

Copy link
Copy Markdown
Contributor Author

I addressed the single nit.

@bors r=nikic

@rust-bors

rust-bors Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ce8a6c8 has been tentatively approved by nikic

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member

Creating a perf-sensitive rollup
@bors rollup=iffy

rust-bors Bot pushed a commit that referenced this pull request Nov 18, 2026
…uwer

Rollup of 4 perf-sensitive pull requests

Successful merges:

 - #165396 (Perf - Separate flag computation & intern functions per `TyKind`)
 - #165774 (Auto trait finder: Avoid recomputing `ParamEnv` if nothing changed)
 - #165863 (Avoid instantiating `FnSig` when not necessary)
 - #166084 (Avoid unnecessary LLVM passes when thin LTO buffer is null)
@rust-bors
rust-bors Bot merged commit ca637b3 into rust-lang:main Nov 18, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 18, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 18, 2026
Rollup merge of #166084 - nnethercote:unnecessary-llvm-passes, r=nikic

Avoid unnecessary LLVM passes when thin LTO buffer is null

`LLVMRustOptimize` runs three passes when `NeedThinLTOBufferPasses` is true: `CanonicalizeAliasesPass`, `NameAnonGlobalPass` and `AssignGUIDPass`. These passes are only needed when the module's bitcode or summary will be written.

This commit updates how `NeedThinLTOBufferPasses` is initialized in `LLVMRustOptimize`; instead of being initialized as always true, it's now only true if a thin LTO buffer is provided. (Subsequent existing code in the function may change `NeedThinLTOBufferPasses` to false.) Avoiding the unnecessary passes improves compile times a little, e.g. on debug builds (which are `O0` + `PreLinkNoLTO`).

That requires a change to the passes added by `LLVMRustModuleSerialize`, because it currently relies on `LLVMRustOptimize` having run `CanonicalizeAliasesPass` and `NameAnonGlobalPass`, and this is no longer true in some cases such as `--emit=llvm-bc`. The passes are idempotent so it doesn't hurt if they are run twice in other cases.

Non-LTO `--emit=llvm-ir,asm` output changes in two ways: unnamed globals stay unnamed (or are named `__unnamed` by LLVM) instead of being named `anon` (this requires updating some tests) and `!guid` metadata disappears.

r? @nikic
@nnethercote
nnethercote deleted the unnecessary-llvm-passes branch November 18, 2026 23:15
@rust-timer

Copy link
Copy Markdown

Note

This PR was benchmarked as part of triage of its containing rollup: triage URL.

Finished benchmarking commit (db947ef): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-0.7%, -0.2%] 27
Improvements ✅
(secondary)
-1.0% [-4.6%, -0.2%] 19
All ❌✅ (primary) -0.5% [-0.7%, -0.2%] 27

Max RSS (memory usage)

Results (primary -1.2%, secondary -2.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.2% [-1.2%, -1.2%] 1
Improvements ✅
(secondary)
-2.5% [-4.8%, -0.5%] 8
All ❌✅ (primary) -1.2% [-1.2%, -1.2%] 1

Cycles

Results (secondary -5.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.4% [0.4%, 0.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.6% [-10.5%, -2.4%] 5
All ❌✅ (primary) - - 0

Binary size

Results (primary -1.0%, secondary -1.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.0% [-2.8%, -0.3%] 67
Improvements ✅
(secondary)
-1.7% [-9.0%, -0.2%] 35
All ❌✅ (primary) -1.0% [-2.8%, -0.3%] 67

Artifact size: 405.23 MiB -> 405.24 MiB (0.00%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

5 participants