Skip to content

Commit

Permalink
Change the contract with the type/attribute parsing to let the dispat…
Browse files Browse the repository at this point in the history
…ch handle the mnemonic

This breaking change requires to remove printing the mnemonic in the print()
method on Type/Attribute classes.
This makes it consistent with the parsing code which alread handles the
mnemonic outside of the parsing method.

This likely won't break the build for anyone, but tests will start
failing for dialects downstream. The fix is trivial and look like
going from:

void emitc::OpaqueType::print(DialectAsmPrinter &printer) const {
  printer << "opaque<\"";

to:

void emitc::OpaqueAttr::print(DialectAsmPrinter &printer) const {
  printer << "<\"";

Reviewed By: rriddle, aartbik

Differential Revision: https://reviews.llvm.org/D113334
  • Loading branch information
joker-eph committed Nov 10, 2021
1 parent c27d85a commit f30a8a6
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mlir/include/mlir/Dialect/ArmSVE/ArmSVE.td
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def ScalableVectorType : ArmSVE_Type<"ScalableVector"> {
);

let printer = [{
$_printer << "vector<";
$_printer << "<";
for (int64_t dim : getShape())
$_printer << dim << 'x';
$_printer << getElementType() << '>';
Expand Down
1 change: 0 additions & 1 deletion mlir/lib/Dialect/Async/IR/Async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ static LogicalResult verify(AwaitOp op) {
#include "mlir/Dialect/Async/IR/AsyncOpsTypes.cpp.inc"

void ValueType::print(DialectAsmPrinter &printer) const {
printer << getMnemonic();
printer << "<";
printer.printType(getValueType());
printer << '>';
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void EmitCDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const {
}

void emitc::OpaqueAttr::print(DialectAsmPrinter &printer) const {
printer << "opaque<\"";
printer << "<\"";
llvm::printEscapedString(getValue(), printer.getStream());
printer << "\">";
}
Expand All @@ -228,7 +228,7 @@ Type emitc::OpaqueType::parse(DialectAsmParser &parser) {
}

void emitc::OpaqueType::print(DialectAsmPrinter &printer) const {
printer << "opaque<\"";
printer << "<\"";
llvm::printEscapedString(getValue(), printer.getStream());
printer << "\">";
}
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2426,7 +2426,7 @@ static constexpr const FastmathFlags fastmathFlagsList[] = {
};

void FMFAttr::print(DialectAsmPrinter &printer) const {
printer << "fastmath<";
printer << "<";
auto flags = llvm::make_filter_range(fastmathFlagsList, [&](auto flag) {
return bitEnumContains(this->getFlags(), flag);
});
Expand Down Expand Up @@ -2464,7 +2464,7 @@ Attribute FMFAttr::parse(DialectAsmParser &parser, Type type) {
}

void LinkageAttr::print(DialectAsmPrinter &printer) const {
printer << "linkage<";
printer << "<";
if (static_cast<uint64_t>(getLinkage()) <= getMaxEnumValForLinkage())
printer << stringifyEnum(getLinkage());
else
Expand Down Expand Up @@ -2580,7 +2580,7 @@ LoopOptionsAttr LoopOptionsAttr::get(MLIRContext *context,
}

void LoopOptionsAttr::print(DialectAsmPrinter &printer) const {
printer << getMnemonic() << "<";
printer << "<";
llvm::interleaveComma(getOptions(), printer, [&](auto option) {
printer << stringifyEnum(option.first) << " = ";
switch (option.first) {
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/PDL/IR/PDLTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Type RangeType::parse(DialectAsmParser &parser) {
}

void RangeType::print(DialectAsmPrinter &printer) const {
printer << "range<";
printer << "<";
(void)generatedTypePrinter(getElementType(), printer);
printer << ">";
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Attribute SparseTensorEncodingAttr::parse(DialectAsmParser &parser, Type type) {

void SparseTensorEncodingAttr::print(DialectAsmPrinter &printer) const {
// Print the struct-like storage in dictionary fashion.
printer << "encoding<{ dimLevelType = [ ";
printer << "<{ dimLevelType = [ ";
for (unsigned i = 0, e = getDimLevelType().size(); i < e; i++) {
switch (getDimLevelType()[i]) {
case DimLevelType::Dense:
Expand Down
13 changes: 6 additions & 7 deletions mlir/test/lib/Dialect/Test/TestAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Attribute AttrWithSelfTypeParamAttr::parse(DialectAsmParser &parser,
}

void AttrWithSelfTypeParamAttr::print(DialectAsmPrinter &printer) const {
printer << "attr_with_self_type_param " << getType();
printer << " " << getType();
}

//===----------------------------------------------------------------------===//
Expand All @@ -53,7 +53,7 @@ Attribute AttrWithTypeBuilderAttr::parse(DialectAsmParser &parser, Type type) {
}

void AttrWithTypeBuilderAttr::print(DialectAsmPrinter &printer) const {
printer << "attr_with_type_builder " << getAttr();
printer << " " << getAttr();
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -82,8 +82,7 @@ Attribute CompoundAAttr::parse(DialectAsmParser &parser, Type type) {
}

void CompoundAAttr::print(DialectAsmPrinter &printer) const {
printer << "cmpnd_a<" << getWidthOfSomething() << ", " << getOneType()
<< ", [";
printer << "<" << getWidthOfSomething() << ", " << getOneType() << ", [";
llvm::interleaveComma(getArrayOfInts(), printer);
printer << "]>";
}
Expand All @@ -110,7 +109,7 @@ Attribute TestI64ElementsAttr::parse(DialectAsmParser &parser, Type type) {
}

void TestI64ElementsAttr::print(DialectAsmPrinter &printer) const {
printer << "i64_elements<[";
printer << "<[";
llvm::interleaveComma(getElements(), printer);
printer << "] : " << getType() << ">";
}
Expand Down Expand Up @@ -177,8 +176,8 @@ Attribute TestSubElementsAccessAttr::parse(::mlir::DialectAsmParser &parser,

void TestSubElementsAccessAttr::print(
::mlir::DialectAsmPrinter &printer) const {
printer << getMnemonic() << "<" << getFirst() << ", " << getSecond() << ", "
<< getThird() << ">";
printer << "<" << getFirst() << ", " << getSecond() << ", " << getThird()
<< ">";
}

void TestSubElementsAccessAttr::walkImmediateSubElements(
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/lib/Dialect/Test/TestTypeDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def IntegerType : Test_Type<"TestInteger"> {

// We define the printer inline.
let printer = [{
$_printer << "int<";
$_printer << "<";
printSignedness($_printer, getImpl()->signedness);
$_printer << ", " << getImpl()->width << ">";
}];
Expand Down Expand Up @@ -122,7 +122,7 @@ class FieldInfo_Type<string name> : Test_Type<name> {
// Prints the type in this format:
// struct<[{field1Name, field1Type}, {field2Name, field2Type}]
let printer = [{
$_printer << "struct" << "<";
$_printer << "<";
for (size_t i=0, e = getImpl()->fields.size(); i < e; i++) {
const auto& field = getImpl()->fields[i];
$_printer << "{" << field.name << "," << field.type << "}";
Expand Down
5 changes: 2 additions & 3 deletions mlir/test/lib/Dialect/Test/TestTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ Type CompoundAType::parse(DialectAsmParser &parser) {
return get(parser.getContext(), widthOfSomething, oneType, arrayOfInts);
}
void CompoundAType::print(DialectAsmPrinter &printer) const {
printer << "cmpnd_a<" << getWidthOfSomething() << ", " << getOneType()
<< ", [";
printer << "<" << getWidthOfSomething() << ", " << getOneType() << ", [";
auto intArray = getArrayOfInts();
llvm::interleaveComma(intArray, printer);
printer << "]>";
Expand Down Expand Up @@ -150,7 +149,7 @@ Type TestTypeWithLayoutType::parse(DialectAsmParser &parser) {
}

void TestTypeWithLayoutType::print(DialectAsmPrinter &printer) const {
printer << "test_type_with_layout<" << getKey() << ">";
printer << "<" << getKey() << ">";
}

unsigned
Expand Down
8 changes: 3 additions & 5 deletions mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,13 +1007,11 @@ void DefGenerator::emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs) {
os << formatv(" .Case<{0}::{1}>([&]({0}::{1} t) {{\n ",
cppNamespace, cppClassName);

os << formatv("printer << {0}::{1}::getMnemonic();", cppNamespace,
cppClassName);
// If the def has no parameters and no printer, just print the mnemonic.
if (def.getNumParameters() == 0 && !def.getPrinterCode()) {
os << formatv("printer << {0}::{1}::getMnemonic();", cppNamespace,
cppClassName);
} else {
if (def.getNumParameters() != 0 || def.getPrinterCode())
os << "t.print(printer);";
}
os << "\n return ::mlir::success();\n })\n";
}
os << llvm::formatv(
Expand Down

0 comments on commit f30a8a6

Please sign in to comment.