@@ -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.
27662817void 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
0 commit comments