Skip to content

Elide temporary Vec allocations before integer folds#159151

Closed
NotPppp1116 wants to merge 1 commit into
rust-lang:mainfrom
NotPppp1116:agent/temporary-vec-fold-elision
Closed

Elide temporary Vec allocations before integer folds#159151
NotPppp1116 wants to merge 1 commit into
rust-lang:mainfrom
NotPppp1116:agent/temporary-vec-fold-elision

Conversation

@NotPppp1116

Copy link
Copy Markdown

This adds a pre-inlining MIR optimization for temporary Vec pipelines of the form:

slice.iter()
    .map(...)
    .collect::<Vec<I>>()
    .into_iter()
    .fold(CONST, integer_op)

The pass replaces the collect call with Iterator::fold over the original producer, removing the intermediate allocation.

The matcher is deliberately conservative:

  • the collection must be the exact standard Vec type with an integer element type;
  • the producer must originate from slice::iter and may contain map, filter, copied, cloned, and enumerate;
  • adapter closures must be non-capturing;
  • the initializer must be constant;
  • the reducer must be an inherent wrapping or saturating add, multiply, or subtract operation;
  • the producer, Vec, and IntoIter temporaries must be SSA, single-use values in a linear control-flow chain.

Integer elements have no drop glue and the accepted reducers cannot panic, so interleaving reduction with iteration does not change panic or drop ordering. The replacement also preserves the original collect unwind edge and repairs storage markers on the direct path.

The codegen test covers six optimized pipelines and verifies that capturing adapters, observed vectors, and arbitrary reducer closures retain their allocations. A MIR-opt test checks both a representative rewrite and a rejected pattern.

Validated on current rust-lang/rust main with:

  • ./x check compiler/rustc_mir_transform
  • ./x test tests/mir-opt/temporary_vec_fold_elision.rs --target x86_64-unknown-linux-gnu
  • ./x test tests/codegen-llvm/temporary-vec-fold-elision.rs --target x86_64-unknown-linux-gnu
  • ./x test tidy

@rustbot

rustbot commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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 Jul 11, 2026
@rustbot

rustbot commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @oli-obk (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir, mir-opt
  • compiler, mir, mir-opt expanded to 75 candidates
  • Random selection from 17 candidates

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
##[group]Linting stage2 bootstrap (stage1 -> stage2, x86_64-unknown-linux-gnu)
error: failed to run `rustc` to learn about target-specific information

Caused by:
  process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/clippy-driver /checkout/obj/build/bootstrap/debug/rustc - --crate-name ___ --print=file-names --sysroot /checkout/obj/build/x86_64-unknown-linux-gnu/stage1 --cfg=windows_raw_dylib -Zannotate-moves -Zunstable-options -Zforce-unstable-if-unmarked -Zmacro-backtrace -Csplit-debuginfo=off -Clink-args=-Wl,-z,origin '-Clink-args=-Wl,-rpath,$ORIGIN/../lib' -Alinker-messages -Zunstable-options --target x86_64-unknown-linux-gnu --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit status: 101)
  --- stderr

  thread 'rustc' (31473) panicked at compiler/rustc_span/src/symbol.rs:2841:13:
  duplicate symbols in the rustc symbol list and the extra symbols added by the driver: [[101, 110, 117, 109, 101, 114, 97, 116, 101, 95, 109, 101, 116, 104, 111, 100], [102, 111, 108, 100], [105, 116, 101, 114, 95, 99, 108, 111, 110, 101, 100], [105, 116, 101, 114, 95, 99, 111, 112, 105, 101, 100], [105, 116, 101, 114, 95, 102, 105, 108, 116, 101, 114], [115, 108, 105, 99, 101, 95, 105, 116, 101, 114]]
  stack backtrace:
     0: __rustc::rust_begin_unwind
     1: core::panicking::panic_fmt
     2: <rustc_span::symbol::Interner>::with_extra_symbols
     3: <rustc_span::SessionGlobals>::new
---
  warning: the ICE couldn't be written to `/checkout/rustc-ice-2026-07-11T22_07_03-31472.txt`: Read-only file system (os error 30)

  note: rustc 1.99.0-nightly (094ac9f96 2026-07-11) running on x86_64-unknown-linux-gnu

  note: compiler flags: -Z annotate-moves -Z unstable-options -Z force-unstable-if-unmarked -Z macro-backtrace -C split-debuginfo=off -C link-args=-Wl,-z,origin -C link-args=-Wl,-rpath,$ORIGIN/../lib -Z unstable-options --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro -Z on-broken-pipe=kill -Z tls-model=initial-exec -Z allow-features=binary-dep-depinfo,proc_macro_span,proc_macro_span_shrink,proc_macro_diagnostic

  query stack during panic:
  end of query stack
  note: Clippy version: clippy 0.1.99 (094ac9f961 2026-07-11)

@saethlin

Copy link
Copy Markdown
Member

We don't accept MIR optimizations that hard-code specific relationships with the standard library like this. There are other problems with this PR, but that is the most fundamental one.

I suggest you engage with compiler development on Zulip if you want to cooperate on the design of the MIR optimization pipeline.

@saethlin saethlin closed this Jul 11, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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