Skip to content

Commit

Permalink
Emit the boilerplate for Type printer/parser dialect dispatching from…
Browse files Browse the repository at this point in the history
… ODS

Add a new `useDefaultTypePrinterParser` boolean settings on the dialect
(default to false for now) that emits the boilerplate to dispatch type
parsing/printing to the auto-generated method.
We will likely turn this on by default in the future.

Differential Revision: https://reviews.llvm.org/D113332
  • Loading branch information
joker-eph committed Nov 10, 2021
1 parent fd6b404 commit c27d85a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitCBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def EmitC_Dialect : Dialect {
}];

let hasConstantMaterializer = 1;
let useDefaultTypePrinterParser = 1;
}

#endif // MLIR_DIALECT_EMITC_IR_EMITCBASE
4 changes: 4 additions & 0 deletions mlir/include/mlir/IR/OpBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ class Dialect {
// it'll dispatch the parsing to every individual attributes directly.
bit useDefaultAttributePrinterParser = 0;

// If this dialect should use default generated type parser boilerplate:
// it'll dispatch the parsing to every individual types directly.
bit useDefaultTypePrinterParser = 0;

// If this dialect overrides the hook for canonicalization patterns.
bit hasCanonicalizer = 0;

Expand Down
4 changes: 4 additions & 0 deletions mlir/include/mlir/TableGen/Dialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class Dialect {
/// attribute printing/parsing.
bool useDefaultAttributePrinterParser() const;

/// Returns true if this dialect should generate the default dispatch for
/// type printing/parsing.
bool useDefaultTypePrinterParser() const;

// Returns whether two dialects are equal by checking the equality of the
// underlying record.
bool operator==(const Dialect &other) const;
Expand Down
19 changes: 0 additions & 19 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,6 @@ Type emitc::OpaqueType::parse(DialectAsmParser &parser) {
return get(parser.getContext(), value);
}

Type EmitCDialect::parseType(DialectAsmParser &parser) const {
llvm::SMLoc typeLoc = parser.getCurrentLocation();
StringRef mnemonic;
if (parser.parseKeyword(&mnemonic))
return Type();
Type genType;
OptionalParseResult parseResult =
generatedTypeParser(parser, mnemonic, genType);
if (parseResult.hasValue())
return genType;
parser.emitError(typeLoc, "unknown type in EmitC dialect");
return Type();
}

void EmitCDialect::printType(Type type, DialectAsmPrinter &os) const {
if (failed(generatedTypePrinter(type, os)))
llvm_unreachable("unexpected 'EmitC' type kind");
}

void emitc::OpaqueType::print(DialectAsmPrinter &printer) const {
printer << "opaque<\"";
llvm::printEscapedString(getValue(), printer.getStream());
Expand Down
4 changes: 4 additions & 0 deletions mlir/lib/TableGen/Dialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ bool Dialect::useDefaultAttributePrinterParser() const {
return def->getValueAsBit("useDefaultAttributePrinterParser");
}

bool Dialect::useDefaultTypePrinterParser() const {
return def->getValueAsBit("useDefaultTypePrinterParser");
}

Dialect::EmitPrefix Dialect::getEmitAccessorPrefix() const {
int prefix = def->getValueAsInt("emitAccessorPrefix");
if (prefix < 0 || prefix > static_cast<int>(EmitPrefix::Both))
Expand Down
32 changes: 32 additions & 0 deletions mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,32 @@ void {0}::printAttribute(::mlir::Attribute attr,
}
)";

/// The code block for default type parser/printer dispatch boilerplate.
/// {0}: the dialect fully qualified class name.
static const char *const dialectDefaultTypePrinterParserDispatch = R"(
/// Parse a type registered to this dialect.
::mlir::Type {0}::parseType(::mlir::DialectAsmParser &parser) const {{
llvm::SMLoc typeLoc = parser.getCurrentLocation();
StringRef mnemonic;
if (parser.parseKeyword(&mnemonic))
return Type();
Type genType;
OptionalParseResult parseResult =
generatedTypeParser(parser, mnemonic, genType);
if (parseResult.hasValue())
return genType;
parser.emitError(typeLoc) << "unknown type `"
<< mnemonic << "` in dialect `" << getNamespace() << "`";
return {{};
}
/// Print a type registered to this dialect.
void {0}::printType(::mlir::Type type,
::mlir::DialectAsmPrinter &printer) const {{
if (succeeded(generatedTypePrinter(type, printer)))
return;
}
)";

/// The code block used to start the auto-generated printer function.
///
/// {0}: The name of the base value type, e.g. Attribute or Type.
Expand Down Expand Up @@ -1020,6 +1046,12 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
os << llvm::formatv(dialectDefaultAttrPrinterParserDispatch,
defs.front().getDialect().getCppClassName());

// Emit the default parser/printer for Types if the dialect asked for it.
if (valueType == "Type" &&
defs.front().getDialect().useDefaultTypePrinterParser())
os << llvm::formatv(dialectDefaultTypePrinterParserDispatch,
defs.front().getDialect().getCppClassName());

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/tools/mlir-tblgen/DialectGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ static void emitDialectDecl(Dialect &dialect,
// add the hooks for parsing/printing.
if (!dialectAttrs.empty() || dialect.useDefaultAttributePrinterParser())
os << attrParserDecl;
if (!dialectTypes.empty())
if (!dialectTypes.empty() || dialect.useDefaultTypePrinterParser())
os << typeParserDecl;

// Add the decls for the various features of the dialect.
Expand Down

0 comments on commit c27d85a

Please sign in to comment.