[Mlir-commits] [mlir] f30a8a6 - Change the contract with the type/attribute parsing to let the dispatch handle the mnemonic

Mehdi Amini llvmlistbot at llvm.org
Tue Nov 9 16:49:11 PST 2021


Author: Mehdi Amini
Date: 2021-11-10T00:47:15Z
New Revision: f30a8a6f6740c1348928090f7749b638bf0f646a

URL: https://github.com/llvm/llvm-project/commit/f30a8a6f6740c1348928090f7749b638bf0f646a
DIFF: https://github.com/llvm/llvm-project/commit/f30a8a6f6740c1348928090f7749b638bf0f646a.diff

LOG: Change the contract with the type/attribute parsing to let the dispatch 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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/ArmSVE/ArmSVE.td
    mlir/lib/Dialect/Async/IR/Async.cpp
    mlir/lib/Dialect/EmitC/IR/EmitC.cpp
    mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
    mlir/lib/Dialect/PDL/IR/PDLTypes.cpp
    mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
    mlir/test/lib/Dialect/Test/TestAttributes.cpp
    mlir/test/lib/Dialect/Test/TestTypeDefs.td
    mlir/test/lib/Dialect/Test/TestTypes.cpp
    mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/ArmSVE/ArmSVE.td b/mlir/include/mlir/Dialect/ArmSVE/ArmSVE.td
index fdb43afe98662..b3d41401b1394 100644
--- a/mlir/include/mlir/Dialect/ArmSVE/ArmSVE.td
+++ b/mlir/include/mlir/Dialect/ArmSVE/ArmSVE.td
@@ -67,7 +67,7 @@ def ScalableVectorType : ArmSVE_Type<"ScalableVector"> {
   );
 
   let printer = [{
-    $_printer << "vector<";
+    $_printer << "<";
     for (int64_t dim : getShape())
       $_printer << dim << 'x';
     $_printer << getElementType() << '>';

diff  --git a/mlir/lib/Dialect/Async/IR/Async.cpp b/mlir/lib/Dialect/Async/IR/Async.cpp
index 8728c4a8b4406..0582980e75635 100644
--- a/mlir/lib/Dialect/Async/IR/Async.cpp
+++ b/mlir/lib/Dialect/Async/IR/Async.cpp
@@ -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 << '>';

diff  --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index f8aebd7c6f25c..e0a5ab9449cfb 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -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 << "\">";
 }
@@ -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 << "\">";
 }

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 566b3a2c5b306..b69988c8fea7f 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -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);
   });
@@ -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
@@ -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) {

diff  --git a/mlir/lib/Dialect/PDL/IR/PDLTypes.cpp b/mlir/lib/Dialect/PDL/IR/PDLTypes.cpp
index 956b075ab1269..0e16d3e929e2a 100644
--- a/mlir/lib/Dialect/PDL/IR/PDLTypes.cpp
+++ b/mlir/lib/Dialect/PDL/IR/PDLTypes.cpp
@@ -93,7 +93,7 @@ Type RangeType::parse(DialectAsmParser &parser) {
 }
 
 void RangeType::print(DialectAsmPrinter &printer) const {
-  printer << "range<";
+  printer << "<";
   (void)generatedTypePrinter(getElementType(), printer);
   printer << ">";
 }

diff  --git a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
index 2349ce516c97c..2d844bbf89bb4 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
+++ b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
@@ -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:

diff  --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
index d2e3f66c93abb..1246facc0be11 100644
--- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
@@ -38,7 +38,7 @@ Attribute AttrWithSelfTypeParamAttr::parse(DialectAsmParser &parser,
 }
 
 void AttrWithSelfTypeParamAttr::print(DialectAsmPrinter &printer) const {
-  printer << "attr_with_self_type_param " << getType();
+  printer << " " << getType();
 }
 
 //===----------------------------------------------------------------------===//
@@ -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();
 }
 
 //===----------------------------------------------------------------------===//
@@ -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 << "]>";
 }
@@ -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() << ">";
 }
@@ -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(

diff  --git a/mlir/test/lib/Dialect/Test/TestTypeDefs.td b/mlir/test/lib/Dialect/Test/TestTypeDefs.td
index 66008f12d574b..553fc54e9acae 100644
--- a/mlir/test/lib/Dialect/Test/TestTypeDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestTypeDefs.td
@@ -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 << ">";
   }];
@@ -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 << "}";

diff  --git a/mlir/test/lib/Dialect/Test/TestTypes.cpp b/mlir/test/lib/Dialect/Test/TestTypes.cpp
index 7278f52a5865e..28aafa4427b61 100644
--- a/mlir/test/lib/Dialect/Test/TestTypes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestTypes.cpp
@@ -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 << "]>";
@@ -150,7 +149,7 @@ Type TestTypeWithLayoutType::parse(DialectAsmParser &parser) {
 }
 
 void TestTypeWithLayoutType::print(DialectAsmPrinter &printer) const {
-  printer << "test_type_with_layout<" << getKey() << ">";
+  printer << "<" << getKey() << ">";
 }
 
 unsigned

diff  --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index d6ea2f2877ce3..1b6e9a4388493 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -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(


        


More information about the Mlir-commits mailing list