[Mlir-commits] [mlir] 279d294 - Use consistent spacing before custom directives for op and attr/type assemblyFormat.

Jeff Niu llvmlistbot at llvm.org
Tue Dec 6 12:13:09 PST 2022


Author: Kevin Gleason
Date: 2022-12-06T12:13:02-08:00
New Revision: 279d294d26c39e86dd7baabf5cd3385676d9a7a4

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

LOG: Use consistent spacing before custom directives for op and attr/type assemblyFormat.

Currently, assemblyFormat `custom<A>($a) custom<B>($b)` has different spacing
if used for Ops vs Attrs/Types. Ops insert a space if needed before the custom directive,
while attributes and types do not.

This leads to the following two patterns in attributes / types:

```
# 1. Whitespace literal
let assemblyFormat = "... ` ` custom<A>($a)"

# 2. Custom printer code includes spacing
void printB(...) {
  printer << ' ' << b;
}
```

Moving this spacing into the generated code allows for some cleanup in mlir and
improves the consistency of custom directives.

Reviewed By: mehdi_amini

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.td
    mlir/test/lib/Dialect/Test/TestAttrDefs.td
    mlir/test/lib/Dialect/Test/TestDialect.cpp
    mlir/test/lib/Dialect/Test/TestOps.td
    mlir/test/lib/Dialect/Test/TestTypeDefs.td
    mlir/test/lib/Dialect/Test/TestTypes.cpp
    mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
    mlir/test/mlir-tblgen/op-format.mlir
    mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.td
index 9d0203d90ed4e..460e7ded84c3f 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.td
@@ -42,7 +42,7 @@ def LLVMArrayType : LLVMType<"LLVMArray", "array", [
 
   let parameters = (ins "Type":$elementType, "unsigned":$numElements);
   let assemblyFormat = [{
-    `<` $numElements `x` ` ` custom<PrettyLLVMType>($elementType) `>`
+    `<` $numElements `x` custom<PrettyLLVMType>($elementType) `>`
   }];
 
   let genVerifyDecl = 1;
@@ -182,7 +182,7 @@ def LLVMFixedVectorType : LLVMType<"LLVMFixedVector", "vec", [
 
   let parameters = (ins "Type":$elementType, "unsigned":$numElements);
   let assemblyFormat = [{
-    `<` $numElements `x` ` ` custom<PrettyLLVMType>($elementType) `>`
+    `<` $numElements `x` custom<PrettyLLVMType>($elementType) `>`
   }];
 
   let genVerifyDecl = 1;

diff  --git a/mlir/test/lib/Dialect/Test/TestAttrDefs.td b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
index a573c7da4f917..9c134e4f28415 100644
--- a/mlir/test/lib/Dialect/Test/TestAttrDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
@@ -312,7 +312,7 @@ def TestArrayOfEnums : ArrayOfAttr<Test_Dialect, "ArrayOfEnums",
 def TestCustomAnchor : Test_Attr<"TestCustomAnchor"> {
   let parameters = (ins "int":$a, OptionalParameter<"mlir::Optional<int>">:$b);
   let mnemonic = "custom_anchor";
-  let assemblyFormat = "`<` $a (`>`) : (`,` ` ` custom<TrueFalse>($b)^ `>`)?";
+  let assemblyFormat = "`<` $a (`>`) : (`,` custom<TrueFalse>($b)^ `>`)?";
 }
 
 def Test_IteratorTypeEnum

diff  --git a/mlir/test/lib/Dialect/Test/TestDialect.cpp b/mlir/test/lib/Dialect/Test/TestDialect.cpp
index d20d4fc26bad0..685be113aec55 100644
--- a/mlir/test/lib/Dialect/Test/TestDialect.cpp
+++ b/mlir/test/lib/Dialect/Test/TestDialect.cpp
@@ -680,7 +680,10 @@ static ParseResult parseCustomDirectiveAttributes(OpAsmParser &parser,
   }
   return success();
 }
-
+static ParseResult parseCustomDirectiveSpacing(OpAsmParser &parser,
+                                               mlir::StringAttr &attr) {
+  return parser.parseAttribute(attr);
+}
 static ParseResult parseCustomDirectiveAttrDict(OpAsmParser &parser,
                                                 NamedAttrList &attrs) {
   return parser.parseOptionalAttrDict(attrs);
@@ -759,7 +762,10 @@ static void printCustomDirectiveAttributes(OpAsmPrinter &printer, Operation *,
   if (optAttribute)
     printer << ", " << optAttribute;
 }
-
+static void printCustomDirectiveSpacing(OpAsmPrinter &printer, Operation *op,
+                                        Attribute attribute) {
+  printer << attribute;
+}
 static void printCustomDirectiveAttrDict(OpAsmPrinter &printer, Operation *op,
                                          DictionaryAttr attrs) {
   printer.printOptionalAttrDict(attrs.getValue());

diff  --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index b52eb327a1e14..ae8a4a95d4025 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -2315,6 +2315,16 @@ def FormatCustomDirectiveAttributes
   }];
 }
 
+def FormatCustomDirectiveSpacing
+    : TEST_Op<"format_custom_directive_spacing"> {
+  let arguments = (ins StrAttr:$attr1, StrAttr:$attr2);
+  let assemblyFormat = [{
+    custom<CustomDirectiveSpacing>($attr1)
+    custom<CustomDirectiveSpacing>($attr2)
+    attr-dict
+  }];
+}
+
 def FormatCustomDirectiveAttrDict
     : TEST_Op<"format_custom_directive_attrdict"> {
   let arguments = (ins I64Attr:$attr, OptionalAttr<I64Attr>:$optAttr);

diff  --git a/mlir/test/lib/Dialect/Test/TestTypeDefs.td b/mlir/test/lib/Dialect/Test/TestTypeDefs.td
index 81fd154d341e6..f061bd3368566 100644
--- a/mlir/test/lib/Dialect/Test/TestTypeDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestTypeDefs.td
@@ -319,10 +319,17 @@ def TestTypeDefaultValuedType : Test_Type<"TestTypeDefaultValuedType"> {
 def TestTypeCustom : Test_Type<"TestTypeCustom"> {
   let parameters = (ins "int":$a, OptionalParameter<"mlir::Optional<int>">:$b);
   let mnemonic = "custom_type";
-  let assemblyFormat = [{ `<` custom<CustomTypeA>($a)
+  let assemblyFormat = [{ `<` custom<CustomTypeA>($a) ``
                               custom<CustomTypeB>(ref($a), $b) `>` }];
 }
 
+def TestTypeCustomSpacing : Test_Type<"TestTypeCustomSpacing"> {
+  let parameters = (ins "int":$a, "int":$b);
+  let mnemonic = "custom_type_spacing";
+  let assemblyFormat = [{ `<` custom<CustomTypeA>($a)
+                              custom<CustomTypeA>($b) `>` }];
+}
+
 def TestTypeCustomString : Test_Type<"TestTypeCustomString"> {
   let parameters = (ins StringRefParameter<>:$foo);
   let mnemonic = "custom_type_string";

diff  --git a/mlir/test/lib/Dialect/Test/TestTypes.cpp b/mlir/test/lib/Dialect/Test/TestTypes.cpp
index ffa83c749c3ed..ff37631acaab0 100644
--- a/mlir/test/lib/Dialect/Test/TestTypes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestTypes.cpp
@@ -134,7 +134,7 @@ static LogicalResult parseBarString(AsmParser &parser, StringRef foo) {
 }
 
 static void printBarString(AsmPrinter &printer, StringRef foo) {
-  printer << ' ' << foo;
+  printer << foo;
 }
 //===----------------------------------------------------------------------===//
 // Tablegen Generated Definitions

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 3fec4ea3e9fc4..80627e754ddbc 100644
--- a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
+++ b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir
@@ -62,6 +62,7 @@ attributes {
 // CHECK: !test.default_valued_type<>
 // CHECK: !test.custom_type<-5>
 // CHECK: !test.custom_type<2 0 1 5>
+// CHECK: !test.custom_type_spacing<1 2>
 // CHECK: !test.custom_type_string<"foo" foo>
 // CHECK: !test.custom_type_string<"bar" bar>
 
@@ -98,6 +99,7 @@ func.func private @test_roundtrip_default_parsers_struct(
   !test.default_valued_type<>,
   !test.custom_type<-5>,
   !test.custom_type<2 9 9 5>,
+  !test.custom_type_spacing<1 2>,
   !test.custom_type_string<"foo" foo>,
   !test.custom_type_string<"bar" bar>
 )

diff  --git a/mlir/test/mlir-tblgen/op-format.mlir b/mlir/test/mlir-tblgen/op-format.mlir
index cddae76b38637..01a63f6c34241 100644
--- a/mlir/test/mlir-tblgen/op-format.mlir
+++ b/mlir/test/mlir-tblgen/op-format.mlir
@@ -398,6 +398,9 @@ func.func @foo() {
   return
 }
 
+// CHECK: test.format_custom_directive_spacing "a" "b"
+test.format_custom_directive_spacing "a" "b"
+
 // CHECK: test.format_literal_following_optional_group(5 : i32) : i32 {a}
 test.format_literal_following_optional_group(5 : i32) : i32 {a}
 

diff  --git a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
index ed155c56f7761..0afdbfa631392 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
@@ -826,6 +826,12 @@ void DefFormat::genStructPrinter(StructDirective *el, FmtContext &ctx,
 
 void DefFormat::genCustomPrinter(CustomDirective *el, FmtContext &ctx,
                                  MethodBody &os) {
+  // Insert a space before the custom directive, if necessary.
+  if (shouldEmitSpace || !lastWasPunctuation)
+    os << tgfmt("$_printer << ' ';\n", &ctx);
+  shouldEmitSpace = true;
+  lastWasPunctuation = false;
+
   os << tgfmt("print$0($_printer", &ctx, el->getName());
   os.indent();
   for (FormatElement *arg : el->getArguments()) {


        


More information about the Mlir-commits mailing list