# mir opt hack days ## current project status * santiago works on github.com/rust-lang/rust/pull/67000 * this caused a bunch of sub-optimizations * unrelated: [optimize zst copy_repeatedly](https://github.com/rust-lang/rust/pull/67658) * unrelated: [optimize compress_undef_range](https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/post.20christmas.20hack.20days/near/184329856) * actually related: [if everything is undef, copy nothing](https://github.com/rust-lang/rust/pull/62655) * wesley works on - Fix broken optimization behavior around specialization ([PR #67662](https://github.com/rust-lang/rust/pull/67662)) - Fix the inliner (aka resolve github.com/rust-lang/rust/pull/63802) - Fix compile time regression with large ZSTs ([PR #67667](https://github.com/rust-lang/rust/pull/67667)) - Unrelated: [lint overflowing casts in const prop](https://github.com/rust-lang/rust/pull/67676) * oli works on * [reduce const eval special treatment of zsts](https://github.com/rust-lang/rust/pull/67501) * mahmut wants to work on this github.com/rust-lang/rust/pull/49206 ## Project ideas - Use the new dataflow framework in const prop to const prop more stuff - write more optimizations (https://github.com/rust-lang/rust/tree/master/src/librustc_mir/transform) - convert `intrinsics::abort()` into `TerminatorKind::Abort` - convert `intrinsics::unreachable()` into `TerminatorKind::Unreachable` - create an MVP for running multiple optimizations in an interleaved manner - not sure if we have any issues or other conversations about this - @nagisa brought this up at the all-hands and wrote a bit about it on their blog (https://kazlauskas.me/entries/the-road-to-bestest-optimiser.html) search for "Hoopl paper" - basically we want e.g. const prop and instcombine to run together so that for every statement first instcombine runs and then constprop instead of running instcombine on the entire mir body and then const prop on the entire mir body. - as a first step this would hopefully give us perf boons because we wouldn't be walking the MIR twice - but later I imagine we can actually run these optimizations in steps so e.g. branch elimination and block merging could suddenly permit const prop to prop more stuff # Santiago Callgrind analysis ``` - rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::step ▒ - 48.74% rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::terminator (inlined) ▒ rustc_mir::interpret::terminator::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_terminator (inlined) ▒ - rustc_mir::interpret::terminator::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_fn_call ▒ - 39.91% <rustc_mir::const_eval::CompileTimeInterpreter as rustc_mir::interpret::machine::Machine>::find_mir_or_eval_fn ▒ - rustc_mir::interpret::terminator::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_const_fn_call ▒ - 39.52% rustc_mir::interpret::place::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::copy_op (inlined) ▒ - rustc_mir::interpret::place::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::copy_op_no_validate ▒ - 39.28% rustc_mir::interpret::memory::Memory<M>::copy (inlined) ▒ - rustc_mir::interpret::memory::Memory<M>::copy_repeatedly ▒ - 37.58% rustc_mir::interpret::memory::Memory<M>::copy_undef_mask (inlined) ▒ - 37.02% rustc::mir::interpret::allocation::Allocation<Tag,Extra>::compress_undef_range ▒ - 13.20% core::iter::range::<impl core::iter::traits::iterator::Iterator for core::ops::range::Range<A>>::next ▒ - 8.74% core::mem::swap (inlined) ▒ - core::ptr::swap_nonoverlapping_one (inlined) ▒ 4.06% core::ptr::write (inlined) ▒ - 2.95% core::ptr::read (inlined) ▒ core::intrinsics::copy_nonoverlapping (inlined) ▒ core::intrinsics::overlaps (inlined) ▒ - 1.73% core::intrinsics::copy_nonoverlapping (inlined) ▒ core::intrinsics::overlaps (inlined) ▒ 1.48% core::cmp::impls::<impl core::cmp::PartialOrd for u64>::lt (inlined) ``` The above analysis is on an older nightly, but https://github.com/rust-lang/rust/blob/3ac40b69c75929dac5115b6a49eb4f1ecc352416/src/librustc_mir/const_eval/machine.rs#L53 is the culprit