[Mlir-commits] [mlir] 691c653 - [mlir][tblgen] Emit diagnostic instead of crashing for invalid interface method arg type (#186430)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Mar 23 05:51:41 PDT 2026


Author: Mehdi Amini
Date: 2026-03-23T13:51:36+01:00
New Revision: 691c6532139deba0f95f7229b0f1064e81e6409a

URL: https://github.com/llvm/llvm-project/commit/691c6532139deba0f95f7229b0f1064e81e6409a
DIFF: https://github.com/llvm/llvm-project/commit/691c6532139deba0f95f7229b0f1064e81e6409a.diff

LOG: [mlir][tblgen] Emit diagnostic instead of crashing for invalid interface method arg type (#186430)

When an interface method argument uses a non-string type (e.g., a
TableGen class reference like `Foo` instead of a string literal
`"Foo"`), the code in `InterfaceMethod::InterfaceMethod` would crash
with an assertion failure in `cast<StringInit>`. Replace the unchecked
cast with a `dyn_cast` and emit a `PrintFatalError` with a descriptive
error message pointing to the source location and identifying which
argument has the wrong type.

Before this fix:
mlir-tblgen: Assertion `isa<To>(Val) && "cast<Ty>() argument of
incompatible type\!"' failed.

After this fix:
error: expected string type for interface method argument #0 ('arg') ...

Fixes #61869

Assisted-by: Claude Code

Added: 
    mlir/test/mlir-tblgen/interface-method-arg-error.td

Modified: 
    mlir/lib/TableGen/Interfaces.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/TableGen/Interfaces.cpp b/mlir/lib/TableGen/Interfaces.cpp
index f4fa65777f585..6cdacf16fbb6a 100644
--- a/mlir/lib/TableGen/Interfaces.cpp
+++ b/mlir/lib/TableGen/Interfaces.cpp
@@ -9,6 +9,7 @@
 #include "mlir/TableGen/Interfaces.h"
 #include "llvm/ADT/FunctionExtras.h"
 #include "llvm/ADT/StringSet.h"
+#include "llvm/ADT/Twine.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 #include <utility>
@@ -30,8 +31,15 @@ InterfaceMethod::InterfaceMethod(const Record *def, std::string uniqueName)
     : def(def), uniqueName(uniqueName) {
   const DagInit *args = def->getValueAsDag("arguments");
   for (unsigned i = 0, e = args->getNumArgs(); i != e; ++i) {
-    arguments.push_back({cast<StringInit>(args->getArg(i))->getValue(),
-                         args->getArgNameStr(i)});
+    const Init *arg = args->getArg(i);
+    const auto *strArg = dyn_cast<StringInit>(arg);
+    if (!strArg)
+      llvm::PrintFatalError(
+          def->getLoc(),
+          "expected string type for interface method argument #" + Twine(i) +
+              " ('" + args->getArgNameStr(i) + "') in '" + def->getName() +
+              "', but got '" + arg->getAsString() + "'");
+    arguments.push_back({strArg->getValue(), args->getArgNameStr(i)});
   }
 }
 

diff  --git a/mlir/test/mlir-tblgen/interface-method-arg-error.td b/mlir/test/mlir-tblgen/interface-method-arg-error.td
new file mode 100644
index 0000000000000..5bbaa93c1227f
--- /dev/null
+++ b/mlir/test/mlir-tblgen/interface-method-arg-error.td
@@ -0,0 +1,13 @@
+// RUN: not mlir-tblgen -gen-op-interface-decls -I %S/../../include %s 2>&1 | FileCheck %s
+
+// Test that a non-string argument type in an interface method gives a readable
+// error instead of a crash. See https://github.com/llvm/llvm-project/issues/61869
+
+include "mlir/IR/OpBase.td"
+
+def Foo {}
+
+// CHECK: error: expected string type for interface method argument #0 ('arg')
+def Bar : OpInterface<"Bar"> {
+  let methods = [InterfaceMethod<[{}], "void", "baz", (ins Foo:$arg)>];
+}


        


More information about the Mlir-commits mailing list