LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 51419 - [InstCombine] Infinite loop in InstCombine on smax+cmp+select sequence
Summary: [InstCombine] Infinite loop in InstCombine on smax+cmp+select sequence
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: PC All
: P normal
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks: release-13.0.1
  Show dependency tree
 
Reported: 2021-08-09 12:37 PDT by Cameron McInally
Modified: 2021-10-11 20:29 PDT (History)
4 users (show)

See Also:
Fixed By Commit(s): 9b942a545cb5 e260e10c4a21 b267d3ce8def ba048518e08f f4006c59497d 5b60faae3f10


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Cameron McInally 2021-08-09 12:37:13 PDT
The following test case is tripping an infinite loop in InstCombine. I've been unable to track down the root cause yet.

This issue was found in release/13.x, but Godbolt shows it failing in trunk as well.

https://godbolt.org/z/xKasbGheK

; RUN: opt -O3 -instcombine -S < %s
define i64 @infloop(double %d, i64 %a) {
  %1 = fptosi double %d to i32
  %2 = call i32 @llvm.smax.i32(i32 %1, i32 0)
  %3 = icmp slt i32 %2, 2
  %4 = zext i1 %3 to i32
  %5 = select i1 %3, i32 %2, i32 1
  %6 = zext i32 %5 to i64
  %7 = mul i64 %6, 4
  %8 = add i64 %a, %7
  ret i64 %8
}
Comment 1 Sanjay Patel 2021-08-09 13:22:12 PDT
Reduced:
declare i32 @llvm.smax.i32(i32, i32);

define i32 @infloop(i32 %x, double %d, i64 %a) {
  %t2 = call i32 @llvm.smax.i32(i32 %x, i32 0)
  %t3 = icmp slt i32 %t2, 2
  %t5 = select i1 %t3, i32 %t2, i32 1
  ret i32 %t5
}


There are opposing transforms somewhere under visitICmp. I'll try to find and fix.
Comment 2 Sanjay Patel 2021-08-10 12:37:40 PDT
https://reviews.llvm.org/rGb267d3ce8def

Leaving open as candidate for blocking 13.0 release.
Comment 3 Sanjay Patel 2021-08-10 12:39:39 PDT
If backporting, we'll also need this patch to avoid breaking tests:
https://reviews.llvm.org/rGe260e10c4a21
Comment 4 Sanjay Patel 2021-08-10 12:41:47 PDT
(In reply to Sanjay Patel from comment #3)
> If backporting, we'll also need this patch to avoid breaking tests:
> https://reviews.llvm.org/rGe260e10c4a21

And I almost forgot already, but that one was on top of adding tests:
https://reviews.llvm.org/rG9b942a545cb5
Comment 5 Cameron McInally 2021-08-10 12:52:41 PDT
Thanks for the quick response, Sanjay. I appreciate it.
Comment 6 Sanjay Patel 2021-08-10 13:01:30 PDT
(In reply to Cameron McInally from comment #5)
> Thanks for the quick response, Sanjay. I appreciate it.

Some day, we'll canonicalize to the intrinsics and get out of this mess. :)

Just curious - did this show up in real code or was it found by fuzzing of some kind?
Comment 7 Cameron McInally 2021-08-10 13:13:36 PDT
(In reply to Sanjay Patel from comment #6)
> Just curious - did this show up in real code or was it found by fuzzing of
> some kind?

It showed up in a handful of SPEC codes (e.g. omnetpp, wrf, blender). We don't use the Clang frontend though. And we also do heavy vectorization before opt is called.
Comment 8 Sanjay Patel 2021-08-11 05:43:30 PDT
(In reply to Cameron McInally from comment #7)
> It showed up in a handful of SPEC codes (e.g. omnetpp, wrf, blender). We
> don't use the Clang frontend though. And we also do heavy vectorization
> before opt is called.

Thanks - that sounds like good incentive to try to backport this to the release.

After reviewing my patch, I think that we do *not* need to backport the changes to InstSimplify to get the InstCombine fix to patch cleanly.

So to be clear, I expect that b267d3ce8def can be backported alone if that is viewed as a less risky/more direct bug fix.

But taking 9b942a545cb5 and e260e10c4a21 (InstSimplify) would get us more in line with the current 'main' branch.
Comment 9 Tom Stellard 2021-08-16 11:45:22 PDT
Merged: 5b60faae3f10