[Mlir-commits] [mlir] [MLIR][Arith] Ensure ConstantOp validates signless integers for vectors (PR #177857)
Miloš Poletanović
llvmlistbot at llvm.org
Sun Jan 25 05:57:06 PST 2026
https://github.com/milos1397 created https://github.com/llvm/llvm-project/pull/177857
Fixes #177818
`arith::ConstantOp::isBuildableWith()` was only checking scalar integers for signlessness, allowing signed vector element types to pass validation incorrectly.
>From b26e1ca0d76fe3fec6ddc70168867288ad1d5b55 Mon Sep 17 00:00:00 2001
From: Milos Poletanovic <mpoletanovic at syrmia.com>
Date: Sun, 25 Jan 2026 14:30:56 +0100
Subject: [PATCH] [MLIR][Arith] Ensure ConstantOp validates signless integers
for vectors
---
mlir/lib/Dialect/Arith/IR/ArithOps.cpp | 8 ++++---
.../Conversion/SCFToSPIRV/signed-vector.mlir | 23 +++++++++++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
create mode 100644 mlir/test/Conversion/SCFToSPIRV/signed-vector.mlir
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 565a537616971..9bc1be7b2e9f1 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -232,9 +232,11 @@ bool arith::ConstantOp::isBuildableWith(Attribute value, Type type) {
if (!typedAttr || typedAttr.getType() != type)
return false;
// Integer values must be signless.
- if (llvm::isa<IntegerType>(type) &&
- !llvm::cast<IntegerType>(type).isSignless())
- return false;
+ auto elemType = getElementTypeOrSelf(type);
+ if (auto intType = dyn_cast<IntegerType>(elemType)) {
+ if (!intType.isSignless())
+ return false;
+ }
// Integer, float, and element attributes are buildable.
return llvm::isa<IntegerAttr, FloatAttr, ElementsAttr>(value);
}
diff --git a/mlir/test/Conversion/SCFToSPIRV/signed-vector.mlir b/mlir/test/Conversion/SCFToSPIRV/signed-vector.mlir
new file mode 100644
index 0000000000000..7a220e61a56f4
--- /dev/null
+++ b/mlir/test/Conversion/SCFToSPIRV/signed-vector.mlir
@@ -0,0 +1,23 @@
+// NOTE: Assertions have been autogenerated by utils/generate-test-checks.py
+
+// This script is intended to make adding checks to a test case quick and easy.
+// It is *not* authoritative about what constitutes a good test. After using the
+// script, be sure to review and refine the generated checks. For example,
+// CHECK lines should be minimized and named to reflect the test’s intent.
+// For comprehensive guidelines, see:
+// * https://mlir.llvm.org/getting_started/TestingGuide/
+
+module {
+// CHECK-LABEL: spirv.func @main() "None" {
+// CHECK: %[[THREAD_ID_0:.*]] = gpu.thread_id x
+// CHECK: %[[VAL_0:.*]] = spirv.Constant -5 : si32
+// CHECK: %[[FROM_ELEMENTS_0:.*]] = vector.from_elements %[[VAL_0]] : vector<1xsi32>
+// CHECK: spirv.Return
+// CHECK: }
+ func.func @main() {
+ %0 = gpu.thread_id x
+ %2 = spirv.Constant -5 : si32
+ %3 = vector.from_elements %2 : vector<1xsi32>
+ return
+ }
+}
\ No newline at end of file
More information about the Mlir-commits
mailing list