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

[InstCombine] Infinite combine loop #48549

Closed
haoxintu opened this issue Feb 16, 2021 · 5 comments
Closed

[InstCombine] Infinite combine loop #48549

haoxintu opened this issue Feb 16, 2021 · 5 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla

Comments

@haoxintu
Copy link

Bugzilla Link 49205
Resolution FIXED
Resolved on Mar 02, 2021 20:13
Version trunk
OS Linux
Blocks #48246
CC @RKSimon,@nikic,@rotateright,@tstellar
Fixed by commit(s) 9502061 692808e

Extended Description

Hi, all.

This code, s.c, is a valid program but makes Clang hung on at compile time at -O1 to -Os.

$cat s.c
int a;
void b() {
int *c = &a;
int *d = &a;
int **e = &d;
for (*c = 7; c <= 2; *c += 1) {
int **f = d;
*c = 0;
**e /= f ? (c ? 10 : **f) % (*c &= 6) : 0;
}
*d = (*d -= *c & **e != 4) && (4 != a) - **e & d == c;
int g = &d;
g++;
}

$time clang -c -w -O0 s.c

real 0m0.021s
user 0m0.011s
sys 0m0.010s

$clang -c -w -O1 test.cc
//endless compiling (the same as -O2,-O3, and -Os options)

$clang -v
clang version 13.0.0 (https://github.com/llvm/llvm-project 22f00f6)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/haoxin/haoxin-data/dut-research/compilers/llvm-project/build-20210216/bin
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Candidate multilib: .;@m64
Selected multilib: .;@m64

This problem only occurs in clang-trunk and clang-11.0.0 or clang-11.0.1, while other released versions from clang-10 downwards perform well in this case.

Thanks.
Haoxin

@haoxintu
Copy link
Author

assigned to @rotateright

@nikic
Copy link
Contributor

nikic commented Feb 19, 2021

Bugpoint reduction:

opt -instcombine

; ModuleID = ''
source_filename = "test.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@​a = external dso_local global i32, align 4

define dso_local void @​b() local_unnamed_addr #​0 {
entry:
br label %for.cond

for.cond: ; preds = %for.body, %entry
%storemerge = phi i32 [ 7, %entry ], [ %add, %for.body ]
br i1 icmp uge (i32* inttoptr (i64 2 to i32*), i32* @​a), label %for.body, label %for.end

for.body: ; preds = %for.cond
%0 = load i32, i32* @​a, align 4
%div = sdiv i32 %0, undef
%add = add nsw i32 %div, 1
br label %for.cond

for.end: ; preds = %for.cond
%cmp6 = icmp ne i32 %storemerge, 4
%conv = zext i1 %cmp6 to i32
%and7 = and i32 %storemerge, %conv
%sub = sub nsw i32 %storemerge, %and7
%cmp9 = icmp ne i32 %sub, 4
%conv10 = zext i1 %cmp9 to i32
%sub11 = sub nsw i32 %conv10, %sub
%and14 = and i32 %sub11, 1
%1 = select i1 false, i32 0, i32 %and14
store i32 %1, i32* @​a, align 4
ret void
}

attributes #​0 = { "tune-cpu"="generic" }

!llvm.ident = !{#0}

!​0 = !{!"clang version 13.0.0"}

@rotateright
Copy link
Contributor

The bug appears to be in InstCombine's SimplifyDemandedBits of a select. We may have conflicting motives in changing a select's constant operands into the least set bits vs. matching a cmp's operand (another min/max hack that could disappear with canonicalization to intrinsics).

So we're ping-ponging between:
IC: Visiting: %sub11 = select i1 %cmp9, i32 1, i32 6
IC: Visiting: %sub11 = select i1 %cmp9, i32 1, i32 4

I think there's a small fix to avoid that - will try to reduce further and confirm.

@rotateright
Copy link
Contributor

This should fix the bug in trunk:
https://reviews.llvm.org/rG9502061bcc86

I'm marking this as blocking the 12.0 release to see if we can still get it in there.

As noted in the description, this was visible in the 11.0 release, but not before that. The bug was likely introduced with:
https://reviews.llvm.org/rGa59cc5e128f09ec

@tstellar
Copy link
Collaborator

tstellar commented Mar 3, 2021

Merged: 692808e

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla
Projects
None yet
Development

No branches or pull requests

4 participants