Skip to content

compiler: attach range metadata to slice len/cap #5147

@niaow

Description

@niaow

The slice len/cap are known to be in the range [0, (^uintptr(0) >> 1) / elemSize].
We should provide this information to LLVM because:

  1. It can prove that index math (like x + y / 2) will not overflow
  2. We sometimes mix signed and unsigned comparisons, which can confuse the compiler.

In order to do this, we must:

  1. Wrap LLVMCreateConstantRangeAttribute in our bindings
  2. Attach the range metadata to the function parameters
  3. Attach the range metadata to slicing operations

Unfortunately, LLVMCreateConstantRangeAttribute was not introduced until LLVM 19.1. We may need to condition the logic on LLVM version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions