[Mlir-commits] [llvm] [mlir] [MLIR] Convert BytecodeDialectInterface to ods (PR #188852)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Mar 26 14:48:08 PDT 2026


https://github.com/aidint created https://github.com/llvm/llvm-project/pull/188852

This PR converts `BytecodeDialectInterface` to ODS.

>From 84be16a918f4175b5bcbf9b9bfc83a570cbdda0d Mon Sep 17 00:00:00 2001
From: aidint <at.aidin at gmail.com>
Date: Thu, 26 Mar 2026 20:50:50 +0100
Subject: [PATCH 1/2] convert BytecodeDialectInterface to ODS

---
 .../mlir/Bytecode/BytecodeDialectInterface.td | 93 +++++++++++++++++++
 .../mlir/Bytecode/BytecodeImplementation.h    | 77 +--------------
 mlir/include/mlir/Bytecode/CMakeLists.txt     |  4 +
 3 files changed, 99 insertions(+), 75 deletions(-)
 create mode 100644 mlir/include/mlir/Bytecode/BytecodeDialectInterface.td

diff --git a/mlir/include/mlir/Bytecode/BytecodeDialectInterface.td b/mlir/include/mlir/Bytecode/BytecodeDialectInterface.td
new file mode 100644
index 0000000000000..f06715e233960
--- /dev/null
+++ b/mlir/include/mlir/Bytecode/BytecodeDialectInterface.td
@@ -0,0 +1,93 @@
+#ifndef MLIR_INTERFACES_BYTECODEDIALECTINTERFACE
+#define MLIR_INTERFACES_BYTECODEDIALECTINTERFACE
+
+include "mlir/IR/Interfaces.td"
+
+def BytecodeDialectInterface : DialectInterface<"BytecodeDialectInterface"> {
+  let description = [{}];
+  let cppNamespace = "::mlir";
+
+  let methods = [
+    InterfaceMethod<[{
+        Read an attribute belonging to this dialect from the given reader. This
+        method should return null in the case of failure. Optionally, the dialect
+        version can be accessed through the reader.
+      }],
+      "::mlir::Attribute", "readAttribute",
+      (ins "::mlir::DialectBytecodeReader &":$reader),
+      [{
+        reader.emitError() << "dialect " << getDialect()->getNamespace()
+                           << " does not support reading attributes from bytecode";
+        return ::mlir::Attribute();
+      }]
+    >,
+    InterfaceMethod<[{
+        Read a type belonging to this dialect from the given reader. This method
+        should return null in the case of failure. Optionally, the dialect version
+        can be accessed thorugh the reader.
+      }],
+      "::mlir::Type", "readType", (ins "::mlir::DialectBytecodeReader &":$reader),
+      [{
+        reader.emitError() << "dialect " << getDialect()->getNamespace()
+                           << " does not support reading types from bytecode";
+        return ::mlir::Type();
+      }]
+    >,
+    InterfaceMethod<[{
+        Write the given attribute, which belongs to this dialect, to the given
+        writer. This method may return failure to indicate that the given
+        attribute could not be encoded, in which case the textual format will be
+        used to encode this attribute instead.
+      }],
+      "::llvm::LogicalResult", "writeAttribute",
+      (ins "::mlir::Attribute":$attribute, "::mlir::DialectBytecodeWriter &":$writer),
+      [{
+        return ::llvm::failure();
+      }]
+    >,
+    InterfaceMethod<[{
+        Write the given type, which belongs to this dialect, to the given writer.
+        This method may return failure to indicate that the given type could not
+        be encoded, in which case the textual format will be used to encode this
+        type instead.
+      }],
+      "::llvm::LogicalResult", "writeType",
+      (ins "::mlir::Type":$type, "::mlir::DialectBytecodeWriter &":$writer),
+      [{
+        return ::llvm::failure();
+      }]
+    >,
+    InterfaceMethod<[{
+        Write the version of this dialect to the given writer.
+      }],
+      "void", "writeVersion",
+      (ins "::mlir::DialectBytecodeWriter &":$writer)
+    >,
+    InterfaceMethod<[{
+        Read the version of this dialect from the provided reader and return it as
+        a `unique_ptr` to a dialect version object.
+      }],
+      "std::unique_ptr<::mlir::DialectVersion>", "readVersion",
+      (ins "::mlir::DialectBytecodeReader &":$reader),
+      [{
+        reader.emitError("Dialect does not support versioning");
+        return nullptr;
+      }]
+    >,
+    InterfaceMethod<[{
+        Hook invoked after parsing completed, if a version directive was present
+        and included an entry for the current dialect. This hook offers the
+        opportunity to the dialect to visit the IR and upgrades constructs emitted
+        by the version of the dialect corresponding to the provided version.
+      }],
+      "::llvm::LogicalResult", "upgradeFromVersion",
+      (ins "::mlir::Operation *":$topLevelOp, "const ::mlir::DialectVersion &":$version),
+      [{
+        return ::llvm::success();
+      }]
+    >
+  ];
+}
+
+
+#endif
diff --git a/mlir/include/mlir/Bytecode/BytecodeImplementation.h b/mlir/include/mlir/Bytecode/BytecodeImplementation.h
index fe85908e476ff..f1381785a2703 100644
--- a/mlir/include/mlir/Bytecode/BytecodeImplementation.h
+++ b/mlir/include/mlir/Bytecode/BytecodeImplementation.h
@@ -421,81 +421,6 @@ class DialectBytecodeWriter {
   }
 };
 
-//===----------------------------------------------------------------------===//
-// BytecodeDialectInterface
-//===----------------------------------------------------------------------===//
-
-class BytecodeDialectInterface
-    : public DialectInterface::Base<BytecodeDialectInterface> {
-public:
-  using Base::Base;
-
-  //===--------------------------------------------------------------------===//
-  // Reading
-  //===--------------------------------------------------------------------===//
-
-  /// Read an attribute belonging to this dialect from the given reader. This
-  /// method should return null in the case of failure. Optionally, the dialect
-  /// version can be accessed through the reader.
-  virtual Attribute readAttribute(DialectBytecodeReader &reader) const {
-    reader.emitError() << "dialect " << getDialect()->getNamespace()
-                       << " does not support reading attributes from bytecode";
-    return Attribute();
-  }
-
-  /// Read a type belonging to this dialect from the given reader. This method
-  /// should return null in the case of failure. Optionally, the dialect version
-  /// can be accessed thorugh the reader.
-  virtual Type readType(DialectBytecodeReader &reader) const {
-    reader.emitError() << "dialect " << getDialect()->getNamespace()
-                       << " does not support reading types from bytecode";
-    return Type();
-  }
-
-  //===--------------------------------------------------------------------===//
-  // Writing
-  //===--------------------------------------------------------------------===//
-
-  /// Write the given attribute, which belongs to this dialect, to the given
-  /// writer. This method may return failure to indicate that the given
-  /// attribute could not be encoded, in which case the textual format will be
-  /// used to encode this attribute instead.
-  virtual LogicalResult writeAttribute(Attribute attr,
-                                       DialectBytecodeWriter &writer) const {
-    return failure();
-  }
-
-  /// Write the given type, which belongs to this dialect, to the given writer.
-  /// This method may return failure to indicate that the given type could not
-  /// be encoded, in which case the textual format will be used to encode this
-  /// type instead.
-  virtual LogicalResult writeType(Type type,
-                                  DialectBytecodeWriter &writer) const {
-    return failure();
-  }
-
-  /// Write the version of this dialect to the given writer.
-  virtual void writeVersion(DialectBytecodeWriter &writer) const {}
-
-  // Read the version of this dialect from the provided reader and return it as
-  // a `unique_ptr` to a dialect version object.
-  virtual std::unique_ptr<DialectVersion>
-  readVersion(DialectBytecodeReader &reader) const {
-    reader.emitError("Dialect does not support versioning");
-    return nullptr;
-  }
-
-  /// Hook invoked after parsing completed, if a version directive was present
-  /// and included an entry for the current dialect. This hook offers the
-  /// opportunity to the dialect to visit the IR and upgrades constructs emitted
-  /// by the version of the dialect corresponding to the provided version.
-  virtual LogicalResult
-  upgradeFromVersion(Operation *topLevelOp,
-                     const DialectVersion &version) const {
-    return success();
-  }
-};
-
 /// Helper for resource handle reading that returns LogicalResult.
 template <typename T, typename... Ts>
 static LogicalResult readResourceHandle(DialectBytecodeReader &reader,
@@ -559,4 +484,6 @@ auto getChecked(function_ref<InFlightDiagnostic()> emitError,
 
 } // namespace mlir
 
+#include "mlir/Bytecode/BytecodeDialectInterface.h.inc"
+
 #endif // MLIR_BYTECODE_BYTECODEIMPLEMENTATION_H
diff --git a/mlir/include/mlir/Bytecode/CMakeLists.txt b/mlir/include/mlir/Bytecode/CMakeLists.txt
index 8da03ae06a611..1e29c467c69ee 100644
--- a/mlir/include/mlir/Bytecode/CMakeLists.txt
+++ b/mlir/include/mlir/Bytecode/CMakeLists.txt
@@ -1 +1,5 @@
 add_mlir_interface(BytecodeOpInterface)
+
+set(LLVM_TARGET_DEFINITIONS BytecodeDialectInterface.td)
+mlir_tablegen(BytecodeDialectInterface.h.inc -gen-dialect-interface-decls)
+add_mlir_generic_tablegen_target(MLIRBytecodeDialectInterfaceIncGen)

>From e47f0973d3f8847accdf717ef5e9135a22b6f556 Mon Sep 17 00:00:00 2001
From: aidint <at.aidin at gmail.com>
Date: Thu, 26 Mar 2026 22:46:48 +0100
Subject: [PATCH 2/2] add corresponding bazel changes

---
 utils/bazel/llvm-project-overlay/mlir/BUILD.bazel  | 14 ++++++++++++++
 .../llvm-project-overlay/mlir/test/BUILD.bazel     |  1 +
 2 files changed, 15 insertions(+)

diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index 992f774f22b4e..e32307183d042 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -403,6 +403,16 @@ cc_library(
     ],
 )
 
+gentbl_cc_library(
+    name = "BytecodeDialectInterfaceIncGen",
+    tbl_outs = {
+        "include/mlir/Bytecode/BytecodeDialectInterface.h.inc": ["-gen-dialect-interface-decls"],
+    },
+    tblgen = ":mlir-tblgen",
+    td_file = "include/mlir/Bytecode/BytecodeDialectInterface.td",
+    deps = [":OpBaseTdFiles"],
+)
+
 cc_library(
     name = "IR",
     srcs = glob([
@@ -435,6 +445,7 @@ cc_library(
         ":BuiltinOpsIncGen",
         ":BuiltinTypeInterfacesIncGen",
         ":BuiltinTypesIncGen",
+        ":BytecodeDialectInterfaceIncGen",
         ":BytecodeOpInterfaceIncGen",
         ":CallOpInterfacesIncGen",
         ":DataLayoutInterfacesIncGen",
@@ -5365,6 +5376,7 @@ cc_library(
     ),
     includes = ["include"],
     deps = [
+        ":BytecodeDialectInterfaceIncGen",
         ":BytecodeOpInterface",
         ":CallOpInterfaces",
         ":ControlFlowInterfaces",
@@ -10734,6 +10746,7 @@ cc_library(
     ],
     includes = ["include"],
     deps = [
+        ":BytecodeDialectInterfaceIncGen",
         ":BytecodeOpInterface",
         ":IR",
         ":InferTypeOpInterface",
@@ -11924,6 +11937,7 @@ cc_library(
     includes = ["include"],
     deps = [
         ":ArithDialect",
+        ":BytecodeDialectInterfaceIncGen",
         ":BytecodeOpInterface",
         ":Dialect",
         ":DialectUtils",
diff --git a/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
index 3547f1f1b07b8..4ba252701fe00 100644
--- a/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
@@ -385,6 +385,7 @@ cc_library(
         "//mlir:ArithDialect",
         "//mlir:BufferizationDialect",
         "//mlir:BufferizationInterfaces",
+        "//mlir:BytecodeDialectInterfaceIncGen",
         "//mlir:BytecodeOpInterface",
         "//mlir:CallOpInterfaces",
         "//mlir:CommonFolders",



More information about the Mlir-commits mailing list