Elide temporary Vec allocations before integer folds#159151
Conversation
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
|
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 (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
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. |
This adds a pre-inlining MIR optimization for temporary
Vecpipelines of the form:The pass replaces the
collectcall withIterator::foldover the original producer, removing the intermediate allocation.The matcher is deliberately conservative:
Vectype with an integer element type;slice::iterand may containmap,filter,copied,cloned, andenumerate;Vec, andIntoItertemporaries 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
collectunwind 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/rustmain 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