[Mlir-commits] [mlir] [mlir][spirv] Fix crash when spirv.struct member type is not a SPIR-V type (PR #183942)

Mehdi Amini llvmlistbot at llvm.org
Sat Feb 28 12:11:47 PST 2026


https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/183942

When parsing a spirv.struct type, any MLIR type was accepted as a member type without validation. This caused a crash in TypeExtensionVisitor and TypeCapabilityVisitor which unconditionally used cast<SPIRVType> on struct element types, asserting when a non-SPIR-V type (e.g., vector<2x2xi1>) was encountered.

Fix the parser to reject non-SPIR-V member types with a proper error message. Also make the add(Type) overloads in both visitors use a safe dyn_cast so that programmatically-constructed invalid structs don't produce a crash.

Fixes #179675

>From af4bf70dae450aa873ef340f0e5558d36620b525 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Sat, 28 Feb 2026 09:03:11 -0800
Subject: [PATCH] [mlir][spirv] Fix crash when spirv.struct member type is not
 a SPIR-V type

When parsing a spirv.struct type, any MLIR type was accepted as a
member type without validation. This caused a crash in
TypeExtensionVisitor and TypeCapabilityVisitor which unconditionally
used cast<SPIRVType> on struct element types, asserting when a
non-SPIR-V type (e.g., vector<2x2xi1>) was encountered.

Fix the parser to reject non-SPIR-V member types with a proper error
message.

Fixes #179675
---
 mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp | 5 +++++
 mlir/test/Dialect/SPIRV/IR/types.mlir      | 6 ++++++
 2 files changed, 11 insertions(+)

diff --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp
index 78f33c238d414..c9b22fe145d88 100644
--- a/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp
@@ -774,6 +774,11 @@ static Type parseStructType(SPIRVDialect const &dialect,
     Type memberType;
     if (parser.parseType(memberType))
       return Type();
+    if (!isa<SPIRVType>(memberType)) {
+      parser.emitError(parser.getNameLoc(),
+                       "member type must be a valid SPIR-V type");
+      return Type();
+    }
     memberTypes.push_back(memberType);
 
     if (succeeded(parser.parseOptionalLSquare()))
diff --git a/mlir/test/Dialect/SPIRV/IR/types.mlir b/mlir/test/Dialect/SPIRV/IR/types.mlir
index 98509fb376acf..710673b73cee5 100644
--- a/mlir/test/Dialect/SPIRV/IR/types.mlir
+++ b/mlir/test/Dialect/SPIRV/IR/types.mlir
@@ -384,6 +384,12 @@ func.func private @struct_missing_member_decorator_value(!spirv.struct<(!spirv.m
 
 // -----
 
+// Regression test for https://github.com/llvm/llvm-project/issues/179675
+// expected-error @+1 {{member type must be a valid SPIR-V type}}
+func.func private @struct_type_non_spirv_member(!spirv.struct<(vector<2x2xi1>)>) -> ()
+
+// -----
+
 //===----------------------------------------------------------------------===//
 // StructType (identified)
 //===----------------------------------------------------------------------===//



More information about the Mlir-commits mailing list