Skip to content

Preserve typed pointee strides for compound pointer expressions #678

Closed

Description

@LunaStev

Problem

infer_ptr_pointee_ty reconstructs the pointee type from a small set of AST shapes. It handles variables, selected address-of expressions, casts, and grouping, but falls back to i8 for other expressions, including a pointer returned by a call or another pointer arithmetic expression.

gep_with_i64_offset uses this inferred type as the GEP element type. Consequently, two expressions with the same semantic ptr<T> type can advance by different byte counts solely because their syntax differs.

Code references:

Source-derived regression

Save as pointer_stride.wave:

fun identity(p: ptr<i32>) -> ptr<i32> {
    return p;
}

fun main() -> i32 {
    var storage: array<i32, 4> = [10, 20, 30, 40];
    var p: ptr<i32> = &storage[0];
    var direct: ptr<i32> = p + 2;
    var nested: ptr<i32> = (p + 1) + 1;
    var called: ptr<i32> = identity(p) + 1;

    var base: u64 = p as u64;
    var direct_bytes: u64 = (direct as u64) - base;
    var nested_bytes: u64 = (nested as u64) - base;
    var called_bytes: u64 = (called as u64) - base;

    if (direct_bytes != 8) {
        return 1;
    }
    if (nested_bytes != 8) {
        return 2;
    }
    if (called_bytes != 4) {
        return 3;
    }
    return 0;
}

Expected: exit 0. All address comparisons stay inside the same live allocation; the test does not dereference a potentially miscomputed pointer.

Following the current lowering, the direct expression advances 8 bytes, the nested expression advances 4 then 1 byte, and the call-result expression advances only 1 byte. These are source-derived predictions, not a recorded wavec execution. A separately authored LLVM reduction demonstrated the 8/5/1-byte distinction, but it was not generated by Wave.

Expected behavior and scope

Use the pointer expression's resolved Wave type from typed HIR to select the target-layout pointee type. Preserve that type across calls, fields, indexing, dereferencing, and nested arithmetic rather than defaulting a known non-byte pointer to i8.

Retain byte strides for genuine byte/string pointers, explicit casts, and the existing unchecked pointer-validity contract. Evaluate pointer-producing expressions once. This does not change pointer subtraction units, add bounds checks, or introduce new pointer syntax.

Completion criteria

  • wavec check pointer_stride.wave accepts the regression, and actual Wave-generated IR uses the correct pointee stride for all three expressions.
  • The regression exits 0 at unoptimized and optimized settings using the project's supported LLVM toolchain.
  • Cover nested pointer addition/subtraction, function and method results, pointer fields, and pointer-valued indexing/dereferencing where already supported.
  • Cover ptr<u8>, ptr<i32>, and a multi-byte aggregate pointee; compare equivalent expressions against the target's actual layout.
  • Preserve signed/unsigned offset handling and evaluate side-effecting base expressions exactly once.
  • Add focused coverage in tests/codegen_regressions.rs and runnable cases under tests/cases/; the tests must inspect real compiler output, not only hand-written IR.
  • Keep existing valid simple-variable and explicit-cast pointer arithmetic working.

Difficulty: medium. Related #436 concerns the integer index operand; this issue concerns the pointee stride of the pointer base expression. #414's broader 32-bit target work is not a prerequisite.

Activity

  1. added
    bug
    help wanted
    needs testing
    on Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wantedneeds testing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions