[Mlir-commits] [mlir] [mlir][spirv] Check output of getConstantInt (PR #140568)

Igor Wodiany llvmlistbot at llvm.org
Tue Jun 3 07:31:30 PDT 2025


https://github.com/IgWod-IMG updated https://github.com/llvm/llvm-project/pull/140568

>From afd991cc18f3b7c10294850686ab435bd192cb34 Mon Sep 17 00:00:00 2001
From: Igor Wodiany <igor.wodiany at imgtec.com>
Date: Mon, 19 May 2025 17:04:42 +0100
Subject: [PATCH 1/3] [mlir][spirv] Check output of getConstantInt

This patch adds an assert to check if the result of getConstantInt
is non-null. Previously the code failed with Segmentation Fault if
getConstantInt failed to look up the value. This primarily occurred
when the value was defined as OpSpecConstant rather than OpConstant.
---
 .../Target/SPIRV/Deserialization/Deserializer.cpp | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp b/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
index 7afd6e9b25b77..9008d88866f40 100644
--- a/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
+++ b/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
@@ -1061,12 +1061,19 @@ LogicalResult spirv::Deserializer::processCooperativeMatrixTypeKHR(
            << operands[2];
   }
 
-  unsigned rows = getConstantInt(operands[3]).getInt();
-  unsigned columns = getConstantInt(operands[4]).getInt();
+  IntegerAttr rowsAttr = getConstantInt(operands[3]);
+  assert(rowsAttr);
+  unsigned rows = rowsAttr.getInt();
+
+  IntegerAttr columnsAttr = getConstantInt(operands[4]);
+  assert(columnsAttr);
+  unsigned columns = columnsAttr.getInt();
+
+  IntegerAttr useAttr = getConstantInt(operands[5]);
+  assert(useAttr);
 
   std::optional<spirv::CooperativeMatrixUseKHR> use =
-      spirv::symbolizeCooperativeMatrixUseKHR(
-          getConstantInt(operands[5]).getInt());
+      spirv::symbolizeCooperativeMatrixUseKHR(useAttr.getInt());
   if (!use) {
     return emitError(
                unknownLoc,

>From 7ea6cca3a5ebda51142d19a0935210b1f7ead094 Mon Sep 17 00:00:00 2001
From: Igor Wodiany <igor.wodiany at imgtec.com>
Date: Tue, 27 May 2025 13:44:18 +0100
Subject: [PATCH 2/3] Use emitError

---
 .../SPIRV/Deserialization/Deserializer.cpp      | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp b/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
index 9008d88866f40..d3360a82da1b5 100644
--- a/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
+++ b/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
@@ -1062,15 +1062,18 @@ LogicalResult spirv::Deserializer::processCooperativeMatrixTypeKHR(
   }
 
   IntegerAttr rowsAttr = getConstantInt(operands[3]);
-  assert(rowsAttr);
-  unsigned rows = rowsAttr.getInt();
-
   IntegerAttr columnsAttr = getConstantInt(operands[4]);
-  assert(columnsAttr);
-  unsigned columns = columnsAttr.getInt();
-
   IntegerAttr useAttr = getConstantInt(operands[5]);
-  assert(useAttr);
+
+  if (!rowsAttr || !columnsAttr || !useAttr)
+    return emitError(
+               unknownLoc,
+               "OpTypeCooperativeMatrixKHR references undefined constant <id> ")
+           << (rowsAttr ? (columnsAttr ? operands[5] : operands[4])
+                        : operands[3]);
+
+  unsigned rows = rowsAttr.getInt();
+  unsigned columns = columnsAttr.getInt();
 
   std::optional<spirv::CooperativeMatrixUseKHR> use =
       spirv::symbolizeCooperativeMatrixUseKHR(useAttr.getInt());

>From 2526c7a9a127ebf21f099c7973d2069e4281b1de Mon Sep 17 00:00:00 2001
From: Igor Wodiany <igor.wodiany at imgtec.com>
Date: Tue, 27 May 2025 13:44:18 +0100
Subject: [PATCH 3/3] Split check

---
 .../SPIRV/Deserialization/Deserializer.cpp    | 20 +++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp b/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
index d3360a82da1b5..1d3436c99a63c 100644
--- a/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
+++ b/mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
@@ -1065,12 +1065,20 @@ LogicalResult spirv::Deserializer::processCooperativeMatrixTypeKHR(
   IntegerAttr columnsAttr = getConstantInt(operands[4]);
   IntegerAttr useAttr = getConstantInt(operands[5]);
 
-  if (!rowsAttr || !columnsAttr || !useAttr)
-    return emitError(
-               unknownLoc,
-               "OpTypeCooperativeMatrixKHR references undefined constant <id> ")
-           << (rowsAttr ? (columnsAttr ? operands[5] : operands[4])
-                        : operands[3]);
+  if (!rowsAttr)
+    return emitError(unknownLoc, "OpTypeCooperativeMatrixKHR `Rows` references "
+                                 "undefined constant <id> ")
+           << operands[3];
+
+  if (!columnsAttr)
+    return emitError(unknownLoc, "OpTypeCooperativeMatrixKHR `Columns` "
+                                 "references undefined constant <id> ")
+           << operands[4];
+
+  if (!useAttr)
+    return emitError(unknownLoc, "OpTypeCooperativeMatrixKHR `Use` references "
+                                 "undefined constant <id> ")
+           << operands[5];
 
   unsigned rows = rowsAttr.getInt();
   unsigned columns = columnsAttr.getInt();



More information about the Mlir-commits mailing list