Skip to content

Commit 1de4485

Browse files
committed
fix parsing & writing to be bitwise exact
1 parent d2ad942 commit 1de4485

File tree

6 files changed

+105
-56
lines changed

6 files changed

+105
-56
lines changed

mlir/include/mlir/Bytecode/BytecodeOpInterface.td

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,7 @@ def FallbackBytecodeOpInterface : OpInterface<"FallbackBytecodeOpInterface"> {
6060
Get the original name for this operation from the bytecode.
6161
}],
6262
"::mlir::StringRef", "getOriginalOperationName", (ins)
63-
>,
64-
StaticInterfaceMethod<[{
65-
Read the properties blob for this operation from the bytecode and populate the state.
66-
}],
67-
"::mlir::LogicalResult", "readPropertiesBlob", (ins
68-
"::mlir::ArrayRef<char>":$blob,
69-
"::mlir::OperationState &":$state)
70-
>,
71-
InterfaceMethod<[{
72-
Get the properties blob for this operation to be emitted into the bytecode.
73-
}],
74-
"::mlir::ArrayRef<char>", "getPropertiesBlob", (ins)
75-
>,
63+
>
7664
];
7765
}
7866

mlir/lib/Bytecode/Reader/BytecodeReader.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,11 +1118,6 @@ class PropertiesSectionReader {
11181118
return failure();
11191119
}
11201120

1121-
// If the op is a "fallback" op, give it a handle to the raw properties
1122-
// buffer.
1123-
if (auto *iface = opName->getInterface<FallbackBytecodeOpInterface>())
1124-
return iface->readPropertiesBlob(rawProperties, opState);
1125-
11261121
// Setup a new reader to read from the `rawProperties` sub-buffer.
11271122
EncodingReader reader(
11281123
StringRef(rawProperties.begin(), rawProperties.size()), fileLoc);
@@ -2182,6 +2177,9 @@ BytecodeReader::Impl::parseRegions(std::vector<RegionReadState> &regionStack,
21822177

21832178
// Parse the bytecode.
21842179
{
2180+
// If the op is registered (and serialized in a compatible manner), or
2181+
// unregistered but uses standard properties encoding, parsing without
2182+
// going through the fallback path should work.
21852183
EncodingReader::Snapshot snapshot(reader);
21862184
op = parseOpWithoutRegions(reader, readState, isIsolatedFromAbove,
21872185
/*useDialectFallback=*/false);

mlir/lib/Bytecode/Writer/BytecodeWriter.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,6 @@ 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-
528521
EncodingEmitter emitter;
529522
DialectWriter propertiesWriter(config.bytecodeVersion, emitter,
530523
numberingState, stringSection,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: mlir-opt %s --emit-bytecode > %T/versioning-fallback.mlirbc
2+
"test.versionedD"() <{attribute = #test.attr_params<42, 24>}> : () -> ()
3+
4+
// COM: check that versionedD was parsed as a fallback op.
5+
// RUN: mlir-opt %T/versioning-fallback.mlirbc | FileCheck %s --check-prefix=CHECK-PARSE
6+
// CHECK-PARSE: test.bytecode.fallback
7+
// CHECK-PARSE-SAME: opname = "test.versionedD"
8+
9+
// COM: check that the bytecode roundtrip was successful
10+
// RUN: mlir-opt %T/versioning-fallback.mlirbc --verify-roundtrip
11+
12+
// COM: check that the bytecode roundtrip is bitwise exact
13+
// RUN: mlir-opt %T/versioning-fallback.mlirbc --emit-bytecode | diff %T/versioning-fallback.mlirbc -

mlir/test/lib/Dialect/Test/TestOpDefs.cpp

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
#include "TestOps.h"
1111
#include "mlir/Bytecode/BytecodeImplementation.h"
1212
#include "mlir/Dialect/Tensor/IR/Tensor.h"
13+
#include "mlir/IR/Attributes.h"
1314
#include "mlir/IR/BuiltinAttributes.h"
15+
#include "mlir/IR/BuiltinTypes.h"
1416
#include "mlir/IR/Verifier.h"
1517
#include "mlir/Interfaces/FunctionImplementation.h"
1618
#include "mlir/Interfaces/MemorySlotInterfaces.h"
19+
#include "llvm/ADT/StringRef.h"
1720
#include "llvm/Support/LogicalResult.h"
1821
#include <cstdint>
1922

@@ -1238,24 +1241,60 @@ void TestVersionedOpA::writeProperties(mlir::DialectBytecodeWriter &writer) {
12381241
// TestVersionedOpD
12391242
//===----------------------------------------------------------------------===//
12401243

1241-
// LogicalResult
1242-
// TestVersionedOpD::readProperties(mlir::DialectBytecodeReader &reader,
1243-
// mlir::OperationState &state) {
1244-
// auto &prop = state.getOrAddProperties<Properties>();
1245-
// StringRef res;
1246-
// if (failed(reader.readString(res)))
1247-
// return failure();
1248-
// if (failed(reader.readAttribute(prop.attribute)))
1249-
// return failure();
1244+
LogicalResult
1245+
TestVersionedOpD::readProperties(mlir::DialectBytecodeReader &reader,
1246+
mlir::OperationState &state) {
1247+
// Always fail so that this uses the fallback path.
1248+
return failure();
1249+
}
1250+
1251+
struct FallbackCompliantPropertiesEncoding {
1252+
int64_t version;
1253+
SmallVector<Attribute> requiredAttributes;
1254+
SmallVector<Attribute> optionalAttributes;
1255+
1256+
void writeProperties(DialectBytecodeWriter &writer) const {
1257+
// Write the op version.
1258+
writer.writeSignedVarInt(version);
1259+
1260+
// Write the required attributes.
1261+
writer.writeList(requiredAttributes,
1262+
[&](Attribute attr) { writer.writeAttribute(attr); });
1263+
1264+
// Write the optional attributes.
1265+
writer.writeList(optionalAttributes, [&](Attribute attr) {
1266+
writer.writeOptionalAttribute(attr);
1267+
});
1268+
}
1269+
1270+
LogicalResult readProperties(DialectBytecodeReader &reader) {
1271+
// Read the op version.
1272+
if (failed(reader.readSignedVarInt(version)))
1273+
return failure();
12501274

1251-
// return success();
1252-
// }
1275+
// Read the required attributes.
1276+
if (failed(reader.readList(requiredAttributes, [&](Attribute &attr) {
1277+
return reader.readAttribute(attr);
1278+
})))
1279+
return failure();
12531280

1254-
// void TestVersionedOpD::writeProperties(mlir::DialectBytecodeWriter &writer) {
1255-
// auto &prop = getProperties();
1256-
// writer.writeOwnedString("version 1");
1257-
// writer.writeAttribute(prop.attribute);
1258-
// }
1281+
// Read the optional attributes.
1282+
if (failed(reader.readList(optionalAttributes, [&](Attribute &attr) {
1283+
return reader.readOptionalAttribute(attr);
1284+
})))
1285+
return failure();
1286+
1287+
return success();
1288+
}
1289+
};
1290+
1291+
void TestVersionedOpD::writeProperties(mlir::DialectBytecodeWriter &writer) {
1292+
FallbackCompliantPropertiesEncoding encoding{
1293+
.version = 1,
1294+
.requiredAttributes = {getAttribute()},
1295+
.optionalAttributes = {}};
1296+
encoding.writeProperties(writer);
1297+
}
12591298

12601299
//===----------------------------------------------------------------------===//
12611300
// TestBytecodeFallbackOp
@@ -1272,17 +1311,30 @@ StringRef TestBytecodeFallbackOp::getOriginalOperationName() {
12721311
}
12731312

12741313
LogicalResult
1275-
TestBytecodeFallbackOp::readPropertiesBlob(ArrayRef<char> blob,
1276-
OperationState &state) {
1277-
state.getOrAddProperties<Properties>().bytecodeProperties =
1278-
DenseI8ArrayAttr::get(state.getContext(),
1279-
ArrayRef((const int8_t *)blob.data(), blob.size()));
1314+
TestBytecodeFallbackOp::readProperties(DialectBytecodeReader &reader,
1315+
OperationState &state) {
1316+
FallbackCompliantPropertiesEncoding encoding;
1317+
if (failed(encoding.readProperties(reader)))
1318+
return failure();
1319+
1320+
auto &props = state.getOrAddProperties<Properties>();
1321+
props.opversion = encoding.version;
1322+
props.encodedReqdAttributes =
1323+
ArrayAttr::get(state.getContext(), encoding.requiredAttributes);
1324+
props.encodedOptAttributes =
1325+
ArrayAttr::get(state.getContext(), encoding.optionalAttributes);
1326+
12801327
return success();
12811328
}
12821329

1283-
ArrayRef<char> TestBytecodeFallbackOp::getPropertiesBlob() {
1284-
return ArrayRef((const char *)getBytecodeProperties().data(),
1285-
getBytecodeProperties().size());
1330+
void TestBytecodeFallbackOp::writeProperties(DialectBytecodeWriter &writer) {
1331+
FallbackCompliantPropertiesEncoding encoding{
1332+
.version = getOpversion(),
1333+
.requiredAttributes =
1334+
llvm::to_vector(getEncodedReqdAttributes().getValue()),
1335+
.optionalAttributes =
1336+
llvm::to_vector(getEncodedOptAttributes().getValue())};
1337+
encoding.writeProperties(writer);
12861338
}
12871339

12881340
//===----------------------------------------------------------------------===//

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,23 +3030,28 @@ def TestVersionedOpC : TEST_Op<"versionedC"> {
30303030
);
30313031
}
30323032

3033-
// def TestVersionedOpD : TEST_Op<"versionedD"> {
3034-
// let arguments = (ins AnyAttrOf<[TestAttrParams,
3035-
// I32ElementsAttr]>:$attribute
3036-
// );
3033+
// This op is used to generate tests for the bytecode dialect fallback path.
3034+
def TestVersionedOpD : TEST_Op<"versionedD"> {
3035+
let arguments = (ins AnyAttrOf<[TestAttrParams,
3036+
I32ElementsAttr]>:$attribute
3037+
);
30373038

3038-
// let useCustomPropertiesEncoding = 1;
3039-
// }
3039+
let useCustomPropertiesEncoding = 1;
3040+
}
30403041

30413042
def TestBytecodeFallbackOp : TEST_Op<"bytecode.fallback", [
30423043
DeclareOpInterfaceMethods<FallbackBytecodeOpInterface, ["setOriginalOperationName", "readPropertiesBlob", "getPropertiesBlob"]>
30433044
]> {
30443045
let arguments = (ins
30453046
StrAttr:$opname,
3046-
DenseI8ArrayAttr:$bytecodeProperties,
3047+
IntProp<"int64_t">:$opversion,
3048+
ArrayAttr:$encodedReqdAttributes,
3049+
ArrayAttr:$encodedOptAttributes,
30473050
Variadic<AnyType>:$operands);
30483051
let regions = (region VariadicRegion<AnyRegion>:$bodyRegions);
30493052
let results = (outs Variadic<AnyType>:$results);
3053+
3054+
let useCustomPropertiesEncoding = 1;
30503055
}
30513056

30523057
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)