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

Make use of getelementptr nuw in pointer::add #137217

Closed
nikic opened this issue Feb 18, 2025 · 4 comments · Fixed by #137271
Closed

Make use of getelementptr nuw in pointer::add #137217

nikic opened this issue Feb 18, 2025 · 4 comments · Fixed by #137271
Assignees
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikic
Copy link
Contributor

nikic commented Feb 18, 2025

add() on pointers should generate getelementptr inbounds nuw on LLVM 20 and above, to let LLVM know that the offset cannot be negative. We'll need a new variant of intrinsic::offset to pass down this information.

@nikic nikic added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such labels Feb 18, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 18, 2025
@nikic
Copy link
Contributor Author

nikic commented Feb 18, 2025

cc @scottmcm as this seems like something up your alley :)

@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 18, 2025
@scottmcm scottmcm added the E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. label Feb 18, 2025
@scottmcm
Copy link
Member

Shouldn't need a new intrinsic -- we already lower add to MIR Offset (#110837) -- so it's just a matter of updating

mir::BinOp::Offset => {
let pointee_type = input_ty
.builtin_deref(true)
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", input_ty));
let pointee_layout = bx.cx().layout_of(pointee_type);
if pointee_layout.is_zst() {
// `Offset` works in terms of the size of pointee,
// so offsetting a pointer to ZST is a noop.
lhs
} else {
let llty = bx.cx().backend_type(pointee_layout);
bx.inbounds_gep(llty, lhs, &[rhs])
}
}

to check whether the RHS is unsigned, and add nuw if it is.

@nikic
Copy link
Contributor Author

nikic commented Feb 18, 2025

Oh nice, that will make things a lot simpler. Maybe I can even implement this myself...

@scottmcm
Copy link
Member

If you're tempted, go for it! Should be pretty similar to https://github.com/rust-lang/rust/pull/136575/files#diff-76d7dff532dc6719661f41c02777d2b1f8888cd0b8113b9908ed0085ef49fff4 , just with less confusion along the way since you actually know the LLVM API (unlike me :P)

If not I'll get to it eventually, just not sure when that'll be. I'm excited for it, since I tried doing it with assumes in #123598 but perf wasn't happy with me.

@nikic nikic self-assigned this Feb 19, 2025
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 19, 2025
Emit getelementptr inbounds nuw for pointer::add()

Lower pointer::add (via intrinsic::offset with unsigned offset) to getelementptr inbounds nuw on LLVM versions that support it. This lets LLVM make use of the pre-condition that the offset addition does not wrap in an unsigned sense. Together with inbounds, this also implies that the offset is non-negative.

Fixes rust-lang#137217.
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 19, 2025
Emit getelementptr inbounds nuw for pointer::add()

Lower pointer::add (via intrinsic::offset with unsigned offset) to getelementptr inbounds nuw on LLVM versions that support it. This lets LLVM make use of the pre-condition that the offset addition does not wrap in an unsigned sense. Together with inbounds, this also implies that the offset is non-negative.

Fixes rust-lang#137217.
@bors bors closed this as completed in e0be1a0 Feb 24, 2025
RalfJung pushed a commit to RalfJung/miri that referenced this issue Feb 24, 2025
Emit getelementptr inbounds nuw for pointer::add()

Lower pointer::add (via intrinsic::offset with unsigned offset) to getelementptr inbounds nuw on LLVM versions that support it. This lets LLVM make use of the pre-condition that the offset addition does not wrap in an unsigned sense. Together with inbounds, this also implies that the offset is non-negative.

Fixes rust-lang/rust#137217.
github-merge-queue bot pushed a commit to rust-lang/rust-analyzer that referenced this issue Feb 24, 2025
Emit getelementptr inbounds nuw for pointer::add()

Lower pointer::add (via intrinsic::offset with unsigned offset) to getelementptr inbounds nuw on LLVM versions that support it. This lets LLVM make use of the pre-condition that the offset addition does not wrap in an unsigned sense. Together with inbounds, this also implies that the offset is non-negative.

Fixes rust-lang/rust#137217.
BoxyUwU pushed a commit to BoxyUwU/rustc-dev-guide that referenced this issue Feb 25, 2025
Emit getelementptr inbounds nuw for pointer::add()

Lower pointer::add (via intrinsic::offset with unsigned offset) to getelementptr inbounds nuw on LLVM versions that support it. This lets LLVM make use of the pre-condition that the offset addition does not wrap in an unsigned sense. Together with inbounds, this also implies that the offset is non-negative.

Fixes rust-lang/rust#137217.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants