Issue 242347
Summary [Hexagon] hexagon-vc raises alignment of masked store
Labels new issue
Assignees
Reporter ttm02
    ## Summary

`HexagonVectorCombine` (`hexagon-vc`) rewrites the alignment of `llvm.masked.store` (and `llvm.masked.load`) inside nested loops to `align 128`, even though the pointer is only 4-aligned.
The pointer is a loop PHI that starts at an outer PHI, and the outer PHI advances by a runtime stride.
The pass looks at the 128-aligned base and the constant `+128` inner step, but never at the runtime outer step.

## Reproducer

```llvm
target triple = "hexagon-unknown-unknown-elf"

; %nrows rows of %stride bytes starting at a 128-byte-aligned %base.
; Each row is stored in 128-byte chunks, but the bug also surfaces with <16 x float> vectors.
; The row start %row is not aligned (e.g. %stride = 48), so the store is only 4-aligned (float type).
define void @rows(ptr %base, i32 %nrows, i32 %stride, i32 %len, ptr %src) #0 {
entry:
  call void @llvm.assume(i1 true) [ "align"(ptr %base, i32 128) ]
  ; The values of %mask and %value don't matter for this bug.
 %value = load <32 x float>, ptr %src, align 4
  %mask = call <32 x i1> @llvm.get.active.lane.mask.v32i1.i32(i32 0, i32 %len)
  br label %outer

outer:
  %row = phi ptr [ %base, %entry ], [ %row.next, %outer.latch ]
  %r = phi i32 [ 0, %entry ], [ %r.next, %outer.latch ]
  br label %inner

inner:
  %p = phi ptr [ %row, %outer ], [ %p.next, %inner ]
  %i = phi i32 [ 0, %outer ], [ %i.next, %inner ]
  call void @llvm.masked.store.v32f32.p0(<32 x float> %value, ptr align 4 %p, <32 x i1> %mask)
  %p.next = getelementptr i8, ptr %p, i32 128
  %i.next = add i32 %i, 32
  %ic = icmp slt i32 %i.next, %len
  br i1 %ic, label %inner, label %outer.latch

outer.latch:
  %row.next = getelementptr i8, ptr %row, i32 %stride
  %r.next = add i32 %r, 1
  %oc = icmp slt i32 %r.next, %nrows
 br i1 %oc, label %outer, label %exit

exit:
  ret void
}

attributes #0 = { "target-cpu"="hexagonv79" "target-features"="+hvxv79,+hvx-length128b" }

; To run this reproducer as a lit test:
; RUN: opt -S -mtriple=hexagon -mattr=+hvxv79,+hvx-length128b -hexagon-vc < %s | FileCheck %s
; The row start advances by a runtime %stride, so %p is only 4-aligned.
; CHECK: call void @llvm.masked.store.v32f32.p0(<32 x float> %{{.*}}, ptr align 4 %p,
```

```console
$ opt -S -mtriple=hexagon -mattr=+hvxv79,+hvx-length128b -hexagon-vc repro.ll
```

**Expected:** `ptr align 4 %p` stays unchanged. Nothing in the IR proves more than 4-byte alignment for `%p`.

**Actual:** `ptr align 128 %p`.
With the false `align 128`, codegen emits an aligned HVX store, which ignores the low 7 address bits, so data is written to the wrong address.
The same happens with `llc -O3`, where `hexagon-vc` runs as part of the pipeline.

## AI tool use
The reproducer was reduced with AI assistance (Claude Code).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to