Skip to content

Conversation

@ivanradanov
Copy link
Contributor

When InstTy is a type like IntrinsicInst which can have a variable number of arguments, we can encounter a case where Operation will have fewer than two arguments and error at the getOperand() calls.

I think this was introduced by c11ea449e59cf

An alternative fix would be to have a version of matchTwoInputRecurrence where we pass I to it here and check I == Operation in the if instead of Operation->getNumOperands() == 2 .

@ivanradanov ivanradanov requested a review from nikic as a code owner August 8, 2025 12:32
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Aug 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 8, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Ivan R. Ivanov (ivanradanov)

Changes

When InstTy is a type like IntrinsicInst which can have a variable number of arguments, we can encounter a case where Operation will have fewer than two arguments and error at the getOperand() calls.

I think this was introduced by c11ea449e59cf

An alternative fix would be to have a version of matchTwoInputRecurrence where we pass I to it here and check I == Operation in the if instead of Operation->getNumOperands() == 2 .


Full diff: https://github.com/llvm/llvm-project/pull/152700.diff

1 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+2-1)
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)

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.
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test?

@ivanradanov
Copy link
Contributor Author

I added a test. For reference, before the patch, this is how it crashes:

AnalysisTests: src/llvm-project/llvm/include/llvm/IR/InstrTypes.h:2353: Value *llvm::CallBase::getOperand(unsigned int) const: Assertion `i_nocapture < OperandTraits<CallBase>::operands(this) && "getOperand() out of range!"' failed.

I also noticed that the == 2 does not work for intrinsics as they also have the callee argument so I changed to >= 2

@ivanradanov ivanradanov force-pushed the ivan/match-two-input-recurrance branch from 222ec9d to ab28b45 Compare August 8, 2025 13:04
@ivanradanov ivanradanov force-pushed the ivan/match-two-input-recurrance branch from 2e1cc47 to a5bc84b Compare August 8, 2025 14:42
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@@ -0,0 +1,16 @@
; Check that we do not crash (see PR #152700)
; RUN: opt < %s -passes=instcombine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
; RUN: opt < %s -passes=instcombine
; RUN: opt < %s -passes=instcombine | FileCheck %s

and generate check lines with update_test_checks.py. We prefer checking the output even for crash tests.

@antoniofrighetto antoniofrighetto merged commit 7c141e2 into llvm:main Aug 8, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants