Skip to content

New MIR opt pass simplify_pow_of_two#114254

Closed
Centri3 wants to merge 5 commits intorust-lang:masterfrom
Centri3:simplify-pow-of-two
Closed

New MIR opt pass simplify_pow_of_two#114254
Centri3 wants to merge 5 commits intorust-lang:masterfrom
Centri3:simplify-pow-of-two

Conversation

@Centri3
Copy link
Member

@Centri3 Centri3 commented Jul 30, 2023

This detects calls to x.pow where x is a power of two and an integer. This can use shl instead, with some nuances. Unfortunately modifying pow instead results in this check being done at runtime, and also getting the power used, thus is even slower than without it.

Supersedes the clippy lint rust-lang/rust-clippy#11057

I haven't benchmarked this yet but from my testing, without debug assertions this has 0 branches, thus is branchless, and only has a couple instructions, so I'm 99% confident this is faster. With debug assertions, I'm not sure, custom_mir doesn't really like assertions 😅

For calls where x is not 2, the rhs for shl can be exp * <power used to get x>.

Hope I did everything right ^^

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-mir-opt Area: MIR optimizations 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.