Skip to content

Commit 1a7e8b9

Browse files
authored
[mlir][tosa] Add scaffolding for bytecode version. (#67374)
This does not decide on any specific bytecode version or structure, but adds the scaffolding to make it easy to add.
1 parent b26157e commit 1a7e8b9

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

mlir/include/mlir/Dialect/Tosa/IR/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ set(LLVM_TARGET_DEFINITIONS TosaOps.td)
66
mlir_tablegen(TosaAttributes.h.inc -gen-attrdef-decls)
77
mlir_tablegen(TosaAttributes.cpp.inc -gen-attrdef-defs)
88
add_public_tablegen_target(MLIRTosaAttributesIncGen)
9+
10+
set(LLVM_TARGET_DEFINITIONS TosaDialectBytecode.td)
11+
mlir_tablegen(TosaDialectBytecode.cpp.inc -gen-bytecode -bytecode-dialect="Tosa")
12+
add_public_tablegen_target(MLIRTosaDialectBytecodeIncGen)
13+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- TosaBytecode.td - Tosa bytecode defs -------------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This is the TOSA bytecode reader/writer definition file.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef TOSA_DIALECT_BYTECODE
14+
#define TOSA_DIALECT_BYTECODE
15+
16+
include "mlir/IR/BytecodeBase.td"
17+
18+
/// This enum contains marker codes used to indicate which attribute is
19+
/// currently being decoded, and how it should be decoded. The order of these
20+
/// codes should generally be unchanged, as any changes will inevitably break
21+
/// compatibility with older bytecode.
22+
23+
def TosaDialectTypes : DialectTypes<"Tosa"> {
24+
let elems = [];
25+
}
26+
27+
#endif // TOSA_DIALECT_BYTECODE
28+

mlir/lib/Dialect/Tosa/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_mlir_dialect_library(MLIRTosaDialect
99

1010
DEPENDS
1111
MLIRTosaAttributesIncGen
12+
MLIRTosaDialectBytecodeIncGen
1213
MLIRTosaOpsIncGen
1314
MLIRTosaInterfacesIncGen
1415

mlir/lib/Dialect/Tosa/IR/TosaOps.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ using namespace mlir::tosa;
3939
#include "mlir/Dialect/Tosa/IR/TosaInterfaces.cpp.inc"
4040

4141
namespace {
42+
#include "mlir/Dialect/Tosa/IR/TosaDialectBytecode.cpp.inc"
43+
4244
//===----------------------------------------------------------------------===//
4345
// Dialect Function Inliner Interface.
4446
//===----------------------------------------------------------------------===//
@@ -62,6 +64,53 @@ struct TosaInlinerInterface : public DialectInlinerInterface {
6264
isa<tosa::WhileOp>(dest->getParentOp()));
6365
}
6466
};
67+
68+
/// This class implements the bytecode interface for the Tosa dialect.
69+
struct TosaDialectBytecodeInterface : public BytecodeDialectInterface {
70+
TosaDialectBytecodeInterface(Dialect *dialect)
71+
: BytecodeDialectInterface(dialect) {}
72+
73+
//===--------------------------------------------------------------------===//
74+
// Attributes
75+
76+
Attribute readAttribute(DialectBytecodeReader &reader) const override {
77+
return ::readAttribute(getContext(), reader);
78+
}
79+
80+
LogicalResult writeAttribute(Attribute attr,
81+
DialectBytecodeWriter &writer) const override {
82+
return ::writeAttribute(attr, writer);
83+
}
84+
85+
//===--------------------------------------------------------------------===//
86+
// Types
87+
88+
Type readType(DialectBytecodeReader &reader) const override {
89+
return ::readType(getContext(), reader);
90+
}
91+
92+
LogicalResult writeType(Type type,
93+
DialectBytecodeWriter &writer) const override {
94+
return ::writeType(type, writer);
95+
}
96+
97+
void writeVersion(DialectBytecodeWriter &writer) const final {
98+
// TODO: Populate.
99+
}
100+
101+
std::unique_ptr<DialectVersion>
102+
readVersion(DialectBytecodeReader &reader) const final {
103+
// TODO: Populate
104+
reader.emitError("Dialect does not support versioning");
105+
return nullptr;
106+
}
107+
108+
LogicalResult upgradeFromVersion(Operation *topLevelOp,
109+
const DialectVersion &version_) const final {
110+
return success();
111+
}
112+
};
113+
65114
} // namespace
66115

67116
//===----------------------------------------------------------------------===//
@@ -84,7 +133,7 @@ void TosaDialect::initialize() {
84133
#define GET_ATTRDEF_LIST
85134
#include "mlir/Dialect/Tosa/IR/TosaAttributes.cpp.inc"
86135
>();
87-
addInterfaces<TosaInlinerInterface>();
136+
addInterfaces<TosaDialectBytecodeInterface, TosaInlinerInterface>();
88137
}
89138

90139
Operation *TosaDialect::materializeConstant(OpBuilder &builder, Attribute value,

utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10612,6 +10612,25 @@ gentbl_cc_library(
1061210612
deps = [":TosaDialectTdFiles"],
1061310613
)
1061410614

10615+
gentbl_cc_library(
10616+
name = "TosaDialectBytecodeGen",
10617+
strip_include_prefix = "include",
10618+
tbl_outs = [
10619+
(
10620+
[
10621+
"-gen-bytecode",
10622+
"-bytecode-dialect=Tosa",
10623+
],
10624+
"include/mlir/Dialect/Tosa/IR/TosaDialectBytecode.cpp.inc",
10625+
),
10626+
],
10627+
tblgen = ":mlir-tblgen",
10628+
td_file = "include/mlir/Dialect/Tosa/IR/TosaDialectBytecode.td",
10629+
deps = [
10630+
":BytecodeTdFiles",
10631+
],
10632+
)
10633+
1061510634
gentbl_cc_library(
1061610635
name = "TosaInterfacesIncGen",
1061710636
tbl_outs = [
@@ -10689,6 +10708,7 @@ cc_library(
1068910708
":QuantOps",
1069010709
":Support",
1069110710
":TensorDialect",
10711+
":TosaDialectBytecodeGen",
1069210712
":TosaDialectIncGen",
1069310713
":TosaInterfacesIncGen",
1069410714
":TosaPassIncGen",

0 commit comments

Comments
 (0)