Skip to content

Commit 600d1a3

Browse files
committed
[SCEV] Fix BinomialCoefficient Iteration to fit in W bits
BinomialCoefficient computes the value of W-bit IV at iteration It of a loop. When W is 1, we can call multiplicative inverse on 0 which triggers an assert since 1b76120. Since the arithmetic is supposed to wrap if It or K does not fit in W bits, do the truncation into W bits after we do the shift. Fixes #87798
1 parent f46f646 commit 600d1a3

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,9 @@ static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
928928
APInt OddFactorial(W, 1);
929929
unsigned T = 1;
930930
for (unsigned i = 3; i <= K; ++i) {
931-
APInt Mult(W, i);
932-
unsigned TwoFactors = Mult.countr_zero();
931+
unsigned TwoFactors = countr_zero(i);
933932
T += TwoFactors;
934-
Mult.lshrInPlace(TwoFactors);
935-
OddFactorial *= Mult;
933+
OddFactorial *= (i >> TwoFactors);
936934
}
937935

938936
// We need at least W + T bits for the multiplication step
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
; RUN: opt -disable-output -passes='print<scalar-evolution>' -verify-scev < %s 2>&1 | FileCheck %s
2+
3+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
; print<scalar-evolution> is used to compute SCEVs for all values in the
7+
; function.
8+
; We should not crash on multiplicative inverse called within SCEV's binomial
9+
; coefficient function.
10+
11+
; CHECK: Classifying expressions for: @pr87798
12+
; CHECK: %phi = phi i32 [ 0, %bb ], [ %add4, %bb1 ]
13+
; CHECK-NEXT: --> {0,+,0,+,0,+,2,+,3}<%bb1> U: full-set S: full-set Exits: 0 LoopDispositions: { %bb1: Computable }
14+
15+
; CHECK-NEXT: %phi2 = phi i32 [ 0, %bb ], [ %add, %bb1 ]
16+
; CHECK-NEXT: --> {0,+,0,+,1}<%bb1> U: full-set S: full-set Exits: 0 LoopDispositions: { %bb1: Computable }
17+
; CHECK-NEXT: %phi3 = phi i32 [ 0, %bb ], [ %add5, %bb1 ]
18+
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%bb1> U: [0,1) S: [0,1) Exits: 0 LoopDispositions: { %bb1: Computable }
19+
; CHECK-NEXT: %add = add i32 %phi2, %phi3
20+
; CHECK-NEXT: --> {0,+,1,+,1}<%bb1> U: full-set S: full-set Exits: 0 LoopDispositions: { %bb1: Computable }
21+
; CHECK-NEXT: %mul = mul i32 %phi2, %phi3
22+
; CHECK-NEXT: --> {0,+,0,+,2,+,3}<%bb1> U: full-set S: full-set Exits: 0 LoopDispositions: { %bb1: Computable }
23+
; CHECK-NEXT: %add4 = add i32 %mul, %phi
24+
; CHECK-NEXT: --> {0,+,0,+,2,+,5,+,3}<%bb1> U: full-set S: full-set Exits: 0 LoopDispositions: { %bb1: Computable }
25+
; CHECK-NEXT: %and = and i32 %phi, 1
26+
; CHECK-NEXT: --> (zext i1 {false,+,false,+,false,+,false,+,true}<%bb1> to i32) U: [0,2) S: [0,2) Exits: 0 LoopDispositions: { %bb1: Computable }
27+
; CHECK-NEXT: %add5 = add i32 %phi3, 1
28+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%bb1> U: [1,2) S: [1,2) Exits: 1 LoopDispositions: { %bb1: Computable }
29+
; CHECK-NEXT: %phi9 = phi i32 [ %and, %bb1 ]
30+
; CHECK-NEXT: --> (zext i1 {false,+,false,+,false,+,false,+,true}<%bb1> to i32) U: [0,2) S: [0,2) --> 0 U: [0,1) S: [0,1)
31+
; CHECK-NEXT: %zext = zext i32 %phi9 to i64
32+
; CHECK-NEXT: --> poison U: full-set S: full-set
33+
34+
define i32 @pr87798() {
35+
bb:
36+
br label %bb1
37+
38+
bb1: ; preds = %bb1, %bb
39+
%phi = phi i32 [ 0, %bb ], [ %add4, %bb1 ]
40+
%phi2 = phi i32 [ 0, %bb ], [ %add, %bb1 ]
41+
%phi3 = phi i32 [ 0, %bb ], [ %add5, %bb1 ]
42+
%add = add i32 %phi2, %phi3
43+
%mul = mul i32 %phi2, %phi3
44+
%add4 = add i32 %mul, %phi
45+
%and = and i32 %phi, 1
46+
%add5 = add i32 %phi3, 1
47+
br i1 true, label %preheader, label %bb1
48+
49+
preheader: ; preds = %bb1
50+
%phi9 = phi i32 [ %and, %bb1 ]
51+
br label %loop
52+
53+
loop: ; preds = %preheader, %loop
54+
br label %loop
55+
56+
bb7: ; No predecessors!
57+
%zext = zext i32 %phi9 to i64
58+
ret i32 0
59+
}

0 commit comments

Comments
 (0)