Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations should know rounding up a pointer moves it by less than the alignment #62502

Open
scottmcm opened this issue May 2, 2023 · 0 comments

Comments

@scottmcm
Copy link

scottmcm commented May 2, 2023

If you round up a pointer and calculate how far it moved from the original pointer (((p + (a-1)) & -a) - p), that distance is always less than the alignment: https://alive2.llvm.org/ce/z/fL_uXq

define i1 @src(i64 noundef %p, i64 noundef %a) noundef zeroext {
%start:
  %0 = ctpop i64 noundef %a
  %_3 = icmp eq i64 %0, 1
  assume i1 %_3
  %_8 = add i64 noundef %p, -1
  %_7 = add i64 %_8, noundef %a
  %_9 = sub i64 0, noundef %a
  %_6 = and i64 %_7, %_9
  %offset = sub i64 %_6, noundef %p
  %1 = icmp ult i64 %offset, noundef %a
  ret i1 %1
}
=>
define i1 @tgt(i64 noundef %p, i64 noundef %a) noundef zeroext {
%start:
  %0 = ctpop i64 noundef %a
  %_3 = icmp eq i64 %0, 1
  assume i1 %_3
  ret i1 1
}
Transformation seems to be correct!

But it appears that optimizations can't take advantage of that today https://llvm.godbolt.org/z/Mcc7ads1j, not even for constant alignment.


(Found trying to use https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.align_offset in Rust, when I noticed unnecessary checks.)

bors added a commit to rust-lang-ci/rust that referenced this issue May 5, 2023
`assume` the runtime range of `align_offset`

Found when I saw code with `align_to` having extraneous checks.

Demo that LLVM can't do this today: <https://rust.godbolt.org/z/6dnG749bq>

(It's filed as llvm/llvm-project#62502.)
thomcc pushed a commit to tcdi/postgrestd that referenced this issue Jul 18, 2023
`assume` the runtime range of `align_offset`

Found when I saw code with `align_to` having extraneous checks.

Demo that LLVM can't do this today: <https://rust.godbolt.org/z/6dnG749bq>

(It's filed as llvm/llvm-project#62502.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants