[Mlir-commits] [mlir] [mlir][VCIX] Check legalized type before use in math.tan test pattern (PR #236051)
Akshay Khadse
llvmlistbot at llvm.org
Tue Oct 27 02:45:37 PDT 2026
https://github.com/akshaykhadse created https://github.com/llvm/llvm-project/pull/236051
`MathTanToVCIX` called `getElementType()` on the result of `legalizeVectorType()` before checking whether legalization failed. For types it can't legalize (scalars, multi-dimensional vectors, scalable vectors with unsupported element types) that result is null, so the pattern crashed instead of leaving the op alone like the cos, sin and log patterns do.
This moves the null check above the first use and adds tests for a scalar `math.tan` and a `vector<2x4xf32>` one.
Fixes #231844
>From 8b5ea9c7ddc5126c1b6a7c6159d4ecf910bbefb3 Mon Sep 17 00:00:00 2001
From: akshaykhadse <akshayskhadse at gmail.com>
Date: Fri, 23 Oct 2026 20:22:14 -0400
Subject: [PATCH] [mlir][VCIX] Check legalized type before use in math.tan test
pattern
MathTanToVCIX called getElementType() on the type returned by
legalizeVectorType() before checking whether legalization failed. For
types that cannot be legalized (scalars, multi-dimensional vectors, or
scalable vectors with unsupported element types) the result is null, so
the pattern crashed instead of leaving the op untouched as the cos, sin
and log patterns do.
Move the null check ahead of the first use.
Fixes #231844
---
.../Conversion/MathToVCIX/math-to-vcix.mlir | 20 +++++++++++++++++++
.../MathToVCIX/TestMathToVCIXConversion.cpp | 2 +-
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/mlir/test/Conversion/MathToVCIX/math-to-vcix.mlir b/mlir/test/Conversion/MathToVCIX/math-to-vcix.mlir
index 9d4bf7bdb56556..855ede15ce5e61 100644
--- a/mlir/test/Conversion/MathToVCIX/math-to-vcix.mlir
+++ b/mlir/test/Conversion/MathToVCIX/math-to-vcix.mlir
@@ -202,3 +202,23 @@ func.func @no_vector_type(%arg0: f32) -> f32 {
%0 = math.cos %arg0 : f32
return %0 : f32
}
+
+// -----
+
+// Types that cannot be legalized for RVV are left untouched.
+
+// CHECK-LABEL: func.func @tan_no_vector_type
+// CHECK: math.tan %{{.*}} : f32
+func.func @tan_no_vector_type(%arg0: f32) -> f32 {
+ %0 = math.tan %arg0 : f32
+ return %0 : f32
+}
+
+// -----
+
+// CHECK-LABEL: func.func @tan_multi_dim_vector
+// CHECK: math.tan %{{.*}} : vector<2x4xf32>
+func.func @tan_multi_dim_vector(%arg0: vector<2x4xf32>) -> vector<2x4xf32> {
+ %0 = math.tan %arg0 : vector<2x4xf32>
+ return %0 : vector<2x4xf32>
+}
diff --git a/mlir/test/lib/Conversion/MathToVCIX/TestMathToVCIXConversion.cpp b/mlir/test/lib/Conversion/MathToVCIX/TestMathToVCIXConversion.cpp
index 80d3fc0544a4b0..96063b06e72a89 100644
--- a/mlir/test/lib/Conversion/MathToVCIX/TestMathToVCIXConversion.cpp
+++ b/mlir/test/lib/Conversion/MathToVCIX/TestMathToVCIXConversion.cpp
@@ -146,9 +146,9 @@ struct MathTanToVCIX final : OpRewritePattern<math::TanOp> {
PatternRewriter &rewriter) const override {
const Type opType = op.getOperand().getType();
auto [n, legalType] = legalizeVectorType(opType);
- Type eltTy = legalType.getElementType();
if (!legalType)
return rewriter.notifyMatchFailure(op, "cannot legalize type for RVV");
+ Type eltTy = legalType.getElementType();
Location loc = op.getLoc();
Value vec = op.getOperand();
Attribute opcodeAttr = rewriter.getI64IntegerAttr(0);
More information about the Mlir-commits
mailing list