Skip to content

Commit 6804e64

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.
1 parent 450f195 commit 6804e64

File tree

9 files changed

+156
-34
lines changed

9 files changed

+156
-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
@@ -376,6 +376,19 @@ class AsmPrinter::Impl {
376376
/// Returns the output stream of the printer.
377377
raw_ostream &getStream() { return os; }
378378

379+
/// Print a newline and indent the printer to the start of the current
380+
/// operation.
381+
void printNewline() {
382+
os << newLine;
383+
os.indent(currentIndent);
384+
}
385+
386+
/// Increase indentation.
387+
void increaseIndent() { currentIndent += indentWidth; }
388+
389+
/// Decrease indentation.
390+
void decreaseIndent() { currentIndent -= indentWidth; }
391+
379392
template <typename Container, typename UnaryFunctor>
380393
inline void interleaveComma(const Container &c, UnaryFunctor eachFn) const {
381394
llvm::interleaveComma(c, os, eachFn);
@@ -490,6 +503,12 @@ class AsmPrinter::Impl {
490503

491504
/// A tracker for the number of new lines emitted during printing.
492505
NewLineCounter newLine;
506+
507+
/// The number of spaces used for indenting nested operations.
508+
const static unsigned indentWidth = 2;
509+
510+
/// This is the current indentation level for nested structures.
511+
unsigned currentIndent = 0;
493512
};
494513
} // namespace mlir
495514

@@ -942,6 +961,9 @@ class DummyAliasDialectAsmPrinter : public DialectAsmPrinter {
942961

943962
/// The following are hooks of `DialectAsmPrinter` that are not necessary for
944963
/// determining potential aliases.
964+
void printNewline() override {}
965+
void increaseIndent() override {}
966+
void decreaseIndent() override {}
945967
void printFloat(const APFloat &) override {}
946968
void printKeywordOrString(StringRef) override {}
947969
void printString(StringRef) override {}
@@ -2708,6 +2730,13 @@ void AsmPrinter::Impl::printDialectAttribute(Attribute attr) {
27082730
{
27092731
llvm::raw_string_ostream attrNameStr(attrName);
27102732
Impl subPrinter(attrNameStr, state);
2733+
2734+
// The values of currentIndent and newLine are assigned to the created
2735+
// subprinter, so that the indent level and number of printed lines can be
2736+
// tracked.
2737+
subPrinter.currentIndent = currentIndent;
2738+
subPrinter.newLine = newLine;
2739+
27112740
DialectAsmPrinter printer(subPrinter);
27122741
dialect.printAttribute(attr, printer);
27132742
}
@@ -2722,6 +2751,13 @@ void AsmPrinter::Impl::printDialectType(Type type) {
27222751
{
27232752
llvm::raw_string_ostream typeNameStr(typeName);
27242753
Impl subPrinter(typeNameStr, state);
2754+
2755+
// The values of currentIndent and newLine are assigned to the created
2756+
// subprinter, so that the indent level and number of printed lines can be
2757+
// tracked.
2758+
subPrinter.currentIndent = currentIndent;
2759+
subPrinter.newLine = newLine;
2760+
27252761
DialectAsmPrinter printer(subPrinter);
27262762
dialect.printType(type, printer);
27272763
}
@@ -2762,6 +2798,21 @@ raw_ostream &AsmPrinter::getStream() const {
27622798
return impl->getStream();
27632799
}
27642800

2801+
void AsmPrinter::printNewline() {
2802+
assert(impl && "expected AsmPrinter::printNewLine to be overriden");
2803+
impl->printNewline();
2804+
}
2805+
2806+
void AsmPrinter::increaseIndent() {
2807+
assert(impl && "expected AsmPrinter::increaseIndent to be overriden");
2808+
impl->increaseIndent();
2809+
}
2810+
2811+
void AsmPrinter::decreaseIndent() {
2812+
assert(impl && "expected AsmPrinter::decreaseIndent to be overriden");
2813+
impl->decreaseIndent();
2814+
}
2815+
27652816
/// Print the given floating point value in a stablized form.
27662817
void AsmPrinter::printFloat(const APFloat &value) {
27672818
assert(impl && "expected AsmPrinter::printFloat to be overriden");
@@ -3087,19 +3138,6 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
30873138
printTrailingLocation(loc);
30883139
}
30893140

3090-
/// Print a newline and indent the printer to the start of the current
3091-
/// operation.
3092-
void printNewline() override {
3093-
os << newLine;
3094-
os.indent(currentIndent);
3095-
}
3096-
3097-
/// Increase indentation.
3098-
void increaseIndent() override { currentIndent += indentWidth; }
3099-
3100-
/// Decrease indentation.
3101-
void decreaseIndent() override { currentIndent -= indentWidth; }
3102-
31033141
/// Print a block argument in the usual format of:
31043142
/// %ssaName : type {attr1=42} loc("here")
31053143
/// where location printing is controlled by the standard internal option.
@@ -3225,12 +3263,6 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
32253263
// top-level we start with "builtin" as the default, so that the top-level
32263264
// `module` operation prints as-is.
32273265
SmallVector<StringRef> defaultDialectStack{"builtin"};
3228-
3229-
/// The number of spaces used for indenting nested operations.
3230-
const static unsigned indentWidth = 2;
3231-
3232-
// This is the current indentation level for nested structures.
3233-
unsigned currentIndent = 0;
32343266
};
32353267
} // namespace
32363268

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,11 @@ def TestConditionalAliasAttr : Test_Attr<"TestConditionalAlias"> {
340340
}];
341341
}
342342

343+
def TestAttrNewlineAndIndent : Test_Attr<"TestAttrNewlineAndIndent"> {
344+
let mnemonic = "newline_and_indent";
345+
let parameters = (ins "::mlir::Type":$indentType);
346+
let hasCustomAssemblyFormat = 1;
347+
}
348+
349+
343350
#endif // TEST_ATTRDEFS

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,35 @@ static void printConditionalAlias(AsmPrinter &p, StringAttr value) {
239239
p.printKeywordOrString(value);
240240
}
241241

242+
//===----------------------------------------------------------------------===//
243+
// TestAttrNewlineAndIndent
244+
//===----------------------------------------------------------------------===//
245+
246+
Attribute TestAttrNewlineAndIndentAttr::parse(::mlir::AsmParser &parser,
247+
::mlir::Type type) {
248+
Type indentType;
249+
if (parser.parseLess()) {
250+
return {};
251+
}
252+
if (parser.parseType(indentType)) {
253+
return {};
254+
}
255+
if (parser.parseGreater()) {
256+
return {};
257+
}
258+
return get(parser.getContext(), indentType);
259+
}
260+
261+
void TestAttrNewlineAndIndentAttr::print(::mlir::AsmPrinter &printer) const {
262+
printer << "<";
263+
printer.increaseIndent();
264+
printer.printNewline();
265+
printer << getIndentType();
266+
printer.decreaseIndent();
267+
printer.printNewline();
268+
printer << ">";
269+
}
270+
242271
//===----------------------------------------------------------------------===//
243272
// Tablegen Generated Definitions
244273
//===----------------------------------------------------------------------===//

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
@@ -530,3 +530,30 @@ void TestRecursiveAliasType::print(AsmPrinter &printer) const {
530530
}
531531
printer << ">";
532532
}
533+
534+
//===----------------------------------------------------------------------===//
535+
// TestTypeNewlineAndIndent
536+
//===----------------------------------------------------------------------===//
537+
538+
Type TestTypeNewlineAndIndentType::parse(::mlir::AsmParser &parser) {
539+
if (parser.parseLess()) {
540+
return {};
541+
}
542+
if (parser.parseKeyword("indented_content")) {
543+
return {};
544+
}
545+
if (parser.parseGreater()) {
546+
return {};
547+
}
548+
return get(parser.getContext());
549+
}
550+
551+
void TestTypeNewlineAndIndentType::print(::mlir::AsmPrinter &printer) const {
552+
printer << "<";
553+
printer.increaseIndent();
554+
printer.printNewline();
555+
printer << "indented_content";
556+
printer.decreaseIndent();
557+
printer.printNewline();
558+
printer << ">";
559+
}

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
@@ -903,9 +903,7 @@ void DefFormat::genOptionalGroupPrinter(OptionalElement *el, FmtContext &ctx,
903903
void DefFormat::genWhitespacePrinter(WhitespaceElement *el, FmtContext &ctx,
904904
MethodBody &os) {
905905
if (el->getValue() == "\\n") {
906-
// FIXME: The newline should be `printer.printNewLine()`, i.e., handled by
907-
// the printer.
908-
os << tgfmt("$_printer << '\\n';\n", &ctx);
906+
os << tgfmt("$_printer.printNewline();\n", &ctx);
909907
} else if (!el->getValue().empty()) {
910908
os << tgfmt("$_printer << \"$0\";\n", &ctx, el->getValue());
911909
} else {

0 commit comments

Comments
 (0)