Skip to content

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Mar 20, 2025

Today, Some(x) always goes through an alloca, even in trivial cases where the niching means the constructor doesn't even change the value.

For example, https://rust.godbolt.org/z/6KG6PqoYz

pub fn demo(r: &i32) -> Option<&i32> {
    Some(r)
}

currently emits the IR

define align 4 ptr @demo(ptr align 4 %r) unnamed_addr {
start:
  %_0 = alloca [8 x i8], align 8
  store ptr %r, ptr %_0, align 8
  %0 = load ptr, ptr %_0, align 8
  ret ptr %0
}

but with this PR it becomes just

define align 4 ptr @demo(ptr align 4 %r) unnamed_addr {
start:
  ret ptr %r
}

(Of course the optimizer can clean that up, but it'd be nice if it didn't have to -- especially in debug where it doesn't run. This is like #123886, but that only handled non-simd structs -- this PR generalizes it to all non-simd ADTs.)

Doing this means handing variants other than FIRST_VARIANT, handling the active field for unions, refactoring the discriminant code so the Place and Operand parts can share the calculation, etc.

Other PRs that led up to this one:


try-job: aarch64-gnu

@rustbot
Copy link
Collaborator

rustbot commented Mar 20, 2025

r? @Nadrieril

rustbot has assigned @Nadrieril.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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 Mar 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 20, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 20, 2025

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@saethlin
Copy link
Member

r? codegen

@rustbot rustbot assigned saethlin and unassigned Nadrieril Mar 20, 2025
@saethlin
Copy link
Member

Spiderman meme

@scottmcm
Copy link
Member 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 Mar 20, 2025
@bors
Copy link
Collaborator

bors commented Mar 20, 2025

⌛ Trying commit aa8ceb8 with merge 875f416...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 20, 2025
Allow `enum` and `union` literals to also create SSA values

Today, `Some(x)` always goes through an `alloca`, even in trivial cases where the niching means the constructor doesn't even change the value.

For example, <https://rust.godbolt.org/z/6KG6PqoYz>
```rust
pub fn demo(r: &i32) -> Option<&i32> {
    Some(r)
}
```
currently emits the IR
```llvm
define align 4 ptr `@demo(ptr` align 4 %r) unnamed_addr {
start:
  %_0 = alloca [8 x i8], align 8
  store ptr %r, ptr %_0, align 8
  %0 = load ptr, ptr %_0, align 8
  ret ptr %0
}
```
but with this PR it becomes just
```llvm
define align 4 ptr `@demo(ptr` align 4 %r) unnamed_addr {
start:
  ret ptr %r
}
```
(Of course the optimizer can clean that up, but it'd be nice if it didn't have to -- especially in debug where it doesn't run.  This is like rust-lang#123886, but that only handled non-simd `struct`s -- this PR generalizes it to all non-simd ADTs.)

There's two commits you can review independently:
1. The first is simplifying how the aggregate handling works.  Past-me wrote something overly complicated, needing arrayvecs and zipping, depending on a careful iteration order of the fields, and fragile enough that even for just structs it needed extra double-checks to make sure it even made the right variant.  It's replaced with something far more direct that works just like `extract_field`: use the offset to put it in exactly the correct immediate in the `OperandValue`.  This doesn't support anything new, just refactors -- including moving some things off `FunctionCx` that had no reason to be there.  (I have no idea why my past self put them there.)
2. The second extends that work to support more ADTs.  That means handing variants other than `FIRST_VARIANT`, handling the active field for unions, refactoring the discriminant code so the Place and Operand parts can share the calculation, etc.
@bors
Copy link
Collaborator

bors commented Mar 20, 2025

☀️ Try build successful - checks-actions
Build commit: 875f416 (875f416b9aa583f09153eac5b56d36fcd932b274)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (875f416): 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.0% [0.0%, 0.0%] 1
Regressions ❌
(secondary)
0.2% [0.2%, 0.3%] 3
Improvements ✅
(primary)
-0.3% [-0.5%, -0.2%] 7
Improvements ✅
(secondary)
-0.3% [-0.4%, -0.3%] 2
All ❌✅ (primary) -0.3% [-0.5%, 0.0%] 8

Max RSS (memory usage)

Results (primary -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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.3% [-2.3%, -2.3%] 1

Cycles

Results (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
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-2.3%, -1.6%] 8
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.1%, secondary -0.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
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 57
Improvements ✅
(secondary)
-0.1% [-0.3%, -0.0%] 27
All ❌✅ (primary) -0.1% [-0.2%, -0.0%] 57

Bootstrap: 775.297s -> 774.424s (-0.11%)
Artifact size: 365.52 MiB -> 365.52 MiB (0.00%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 23, 2025

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member Author

Ben said I should re-roll this
r? codegen

@workingjubilee
Copy link
Member

hello.

bors added a commit that referenced this pull request Jul 5, 2025
Let `rvalue_creates_operand` return true for *all* `Rvalue::Aggregate`s

Draft for now because it's built on Ralf's #143291

Inspired by #138759 (comment) where I noticed that we were nearly at this point, plus the comments I was writing in #143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
scottmcm added a commit to scottmcm/rust that referenced this pull request Jul 6, 2025
Inspired by <rust-lang#138759 (comment)> where I noticed that we were nearly at this point, plus the comments I was writing in 143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
scottmcm added a commit to scottmcm/rust that referenced this pull request Jul 6, 2025
Inspired by <rust-lang#138759 (comment)> where I noticed that we were nearly at this point, plus the comments I was writing in 143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
scottmcm added a commit to scottmcm/rust that referenced this pull request Jul 8, 2025
Inspired by <rust-lang#138759 (comment)> where I noticed that we were nearly at this point, plus the comments I was writing in 143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
bors added a commit that referenced this pull request Jul 9, 2025
Let `rvalue_creates_operand` return true for *all* `Rvalue::Aggregate`s

~~Draft for now because it's built on Ralf's #143291~~

Inspired by #138759 (comment) where I noticed that we were nearly at this point, plus the comments I was writing in #143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
github-actions bot pushed a commit to devnexen/miri that referenced this pull request Jul 10, 2025
Let `rvalue_creates_operand` return true for *all* `Rvalue::Aggregate`s

~~Draft for now because it's built on Ralf's rust-lang/rust#143291~~

Inspired by rust-lang/rust#138759 (comment) where I noticed that we were nearly at this point, plus the comments I was writing in rust-lang/rust#143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
LorrensP-2158466 pushed a commit to LorrensP-2158466/rust that referenced this pull request Jul 13, 2025
Inspired by <rust-lang#138759 (comment)> where I noticed that we were nearly at this point, plus the comments I was writing in 143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Jul 15, 2025
Let `rvalue_creates_operand` return true for *all* `Rvalue::Aggregate`s

~~Draft for now because it's built on Ralf's rust-lang/rust#143291~~

Inspired by rust-lang/rust#138759 (comment) where I noticed that we were nearly at this point, plus the comments I was writing in rust-lang/rust#143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
FractalFir pushed a commit to FractalFir/rust that referenced this pull request Jul 24, 2025
Inspired by <rust-lang#138759 (comment)> where I noticed that we were nearly at this point, plus the comments I was writing in 143410 that reminded me a type-dependent `true` is fine.

This PR splits the `OperandRef::builder` logic out to a separate type, with the updates needed to handle SIMD as well.  In doing so, that makes the existing `Aggregate` path in `codegen_rvalue_operand` capable of handing SIMD values just fine.

As a result, we no longer need to do layout calculations for aggregate result types when running the analysis to determine which things can be SSA in codegen.
@saethlin
Copy link
Member

saethlin commented Oct 9, 2025

@craterbot run mode=build-and-test start=master#d98a5da813da67eb189387b8ccfb73cf481275d8+rustflags=-Copt-level=3 end=master#733b47ea4b1b86216f14ef56e49440c33933f230+rustflags=-Copt-level=3

@craterbot
Copy link
Collaborator

👌 Experiment pr-138759 created and queued.
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 9, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-138759 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Zalathar added a commit to Zalathar/rust that referenced this pull request Oct 12, 2025
…ottmcm

Change int-to-ptr transmute lowering back to inttoptr

This is a revert of rust-lang#121282, but with a regression test to cover the reported miscompile in rust-lang#147265 that was caused by the way the code here combines with rust-lang#138759.
bors added a commit that referenced this pull request Oct 13, 2025
Change int-to-ptr transmute lowering back to inttoptr

This is a revert of #121282, but with a regression test to cover the reported miscompile in #147265 that was caused by the way the code here combines with #138759.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Oct 14, 2025
Change int-to-ptr transmute lowering back to inttoptr

This is a revert of rust-lang/rust#121282, but with a regression test to cover the reported miscompile in rust-lang/rust#147265 that was caused by the way the code here combines with rust-lang/rust#138759.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Oct 15, 2025
Change int-to-ptr transmute lowering back to inttoptr

This is a revert of rust-lang/rust#121282, but with a regression test to cover the reported miscompile in rust-lang/rust#147265 that was caused by the way the code here combines with rust-lang/rust#138759.
@craterbot
Copy link
Collaborator

🎉 Experiment pr-138759 is completed!
📊 272 regressed and 231 fixed (713837 total)
📊 6273 spurious results on the retry-regessed-list.txt, consider a retry1 if this is a significant amount.
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Footnotes

  1. re-run the experiment with crates=https://crater-reports.s3.amazonaws.com/pr-138759/retry-regressed-list.txt

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Oct 15, 2025
@saethlin
Copy link
Member

@craterbot run mode=build-and-test start=master#d98a5da813da67eb189387b8ccfb73cf481275d8+rustflags=-Copt-level=3 end=master#733b47ea4b1b86216f14ef56e49440c33933f230+rustflags=-Copt-level=3 crates=https://crater-reports.s3.amazonaws.com/pr-138759/retry-regressed-list.txt name=pr-138759-retry p=1

@craterbot
Copy link
Collaborator

👌 Experiment pr-138759-retry created and queued.
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-codegen Area: Code generation merged-by-bors This PR was explicitly merged by bors. PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-crater Status: Waiting on a crater run to be completed. 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.

9 participants