[llvm] [mlir][tosa] Add scaffolding for bytecode version. (PR #67374)

Jacques Pienaar via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 14:31:37 PDT 2023


https://github.com/jpienaar created https://github.com/llvm/llvm-project/pull/67374

This does not decide on any specific bytecode version or structure, but adds the scaffolding to make it easy to add.

>From 629097e46fb0b5814145787294caaed9bff8be7a Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Mon, 25 Sep 2023 14:17:02 -0700
Subject: [PATCH] [mlir][tosa] Add scaffolding for bytecode version.

This does not decide on any specific bytecode version or structure, but adds
the scaffolding to make it easy to add.
---
 .../mlir/Dialect/Tosa/IR/CMakeLists.txt       |  5 ++
 .../Dialect/Tosa/IR/TosaDialectBytecode.td    | 28 +++++++++++
 mlir/lib/Dialect/Tosa/CMakeLists.txt          |  1 +
 mlir/lib/Dialect/Tosa/IR/TosaOps.cpp          | 49 +++++++++++++++++++
 .../llvm-project-overlay/mlir/BUILD.bazel     | 20 ++++++++
 5 files changed, 103 insertions(+)
 create mode 100644 mlir/include/mlir/Dialect/Tosa/IR/TosaDialectBytecode.td

diff --git a/mlir/include/mlir/Dialect/Tosa/IR/CMakeLists.txt b/mlir/include/mlir/Dialect/Tosa/IR/CMakeLists.txt
index f3d4598bc326142..12b4fc402c390f9 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/Tosa/IR/CMakeLists.txt
@@ -6,3 +6,8 @@ set(LLVM_TARGET_DEFINITIONS TosaOps.td)
 mlir_tablegen(TosaAttributes.h.inc -gen-attrdef-decls)
 mlir_tablegen(TosaAttributes.cpp.inc -gen-attrdef-defs)
 add_public_tablegen_target(MLIRTosaAttributesIncGen)
+
+set(LLVM_TARGET_DEFINITIONS TosaDialectBytecode.td)
+mlir_tablegen(TosaDialectBytecode.cpp.inc -gen-bytecode -bytecode-dialect="Tosa")
+add_public_tablegen_target(MLIRTosaDialectBytecodeIncGen)
+
diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaDialectBytecode.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaDialectBytecode.td
new file mode 100644
index 000000000000000..258841a312c62ca
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaDialectBytecode.td
@@ -0,0 +1,28 @@
+//===-- TosaBytecode.td - Tosa bytecode defs -------------*- tablegen -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This is the TOSA bytecode reader/writer definition file.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TOSA_DIALECT_BYTECODE
+#define TOSA_DIALECT_BYTECODE
+
+include "mlir/IR/BytecodeBase.td"
+
+/// This enum contains marker codes used to indicate which attribute is
+/// currently being decoded, and how it should be decoded. The order of these
+/// codes should generally be unchanged, as any changes will inevitably break
+/// compatibility with older bytecode.
+
+def TosaDialectTypes : DialectTypes<"Tosa"> {
+  let elems = [];
+}
+
+#endif // TOSA_DIALECT_BYTECODE
+
diff --git a/mlir/lib/Dialect/Tosa/CMakeLists.txt b/mlir/lib/Dialect/Tosa/CMakeLists.txt
index b36e4a1cabb479e..8e32579e0e4c2e3 100644
--- a/mlir/lib/Dialect/Tosa/CMakeLists.txt
+++ b/mlir/lib/Dialect/Tosa/CMakeLists.txt
@@ -9,6 +9,7 @@ add_mlir_dialect_library(MLIRTosaDialect
 
   DEPENDS
   MLIRTosaAttributesIncGen
+  MLIRTosaDialectBytecodeIncGen
   MLIRTosaOpsIncGen
   MLIRTosaInterfacesIncGen
 
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index 616aad8c4aaf08f..c2f8b4a0194bbf5 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -39,6 +39,8 @@ using namespace mlir::tosa;
 #include "mlir/Dialect/Tosa/IR/TosaInterfaces.cpp.inc"
 
 namespace {
+#include "mlir/Dialect/Tosa/IR/TosaDialectBytecode.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // Dialect Function Inliner Interface.
 //===----------------------------------------------------------------------===//
@@ -62,6 +64,53 @@ struct TosaInlinerInterface : public DialectInlinerInterface {
             isa<tosa::WhileOp>(dest->getParentOp()));
   }
 };
+
+/// This class implements the bytecode interface for the Tosa dialect.
+struct TosaDialectBytecodeInterface : public BytecodeDialectInterface {
+  TosaDialectBytecodeInterface(Dialect *dialect)
+      : BytecodeDialectInterface(dialect) {}
+
+  //===--------------------------------------------------------------------===//
+  // Attributes
+
+  Attribute readAttribute(DialectBytecodeReader &reader) const override {
+    return ::readAttribute(getContext(), reader);
+  }
+
+  LogicalResult writeAttribute(Attribute attr,
+                               DialectBytecodeWriter &writer) const override {
+    return ::writeAttribute(attr, writer);
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Types
+
+  Type readType(DialectBytecodeReader &reader) const override {
+    return ::readType(getContext(), reader);
+  }
+
+  LogicalResult writeType(Type type,
+                          DialectBytecodeWriter &writer) const override {
+    return ::writeType(type, writer);
+  }
+
+  void writeVersion(DialectBytecodeWriter &writer) const final {
+    // TODO: Populate.
+  }
+
+  std::unique_ptr<DialectVersion>
+  readVersion(DialectBytecodeReader &reader) const final {
+    // TODO: Populate
+    reader.emitError("Dialect does not support versioning");
+    return nullptr;
+  }
+
+  LogicalResult upgradeFromVersion(Operation *topLevelOp,
+                                   const DialectVersion &version_) const final {
+    return success();
+  }
+};
+
 } // namespace
 
 //===----------------------------------------------------------------------===//
diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index 40c477879ed0035..cd50a24782d5d9e 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -10611,6 +10611,25 @@ gentbl_cc_library(
     deps = [":TosaDialectTdFiles"],
 )
 
+gentbl_cc_library(
+    name = "TosaDialectBytecodeGen",
+    strip_include_prefix = "include",
+    tbl_outs = [
+        (
+            [
+                "-gen-bytecode",
+                "-bytecode-dialect=Tosa",
+            ],
+            "include/mlir/Dialect/Tosa/IR/TosaDialectBytecode.cpp.inc",
+        ),
+    ],
+    tblgen = ":mlir-tblgen",
+    td_file = "include/mlir/Dialect/Tosa/IR/TosaDialectBytecode.td",
+    deps = [
+        ":BytecodeTdFiles",
+    ],
+)
+
 gentbl_cc_library(
     name = "TosaInterfacesIncGen",
     tbl_outs = [
@@ -10688,6 +10707,7 @@ cc_library(
         ":QuantOps",
         ":Support",
         ":TensorDialect",
+        ":TosaDialectBytecodeGen",
         ":TosaDialectIncGen",
         ":TosaInterfacesIncGen",
         ":TosaPassIncGen",



More information about the llvm-commits mailing list