Skip to content

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

Closed

Description

@nikic

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.

Activity

  1. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
    on Feb 18, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 18, 2025
  3. nikic commented on Feb 18, 2025

    @nikic
    ContributorAuthor

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

  4. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 18, 2025
  5. added
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    on Feb 18, 2025
  6. scottmcm commented on Feb 18, 2025

    @scottmcm
    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.

  7. nikic commented on Feb 18, 2025

    @nikic
    ContributorAuthor

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

  8. scottmcm commented on Feb 18, 2025

    @scottmcm
    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.

  9. self-assigned this
    on Feb 19, 2025
  10. added 3 commits that reference this issue on Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchE-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions