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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Feb 28 12:12:15 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-spirv

Author: Mehdi Amini (joker-eph)

<details>
<summary>Changes</summary>

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

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


2 Files Affected:

- (modified) mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp (+5) 
- (modified) mlir/test/Dialect/SPIRV/IR/types.mlir (+6) 


``````````diff
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)
 //===----------------------------------------------------------------------===//

``````````

</details>


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


More information about the Mlir-commits mailing list