Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LoopFlatten miscompile #59339

Closed
jreiffers opened this issue Dec 5, 2022 · 2 comments
Closed

LoopFlatten miscompile #59339

jreiffers opened this issue Dec 5, 2022 · 2 comments

Comments

@jreiffers
Copy link
Contributor

jreiffers commented Dec 5, 2022

Input:

; RUN: opt < %s -S -opaque-pointers -loop-flatten -verify-loop-info -verify-dom-info -verify-scev -verify

define void @test0(i16* %0, i16* %1) {
2:
  br label %.preheader

.preheader:
  %3 = phi i64 [ 0, %2 ], [ %7, %6 ]
  %4 = mul i64 %3, 3
  %5 = getelementptr i16, ptr %1, i64 %4
  br label %9

6:
  %7 = add i64 %3, 1
  %8 = icmp slt i64 %7, 2
  br i1 %8, label %.preheader, label %16

9:
  %10 = phi i64 [ 0, %.preheader ], [ %14, %9 ]
  %11 = load i16, ptr %5, align 2
  %12 = add i64 %10, %4
  %13 = getelementptr i16, ptr %0, i64 %12
  store i16 %11, ptr %13, align 2
  %14 = add nuw nsw i64 %10, 1
  %15 = icmp ult i64 %14, 3
  br i1 %15, label %9, label %6

16:
  ret void
}

Note that %3 goes from 0 to 1, so the loads for %11 are %1[0], %1[0], %1[0], %1[3], %1[3], %1[3].

The output is:

define void @test0(ptr %0, ptr %1) {
  %flatten.tripcount = mul i64 3, 2
  br label %.preheader

.preheader:                                       ; preds = %6, %2
  %3 = phi i64 [ 0, %2 ], [ %7, %6 ]
  %4 = mul i64 %3, 3
  %5 = getelementptr i16, ptr %1, i64 %4
  br label %9

6:                                                ; preds = %9
  %7 = add i64 %3, 1
  %8 = icmp slt i64 %7, %flatten.tripcount
  br i1 %8, label %.preheader, label %16

9:                                                ; preds = %.preheader
  %10 = phi i64 [ 0, %.preheader ]
  %11 = load i16, ptr %5, align 2
  %12 = add i64 %10, %4
  %13 = getelementptr i16, ptr %0, i64 %3
  store i16 %11, ptr %13, align 2
  %14 = add nuw nsw i64 %10, 1
  %15 = icmp ult i64 %14, 3
  br label %6

16:                                               ; preds = %6
  ret void
}

Now, %3 goes from 0 to 5, and the loads are from %1[0], %1[3], %1[6], ...

The culprit seems to be 3ea6a9a.

@sjoerdmeijer
Copy link
Collaborator

Thanks for the nice reproducer. I will see if a quick/simple fix is possible before the end of today, or revert this.

@jreiffers
Copy link
Contributor Author

Thanks!

sjoerdmeijer added a commit that referenced this issue Dec 5, 2022
This reverts commit 3ea6a9a because of the
reported miscompilation in: #59339
stuij added a commit to stuij/llvm-project that referenced this issue Apr 29, 2024
The enablement of LoopFlatten got reverted by commit 8250180 because of a
reported miscompilation in llvm#59339. That issue got fixed by commit 161bfa5.
nikic pushed a commit to nikic/llvm-project that referenced this issue Apr 30, 2024
The enablement of LoopFlatten got reverted by commit 8250180 because of a
reported miscompilation in llvm#59339. That issue got fixed by commit 161bfa5.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants