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

[LVI] Miscompile by CorrelatedValuePropagation: LVI doesn't take nsw into account

Open DaniilSuchkov opened this issue 7 years ago • 4 comments

Bugzilla Link 43802
Resolution FIXED
Resolved on Oct 28, 2019 06:18
Version trunk
OS Linux
CC @fhahn,@LebedevRI,@nikic,@nunoplopes,@regehr,@rotateright

Extended Description

Consider following code: define i32 @​foo(i32 %arg) { bb: %tmp = sub nsw i32 0, %arg %tmp1 = icmp eq i32 %arg, -2147483648 br i1 %tmp1, label %bb2, label %bb3

bb2: ; preds = %bb br label %bb3

bb3: ; preds = %bb2, %bb %tmp4 = phi i32 [ -2147483648, %bb2 ], [ %tmp, %bb ] ret i32 %tmp4 }

This cmd: opt -correlated-propagation -S repro.ll

Produces following result: define i32 @​foo(i32 %arg) { bb: %tmp = sub nsw i32 0, %arg %tmp1 = icmp eq i32 %arg, -2147483648 br i1 %tmp1, label %bb2, label %bb3

bb2: ; preds = %bb br label %bb3

bb3: ; preds = %bb2, %bb ret i32 %tmp }

Before the transformation result of foo was defined for all %arg values, after the transformation result of foo when %arg == MIN_INT is undef.

This regression was introduced by following commit: commit 3b6d46761f139e7ee3057758698efa4487323829 Author: Sanjay Patel [email protected] Date: Tue Apr 10 20:42:39 2018 +0000

[CVP] simplify phi with constant incoming values that match common variable edge values

This is based on an example that was recently posted on llvm-dev:

void *propagate_null(void* b, int* g) {
  if (!b) {
    return 0;
  }
  (*g)++;
  return b;
}

https://godbolt.org/g/xYk3qG

The original code or constant propagation in other passes has obscured the fact
that the phi can be removed completely.

Differential Revision: https://reviews.llvm.org/D45448

llvm-svn: 329755

The transformation introduced by the commit replaces phi when all incoming values except one are constants and that one non-constant incoming value is equal to every constant when coming over the corresponding edge. To check this condition it queries LazyValueInfo::getConstantOnEdge for the non-constant value and the incoming edge and compares the result with the corresponding incoming constant. It appears that LazyValueInfo::getConstantOnEdge doesn't take the nsw flag into account.

DaniilSuchkov avatar Oct 25 '19 08:10 DaniilSuchkov

Should be minimally fixed with: https://reviews.llvm.org/rGf2e93d10fe0c

As noted in https://reviews.llvm.org/D69442 - the patch appears to be sufficient given current LVI behavior. If that analysis changes, we might need a different/stronger fix. We can also try to restore the wrapping flags with further analysis.

rotateright avatar Oct 28 '19 13:10 rotateright

fix seems reasonable, Sanjay.

I don't really remember what LVI does for these flags, but I expect it's the usual "returns sound result for the non-poison case" in which case fixing LVI isn't really a good option here?

regehr avatar Oct 25 '19 19:10 regehr

Proposal: https://reviews.llvm.org/D69442

rotateright avatar Oct 25 '19 19:10 rotateright

The bug is real, but I'm not sure how to solve it yet. Drop all poison-generating flags for the propagated value when performing this transform in CVP?

cc'ing some more poison-knowledgeable people.

rotateright avatar Oct 25 '19 18:10 rotateright