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
12741313LogicalResult
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// ===----------------------------------------------------------------------===//
0 commit comments