[llvm] [ValueTracking] Add missing check for two-value PN recurrance matching (PR #152700)

Ivan R. Ivanov via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 8 07:57:16 PDT 2025


https://github.com/ivanradanov updated https://github.com/llvm/llvm-project/pull/152700

>From 7549fd2f17ccb68b8cad59b7b0085afe93ba1ebe Mon Sep 17 00:00:00 2001
From: Ivan Radanov Ivanov <iivanov at nvidia.com>
Date: Fri, 8 Aug 2025 05:07:40 -0700
Subject: [PATCH 1/4] [ValueTracking] Add missing check for two-value PN
 recurrance matching

When InstTy is a type like IntrinsicInst which can have a variable number of
arguments, we can encounter a case where Opetaion will have fewer than two
arguments and error at the getOperand() calls.
---
 llvm/lib/Analysis/ValueTracking.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 1e70228905c33..b142fc6cc1334 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9147,7 +9147,8 @@ static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
     return false;
 
   for (unsigned I = 0; I != 2; ++I) {
-    if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I))) {
+    if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
+        Operation && Operation->getNumOperands() == 2) {
       Value *LHS = Operation->getOperand(0);
       Value *RHS = Operation->getOperand(1);
       if (LHS != PN && RHS != PN)

>From 17613e1cde4a7dc1564ba2b68f536ef0a3f5e8e4 Mon Sep 17 00:00:00 2001
From: Ivan Radanov Ivanov <iivanov at nvidia.com>
Date: Fri, 8 Aug 2025 06:01:04 -0700
Subject: [PATCH 2/4] Actually check >= operands

---
 llvm/lib/Analysis/ValueTracking.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index b142fc6cc1334..b0e4b009f3501 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9148,7 +9148,7 @@ static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
 
   for (unsigned I = 0; I != 2; ++I) {
     if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
-        Operation && Operation->getNumOperands() == 2) {
+        Operation && Operation->getNumOperands() >= 2) {
       Value *LHS = Operation->getOperand(0);
       Value *RHS = Operation->getOperand(1);
       if (LHS != PN && RHS != PN)

>From a5bc84b04de97c5806bfa7336c2f499a50ece2e7 Mon Sep 17 00:00:00 2001
From: Ivan Radanov Ivanov <iivanov at nvidia.com>
Date: Fri, 8 Aug 2025 07:40:00 -0700
Subject: [PATCH 3/4] Add lit test

---
 llvm/test/Analysis/ValueTracking/pr152700.ll | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
 create mode 100644 llvm/test/Analysis/ValueTracking/pr152700.ll

diff --git a/llvm/test/Analysis/ValueTracking/pr152700.ll b/llvm/test/Analysis/ValueTracking/pr152700.ll
new file mode 100644
index 0000000000000..5b7b6536fa81e
--- /dev/null
+++ b/llvm/test/Analysis/ValueTracking/pr152700.ll
@@ -0,0 +1,16 @@
+; Check that we do not crash (see PR #152700)
+; RUN: opt < %s -passes=instcombine
+
+declare noundef i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
+declare i32 @llvm.umin.i32(i32, i32)
+define i32 @foo(i1 %c, i32 %arg) {
+entry:
+  %i = call i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
+  br i1 %c, label %bb.1, label %bb.2
+bb.1:
+  br label %bb.2
+bb.2:
+  %phi = phi i32 [ %i, %entry ], [ 0, %bb.1 ]
+  %res = call i32 @llvm.umin.i32(i32 %phi, i32 %arg)
+  ret i32 %res
+}

>From f80ad3f4a0a1f74c06017d66471590d2b5135a88 Mon Sep 17 00:00:00 2001
From: Ivan Radanov Ivanov <iivanov at nvidia.com>
Date: Fri, 8 Aug 2025 07:56:59 -0700
Subject: [PATCH 4/4] Add checks

---
 llvm/test/Analysis/ValueTracking/pr152700.ll | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/llvm/test/Analysis/ValueTracking/pr152700.ll b/llvm/test/Analysis/ValueTracking/pr152700.ll
index 5b7b6536fa81e..91644c543a173 100644
--- a/llvm/test/Analysis/ValueTracking/pr152700.ll
+++ b/llvm/test/Analysis/ValueTracking/pr152700.ll
@@ -1,9 +1,21 @@
-; Check that we do not crash (see PR #152700)
-; RUN: opt < %s -passes=instcombine
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 
 declare noundef i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
 declare i32 @llvm.umin.i32(i32, i32)
 define i32 @foo(i1 %c, i32 %arg) {
+; CHECK-LABEL: define i32 @foo(
+; CHECK-SAME: i1 [[C:%.*]], i32 [[ARG:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[I:%.*]] = call i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
+; CHECK-NEXT:    br i1 [[C]], label %[[BB_1:.*]], label %[[BB_2:.*]]
+; CHECK:       [[BB_1]]:
+; CHECK-NEXT:    br label %[[BB_2]]
+; CHECK:       [[BB_2]]:
+; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ [[I]], %[[ENTRY]] ], [ 0, %[[BB_1]] ]
+; CHECK-NEXT:    [[RES:%.*]] = call i32 @llvm.umin.i32(i32 [[PHI]], i32 [[ARG]])
+; CHECK-NEXT:    ret i32 [[RES]]
+;
 entry:
   %i = call i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
   br i1 %c, label %bb.1, label %bb.2



More information about the llvm-commits mailing list