Skip to content

Commit fab9e00

Browse files
committed
first shim
1 parent f409340 commit fab9e00

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

mlir/include/mlir/Bytecode/BytecodeImplementation.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "mlir/IR/OpImplementation.h"
2222
#include "llvm/ADT/STLExtras.h"
2323
#include "llvm/ADT/Twine.h"
24+
#include "llvm/Support/ErrorHandling.h"
2425

2526
namespace mlir {
2627
//===--------------------------------------------------------------------===//
@@ -445,6 +446,14 @@ class BytecodeDialectInterface
445446
return Type();
446447
}
447448

449+
/// Fall back to an operation of this type if parsing an op from bytecode
450+
/// fails for any reason. This can be used to handle new ops emitted from a
451+
/// different version of the dialect, that cannot be read by an older version
452+
/// of the dialect.
453+
virtual FailureOr<OperationName> getFallbackOperationName() const {
454+
return failure();
455+
}
456+
448457
//===--------------------------------------------------------------------===//
449458
// Writing
450459
//===--------------------------------------------------------------------===//

mlir/include/mlir/Bytecode/BytecodeOpInterface.td

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,28 @@ def BytecodeOpInterface : OpInterface<"BytecodeOpInterface"> {
4040
];
4141
}
4242

43+
// `FallbackBytecodeOpInterface`
44+
def FallbackBytecodeOpInterface : OpInterface<"FallbackBytecodeOpInterface"> {
45+
let description = [{
46+
This interface allows fallback operations direct access to the bytecode
47+
property streams.
48+
}];
49+
let cppNamespace = "::mlir";
50+
51+
let methods = [
52+
StaticInterfaceMethod<[{
53+
Read the properties blob for this operation from the bytecode and populate the state.
54+
}],
55+
"LogicalResult", "readPropertiesBlob", (ins
56+
"ArrayRef<char>":$blob,
57+
"::mlir::OperationState &":$state)
58+
>,
59+
InterfaceMethod<[{
60+
Get the properties blob for this operation to be emitted into the bytecode.
61+
}],
62+
"ArrayRef<char>", "getPropertiesBlob", (ins)
63+
>,
64+
];
65+
}
66+
4367
#endif // MLIR_BYTECODE_BYTECODEOPINTERFACES

mlir/lib/Bytecode/Reader/BytecodeReader.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,13 +1106,18 @@ class PropertiesSectionReader {
11061106
dialectReader.withEncodingReader(reader).readBlob(rawProperties)))
11071107
return failure();
11081108
}
1109+
1110+
// If the op is a "fallback" op, give it a handle to the raw properties
1111+
// buffer.
1112+
if (auto *iface = opName->getInterface<FallbackBytecodeOpInterface>())
1113+
return iface->readPropertiesBlob(rawProperties, opState);
1114+
11091115
// Setup a new reader to read from the `rawProperties` sub-buffer.
11101116
EncodingReader reader(
11111117
StringRef(rawProperties.begin(), rawProperties.size()), fileLoc);
11121118
DialectReader propReader = dialectReader.withEncodingReader(reader);
11131119

1114-
auto *iface = opName->getInterface<BytecodeOpInterface>();
1115-
if (iface)
1120+
if (auto *iface = opName->getInterface<BytecodeOpInterface>())
11161121
return iface->readProperties(propReader, opState);
11171122
if (opName->isRegistered())
11181123
return propReader.emitError(
@@ -1506,7 +1511,7 @@ class mlir::BytecodeReader::Impl {
15061511
UseListOrderStorage(bool isIndexPairEncoding,
15071512
SmallVector<unsigned, 4> &&indices)
15081513
: indices(std::move(indices)),
1509-
isIndexPairEncoding(isIndexPairEncoding){};
1514+
isIndexPairEncoding(isIndexPairEncoding) {};
15101515
/// The vector containing the information required to reorder the
15111516
/// use-list of a value.
15121517
SmallVector<unsigned, 4> indices;
@@ -1863,10 +1868,18 @@ BytecodeReader::Impl::parseOpName(EncodingReader &reader,
18631868
// Load the dialect and its version.
18641869
DialectReader dialectReader(attrTypeReader, stringReader, resourceReader,
18651870
dialectsMap, reader, version);
1866-
if (failed(opName->dialect->load(dialectReader, getContext())))
1871+
if (succeeded(opName->dialect->load(dialectReader, getContext()))) {
1872+
opName->opName.emplace(
1873+
(opName->dialect->name + "." + opName->name).str(), getContext());
1874+
} else if (auto fallbackOp =
1875+
opName->dialect->interface->getFallbackOperationName();
1876+
succeeded(fallbackOp)) {
1877+
// If the dialect's bytecode interface specifies a fallback op, we want
1878+
// to use that instead of an unregistered op.
1879+
opName->opName.emplace(*fallbackOp);
1880+
} else {
18671881
return failure();
1868-
opName->opName.emplace((opName->dialect->name + "." + opName->name).str(),
1869-
getContext());
1882+
}
18701883
}
18711884
}
18721885
return *opName->opName;

mlir/lib/Bytecode/Writer/BytecodeWriter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,13 @@ class PropertiesSectionBuilder {
518518
return emit(scratch);
519519
}
520520

521+
if (auto iface = dyn_cast<FallbackBytecodeOpInterface>(op)) {
522+
// Fallback ops should write the properties payload to the bytecode buffer
523+
// directly.
524+
ArrayRef<char> encodedProperties = iface.getPropertiesBlob();
525+
return emit(encodedProperties);
526+
}
527+
521528
EncodingEmitter emitter;
522529
DialectWriter propertiesWriter(config.bytecodeVersion, emitter,
523530
numberingState, stringSection,

0 commit comments

Comments
 (0)