Skip to content

Conversation

Kmeakin
Copy link
Contributor

@Kmeakin Kmeakin commented Oct 2, 2025

Optimize checked_ilog and pow when the base is a power of two

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 2, 2025

r? @scottmcm

rustbot has assigned @scottmcm.
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

@Kmeakin Kmeakin force-pushed the km/optimize-ilog-base-power-of-two branch from 8f19ce6 to e5ca8cd Compare October 2, 2025 01:31
@rust-log-analyzer

This comment has been minimized.

@workingjubilee
Copy link
Member

workingjubilee commented Oct 2, 2025

does this affect the codegen in practical circumstances? I would expect even a fairly weak optimizer to perform this optimization, making us hand-coding it irrelevant and potentially even harmful because we introduce more conditional logic to chew through.

@Kmeakin Kmeakin changed the title Optimize checked_ilog when base is a power of two Optimize checked_ilog and pow when base is a power of two Oct 2, 2025
@Kmeakin
Copy link
Contributor Author

Kmeakin commented Oct 2, 2025

does this affect the codegen in practical circumstances?

It replaces a loop by some bit manipulations. Whether anyone actually calls either function with a compile-time known, power of 2 base is another question. But it can't hurt, since it is guarded by is_statically_known.

I would expect even a fairly weak optimizer to perform this optimization, making us hand-coding it irrelevant

No, in either function, LLVM is not able to see that the loop is performing a log/pow and apply the identities: https://godbolt.org/z/6vMsxc9Kh

and potentially even harmful because we introduce more conditional logic to chew through.

They're guarded by is_statically_known so will be ignored if the base is not known at compile-time

@Kmeakin Kmeakin force-pushed the km/optimize-ilog-base-power-of-two branch from e5ca8cd to 2faa397 Compare October 2, 2025 02:29
if base == 2 ** k, then
log(base, n) == log(2, n) / k
if base == 2 ** k, then
   (2 ** k) ** n
== 2 ** (k * n)
== 1 << (k * n)
@Kmeakin Kmeakin force-pushed the km/optimize-ilog-base-power-of-two branch from 2faa397 to abb7f32 Compare October 2, 2025 02:38
@workingjubilee
Copy link
Member

No, in either function, LLVM is not able to see that the loop is performing a log/pow and apply the identities: https://godbolt.org/z/6vMsxc9Kh

...then I'm kinda surprised! Nice catch.

Comment on lines 1574 to +1576
if base == 2 {
return self.checked_ilog2();
} else if base == 10 {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there's no need for the base == 2 case any more? The is_power_of_two one would handle it.

Comment on lines +1580 to +1583
if base.is_power_of_two() {
let k = base.ilog2();
return Some(try_opt!(self.checked_ilog2()) / k);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the a problem because it'll apply for base == 1, then divide by zero?

@scottmcm
Copy link
Member

scottmcm commented Oct 2, 2025

does this affect the codegen in practical circumstances?

Would be good to have codegen tests to demonstrate what this is doing -- especially since that way there's a way for people to try removing the special cases later if they think that LLVM no longer needs them.

@scottmcm scottmcm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants