[Mlir-commits] [mlir] [mlir][LLVM] Disallow opaque struct types as function arguments (PR #185200)

Markus Böck llvmlistbot at llvm.org
Sat Mar 7 08:24:17 PST 2026


https://github.com/zero9178 created https://github.com/llvm/llvm-project/pull/185200

Function types are only allowed to take first-class values as arguments. The LLVM dialect implemented this correctly so far except for allowing opaque struct types. When translated to LLVM proper, invalid IR would be created with confusing assertion errors.

This PR matches LLVM by disallowing opaque struct types as arguments, allowing users to catch this kind of mistake early while still in the MLIR world.

The corresponding LLVM logic is here: https://github.com/llvm/llvm-project/blob/c4898f3f229027e6cbdf8f9db77b8c14d70f6599/llvm/lib/IR/Type.cpp#L404

>From a20b2dbcd33583c1cb8abd40c56cda6a0d5d9935 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Markus=20B=C3=B6ck?= <markus.boeck02 at gmail.com>
Date: Sat, 7 Mar 2026 17:22:55 +0100
Subject: [PATCH] [mlir][LLVM] Disallow opaque struct types as function
 arguments

Function types are only allowed to take first-class values as arguments.
The LLVM dialect implemented this correctly so far except for allowing opaque struct types.
When translated to LLVM proper, invalid IR would be created with confusing assertion errors.

This PR matches LLVM by disallowing opaque struct types as arguments, allowing users to catch this kind of mistake early while still in the MLIR world.

The corresponding LLVM logic is here: https://github.com/llvm/llvm-project/blob/c4898f3f229027e6cbdf8f9db77b8c14d70f6599/llvm/lib/IR/Type.cpp#L404
---
 mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp    | 3 +++
 mlir/test/Dialect/LLVMIR/types-invalid.mlir | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
index e24615c8d3046..63acd075c7017 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
@@ -208,6 +208,9 @@ LLVMArrayType::getPreferredAlignment(const DataLayout &dataLayout,
 //===----------------------------------------------------------------------===//
 
 bool LLVMFunctionType::isValidArgumentType(Type type) {
+  if (auto structType = dyn_cast<LLVMStructType>(type))
+    return !structType.isOpaque();
+
   return !llvm::isa<LLVMVoidType, LLVMFunctionType>(type);
 }
 
diff --git a/mlir/test/Dialect/LLVMIR/types-invalid.mlir b/mlir/test/Dialect/LLVMIR/types-invalid.mlir
index 04710fa6f2396..71ebe4e1b4ef1 100644
--- a/mlir/test/Dialect/LLVMIR/types-invalid.mlir
+++ b/mlir/test/Dialect/LLVMIR/types-invalid.mlir
@@ -14,6 +14,13 @@ func.func @function_returning_function() {
 
 // -----
 
+func.func @function_taking_opaque_struct() {
+  // expected-error @+1 {{invalid function argument type}}
+  "some.op"() : () -> !llvm.func<void(struct<"foo", opaque>)>
+}
+
+// -----
+
 func.func @function_taking_function() {
   // expected-error @+1 {{invalid function argument type}}
   "some.op"() : () -> !llvm.func<void (func<void ()>)>



More information about the Mlir-commits mailing list