Skip to content

Allow enum and union literals to also create SSA values #138759

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

Merged
merged 2 commits into from
Jul 5, 2025

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
Copy link
Collaborator

bors commented Jul 4, 2025

⌛ Testing commit 61b98de with merge 8d5bfde...

@scottmcm
Copy link
Member Author

scottmcm commented Jul 4, 2025

@bors try

@bors
Copy link
Collaborator

bors commented Jul 4, 2025

⌛ Trying commit 61b98de with merge b4976a8...

bors added a commit that referenced this pull request Jul 4, 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 #123886, but that only handled non-simd `struct`s -- 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:
- #142005
- #142103
- #142324
- #142383

---

try-job: aarch64-gnu
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 4, 2025

💔 Test failed - checks-actions

@scottmcm
Copy link
Member Author

scottmcm commented Jul 4, 2025

The failure in bors was

2025-07-04T03:17:44.7553729Z ---- [ui] tests/ui/sanitizer/memory-eager.rs#unoptimized stdout ----
2025-07-04T03:17:44.7553993Z 
2025-07-04T03:17:44.7554643Z error in revision `unoptimized`: error pattern ' Uninitialized value was created by an allocation' not found!
2025-07-04T03:17:44.7555297Z 
2025-07-04T03:17:44.7555502Z error in revision `unoptimized`: error pattern ' in the stack frame' not found!
2025-07-04T03:17:44.7555890Z 
2025-07-04T03:17:44.7556061Z error in revision `unoptimized`: multiple error patterns not found
2025-07-04T03:17:44.7556375Z status: exit status: 0
2025-07-04T03:17:44.7560032Z command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/sanitizer/memory-eager.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--cfg" "unoptimized" "--check-cfg" "cfg(test,FALSE,unoptimized,optimized)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/sanitizer/memory-eager.unoptimized/a" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "-Z" "sanitizer=memory" "-Zsanitizer-memory-track-origins"
2025-07-04T03:17:44.7563649Z stdout: none
2025-07-04T03:17:44.7563813Z stderr: none
2025-07-04T03:17:44.7563914Z 
2025-07-04T03:17:44.7563920Z 
2025-07-04T03:17:44.7563926Z 
2025-07-04T03:17:44.7563991Z failures:
2025-07-04T03:17:44.7564320Z     [ui] tests/ui/sanitizer/memory-eager.rs#unoptimized
2025-07-04T03:17:44.7564531Z 
2025-07-04T03:17:44.7564874Z test result: FAILED. 19070 passed; 1 failed; 271 ignored; 0 measured; 0 filtered out; finished in 609.45s

but those strings are clearly there in the failures in #138759 (comment) when I made it run-pass to see the stderr :/

Let's try running it again with the test left alone and see what happens...
@bors try

@bors
Copy link
Collaborator

bors commented Jul 4, 2025

⌛ Trying commit caeacba with merge aeee77c...

bors added a commit that referenced this pull request Jul 4, 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 #123886, but that only handled non-simd `struct`s -- 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:
- #142005
- #142103
- #142324
- #142383

---

try-job: aarch64-gnu
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 4, 2025

💔 Test failed - checks-actions

@scottmcm
Copy link
Member Author

scottmcm commented Jul 4, 2025

@bors try

bors added a commit that referenced this pull request Jul 4, 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 #123886, but that only handled non-simd `struct`s -- 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:
- #142005
- #142103
- #142324
- #142383

---

try-job: aarch64-gnu
@bors
Copy link
Collaborator

bors commented Jul 4, 2025

⌛ Trying commit d020e38 with merge f966b4d...

@workingjubilee
Copy link
Member

Sorry, I just got distracted by more recent PRs due to the rapidity of the #[align] thing being pushed through.

@bors
Copy link
Collaborator

bors commented Jul 4, 2025

☀️ Try build successful - checks-actions
Build commit: f966b4d (f966b4df254448cc3b864479a88ba650cd19e0f5)

@scottmcm
Copy link
Member Author

scottmcm commented Jul 5, 2025

@bors r=saethlin

@bors
Copy link
Collaborator

bors commented Jul 5, 2025

📌 Commit d020e38 has been approved by saethlin

It is now in the queue for this repository.

@bors bors 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 5, 2025
@bors
Copy link
Collaborator

bors commented Jul 5, 2025

⌛ Testing commit d020e38 with merge 733b47e...

@bors
Copy link
Collaborator

bors commented Jul 5, 2025

☀️ Test successful - checks-actions
Approved by: saethlin
Pushing 733b47e to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 5, 2025
@bors bors merged commit 733b47e into rust-lang:master Jul 5, 2025
12 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jul 5, 2025
@scottmcm scottmcm deleted the operand-builder branch July 5, 2025 04:41
Copy link
Contributor

github-actions bot commented Jul 5, 2025

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing d98a5da (parent) -> 733b47e (this PR)

Test differences

Show 10 test diffs

Stage 1

  • [codegen] tests/codegen/enum/enum-aggregate.rs: [missing] -> pass (J0)
  • [codegen] tests/codegen/union-aggregate.rs: [missing] -> pass (J0)

Stage 2

  • [codegen] tests/codegen/enum/enum-aggregate.rs: [missing] -> pass (J1)
  • [codegen] tests/codegen/union-aggregate.rs: [missing] -> pass (J1)
  • [codegen] tests/codegen/enum/enum-aggregate.rs: [missing] -> ignore (only executed when the pointer width is 64bit) (J2)
  • [codegen] tests/codegen/union-aggregate.rs: [missing] -> ignore (only executed when the pointer width is 64bit) (J2)

Additionally, 4 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 733b47ea4b1b86216f14ef56e49440c33933f230 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. x86_64-apple-2: 3289.8s -> 5589.6s (69.9%)
  2. x86_64-apple-1: 6054.3s -> 9878.0s (63.2%)
  3. i686-gnu-2: 5327.6s -> 6172.8s (15.9%)
  4. x86_64-gnu-tools: 3298.7s -> 3776.3s (14.5%)
  5. pr-check-1: 1570.2s -> 1773.0s (12.9%)
  6. x86_64-gnu-debug: 5257.7s -> 5885.7s (11.9%)
  7. x86_64-rust-for-linux: 2557.5s -> 2857.9s (11.7%)
  8. dist-x86_64-apple: 9374.7s -> 8273.4s (-11.7%)
  9. pr-check-2: 1856.9s -> 2056.4s (10.7%)
  10. aarch64-apple: 4206.6s -> 4637.2s (10.2%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

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-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.

8 participants