summaryrefslogtreecommitdiff
path: root/mlir/lib/Bytecode/Writer/IRNumbering.cpp
diff options
context:
space:
mode:
authorMehdi Amini <joker.eph@gmail.com>2023-07-28 10:43:51 -0700
committerMehdi Amini <joker.eph@gmail.com>2023-07-28 10:44:02 -0700
commitb299ec16661f653df66cdaf161cdc5441bc9803c (patch)
tree871d39e32caf5b3cacdc546905d4ee7731b8053e /mlir/lib/Bytecode/Writer/IRNumbering.cpp
parentbb65caf90ae1ade0ab1896c8e781cff34b34a846 (diff)
Expose callbacks for encoding of types/attributes
[mlir] Expose a mechanism to provide a callback for encoding types and attributes in MLIR bytecode. Two callbacks are exposed, respectively, to the BytecodeWriterConfig and to the ParserConfig. At bytecode parsing/printing, clients have the ability to specify a callback to be used to optionally read/write the encoding. On failure, fallback path will execute the default parsers and printers for the dialect. Testing shows how to leverage this functionality to support back-deployment and backward-compatibility usecases when roundtripping to bytecode a client dialect with type/attributes dependencies on upstream. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D153383
Diffstat (limited to 'mlir/lib/Bytecode/Writer/IRNumbering.cpp')
-rw-r--r--mlir/lib/Bytecode/Writer/IRNumbering.cpp40
1 files changed, 34 insertions, 6 deletions
diff --git a/mlir/lib/Bytecode/Writer/IRNumbering.cpp b/mlir/lib/Bytecode/Writer/IRNumbering.cpp
index ef643ca6d74c..67f929059e47 100644
--- a/mlir/lib/Bytecode/Writer/IRNumbering.cpp
+++ b/mlir/lib/Bytecode/Writer/IRNumbering.cpp
@@ -314,9 +314,22 @@ void IRNumberingState::number(Attribute attr) {
// If this attribute will be emitted using the bytecode format, perform a
// dummy writing to number any nested components.
- if (const auto *interface = numbering->dialect->interface) {
- // TODO: We don't allow custom encodings for mutable attributes right now.
- if (!attr.hasTrait<AttributeTrait::IsMutable>()) {
+ // TODO: We don't allow custom encodings for mutable attributes right now.
+ if (!attr.hasTrait<AttributeTrait::IsMutable>()) {
+ // Try overriding emission with callbacks.
+ for (const auto &callback : config.getAttributeWriterCallbacks()) {
+ NumberingDialectWriter writer(*this);
+ // The client has the ability to override the group name through the
+ // callback.
+ std::optional<StringRef> groupNameOverride;
+ if (succeeded(callback->write(attr, groupNameOverride, writer))) {
+ if (groupNameOverride.has_value())
+ numbering->dialect = &numberDialect(*groupNameOverride);
+ return;
+ }
+ }
+
+ if (const auto *interface = numbering->dialect->interface) {
NumberingDialectWriter writer(*this);
if (succeeded(interface->writeAttribute(attr, writer)))
return;
@@ -464,9 +477,24 @@ void IRNumberingState::number(Type type) {
// If this type will be emitted using the bytecode format, perform a dummy
// writing to number any nested components.
- if (const auto *interface = numbering->dialect->interface) {
- // TODO: We don't allow custom encodings for mutable types right now.
- if (!type.hasTrait<TypeTrait::IsMutable>()) {
+ // TODO: We don't allow custom encodings for mutable types right now.
+ if (!type.hasTrait<TypeTrait::IsMutable>()) {
+ // Try overriding emission with callbacks.
+ for (const auto &callback : config.getTypeWriterCallbacks()) {
+ NumberingDialectWriter writer(*this);
+ // The client has the ability to override the group name through the
+ // callback.
+ std::optional<StringRef> groupNameOverride;
+ if (succeeded(callback->write(type, groupNameOverride, writer))) {
+ if (groupNameOverride.has_value())
+ numbering->dialect = &numberDialect(*groupNameOverride);
+ return;
+ }
+ }
+
+ // If this attribute will be emitted using the bytecode format, perform a
+ // dummy writing to number any nested components.
+ if (const auto *interface = numbering->dialect->interface) {
NumberingDialectWriter writer(*this);
if (succeeded(interface->writeType(type, writer)))
return;