Skip to content

Commit d96da4e

Browse files
Andruszkiewicz, JacentyJacenty-And-Intel
authored andcommitted
Enable printing newlines and indents in attribute and type printers
This commit moves the code responsible for adding newlines and tracking indent, so that it can be used not only for operation printers, but also for attribute and type printers. It could be useful for nested attributes, where proper formatting with newlines and indents would benefit the readability of the IR. Currently, everything is printed on one line, which makes it difficult to read if the attribute is more verbose and there are multiple levels of nesting.
1 parent 3834523 commit d96da4e

File tree

9 files changed

+154
-34
lines changed

9 files changed

+154
-34
lines changed

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ class AsmPrinter {
116116
/// Return the raw output stream used by this printer.
117117
virtual raw_ostream &getStream() const;
118118

119+
/// Print a newline and indent the printer to the start of the current
120+
/// operation.
121+
virtual void printNewline();
122+
123+
/// Increase indentation.
124+
virtual void increaseIndent();
125+
126+
/// Decrease indentation.
127+
virtual void decreaseIndent();
128+
119129
/// Print the given floating point value in a stabilized form that can be
120130
/// roundtripped through the IR. This is the companion to the 'parseFloat'
121131
/// hook on the AsmParser.
@@ -417,16 +427,6 @@ class OpAsmPrinter : public AsmPrinter {
417427
/// Print a loc(...) specifier if printing debug info is enabled.
418428
virtual void printOptionalLocationSpecifier(Location loc) = 0;
419429

420-
/// Print a newline and indent the printer to the start of the current
421-
/// operation.
422-
virtual void printNewline() = 0;
423-
424-
/// Increase indentation.
425-
virtual void increaseIndent() = 0;
426-
427-
/// Decrease indentation.
428-
virtual void decreaseIndent() = 0;
429-
430430
/// Print a block argument in the usual format of:
431431
/// %ssaName : type {attr1=42} loc("here")
432432
/// where location printing is controlled by the standard internal option.

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,19 @@ class AsmPrinter::Impl {
393393
/// Returns the output stream of the printer.
394394
raw_ostream &getStream() { return os; }
395395

396+
/// Print a newline and indent the printer to the start of the current
397+
/// operation.
398+
void printNewline() {
399+
os << newLine;
400+
os.indent(currentIndent);
401+
}
402+
403+
/// Increase indentation.
404+
void increaseIndent() { currentIndent += indentWidth; }
405+
406+
/// Decrease indentation.
407+
void decreaseIndent() { currentIndent -= indentWidth; }
408+
396409
template <typename Container, typename UnaryFunctor>
397410
inline void interleaveComma(const Container &c, UnaryFunctor eachFn) const {
398411
llvm::interleaveComma(c, os, eachFn);
@@ -507,6 +520,12 @@ class AsmPrinter::Impl {
507520

508521
/// A tracker for the number of new lines emitted during printing.
509522
NewLineCounter newLine;
523+
524+
/// The number of spaces used for indenting nested operations.
525+
const static unsigned indentWidth = 2;
526+
527+
/// This is the current indentation level for nested structures.
528+
unsigned currentIndent = 0;
510529
};
511530
} // namespace mlir
512531

@@ -959,6 +978,9 @@ class DummyAliasDialectAsmPrinter : public DialectAsmPrinter {
959978

960979
/// The following are hooks of `DialectAsmPrinter` that are not necessary for
961980
/// determining potential aliases.
981+
void printNewline() override {}
982+
void increaseIndent() override {}
983+
void decreaseIndent() override {}
962984
void printFloat(const APFloat &) override {}
963985
void printKeywordOrString(StringRef) override {}
964986
void printString(StringRef) override {}
@@ -2739,6 +2761,13 @@ void AsmPrinter::Impl::printDialectAttribute(Attribute attr) {
27392761
{
27402762
llvm::raw_string_ostream attrNameStr(attrName);
27412763
Impl subPrinter(attrNameStr, state);
2764+
2765+
// The values of currentIndent and newLine are assigned to the created
2766+
// subprinter, so that the indent level and number of printed lines can be
2767+
// tracked.
2768+
subPrinter.currentIndent = currentIndent;
2769+
subPrinter.newLine = newLine;
2770+
27422771
DialectAsmPrinter printer(subPrinter);
27432772
dialect.printAttribute(attr, printer);
27442773
}
@@ -2753,6 +2782,13 @@ void AsmPrinter::Impl::printDialectType(Type type) {
27532782
{
27542783
llvm::raw_string_ostream typeNameStr(typeName);
27552784
Impl subPrinter(typeNameStr, state);
2785+
2786+
// The values of currentIndent and newLine are assigned to the created
2787+
// subprinter, so that the indent level and number of printed lines can be
2788+
// tracked.
2789+
subPrinter.currentIndent = currentIndent;
2790+
subPrinter.newLine = newLine;
2791+
27562792
DialectAsmPrinter printer(subPrinter);
27572793
dialect.printType(type, printer);
27582794
}
@@ -2793,6 +2829,21 @@ raw_ostream &AsmPrinter::getStream() const {
27932829
return impl->getStream();
27942830
}
27952831

2832+
void AsmPrinter::printNewline() {
2833+
assert(impl && "expected AsmPrinter::printNewLine to be overriden");
2834+
impl->printNewline();
2835+
}
2836+
2837+
void AsmPrinter::increaseIndent() {
2838+
assert(impl && "expected AsmPrinter::increaseIndent to be overriden");
2839+
impl->increaseIndent();
2840+
}
2841+
2842+
void AsmPrinter::decreaseIndent() {
2843+
assert(impl && "expected AsmPrinter::decreaseIndent to be overriden");
2844+
impl->decreaseIndent();
2845+
}
2846+
27962847
/// Print the given floating point value in a stablized form.
27972848
void AsmPrinter::printFloat(const APFloat &value) {
27982849
assert(impl && "expected AsmPrinter::printFloat to be overriden");
@@ -3118,19 +3169,6 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
31183169
printTrailingLocation(loc);
31193170
}
31203171

3121-
/// Print a newline and indent the printer to the start of the current
3122-
/// operation.
3123-
void printNewline() override {
3124-
os << newLine;
3125-
os.indent(currentIndent);
3126-
}
3127-
3128-
/// Increase indentation.
3129-
void increaseIndent() override { currentIndent += indentWidth; }
3130-
3131-
/// Decrease indentation.
3132-
void decreaseIndent() override { currentIndent -= indentWidth; }
3133-
31343172
/// Print a block argument in the usual format of:
31353173
/// %ssaName : type {attr1=42} loc("here")
31363174
/// where location printing is controlled by the standard internal option.
@@ -3256,12 +3294,6 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
32563294
// top-level we start with "builtin" as the default, so that the top-level
32573295
// `module` operation prints as-is.
32583296
SmallVector<StringRef> defaultDialectStack{"builtin"};
3259-
3260-
/// The number of spaces used for indenting nested operations.
3261-
const static unsigned indentWidth = 2;
3262-
3263-
// This is the current indentation level for nested structures.
3264-
unsigned currentIndent = 0;
32653297
};
32663298
} // namespace
32673299

mlir/test/lib/Dialect/Test/TestAttrDefs.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,5 +377,10 @@ def NestedPolynomialAttr2 : Test_Attr<"NestedPolynomialAttr2"> {
377377
}];
378378
}
379379

380+
def TestAttrNewlineAndIndent : Test_Attr<"TestAttrNewlineAndIndent"> {
381+
let mnemonic = "newline_and_indent";
382+
let parameters = (ins "::mlir::Type":$indentType);
383+
let hasCustomAssemblyFormat = 1;
384+
}
380385

381386
#endif // TEST_ATTRDEFS

mlir/test/lib/Dialect/Test/TestAttributes.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,35 @@ static ParseResult parseCustomFloatAttr(AsmParser &p, StringAttr &typeStrAttr,
316316
return success();
317317
}
318318

319+
//===----------------------------------------------------------------------===//
320+
// TestAttrNewlineAndIndent
321+
//===----------------------------------------------------------------------===//
322+
323+
Attribute TestAttrNewlineAndIndentAttr::parse(::mlir::AsmParser &parser,
324+
::mlir::Type type) {
325+
Type indentType;
326+
if (parser.parseLess()) {
327+
return {};
328+
}
329+
if (parser.parseType(indentType)) {
330+
return {};
331+
}
332+
if (parser.parseGreater()) {
333+
return {};
334+
}
335+
return get(parser.getContext(), indentType);
336+
}
337+
338+
void TestAttrNewlineAndIndentAttr::print(::mlir::AsmPrinter &printer) const {
339+
printer << "<";
340+
printer.increaseIndent();
341+
printer.printNewline();
342+
printer << getIndentType();
343+
printer.decreaseIndent();
344+
printer.printNewline();
345+
printer << ">";
346+
}
347+
319348
//===----------------------------------------------------------------------===//
320349
// Tablegen Generated Definitions
321350
//===----------------------------------------------------------------------===//

mlir/test/lib/Dialect/Test/TestTypeDefs.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,9 @@ def TestRecursiveAlias
392392
}];
393393
}
394394

395+
def TestTypeNewlineAndIndent : Test_Type<"TestTypeNewlineAndIndent"> {
396+
let mnemonic = "newline_and_indent";
397+
let hasCustomAssemblyFormat = 1;
398+
}
399+
395400
#endif // TEST_TYPEDEFS

mlir/test/lib/Dialect/Test/TestTypes.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,30 @@ void TestRecursiveAliasType::print(AsmPrinter &printer) const {
531531
}
532532
printer << ">";
533533
}
534+
535+
//===----------------------------------------------------------------------===//
536+
// TestTypeNewlineAndIndent
537+
//===----------------------------------------------------------------------===//
538+
539+
Type TestTypeNewlineAndIndentType::parse(::mlir::AsmParser &parser) {
540+
if (parser.parseLess()) {
541+
return {};
542+
}
543+
if (parser.parseKeyword("indented_content")) {
544+
return {};
545+
}
546+
if (parser.parseGreater()) {
547+
return {};
548+
}
549+
return get(parser.getContext());
550+
}
551+
552+
void TestTypeNewlineAndIndentType::print(::mlir::AsmPrinter &printer) const {
553+
printer << "<";
554+
printer.increaseIndent();
555+
printer.printNewline();
556+
printer << "indented_content";
557+
printer.decreaseIndent();
558+
printer.printNewline();
559+
printer << ">";
560+
}

mlir/test/mlir-tblgen/testdialect-attrdefs.mlir

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt %s -split-input-file -verify-diagnostics | FileCheck %s
1+
// RUN: mlir-opt %s -split-input-file -verify-diagnostics | FileCheck %s --strict-whitespace
22

33
// CHECK-LABEL: func private @compoundA()
44
// CHECK-SAME: #test.cmpnd_a<1, !test.smpla, [5, 6]>
@@ -44,3 +44,17 @@ func.func private @hexdecimalInteger() attributes {
4444
// expected-error @below {{expected an integer}}
4545
sdg = #test.decimal_shape<1x0xb>
4646
}
47+
48+
// CHECK-LABEL: @newlineAndIndent
49+
// CHECK-SAME: indent = #test.newline_and_indent<
50+
// CHECK-NEXT: {{^ }}!test.newline_and_indent<
51+
// CHECK-NEXT: {{^ }}indented_content
52+
// CHECK-NEXT: {{^ }}>
53+
// CHECK-NEXT: {{^ }}>
54+
func.func private @newlineAndIndent() attributes {
55+
indent = #test.newline_and_indent<
56+
!test.newline_and_indent<
57+
indented_content
58+
>
59+
>
60+
}

mlir/test/mlir-tblgen/testdialect-typedefs.mlir

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt %s | mlir-opt -verify-diagnostics | FileCheck %s
1+
// RUN: mlir-opt %s | mlir-opt -verify-diagnostics | FileCheck %s --strict-whitespace
22

33
//////////////
44
// Tests the types in the 'Test' dialect, not the ones in 'typedefs.mlir'
@@ -42,3 +42,13 @@ func.func @testInt(%A : !test.int<s, 8>, %B : !test.int<unsigned, 2>, %C : !test
4242
func.func @structTest (%A : !test.struct< {field1, !test.smpla}, {field2, !test.int<none, 3>} > ) {
4343
return
4444
}
45+
46+
// CHECK-LABEL: @newlineAndIndent
47+
// CHECK-SAME: !test.newline_and_indent<
48+
// CHECK-NEXT: {{^ }}indented_content
49+
// CHECK-NEXT: {{^ }}>
50+
func.func @newlineAndIndent(%A : !test.newline_and_indent<
51+
indented_content
52+
>) {
53+
return
54+
}

mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,9 +924,7 @@ void DefFormat::genOptionalGroupPrinter(OptionalElement *el, FmtContext &ctx,
924924
void DefFormat::genWhitespacePrinter(WhitespaceElement *el, FmtContext &ctx,
925925
MethodBody &os) {
926926
if (el->getValue() == "\\n") {
927-
// FIXME: The newline should be `printer.printNewLine()`, i.e., handled by
928-
// the printer.
929-
os << tgfmt("$_printer << '\\n';\n", &ctx);
927+
os << tgfmt("$_printer.printNewline();\n", &ctx);
930928
} else if (!el->getValue().empty()) {
931929
os << tgfmt("$_printer << \"$0\";\n", &ctx, el->getValue());
932930
} else {

0 commit comments

Comments
 (0)