Skip to content

Commit caa32e6

Browse files
authored
[llvm][LSR] Fix where invariant on ScaledReg & Scale is violated (#112576)
Comments attached to the `ScaledReg` field of `struct Formula` explains that, `ScaledReg` must be non-null when `Scale` is non-zero. This fixes up a code path where this invariant is violated. Also, add an assert to ensure this invariant holds true. Without this patch, compiler aborts with the attached test case. Fixes #76504
1 parent 8b6764f commit caa32e6

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,9 @@ static bool containsAddRecDependentOnLoop(const SCEV *S, const Loop &L) {
622622
/// representation.
623623
/// \see Formula::BaseRegs.
624624
bool Formula::isCanonical(const Loop &L) const {
625+
assert((Scale == 0 || ScaledReg) &&
626+
"ScaledReg must be non-null if Scale is non-zero");
627+
625628
if (!ScaledReg)
626629
return BaseRegs.size() <= 1;
627630

@@ -3973,9 +3976,10 @@ void LSRInstance::GenerateReassociationsImpl(LSRUse &LU, unsigned LUIdx,
39733976
F.UnfoldedOffset =
39743977
Immediate::getFixed((uint64_t)F.UnfoldedOffset.getFixedValue() +
39753978
InnerSumSC->getValue()->getZExtValue());
3976-
if (IsScaledReg)
3979+
if (IsScaledReg) {
39773980
F.ScaledReg = nullptr;
3978-
else
3981+
F.Scale = 0;
3982+
} else
39793983
F.BaseRegs.erase(F.BaseRegs.begin() + Idx);
39803984
} else if (IsScaledReg)
39813985
F.ScaledReg = InnerSum;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; Reduced from https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=65323 by @RKSimon
2+
;
3+
; RUN: opt -S -passes=loop-reduce %s | FileCheck %s
4+
;
5+
; Make sure we don't trigger an assertion.
6+
7+
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"
8+
target triple = "x86_64-unknown-linux-gnu"
9+
10+
@G = external global i32
11+
12+
define void @foo() {
13+
; CHECK-LABEL: foo
14+
bb8:
15+
br label %bb30
16+
17+
bb30: ; preds = %bb30, %bb8
18+
%l0 = phi i64 [ -2222, %bb8 ], [ %r23, %bb30 ]
19+
%A22 = alloca i16, align 2
20+
%r23 = add nuw i64 1, %l0
21+
%G7 = getelementptr i16, ptr %A22, i64 %r23
22+
%B15 = urem i64 %r23, %r23
23+
%G6 = getelementptr i16, ptr %G7, i64 %B15
24+
%B1 = urem i64 %r23, %r23
25+
%B8 = sub i64 -1, %r23
26+
%B18 = sub i64 %B8, %B1
27+
%G5 = getelementptr i16, ptr %G6, i64 %B18
28+
store ptr %G5, ptr undef, align 8
29+
br label %bb30
30+
}

0 commit comments

Comments
 (0)