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

[Indvars] Incorrect hoisting of udiv/urem

Open serguei-katkov opened this issue 9 years ago • 10 comments

Bugzilla Link 35406
Resolution FIXED
Resolved on Dec 14, 2017 22:21
Version trunk
OS Windows NT
CC @hfinkel

Extended Description

Indvars does an incorrect transformation for the following reproducer:

; ModuleID = 'repro.ll' ;target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:1" target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128:1" target triple = "x86_64-unknown-linux-gnu"

define i32 @​test(i8* %p, i64* %p1) { entry: br label %loop1

loop1: %local_0_ = phi i32 [ 8, %entry ], [ %9, %loop2.exit ] %local_2_ = phi i32 [ 63864, %entry ], [ %local_2_43, %loop2.exit ] %local_3_ = phi i32 [ 51, %entry ], [ %local_3_44, %loop2.exit ] %0 = udiv i32 14, %local_0_ %1 = icmp ugt i32 %local_0_, 14 br i1 %1, label %exit, label %general_case24

general_case24: ; %2 = urem i32 60392, %0 %2 = udiv i32 60392, %0 br i1 false, label %loop2, label %loop2.exit

loop2: %local_1_56 = phi i32 [ %2, %general_case24 ], [ %3, %loop2 ] %local_2_57 = phi i32 [ 1, %general_case24 ], [ %7, %loop2 ] %3 = add i32 %local_1_56, -1 %4 = load i64* %p1, align 8 ; %4 = load atomic i64, i64* %p1 unordered, align 8 %5 = sext i32 %3 to i64 %6 = sub i64 %4, %5 ; store atomic i64 %6, i64* %p1 unordered, align 8 store i64 %6, i64* %p1, align 8 %7 = add nuw nsw i32 %local_2_57, 1 %8 = icmp ugt i32 %local_2_57, 7 br i1 %8, label %loop2.exit, label %loop2

loop2.exit: %local_2_43 = phi i32 [ %local_2_, %general_case24 ], [ 9, %loop2 ] %local_3_44 = phi i32 [ %local_3_, %general_case24 ], [ %local_1_56, %loop2 ] %9 = add nuw nsw i32 %local_0_, 1 %10 = icmp ugt i32 %local_0_, 129 br i1 %10, label %exit, label %loop1

exit: ret i32 0 }

opt --indvars -S repro.ll results in

... loop1: ; preds = %loop2.exit, %entry %indvars.iv = phi i32 [ %indvars.iv.next, %loop2.exit ], [ 8, %entry ] %0 = udiv i32 14, %indvars.iv %1 = udiv i32 60392, %0 %2 = zext i32 %1 to i64 %3 = udiv i32 14, %indvars.iv %4 = icmp ugt i32 %indvars.iv, 14 br i1 %4, label %exit, label %general_case24 ...

%1 = udiv i32 60392, %0 instruction causes a division by zero in case %indvars.iv == 15.

serguei-katkov avatar Nov 24 '17 05:11 serguei-katkov

Fix is landed as rL320789.

serguei-katkov avatar Dec 15 '17 06:12 serguei-katkov

Posted a fix on the review: https://reviews.llvm.org/D41229

Please take a look.

serguei-katkov avatar Dec 14 '17 11:12 serguei-katkov

It seems that the similar problem was tried to be fixed in: commit b226b5d5c0ca4a2475b6f3a92b6c6c44a1992453 Author: Sebastian Pop [email protected] Date: Mon Dec 12 02:52:51 2016 +0000

[SCEVExpand] do not hoist divisions by zero (#30935 )

SCEVExpand computes the insertion point for the components of a SCEV to be code
generated.  When it comes to generating code for a division, SCEVexpand would
not be able to check (at compilation time) all the conditions necessary to avoid
a division by zero.  The patch disables hoisting of expressions containing
divisions by anything other than non-zero constants in order to avoid hoisting
these expressions past conditions that should hold before doing the division.

The patch passes check-all on x86_64-linux.

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

but has been reverted by Author: Reid Kleckner [email protected] Date: Mon Dec 12 18:52:32 2016 +0000

Revert "[SCEVExpand] do not hoist divisions by zero (#30935 )"

Reverts r289412. It caused an OOB PHI operand access in instcombine when
ASan is enabled. Reduction in progress.

Also reverts "[SCEVExpander] Add a test case related to r289412"

serguei-katkov avatar Nov 29 '17 11:11 serguei-katkov

Commenting out the if in comment 5 or adding "&& isSafeToExpand(S, SE)" into if condition fixes the bug.

Sanjoy, if you advice me what is the best way to fix it, I can prepare a patch...

serguei-katkov avatar Nov 29 '17 11:11 serguei-katkov

What I do not like here is: Value *SCEVExpander::expand(const SCEV *S) { ... // If the SCEV is computable at this level, insert it into the header // after the PHIs (and after any other instructions that we've inserted // there) so that it is guaranteed to dominate any user inside the loop. if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L)) InsertPt = &*L->getHeader()->getFirstInsertionPt();

So it is not clear what is the reason to move InsertPt to LoopHeader? At least we must ensure that it is safe (does not traverse over any guard).

serguei-katkov avatar Nov 29 '17 11:11 serguei-katkov

Hi Sanjoy, thank you for response.

I will probably will try to fix it earlier next week or so. So it is not urgent but good to be fixed :)

Let's do it in this way: you or I will look into this. Don't worry a lot about it, please.

Will it work for you?

serguei-katkov avatar Nov 29 '17 07:11 serguei-katkov

Hi Serguei,

I can't commit to looking at this soon. If you're blocked on this bug then I suggest you go ahead and propose a fix (I'm happy to review).

Otherwise if you're okay waiting a bit (few weeks ish) then keeping this assigned to me is fine.

sanjoy avatar Nov 29 '17 07:11 sanjoy

My understanding of the issue as follows: indvars tries to wide the %local_1_56. It is a phi node, it needs to wide incoming values, specifically %2 = udiv i32 60392, %0

It correctly does it but it places the widen code to BB loops not general_case24 and this causes a bug due to udiv is moved over guard represented by %1 = icmp ugt i32 %local_0_, 14

so in general I do not think that Sanjoy's change causes the issue itself but I assigned it to Sanjoy formally due to issue becomes to be reproducible since his change.

serguei-katkov avatar Nov 27 '17 04:11 serguei-katkov

It seems the bug is caused by the patch: commit 65f2077c6246534bb5d89fc2d205ef5de582549b Author: Sanjoy Das [email protected] Date: Fri Oct 10 21:22:34 2014 +0000

This patch teaches ScalarEvolution to pick and use !range metadata.
It also makes it more aggressive in querying range information by
adding a call to isKnownPredicateWithRanges to
isLoopBackedgeGuardedByCond and isLoopEntryGuardedByCond.

phabricator: http://reviews.llvm.org/D5638

Reviewed by: atrick, hfinkel

So assign to Sanjoy (sorry:)).

Sanjoy, could you please take a look?

serguei-katkov avatar Nov 24 '17 05:11 serguei-katkov

assigned to @serguei-katkov

serguei-katkov avatar Nov 24 '17 05:11 serguei-katkov