Issue 241002
Summary [LAA] Symbolic stride check of `poison` stride can introduce immediate UB
Labels llvm:analysis
Assignees
Reporter MacDue
    ```llvm
; @reproducer passes poison as the symbolic stride, but also passes false as
; the condition guarding every use of that stride. The source program never
; consumes the poison and returns 42.
;
; Loop vectorization versions the symbolic stride and currently emits:
;
;   %ident.check = icmp ne i64 %stride, 1
;   br i1 %ident.check, label %scalar.ph, label %vector.memcheck
;
; The comparison and branch are not frozen. When %stride is poison, branching
; on %ident.check is immediate undefined behavior.

@src = "" [1024 x i32] zeroinitializer, align 4
@dst = global [1024 x i32] zeroinitializer, align 4

define i32 @predicated_symbolic_stride(ptr %dst, ptr %src, i64 %stride, i1 %condition) {
entry:
  br label %loop

loop:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %latch ]
  br i1 %condition, label %then, label %latch

then:
 %index = mul i64 %iv, %stride
  %src.ptr = getelementptr i32, ptr %src, i64 %index
  %value = load i32, ptr %src.ptr, align 4
  %dst.ptr = getelementptr i32, ptr %dst, i64 %iv
  store i32 %value, ptr %dst.ptr, align 4
  br label %latch

latch:
  %iv.next = add nuw nsw i64 %iv, 1
  %exit = icmp eq i64 %iv.next, 1024
  br i1 %exit, label %return, label %loop, !llvm.loop !0

return:
  ret i32 42
}

define i32 @reproducer() {
entry:
  %result = call i32 @predicated_symbolic_stride(
      ptr @dst, ptr @src, i64 poison, i1 false)
  ret i32 %result
}

!0 = distinct !{!0, !1}
!1 = !{!"llvm.loop.vectorize.enable"}
```

Godbolt: https://godbolt.org/z/Yqrrd7xve

Assisted-by: AI

(I don't think this is triggers a miscompile, but I came across this while looking at LAA).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to