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

wrong code with "opt -instcombine -early-cse -jump-threading -correlated-propagation -loop-rotate -indvars -gvn"

Open zhendongsu opened this issue 10 years ago • 17 comments

Bugzilla Link 31181
Resolution FIXED
Resolved on Jun 29, 2019 09:35
Version trunk
OS All
Attachments bitcode before optimization, bitcode after optimization
CC @arpilipe,@zmodem,@hfinkel,@nikic,@nunoplopes,@zygoloid,@rlougher,@sanjoy
Fixed by commit(s) 362292, 364228

Extended Description

$ clang -v clang version 4.0.0 (trunk 287975) 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 -instcombine -early-cse -jump-threading -correlated-propagation -loop-rotate -indvars -gvn -o small-opt.bc small.bc $ llc -o small.s small-opt.bc $ gcc small.s $ timeout -s 9 10 ./a.out Killed $ $ clang -O0 small.c $ ./a.out $


int a;

int main () { for (a = -2; a < -1; a++) ; return 0; }

zhendongsu avatar Nov 28 '16 09:11 zhendongsu

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

rlougher avatar Nov 26 '21 22:11 rlougher

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

zmodem avatar Nov 26 '21 22:11 zmodem

LFTR issue fixed in https://reviews.llvm.org/rL362292 and CVP nowrap inference reenabled in https://reviews.llvm.org/rL364228.

nikic avatar Jun 29 '19 16:06 nikic

Attempt at fixing this: https://reviews.llvm.org/D60935

nikic avatar Apr 20 '19 12:04 nikic

For reference, the full repro IR for -indvars is:

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

@​a = global i32 0

define i32 @​main() { entry: br label %loop

loop:
%storemerge = phi i32 [ -2, %entry ], [ %inc, %loop ] store i32 %storemerge, i32* @​a %cmp = icmp slt i32 %storemerge, -1 %inc = add nuw nsw i32 %storemerge, 1 br i1 %cmp, label %loop, label %exit

exit:
ret i32 0 }

I initially thought this no longer reproduces, because I habitually dropped the datalayout, but apparently it's important here.

nikic avatar Apr 20 '19 09:04 nikic

Has there been any progress here?

zmodem avatar Aug 04 '17 23:08 zmodem

*** Bug llvm/llvm-bugzilla-archive#32047 has been marked as a duplicate of this bug. ***

rlougher avatar Feb 27 '17 18:02 rlougher

So my two cents would be that despite this being a bug in IndVarSimplify, we cherry-pick a revert of Artur's CVP change into the release branch for now.

Sounds good to me. I've reverted in r296030 and will merge that to the branch once it's been on the buildbots a bit.

Merged the revert to 4.0 in r296148.

zmodem avatar Feb 24 '17 18:02 zmodem

So my two cents would be that despite this being a bug in IndVarSimplify, we cherry-pick a revert of Artur's CVP change into the release branch for now.

Sounds good to me. I've reverted in r296030 and will merge that to the branch once it's been on the buildbots a bit.

zmodem avatar Feb 23 '17 22:02 zmodem

Since nsw, nuw flags can be control dependant, probably we should treat them similarly to metadata and strip them when we speculate the instruction.

As far as I can see, LoopRotate's speculation is fine by itself, but indvars should presuambly be stripping nsw and nuw when reusing an existing add instruction.

Hmm, when you say it's fine by itself you mean as long as the value isn't used until the loop condition has been checked, as it might be poison, right?

Only indvars does exactly that: uses the poison value.

I think the transformation happens in IndVarSimplify::linearFunctionTestReplace. We could of course check if CmpIndVar is an add and strip the flags.

But what if CmpIndVar is poison for some other reason, or has passed through other instructions. Can we "un-poison" it in a safe manner somehow?

That is what I'm working on now, but doing that without regressing other places (in terms of performance) is probably going to be too risky to pull in to 4.0.

So my two cents would be that despite this being a bug in IndVarSimplify, we cherry-pick a revert of Artur's CVP change into the release branch for now.

sanjoy avatar Feb 23 '17 22:02 sanjoy

Since nsw, nuw flags can be control dependant, probably we should treat them similarly to metadata and strip them when we speculate the instruction.

As far as I can see, LoopRotate's speculation is fine by itself, but indvars should presuambly be stripping nsw and nuw when reusing an existing add instruction.

Hmm, when you say it's fine by itself you mean as long as the value isn't used until the loop condition has been checked, as it might be poison, right?

Only indvars does exactly that: uses the poison value.

I think the transformation happens in IndVarSimplify::linearFunctionTestReplace. We could of course check if CmpIndVar is an add and strip the flags.

But what if CmpIndVar is poison for some other reason, or has passed through other instructions. Can we "un-poison" it in a safe manner somehow?

zmodem avatar Feb 23 '17 22:02 zmodem

*** Bug llvm/llvm-bugzilla-archive#31325 has been marked as a duplicate of this bug. ***

zmodem avatar Feb 16 '17 21:02 zmodem

Since nsw, nuw flags can be control dependant, probably we should treat them similarly to metadata and strip them when we speculate the instruction.

As far as I can see, LoopRotate's speculation is fine by itself, but indvars should presuambly be stripping nsw and nuw when reusing an existing add instruction.

CVP correctly marks the add as nuw basing on the loop latch check. After CVP:

define i32 @​main() #​0 { entry: br label %for.cond

for.cond: ; preds = %for.inc, %entry %storemerge = phi i32 [ -2, %entry ], [ %inc, %for.inc ] store i32 %storemerge, i32* @​a, align 4 %cmp = icmp slt i32 %storemerge, -1 br i1 %cmp, label %for.inc, label %for.end

for.inc: ; preds = %for.cond %inc = add nuw nsw i32 %storemerge, 1 br label %for.cond

for.end: ; preds = %for.cond ret i32 0 }

The problem is that LoopRotation later moves the addition over the check which guarantees nuw:

for.cond: ; preds = %for.cond, %entry %storemerge = phi i32 [ -2, %entry ], [ %inc, %for.cond ] store i32 %storemerge, i32* @​a, align 4 %cmp = icmp slt i32 %storemerge, -1 %inc = add nuw nsw i32 %storemerge, 1 br i1 %cmp, label %for.cond, label %for.end

Then indvars transforms the loop latch from pre increment check to the post increment check:

for.cond: ; preds = %for.cond, %entry %storemerge = phi i32 [ -2, %entry ], [ %inc, %for.cond ] store i32 %storemerge, i32* @​a, align 4 %inc = add nuw nsw i32 %storemerge, 1 %exitcond = icmp ne i32 %inc, 0 br i1 %exitcond, label %for.cond, label %for.end

The check is never false because of nuw flag.

Since nsw, nuw flags can be control dependant, probably we should treat them similarly to metadata and strip them when we speculate the instruction.

arpilipe avatar Feb 03 '17 13:02 arpilipe

Bisection points to:

r282872 - CVP. Turn marking adds as no wrap on by default (was turned off by 279082)

Artur, can you take a look?

zmodem avatar Feb 03 '17 02:02 zmodem

Looks like a 3.9 -> 4.0 regression.

zmodem avatar Jan 31 '17 01:01 zmodem

assigned to @arpilipe

zhendongsu avatar Nov 28 '16 09:11 zhendongsu