Skip to content

Commit fafd534

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 ef50227 commit fafd534

File tree

9 files changed

+157
-34
lines changed

9 files changed

+157
-34
lines changed

mlir/include/mlir/IR/OpImplementation.h

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

133+
/// Print a newline and indent the printer to the start of the current
134+
/// operation.
135+
virtual void printNewline();
136+
137+
/// Increase indentation.
138+
virtual void increaseIndent();
139+
140+
/// Decrease indentation.
141+
virtual void decreaseIndent();
142+
133143
/// Print the given floating point value in a stabilized form that can be
134144
/// roundtripped through the IR. This is the companion to the 'parseFloat'
135145
/// hook on the AsmParser.
@@ -448,16 +458,6 @@ class OpAsmPrinter : public AsmPrinter {
448458
/// Print a loc(...) specifier if printing debug info is enabled.
449459
virtual void printOptionalLocationSpecifier(Location loc) = 0;
450460

451-
/// Print a newline and indent the printer to the start of the current
452-
/// operation.
453-
virtual void printNewline() = 0;
454-
455-
/// Increase indentation.
456-
virtual void increaseIndent() = 0;
457-
458-
/// Decrease indentation.
459-
virtual void decreaseIndent() = 0;
460-
461461
/// Print a block argument in the usual format of:
462462
/// %ssaName : type {attr1=42} loc("here")
463463
/// 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
@@ -418,6 +418,19 @@ class AsmPrinter::Impl {
418418
/// Returns the output stream of the printer.
419419
raw_ostream &getStream() { return os; }
420420

421+
/// Print a newline and indent the printer to the start of the current
422+
/// operation.
423+
void printNewline() {
424+
os << newLine;
425+
os.indent(currentIndent);
426+
}
427+
428+
/// Increase indentation.
429+
void increaseIndent() { currentIndent += indentWidth; }
430+
431+
/// Decrease indentation.
432+
void decreaseIndent() { currentIndent -= indentWidth; }
433+
421434
template <typename Container, typename UnaryFunctor>
422435
inline void interleaveComma(const Container &c, UnaryFunctor eachFn) const {
423436
llvm::interleaveComma(c, os, eachFn);
@@ -532,6 +545,12 @@ class AsmPrinter::Impl {
532545

533546
/// A tracker for the number of new lines emitted during printing.
534547
NewLineCounter newLine;
548+
549+
/// The number of spaces used for indenting nested operations.
550+
const static unsigned indentWidth = 2;
551+
552+
/// This is the current indentation level for nested structures.
553+
unsigned currentIndent = 0;
535554
};
536555
} // namespace mlir
537556

@@ -1004,6 +1023,9 @@ class DummyAliasDialectAsmPrinter : public DialectAsmPrinter {
10041023

10051024
/// The following are hooks of `DialectAsmPrinter` that are not necessary for
10061025
/// determining potential aliases.
1026+
void printNewline() override {}
1027+
void increaseIndent() override {}
1028+
void decreaseIndent() override {}
10071029
void printFloat(const APFloat &) override {}
10081030
void printKeywordOrString(StringRef) override {}
10091031
void printString(StringRef) override {}
@@ -2892,6 +2914,13 @@ void AsmPrinter::Impl::printDialectAttribute(Attribute attr) {
28922914
{
28932915
llvm::raw_string_ostream attrNameStr(attrName);
28942916
Impl subPrinter(attrNameStr, state);
2917+
2918+
// The values of currentIndent and newLine are assigned to the created
2919+
// subprinter, so that the indent level and number of printed lines can be
2920+
// tracked.
2921+
subPrinter.currentIndent = currentIndent;
2922+
subPrinter.newLine = newLine;
2923+
28952924
DialectAsmPrinter printer(subPrinter);
28962925
dialect.printAttribute(attr, printer);
28972926
}
@@ -2906,6 +2935,13 @@ void AsmPrinter::Impl::printDialectType(Type type) {
29062935
{
29072936
llvm::raw_string_ostream typeNameStr(typeName);
29082937
Impl subPrinter(typeNameStr, state);
2938+
2939+
// The values of currentIndent and newLine are assigned to the created
2940+
// subprinter, so that the indent level and number of printed lines can be
2941+
// tracked.
2942+
subPrinter.currentIndent = currentIndent;
2943+
subPrinter.newLine = newLine;
2944+
29092945
DialectAsmPrinter printer(subPrinter);
29102946
dialect.printType(type, printer);
29112947
}
@@ -2946,6 +2982,21 @@ raw_ostream &AsmPrinter::getStream() const {
29462982
return impl->getStream();
29472983
}
29482984

2985+
void AsmPrinter::printNewline() {
2986+
assert(impl && "expected AsmPrinter::printNewLine to be overriden");
2987+
impl->printNewline();
2988+
}
2989+
2990+
void AsmPrinter::increaseIndent() {
2991+
assert(impl && "expected AsmPrinter::increaseIndent to be overriden");
2992+
impl->increaseIndent();
2993+
}
2994+
2995+
void AsmPrinter::decreaseIndent() {
2996+
assert(impl && "expected AsmPrinter::decreaseIndent to be overriden");
2997+
impl->decreaseIndent();
2998+
}
2999+
29493000
/// Print the given floating point value in a stablized form.
29503001
void AsmPrinter::printFloat(const APFloat &value) {
29513002
assert(impl && "expected AsmPrinter::printFloat to be overriden");
@@ -3276,19 +3327,6 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
32763327
printTrailingLocation(loc);
32773328
}
32783329

3279-
/// Print a newline and indent the printer to the start of the current
3280-
/// operation.
3281-
void printNewline() override {
3282-
os << newLine;
3283-
os.indent(currentIndent);
3284-
}
3285-
3286-
/// Increase indentation.
3287-
void increaseIndent() override { currentIndent += indentWidth; }
3288-
3289-
/// Decrease indentation.
3290-
void decreaseIndent() override { currentIndent -= indentWidth; }
3291-
32923330
/// Print a block argument in the usual format of:
32933331
/// %ssaName : type {attr1=42} loc("here")
32943332
/// where location printing is controlled by the standard internal option.
@@ -3414,12 +3452,6 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
34143452
// top-level we start with "builtin" as the default, so that the top-level
34153453
// `module` operation prints as-is.
34163454
SmallVector<StringRef> defaultDialectStack{"builtin"};
3417-
3418-
/// The number of spaces used for indenting nested operations.
3419-
const static unsigned indentWidth = 2;
3420-
3421-
// This is the current indentation level for nested structures.
3422-
unsigned currentIndent = 0;
34233455
};
34243456
} // namespace
34253457

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,10 @@ def TestCustomStorageCtorAttr : Test_Attr<"TestCustomStorageCtorAttr"> {
439439
let hasStorageCustomConstructor = 1;
440440
}
441441

442+
def TestAttrNewlineAndIndent : Test_Attr<"TestAttrNewlineAndIndent"> {
443+
let mnemonic = "newline_and_indent";
444+
let parameters = (ins "::mlir::Type":$indentType);
445+
let hasCustomAssemblyFormat = 1;
446+
}
447+
442448
#endif // TEST_ATTRDEFS

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,35 @@ bool TestConstMemorySpaceAttr::isValidPtrIntCast(
430430
return false;
431431
}
432432

433+
//===----------------------------------------------------------------------===//
434+
// TestAttrNewlineAndIndent
435+
//===----------------------------------------------------------------------===//
436+
437+
Attribute TestAttrNewlineAndIndentAttr::parse(::mlir::AsmParser &parser,
438+
::mlir::Type type) {
439+
Type indentType;
440+
if (parser.parseLess()) {
441+
return {};
442+
}
443+
if (parser.parseType(indentType)) {
444+
return {};
445+
}
446+
if (parser.parseGreater()) {
447+
return {};
448+
}
449+
return get(parser.getContext(), indentType);
450+
}
451+
452+
void TestAttrNewlineAndIndentAttr::print(::mlir::AsmPrinter &printer) const {
453+
printer << "<";
454+
printer.increaseIndent();
455+
printer.printNewline();
456+
printer << getIndentType();
457+
printer.decreaseIndent();
458+
printer.printNewline();
459+
printer << ">";
460+
}
461+
433462
//===----------------------------------------------------------------------===//
434463
// Tablegen Generated Definitions
435464
//===----------------------------------------------------------------------===//

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,4 +470,9 @@ def TestMemrefType : Test_Type<"TestMemref",
470470
}];
471471
}
472472

473+
def TestTypeNewlineAndIndent : Test_Type<"TestTypeNewlineAndIndent"> {
474+
let mnemonic = "newline_and_indent";
475+
let hasCustomAssemblyFormat = 1;
476+
}
477+
473478
#endif // TEST_TYPEDEFS

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,30 @@ ::mlir::LogicalResult TestTensorType::verifyCompatibleBufferType(
573573
getElementType() == testMemref.getElementType();
574574
return mlir::success(valid);
575575
}
576+
577+
//===----------------------------------------------------------------------===//
578+
// TestTypeNewlineAndIndent
579+
//===----------------------------------------------------------------------===//
580+
581+
Type TestTypeNewlineAndIndentType::parse(::mlir::AsmParser &parser) {
582+
if (parser.parseLess()) {
583+
return {};
584+
}
585+
if (parser.parseKeyword("indented_content")) {
586+
return {};
587+
}
588+
if (parser.parseGreater()) {
589+
return {};
590+
}
591+
return get(parser.getContext());
592+
}
593+
594+
void TestTypeNewlineAndIndentType::print(::mlir::AsmPrinter &printer) const {
595+
printer << "<";
596+
printer.increaseIndent();
597+
printer.printNewline();
598+
printer << "indented_content";
599+
printer.decreaseIndent();
600+
printer.printNewline();
601+
printer << ">";
602+
}

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

Lines changed: 17 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,19 @@ func.func private @hexdecimalInteger() attributes {
4444
// expected-error @below {{expected an integer}}
4545
sdg = #test.decimal_shape<1x0xb>
4646
}
47+
48+
// -----
49+
50+
// CHECK-LABEL: @newlineAndIndent
51+
// CHECK-SAME: indent = #test.newline_and_indent<
52+
// CHECK-NEXT: {{^ }}!test.newline_and_indent<
53+
// CHECK-NEXT: {{^ }}indented_content
54+
// CHECK-NEXT: {{^ }}>
55+
// CHECK-NEXT: {{^ }}>
56+
func.func private @newlineAndIndent() attributes {
57+
indent = #test.newline_and_indent<
58+
!test.newline_and_indent<
59+
indented_content
60+
>
61+
>
62+
}

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
@@ -958,9 +958,7 @@ void DefFormat::genOptionalGroupPrinter(OptionalElement *el, FmtContext &ctx,
958958
void DefFormat::genWhitespacePrinter(WhitespaceElement *el, FmtContext &ctx,
959959
MethodBody &os) {
960960
if (el->getValue() == "\\n") {
961-
// FIXME: The newline should be `printer.printNewLine()`, i.e., handled by
962-
// the printer.
963-
os << tgfmt("$_printer << '\\n';\n", &ctx);
961+
os << tgfmt("$_printer.printNewline();\n", &ctx);
964962
} else if (!el->getValue().empty()) {
965963
os << tgfmt("$_printer << \"$0\";\n", &ctx, el->getValue());
966964
} else {

0 commit comments

Comments
 (0)