Skip to content

Remove redundant drop terminators ahead of mir_coroutine_witnesses#155022

Draft
tmandry wants to merge 5 commits intorust-lang:mainfrom
tmandry:remove-dead-drops
Draft

Remove redundant drop terminators ahead of mir_coroutine_witnesses#155022
tmandry wants to merge 5 commits intorust-lang:mainfrom
tmandry:remove-dead-drops

Conversation

@tmandry
Copy link
Copy Markdown
Member

@tmandry tmandry commented Apr 9, 2026

This change is meant to fix #151942. Putting this up to see the perf effects of running it on every MIR body; then I'll work out a strategy to merge it.

Related issues to triage:


Coroutine traits like Send and Sync are computed based on the mir_coroutine_witnesses query to get the types saved in the coroutine. This query runs the regular coroutine analysis pass on mir_promoted to compute which locals are saved across suspension points.

At this stage of MIR, drop terminators exist along every exit path of the function, even when a local has already been moved from. Those extra drop terminators causes locals (like guard in the case below) to appear live over suspension points, even when they have just been dropped.

async fn bar(mutex: &Mutex<()>) {
    let mut guard = mutex.lock().unwrap();
    loop {
        drop(guard);
        foo().await;
        guard = mutex.lock().unwrap();
    }
}

See this gist for MIR of a simplified version of this function.

Later MIR passes like drop_elaboration and remove_unneeded_drops remove the redundant drop terminators, but they depend on type info to check needs_drop on a place's type. Since mir_coroutine_witnesses query is run inside typeck, we cannot depend on this without introducing cycles.

Instead, we implement a simple pass that removes drop terminators for definitely-uninitialized places. It avoids the need for type info by skipping the check for whether types have destructors.

r? @ghost

@rustbot rustbot added 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 Apr 9, 2026
@tmandry tmandry changed the title Remove dead drops Remove redundant drop terminators ahead of mir_coroutine_witnesses Apr 9, 2026
@tmandry
Copy link
Copy Markdown
Member Author

tmandry commented Apr 9, 2026

@bors try
@rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 9, 2026
Remove redundant drop terminators ahead of mir_coroutine_witnesses
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 9, 2026
@rust-log-analyzer

This comment was marked as off-topic.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 9, 2026

☀️ Try build successful (CI)
Build commit: 5cfde26 (5cfde2679121005039f5e66aeb50f0105493903e, parent: 900485642855f4729d926ecf24680a791f9293cf)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (5cfde26): comparison URL.

Overall result: ❌ regressions - please read:

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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never
@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)
1.1% [0.2%, 5.1%] 55
Regressions ❌
(secondary)
1.5% [0.1%, 5.6%] 96
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.1% [0.2%, 5.1%] 55

Max RSS (memory usage)

Results (primary 6.7%, secondary 5.1%)

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

mean range count
Regressions ❌
(primary)
6.7% [0.5%, 14.6%] 66
Regressions ❌
(secondary)
5.1% [1.2%, 11.8%] 46
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 6.7% [0.5%, 14.6%] 66

Cycles

Results (primary 2.9%, secondary 2.8%)

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

mean range count
Regressions ❌
(primary)
2.9% [2.1%, 5.5%] 13
Regressions ❌
(secondary)
3.1% [1.3%, 4.9%] 16
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.3% [-2.3%, -2.3%] 1
All ❌✅ (primary) 2.9% [2.1%, 5.5%] 13

Binary size

Results (primary 0.1%, secondary -0.6%)

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

mean range count
Regressions ❌
(primary)
0.1% [0.1%, 0.1%] 5
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.6% [-1.1%, -0.0%] 12
All ❌✅ (primary) 0.1% [0.1%, 0.1%] 5

Bootstrap: 489.506s -> 497.499s (1.63%)
Artifact size: 395.50 MiB -> 395.57 MiB (0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 9, 2026
@rust-log-analyzer

This comment was marked as off-topic.

@tmandry tmandry force-pushed the remove-dead-drops branch from 4d04916 to 11e9fef Compare April 9, 2026 06:13
@tmandry
Copy link
Copy Markdown
Member Author

tmandry commented Apr 9, 2026

@bors try
@rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 9, 2026
Remove redundant drop terminators ahead of mir_coroutine_witnesses
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 9, 2026
@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
fmt: checked 6784 files
tidy check
tidy [rustdoc_json (src)]: `rustdoc-json-types` modified, checking format version
tidy: Skipping binary file check, read-only filesystem
tidy [style (tests)]: /checkout/tests/ui/coroutine/mutex_guard_await.rs:9: line longer than 100 chars
tidy [style (tests)]: FAIL
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'virtualenv'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (26.0.1)
linting python files
---
info: ES-Check: there were no ES version matching errors!  🎉
typechecking javascript files
tidy: The following check failed: style (tests)
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools-bin/rust-tidy --root-path=/checkout --cargo-path=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo --output-dir=/checkout/obj/build --concurrency=4 --npm-path=/node/bin/yarn --ci=true --extra-checks=py,cpp,js,spellcheck` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1618:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1417:29

--- BACKTRACE vvv
   0: <bootstrap::utils::exec::DeferredCommand>::finish_process
             at /checkout/src/bootstrap/src/utils/exec.rs:939:17
   1: <bootstrap::utils::exec::DeferredCommand>::wait_for_output::<&bootstrap::utils::exec::ExecutionContext>
             at /checkout/src/bootstrap/src/utils/exec.rs:831:21
   2: <bootstrap::utils::exec::ExecutionContext>::run
             at /checkout/src/bootstrap/src/utils/exec.rs:741:45
   3: <bootstrap::utils::exec::BootstrapCommand>::run::<&bootstrap::core::builder::Builder>
             at /checkout/src/bootstrap/src/utils/exec.rs:339:27
   4: <bootstrap::core::build_steps::test::Tidy as bootstrap::core::builder::Step>::run
             at /checkout/src/bootstrap/src/core/build_steps/test.rs:1417:29
   5: <bootstrap::core::builder::Builder>::ensure::<bootstrap::core::build_steps::test::Tidy>
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1579:36
   6: <bootstrap::core::build_steps::test::Tidy as bootstrap::core::builder::Step>::make_run
             at /checkout/src/bootstrap/src/core/build_steps/test.rs:1339:21
   7: <bootstrap::core::builder::StepDescription>::maybe_run
             at /checkout/src/bootstrap/src/core/builder/mod.rs:476:13
   8: bootstrap::core::builder::cli_paths::match_paths_to_steps_and_run
             at /checkout/src/bootstrap/src/core/builder/cli_paths.rs:232:18
   9: <bootstrap::core::builder::Builder>::run_step_descriptions
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1122:9
  10: <bootstrap::core::builder::Builder>::execute_cli
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1101:14
  11: <bootstrap::Build>::build
             at /checkout/src/bootstrap/src/lib.rs:799:25
  12: bootstrap::main
             at /checkout/src/bootstrap/src/bin/main.rs:130:11
  13: <fn() as core::ops::function::FnOnce<()>>::call_once
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/core/src/ops/function.rs:250:5
  14: std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/std/src/sys/backtrace.rs:166:18
  15: std::rt::lang_start::<()>::{closure#0}
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/std/src/rt.rs:206:18
  16: <&dyn core::ops::function::Fn<(), Output = i32> + core::panic::unwind_safe::RefUnwindSafe + core::marker::Sync as core::ops::function::FnOnce<()>>::call_once
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/core/src/ops/function.rs:287:21
  17: std::panicking::catch_unwind::do_call::<&dyn core::ops::function::Fn<(), Output = i32> + core::panic::unwind_safe::RefUnwindSafe + core::marker::Sync, i32>
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/std/src/panicking.rs:581:40
  18: std::panicking::catch_unwind::<i32, &dyn core::ops::function::Fn<(), Output = i32> + core::panic::unwind_safe::RefUnwindSafe + core::marker::Sync>
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/std/src/panicking.rs:544:19
  19: std::panic::catch_unwind::<&dyn core::ops::function::Fn<(), Output = i32> + core::panic::unwind_safe::RefUnwindSafe + core::marker::Sync, i32>
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/std/src/panic.rs:359:14
  20: std::rt::lang_start_internal::{closure#0}
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/std/src/rt.rs:175:24
  21: std::panicking::catch_unwind::do_call::<std::rt::lang_start_internal::{closure#0}, isize>
             at /rustc/ad726b5063362ec9897ef3d67452fc5606ee70fa/library/std/src/panicking.rs:581:40
---
  28: __libc_start_main
  29: _start


Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:02:54
  local time: Thu Apr  9 06:19:45 UTC 2026
  network time: Thu, 09 Apr 2026 06:19:45 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 9, 2026

☀️ Try build successful (CI)
Build commit: dc4f33d (dc4f33d8604405ae6c35c052336e3c3eb37de413, parent: d0442e2800d356ae282ddcdbe0eff8798fe648b6)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (dc4f33d): comparison URL.

Overall result: ❌ regressions - please read:

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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never
@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.5% [0.2%, 0.8%] 35
Regressions ❌
(secondary)
1.1% [0.1%, 3.6%] 89
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) 0.5% [0.2%, 0.8%] 35

Max RSS (memory usage)

Results (primary 6.7%, secondary 4.8%)

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

mean range count
Regressions ❌
(primary)
6.7% [0.5%, 14.2%] 65
Regressions ❌
(secondary)
5.3% [0.9%, 11.8%] 42
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.3% [-2.8%, -2.0%] 3
All ❌✅ (primary) 6.7% [0.5%, 14.2%] 65

Cycles

Results (primary 1.8%, secondary 3.4%)

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

mean range count
Regressions ❌
(primary)
1.8% [1.3%, 2.4%] 2
Regressions ❌
(secondary)
3.4% [2.6%, 5.4%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.8% [1.3%, 2.4%] 2

Binary size

Results (secondary -0.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)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.6% [-1.1%, -0.0%] 12
All ❌✅ (primary) - - 0

Bootstrap: 489.468s -> 489.718s (0.05%)
Artifact size: 395.51 MiB -> 395.60 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async function's future not Send even when non-Send type is not alive across await expression

4 participants