Skip to content

Commit d4e2c63

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 de3e9d4 commit d4e2c63

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 {}
@@ -2738,6 +2760,13 @@ void AsmPrinter::Impl::printDialectAttribute(Attribute attr) {
27382760
{
27392761
llvm::raw_string_ostream attrNameStr(attrName);
27402762
Impl subPrinter(attrNameStr, state);
2763+
2764+
// The values of currentIndent and newLine are assigned to the created
2765+
// subprinter, so that the indent level and number of printed lines can be
2766+
// tracked.
2767+
subPrinter.currentIndent = currentIndent;
2768+
subPrinter.newLine = newLine;
2769+
27412770
DialectAsmPrinter printer(subPrinter);
27422771
dialect.printAttribute(attr, printer);
27432772
}
@@ -2752,6 +2781,13 @@ void AsmPrinter::Impl::printDialectType(Type type) {
27522781
{
27532782
llvm::raw_string_ostream typeNameStr(typeName);
27542783
Impl subPrinter(typeNameStr, state);
2784+
2785+
// The values of currentIndent and newLine are assigned to the created
2786+
// subprinter, so that the indent level and number of printed lines can be
2787+
// tracked.
2788+
subPrinter.currentIndent = currentIndent;
2789+
subPrinter.newLine = newLine;
2790+
27552791
DialectAsmPrinter printer(subPrinter);
27562792
dialect.printType(type, printer);
27572793
}
@@ -2792,6 +2828,21 @@ raw_ostream &AsmPrinter::getStream() const {
27922828
return impl->getStream();
27932829
}
27942830

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

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,5 +368,10 @@ def NestedPolynomialAttr2 : Test_Attr<"NestedPolynomialAttr2"> {
368368
}];
369369
}
370370

371+
def TestAttrNewlineAndIndent : Test_Attr<"TestAttrNewlineAndIndent"> {
372+
let mnemonic = "newline_and_indent";
373+
let parameters = (ins "::mlir::Type":$indentType);
374+
let hasCustomAssemblyFormat = 1;
375+
}
371376

372377
#endif // TEST_ATTRDEFS

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

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

283+
//===----------------------------------------------------------------------===//
284+
// TestAttrNewlineAndIndent
285+
//===----------------------------------------------------------------------===//
286+
287+
Attribute TestAttrNewlineAndIndentAttr::parse(::mlir::AsmParser &parser,
288+
::mlir::Type type) {
289+
Type indentType;
290+
if (parser.parseLess()) {
291+
return {};
292+
}
293+
if (parser.parseType(indentType)) {
294+
return {};
295+
}
296+
if (parser.parseGreater()) {
297+
return {};
298+
}
299+
return get(parser.getContext(), indentType);
300+
}
301+
302+
void TestAttrNewlineAndIndentAttr::print(::mlir::AsmPrinter &printer) const {
303+
printer << "<";
304+
printer.increaseIndent();
305+
printer.printNewline();
306+
printer << getIndentType();
307+
printer.decreaseIndent();
308+
printer.printNewline();
309+
printer << ">";
310+
}
311+
283312
//===----------------------------------------------------------------------===//
284313
// Tablegen Generated Definitions
285314
//===----------------------------------------------------------------------===//

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 | mlir-opt -verify-diagnostics | FileCheck %s
1+
// RUN: mlir-opt %s | mlir-opt -verify-diagnostics | FileCheck %s --strict-whitespace
22

33
// CHECK-LABEL: func private @compoundA()
44
// CHECK-SAME: #test.cmpnd_a<1, !test.smpla, [5, 6]>
@@ -19,3 +19,17 @@ func.func private @qualifiedAttr() attributes {foo = #test.cmpnd_nested_outer_qu
1919
func.func private @overriddenAttr() attributes {
2020
foo = #test.override_builder<5>
2121
}
22+
23+
// CHECK-LABEL: @newlineAndIndent
24+
// CHECK-SAME: indent = #test.newline_and_indent<
25+
// CHECK-NEXT: {{^ }}!test.newline_and_indent<
26+
// CHECK-NEXT: {{^ }}indented_content
27+
// CHECK-NEXT: {{^ }}>
28+
// CHECK-NEXT: {{^ }}>
29+
func.func private @newlineAndIndent() attributes {
30+
indent = #test.newline_and_indent<
31+
!test.newline_and_indent<
32+
indented_content
33+
>
34+
>
35+
}

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)