diff --git a/mlir/include/mlir/IR/BuiltinDialect.td b/mlir/include/mlir/IR/BuiltinDialect.td --- a/mlir/include/mlir/IR/BuiltinDialect.td +++ b/mlir/include/mlir/IR/BuiltinDialect.td @@ -19,9 +19,10 @@ def Builtin_Dialect : Dialect { let summary = "A dialect containing the builtin Attributes, Operations, and Types"; - let name = "builtin"; let cppNamespace = "::mlir"; + let useDefaultAttributePrinterParser = 0; + let useDefaultTypePrinterParser = 0; let extraClassDeclaration = [{ private: // Register the builtin Attributes. diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -314,11 +314,11 @@ // If this dialect should use default generated attribute parser boilerplate: // it'll dispatch the parsing to every individual attributes directly. - bit useDefaultAttributePrinterParser = 0; + bit useDefaultAttributePrinterParser = 1; // If this dialect should use default generated type parser boilerplate: // it'll dispatch the parsing to every individual types directly. - bit useDefaultTypePrinterParser = 0; + bit useDefaultTypePrinterParser = 1; // If this dialect overrides the hook for canonicalization patterns. bit hasCanonicalizer = 0; diff --git a/mlir/lib/Dialect/Async/IR/Async.cpp b/mlir/lib/Dialect/Async/IR/Async.cpp --- a/mlir/lib/Dialect/Async/IR/Async.cpp +++ b/mlir/lib/Dialect/Async/IR/Async.cpp @@ -346,22 +346,3 @@ } return ValueType::get(ty); } - -/// Print a type registered to this dialect. -void AsyncDialect::printType(Type type, DialectAsmPrinter &os) const { - if (failed(generatedTypePrinter(type, os))) - llvm_unreachable("unexpected 'async' type kind"); -} - -/// Parse a type registered to this dialect. -Type AsyncDialect::parseType(DialectAsmParser &parser) const { - StringRef typeTag; - if (parser.parseKeyword(&typeTag)) - return Type(); - Type genType; - auto parseResult = generatedTypeParser(parser, typeTag, genType); - if (parseResult.hasValue()) - return genType; - parser.emitError(parser.getNameLoc(), "unknown async type: ") << typeTag; - return {}; -} diff --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp --- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp +++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp @@ -180,26 +180,6 @@ return get(parser.getContext(), value); } -Attribute EmitCDialect::parseAttribute(DialectAsmParser &parser, - Type type) const { - llvm::SMLoc typeLoc = parser.getCurrentLocation(); - StringRef mnemonic; - if (parser.parseKeyword(&mnemonic)) - return Attribute(); - Attribute genAttr; - OptionalParseResult parseResult = - generatedAttributeParser(parser, mnemonic, type, genAttr); - if (parseResult.hasValue()) - return genAttr; - parser.emitError(typeLoc, "unknown attribute in EmitC dialect"); - return Attribute(); -} - -void EmitCDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const { - if (failed(generatedAttributePrinter(attr, os))) - llvm_unreachable("unexpected 'EmitC' attribute kind"); -} - void emitc::OpaqueAttr::print(AsmPrinter &printer) const { printer << "<\""; llvm::printEscapedString(getValue(), printer.getStream()); diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp --- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp +++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp @@ -2906,28 +2906,3 @@ llvm::sort(options, llvm::less_first()); return get(parser.getContext(), options); } - -Attribute LLVMDialect::parseAttribute(DialectAsmParser &parser, - Type type) const { - if (type) { - parser.emitError(parser.getNameLoc(), "unexpected type"); - return {}; - } - StringRef attrKind; - if (parser.parseKeyword(&attrKind)) - return {}; - { - Attribute attr; - auto parseResult = generatedAttributeParser(parser, attrKind, type, attr); - if (parseResult.hasValue()) - return attr; - } - parser.emitError(parser.getNameLoc(), "unknown attribute type: ") << attrKind; - return {}; -} - -void LLVMDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const { - if (succeeded(generatedAttributePrinter(attr, os))) - return; - llvm_unreachable("Unknown attribute type"); -} diff --git a/mlir/lib/Dialect/PDL/IR/PDLTypes.cpp b/mlir/lib/Dialect/PDL/IR/PDLTypes.cpp --- a/mlir/lib/Dialect/PDL/IR/PDLTypes.cpp +++ b/mlir/lib/Dialect/PDL/IR/PDLTypes.cpp @@ -53,15 +53,6 @@ return Type(); } -Type PDLDialect::parseType(DialectAsmParser &parser) const { - return parsePDLType(parser); -} - -void PDLDialect::printType(Type type, DialectAsmPrinter &printer) const { - if (failed(generatedTypePrinter(type, printer))) - llvm_unreachable("unknown 'pdl' type"); -} - //===----------------------------------------------------------------------===// // PDL Types //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp --- a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp +++ b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp @@ -346,22 +346,3 @@ #define GET_OP_CLASSES #include "mlir/Dialect/SparseTensor/IR/SparseTensorOps.cpp.inc" - -Attribute SparseTensorDialect::parseAttribute(DialectAsmParser &parser, - Type type) const { - StringRef attrTag; - if (failed(parser.parseKeyword(&attrTag))) - return Attribute(); - Attribute attr; - auto parseResult = generatedAttributeParser(parser, attrTag, type, attr); - if (parseResult.hasValue()) - return attr; - parser.emitError(parser.getNameLoc(), "unknown sparse tensor attribute"); - return Attribute(); -} - -void SparseTensorDialect::printAttribute(Attribute attr, - DialectAsmPrinter &printer) const { - if (succeeded(generatedAttributePrinter(attr, printer))) - return; -} diff --git a/mlir/test/lib/Dialect/Test/TestDialect.td b/mlir/test/lib/Dialect/Test/TestDialect.td --- a/mlir/test/lib/Dialect/Test/TestDialect.td +++ b/mlir/test/lib/Dialect/Test/TestDialect.td @@ -22,7 +22,7 @@ let hasRegionResultAttrVerify = 1; let hasOperationInterfaceFallback = 1; let hasNonDefaultDestructor = 1; - let useDefaultAttributePrinterParser = 1; + let useDefaultTypePrinterParser = 0; let dependentDialects = ["::mlir::DLTIDialect"]; let extraClassDeclaration = [{ @@ -36,6 +36,9 @@ ::mlir::OpAsmPrinter &printer)> getOperationPrinter(::mlir::Operation *op) const override; + ::mlir::Type parseType(::mlir::DialectAsmParser &parser) const override; + void printType(::mlir::Type type, + ::mlir::DialectAsmPrinter &printer) const override; private: // Storage for a custom fallback interface. void *fallbackEffectOpInterfaces; diff --git a/mlir/test/mlir-tblgen/attr-or-type-format.td b/mlir/test/mlir-tblgen/attr-or-type-format.td --- a/mlir/test/mlir-tblgen/attr-or-type-format.td +++ b/mlir/test/mlir-tblgen/attr-or-type-format.td @@ -7,6 +7,7 @@ def Test_Dialect : Dialect { let name = "TestDialect"; let cppNamespace = "::test"; + let useDefaultTypePrinterParser = 0; } class TestAttr : AttrDef; diff --git a/mlir/test/mlir-tblgen/typedefs.td b/mlir/test/mlir-tblgen/typedefs.td --- a/mlir/test/mlir-tblgen/typedefs.td +++ b/mlir/test/mlir-tblgen/typedefs.td @@ -34,7 +34,6 @@ def Test_Dialect: Dialect { // DECL-NOT: TestDialect -// DEF-NOT: TestDialect let name = "TestDialect"; let cppNamespace = "::test"; } diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp --- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp +++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp @@ -616,9 +616,11 @@ protected: DefGenerator(std::vector &&defs, raw_ostream &os, - StringRef defType, StringRef valueType, bool isAttrGenerator) + StringRef defType, StringRef valueType, bool isAttrGenerator, + bool needsDialectParserPrinter) : defRecords(std::move(defs)), os(os), defType(defType), - valueType(valueType), isAttrGenerator(isAttrGenerator) {} + valueType(valueType), isAttrGenerator(isAttrGenerator), + needsDialectParserPrinter(needsDialectParserPrinter) {} /// Emit the list of def type names. void emitTypeDefList(ArrayRef defs); @@ -637,19 +639,29 @@ /// Flag indicating if this generator is for Attributes. False if the /// generator is for types. bool isAttrGenerator; + /// Track if we need to emit the printAttribute/parseAttribute + /// implementations. + bool needsDialectParserPrinter; }; /// A specialized generator for AttrDefs. struct AttrDefGenerator : public DefGenerator { AttrDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os) : DefGenerator(records.getAllDerivedDefinitions("AttrDef"), os, "Attr", - "Attribute", /*isAttrGenerator=*/true) {} + "Attribute", + /*isAttrGenerator=*/true, + /*needsDialectParserPrinter=*/ + !records.getAllDerivedDefinitions("DialectAttr").empty()) { + } }; /// A specialized generator for TypeDefs. struct TypeDefGenerator : public DefGenerator { TypeDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os) : DefGenerator(records.getAllDerivedDefinitions("TypeDef"), os, "Type", - "Type", /*isAttrGenerator=*/false) {} + "Type", /*isAttrGenerator=*/false, + /*needsDialectParserPrinter=*/ + !records.getAllDerivedDefinitions("DialectType").empty()) { + } }; } // namespace @@ -860,7 +872,7 @@ Dialect firstDialect = defs.front().getDialect(); // Emit the default parser/printer for Attributes if the dialect asked for // it. - if (valueType == "Attribute" && + if (valueType == "Attribute" && needsDialectParserPrinter && firstDialect.useDefaultAttributePrinterParser()) { NamespaceEmitter nsEmitter(os, firstDialect); os << llvm::formatv(dialectDefaultAttrPrinterParserDispatch, @@ -868,7 +880,8 @@ } // Emit the default parser/printer for Types if the dialect asked for it. - if (valueType == "Type" && firstDialect.useDefaultTypePrinterParser()) { + if (valueType == "Type" && needsDialectParserPrinter && + firstDialect.useDefaultTypePrinterParser()) { NamespaceEmitter nsEmitter(os, firstDialect); os << llvm::formatv(dialectDefaultTypePrinterParserDispatch, firstDialect.getCppClassName()); diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp --- a/mlir/tools/mlir-tblgen/DialectGen.cpp +++ b/mlir/tools/mlir-tblgen/DialectGen.cpp @@ -210,9 +210,9 @@ // Check for any attributes/types registered to this dialect. If there are, // add the hooks for parsing/printing. - if (!dialectAttrs.empty() || dialect.useDefaultAttributePrinterParser()) + if (!dialectAttrs.empty() && dialect.useDefaultAttributePrinterParser()) os << attrParserDecl; - if (!dialectTypes.empty() || dialect.useDefaultTypePrinterParser()) + if (!dialectTypes.empty() && dialect.useDefaultTypePrinterParser()) os << typeParserDecl; // Add the decls for the various features of the dialect.