Skip to content

[LoopVectorize] Miscompile with iteration-local scoped alias metadata #79137

Closed
@nikic

Description

@nikic

C reproducer: https://clang.godbolt.org/z/f6rE4jvhM

IR reproducer: https://llvm.godbolt.org/z/7anbPon8K

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define hidden void @test(ptr %arg, i64 %num) {
entry:
  %icmp = icmp ult i64 %num, 2
  br i1 %icmp, label %exit, label %preheader

preheader:
  %arg.1 = getelementptr inbounds i8, ptr %arg, i64 1
  %end = add i64 %num, -2
  br label %loop

loop:
  %prev.ptr = phi ptr [ %cur.ptr, %loop ], [ %arg, %preheader ]
  %iv = phi i64 [ %iv.next, %loop ], [ 0, %preheader ]
  %cur.ptr = getelementptr inbounds i8, ptr %arg.1, i64 %iv
  call void @llvm.experimental.noalias.scope.decl(metadata !0)
  call void @llvm.experimental.noalias.scope.decl(metadata !3)
  %load.prev = load i8, ptr %prev.ptr, align 1, !alias.scope !0, !noalias !3
  %load.cur = load i8, ptr %cur.ptr, align 1, !alias.scope !3
  %add = add i8 %load.cur, %load.prev
  store i8 %add, ptr %cur.ptr, align 1, !alias.scope !3
  %iv.next = add nuw i64 %iv, 1
  %cmp = icmp eq i64 %iv, %end
  br i1 %cmp, label %exit, label %loop

exit:
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: readwrite)
declare void @llvm.experimental.noalias.scope.decl(metadata) #0

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: readwrite) }

!0 = !{!1}
!1 = distinct !{!1, !2}
!2 = distinct !{!2}
!3 = !{!4}
!4 = distinct !{!4, !5}
!5 = distinct !{!5}

The current and previous value are only noalias within the iteration (per the location of the @llvm.experimental.noalias.scope.decl), but the vectorization appears to assume that the underlying objects don't alias.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions