[llvm] [DA] use NSW arithmetic (PR #116632)

Sebastian Pop via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 11:53:54 PST 2025


https://github.com/sebpop updated https://github.com/llvm/llvm-project/pull/116632

>From 11aaf58798b484f7a2aa00b2422b275b52ddad04 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

Revert part of
https://github.com/llvm/llvm-project/commit/c0661aeaf8daf371023cf5669be4bd9b428882d0
that modified the definition of what an affine subscript is.

This patch fixes https://github.com/llvm/llvm-project/issues/51512
---
 llvm/lib/Analysis/DependenceAnalysis.cpp      |  8 ----
 .../Analysis/DependenceAnalysis/PR51512.ll    | 44 +++++++++++++++++++
 2 files changed, 44 insertions(+), 8 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 6ce2875beeccac4..a92f90881be4949 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -981,14 +981,6 @@ bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
 
   const SCEV *Start = AddRec->getStart();
   const SCEV *Step = AddRec->getStepRecurrence(*SE);
-  const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
-  if (!isa<SCEVCouldNotCompute>(UB)) {
-    if (SE->getTypeSizeInBits(Start->getType()) <
-        SE->getTypeSizeInBits(UB->getType())) {
-      if (!AddRec->getNoWrapFlags())
-        return false;
-    }
-  }
   if (!isLoopInvariant(Step, LoopNest))
     return false;
   if (IsSrc)
diff --git a/llvm/test/Analysis/DependenceAnalysis/PR51512.ll b/llvm/test/Analysis/DependenceAnalysis/PR51512.ll
new file mode 100644
index 000000000000000..744ffea57a2e7d9
--- /dev/null
+++ b/llvm/test/Analysis/DependenceAnalysis/PR51512.ll
@@ -0,0 +1,44 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 \
+; RUN: | FileCheck %s
+
+; Check that the testcase does not crash the compiler.
+; See https://github.com/llvm/llvm-project/issues/51512 for details.
+
+define void @foo() {
+; CHECK-LABEL: 'foo'
+; CHECK-NEXT:  Src: store i32 42, ptr %getelementptr, align 1 --> Dst: store i32 42, ptr %getelementptr, align 1
+; CHECK-NEXT:    da analyze - consistent output [0 S]!
+; CHECK-NEXT:  Src: store i32 42, ptr %getelementptr, align 1 --> Dst: store i32 0, ptr %getelementptr5, align 1
+; CHECK-NEXT:    da analyze - output [0 *|<]!
+; CHECK-NEXT:  Src: store i32 0, ptr %getelementptr5, align 1 --> Dst: store i32 0, ptr %getelementptr5, align 1
+; CHECK-NEXT:    da analyze - none!
+;
+bb:
+  %alloca = alloca [2 x [5 x i32]], align 1
+  br label %bb1
+
+bb1:                                              ; preds = %bb7, %bb
+  %phi = phi i32 [ 0, %bb ], [ %add8, %bb7 ]
+  %trunc = trunc i32 %phi to i16
+  %add = add i16 %trunc, 3
+  %getelementptr = getelementptr inbounds [2 x [5 x i32]], ptr %alloca, i16 0, i16 %trunc, i16 %add
+  br label %bb2
+
+bb2:                                              ; preds = %bb2, %bb1
+  %phi3 = phi i32 [ 0, %bb1 ], [ %add6, %bb2 ]
+  store i32 42, ptr %getelementptr, align 1
+  %trunc4 = trunc i32 %phi3 to i16
+  %getelementptr5 = getelementptr inbounds [2 x [5 x i32]], ptr %alloca, i16 0, i16 %trunc4, i16 %add
+  store i32 0, ptr %getelementptr5, align 1
+  %add6 = add nuw nsw i32 %phi3, 1
+  br i1 false, label %bb2, label %bb7
+
+bb7:                                              ; preds = %bb2
+  %add8 = add nuw nsw i32 %phi, 1
+  %icmp = icmp eq i32 %phi, 0
+  br i1 %icmp, label %bb1, label %bb9
+
+bb9:                                              ; preds = %bb7
+  ret void
+}



More information about the llvm-commits mailing list