Skip to content

Set dead_on_return attribute for indirect arguments #145093

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Aug 8, 2025

Set the dead_on_return attribute (added in LLVM 21) for arguments that are passed indirectly, but not byval.

This indicates that the value of the argument on return does not matter, enabling additional dead store elimination.

From LangRef:

This attribute indicates that the memory pointed to by the argument is dead upon function return, both upon normal return and if the calls unwinds, meaning that the caller will not depend on its contents. Stores that would be observable either on the return path or on the unwind path may be elided.

Specifically, the behavior is as-if any memory written through the pointer during the execution of the function is overwritten with a poison value upon function return. The caller may access the memory, but any load not preceded by a store will return poison.

This attribute does not imply aliasing properties. For pointer arguments that do not alias other memory locations, noalias attribute may be used in conjunction. Conversely, this attribute always implies dead_on_unwind.

This attribute cannot be applied to return values.

This fixes parts of #96497.

@rustbot
Copy link
Collaborator

rustbot commented Aug 8, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
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 A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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 Aug 8, 2025
@nikic
Copy link
Contributor Author

nikic commented Aug 8, 2025

I don't really expect this to have any appreciate impact in practice, but let's check...

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Aug 8, 2025
Set dead_on_return attribute for indirect arguments
@rust-bors
Copy link

rust-bors bot commented Aug 8, 2025

⌛ Trying commit 9c1bbfa with merge 7642ceb

To cancel the try build, run the command @bors try cancel.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 8, 2025
@rust-bors
Copy link

rust-bors bot commented Aug 8, 2025

☀️ Try build successful (CI)
Build commit: 7642ceb (7642cebd8079c4acdbbae34e69cfc8d7855e1086, parent: 2886b36df4a646dd8d82fb65bf0c9d8d96c1f71a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7642ceb): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

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

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (secondary 2.6%)

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

Cycles

Results (secondary 2.9%)

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

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 465.538s -> 465.32s (-0.05%)
Artifact size: 377.38 MiB -> 377.36 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 8, 2025
Comment on lines 9 to 14
pub fn test(mut s: String) {
// CHECK-LABEL: @test
// CHECK: __rust_dealloc
// CHECK-NOT: store
s = String::new();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This test passing indicates that this fixes (part of) #96497, specifically #96497 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, I wasn't aware we had an issue for this.

@nnethercote
Copy link
Contributor

r=me. The only question is does this fix all of #96497, or just the comment that @erikdesjardins linked to?

@nnethercote nnethercote 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 Aug 8, 2025
Set the dead_on_return attribute (added in LLVM 21) for arguments
that are passed indirectly, but not byval.

This indicates that the value of the argument on return does not
matter, enabling additional dead store elimination.
@nikic
Copy link
Contributor Author

nikic commented Aug 11, 2025

r=me. The only question is does this fix all of #96497, or just the comment that @erikdesjardins linked to?

I've added two more cases from the issue as tests. One case that's not fixed is the Vec::fill one (https://rust.godbolt.org/z/zPj3nrfbh). So I've adjusted the PR description to not close the issue yet.

@nnethercote
Copy link
Contributor

Great, thank you. r=me, merge when you are ready.

@nikic
Copy link
Contributor Author

nikic commented Aug 11, 2025

@bors r=nnethercote

@bors
Copy link
Collaborator

bors commented Aug 11, 2025

📌 Commit ebef9d7 has been approved by nnethercote

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 Aug 11, 2025
@bors
Copy link
Collaborator

bors commented Aug 12, 2025

⌛ Testing commit ebef9d7 with merge 21f6a33...

bors added a commit that referenced this pull request Aug 12, 2025
Set dead_on_return attribute for indirect arguments

Set the dead_on_return attribute (added in LLVM 21) for arguments that are passed indirectly, but not byval.

This indicates that the value of the argument on return does not matter, enabling additional dead store elimination.

From LangRef:

> This attribute indicates that the memory pointed to by the argument is dead upon function return, both upon normal return and if the calls unwinds, meaning that the caller will not depend on its contents. Stores that would be observable either on the return path or on the unwind path may be elided.
>
> Specifically, the behavior is as-if any memory written through the pointer during the execution of the function is overwritten with a poison value upon function return. The caller may access the memory, but any load not preceded by a store will return poison.
>
> This attribute does not imply aliasing properties. For pointer arguments that do not alias other memory locations, noalias attribute may be used in conjunction. Conversely, this attribute always implies dead_on_unwind.
>
> This attribute cannot be applied to return values.

This fixes parts of #96497.
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-apple-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Launching test 'f32 small integer values'
Launching test 'f64 small integer values'
Launching test 'f32 subnormal'
Launching test 'f64 subnormal'
Command `/Users/runner/work/rust/rust/build/x86_64-apple-darwin/stage0/bin/cargo run --target x86_64-apple-darwin -Zbinary-dep-depinfo -j 4 -Zroot-dir=/Users/runner/work/rust/rust --locked --color always --release --manifest-path /Users/runner/work/rust/rust/src/tools/test-float-parse/Cargo.toml -- --skip-huge [workdir=/Users/runner/work/rust/rust]` failed with exit code 1
Created at: src/bootstrap/src/core/builder/cargo.rs:423:33
Executed at: src/bootstrap/src/core/build_steps/test.rs:3763:30

Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 1:54:14
  local time: Tue Aug 12 08:25:47 UTC 2025
  network time: Tue, 12 Aug 2025 08:25:47 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@bors
Copy link
Collaborator

bors commented Aug 12, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 12, 2025
@nikic
Copy link
Contributor Author

nikic commented Aug 12, 2025

@bors retry Not super clear what actually failed there, but it looks spurious.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2025
@bors
Copy link
Collaborator

bors commented Aug 12, 2025

⌛ Testing commit ebef9d7 with merge fec132b...

bors added a commit that referenced this pull request Aug 12, 2025
Set dead_on_return attribute for indirect arguments

Set the dead_on_return attribute (added in LLVM 21) for arguments that are passed indirectly, but not byval.

This indicates that the value of the argument on return does not matter, enabling additional dead store elimination.

From LangRef:

> This attribute indicates that the memory pointed to by the argument is dead upon function return, both upon normal return and if the calls unwinds, meaning that the caller will not depend on its contents. Stores that would be observable either on the return path or on the unwind path may be elided.
>
> Specifically, the behavior is as-if any memory written through the pointer during the execution of the function is overwritten with a poison value upon function return. The caller may access the memory, but any load not preceded by a store will return poison.
>
> This attribute does not imply aliasing properties. For pointer arguments that do not alias other memory locations, noalias attribute may be used in conjunction. Conversely, this attribute always implies dead_on_unwind.
>
> This attribute cannot be applied to return values.

This fixes parts of #96497.
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-msvc-ext1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Verifying foo v0.0.1 (C:\a\rust\rust\build\x86_64-pc-windows-msvc\stage1-tools\x86_64-pc-windows-msvc\tmp\cit\t2372\foo)
 Downloading crates ...
  Downloaded bar v1.0.0 (registry `dummy-registry`)
   Compiling bar v1.0.0
   Compiling foo v0.0.1 (C:\a\rust\rust\build\x86_64-pc-windows-msvc\stage1-tools\x86_64-pc-windows-msvc\tmp\cit\t2372\foo\target\package\foo-0.0.1)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.69s

stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: cargo_test_support::panic_error::pe
   3: <cargo_test_support::Execs>::run
   4: testsuite::package::reserved_windows_name
   5: <<std::hash::random::RandomState>::new::KEYS::{closure#1} as core::ops::function::FnOnce<(core::option::Option<&mut core::option::Option<core::cell::Cell<(u64, u64)>>>,)>>::call_once
   6: test::run_test
   7: test::run_test
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


@bors
Copy link
Collaborator

bors commented Aug 12, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 12, 2025
@nikic
Copy link
Contributor Author

nikic commented Aug 12, 2025

@bors retry Same reserved_windows_name failure also happened on #145161 recently.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2025
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.

7 participants