[Mlir-commits] [mlir] 740e832 - [mlir][ods] Attribute and type formats: support whitespaces

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Feb 8 15:27:40 PST 2022


Author: Mogball
Date: 2022-02-08T23:27:36Z
New Revision: 740e8326444d1f2be54e02f73dcbf9ecb2463083

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

LOG: [mlir][ods] Attribute and type formats: support whitespaces

Supports whitespace elements: ` ` and `\\n` as well as the "empty" whitespace `` that removes an otherwise printed space.

Depends on D118208

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D118210

Added: 
    

Modified: 
    mlir/test/lib/Dialect/Test/TestTypeDefs.td
    mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
    mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/test/lib/Dialect/Test/TestTypeDefs.td b/mlir/test/lib/Dialect/Test/TestTypeDefs.td
index 8751ee27eef42..b8dce7ec50f93 100644
--- a/mlir/test/lib/Dialect/Test/TestTypeDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestTypeDefs.td
@@ -324,4 +324,10 @@ def TestTypeOptionalGroupStruct : Test_Type<"TestTypeOptionalGroupStruct"> {
   let assemblyFormat = "`<` (`(` struct(params)^ `)`) : (`x`)? `>`";
 }
 
+def TestTypeSpaces : Test_Type<"TestTypeSpaceS"> {
+  let parameters = (ins "int":$a, "int":$b);
+  let mnemonic = "spaces";
+  let assemblyFormat = "`<` ` ` $a `\\n` `(` `)` `` `(` `)` $b `>`";
+}
+
 #endif // TEST_TYPEDEFS

diff  --git a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
index 3b05d6c7c1371..47f25cb20406b 100644
--- a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
+++ b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
@@ -42,6 +42,8 @@ attributes {
 // CHECK: !test.optional_group_struct<x>
 // CHECK: !test.optional_group_struct<(b = 5)>
 // CHECK: !test.optional_group_struct<(a = 10, b = 5)>
+// CHECK: !test.spaces< 5
+// CHECK-NEXT: ()() 6>
 func private @test_roundtrip_default_parsers_struct(
   !test.no_parser<255, [1, 2, 3, 4, 5], "foobar", 4>
 ) -> (
@@ -67,5 +69,6 @@ func private @test_roundtrip_default_parsers_struct(
   !test.optional_group_params<(5, 6)>,
   !test.optional_group_struct<x>,
   !test.optional_group_struct<(b = 5)>,
-  !test.optional_group_struct<(b = 5, a = 10)>
+  !test.optional_group_struct<(b = 5, a = 10)>,
+  !test.spaces<5 ()() 6>
 )

diff  --git a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
index 2a1fcf936237f..77087b6b4f698 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
@@ -207,6 +207,9 @@ class DefFormat {
   /// Generate the printer code for an optional group.
   void genOptionalGroupPrinter(OptionalElement *el, FmtContext &ctx,
                                MethodBody &os);
+  /// Generate a printer (or space eraser) for a whitespace element.
+  void genWhitespacePrinter(WhitespaceElement *el, FmtContext &ctx,
+                            MethodBody &os);
 
   /// The ODS definition of the attribute or type whose format is being used to
   /// generate a parser and printer.
@@ -292,6 +295,8 @@ void DefFormat::genElementParser(FormatElement *el, FmtContext &ctx,
     return genStructParser(strct, ctx, os);
   if (auto *optional = dyn_cast<OptionalElement>(el))
     return genOptionalGroupParser(optional, ctx, os);
+  if (isa<WhitespaceElement>(el))
+    return;
 
   llvm_unreachable("unknown format element");
 }
@@ -612,6 +617,8 @@ void DefFormat::genElementPrinter(FormatElement *el, FmtContext &ctx,
     return genVariablePrinter(var, ctx, os);
   if (auto *optional = dyn_cast<OptionalElement>(el))
     return genOptionalGroupPrinter(optional, ctx, os);
+  if (auto *whitespace = dyn_cast<WhitespaceElement>(el))
+    return genWhitespacePrinter(whitespace, ctx, os);
 
   llvm::PrintFatalError("unsupported format element");
 }
@@ -752,6 +759,20 @@ void DefFormat::genOptionalGroupPrinter(OptionalElement *el, FmtContext &ctx,
   os.unindent() << "}\n";
 }
 
+void DefFormat::genWhitespacePrinter(WhitespaceElement *el, FmtContext &ctx,
+                                     MethodBody &os) {
+  if (el->getValue() == "\\n") {
+    // FIXME: The newline should be `printer.printNewLine()`, i.e., handled by
+    // the printer.
+    os << tgfmt("$_printer << '\\n';\n", &ctx);
+  } else if (!el->getValue().empty()) {
+    os << tgfmt("$_printer << \"$0\";\n", &ctx, el->getValue());
+  } else {
+    lastWasPunctuation = true;
+  }
+  shouldEmitSpace = false;
+}
+
 //===----------------------------------------------------------------------===//
 // DefFormatParser
 //===----------------------------------------------------------------------===//


        


More information about the Mlir-commits mailing list