Index: mlir/include/mlir/IR/OpBase.td =================================================================== --- mlir/include/mlir/IR/OpBase.td +++ mlir/include/mlir/IR/OpBase.td @@ -239,23 +239,20 @@ }]; // The call expression to emit the storage type to bytecode. + // Writes the attribute returned by `convertToAttribute` by default. // // Format: // - `$_storage` is the storage type value. // - `$_writer` is a `DialectBytecodeWriter`. - code writeToMlirBytecode = [{ - writeToMlirBytecode($_writer, $_storage) - }]; + code writeToMlirBytecode = ?; // The call expression to read the storage type from bytecode. + // Tries to read the attribute returned by `convertToAttribute` by default. // // Format: // - `$_storage` is the storage type value. // - `$_reader` is a `DialectBytecodeReader`. - code readFromMlirBytecode = [{ - if (::mlir::failed(readFromMlirBytecode($_reader, $_storage))) - return ::mlir::failure(); - }]; + code readFromMlirBytecode = ?; // Default value for the property. string defaultValue = ?; Index: mlir/include/mlir/TableGen/Property.h =================================================================== --- mlir/include/mlir/TableGen/Property.h +++ mlir/include/mlir/TableGen/Property.h @@ -58,13 +58,13 @@ // in the provided interface type and assign it to the storage. StringRef getConvertFromAttributeCall() const; - // Returns the method call which reads this property from + // If defined, returns the method call which reads this property from // bytecode and assign it to the storage. - StringRef getReadFromMlirBytecodeCall() const; + std::optional getReadFromMlirBytecodeCall() const; - // Returns the method call which write this property's + // If defined, returns the method call which write this property's // to the the bytecode. - StringRef getWriteToMlirBytecodeCall() const; + std::optional getWriteToMlirBytecodeCall() const; // Returns the code to compute the hash for this property. StringRef getHashPropertyCall() const; Index: mlir/lib/TableGen/Property.cpp =================================================================== --- mlir/lib/TableGen/Property.cpp +++ mlir/lib/TableGen/Property.cpp @@ -72,14 +72,18 @@ return getValueAsString(init); } -StringRef Property::getReadFromMlirBytecodeCall() const { +std::optional Property::getReadFromMlirBytecodeCall() const { const auto *init = def->getValueInit("readFromMlirBytecode"); - return getValueAsString(init); + if (const auto *stringInit = dyn_cast(init)) + return stringInit->getValue(); + return std::nullopt; } -StringRef Property::getWriteToMlirBytecodeCall() const { +std::optional Property::getWriteToMlirBytecodeCall() const { const auto *init = def->getValueInit("writeToMlirBytecode"); - return getValueAsString(init); + if (const auto *stringInit = dyn_cast(init)) + return stringInit->getValue(); + return std::nullopt; } StringRef Property::getHashPropertyCall() const { Index: mlir/test/lib/Dialect/Test/TestDialect.cpp =================================================================== --- mlir/test/lib/Dialect/Test/TestDialect.cpp +++ mlir/test/lib/Dialect/Test/TestDialect.cpp @@ -65,44 +65,6 @@ return hash_value(StringRef(content)); } -static LogicalResult readFromMlirBytecode(DialectBytecodeReader &reader, - MyPropStruct &prop) { - StringRef str; - if (failed(reader.readString(str))) - return failure(); - prop.content = str.str(); - return success(); -} - -static void writeToMlirBytecode(::mlir::DialectBytecodeWriter &writer, - MyPropStruct &prop) { - writer.writeOwnedString(prop.content); -} - -static LogicalResult readFromMlirBytecode(DialectBytecodeReader &reader, - MutableArrayRef prop) { - uint64_t size; - if (failed(reader.readVarInt(size))) - return failure(); - if (size != prop.size()) - return reader.emitError("array size mismach when reading properties: ") - << size << " vs expected " << prop.size(); - for (auto &elt : prop) { - uint64_t value; - if (failed(reader.readVarInt(value))) - return failure(); - elt = value; - } - return success(); -} - -static void writeToMlirBytecode(::mlir::DialectBytecodeWriter &writer, - ArrayRef prop) { - writer.writeVarInt(prop.size()); - for (auto elt : prop) - writer.writeVarInt(elt); -} - static LogicalResult setPropertiesFromAttribute(PropertiesWithCustomPrint &prop, Attribute attr, InFlightDiagnostic *diagnostic); Index: mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp =================================================================== --- mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -1357,10 +1357,34 @@ if (const auto *namedProperty = attrOrProp.dyn_cast()) { StringRef name = namedProperty->name; - FmtContext fctx; - fctx.addSubst("_reader", "reader") - .addSubst("_writer", "writer") - .addSubst("_storage", propertyStorage); + + std::string readFromMlirBytecode; + if (namedProperty->prop.getReadFromMlirBytecodeCall()) { + FmtContext fctx; + fctx.addSubst("_reader", "reader") + .addSubst("_storage", propertyStorage); + readFromMlirBytecode = + tgfmt(*namedProperty->prop.getReadFromMlirBytecodeCall(), &fctx); + } else { + const char *defaultByteCodeReaderFmt = R"decl( + ::mlir::Attribute attr; + if (::mlir::failed(reader.readAttribute(attr))) + return ::mlir::failure(); + if (::mlir::failed([&]{ + {0} + }())) + return ::mlir::failure(); + )decl"; + + FmtContext fctx; + fctx.addSubst("_diag", "nullptr") + .addSubst("_attr", "attr") + .addSubst("_storage", propertyStorage); + readFromMlirBytecode = formatv( + defaultByteCodeReaderFmt, + tgfmt(namedProperty->prop.getConvertFromAttributeCall(), &fctx)); + } + readPropertiesMethod << formatv( R"( {{ @@ -1373,8 +1397,24 @@ return ::mlir::failure(); } )", - name, - tgfmt(namedProperty->prop.getReadFromMlirBytecodeCall(), &fctx)); + name, readFromMlirBytecode); + + std::string writeToMlirBytecode; + if (namedProperty->prop.getWriteToMlirBytecodeCall()) { + FmtContext fctx; + fctx.addSubst("_writer", "writer") + .addSubst("_storage", propertyStorage); + writeToMlirBytecode = + tgfmt(*namedProperty->prop.getWriteToMlirBytecodeCall(), &fctx); + } else { + FmtContext fctx; + fctx.addSubst("_ctxt", "getContext()") + .addSubst("_storage", propertyStorage); + writeToMlirBytecode = formatv( + "writer.writeAttribute({0})", + tgfmt(namedProperty->prop.getConvertToAttributeCall(), &fctx)); + } + writePropertiesMethod << formatv( R"( {{ @@ -1382,7 +1422,7 @@ {1}; } )", - name, tgfmt(namedProperty->prop.getWriteToMlirBytecodeCall(), &fctx)); + name, writeToMlirBytecode); continue; } const auto *namedAttr = attrOrProp.dyn_cast();