Issue 203104
Summary [LoopInterchange] Miscompile when outer loop reduction has extra use in inner loop
Labels miscompilation
Assignees
Reporter nikic
    https://llvm.godbolt.org/z/P48nz4sfb
```llvm
; Stray use of the outer reduction PHI (%sum) inside the inner loop body.
; %sum holds the accumulated value at the start of outer iteration i.
; After interchange, %sum becomes the inner-loop accumulator and takes
; completely different values at each (i,j) point.

@A = global [4 x [4 x i32]] zeroinitializer
@B = global [4 x [4 x i32]] zeroinitializer

define i32 @f(ptr noalias %A, ptr noalias %B) {
entry:
  br label %outer.header

outer.header:
  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
  %sum = phi i32 [ 0, %entry ], [ %sum.lcssa, %outer.latch ]
  br label %inner.header

inner.header:
  %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.latch ]
  %sum.inner = phi i32 [ %sum, %outer.header ], [ %sum.next, %inner.latch ]
  %gepA = getelementptr inbounds [4 x [4 x i32]], ptr %A, i64 0, i64 %j, i64 %i
  %a = load i32, ptr %gepA
  %sum.next = add i32 %sum.inner, %a
  %gepB = getelementptr inbounds [4 x [4 x i32]], ptr %B, i64 0, i64 %j, i64 %i
  store i32 %sum, ptr %gepB ; stray use of outer reduction PHI
  br label %inner.latch

inner.latch:
  %j.next = add nuw nsw i64 %j, 1
  %jcmp = icmp slt i64 %j.next, 4
  br i1 %jcmp, label %inner.header, label %outer.latch

outer.latch:
  %sum.lcssa = phi i32 [ %sum.next, %inner.latch ]
  %i.next = add nuw nsw i64 %i, 1
  %icmp = icmp slt i64 %i.next, 4
  br i1 %icmp, label %outer.header, label %exit

exit:
  %res = phi i32 [ %sum.lcssa, %outer.latch ]
  ret i32 %res
}
```
Disclosure: The test case is AI generated.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to