rust-lang / rust-lang/rust

Emit `dereferenceable` assumptions for slices

Open
#137,568 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen A-LLVM C-bug I-slow T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how Rust emits existing dereferenceable attributes and metadata for pointers to Sized types, then inspect the compiler's LLVM assume operand-bundle support. Verify behavior using slice references such as &[u8] and &[u16]. Done means dynamically sized slice pointers receive assumptions with byte lengths derived from their metadata, as shown in the issue's LLVM examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.