-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Allow for better optimizations of iterators for zero-sized types #25434
Conversation
Using regular pointer arithmetic to iterate collections of zero-sized types doesn't work, because we'd get the same pointer all the time. Our current solution is to convert the pointer to an integer, add an offset and then convert back, but this inhibits certain optimizations. What we should do instead is to convert the pointer to one that points to an i8*, and then use a LLVM GEP instructions without the inbounds flag to perform the pointer arithmetic. This allows to generate pointers that point outside allocated objects without causing UB (as long as you don't dereference them), and it wraps around using two's complement, i.e. it behaves exactly like the wrapping_* operations we're currently using, with the added benefit of LLVM being able to better optimize the resulting IR.
(rust_highfive has picked a reviewer for you, use r? to override) |
Is it undefined behavior in LLVM to use |
Yes, Not sure what you mean by the base pointer thing. |
Using regular pointer arithmetic to iterate collections of zero-sized types doesn't work, because we'd get the same pointer all the time. Our current solution is to convert the pointer to an integer, add an offset and then convert back, but this inhibits certain optimizations. What we should do instead is to convert the pointer to one that points to an i8\*, and then use a LLVM GEP instructions without the inbounds flag to perform the pointer arithmetic. This allows to generate pointers that point outside allocated objects without causing UB (as long as you don't dereference them), and it wraps around using two's complement, i.e. it behaves exactly like the wrapping_* operations we're currently using, with the added benefit of LLVM being able to better optimize the resulting IR.
Using regular pointer arithmetic to iterate collections of zero-sized types
doesn't work, because we'd get the same pointer all the time. Our
current solution is to convert the pointer to an integer, add an offset
and then convert back, but this inhibits certain optimizations.
What we should do instead is to convert the pointer to one that points
to an i8*, and then use a LLVM GEP instructions without the inbounds
flag to perform the pointer arithmetic. This allows to generate pointers
that point outside allocated objects without causing UB (as long as you
don't dereference them), and it wraps around using two's complement,
i.e. it behaves exactly like the wrapping_* operations we're currently
using, with the added benefit of LLVM being able to better optimize the
resulting IR.