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

InstCombine hangs (loops forever) at -O1 or higher...

Open cmtice opened this issue 10 years ago • 6 comments

Bugzilla Link 27703
Resolution FIXED
Resolved on May 26, 2016 16:24
Version trunk
OS Linux
Blocks llvm/llvm-project#24719
Attachments .i file to be compiled to reproduce the problem., debug output log
CC @weiguozhi,@majnemer,@hfinkel

Extended Description

On the function mem_benchmark_0 in this attached .i file, if I compile it a -O1 or -O2, clang/llvm goes into an infinite loop in inst combine. It appears to be adding/removing items from the worklist indefinitely, while trying to "combine redundant instructions". At -O0 it compiles in a matter of seconds. At -O2 I aborted it after 30 minutes (and I've seen it run for several hours before being aborted).

I'm attaching the .i file and also about 10 seconds worth of output from passing '-mllvm -debug-only=instcombine' to the compile process.

The full command to compile this is:

clang-3.9 -cc1 -triple x86_64-cros-linux-gnu -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lib_mem.c -mrelocation-model pic -pic-level 2 -pie-level 2 -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -momit-leaf-frame-pointer -dwarf-column-info -debugger-tuning=gdb -D _FORTIFY_SOURCE=2 -D RUSAGE -D HAVE_uint=1 -D HAVE_int64_t=1 -D HAVE_pmap_clnt_h -D HAVE_socklen_t -D HAVE_DRAND48 -D HAVE_SCHED_SETAFFINITY=1 -O2 -ferror-limit 19 -fmessage-length 83 -stack-protector 2 -o lib_mem.o -x c lib_mem.i

This is a regression, as a 3.8 version of clang that we have works just fine on this file.

cmtice avatar May 10 '16 21:05 cmtice

Fixed by r270135.

weiguozhi avatar May 26 '16 23:05 weiguozhi

After load combine, bb6 is changed to

bb6: ; preds = %bb6, %bb %.0 = phi i8** [ undef, %bb ], [ %tmp1, %bb6 ] %0 = bitcast i8** %.0 to i8*** %tmp1 = load i8**, i8*** %0, align 8 br label %bb6

It triggers optimizeBitCastFromPhi,

bb6: ; preds = %bb6, %bb %0 = phi i8*** [ undef, %bb ], [ %1, %bb6 ] %tmp1 = load i8**, i8*** %0, align 8 %1 = bitcast i8** %tmp1 to i8*** br label %bb6

Then it is transferred to,

bb6: ; preds = %bb6, %bb %.0 = phi i8*** [ undef, %bb ], [ %tmp1, %bb6 ] %0 = bitcast i8*** %.0 to i8**** %tmp1 = load i8***, i8**** %0, align 8 br label %bb6

...

The key property that causes the indefinite loop is the loaded value used as the address of the same instruction, in this situation one bitcast is required to change the value type to the pointer type, we should not try to optimize out the the bitcast.

Will prepare the patch soon.

weiguozhi avatar May 10 '16 23:05 weiguozhi

investigating...

weiguozhi avatar May 10 '16 22:05 weiguozhi

Revert r263734 fixes it.

Guozhi Wei, please take a look.

majnemer avatar May 10 '16 21:05 majnemer

Reduced: target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-cros-linux-gnu"

define void @​mem_benchmark_0() { bb: br label %bb6

bb6: ; preds = %bb5 %.0 = phi i8** [ undef, %bb ], [ %bc, %bb6 ] %tmp = load i8*, i8** %.0, align 8 %bc = bitcast i8* %tmp to i8** br label %bb6

bb206: ; preds = %bb5 ret void }

run with -instcombine to reproduce...

majnemer avatar May 10 '16 21:05 majnemer

Taking a look.

majnemer avatar May 10 '16 21:05 majnemer