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

[SCEV] computeBackedgeTakenCount() returns incorrect BackedgeTakenInfo

Open CongzheUalberta opened this issue 6 years ago • 7 comments

Bugzilla Link 48225
Resolution FIXED
Resolved on Nov 23, 2020 07:35
Version trunk
OS Linux
CC @LebedevRI

Extended Description

The IR that exposed this bug is the following.

; ModuleID = 'test.c' source_filename = "test.c" target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" target triple = "aarch64-unknown-linux-gnu"

@​a = common dso_local local_unnamed_addr global i32 0, align 4 @​b = common dso_local local_unnamed_addr global i32 0, align 4

; Function Attrs: minsize nofree norecurse nounwind optsize define dso_local i32 @​main() local_unnamed_addr { entry: %0 = load i32, i32* @​b, align 4 %cmp1 = icmp ne i32 %0, 0 %conv = zext i1 %cmp1 to i32 br label %for.cond

for.cond: ; preds = %for.body, %entry %storemerge = phi i32 [ 0, %entry ], [ %inc, %for.body ] %cmp = icmp ult i32 %storemerge, 2 br i1 %cmp, label %for.body, label %for.end

for.body: ; preds = %for.cond %cmp2 = icmp eq i32 %storemerge, %conv %tobool = icmp ne i32 %storemerge, 0 %or.cond = and i1 %tobool, %cmp2 %inc = add nuw nsw i32 %storemerge, 1 br i1 %or.cond, label %for.cond4.preheader, label %for.cond

for.cond4.preheader: ; preds = %for.body %storemerge.lcssa6 = phi i32 [ %storemerge, %for.body ] store i32 %storemerge.lcssa6, i32* @​a, align 4 br label %for.cond4

for.cond4: ; preds = %for.cond4.preheader, %for.cond4 br label %for.cond4

for.end: ; preds = %for.cond %storemerge.lcssa = phi i32 [ %storemerge, %for.cond ] store i32 %storemerge.lcssa, i32* @​a, align 4 ret i32 0 }

For the IR above, the BackedgeTakenInfo returned from ScalarEvolution::computeBackedgeTakenCount() is not correct. To be specific, the field MaxAndComplete of BackedgeTakenInfo is incorrect. Dumping its value using BackedgeTakenInfo.getMax()->dump() gives 1 meaning the computed backedge count is 1. However the true value of backedge count should be 2.

Further investigation shows that the bug is in function ScalarEvolution::computeExitLimitFromCondImpl(). When ExitCond is Instruction::AND, this function computes two ExitLimit objects for the two operands of AND, then does some post-processing and returns the result. In the example, consider the ExitCond %or.cond of BB for.body, its two operands are:

%tobool = icmp ne i32 %storemerge, 0 %cmp2 = icmp eq i32 %storemerge, %conv

The ExitLimit.MaxNotTaken computed from %tobool is 1. The ExitLimit.MaxNotTaken computed from %cmp2 is also 1. Therefore it determines that the MaxBECount for BB for.body is 1.

However, it does not consider the fact that the two operands of AND are sometimes not independent which results in the AND operator always evaluated to false. This is exactly the case in the example above: %conv = zext i1 %cmp1 to i32 so its range is [0,1] and at runtime it is actually 0. Thus %or.cond is always false, meaning we always take the backedge when exiting BB for.body.

I think in this case the MaxBECount computed from ScalarEvolution::computeExitLimitFromCondImpl() is supposed to be CouldNotCompute.

Suggested fix: in function ScalarEvolution::computeExitLimitFromCondImpl(), when ExitCond is Instruction::AND, check if the two operands are independent. If not, then directly return CouldNotCompute. To be specific, a simple check could consider catching the following case:

operand 1: cmp eq %var1, %var2 operand 2: cmp ne %var1, %var3 where the range of %var2 is a subset of the range of %var3.

This simple check can be or needs be generalized, but that's a simple first fix off the top of my head.

CongzheUalberta avatar Nov 19 '20 03:11 CongzheUalberta

Fix merged.

commit 48d7cc6ae23b0e5b1922457462d0f6e4582a1ae7 Author: Max Kazantsev [email protected] Date: Mon Nov 23 16:45:20 2020 +0700

[SCEV] Fix incorrect treatment of max taken count. llvm/llvm-project#47569 

SCEV makes a logical mistake when handling EitherMayExit in
case when both conditions must be met to exit the loop. The
mistake looks like follows: "if condition `A` fails within at most `X`

first iterations, and B fails within at most Y first iterations, then A & B fails at most within min (X, Y) first iterations". This is wrong, because both of them must fail at the same time.

Simple example illustrating this is following: we have an IV with step 1,
condition `A` = "IV is even", condition `B` = "IV is odd". Both `A` and

B will fail within first two iterations. But it doesn't mean that both of them will fail within first two first iterations at the same time, which would mean that IV is neither even nor odd at the same time within first 2 iterations.

We can only do so for known exact BE counts, but not for max.

Differential Revision: https://reviews.llvm.org/D91942
Reviewed By: nikic

Thanks for the fix!

CongzheUalberta avatar Nov 23 '20 15:11 CongzheUalberta

Fix merged.

commit 48d7cc6ae23b0e5b1922457462d0f6e4582a1ae7 Author: Max Kazantsev [email protected] Date: Mon Nov 23 16:45:20 2020 +0700

[SCEV] Fix incorrect treatment of max taken count. llvm/llvm-project#47569 

SCEV makes a logical mistake when handling EitherMayExit in
case when both conditions must be met to exit the loop. The
mistake looks like follows: "if condition `A` fails within at most `X` first
iterations, and `B` fails within at most `Y` first iterations, then `A & B`
fails at most within `min (X, Y)` first iterations". This is wrong, because
both of them must fail at the same time.

Simple example illustrating this is following: we have an IV with step 1,
condition `A` = "IV is even", condition `B` = "IV is odd". Both `A` and `B`
will fail within first two iterations. But it doesn't mean that both of them
will fail within first two first iterations at the same time, which would mean
that IV is neither even nor odd at the same time within first 2 iterations.

We can only do so for known exact BE counts, but not for max.

Differential Revision: https://reviews.llvm.org/D91942
Reviewed By: nikic

llvmbot avatar Nov 23 '20 09:11 llvmbot

Fix on review: https://reviews.llvm.org/D91942

llvmbot avatar Nov 23 '20 08:11 llvmbot

Thanks for pointing out this bug. The situation is worrysome indeed.

I don't think it can be solved as you propose, because it's impossible to prove that the operands are independent. Even if their subtrees have no single common element in their subtrees, the dependencies between them can be imposed by some dominating checks. We sure cannot analyze all of them, especially when the dependencies are sophisticated.

If I understand it correctly, the code I'm seeing in computeExitLimitFromCondImpl is just not valid when ExitIfTrue = true. In fact, this code says "if condition A becomes true within first N iterations, and condition B becomes true within first M iterations, then A & B becomes true within first min(N, M) iterations". This is not true for such simple conditions that "IV is even" and "IV is odd": each of them becomes true within first two iterations, but it doesn't mean that IV becomes odd and even at the same time within first two iterations.

I'll see if we can fix it other way than turning off support of ExitIfTrue = true for this case.

Thanks for the reply, and for providing relevant test cases. I do agree with your understanding.

I had been trying to find a way to prove if the operands of "AND/OR" are independent, but as you indicated, probably it is not a viable approach, I did not find relevant code in llvm that does it.

CongzheUalberta avatar Nov 23 '20 01:11 CongzheUalberta

Checked in tests as

commit 0c101c9cbcc70d7d7135d6ba4e34099a3a018832 Author: Max Kazantsev [email protected] Date: Fri Nov 20 15:57:44 2020 +0700

[Test] Add tests demonstrating a bug in SCEV, llvm/llvm-project#47569 

Slightly simplified version of original test reported by Congzhe Cao.

I'll see how to fix it.

llvmbot avatar Nov 20 '20 09:11 llvmbot

Slightly simplified test case & added its counterpart for "OR" operation:

define void @​test_01(i1 %boolcond) { entry: %conv = zext i1 %boolcond to i32 br label %loop

loop: %iv = phi i32 [ 0, %entry ], [ %inc, %backedge ] %cmp = icmp ult i32 %iv, 2 br i1 %cmp, label %backedge, label %for.end

backedge: %cond.false.on.first.iter = icmp ne i32 %iv, 0 %cond.false.on.second.iter = icmp eq i32 %iv, %conv %or.cond = and i1 %cond.false.on.first.iter, %cond.false.on.second.iter %inc = add nuw nsw i32 %iv, 1 br i1 %or.cond, label %exit, label %loop

exit: unreachable

for.end: ret void }

define void @​test_02(i1 %boolcond) { entry: %conv = zext i1 %boolcond to i32 br label %loop

loop: %iv = phi i32 [ 0, %entry ], [ %inc, %backedge ] %cmp = icmp ult i32 %iv, 2 br i1 %cmp, label %backedge, label %for.end

backedge: %cond.true.on.first.iter = icmp eq i32 %iv, 0 %cond.true.on.second.iter = icmp ne i32 %iv, %conv %or.cond = or i1 %cond.true.on.first.iter, %cond.true.on.second.iter %inc = add nuw nsw i32 %iv, 1 br i1 %or.cond, label %loop, label %exit

exit: unreachable

for.end: ret void }

llvmbot avatar Nov 20 '20 08:11 llvmbot

Thanks for pointing out this bug. The situation is worrysome indeed.

I don't think it can be solved as you propose, because it's impossible to prove that the operands are independent. Even if their subtrees have no single common element in their subtrees, the dependencies between them can be imposed by some dominating checks. We sure cannot analyze all of them, especially when the dependencies are sophisticated.

If I understand it correctly, the code I'm seeing in computeExitLimitFromCondImpl is just not valid when ExitIfTrue = true. In fact, this code says "if condition A becomes true within first N iterations, and condition B becomes true within first M iterations, then A & B becomes true within first min(N, M) iterations". This is not true for such simple conditions that "IV is even" and "IV is odd": each of them becomes true within first two iterations, but it doesn't mean that IV becomes odd and even at the same time within first two iterations.

I'll see if we can fix it other way than turning off support of ExitIfTrue = true for this case.

llvmbot avatar Nov 20 '20 07:11 llvmbot