llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

wrong code from the StructurizeCFG pass (-structurizecfg)

Open zhendongsu opened this issue 10 years ago • 4 comments

Bugzilla Link 31076
Resolution FIXED
Resolved on Apr 29, 2020 13:22
Version trunk
OS All
Blocks llvm/llvm-project#28112
Attachments bitcode before "opt -structurizecfg", bitcode after "opt -structurizecfg"
CC @bjope,@ekatz,@arsenm,@tstellar

Extended Description

$ clang -v clang version 4.0.0 (trunk 287325) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /usr/local/clang-trunk/bin Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9 Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9.4 Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5 Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5.3.0 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.4 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.4.7 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6.4 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.3 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.5 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.4 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.3.0 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.2.0 Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9 Candidate multilib: .;@m64 Candidate multilib: 32;@m32 Candidate multilib: x32;@mx32 Selected multilib: .;@m64 $ $ clang -c -emit-llvm -o small.bc small.c $ opt -structurizecfg -o small-opt.bc small.bc $ $ clang small.bc; ./a.out 1 $ clang small-opt.bc; ./a.out 0 $ $ cat small.c int printf (const char *, ...);

int a, b, c, d;

void fn1 () { while (b) { if (c) return; while (d) ; } a = 1; }

int main () { fn1 (); printf ("%d\n", a); return 0; } $

zhendongsu avatar Nov 20 '16 06:11 zhendongsu

mentioned in issue llvm/llvm-bugzilla-archive#31240

Artem-B avatar Nov 26 '21 22:11 Artem-B

The testcase is still reproducible in LLVM 6.0, but fixed in LLVM 7.1.

ekatz avatar Apr 29 '20 20:04 ekatz

I think I stumbled on the same problem when trying to use structurizecfg in order to make the input to our backend (non AMDGPU) a little bit more fuzzy.

Anyway, this is a miscompile, so I guess someone at AMD should be interested.

Here is my reproducer (llvm-lit compatible):

;----------------------------------------------------------------------- ; RUN: opt < %s -S -structurizecfg -O1 | FileCheck %s ; RUN: opt < %s -S -O1 | FileCheck %s

; CHECK: ret i16 15{{$}}

; The program corresponds to: ; ; i = start & 0; ; while (i < 20) ; { ; i++; ; if (i == 0) ; { ; continue; ; } ; if (i == 15) ; { ; break; ; } ; } ; return i; ; ; So for expected return value is 15.

define i16 @​foo(i16 %start) { entry: %i.start = and i16 %start, 0 br label %while.cond

while.cond: ; preds = %while.body, %entry %i.0 = phi i16 [ %i.start, %entry ], [ %inc, %while.body ] %cmp.old = icmp slt i16 %i.0, 20 br i1 %cmp.old, label %while.body, label %while.end

while.body: ; preds = %if.end, %while.cond %i.1 = phi i16 [ %i.0, %while.cond ], [ %inc, %if.end ] %inc = add nsw i16 %i.1, 1 %cmp1 = icmp eq i16 %inc, 0 br i1 %cmp1, label %while.cond, label %if.end

if.end: ; preds = %while.body %cmp2 = icmp ne i16 %inc, 15 %cmp = icmp slt i16 %inc, 20 %or.cond = and i1 %cmp2, %cmp br i1 %or.cond, label %while.body, label %while.end

while.end: ; preds = %if.end, %while.cond %i.2 = phi i16 [ %inc, %if.end ], [ %i.0, %while.cond ] ret i16 %i.2 }

;-----------------------------------------------------------------------

bjope avatar May 24 '18 15:05 bjope

This pass is used only by AMDGPU, cc:ing AMD people.

llvmbot avatar Nov 21 '16 00:11 llvmbot