[llvm] [RISCV] Fix assertion in combineBinOpOfExtractToReduceTree on type mismatch (PR #202201)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 7 06:11:09 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Tanmay Gulhane (trg-rgb)

<details>
<summary>Changes</summary>

## Summary

combineBinOpOfExtractToReduceTree asserts that the extract source vector's element type equals the binop's value type (SrcVecVT.getVectorElementType() == VT). This invariant does not hold for all valid inputs.

A <1 x i1> binary operation under -mattr=+zve32x reaches this point with the source vector element type differing from VT, which trips the assertion in an assertions-enabled build and silently proceeds on a false assumption otherwise.

## Reproducer

```llvm
define <1 x i1> @<!-- -->f(<1 x i1> %a, <1 x i1> %b) {
  %x = xor <1 x i1> %a, %a
  %r = or <1 x i1> %b, %x
  ret <1 x i1> %r
}
```

Command and assertion:

```
llc -mtriple=riscv64 -mattr=+zve32x repro.ll
RISCVISelLowering.cpp:15899: Assertion `SrcVecVT.getVectorElementType() == VT' failed.
```

The IR verifies clean under opt -passes=verify. The assertion was introduced in f0505c3dbe50 (#<!-- -->67821).

## Fix

Convert the assertion into an early return, so the combine declines when its precondition is not met. This matches the existing bail-out style in the same function (the isScalableVector and getScalarSizeInBits() > getELen() checks immediately following). The change only ever skips the fold; it never alters correct output.

## Testing

Added llvm/test/CodeGen/RISCV/rvv/fold-binary-reduce-zve32x-crash.ll, which crashes without the fix and passes with it. The full check-llvm-codegen-riscv suite (2565 tests) passes.

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


2 Files Affected:

- (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+2-1) 
- (added) llvm/test/CodeGen/RISCV/rvv/fold-binary-reduce-zve32x-crash.ll (+17) 


``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 859b6b16275d0..3bc1abc10e248 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -15896,7 +15896,8 @@ combineBinOpOfExtractToReduceTree(SDNode *N, SelectionDAG &DAG,
   uint64_t RHSIdx = cast<ConstantSDNode>(RHS.getOperand(1))->getLimitedValue();
   SDValue SrcVec = RHS.getOperand(0);
   EVT SrcVecVT = SrcVec.getValueType();
-  assert(SrcVecVT.getVectorElementType() == VT);
+  if (SrcVecVT.getVectorElementType() != VT)
+    return SDValue();
   if (SrcVecVT.isScalableVector())
     return SDValue();
 
diff --git a/llvm/test/CodeGen/RISCV/rvv/fold-binary-reduce-zve32x-crash.ll b/llvm/test/CodeGen/RISCV/rvv/fold-binary-reduce-zve32x-crash.ll
new file mode 100644
index 0000000000000..86f7e718d47fe
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv/fold-binary-reduce-zve32x-crash.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv64 -mattr=+zve32x -verify-machineinstrs < %s | FileCheck %s
+
+; Regression test for combineBinOpOfExtractToReduceTree, which asserted
+; SrcVecVT.getVectorElementType() == VT and crashed on a <1 x i1> binop
+; under zve32x, where the extract source element type does not match the
+; node value type. The combine must decline instead of asserting.
+
+define <1 x i1> @fold_or_v1i1_extract(<1 x i1> %a, <1 x i1> %b) {
+; CHECK-LABEL: fold_or_v1i1_extract:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    mv a0, a1
+; CHECK-NEXT:    ret
+  %x = xor <1 x i1> %a, %a
+  %r = or <1 x i1> %b, %x
+  ret <1 x i1> %r
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/202201


More information about the llvm-commits mailing list