[llvm] [DA] use NSW arithmetic (PR #116632)
Sebastian Pop via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 18 06:27:20 PST 2024
https://github.com/sebpop created https://github.com/llvm/llvm-project/pull/116632
DA uses SCEV to solve linear constraints. When it generates SCEVs with negative strides, i.e., {0,+,-1}, make sure the SCEVs are marked as non wrap arithmetic. This patch fixes https://github.com/llvm/llvm-project/issues/51512
>From 2acdebf852b910ad27e7c9f0689e844c3ff99247 Mon Sep 17 00:00:00 2001
From: Sebastian Pop <spop at nvidia.com>
Date: Fri, 15 Nov 2024 01:04:58 +0000
Subject: [PATCH] [DA] use NSW arithmetic
DA uses SCEV to solve linear constraints. When it generates SCEVs with negative
strides, i.e., {0,+,-1}, make sure the SCEVs are marked as non wrap arithmetic.
This patch fixes https://github.com/llvm/llvm-project/issues/51512
---
llvm/lib/Analysis/DependenceAnalysis.cpp | 10 +++---
.../Analysis/DependenceAnalysis/PR51512.ll | 34 +++++++++++++++++++
2 files changed, 38 insertions(+), 6 deletions(-)
create mode 100644 llvm/test/Analysis/DependenceAnalysis/PR51512.ll
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index a4a98ea0bae146..f08e4325a0bc75 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -3116,11 +3116,9 @@ const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
const Loop *TargetLoop,
const SCEV *Value) const {
const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
- if (!AddRec) // create a new addRec
- return SE->getAddRecExpr(Expr,
- Value,
- TargetLoop,
- SCEV::FlagAnyWrap); // Worst case, with no info.
+ if (!AddRec)
+ return SE->getAddRecExpr(Expr, Value, TargetLoop, SCEV::FlagNSW);
+
if (AddRec->getLoop() == TargetLoop) {
const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
if (Sum->isZero())
@@ -3131,7 +3129,7 @@ const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
AddRec->getNoWrapFlags());
}
if (SE->isLoopInvariant(AddRec, TargetLoop))
- return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
+ return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagNSW);
return SE->getAddRecExpr(
addToCoefficient(AddRec->getStart(), TargetLoop, Value),
AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
diff --git a/llvm/test/Analysis/DependenceAnalysis/PR51512.ll b/llvm/test/Analysis/DependenceAnalysis/PR51512.ll
new file mode 100644
index 00000000000000..52419adff8f8b5
--- /dev/null
+++ b/llvm/test/Analysis/DependenceAnalysis/PR51512.ll
@@ -0,0 +1,34 @@
+; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa
+
+; Check that the testcase does not crash the compiler.
+; https://github.com/llvm/llvm-project/issues/51512
+
+define void @func_1() {
+entry:
+ %l_83.i.i = alloca [2 x [5 x i32]], align 1
+ br label %for.cond857.preheader.i.i
+
+for.cond857.preheader.i.i: ; preds = %cleanup.cont1138.i.i, %entry
+ %l_89.08.i.i = phi i32 [ 0, %entry ], [ %add1140.i.i, %cleanup.cont1138.i.i ]
+ %0 = trunc i32 %l_89.08.i.i to i16
+ %1 = add i16 %0, 3
+ %arrayidx916.i.i = getelementptr inbounds [2 x [5 x i32]], [2 x [5 x i32]]* %l_83.i.i, i16 0, i16 %0, i16 %1
+ br label %for.body860.i.i
+
+for.body860.i.i: ; preds = %for.body860.i.i, %for.cond857.preheader.i.i
+ %l_74.07.i.i = phi i32 [ 0, %for.cond857.preheader.i.i ], [ %add964.i.i, %for.body860.i.i ]
+ store i32 undef, i32* %arrayidx916.i.i, align 1
+ %2 = trunc i32 %l_74.07.i.i to i16
+ %arrayidx962.i.i = getelementptr inbounds [2 x [5 x i32]], [2 x [5 x i32]]* %l_83.i.i, i16 0, i16 %2, i16 %1
+ store i32 0, i32* %arrayidx962.i.i, align 1
+ %add964.i.i = add nuw nsw i32 %l_74.07.i.i, 1
+ br i1 false, label %for.body860.i.i, label %cleanup.cont1138.i.i
+
+cleanup.cont1138.i.i: ; preds = %for.body860.i.i
+ %add1140.i.i = add nuw nsw i32 %l_89.08.i.i, 1
+ %cmp602.i.i = icmp eq i32 %l_89.08.i.i, 0
+ br i1 %cmp602.i.i, label %for.cond857.preheader.i.i, label %for.cond1480.i.i.preheader
+
+for.cond1480.i.i.preheader: ; preds = %cleanup.cont1138.i.i
+ unreachable
+}
More information about the llvm-commits
mailing list