Skip to content
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

A MIR pass simplifying trivial enum uses, and other temporary locals #138739

Closed
wants to merge 1 commit into from

Conversation

FractalFir
Copy link
Contributor

Motivation

Fixes:#138544

Currently, MIR optimizations are unable to remove(due to pass ordering) simple option uses in loops:

_9 = Option::<u32>::Some(copy _7);
StorageDead(_6);
_10 = copy ((_9 as Some).0: u32);

This pass removes such temporary enums(and a few other uneded temporaries).
It works by walking statements one by one, collecting rvalues assigned directly to locals.

Then, knowing what rvalue is assigned to what local, it replaces places in the following statements, like this:

_9 = Option::<u32>::Some(copy _7);
StorageDead(_6);
_10 =  copy _7;

Ensuring soundness

This pass has several deliberate limitations. It will not:

  1. Move any places across a lvalue with a deref, since that could write to memory
_2 = Option::<u32>::Some(copy _1);
(*_4) = copy _5; // Could be indirectly writing to _1!
_3 = copy ((_2 as Some).0: u32);
  1. Propagate with an rvalue with a deref, since moving memory reads across a StorageDead may not always be sound
_2 = &_1
_3 = Option::<u32>::Some(copy (*_2));
StorageDead(_1) // Since _1 is no longer valid, _2 points to unintialized memory, and moving a read to it is not sound.
 _4 =  copy ((_3 as Some).0: u32);;
  1. Propagate an rvalue with a Move operand, since the semantics of Moves are not decided yet, and may leave behind uninitialized memory.
_9 = Option::<u32>::Some(move _7); // _7 is(potentially) no longer valid after this statement!
_10 = copy ((_9 as Some).0: u32);

Still, despite those limitations, the pass is able to simplify most if not all simple uses of enums(and clean up some temporaries).

This pass is designed to be extendable, and I have a few ideas for simplifying other pieces of MIR using it.Still, I decided to make a PR for this simpler, easier to review version of this pass, and extend the pass later.

@rustbot
Copy link
Collaborator

rustbot commented Mar 20, 2025

r? @fee1-dead

rustbot has assigned @fee1-dead.
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 to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-log-analyzer

This comment has been minimized.

@jieyouxu
Copy link
Member

r? mir-opt

@rustbot rustbot assigned saethlin and unassigned fee1-dead Mar 20, 2025
@FractalFir
Copy link
Contributor Author

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)

That is a bit odd - the PR builds and passes all tests locally. I guess this assertion is only enabled in debug builds - I'll see if I can reproduce & fix the build faliure.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 21, 2025
@rustbot

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 21, 2025

Some changes occurred in src/tools/cargo

cc @ehuss

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 21, 2025
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
  cargo:rustc-cfg=
  --- stderr

  thread 'main' panicked at library/core/src/fmt/mod.rs:1493:5:
  assertion failed: arg.position < args.len()
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
[RUSTC-TIMING] once_cell test:false 0.505
[RUSTC-TIMING] futures_io test:false 0.342
error: failed to run custom build command for `libc v0.2.155`
---
  cargo:rustc-cfg=
  --- stderr

  thread 'main' panicked at library/core/src/fmt/mod.rs:1493:5:
  assertion failed: arg.position < args.len()
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[RUSTC-TIMING] futures_task test:false 0.395
[RUSTC-TIMING] smallvec test:false 0.561
[RUSTC-TIMING] build_script_build test:false 0.330
[RUSTC-TIMING] autocfg test:false 0.607
---
    Finished `dev` profile [unoptimized] target(s) in 0.05s
##[endgroup]
WARN: currently no CI rustc builds have rustc debug assertions enabled. Please either set `rust.debug-assertions` to `false` if you want to use download CI rustc or set `rust.download-rustc` to `false`.
[TIMING] core::build_steps::tool::LibcxxVersionTool { target: x86_64-unknown-linux-gnu } -- 0.001
ERROR: Tool `book` was not recorded in tool state.
ERROR: Tool `nomicon` was not recorded in tool state.
ERROR: Tool `reference` was not recorded in tool state.
ERROR: Tool `rust-by-example` was not recorded in tool state.
ERROR: Tool `edition-guide` was not recorded in tool state.
ERROR: Tool `embedded-book` was not recorded in tool state.
Build completed unsuccessfully in 0:00:00
  local time: Sat Mar 22 00:01:43 UTC 2025
  network time: Sat, 22 Mar 2025 00:01:43 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@FractalFir
Copy link
Contributor Author

I am closing this PR, since the build failures look like a miscompilation. I'll see if I can make a fully sound version of this PR.

@FractalFir FractalFir closed this Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants