Skip to content

Emit dereferenceable assumptions for slices #137568

Open
@Kmeakin

Description

@Kmeakin

Rust currently emits dereferenceable attributes/metadata for pointers to Sized types. The number of bytes in dereferenceable attributes/metadata must be a constant, so cannot be used for pointers to dynamically sized types like [T], str. But assume operand bundles can be non-constants. So for pointers to DSTs, Rust should emit appropriate assumes.

Currently LLVM does not make use of variable-sized dereferenceable assumptions, but that may be changing soon, so it would be good if Rust could exploit this once it is added.

fn ptr_to_slice_u8(xs: &[u8]) {}
fn ptr_to_slice_u16(xs: &[u16]) {}
define void @ptr_to_slice_u8(ptr noalias nocapture noundef nonnull readonly align 1 %xs.0, i64 noundef %xs.1) unnamed_addr #0 {
  call void @llvm.assume(i1 true) [ "dereferenceable"(ptr %xs.0, i64 %xs.1) ]
  ret void
}

define void @ptr_to_slice_u16(ptr noalias nocapture noundef nonnull readonly align 2 %xs.0, i64 noundef %xs.1) unnamed_addr #0 {
  %len_bytes = mul nsw nuw i64 %xs.1, 2
  call void @llvm.assume(i1 true) [ "dereferenceable"(ptr %xs.0, i64 %len_bytes) ]  
  ret void
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.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