[Mlir-commits] [mlir] [MLIR][Python] Support dynamic traits in python-defined dialects (PR #179705)

Rolf Morel llvmlistbot at llvm.org
Mon Feb 9 06:45:17 PST 2026


================
@@ -0,0 +1,87 @@
+//===- ExtensibleDialect - C API for MLIR Extensible Dialect --------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir-c/ExtensibleDialect.h"
+#include "mlir/CAPI/IR.h"
+#include "mlir/CAPI/Support.h"
+#include "mlir/IR/ExtensibleDialect.h"
+#include "mlir/IR/OperationSupport.h"
+
+using namespace mlir;
+
+DEFINE_C_API_PTR_METHODS(MlirDynamicOpTrait, DynamicOpTrait)
+
+bool mlirDynamicOpTraitAttach(MlirDynamicOpTrait dynamicOpTrait,
+                              MlirStringRef opName, MlirContext context) {
+  std::optional<RegisteredOperationName> opNameFound =
+      RegisteredOperationName::lookup(unwrap(opName), unwrap(context));
+  assert(opNameFound && "operation name must be registered in the context");
+
+  // The original getImpl() is protected, so we create a small helper struct
+  // here.
+  struct RegisteredOperationNameWithImpl : RegisteredOperationName {
+    Impl *getImpl() { return RegisteredOperationName::getImpl(); }
+  };
+  OperationName::Impl *impl =
+      static_cast<RegisteredOperationNameWithImpl &>(*opNameFound).getImpl();
+
+  std::unique_ptr<DynamicOpTrait> trait(unwrap(dynamicOpTrait));
+  // TODO: we should check whether the `impl` is a DynamicOpDefinition here
+  // via llvm-style RTTI.
----------------
rolfmorel wrote:

Could you explain why we cannot just do the type switch or the like here now already? I.e. what is causing this to be a TODO?

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


More information about the Mlir-commits mailing list