Skip to content

Run the FatLtoCleanup pass to lower llvm.type.checked.load - #165572

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
folkertdev:fat-lto-cleanup
Nov 27, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
folkertdev:fat-lto-cleanup

Conversation

@folkertdev

Copy link
Copy Markdown
Contributor

tracking issue: #68262

fixes #124093

Run the FatLtoCleanup pass which lowers llvm.type.checked.load into a standard load when doing a non-LTO compilation. Previously using -Zvirtual-function-elimination on godbolt would in many cases trigger an error like

error: example.rs:21:5: in function _RNvCsjRjZ17mMryE_7example4call i32 (ptr, ptr): llvm.type.checked.load intrinsic must be lowered by the LowerTypeTests pass before code generation

Because there --emit=asm is passed to rustc and that is a non-LTO compilation where checked loads are not supported.


LLVM uses this pass here as a part of PassBuilder::buildFatLTODefaultPipeline

https://github.com/llvm/llvm-project/blob/5da6b3c590792a069c40d8982c8b0fb3557f241a/llvm/lib/Passes/PassBuilderPipelines.cpp#L1827-L1837

We don't really have an equivalent to that I think, so I just inserted it right before we start doing instruction selection (but after bitcode/llvm ir has been written), which appears to work.

@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 6, 2026
@folkertdev
folkertdev force-pushed the fat-lto-cleanup branch 2 times, most recently from f68d0fb to a4ec404 Compare November 7, 2026 12:21

@folkertdev folkertdev left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

r? nikic (or someone else familiar with the LLVM side)
cc @bjorn3 (because LTO)

View changes since this review

Comment on lines +689 to +690
ModuleAnalysisManager MAM;
FatLtoCleanup().run(*unwrap(M), MAM);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is some fancier pass manager thing needed/desirable here?

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.

The pass doesn't actually use any analyses, so this is fine.

// Rustc emits checked loads when using `-Zvirtual-function-elimination`.
// This flag implies fat LTO, but for non-LTO compilations (e.g. building
// an rlib, or when passing --emit=asm) the checked loads are not supported.
unsafe { llvm::LLVMRustRunFatLtoCleanup(llmod) };

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the only location that seems to work. Putting it earlier removes the checked loads from the bitcode (or from the IR, making .bc and .ll inconsistent) defeating the purpose, doing it later makes non-LTO compilations hit the error this PR is supposed to fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this be done at the end of lto::run_pass_manager? That would make sure it doesn't get run before emitting pre-LTO bitcode, but does get run before emitting post-LTO bitcode.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think that doesn't actually run for non-LTO compilations, like creating an rlib here, going off of the test I added here failing when moving the logic there, and this function:

match sess_lto {
Lto::ThinLocal if !linker_does_lto => ComputedLtoType::Thin,
Lto::Thin if !linker_does_lto && !is_rlib => ComputedLtoType::Thin,
Lto::Fat if !is_rlib => ComputedLtoType::Fat,
_ => ComputedLtoType::No,
}

I think we want the rlib case to work?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right, missed that this PR is about non-LTO builds.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's confusing because we only actually need to run this pass if -Clto=fat is passed, but then internally that doesn't mean that the fat LTO logic actually runs.

// This flag implies fat LTO, but for non-LTO compilations (e.g. building
// an rlib, or when passing --emit=asm) the checked loads are not supported,
// and they must be removed.
extern "C" void LLVMRustRunFatLtoCleanup(LLVMModuleRef M) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I previously gated this on whether the Virtual Function Elim module flag was set, but the FatLtoCleanup pass has a very cheap check for whether it should do any work at all (here), so I don't think that is necessary.

@folkertdev
folkertdev marked this pull request as ready for review November 7, 2026 12:31
@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 7, 2026
@folkertdev folkertdev changed the title Run the FatLtoCleanup pass when using virtual function elimination Run the FatLtoCleanup pass to lower llvm.type.checked.load Nov 7, 2026
@rustbot

This comment has been minimized.

@nikic

nikic commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@bors r+

I feel like it would make more sense for LLVM to just do this itself in the backend, rather than requiring a separate pass...

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

❌ Cannot approve commit 4fea718, because CI currently fails on this PR. Use @bors r+ force to override the PR CI check.

This LLVM pass lowers `llvm.type.checked.load` into a standard load when
doing a non-LTO compilation.
@rustbot

rustbot commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@folkertdev

Copy link
Copy Markdown
Contributor Author

Looks like CI just got stuck before, but it's passing now

@bors r=nikic

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4484ce5 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 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 26, 2026
Run the `FatLtoCleanup` pass to lower `llvm.type.checked.load`

tracking issue: rust-lang#68262

fixes rust-lang#124093

Run the `FatLtoCleanup` pass which lowers `llvm.type.checked.load` into a standard load when doing a non-LTO compilation. Previously using `-Zvirtual-function-elimination` on godbolt would in many cases trigger an error like

```
error: example.rs:21:5: in function _RNvCsjRjZ17mMryE_7example4call i32 (ptr, ptr): llvm.type.checked.load intrinsic must be lowered by the LowerTypeTests pass before code generation
```

Because there `--emit=asm` is passed to `rustc` and that is a non-LTO compilation where checked loads are not supported.

---

LLVM uses this pass here as a part of `PassBuilder::buildFatLTODefaultPipeline`

https://github.com/llvm/llvm-project/blob/5da6b3c590792a069c40d8982c8b0fb3557f241a/llvm/lib/Passes/PassBuilderPipelines.cpp#L1827-L1837

We don't really have an equivalent to that I think, so I just inserted it right before we start doing instruction selection (but after bitcode/llvm ir has been written), which appears  to work.
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 17 pull requests

Successful merges:

 - #165572 (Run the `FatLtoCleanup` pass to lower `llvm.type.checked.load`)
 - #166235 (Keep opening '(' when suggesting to remove '&' from pattern)
 - #166516 (Fix ICE in iterator method suggestion when the receiver is a block)
 - #166586 (abby test dsl: one binder to rule them all)
 - #166604 (fix: embed-source-dwarf test with remapped debuginfo)
 - #166620 (Change wording of overwrite span in `unused_assignments` lint)
 - #164289 (Add regression test for self-referential GAT with missing lifetime argument)
 - #165859 (Add codegen test for MaybeUninit array built in the return slot)
 - #166092 (add fake borrow tests for divergent match guards)
 - #166292 (Don't suggest closure braces for errors inside the body)
 - #166435 (Encode direct const mir)
 - #166447 (Document allocator behaviour of BTreeMap::append and BTreeSet::append)
 - #166558 (triagebot: Send "in progress" message when nomination is in progress pt. 2)
 - #166584 (Arg pos const mismatch ice)
 - #166599 (add alias-bound test)
 - #166606 (llvm-wrapper: Pass LibraryOptionsContext to computeDeadSymbolsWithConstProp etc.)
 - #166623 (Correct feature name for `is_cased()` method)

Failed merges:

 - #166583 (abby dsl tests: the passing test should check-pass)
rust-bors Bot pushed a commit that referenced this pull request Nov 27, 2026
Rollup of 20 pull requests

Successful merges:

 - #164161 (`-Znext-solver` Support a bit more types for rustdoc auto traits)
 - #165572 (Run the `FatLtoCleanup` pass to lower `llvm.type.checked.load`)
 - #166235 (Keep opening '(' when suggesting to remove '&' from pattern)
 - #166335 (A few fixes in x86 std_detect)
 - #166516 (Fix ICE in iterator method suggestion when the receiver is a block)
 - #166586 (abby test dsl: one binder to rule them all)
 - #166604 (fix: embed-source-dwarf test with remapped debuginfo)
 - #166620 (Change wording of overwrite span in `unused_assignments` lint)
 - #166626 (Remove some outdated comments about None terminator)
 - #164289 (Add regression test for self-referential GAT with missing lifetime argument)
 - #165859 (Add codegen test for MaybeUninit array built in the return slot)
 - #166092 (add fake borrow tests for divergent match guards)
 - #166292 (Don't suggest closure braces for errors inside the body)
 - #166435 (Encode direct const mir)
 - #166447 (Document allocator behaviour of BTreeMap::append and BTreeSet::append)
 - #166558 (triagebot: Send "in progress" message when nomination is in progress pt. 2)
 - #166584 (Arg pos const mismatch ice)
 - #166599 (add alias-bound test)
 - #166606 (llvm-wrapper: Pass LibraryOptionsContext to computeDeadSymbolsWithConstProp etc.)
 - #166623 (Correct feature name for `is_cased()` method)

Failed merges:

 - #166583 (abby dsl tests: the passing test should check-pass)
@rust-bors
rust-bors Bot merged commit 90b5fdc into rust-lang:main Nov 27, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 27, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 27, 2026
Rollup merge of #165572 - folkertdev:fat-lto-cleanup, r=nikic

Run the `FatLtoCleanup` pass to lower `llvm.type.checked.load`

tracking issue: #68262

fixes #124093

Run the `FatLtoCleanup` pass which lowers `llvm.type.checked.load` into a standard load when doing a non-LTO compilation. Previously using `-Zvirtual-function-elimination` on godbolt would in many cases trigger an error like

```
error: example.rs:21:5: in function _RNvCsjRjZ17mMryE_7example4call i32 (ptr, ptr): llvm.type.checked.load intrinsic must be lowered by the LowerTypeTests pass before code generation
```

Because there `--emit=asm` is passed to `rustc` and that is a non-LTO compilation where checked loads are not supported.

---

LLVM uses this pass here as a part of `PassBuilder::buildFatLTODefaultPipeline`

https://github.com/llvm/llvm-project/blob/5da6b3c590792a069c40d8982c8b0fb3557f241a/llvm/lib/Passes/PassBuilderPipelines.cpp#L1827-L1837

We don't really have an equivalent to that I think, so I just inserted it right before we start doing instruction selection (but after bitcode/llvm ir has been written), which appears  to work.
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.

virtual function elimination: llvm error: Do not know how to promote this operator

4 participants