1 //===- AsmPrinter.cpp - MLIR Assembly Printer Implementation --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the MLIR AsmPrinter class, which is used to implement
10 // the various print() methods on the core IR objects.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/IR/AffineExpr.h"
15 #include "mlir/IR/AffineMap.h"
16 #include "mlir/IR/AsmState.h"
17 #include "mlir/IR/Attributes.h"
18 #include "mlir/IR/Builders.h"
19 #include "mlir/IR/BuiltinDialect.h"
20 #include "mlir/IR/BuiltinTypes.h"
21 #include "mlir/IR/Dialect.h"
22 #include "mlir/IR/DialectImplementation.h"
23 #include "mlir/IR/IntegerSet.h"
24 #include "mlir/IR/MLIRContext.h"
25 #include "mlir/IR/OpImplementation.h"
26 #include "mlir/IR/Operation.h"
27 #include "mlir/IR/SubElementInterfaces.h"
28 #include "mlir/IR/Verifier.h"
29 #include "llvm/ADT/APFloat.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/MapVector.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/ScopeExit.h"
34 #include "llvm/ADT/ScopedHashTable.h"
35 #include "llvm/ADT/SetVector.h"
36 #include "llvm/ADT/SmallString.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/ADT/StringSet.h"
39 #include "llvm/ADT/TypeSwitch.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/Endian.h"
43 #include "llvm/Support/Regex.h"
44 #include "llvm/Support/SaveAndRestore.h"
45 #include "llvm/Support/Threading.h"
46 
47 #include <tuple>
48 
49 using namespace mlir;
50 using namespace mlir::detail;
51 
52 #define DEBUG_TYPE "mlir-asm-printer"
53 
54 void OperationName::print(raw_ostream &os) const { os << getStringRef(); }
55 
56 void OperationName::dump() const { print(llvm::errs()); }
57 
58 //===--------------------------------------------------------------------===//
59 // AsmParser
60 //===--------------------------------------------------------------------===//
61 
62 AsmParser::~AsmParser() = default;
63 DialectAsmParser::~DialectAsmParser() = default;
64 OpAsmParser::~OpAsmParser() = default;
65 
66 MLIRContext *AsmParser::getContext() const { return getBuilder().getContext(); }
67 
68 //===----------------------------------------------------------------------===//
69 // DialectAsmPrinter
70 //===----------------------------------------------------------------------===//
71 
72 DialectAsmPrinter::~DialectAsmPrinter() = default;
73 
74 //===----------------------------------------------------------------------===//
75 // OpAsmPrinter
76 //===----------------------------------------------------------------------===//
77 
78 OpAsmPrinter::~OpAsmPrinter() = default;
79 
80 void OpAsmPrinter::printFunctionalType(Operation *op) {
81   auto &os = getStream();
82   os << '(';
83   llvm::interleaveComma(op->getOperands(), os, [&](Value operand) {
84     // Print the types of null values as <<NULL TYPE>>.
85     *this << (operand ? operand.getType() : Type());
86   });
87   os << ") -> ";
88 
89   // Print the result list.  We don't parenthesize single result types unless
90   // it is a function (avoiding a grammar ambiguity).
91   bool wrapped = op->getNumResults() != 1;
92   if (!wrapped && op->getResult(0).getType() &&
93       op->getResult(0).getType().isa<FunctionType>())
94     wrapped = true;
95 
96   if (wrapped)
97     os << '(';
98 
99   llvm::interleaveComma(op->getResults(), os, [&](const OpResult &result) {
100     // Print the types of null values as <<NULL TYPE>>.
101     *this << (result ? result.getType() : Type());
102   });
103 
104   if (wrapped)
105     os << ')';
106 }
107 
108 //===----------------------------------------------------------------------===//
109 // Operation OpAsm interface.
110 //===----------------------------------------------------------------------===//
111 
112 /// The OpAsmOpInterface, see OpAsmInterface.td for more details.
113 #include "mlir/IR/OpAsmInterface.cpp.inc"
114 
115 //===----------------------------------------------------------------------===//
116 // OpPrintingFlags
117 //===----------------------------------------------------------------------===//
118 
119 namespace {
120 /// This struct contains command line options that can be used to initialize
121 /// various bits of the AsmPrinter. This uses a struct wrapper to avoid the need
122 /// for global command line options.
123 struct AsmPrinterOptions {
124   llvm::cl::opt<int64_t> printElementsAttrWithHexIfLarger{
125       "mlir-print-elementsattrs-with-hex-if-larger",
126       llvm::cl::desc(
127           "Print DenseElementsAttrs with a hex string that have "
128           "more elements than the given upper limit (use -1 to disable)")};
129 
130   llvm::cl::opt<unsigned> elideElementsAttrIfLarger{
131       "mlir-elide-elementsattrs-if-larger",
132       llvm::cl::desc("Elide ElementsAttrs with \"...\" that have "
133                      "more elements than the given upper limit")};
134 
135   llvm::cl::opt<bool> printDebugInfoOpt{
136       "mlir-print-debuginfo", llvm::cl::init(false),
137       llvm::cl::desc("Print debug info in MLIR output")};
138 
139   llvm::cl::opt<bool> printPrettyDebugInfoOpt{
140       "mlir-pretty-debuginfo", llvm::cl::init(false),
141       llvm::cl::desc("Print pretty debug info in MLIR output")};
142 
143   // Use the generic op output form in the operation printer even if the custom
144   // form is defined.
145   llvm::cl::opt<bool> printGenericOpFormOpt{
146       "mlir-print-op-generic", llvm::cl::init(false),
147       llvm::cl::desc("Print the generic op form"), llvm::cl::Hidden};
148 
149   llvm::cl::opt<bool> assumeVerifiedOpt{
150       "mlir-print-assume-verified", llvm::cl::init(false),
151       llvm::cl::desc("Skip op verification when using custom printers"),
152       llvm::cl::Hidden};
153 
154   llvm::cl::opt<bool> printLocalScopeOpt{
155       "mlir-print-local-scope", llvm::cl::init(false),
156       llvm::cl::desc("Print with local scope and inline information (eliding "
157                      "aliases for attributes, types, and locations")};
158 
159   llvm::cl::opt<bool> printValueUsers{
160       "mlir-print-value-users", llvm::cl::init(false),
161       llvm::cl::desc(
162           "Print users of operation results and block arguments as a comment")};
163 };
164 } // namespace
165 
166 static llvm::ManagedStatic<AsmPrinterOptions> clOptions;
167 
168 /// Register a set of useful command-line options that can be used to configure
169 /// various flags within the AsmPrinter.
170 void mlir::registerAsmPrinterCLOptions() {
171   // Make sure that the options struct has been initialized.
172   *clOptions;
173 }
174 
175 /// Initialize the printing flags with default supplied by the cl::opts above.
176 OpPrintingFlags::OpPrintingFlags()
177     : printDebugInfoFlag(false), printDebugInfoPrettyFormFlag(false),
178       printGenericOpFormFlag(false), assumeVerifiedFlag(false),
179       printLocalScope(false), printValueUsersFlag(false) {
180   // Initialize based upon command line options, if they are available.
181   if (!clOptions.isConstructed())
182     return;
183   if (clOptions->elideElementsAttrIfLarger.getNumOccurrences())
184     elementsAttrElementLimit = clOptions->elideElementsAttrIfLarger;
185   printDebugInfoFlag = clOptions->printDebugInfoOpt;
186   printDebugInfoPrettyFormFlag = clOptions->printPrettyDebugInfoOpt;
187   printGenericOpFormFlag = clOptions->printGenericOpFormOpt;
188   assumeVerifiedFlag = clOptions->assumeVerifiedOpt;
189   printLocalScope = clOptions->printLocalScopeOpt;
190   printValueUsersFlag = clOptions->printValueUsers;
191 }
192 
193 /// Enable the elision of large elements attributes, by printing a '...'
194 /// instead of the element data, when the number of elements is greater than
195 /// `largeElementLimit`. Note: The IR generated with this option is not
196 /// parsable.
197 OpPrintingFlags &
198 OpPrintingFlags::elideLargeElementsAttrs(int64_t largeElementLimit) {
199   elementsAttrElementLimit = largeElementLimit;
200   return *this;
201 }
202 
203 /// Enable printing of debug information. If 'prettyForm' is set to true,
204 /// debug information is printed in a more readable 'pretty' form.
205 OpPrintingFlags &OpPrintingFlags::enableDebugInfo(bool prettyForm) {
206   printDebugInfoFlag = true;
207   printDebugInfoPrettyFormFlag = prettyForm;
208   return *this;
209 }
210 
211 /// Always print operations in the generic form.
212 OpPrintingFlags &OpPrintingFlags::printGenericOpForm() {
213   printGenericOpFormFlag = true;
214   return *this;
215 }
216 
217 /// Do not verify the operation when using custom operation printers.
218 OpPrintingFlags &OpPrintingFlags::assumeVerified() {
219   assumeVerifiedFlag = true;
220   return *this;
221 }
222 
223 /// Use local scope when printing the operation. This allows for using the
224 /// printer in a more localized and thread-safe setting, but may not necessarily
225 /// be identical of what the IR will look like when dumping the full module.
226 OpPrintingFlags &OpPrintingFlags::useLocalScope() {
227   printLocalScope = true;
228   return *this;
229 }
230 
231 /// Print users of values as comments.
232 OpPrintingFlags &OpPrintingFlags::printValueUsers() {
233   printValueUsersFlag = true;
234   return *this;
235 }
236 
237 /// Return if the given ElementsAttr should be elided.
238 bool OpPrintingFlags::shouldElideElementsAttr(ElementsAttr attr) const {
239   return elementsAttrElementLimit.hasValue() &&
240          *elementsAttrElementLimit < int64_t(attr.getNumElements()) &&
241          !attr.isa<SplatElementsAttr>();
242 }
243 
244 /// Return the size limit for printing large ElementsAttr.
245 Optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const {
246   return elementsAttrElementLimit;
247 }
248 
249 /// Return if debug information should be printed.
250 bool OpPrintingFlags::shouldPrintDebugInfo() const {
251   return printDebugInfoFlag;
252 }
253 
254 /// Return if debug information should be printed in the pretty form.
255 bool OpPrintingFlags::shouldPrintDebugInfoPrettyForm() const {
256   return printDebugInfoPrettyFormFlag;
257 }
258 
259 /// Return if operations should be printed in the generic form.
260 bool OpPrintingFlags::shouldPrintGenericOpForm() const {
261   return printGenericOpFormFlag;
262 }
263 
264 /// Return if operation verification should be skipped.
265 bool OpPrintingFlags::shouldAssumeVerified() const {
266   return assumeVerifiedFlag;
267 }
268 
269 /// Return if the printer should use local scope when dumping the IR.
270 bool OpPrintingFlags::shouldUseLocalScope() const { return printLocalScope; }
271 
272 /// Return if the printer should print users of values.
273 bool OpPrintingFlags::shouldPrintValueUsers() const {
274   return printValueUsersFlag;
275 }
276 
277 /// Returns true if an ElementsAttr with the given number of elements should be
278 /// printed with hex.
279 static bool shouldPrintElementsAttrWithHex(int64_t numElements) {
280   // Check to see if a command line option was provided for the limit.
281   if (clOptions.isConstructed()) {
282     if (clOptions->printElementsAttrWithHexIfLarger.getNumOccurrences()) {
283       // -1 is used to disable hex printing.
284       if (clOptions->printElementsAttrWithHexIfLarger == -1)
285         return false;
286       return numElements > clOptions->printElementsAttrWithHexIfLarger;
287     }
288   }
289 
290   // Otherwise, default to printing with hex if the number of elements is >100.
291   return numElements > 100;
292 }
293 
294 //===----------------------------------------------------------------------===//
295 // NewLineCounter
296 //===----------------------------------------------------------------------===//
297 
298 namespace {
299 /// This class is a simple formatter that emits a new line when inputted into a
300 /// stream, that enables counting the number of newlines emitted. This class
301 /// should be used whenever emitting newlines in the printer.
302 struct NewLineCounter {
303   unsigned curLine = 1;
304 };
305 
306 static raw_ostream &operator<<(raw_ostream &os, NewLineCounter &newLine) {
307   ++newLine.curLine;
308   return os << '\n';
309 }
310 } // namespace
311 
312 //===----------------------------------------------------------------------===//
313 // AliasInitializer
314 //===----------------------------------------------------------------------===//
315 
316 namespace {
317 /// This class represents a specific instance of a symbol Alias.
318 class SymbolAlias {
319 public:
320   SymbolAlias(StringRef name, bool isDeferrable)
321       : name(name), suffixIndex(0), hasSuffixIndex(false),
322         isDeferrable(isDeferrable) {}
323   SymbolAlias(StringRef name, uint32_t suffixIndex, bool isDeferrable)
324       : name(name), suffixIndex(suffixIndex), hasSuffixIndex(true),
325         isDeferrable(isDeferrable) {}
326 
327   /// Print this alias to the given stream.
328   void print(raw_ostream &os) const {
329     os << name;
330     if (hasSuffixIndex)
331       os << suffixIndex;
332   }
333 
334   /// Returns true if this alias supports deferred resolution when parsing.
335   bool canBeDeferred() const { return isDeferrable; }
336 
337 private:
338   /// The main name of the alias.
339   StringRef name;
340   /// The optional suffix index of the alias, if multiple aliases had the same
341   /// name.
342   uint32_t suffixIndex : 30;
343   /// A flag indicating whether this alias has a suffix or not.
344   bool hasSuffixIndex : 1;
345   /// A flag indicating whether this alias may be deferred or not.
346   bool isDeferrable : 1;
347 };
348 
349 /// This class represents a utility that initializes the set of attribute and
350 /// type aliases, without the need to store the extra information within the
351 /// main AliasState class or pass it around via function arguments.
352 class AliasInitializer {
353 public:
354   AliasInitializer(
355       DialectInterfaceCollection<OpAsmDialectInterface> &interfaces,
356       llvm::BumpPtrAllocator &aliasAllocator)
357       : interfaces(interfaces), aliasAllocator(aliasAllocator),
358         aliasOS(aliasBuffer) {}
359 
360   void initialize(Operation *op, const OpPrintingFlags &printerFlags,
361                   llvm::MapVector<Attribute, SymbolAlias> &attrToAlias,
362                   llvm::MapVector<Type, SymbolAlias> &typeToAlias);
363 
364   /// Visit the given attribute to see if it has an alias. `canBeDeferred` is
365   /// set to true if the originator of this attribute can resolve the alias
366   /// after parsing has completed (e.g. in the case of operation locations).
367   void visit(Attribute attr, bool canBeDeferred = false);
368 
369   /// Visit the given type to see if it has an alias.
370   void visit(Type type);
371 
372 private:
373   /// Try to generate an alias for the provided symbol. If an alias is
374   /// generated, the provided alias mapping and reverse mapping are updated.
375   /// Returns success if an alias was generated, failure otherwise.
376   template <typename T>
377   LogicalResult
378   generateAlias(T symbol,
379                 llvm::MapVector<StringRef, std::vector<T>> &aliasToSymbol);
380 
381   /// The set of asm interfaces within the context.
382   DialectInterfaceCollection<OpAsmDialectInterface> &interfaces;
383 
384   /// Mapping between an alias and the set of symbols mapped to it.
385   llvm::MapVector<StringRef, std::vector<Attribute>> aliasToAttr;
386   llvm::MapVector<StringRef, std::vector<Type>> aliasToType;
387 
388   /// An allocator used for alias names.
389   llvm::BumpPtrAllocator &aliasAllocator;
390 
391   /// The set of visited attributes.
392   DenseSet<Attribute> visitedAttributes;
393 
394   /// The set of attributes that have aliases *and* can be deferred.
395   DenseSet<Attribute> deferrableAttributes;
396 
397   /// The set of visited types.
398   DenseSet<Type> visitedTypes;
399 
400   /// Storage and stream used when generating an alias.
401   SmallString<32> aliasBuffer;
402   llvm::raw_svector_ostream aliasOS;
403 };
404 
405 /// This class implements a dummy OpAsmPrinter that doesn't print any output,
406 /// and merely collects the attributes and types that *would* be printed in a
407 /// normal print invocation so that we can generate proper aliases. This allows
408 /// for us to generate aliases only for the attributes and types that would be
409 /// in the output, and trims down unnecessary output.
410 class DummyAliasOperationPrinter : private OpAsmPrinter {
411 public:
412   explicit DummyAliasOperationPrinter(const OpPrintingFlags &printerFlags,
413                                       AliasInitializer &initializer)
414       : printerFlags(printerFlags), initializer(initializer) {}
415 
416   /// Print the given operation.
417   void print(Operation *op) {
418     // Visit the operation location.
419     if (printerFlags.shouldPrintDebugInfo())
420       initializer.visit(op->getLoc(), /*canBeDeferred=*/true);
421 
422     // If requested, always print the generic form.
423     if (!printerFlags.shouldPrintGenericOpForm()) {
424       // Check to see if this is a known operation.  If so, use the registered
425       // custom printer hook.
426       if (auto opInfo = op->getRegisteredInfo()) {
427         opInfo->printAssembly(op, *this, /*defaultDialect=*/"");
428         return;
429       }
430     }
431 
432     // Otherwise print with the generic assembly form.
433     printGenericOp(op);
434   }
435 
436 private:
437   /// Print the given operation in the generic form.
438   void printGenericOp(Operation *op, bool printOpName = true) override {
439     // Consider nested operations for aliases.
440     if (op->getNumRegions() != 0) {
441       for (Region &region : op->getRegions())
442         printRegion(region, /*printEntryBlockArgs=*/true,
443                     /*printBlockTerminators=*/true);
444     }
445 
446     // Visit all the types used in the operation.
447     for (Type type : op->getOperandTypes())
448       printType(type);
449     for (Type type : op->getResultTypes())
450       printType(type);
451 
452     // Consider the attributes of the operation for aliases.
453     for (const NamedAttribute &attr : op->getAttrs())
454       printAttribute(attr.getValue());
455   }
456 
457   /// Print the given block. If 'printBlockArgs' is false, the arguments of the
458   /// block are not printed. If 'printBlockTerminator' is false, the terminator
459   /// operation of the block is not printed.
460   void print(Block *block, bool printBlockArgs = true,
461              bool printBlockTerminator = true) {
462     // Consider the types of the block arguments for aliases if 'printBlockArgs'
463     // is set to true.
464     if (printBlockArgs) {
465       for (BlockArgument arg : block->getArguments()) {
466         printType(arg.getType());
467 
468         // Visit the argument location.
469         if (printerFlags.shouldPrintDebugInfo())
470           // TODO: Allow deferring argument locations.
471           initializer.visit(arg.getLoc(), /*canBeDeferred=*/false);
472       }
473     }
474 
475     // Consider the operations within this block, ignoring the terminator if
476     // requested.
477     bool hasTerminator =
478         !block->empty() && block->back().hasTrait<OpTrait::IsTerminator>();
479     auto range = llvm::make_range(
480         block->begin(),
481         std::prev(block->end(),
482                   (!hasTerminator || printBlockTerminator) ? 0 : 1));
483     for (Operation &op : range)
484       print(&op);
485   }
486 
487   /// Print the given region.
488   void printRegion(Region &region, bool printEntryBlockArgs,
489                    bool printBlockTerminators,
490                    bool printEmptyBlock = false) override {
491     if (region.empty())
492       return;
493 
494     auto *entryBlock = &region.front();
495     print(entryBlock, printEntryBlockArgs, printBlockTerminators);
496     for (Block &b : llvm::drop_begin(region, 1))
497       print(&b);
498   }
499 
500   void printRegionArgument(BlockArgument arg, ArrayRef<NamedAttribute> argAttrs,
501                            bool omitType) override {
502     printType(arg.getType());
503     // Visit the argument location.
504     if (printerFlags.shouldPrintDebugInfo())
505       // TODO: Allow deferring argument locations.
506       initializer.visit(arg.getLoc(), /*canBeDeferred=*/false);
507   }
508 
509   /// Consider the given type to be printed for an alias.
510   void printType(Type type) override { initializer.visit(type); }
511 
512   /// Consider the given attribute to be printed for an alias.
513   void printAttribute(Attribute attr) override { initializer.visit(attr); }
514   void printAttributeWithoutType(Attribute attr) override {
515     printAttribute(attr);
516   }
517   LogicalResult printAlias(Attribute attr) override {
518     initializer.visit(attr);
519     return success();
520   }
521   LogicalResult printAlias(Type type) override {
522     initializer.visit(type);
523     return success();
524   }
525 
526   /// Print the given set of attributes with names not included within
527   /// 'elidedAttrs'.
528   void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
529                              ArrayRef<StringRef> elidedAttrs = {}) override {
530     if (attrs.empty())
531       return;
532     if (elidedAttrs.empty()) {
533       for (const NamedAttribute &attr : attrs)
534         printAttribute(attr.getValue());
535       return;
536     }
537     llvm::SmallDenseSet<StringRef> elidedAttrsSet(elidedAttrs.begin(),
538                                                   elidedAttrs.end());
539     for (const NamedAttribute &attr : attrs)
540       if (!elidedAttrsSet.contains(attr.getName().strref()))
541         printAttribute(attr.getValue());
542   }
543   void printOptionalAttrDictWithKeyword(
544       ArrayRef<NamedAttribute> attrs,
545       ArrayRef<StringRef> elidedAttrs = {}) override {
546     printOptionalAttrDict(attrs, elidedAttrs);
547   }
548 
549   /// Return a null stream as the output stream, this will ignore any data fed
550   /// to it.
551   raw_ostream &getStream() const override { return os; }
552 
553   /// The following are hooks of `OpAsmPrinter` that are not necessary for
554   /// determining potential aliases.
555   void printFloat(const APFloat &value) override {}
556   void printAffineMapOfSSAIds(AffineMapAttr, ValueRange) override {}
557   void printAffineExprOfSSAIds(AffineExpr, ValueRange, ValueRange) override {}
558   void printNewline() override {}
559   void printOperand(Value) override {}
560   void printOperand(Value, raw_ostream &os) override {
561     // Users expect the output string to have at least the prefixed % to signal
562     // a value name. To maintain this invariant, emit a name even if it is
563     // guaranteed to go unused.
564     os << "%";
565   }
566   void printKeywordOrString(StringRef) override {}
567   void printSymbolName(StringRef) override {}
568   void printSuccessor(Block *) override {}
569   void printSuccessorAndUseList(Block *, ValueRange) override {}
570   void shadowRegionArgs(Region &, ValueRange) override {}
571 
572   /// The printer flags to use when determining potential aliases.
573   const OpPrintingFlags &printerFlags;
574 
575   /// The initializer to use when identifying aliases.
576   AliasInitializer &initializer;
577 
578   /// A dummy output stream.
579   mutable llvm::raw_null_ostream os;
580 };
581 } // namespace
582 
583 /// Sanitize the given name such that it can be used as a valid identifier. If
584 /// the string needs to be modified in any way, the provided buffer is used to
585 /// store the new copy,
586 static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
587                                     StringRef allowedPunctChars = "$._-",
588                                     bool allowTrailingDigit = true) {
589   assert(!name.empty() && "Shouldn't have an empty name here");
590 
591   auto copyNameToBuffer = [&] {
592     for (char ch : name) {
593       if (llvm::isAlnum(ch) || allowedPunctChars.contains(ch))
594         buffer.push_back(ch);
595       else if (ch == ' ')
596         buffer.push_back('_');
597       else
598         buffer.append(llvm::utohexstr((unsigned char)ch));
599     }
600   };
601 
602   // Check to see if this name is valid. If it starts with a digit, then it
603   // could conflict with the autogenerated numeric ID's, so add an underscore
604   // prefix to avoid problems.
605   if (isdigit(name[0])) {
606     buffer.push_back('_');
607     copyNameToBuffer();
608     return buffer;
609   }
610 
611   // If the name ends with a trailing digit, add a '_' to avoid potential
612   // conflicts with autogenerated ID's.
613   if (!allowTrailingDigit && isdigit(name.back())) {
614     copyNameToBuffer();
615     buffer.push_back('_');
616     return buffer;
617   }
618 
619   // Check to see that the name consists of only valid identifier characters.
620   for (char ch : name) {
621     if (!llvm::isAlnum(ch) && !allowedPunctChars.contains(ch)) {
622       copyNameToBuffer();
623       return buffer;
624     }
625   }
626 
627   // If there are no invalid characters, return the original name.
628   return name;
629 }
630 
631 /// Given a collection of aliases and symbols, initialize a mapping from a
632 /// symbol to a given alias.
633 template <typename T>
634 static void
635 initializeAliases(llvm::MapVector<StringRef, std::vector<T>> &aliasToSymbol,
636                   llvm::MapVector<T, SymbolAlias> &symbolToAlias,
637                   DenseSet<T> *deferrableAliases = nullptr) {
638   std::vector<std::pair<StringRef, std::vector<T>>> aliases =
639       aliasToSymbol.takeVector();
640   llvm::array_pod_sort(aliases.begin(), aliases.end(),
641                        [](const auto *lhs, const auto *rhs) {
642                          return lhs->first.compare(rhs->first);
643                        });
644 
645   for (auto &it : aliases) {
646     // If there is only one instance for this alias, use the name directly.
647     if (it.second.size() == 1) {
648       T symbol = it.second.front();
649       bool isDeferrable = deferrableAliases && deferrableAliases->count(symbol);
650       symbolToAlias.insert({symbol, SymbolAlias(it.first, isDeferrable)});
651       continue;
652     }
653     // Otherwise, add the index to the name.
654     for (int i = 0, e = it.second.size(); i < e; ++i) {
655       T symbol = it.second[i];
656       bool isDeferrable = deferrableAliases && deferrableAliases->count(symbol);
657       symbolToAlias.insert({symbol, SymbolAlias(it.first, i, isDeferrable)});
658     }
659   }
660 }
661 
662 void AliasInitializer::initialize(
663     Operation *op, const OpPrintingFlags &printerFlags,
664     llvm::MapVector<Attribute, SymbolAlias> &attrToAlias,
665     llvm::MapVector<Type, SymbolAlias> &typeToAlias) {
666   // Use a dummy printer when walking the IR so that we can collect the
667   // attributes/types that will actually be used during printing when
668   // considering aliases.
669   DummyAliasOperationPrinter aliasPrinter(printerFlags, *this);
670   aliasPrinter.print(op);
671 
672   // Initialize the aliases sorted by name.
673   initializeAliases(aliasToAttr, attrToAlias, &deferrableAttributes);
674   initializeAliases(aliasToType, typeToAlias);
675 }
676 
677 void AliasInitializer::visit(Attribute attr, bool canBeDeferred) {
678   if (!visitedAttributes.insert(attr).second) {
679     // If this attribute already has an alias and this instance can't be
680     // deferred, make sure that the alias isn't deferred.
681     if (!canBeDeferred)
682       deferrableAttributes.erase(attr);
683     return;
684   }
685 
686   // Try to generate an alias for this attribute.
687   if (succeeded(generateAlias(attr, aliasToAttr))) {
688     if (canBeDeferred)
689       deferrableAttributes.insert(attr);
690     return;
691   }
692 
693   // Check for any sub elements.
694   if (auto subElementInterface = attr.dyn_cast<SubElementAttrInterface>()) {
695     subElementInterface.walkSubElements([&](Attribute attr) { visit(attr); },
696                                         [&](Type type) { visit(type); });
697   }
698 }
699 
700 void AliasInitializer::visit(Type type) {
701   if (!visitedTypes.insert(type).second)
702     return;
703 
704   // Try to generate an alias for this type.
705   if (succeeded(generateAlias(type, aliasToType)))
706     return;
707 
708   // Check for any sub elements.
709   if (auto subElementInterface = type.dyn_cast<SubElementTypeInterface>()) {
710     subElementInterface.walkSubElements([&](Attribute attr) { visit(attr); },
711                                         [&](Type type) { visit(type); });
712   }
713 }
714 
715 template <typename T>
716 LogicalResult AliasInitializer::generateAlias(
717     T symbol, llvm::MapVector<StringRef, std::vector<T>> &aliasToSymbol) {
718   SmallString<32> nameBuffer;
719   for (const auto &interface : interfaces) {
720     OpAsmDialectInterface::AliasResult result =
721         interface.getAlias(symbol, aliasOS);
722     if (result == OpAsmDialectInterface::AliasResult::NoAlias)
723       continue;
724     nameBuffer = std::move(aliasBuffer);
725     assert(!nameBuffer.empty() && "expected valid alias name");
726     if (result == OpAsmDialectInterface::AliasResult::FinalAlias)
727       break;
728   }
729 
730   if (nameBuffer.empty())
731     return failure();
732 
733   SmallString<16> tempBuffer;
734   StringRef name =
735       sanitizeIdentifier(nameBuffer, tempBuffer, /*allowedPunctChars=*/"$_-",
736                          /*allowTrailingDigit=*/false);
737   name = name.copy(aliasAllocator);
738   aliasToSymbol[name].push_back(symbol);
739   return success();
740 }
741 
742 //===----------------------------------------------------------------------===//
743 // AliasState
744 //===----------------------------------------------------------------------===//
745 
746 namespace {
747 /// This class manages the state for type and attribute aliases.
748 class AliasState {
749 public:
750   // Initialize the internal aliases.
751   void
752   initialize(Operation *op, const OpPrintingFlags &printerFlags,
753              DialectInterfaceCollection<OpAsmDialectInterface> &interfaces);
754 
755   /// Get an alias for the given attribute if it has one and print it in `os`.
756   /// Returns success if an alias was printed, failure otherwise.
757   LogicalResult getAlias(Attribute attr, raw_ostream &os) const;
758 
759   /// Get an alias for the given type if it has one and print it in `os`.
760   /// Returns success if an alias was printed, failure otherwise.
761   LogicalResult getAlias(Type ty, raw_ostream &os) const;
762 
763   /// Print all of the referenced aliases that can not be resolved in a deferred
764   /// manner.
765   void printNonDeferredAliases(raw_ostream &os, NewLineCounter &newLine) const {
766     printAliases(os, newLine, /*isDeferred=*/false);
767   }
768 
769   /// Print all of the referenced aliases that support deferred resolution.
770   void printDeferredAliases(raw_ostream &os, NewLineCounter &newLine) const {
771     printAliases(os, newLine, /*isDeferred=*/true);
772   }
773 
774 private:
775   /// Print all of the referenced aliases that support the provided resolution
776   /// behavior.
777   void printAliases(raw_ostream &os, NewLineCounter &newLine,
778                     bool isDeferred) const;
779 
780   /// Mapping between attribute and alias.
781   llvm::MapVector<Attribute, SymbolAlias> attrToAlias;
782   /// Mapping between type and alias.
783   llvm::MapVector<Type, SymbolAlias> typeToAlias;
784 
785   /// An allocator used for alias names.
786   llvm::BumpPtrAllocator aliasAllocator;
787 };
788 } // namespace
789 
790 void AliasState::initialize(
791     Operation *op, const OpPrintingFlags &printerFlags,
792     DialectInterfaceCollection<OpAsmDialectInterface> &interfaces) {
793   AliasInitializer initializer(interfaces, aliasAllocator);
794   initializer.initialize(op, printerFlags, attrToAlias, typeToAlias);
795 }
796 
797 LogicalResult AliasState::getAlias(Attribute attr, raw_ostream &os) const {
798   auto it = attrToAlias.find(attr);
799   if (it == attrToAlias.end())
800     return failure();
801   it->second.print(os << '#');
802   return success();
803 }
804 
805 LogicalResult AliasState::getAlias(Type ty, raw_ostream &os) const {
806   auto it = typeToAlias.find(ty);
807   if (it == typeToAlias.end())
808     return failure();
809 
810   it->second.print(os << '!');
811   return success();
812 }
813 
814 void AliasState::printAliases(raw_ostream &os, NewLineCounter &newLine,
815                               bool isDeferred) const {
816   auto filterFn = [=](const auto &aliasIt) {
817     return aliasIt.second.canBeDeferred() == isDeferred;
818   };
819   for (const auto &it : llvm::make_filter_range(attrToAlias, filterFn)) {
820     it.second.print(os << '#');
821     os << " = " << it.first << newLine;
822   }
823   for (const auto &it : llvm::make_filter_range(typeToAlias, filterFn)) {
824     it.second.print(os << '!');
825     os << " = type " << it.first << newLine;
826   }
827 }
828 
829 //===----------------------------------------------------------------------===//
830 // SSANameState
831 //===----------------------------------------------------------------------===//
832 
833 namespace {
834 /// Info about block printing: a number which is its position in the visitation
835 /// order, and a name that is used to print reference to it, e.g. ^bb42.
836 struct BlockInfo {
837   int ordering;
838   StringRef name;
839 };
840 
841 /// This class manages the state of SSA value names.
842 class SSANameState {
843 public:
844   /// A sentinel value used for values with names set.
845   enum : unsigned { NameSentinel = ~0U };
846 
847   SSANameState(Operation *op, const OpPrintingFlags &printerFlags);
848 
849   /// Print the SSA identifier for the given value to 'stream'. If
850   /// 'printResultNo' is true, it also presents the result number ('#' number)
851   /// of this value.
852   void printValueID(Value value, bool printResultNo, raw_ostream &stream) const;
853 
854   /// Print the operation identifier.
855   void printOperationID(Operation *op, raw_ostream &stream) const;
856 
857   /// Return the result indices for each of the result groups registered by this
858   /// operation, or empty if none exist.
859   ArrayRef<int> getOpResultGroups(Operation *op);
860 
861   /// Get the info for the given block.
862   BlockInfo getBlockInfo(Block *block);
863 
864   /// Renumber the arguments for the specified region to the same names as the
865   /// SSA values in namesToUse. See OperationPrinter::shadowRegionArgs for
866   /// details.
867   void shadowRegionArgs(Region &region, ValueRange namesToUse);
868 
869 private:
870   /// Number the SSA values within the given IR unit.
871   void numberValuesInRegion(Region &region);
872   void numberValuesInBlock(Block &block);
873   void numberValuesInOp(Operation &op);
874 
875   /// Given a result of an operation 'result', find the result group head
876   /// 'lookupValue' and the result of 'result' within that group in
877   /// 'lookupResultNo'. 'lookupResultNo' is only filled in if the result group
878   /// has more than 1 result.
879   void getResultIDAndNumber(OpResult result, Value &lookupValue,
880                             Optional<int> &lookupResultNo) const;
881 
882   /// Set a special value name for the given value.
883   void setValueName(Value value, StringRef name);
884 
885   /// Uniques the given value name within the printer. If the given name
886   /// conflicts, it is automatically renamed.
887   StringRef uniqueValueName(StringRef name);
888 
889   /// This is the value ID for each SSA value. If this returns NameSentinel,
890   /// then the valueID has an entry in valueNames.
891   DenseMap<Value, unsigned> valueIDs;
892   DenseMap<Value, StringRef> valueNames;
893 
894   /// When printing users of values, an operation without a result might
895   /// be the user. This map holds ids for such operations.
896   DenseMap<Operation *, unsigned> operationIDs;
897 
898   /// This is a map of operations that contain multiple named result groups,
899   /// i.e. there may be multiple names for the results of the operation. The
900   /// value of this map are the result numbers that start a result group.
901   DenseMap<Operation *, SmallVector<int, 1>> opResultGroups;
902 
903   /// This maps blocks to there visitation number in the current region as well
904   /// as the string representing their name.
905   DenseMap<Block *, BlockInfo> blockNames;
906 
907   /// This keeps track of all of the non-numeric names that are in flight,
908   /// allowing us to check for duplicates.
909   /// Note: the value of the map is unused.
910   llvm::ScopedHashTable<StringRef, char> usedNames;
911   llvm::BumpPtrAllocator usedNameAllocator;
912 
913   /// This is the next value ID to assign in numbering.
914   unsigned nextValueID = 0;
915   /// This is the next ID to assign to a region entry block argument.
916   unsigned nextArgumentID = 0;
917   /// This is the next ID to assign when a name conflict is detected.
918   unsigned nextConflictID = 0;
919 
920   /// These are the printing flags.  They control, eg., whether to print in
921   /// generic form.
922   OpPrintingFlags printerFlags;
923 };
924 } // namespace
925 
926 SSANameState::SSANameState(
927     Operation *op, const OpPrintingFlags &printerFlags)
928     : printerFlags(printerFlags) {
929   llvm::SaveAndRestore<unsigned> valueIDSaver(nextValueID);
930   llvm::SaveAndRestore<unsigned> argumentIDSaver(nextArgumentID);
931   llvm::SaveAndRestore<unsigned> conflictIDSaver(nextConflictID);
932 
933   // The naming context includes `nextValueID`, `nextArgumentID`,
934   // `nextConflictID` and `usedNames` scoped HashTable. This information is
935   // carried from the parent region.
936   using UsedNamesScopeTy = llvm::ScopedHashTable<StringRef, char>::ScopeTy;
937   using NamingContext =
938       std::tuple<Region *, unsigned, unsigned, unsigned, UsedNamesScopeTy *>;
939 
940   // Allocator for UsedNamesScopeTy
941   llvm::BumpPtrAllocator allocator;
942 
943   // Add a scope for the top level operation.
944   auto *topLevelNamesScope =
945       new (allocator.Allocate<UsedNamesScopeTy>()) UsedNamesScopeTy(usedNames);
946 
947   SmallVector<NamingContext, 8> nameContext;
948   for (Region &region : op->getRegions())
949     nameContext.push_back(std::make_tuple(&region, nextValueID, nextArgumentID,
950                                           nextConflictID, topLevelNamesScope));
951 
952   numberValuesInOp(*op);
953 
954   while (!nameContext.empty()) {
955     Region *region;
956     UsedNamesScopeTy *parentScope;
957     std::tie(region, nextValueID, nextArgumentID, nextConflictID, parentScope) =
958         nameContext.pop_back_val();
959 
960     // When we switch from one subtree to another, pop the scopes(needless)
961     // until the parent scope.
962     while (usedNames.getCurScope() != parentScope) {
963       usedNames.getCurScope()->~UsedNamesScopeTy();
964       assert((usedNames.getCurScope() != nullptr || parentScope == nullptr) &&
965              "top level parentScope must be a nullptr");
966     }
967 
968     // Add a scope for the current region.
969     auto *curNamesScope = new (allocator.Allocate<UsedNamesScopeTy>())
970         UsedNamesScopeTy(usedNames);
971 
972     numberValuesInRegion(*region);
973 
974     for (Operation &op : region->getOps())
975       for (Region &region : op.getRegions())
976         nameContext.push_back(std::make_tuple(&region, nextValueID,
977                                               nextArgumentID, nextConflictID,
978                                               curNamesScope));
979   }
980 
981   // Manually remove all the scopes.
982   while (usedNames.getCurScope() != nullptr)
983     usedNames.getCurScope()->~UsedNamesScopeTy();
984 }
985 
986 void SSANameState::printValueID(Value value, bool printResultNo,
987                                 raw_ostream &stream) const {
988   if (!value) {
989     stream << "<<NULL VALUE>>";
990     return;
991   }
992 
993   Optional<int> resultNo;
994   auto lookupValue = value;
995 
996   // If this is an operation result, collect the head lookup value of the result
997   // group and the result number of 'result' within that group.
998   if (OpResult result = value.dyn_cast<OpResult>())
999     getResultIDAndNumber(result, lookupValue, resultNo);
1000 
1001   auto it = valueIDs.find(lookupValue);
1002   if (it == valueIDs.end()) {
1003     stream << "<<UNKNOWN SSA VALUE>>";
1004     return;
1005   }
1006 
1007   stream << '%';
1008   if (it->second != NameSentinel) {
1009     stream << it->second;
1010   } else {
1011     auto nameIt = valueNames.find(lookupValue);
1012     assert(nameIt != valueNames.end() && "Didn't have a name entry?");
1013     stream << nameIt->second;
1014   }
1015 
1016   if (resultNo.hasValue() && printResultNo)
1017     stream << '#' << resultNo;
1018 }
1019 
1020 void SSANameState::printOperationID(Operation *op, raw_ostream &stream) const {
1021   auto it = operationIDs.find(op);
1022   if (it == operationIDs.end()) {
1023     stream << "<<UNKOWN OPERATION>>";
1024   } else {
1025     stream << '%' << it->second;
1026   }
1027 }
1028 
1029 ArrayRef<int> SSANameState::getOpResultGroups(Operation *op) {
1030   auto it = opResultGroups.find(op);
1031   return it == opResultGroups.end() ? ArrayRef<int>() : it->second;
1032 }
1033 
1034 BlockInfo SSANameState::getBlockInfo(Block *block) {
1035   auto it = blockNames.find(block);
1036   BlockInfo invalidBlock{-1, "INVALIDBLOCK"};
1037   return it != blockNames.end() ? it->second : invalidBlock;
1038 }
1039 
1040 void SSANameState::shadowRegionArgs(Region &region, ValueRange namesToUse) {
1041   assert(!region.empty() && "cannot shadow arguments of an empty region");
1042   assert(region.getNumArguments() == namesToUse.size() &&
1043          "incorrect number of names passed in");
1044   assert(region.getParentOp()->hasTrait<OpTrait::IsIsolatedFromAbove>() &&
1045          "only KnownIsolatedFromAbove ops can shadow names");
1046 
1047   SmallVector<char, 16> nameStr;
1048   for (unsigned i = 0, e = namesToUse.size(); i != e; ++i) {
1049     auto nameToUse = namesToUse[i];
1050     if (nameToUse == nullptr)
1051       continue;
1052     auto nameToReplace = region.getArgument(i);
1053 
1054     nameStr.clear();
1055     llvm::raw_svector_ostream nameStream(nameStr);
1056     printValueID(nameToUse, /*printResultNo=*/true, nameStream);
1057 
1058     // Entry block arguments should already have a pretty "arg" name.
1059     assert(valueIDs[nameToReplace] == NameSentinel);
1060 
1061     // Use the name without the leading %.
1062     auto name = StringRef(nameStream.str()).drop_front();
1063 
1064     // Overwrite the name.
1065     valueNames[nameToReplace] = name.copy(usedNameAllocator);
1066   }
1067 }
1068 
1069 void SSANameState::numberValuesInRegion(Region &region) {
1070   auto setBlockArgNameFn = [&](Value arg, StringRef name) {
1071     assert(!valueIDs.count(arg) && "arg numbered multiple times");
1072     assert(arg.cast<BlockArgument>().getOwner()->getParent() == &region &&
1073            "arg not defined in current region");
1074     setValueName(arg, name);
1075   };
1076 
1077   if (!printerFlags.shouldPrintGenericOpForm()) {
1078     if (Operation *op = region.getParentOp()) {
1079       if (auto asmInterface = dyn_cast<OpAsmOpInterface>(op))
1080         asmInterface.getAsmBlockArgumentNames(region, setBlockArgNameFn);
1081     }
1082   }
1083 
1084   // Number the values within this region in a breadth-first order.
1085   unsigned nextBlockID = 0;
1086   for (auto &block : region) {
1087     // Each block gets a unique ID, and all of the operations within it get
1088     // numbered as well.
1089     auto blockInfoIt = blockNames.insert({&block, {-1, ""}});
1090     if (blockInfoIt.second) {
1091       // This block hasn't been named through `getAsmBlockArgumentNames`, use
1092       // default `^bbNNN` format.
1093       std::string name;
1094       llvm::raw_string_ostream(name) << "^bb" << nextBlockID;
1095       blockInfoIt.first->second.name = StringRef(name).copy(usedNameAllocator);
1096     }
1097     blockInfoIt.first->second.ordering = nextBlockID++;
1098 
1099     numberValuesInBlock(block);
1100   }
1101 }
1102 
1103 void SSANameState::numberValuesInBlock(Block &block) {
1104   // Number the block arguments. We give entry block arguments a special name
1105   // 'arg'.
1106   bool isEntryBlock = block.isEntryBlock();
1107   SmallString<32> specialNameBuffer(isEntryBlock ? "arg" : "");
1108   llvm::raw_svector_ostream specialName(specialNameBuffer);
1109   for (auto arg : block.getArguments()) {
1110     if (valueIDs.count(arg))
1111       continue;
1112     if (isEntryBlock) {
1113       specialNameBuffer.resize(strlen("arg"));
1114       specialName << nextArgumentID++;
1115     }
1116     setValueName(arg, specialName.str());
1117   }
1118 
1119   // Number the operations in this block.
1120   for (auto &op : block)
1121     numberValuesInOp(op);
1122 }
1123 
1124 void SSANameState::numberValuesInOp(Operation &op) {
1125   // Function used to set the special result names for the operation.
1126   SmallVector<int, 2> resultGroups(/*Size=*/1, /*Value=*/0);
1127   auto setResultNameFn = [&](Value result, StringRef name) {
1128     assert(!valueIDs.count(result) && "result numbered multiple times");
1129     assert(result.getDefiningOp() == &op && "result not defined by 'op'");
1130     setValueName(result, name);
1131 
1132     // Record the result number for groups not anchored at 0.
1133     if (int resultNo = result.cast<OpResult>().getResultNumber())
1134       resultGroups.push_back(resultNo);
1135   };
1136   // Operations can customize the printing of block names in OpAsmOpInterface.
1137   auto setBlockNameFn = [&](Block *block, StringRef name) {
1138     assert(block->getParentOp() == &op &&
1139            "getAsmBlockArgumentNames callback invoked on a block not directly "
1140            "nested under the current operation");
1141     assert(!blockNames.count(block) && "block numbered multiple times");
1142     SmallString<16> tmpBuffer{"^"};
1143     name = sanitizeIdentifier(name, tmpBuffer);
1144     if (name.data() != tmpBuffer.data()) {
1145       tmpBuffer.append(name);
1146       name = tmpBuffer.str();
1147     }
1148     name = name.copy(usedNameAllocator);
1149     blockNames[block] = {-1, name};
1150   };
1151 
1152   if (!printerFlags.shouldPrintGenericOpForm()) {
1153     if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(&op)) {
1154       asmInterface.getAsmBlockNames(setBlockNameFn);
1155       asmInterface.getAsmResultNames(setResultNameFn);
1156     }
1157   }
1158 
1159   unsigned numResults = op.getNumResults();
1160   if (numResults == 0) {
1161     // If value users should be printed, operations with no result need an id.
1162     if (printerFlags.shouldPrintValueUsers()) {
1163       if (operationIDs.try_emplace(&op, nextValueID).second)
1164         ++nextValueID;
1165     }
1166     return;
1167   }
1168   Value resultBegin = op.getResult(0);
1169 
1170   // If the first result wasn't numbered, give it a default number.
1171   if (valueIDs.try_emplace(resultBegin, nextValueID).second)
1172     ++nextValueID;
1173 
1174   // If this operation has multiple result groups, mark it.
1175   if (resultGroups.size() != 1) {
1176     llvm::array_pod_sort(resultGroups.begin(), resultGroups.end());
1177     opResultGroups.try_emplace(&op, std::move(resultGroups));
1178   }
1179 }
1180 
1181 void SSANameState::getResultIDAndNumber(OpResult result, Value &lookupValue,
1182                                         Optional<int> &lookupResultNo) const {
1183   Operation *owner = result.getOwner();
1184   if (owner->getNumResults() == 1)
1185     return;
1186   int resultNo = result.getResultNumber();
1187 
1188   // If this operation has multiple result groups, we will need to find the
1189   // one corresponding to this result.
1190   auto resultGroupIt = opResultGroups.find(owner);
1191   if (resultGroupIt == opResultGroups.end()) {
1192     // If not, just use the first result.
1193     lookupResultNo = resultNo;
1194     lookupValue = owner->getResult(0);
1195     return;
1196   }
1197 
1198   // Find the correct index using a binary search, as the groups are ordered.
1199   ArrayRef<int> resultGroups = resultGroupIt->second;
1200   const auto *it = llvm::upper_bound(resultGroups, resultNo);
1201   int groupResultNo = 0, groupSize = 0;
1202 
1203   // If there are no smaller elements, the last result group is the lookup.
1204   if (it == resultGroups.end()) {
1205     groupResultNo = resultGroups.back();
1206     groupSize = static_cast<int>(owner->getNumResults()) - resultGroups.back();
1207   } else {
1208     // Otherwise, the previous element is the lookup.
1209     groupResultNo = *std::prev(it);
1210     groupSize = *it - groupResultNo;
1211   }
1212 
1213   // We only record the result number for a group of size greater than 1.
1214   if (groupSize != 1)
1215     lookupResultNo = resultNo - groupResultNo;
1216   lookupValue = owner->getResult(groupResultNo);
1217 }
1218 
1219 void SSANameState::setValueName(Value value, StringRef name) {
1220   // If the name is empty, the value uses the default numbering.
1221   if (name.empty()) {
1222     valueIDs[value] = nextValueID++;
1223     return;
1224   }
1225 
1226   valueIDs[value] = NameSentinel;
1227   valueNames[value] = uniqueValueName(name);
1228 }
1229 
1230 StringRef SSANameState::uniqueValueName(StringRef name) {
1231   SmallString<16> tmpBuffer;
1232   name = sanitizeIdentifier(name, tmpBuffer);
1233 
1234   // Check to see if this name is already unique.
1235   if (!usedNames.count(name)) {
1236     name = name.copy(usedNameAllocator);
1237   } else {
1238     // Otherwise, we had a conflict - probe until we find a unique name. This
1239     // is guaranteed to terminate (and usually in a single iteration) because it
1240     // generates new names by incrementing nextConflictID.
1241     SmallString<64> probeName(name);
1242     probeName.push_back('_');
1243     while (true) {
1244       probeName += llvm::utostr(nextConflictID++);
1245       if (!usedNames.count(probeName)) {
1246         name = probeName.str().copy(usedNameAllocator);
1247         break;
1248       }
1249       probeName.resize(name.size() + 1);
1250     }
1251   }
1252 
1253   usedNames.insert(name, char());
1254   return name;
1255 }
1256 
1257 //===----------------------------------------------------------------------===//
1258 // AsmState
1259 //===----------------------------------------------------------------------===//
1260 
1261 namespace mlir {
1262 namespace detail {
1263 class AsmStateImpl {
1264 public:
1265   explicit AsmStateImpl(Operation *op, const OpPrintingFlags &printerFlags,
1266                         AsmState::LocationMap *locationMap)
1267       : interfaces(op->getContext()), nameState(op, printerFlags),
1268         printerFlags(printerFlags), locationMap(locationMap) {}
1269 
1270   /// Initialize the alias state to enable the printing of aliases.
1271   void initializeAliases(Operation *op) {
1272     aliasState.initialize(op, printerFlags, interfaces);
1273   }
1274 
1275   /// Get the state used for aliases.
1276   AliasState &getAliasState() { return aliasState; }
1277 
1278   /// Get the state used for SSA names.
1279   SSANameState &getSSANameState() { return nameState; }
1280 
1281   /// Get the printer flags.
1282   const OpPrintingFlags &getPrinterFlags() const { return printerFlags; }
1283 
1284   /// Register the location, line and column, within the buffer that the given
1285   /// operation was printed at.
1286   void registerOperationLocation(Operation *op, unsigned line, unsigned col) {
1287     if (locationMap)
1288       (*locationMap)[op] = std::make_pair(line, col);
1289   }
1290 
1291 private:
1292   /// Collection of OpAsm interfaces implemented in the context.
1293   DialectInterfaceCollection<OpAsmDialectInterface> interfaces;
1294 
1295   /// The state used for attribute and type aliases.
1296   AliasState aliasState;
1297 
1298   /// The state used for SSA value names.
1299   SSANameState nameState;
1300 
1301   /// Flags that control op output.
1302   OpPrintingFlags printerFlags;
1303 
1304   /// An optional location map to be populated.
1305   AsmState::LocationMap *locationMap;
1306 };
1307 } // namespace detail
1308 } // namespace mlir
1309 
1310 /// Verifies the operation and switches to generic op printing if verification
1311 /// fails. We need to do this because custom print functions may fail for
1312 /// invalid ops.
1313 static OpPrintingFlags verifyOpAndAdjustFlags(Operation *op,
1314                                               OpPrintingFlags printerFlags) {
1315   if (printerFlags.shouldPrintGenericOpForm() ||
1316       printerFlags.shouldAssumeVerified())
1317     return printerFlags;
1318 
1319   LLVM_DEBUG(llvm::dbgs() << DEBUG_TYPE << ": Verifying operation: "
1320                           << op->getName() << "\n");
1321 
1322   // Ignore errors emitted by the verifier. We check the thread id to avoid
1323   // consuming other threads' errors.
1324   auto parentThreadId = llvm::get_threadid();
1325   ScopedDiagnosticHandler diagHandler(op->getContext(), [&](Diagnostic &diag) {
1326     if (parentThreadId == llvm::get_threadid()) {
1327       LLVM_DEBUG({
1328         diag.print(llvm::dbgs());
1329         llvm::dbgs() << "\n";
1330       });
1331       return success();
1332     }
1333     return failure();
1334   });
1335   if (failed(verify(op))) {
1336     LLVM_DEBUG(llvm::dbgs()
1337                << DEBUG_TYPE << ": '" << op->getName()
1338                << "' failed to verify and will be printed in generic form\n");
1339     printerFlags.printGenericOpForm();
1340   }
1341 
1342   return printerFlags;
1343 }
1344 
1345 AsmState::AsmState(Operation *op, const OpPrintingFlags &printerFlags,
1346                    LocationMap *locationMap)
1347     : impl(std::make_unique<AsmStateImpl>(
1348           op, verifyOpAndAdjustFlags(op, printerFlags), locationMap)) {}
1349 AsmState::~AsmState() = default;
1350 
1351 const OpPrintingFlags &AsmState::getPrinterFlags() const {
1352   return impl->getPrinterFlags();
1353 }
1354 
1355 //===----------------------------------------------------------------------===//
1356 // AsmPrinter::Impl
1357 //===----------------------------------------------------------------------===//
1358 
1359 namespace mlir {
1360 class AsmPrinter::Impl {
1361 public:
1362   Impl(raw_ostream &os, OpPrintingFlags flags = llvm::None,
1363        AsmStateImpl *state = nullptr)
1364       : os(os), printerFlags(flags), state(state) {}
1365   explicit Impl(Impl &other)
1366       : Impl(other.os, other.printerFlags, other.state) {}
1367 
1368   /// Returns the output stream of the printer.
1369   raw_ostream &getStream() { return os; }
1370 
1371   template <typename Container, typename UnaryFunctor>
1372   inline void interleaveComma(const Container &c, UnaryFunctor eachFn) const {
1373     llvm::interleaveComma(c, os, eachFn);
1374   }
1375 
1376   /// This enum describes the different kinds of elision for the type of an
1377   /// attribute when printing it.
1378   enum class AttrTypeElision {
1379     /// The type must not be elided,
1380     Never,
1381     /// The type may be elided when it matches the default used in the parser
1382     /// (for example i64 is the default for integer attributes).
1383     May,
1384     /// The type must be elided.
1385     Must
1386   };
1387 
1388   /// Print the given attribute.
1389   void printAttribute(Attribute attr,
1390                       AttrTypeElision typeElision = AttrTypeElision::Never);
1391 
1392   /// Print the alias for the given attribute, return failure if no alias could
1393   /// be printed.
1394   LogicalResult printAlias(Attribute attr);
1395 
1396   void printType(Type type);
1397 
1398   /// Print the alias for the given type, return failure if no alias could
1399   /// be printed.
1400   LogicalResult printAlias(Type type);
1401 
1402   /// Print the given location to the stream. If `allowAlias` is true, this
1403   /// allows for the internal location to use an attribute alias.
1404   void printLocation(LocationAttr loc, bool allowAlias = false);
1405 
1406   void printAffineMap(AffineMap map);
1407   void
1408   printAffineExpr(AffineExpr expr,
1409                   function_ref<void(unsigned, bool)> printValueName = nullptr);
1410   void printAffineConstraint(AffineExpr expr, bool isEq);
1411   void printIntegerSet(IntegerSet set);
1412 
1413 protected:
1414   void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
1415                              ArrayRef<StringRef> elidedAttrs = {},
1416                              bool withKeyword = false);
1417   void printNamedAttribute(NamedAttribute attr);
1418   void printTrailingLocation(Location loc, bool allowAlias = true);
1419   void printLocationInternal(LocationAttr loc, bool pretty = false);
1420 
1421   /// Print a dense elements attribute. If 'allowHex' is true, a hex string is
1422   /// used instead of individual elements when the elements attr is large.
1423   void printDenseElementsAttr(DenseElementsAttr attr, bool allowHex);
1424 
1425   /// Print a dense string elements attribute.
1426   void printDenseStringElementsAttr(DenseStringElementsAttr attr);
1427 
1428   /// Print a dense elements attribute. If 'allowHex' is true, a hex string is
1429   /// used instead of individual elements when the elements attr is large.
1430   void printDenseIntOrFPElementsAttr(DenseIntOrFPElementsAttr attr,
1431                                      bool allowHex);
1432 
1433   void printDialectAttribute(Attribute attr);
1434   void printDialectType(Type type);
1435 
1436   /// This enum is used to represent the binding strength of the enclosing
1437   /// context that an AffineExprStorage is being printed in, so we can
1438   /// intelligently produce parens.
1439   enum class BindingStrength {
1440     Weak,   // + and -
1441     Strong, // All other binary operators.
1442   };
1443   void printAffineExprInternal(
1444       AffineExpr expr, BindingStrength enclosingTightness,
1445       function_ref<void(unsigned, bool)> printValueName = nullptr);
1446 
1447   /// The output stream for the printer.
1448   raw_ostream &os;
1449 
1450   /// A set of flags to control the printer's behavior.
1451   OpPrintingFlags printerFlags;
1452 
1453   /// An optional printer state for the module.
1454   AsmStateImpl *state;
1455 
1456   /// A tracker for the number of new lines emitted during printing.
1457   NewLineCounter newLine;
1458 };
1459 } // namespace mlir
1460 
1461 void AsmPrinter::Impl::printTrailingLocation(Location loc, bool allowAlias) {
1462   // Check to see if we are printing debug information.
1463   if (!printerFlags.shouldPrintDebugInfo())
1464     return;
1465 
1466   os << " ";
1467   printLocation(loc, /*allowAlias=*/allowAlias);
1468 }
1469 
1470 void AsmPrinter::Impl::printLocationInternal(LocationAttr loc, bool pretty) {
1471   TypeSwitch<LocationAttr>(loc)
1472       .Case<OpaqueLoc>([&](OpaqueLoc loc) {
1473         printLocationInternal(loc.getFallbackLocation(), pretty);
1474       })
1475       .Case<UnknownLoc>([&](UnknownLoc loc) {
1476         if (pretty)
1477           os << "[unknown]";
1478         else
1479           os << "unknown";
1480       })
1481       .Case<FileLineColLoc>([&](FileLineColLoc loc) {
1482         if (pretty) {
1483           os << loc.getFilename().getValue();
1484         } else {
1485           os << "\"";
1486           printEscapedString(loc.getFilename(), os);
1487           os << "\"";
1488         }
1489         os << ':' << loc.getLine() << ':' << loc.getColumn();
1490       })
1491       .Case<NameLoc>([&](NameLoc loc) {
1492         os << '\"';
1493         printEscapedString(loc.getName(), os);
1494         os << '\"';
1495 
1496         // Print the child if it isn't unknown.
1497         auto childLoc = loc.getChildLoc();
1498         if (!childLoc.isa<UnknownLoc>()) {
1499           os << '(';
1500           printLocationInternal(childLoc, pretty);
1501           os << ')';
1502         }
1503       })
1504       .Case<CallSiteLoc>([&](CallSiteLoc loc) {
1505         Location caller = loc.getCaller();
1506         Location callee = loc.getCallee();
1507         if (!pretty)
1508           os << "callsite(";
1509         printLocationInternal(callee, pretty);
1510         if (pretty) {
1511           if (callee.isa<NameLoc>()) {
1512             if (caller.isa<FileLineColLoc>()) {
1513               os << " at ";
1514             } else {
1515               os << newLine << " at ";
1516             }
1517           } else {
1518             os << newLine << " at ";
1519           }
1520         } else {
1521           os << " at ";
1522         }
1523         printLocationInternal(caller, pretty);
1524         if (!pretty)
1525           os << ")";
1526       })
1527       .Case<FusedLoc>([&](FusedLoc loc) {
1528         if (!pretty)
1529           os << "fused";
1530         if (Attribute metadata = loc.getMetadata())
1531           os << '<' << metadata << '>';
1532         os << '[';
1533         interleave(
1534             loc.getLocations(),
1535             [&](Location loc) { printLocationInternal(loc, pretty); },
1536             [&]() { os << ", "; });
1537         os << ']';
1538       });
1539 }
1540 
1541 /// Print a floating point value in a way that the parser will be able to
1542 /// round-trip losslessly.
1543 static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
1544   // We would like to output the FP constant value in exponential notation,
1545   // but we cannot do this if doing so will lose precision.  Check here to
1546   // make sure that we only output it in exponential format if we can parse
1547   // the value back and get the same value.
1548   bool isInf = apValue.isInfinity();
1549   bool isNaN = apValue.isNaN();
1550   if (!isInf && !isNaN) {
1551     SmallString<128> strValue;
1552     apValue.toString(strValue, /*FormatPrecision=*/6, /*FormatMaxPadding=*/0,
1553                      /*TruncateZero=*/false);
1554 
1555     // Check to make sure that the stringized number is not some string like
1556     // "Inf" or NaN, that atof will accept, but the lexer will not.  Check
1557     // that the string matches the "[-+]?[0-9]" regex.
1558     assert(((strValue[0] >= '0' && strValue[0] <= '9') ||
1559             ((strValue[0] == '-' || strValue[0] == '+') &&
1560              (strValue[1] >= '0' && strValue[1] <= '9'))) &&
1561            "[-+]?[0-9] regex does not match!");
1562 
1563     // Parse back the stringized version and check that the value is equal
1564     // (i.e., there is no precision loss).
1565     if (APFloat(apValue.getSemantics(), strValue).bitwiseIsEqual(apValue)) {
1566       os << strValue;
1567       return;
1568     }
1569 
1570     // If it is not, use the default format of APFloat instead of the
1571     // exponential notation.
1572     strValue.clear();
1573     apValue.toString(strValue);
1574 
1575     // Make sure that we can parse the default form as a float.
1576     if (strValue.str().contains('.')) {
1577       os << strValue;
1578       return;
1579     }
1580   }
1581 
1582   // Print special values in hexadecimal format. The sign bit should be included
1583   // in the literal.
1584   SmallVector<char, 16> str;
1585   APInt apInt = apValue.bitcastToAPInt();
1586   apInt.toString(str, /*Radix=*/16, /*Signed=*/false,
1587                  /*formatAsCLiteral=*/true);
1588   os << str;
1589 }
1590 
1591 void AsmPrinter::Impl::printLocation(LocationAttr loc, bool allowAlias) {
1592   if (printerFlags.shouldPrintDebugInfoPrettyForm())
1593     return printLocationInternal(loc, /*pretty=*/true);
1594 
1595   os << "loc(";
1596   if (!allowAlias || !state || failed(state->getAliasState().getAlias(loc, os)))
1597     printLocationInternal(loc);
1598   os << ')';
1599 }
1600 
1601 /// Returns true if the given dialect symbol data is simple enough to print in
1602 /// the pretty form, i.e. without the enclosing "".
1603 static bool isDialectSymbolSimpleEnoughForPrettyForm(StringRef symName) {
1604   // The name must start with an identifier.
1605   if (symName.empty() || !isalpha(symName.front()))
1606     return false;
1607 
1608   // Ignore all the characters that are valid in an identifier in the symbol
1609   // name.
1610   symName = symName.drop_while(
1611       [](char c) { return llvm::isAlnum(c) || c == '.' || c == '_'; });
1612   if (symName.empty())
1613     return true;
1614 
1615   // If we got to an unexpected character, then it must be a <>.  Check those
1616   // recursively.
1617   if (symName.front() != '<' || symName.back() != '>')
1618     return false;
1619 
1620   SmallVector<char, 8> nestedPunctuation;
1621   do {
1622     // If we ran out of characters, then we had a punctuation mismatch.
1623     if (symName.empty())
1624       return false;
1625 
1626     auto c = symName.front();
1627     symName = symName.drop_front();
1628 
1629     switch (c) {
1630     // We never allow null characters. This is an EOF indicator for the lexer
1631     // which we could handle, but isn't important for any known dialect.
1632     case '\0':
1633       return false;
1634     case '<':
1635     case '[':
1636     case '(':
1637     case '{':
1638       nestedPunctuation.push_back(c);
1639       continue;
1640     case '-':
1641       // Treat `->` as a special token.
1642       if (!symName.empty() && symName.front() == '>') {
1643         symName = symName.drop_front();
1644         continue;
1645       }
1646       break;
1647     // Reject types with mismatched brackets.
1648     case '>':
1649       if (nestedPunctuation.pop_back_val() != '<')
1650         return false;
1651       break;
1652     case ']':
1653       if (nestedPunctuation.pop_back_val() != '[')
1654         return false;
1655       break;
1656     case ')':
1657       if (nestedPunctuation.pop_back_val() != '(')
1658         return false;
1659       break;
1660     case '}':
1661       if (nestedPunctuation.pop_back_val() != '{')
1662         return false;
1663       break;
1664     default:
1665       continue;
1666     }
1667 
1668     // We're done when the punctuation is fully matched.
1669   } while (!nestedPunctuation.empty());
1670 
1671   // If there were extra characters, then we failed.
1672   return symName.empty();
1673 }
1674 
1675 /// Print the given dialect symbol to the stream.
1676 static void printDialectSymbol(raw_ostream &os, StringRef symPrefix,
1677                                StringRef dialectName, StringRef symString) {
1678   os << symPrefix << dialectName;
1679 
1680   // If this symbol name is simple enough, print it directly in pretty form,
1681   // otherwise, we print it as an escaped string.
1682   if (isDialectSymbolSimpleEnoughForPrettyForm(symString)) {
1683     os << '.' << symString;
1684     return;
1685   }
1686 
1687   os << "<\"";
1688   llvm::printEscapedString(symString, os);
1689   os << "\">";
1690 }
1691 
1692 /// Returns true if the given string can be represented as a bare identifier.
1693 static bool isBareIdentifier(StringRef name) {
1694   // By making this unsigned, the value passed in to isalnum will always be
1695   // in the range 0-255. This is important when building with MSVC because
1696   // its implementation will assert. This situation can arise when dealing
1697   // with UTF-8 multibyte characters.
1698   if (name.empty() || (!isalpha(name[0]) && name[0] != '_'))
1699     return false;
1700   return llvm::all_of(name.drop_front(), [](unsigned char c) {
1701     return isalnum(c) || c == '_' || c == '$' || c == '.';
1702   });
1703 }
1704 
1705 /// Print the given string as a keyword, or a quoted and escaped string if it
1706 /// has any special or non-printable characters in it.
1707 static void printKeywordOrString(StringRef keyword, raw_ostream &os) {
1708   // If it can be represented as a bare identifier, write it directly.
1709   if (isBareIdentifier(keyword)) {
1710     os << keyword;
1711     return;
1712   }
1713 
1714   // Otherwise, output the keyword wrapped in quotes with proper escaping.
1715   os << "\"";
1716   printEscapedString(keyword, os);
1717   os << '"';
1718 }
1719 
1720 /// Print the given string as a symbol reference. A symbol reference is
1721 /// represented as a string prefixed with '@'. The reference is surrounded with
1722 /// ""'s and escaped if it has any special or non-printable characters in it.
1723 static void printSymbolReference(StringRef symbolRef, raw_ostream &os) {
1724   assert(!symbolRef.empty() && "expected valid symbol reference");
1725   os << '@';
1726   printKeywordOrString(symbolRef, os);
1727 }
1728 
1729 // Print out a valid ElementsAttr that is succinct and can represent any
1730 // potential shape/type, for use when eliding a large ElementsAttr.
1731 //
1732 // We choose to use an opaque ElementsAttr literal with conspicuous content to
1733 // hopefully alert readers to the fact that this has been elided.
1734 //
1735 // Unfortunately, neither of the strings of an opaque ElementsAttr literal will
1736 // accept the string "elided". The first string must be a registered dialect
1737 // name and the latter must be a hex constant.
1738 static void printElidedElementsAttr(raw_ostream &os) {
1739   os << R"(opaque<"elided_large_const", "0xDEADBEEF">)";
1740 }
1741 
1742 LogicalResult AsmPrinter::Impl::printAlias(Attribute attr) {
1743   return success(state && succeeded(state->getAliasState().getAlias(attr, os)));
1744 }
1745 
1746 LogicalResult AsmPrinter::Impl::printAlias(Type type) {
1747   return success(state && succeeded(state->getAliasState().getAlias(type, os)));
1748 }
1749 
1750 void AsmPrinter::Impl::printAttribute(Attribute attr,
1751                                       AttrTypeElision typeElision) {
1752   if (!attr) {
1753     os << "<<NULL ATTRIBUTE>>";
1754     return;
1755   }
1756 
1757   // Try to print an alias for this attribute.
1758   if (succeeded(printAlias(attr)))
1759     return;
1760 
1761   if (!isa<BuiltinDialect>(attr.getDialect()))
1762     return printDialectAttribute(attr);
1763 
1764   auto attrType = attr.getType();
1765   if (auto opaqueAttr = attr.dyn_cast<OpaqueAttr>()) {
1766     printDialectSymbol(os, "#", opaqueAttr.getDialectNamespace(),
1767                        opaqueAttr.getAttrData());
1768   } else if (attr.isa<UnitAttr>()) {
1769     os << "unit";
1770     return;
1771   } else if (auto dictAttr = attr.dyn_cast<DictionaryAttr>()) {
1772     os << '{';
1773     interleaveComma(dictAttr.getValue(),
1774                     [&](NamedAttribute attr) { printNamedAttribute(attr); });
1775     os << '}';
1776 
1777   } else if (auto intAttr = attr.dyn_cast<IntegerAttr>()) {
1778     if (attrType.isSignlessInteger(1)) {
1779       os << (intAttr.getValue().getBoolValue() ? "true" : "false");
1780 
1781       // Boolean integer attributes always elides the type.
1782       return;
1783     }
1784 
1785     // Only print attributes as unsigned if they are explicitly unsigned or are
1786     // signless 1-bit values.  Indexes, signed values, and multi-bit signless
1787     // values print as signed.
1788     bool isUnsigned =
1789         attrType.isUnsignedInteger() || attrType.isSignlessInteger(1);
1790     intAttr.getValue().print(os, !isUnsigned);
1791 
1792     // IntegerAttr elides the type if I64.
1793     if (typeElision == AttrTypeElision::May && attrType.isSignlessInteger(64))
1794       return;
1795 
1796   } else if (auto floatAttr = attr.dyn_cast<FloatAttr>()) {
1797     printFloatValue(floatAttr.getValue(), os);
1798 
1799     // FloatAttr elides the type if F64.
1800     if (typeElision == AttrTypeElision::May && attrType.isF64())
1801       return;
1802 
1803   } else if (auto strAttr = attr.dyn_cast<StringAttr>()) {
1804     os << '"';
1805     printEscapedString(strAttr.getValue(), os);
1806     os << '"';
1807 
1808   } else if (auto arrayAttr = attr.dyn_cast<ArrayAttr>()) {
1809     os << '[';
1810     interleaveComma(arrayAttr.getValue(), [&](Attribute attr) {
1811       printAttribute(attr, AttrTypeElision::May);
1812     });
1813     os << ']';
1814 
1815   } else if (auto affineMapAttr = attr.dyn_cast<AffineMapAttr>()) {
1816     os << "affine_map<";
1817     affineMapAttr.getValue().print(os);
1818     os << '>';
1819 
1820     // AffineMap always elides the type.
1821     return;
1822 
1823   } else if (auto integerSetAttr = attr.dyn_cast<IntegerSetAttr>()) {
1824     os << "affine_set<";
1825     integerSetAttr.getValue().print(os);
1826     os << '>';
1827 
1828     // IntegerSet always elides the type.
1829     return;
1830 
1831   } else if (auto typeAttr = attr.dyn_cast<TypeAttr>()) {
1832     printType(typeAttr.getValue());
1833 
1834   } else if (auto refAttr = attr.dyn_cast<SymbolRefAttr>()) {
1835     printSymbolReference(refAttr.getRootReference().getValue(), os);
1836     for (FlatSymbolRefAttr nestedRef : refAttr.getNestedReferences()) {
1837       os << "::";
1838       printSymbolReference(nestedRef.getValue(), os);
1839     }
1840 
1841   } else if (auto opaqueAttr = attr.dyn_cast<OpaqueElementsAttr>()) {
1842     if (printerFlags.shouldElideElementsAttr(opaqueAttr)) {
1843       printElidedElementsAttr(os);
1844     } else {
1845       os << "opaque<" << opaqueAttr.getDialect() << ", \"0x"
1846          << llvm::toHex(opaqueAttr.getValue()) << "\">";
1847     }
1848 
1849   } else if (auto intOrFpEltAttr = attr.dyn_cast<DenseIntOrFPElementsAttr>()) {
1850     if (printerFlags.shouldElideElementsAttr(intOrFpEltAttr)) {
1851       printElidedElementsAttr(os);
1852     } else {
1853       os << "dense<";
1854       printDenseIntOrFPElementsAttr(intOrFpEltAttr, /*allowHex=*/true);
1855       os << '>';
1856     }
1857 
1858   } else if (auto strEltAttr = attr.dyn_cast<DenseStringElementsAttr>()) {
1859     if (printerFlags.shouldElideElementsAttr(strEltAttr)) {
1860       printElidedElementsAttr(os);
1861     } else {
1862       os << "dense<";
1863       printDenseStringElementsAttr(strEltAttr);
1864       os << '>';
1865     }
1866 
1867   } else if (auto sparseEltAttr = attr.dyn_cast<SparseElementsAttr>()) {
1868     if (printerFlags.shouldElideElementsAttr(sparseEltAttr.getIndices()) ||
1869         printerFlags.shouldElideElementsAttr(sparseEltAttr.getValues())) {
1870       printElidedElementsAttr(os);
1871     } else {
1872       os << "sparse<";
1873       DenseIntElementsAttr indices = sparseEltAttr.getIndices();
1874       if (indices.getNumElements() != 0) {
1875         printDenseIntOrFPElementsAttr(indices, /*allowHex=*/false);
1876         os << ", ";
1877         printDenseElementsAttr(sparseEltAttr.getValues(), /*allowHex=*/true);
1878       }
1879       os << '>';
1880     }
1881 
1882   } else if (auto locAttr = attr.dyn_cast<LocationAttr>()) {
1883     printLocation(locAttr);
1884   }
1885   // Don't print the type if we must elide it, or if it is a None type.
1886   if (typeElision != AttrTypeElision::Must && !attrType.isa<NoneType>()) {
1887     os << " : ";
1888     printType(attrType);
1889   }
1890 }
1891 
1892 /// Print the integer element of a DenseElementsAttr.
1893 static void printDenseIntElement(const APInt &value, raw_ostream &os,
1894                                  bool isSigned) {
1895   if (value.getBitWidth() == 1)
1896     os << (value.getBoolValue() ? "true" : "false");
1897   else
1898     value.print(os, isSigned);
1899 }
1900 
1901 static void
1902 printDenseElementsAttrImpl(bool isSplat, ShapedType type, raw_ostream &os,
1903                            function_ref<void(unsigned)> printEltFn) {
1904   // Special case for 0-d and splat tensors.
1905   if (isSplat)
1906     return printEltFn(0);
1907 
1908   // Special case for degenerate tensors.
1909   auto numElements = type.getNumElements();
1910   if (numElements == 0)
1911     return;
1912 
1913   // We use a mixed-radix counter to iterate through the shape. When we bump a
1914   // non-least-significant digit, we emit a close bracket. When we next emit an
1915   // element we re-open all closed brackets.
1916 
1917   // The mixed-radix counter, with radices in 'shape'.
1918   int64_t rank = type.getRank();
1919   SmallVector<unsigned, 4> counter(rank, 0);
1920   // The number of brackets that have been opened and not closed.
1921   unsigned openBrackets = 0;
1922 
1923   auto shape = type.getShape();
1924   auto bumpCounter = [&] {
1925     // Bump the least significant digit.
1926     ++counter[rank - 1];
1927     // Iterate backwards bubbling back the increment.
1928     for (unsigned i = rank - 1; i > 0; --i)
1929       if (counter[i] >= shape[i]) {
1930         // Index 'i' is rolled over. Bump (i-1) and close a bracket.
1931         counter[i] = 0;
1932         ++counter[i - 1];
1933         --openBrackets;
1934         os << ']';
1935       }
1936   };
1937 
1938   for (unsigned idx = 0, e = numElements; idx != e; ++idx) {
1939     if (idx != 0)
1940       os << ", ";
1941     while (openBrackets++ < rank)
1942       os << '[';
1943     openBrackets = rank;
1944     printEltFn(idx);
1945     bumpCounter();
1946   }
1947   while (openBrackets-- > 0)
1948     os << ']';
1949 }
1950 
1951 void AsmPrinter::Impl::printDenseElementsAttr(DenseElementsAttr attr,
1952                                               bool allowHex) {
1953   if (auto stringAttr = attr.dyn_cast<DenseStringElementsAttr>())
1954     return printDenseStringElementsAttr(stringAttr);
1955 
1956   printDenseIntOrFPElementsAttr(attr.cast<DenseIntOrFPElementsAttr>(),
1957                                 allowHex);
1958 }
1959 
1960 void AsmPrinter::Impl::printDenseIntOrFPElementsAttr(
1961     DenseIntOrFPElementsAttr attr, bool allowHex) {
1962   auto type = attr.getType();
1963   auto elementType = type.getElementType();
1964 
1965   // Check to see if we should format this attribute as a hex string.
1966   auto numElements = type.getNumElements();
1967   if (!attr.isSplat() && allowHex &&
1968       shouldPrintElementsAttrWithHex(numElements)) {
1969     ArrayRef<char> rawData = attr.getRawData();
1970     if (llvm::support::endian::system_endianness() ==
1971         llvm::support::endianness::big) {
1972       // Convert endianess in big-endian(BE) machines. `rawData` is BE in BE
1973       // machines. It is converted here to print in LE format.
1974       SmallVector<char, 64> outDataVec(rawData.size());
1975       MutableArrayRef<char> convRawData(outDataVec);
1976       DenseIntOrFPElementsAttr::convertEndianOfArrayRefForBEmachine(
1977           rawData, convRawData, type);
1978       os << '"' << "0x"
1979          << llvm::toHex(StringRef(convRawData.data(), convRawData.size()))
1980          << "\"";
1981     } else {
1982       os << '"' << "0x"
1983          << llvm::toHex(StringRef(rawData.data(), rawData.size())) << "\"";
1984     }
1985 
1986     return;
1987   }
1988 
1989   if (ComplexType complexTy = elementType.dyn_cast<ComplexType>()) {
1990     Type complexElementType = complexTy.getElementType();
1991     // Note: The if and else below had a common lambda function which invoked
1992     // printDenseElementsAttrImpl. This lambda was hitting a bug in gcc 9.1,9.2
1993     // and hence was replaced.
1994     if (complexElementType.isa<IntegerType>()) {
1995       bool isSigned = !complexElementType.isUnsignedInteger();
1996       auto valueIt = attr.value_begin<std::complex<APInt>>();
1997       printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
1998         auto complexValue = *(valueIt + index);
1999         os << "(";
2000         printDenseIntElement(complexValue.real(), os, isSigned);
2001         os << ",";
2002         printDenseIntElement(complexValue.imag(), os, isSigned);
2003         os << ")";
2004       });
2005     } else {
2006       auto valueIt = attr.value_begin<std::complex<APFloat>>();
2007       printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
2008         auto complexValue = *(valueIt + index);
2009         os << "(";
2010         printFloatValue(complexValue.real(), os);
2011         os << ",";
2012         printFloatValue(complexValue.imag(), os);
2013         os << ")";
2014       });
2015     }
2016   } else if (elementType.isIntOrIndex()) {
2017     bool isSigned = !elementType.isUnsignedInteger();
2018     auto valueIt = attr.value_begin<APInt>();
2019     printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
2020       printDenseIntElement(*(valueIt + index), os, isSigned);
2021     });
2022   } else {
2023     assert(elementType.isa<FloatType>() && "unexpected element type");
2024     auto valueIt = attr.value_begin<APFloat>();
2025     printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
2026       printFloatValue(*(valueIt + index), os);
2027     });
2028   }
2029 }
2030 
2031 void AsmPrinter::Impl::printDenseStringElementsAttr(
2032     DenseStringElementsAttr attr) {
2033   ArrayRef<StringRef> data = attr.getRawStringData();
2034   auto printFn = [&](unsigned index) {
2035     os << "\"";
2036     printEscapedString(data[index], os);
2037     os << "\"";
2038   };
2039   printDenseElementsAttrImpl(attr.isSplat(), attr.getType(), os, printFn);
2040 }
2041 
2042 void AsmPrinter::Impl::printType(Type type) {
2043   if (!type) {
2044     os << "<<NULL TYPE>>";
2045     return;
2046   }
2047 
2048   // Try to print an alias for this type.
2049   if (state && succeeded(state->getAliasState().getAlias(type, os)))
2050     return;
2051 
2052   TypeSwitch<Type>(type)
2053       .Case<OpaqueType>([&](OpaqueType opaqueTy) {
2054         printDialectSymbol(os, "!", opaqueTy.getDialectNamespace(),
2055                            opaqueTy.getTypeData());
2056       })
2057       .Case<IndexType>([&](Type) { os << "index"; })
2058       .Case<BFloat16Type>([&](Type) { os << "bf16"; })
2059       .Case<Float16Type>([&](Type) { os << "f16"; })
2060       .Case<Float32Type>([&](Type) { os << "f32"; })
2061       .Case<Float64Type>([&](Type) { os << "f64"; })
2062       .Case<Float80Type>([&](Type) { os << "f80"; })
2063       .Case<Float128Type>([&](Type) { os << "f128"; })
2064       .Case<IntegerType>([&](IntegerType integerTy) {
2065         if (integerTy.isSigned())
2066           os << 's';
2067         else if (integerTy.isUnsigned())
2068           os << 'u';
2069         os << 'i' << integerTy.getWidth();
2070       })
2071       .Case<FunctionType>([&](FunctionType funcTy) {
2072         os << '(';
2073         interleaveComma(funcTy.getInputs(), [&](Type ty) { printType(ty); });
2074         os << ") -> ";
2075         ArrayRef<Type> results = funcTy.getResults();
2076         if (results.size() == 1 && !results[0].isa<FunctionType>()) {
2077           printType(results[0]);
2078         } else {
2079           os << '(';
2080           interleaveComma(results, [&](Type ty) { printType(ty); });
2081           os << ')';
2082         }
2083       })
2084       .Case<VectorType>([&](VectorType vectorTy) {
2085         os << "vector<";
2086         auto vShape = vectorTy.getShape();
2087         unsigned lastDim = vShape.size();
2088         unsigned lastFixedDim = lastDim - vectorTy.getNumScalableDims();
2089         unsigned dimIdx = 0;
2090         for (dimIdx = 0; dimIdx < lastFixedDim; dimIdx++)
2091           os << vShape[dimIdx] << 'x';
2092         if (vectorTy.isScalable()) {
2093           os << '[';
2094           unsigned secondToLastDim = lastDim - 1;
2095           for (; dimIdx < secondToLastDim; dimIdx++)
2096             os << vShape[dimIdx] << 'x';
2097           os << vShape[dimIdx] << "]x";
2098         }
2099         printType(vectorTy.getElementType());
2100         os << '>';
2101       })
2102       .Case<RankedTensorType>([&](RankedTensorType tensorTy) {
2103         os << "tensor<";
2104         for (int64_t dim : tensorTy.getShape()) {
2105           if (ShapedType::isDynamic(dim))
2106             os << '?';
2107           else
2108             os << dim;
2109           os << 'x';
2110         }
2111         printType(tensorTy.getElementType());
2112         // Only print the encoding attribute value if set.
2113         if (tensorTy.getEncoding()) {
2114           os << ", ";
2115           printAttribute(tensorTy.getEncoding());
2116         }
2117         os << '>';
2118       })
2119       .Case<UnrankedTensorType>([&](UnrankedTensorType tensorTy) {
2120         os << "tensor<*x";
2121         printType(tensorTy.getElementType());
2122         os << '>';
2123       })
2124       .Case<MemRefType>([&](MemRefType memrefTy) {
2125         os << "memref<";
2126         for (int64_t dim : memrefTy.getShape()) {
2127           if (ShapedType::isDynamic(dim))
2128             os << '?';
2129           else
2130             os << dim;
2131           os << 'x';
2132         }
2133         printType(memrefTy.getElementType());
2134         if (!memrefTy.getLayout().isIdentity()) {
2135           os << ", ";
2136           printAttribute(memrefTy.getLayout(), AttrTypeElision::May);
2137         }
2138         // Only print the memory space if it is the non-default one.
2139         if (memrefTy.getMemorySpace()) {
2140           os << ", ";
2141           printAttribute(memrefTy.getMemorySpace(), AttrTypeElision::May);
2142         }
2143         os << '>';
2144       })
2145       .Case<UnrankedMemRefType>([&](UnrankedMemRefType memrefTy) {
2146         os << "memref<*x";
2147         printType(memrefTy.getElementType());
2148         // Only print the memory space if it is the non-default one.
2149         if (memrefTy.getMemorySpace()) {
2150           os << ", ";
2151           printAttribute(memrefTy.getMemorySpace(), AttrTypeElision::May);
2152         }
2153         os << '>';
2154       })
2155       .Case<ComplexType>([&](ComplexType complexTy) {
2156         os << "complex<";
2157         printType(complexTy.getElementType());
2158         os << '>';
2159       })
2160       .Case<TupleType>([&](TupleType tupleTy) {
2161         os << "tuple<";
2162         interleaveComma(tupleTy.getTypes(),
2163                         [&](Type type) { printType(type); });
2164         os << '>';
2165       })
2166       .Case<NoneType>([&](Type) { os << "none"; })
2167       .Default([&](Type type) { return printDialectType(type); });
2168 }
2169 
2170 void AsmPrinter::Impl::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
2171                                              ArrayRef<StringRef> elidedAttrs,
2172                                              bool withKeyword) {
2173   // If there are no attributes, then there is nothing to be done.
2174   if (attrs.empty())
2175     return;
2176 
2177   // Functor used to print a filtered attribute list.
2178   auto printFilteredAttributesFn = [&](auto filteredAttrs) {
2179     // Print the 'attributes' keyword if necessary.
2180     if (withKeyword)
2181       os << " attributes";
2182 
2183     // Otherwise, print them all out in braces.
2184     os << " {";
2185     interleaveComma(filteredAttrs,
2186                     [&](NamedAttribute attr) { printNamedAttribute(attr); });
2187     os << '}';
2188   };
2189 
2190   // If no attributes are elided, we can directly print with no filtering.
2191   if (elidedAttrs.empty())
2192     return printFilteredAttributesFn(attrs);
2193 
2194   // Otherwise, filter out any attributes that shouldn't be included.
2195   llvm::SmallDenseSet<StringRef> elidedAttrsSet(elidedAttrs.begin(),
2196                                                 elidedAttrs.end());
2197   auto filteredAttrs = llvm::make_filter_range(attrs, [&](NamedAttribute attr) {
2198     return !elidedAttrsSet.contains(attr.getName().strref());
2199   });
2200   if (!filteredAttrs.empty())
2201     printFilteredAttributesFn(filteredAttrs);
2202 }
2203 
2204 void AsmPrinter::Impl::printNamedAttribute(NamedAttribute attr) {
2205   // Print the name without quotes if possible.
2206   ::printKeywordOrString(attr.getName().strref(), os);
2207 
2208   // Pretty printing elides the attribute value for unit attributes.
2209   if (attr.getValue().isa<UnitAttr>())
2210     return;
2211 
2212   os << " = ";
2213   printAttribute(attr.getValue());
2214 }
2215 
2216 void AsmPrinter::Impl::printDialectAttribute(Attribute attr) {
2217   auto &dialect = attr.getDialect();
2218 
2219   // Ask the dialect to serialize the attribute to a string.
2220   std::string attrName;
2221   {
2222     llvm::raw_string_ostream attrNameStr(attrName);
2223     Impl subPrinter(attrNameStr, printerFlags, state);
2224     DialectAsmPrinter printer(subPrinter);
2225     dialect.printAttribute(attr, printer);
2226   }
2227   printDialectSymbol(os, "#", dialect.getNamespace(), attrName);
2228 }
2229 
2230 void AsmPrinter::Impl::printDialectType(Type type) {
2231   auto &dialect = type.getDialect();
2232 
2233   // Ask the dialect to serialize the type to a string.
2234   std::string typeName;
2235   {
2236     llvm::raw_string_ostream typeNameStr(typeName);
2237     Impl subPrinter(typeNameStr, printerFlags, state);
2238     DialectAsmPrinter printer(subPrinter);
2239     dialect.printType(type, printer);
2240   }
2241   printDialectSymbol(os, "!", dialect.getNamespace(), typeName);
2242 }
2243 
2244 //===--------------------------------------------------------------------===//
2245 // AsmPrinter
2246 //===--------------------------------------------------------------------===//
2247 
2248 AsmPrinter::~AsmPrinter() = default;
2249 
2250 raw_ostream &AsmPrinter::getStream() const {
2251   assert(impl && "expected AsmPrinter::getStream to be overriden");
2252   return impl->getStream();
2253 }
2254 
2255 /// Print the given floating point value in a stablized form.
2256 void AsmPrinter::printFloat(const APFloat &value) {
2257   assert(impl && "expected AsmPrinter::printFloat to be overriden");
2258   printFloatValue(value, impl->getStream());
2259 }
2260 
2261 void AsmPrinter::printType(Type type) {
2262   assert(impl && "expected AsmPrinter::printType to be overriden");
2263   impl->printType(type);
2264 }
2265 
2266 void AsmPrinter::printAttribute(Attribute attr) {
2267   assert(impl && "expected AsmPrinter::printAttribute to be overriden");
2268   impl->printAttribute(attr);
2269 }
2270 
2271 LogicalResult AsmPrinter::printAlias(Attribute attr) {
2272   assert(impl && "expected AsmPrinter::printAlias to be overriden");
2273   return impl->printAlias(attr);
2274 }
2275 
2276 LogicalResult AsmPrinter::printAlias(Type type) {
2277   assert(impl && "expected AsmPrinter::printAlias to be overriden");
2278   return impl->printAlias(type);
2279 }
2280 
2281 void AsmPrinter::printAttributeWithoutType(Attribute attr) {
2282   assert(impl &&
2283          "expected AsmPrinter::printAttributeWithoutType to be overriden");
2284   impl->printAttribute(attr, Impl::AttrTypeElision::Must);
2285 }
2286 
2287 void AsmPrinter::printKeywordOrString(StringRef keyword) {
2288   assert(impl && "expected AsmPrinter::printKeywordOrString to be overriden");
2289   ::printKeywordOrString(keyword, impl->getStream());
2290 }
2291 
2292 void AsmPrinter::printSymbolName(StringRef symbolRef) {
2293   assert(impl && "expected AsmPrinter::printSymbolName to be overriden");
2294   ::printSymbolReference(symbolRef, impl->getStream());
2295 }
2296 
2297 //===----------------------------------------------------------------------===//
2298 // Affine expressions and maps
2299 //===----------------------------------------------------------------------===//
2300 
2301 void AsmPrinter::Impl::printAffineExpr(
2302     AffineExpr expr, function_ref<void(unsigned, bool)> printValueName) {
2303   printAffineExprInternal(expr, BindingStrength::Weak, printValueName);
2304 }
2305 
2306 void AsmPrinter::Impl::printAffineExprInternal(
2307     AffineExpr expr, BindingStrength enclosingTightness,
2308     function_ref<void(unsigned, bool)> printValueName) {
2309   const char *binopSpelling = nullptr;
2310   switch (expr.getKind()) {
2311   case AffineExprKind::SymbolId: {
2312     unsigned pos = expr.cast<AffineSymbolExpr>().getPosition();
2313     if (printValueName)
2314       printValueName(pos, /*isSymbol=*/true);
2315     else
2316       os << 's' << pos;
2317     return;
2318   }
2319   case AffineExprKind::DimId: {
2320     unsigned pos = expr.cast<AffineDimExpr>().getPosition();
2321     if (printValueName)
2322       printValueName(pos, /*isSymbol=*/false);
2323     else
2324       os << 'd' << pos;
2325     return;
2326   }
2327   case AffineExprKind::Constant:
2328     os << expr.cast<AffineConstantExpr>().getValue();
2329     return;
2330   case AffineExprKind::Add:
2331     binopSpelling = " + ";
2332     break;
2333   case AffineExprKind::Mul:
2334     binopSpelling = " * ";
2335     break;
2336   case AffineExprKind::FloorDiv:
2337     binopSpelling = " floordiv ";
2338     break;
2339   case AffineExprKind::CeilDiv:
2340     binopSpelling = " ceildiv ";
2341     break;
2342   case AffineExprKind::Mod:
2343     binopSpelling = " mod ";
2344     break;
2345   }
2346 
2347   auto binOp = expr.cast<AffineBinaryOpExpr>();
2348   AffineExpr lhsExpr = binOp.getLHS();
2349   AffineExpr rhsExpr = binOp.getRHS();
2350 
2351   // Handle tightly binding binary operators.
2352   if (binOp.getKind() != AffineExprKind::Add) {
2353     if (enclosingTightness == BindingStrength::Strong)
2354       os << '(';
2355 
2356     // Pretty print multiplication with -1.
2357     auto rhsConst = rhsExpr.dyn_cast<AffineConstantExpr>();
2358     if (rhsConst && binOp.getKind() == AffineExprKind::Mul &&
2359         rhsConst.getValue() == -1) {
2360       os << "-";
2361       printAffineExprInternal(lhsExpr, BindingStrength::Strong, printValueName);
2362       if (enclosingTightness == BindingStrength::Strong)
2363         os << ')';
2364       return;
2365     }
2366 
2367     printAffineExprInternal(lhsExpr, BindingStrength::Strong, printValueName);
2368 
2369     os << binopSpelling;
2370     printAffineExprInternal(rhsExpr, BindingStrength::Strong, printValueName);
2371 
2372     if (enclosingTightness == BindingStrength::Strong)
2373       os << ')';
2374     return;
2375   }
2376 
2377   // Print out special "pretty" forms for add.
2378   if (enclosingTightness == BindingStrength::Strong)
2379     os << '(';
2380 
2381   // Pretty print addition to a product that has a negative operand as a
2382   // subtraction.
2383   if (auto rhs = rhsExpr.dyn_cast<AffineBinaryOpExpr>()) {
2384     if (rhs.getKind() == AffineExprKind::Mul) {
2385       AffineExpr rrhsExpr = rhs.getRHS();
2386       if (auto rrhs = rrhsExpr.dyn_cast<AffineConstantExpr>()) {
2387         if (rrhs.getValue() == -1) {
2388           printAffineExprInternal(lhsExpr, BindingStrength::Weak,
2389                                   printValueName);
2390           os << " - ";
2391           if (rhs.getLHS().getKind() == AffineExprKind::Add) {
2392             printAffineExprInternal(rhs.getLHS(), BindingStrength::Strong,
2393                                     printValueName);
2394           } else {
2395             printAffineExprInternal(rhs.getLHS(), BindingStrength::Weak,
2396                                     printValueName);
2397           }
2398 
2399           if (enclosingTightness == BindingStrength::Strong)
2400             os << ')';
2401           return;
2402         }
2403 
2404         if (rrhs.getValue() < -1) {
2405           printAffineExprInternal(lhsExpr, BindingStrength::Weak,
2406                                   printValueName);
2407           os << " - ";
2408           printAffineExprInternal(rhs.getLHS(), BindingStrength::Strong,
2409                                   printValueName);
2410           os << " * " << -rrhs.getValue();
2411           if (enclosingTightness == BindingStrength::Strong)
2412             os << ')';
2413           return;
2414         }
2415       }
2416     }
2417   }
2418 
2419   // Pretty print addition to a negative number as a subtraction.
2420   if (auto rhsConst = rhsExpr.dyn_cast<AffineConstantExpr>()) {
2421     if (rhsConst.getValue() < 0) {
2422       printAffineExprInternal(lhsExpr, BindingStrength::Weak, printValueName);
2423       os << " - " << -rhsConst.getValue();
2424       if (enclosingTightness == BindingStrength::Strong)
2425         os << ')';
2426       return;
2427     }
2428   }
2429 
2430   printAffineExprInternal(lhsExpr, BindingStrength::Weak, printValueName);
2431 
2432   os << " + ";
2433   printAffineExprInternal(rhsExpr, BindingStrength::Weak, printValueName);
2434 
2435   if (enclosingTightness == BindingStrength::Strong)
2436     os << ')';
2437 }
2438 
2439 void AsmPrinter::Impl::printAffineConstraint(AffineExpr expr, bool isEq) {
2440   printAffineExprInternal(expr, BindingStrength::Weak);
2441   isEq ? os << " == 0" : os << " >= 0";
2442 }
2443 
2444 void AsmPrinter::Impl::printAffineMap(AffineMap map) {
2445   // Dimension identifiers.
2446   os << '(';
2447   for (int i = 0; i < (int)map.getNumDims() - 1; ++i)
2448     os << 'd' << i << ", ";
2449   if (map.getNumDims() >= 1)
2450     os << 'd' << map.getNumDims() - 1;
2451   os << ')';
2452 
2453   // Symbolic identifiers.
2454   if (map.getNumSymbols() != 0) {
2455     os << '[';
2456     for (unsigned i = 0; i < map.getNumSymbols() - 1; ++i)
2457       os << 's' << i << ", ";
2458     if (map.getNumSymbols() >= 1)
2459       os << 's' << map.getNumSymbols() - 1;
2460     os << ']';
2461   }
2462 
2463   // Result affine expressions.
2464   os << " -> (";
2465   interleaveComma(map.getResults(),
2466                   [&](AffineExpr expr) { printAffineExpr(expr); });
2467   os << ')';
2468 }
2469 
2470 void AsmPrinter::Impl::printIntegerSet(IntegerSet set) {
2471   // Dimension identifiers.
2472   os << '(';
2473   for (unsigned i = 1; i < set.getNumDims(); ++i)
2474     os << 'd' << i - 1 << ", ";
2475   if (set.getNumDims() >= 1)
2476     os << 'd' << set.getNumDims() - 1;
2477   os << ')';
2478 
2479   // Symbolic identifiers.
2480   if (set.getNumSymbols() != 0) {
2481     os << '[';
2482     for (unsigned i = 0; i < set.getNumSymbols() - 1; ++i)
2483       os << 's' << i << ", ";
2484     if (set.getNumSymbols() >= 1)
2485       os << 's' << set.getNumSymbols() - 1;
2486     os << ']';
2487   }
2488 
2489   // Print constraints.
2490   os << " : (";
2491   int numConstraints = set.getNumConstraints();
2492   for (int i = 1; i < numConstraints; ++i) {
2493     printAffineConstraint(set.getConstraint(i - 1), set.isEq(i - 1));
2494     os << ", ";
2495   }
2496   if (numConstraints >= 1)
2497     printAffineConstraint(set.getConstraint(numConstraints - 1),
2498                           set.isEq(numConstraints - 1));
2499   os << ')';
2500 }
2501 
2502 //===----------------------------------------------------------------------===//
2503 // OperationPrinter
2504 //===----------------------------------------------------------------------===//
2505 
2506 namespace {
2507 /// This class contains the logic for printing operations, regions, and blocks.
2508 class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
2509 public:
2510   using Impl = AsmPrinter::Impl;
2511   using Impl::printType;
2512 
2513   explicit OperationPrinter(raw_ostream &os, AsmStateImpl &state)
2514       : Impl(os, state.getPrinterFlags(), &state),
2515         OpAsmPrinter(static_cast<Impl &>(*this)) {}
2516 
2517   /// Print the given top-level operation.
2518   void printTopLevelOperation(Operation *op);
2519 
2520   /// Print the given operation with its indent and location.
2521   void print(Operation *op);
2522   /// Print the bare location, not including indentation/location/etc.
2523   void printOperation(Operation *op);
2524   /// Print the given operation in the generic form.
2525   void printGenericOp(Operation *op, bool printOpName) override;
2526 
2527   /// Print the name of the given block.
2528   void printBlockName(Block *block);
2529 
2530   /// Print the given block. If 'printBlockArgs' is false, the arguments of the
2531   /// block are not printed. If 'printBlockTerminator' is false, the terminator
2532   /// operation of the block is not printed.
2533   void print(Block *block, bool printBlockArgs = true,
2534              bool printBlockTerminator = true);
2535 
2536   /// Print the ID of the given value, optionally with its result number.
2537   void printValueID(Value value, bool printResultNo = true,
2538                     raw_ostream *streamOverride = nullptr) const;
2539 
2540   /// Print the ID of the given operation.
2541   void printOperationID(Operation *op,
2542                         raw_ostream *streamOverride = nullptr) const;
2543 
2544   //===--------------------------------------------------------------------===//
2545   // OpAsmPrinter methods
2546   //===--------------------------------------------------------------------===//
2547 
2548   /// Print a newline and indent the printer to the start of the current
2549   /// operation.
2550   void printNewline() override {
2551     os << newLine;
2552     os.indent(currentIndent);
2553   }
2554 
2555   /// Print a block argument in the usual format of:
2556   ///   %ssaName : type {attr1=42} loc("here")
2557   /// where location printing is controlled by the standard internal option.
2558   /// You may pass omitType=true to not print a type, and pass an empty
2559   /// attribute list if you don't care for attributes.
2560   void printRegionArgument(BlockArgument arg,
2561                            ArrayRef<NamedAttribute> argAttrs = {},
2562                            bool omitType = false) override;
2563 
2564   /// Print the ID for the given value.
2565   void printOperand(Value value) override { printValueID(value); }
2566   void printOperand(Value value, raw_ostream &os) override {
2567     printValueID(value, /*printResultNo=*/true, &os);
2568   }
2569 
2570   /// Print an optional attribute dictionary with a given set of elided values.
2571   void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
2572                              ArrayRef<StringRef> elidedAttrs = {}) override {
2573     Impl::printOptionalAttrDict(attrs, elidedAttrs);
2574   }
2575   void printOptionalAttrDictWithKeyword(
2576       ArrayRef<NamedAttribute> attrs,
2577       ArrayRef<StringRef> elidedAttrs = {}) override {
2578     Impl::printOptionalAttrDict(attrs, elidedAttrs,
2579                                 /*withKeyword=*/true);
2580   }
2581 
2582   /// Print the given successor.
2583   void printSuccessor(Block *successor) override;
2584 
2585   /// Print an operation successor with the operands used for the block
2586   /// arguments.
2587   void printSuccessorAndUseList(Block *successor,
2588                                 ValueRange succOperands) override;
2589 
2590   /// Print the given region.
2591   void printRegion(Region &region, bool printEntryBlockArgs,
2592                    bool printBlockTerminators, bool printEmptyBlock) override;
2593 
2594   /// Renumber the arguments for the specified region to the same names as the
2595   /// SSA values in namesToUse. This may only be used for IsolatedFromAbove
2596   /// operations. If any entry in namesToUse is null, the corresponding
2597   /// argument name is left alone.
2598   void shadowRegionArgs(Region &region, ValueRange namesToUse) override {
2599     state->getSSANameState().shadowRegionArgs(region, namesToUse);
2600   }
2601 
2602   /// Print the given affine map with the symbol and dimension operands printed
2603   /// inline with the map.
2604   void printAffineMapOfSSAIds(AffineMapAttr mapAttr,
2605                               ValueRange operands) override;
2606 
2607   /// Print the given affine expression with the symbol and dimension operands
2608   /// printed inline with the expression.
2609   void printAffineExprOfSSAIds(AffineExpr expr, ValueRange dimOperands,
2610                                ValueRange symOperands) override;
2611 
2612   /// Print users of this operation or id of this operation if it has no result.
2613   void printUsersComment(Operation *op);
2614 
2615   /// Print users of this block arg.
2616   void printUsersComment(BlockArgument arg);
2617 
2618   /// Print the users of a value.
2619   void printValueUsers(Value value);
2620 
2621   /// Print either the ids of the result values or the id of the operation if
2622   /// the operation has no results.
2623   void printUserIDs(Operation *user, bool prefixComma = false);
2624 
2625 private:
2626   // Contains the stack of default dialects to use when printing regions.
2627   // A new dialect is pushed to the stack before parsing regions nested under an
2628   // operation implementing `OpAsmOpInterface`, and popped when done. At the
2629   // top-level we start with "builtin" as the default, so that the top-level
2630   // `module` operation prints as-is.
2631   SmallVector<StringRef> defaultDialectStack{"builtin"};
2632 
2633   /// The number of spaces used for indenting nested operations.
2634   const static unsigned indentWidth = 2;
2635 
2636   // This is the current indentation level for nested structures.
2637   unsigned currentIndent = 0;
2638 };
2639 } // namespace
2640 
2641 void OperationPrinter::printTopLevelOperation(Operation *op) {
2642   // Output the aliases at the top level that can't be deferred.
2643   state->getAliasState().printNonDeferredAliases(os, newLine);
2644 
2645   // Print the module.
2646   print(op);
2647   os << newLine;
2648 
2649   // Output the aliases at the top level that can be deferred.
2650   state->getAliasState().printDeferredAliases(os, newLine);
2651 }
2652 
2653 /// Print a block argument in the usual format of:
2654 ///   %ssaName : type {attr1=42} loc("here")
2655 /// where location printing is controlled by the standard internal option.
2656 /// You may pass omitType=true to not print a type, and pass an empty
2657 /// attribute list if you don't care for attributes.
2658 void OperationPrinter::printRegionArgument(BlockArgument arg,
2659                                            ArrayRef<NamedAttribute> argAttrs,
2660                                            bool omitType) {
2661   printOperand(arg);
2662   if (!omitType) {
2663     os << ": ";
2664     printType(arg.getType());
2665   }
2666   printOptionalAttrDict(argAttrs);
2667   // TODO: We should allow location aliases on block arguments.
2668   printTrailingLocation(arg.getLoc(), /*allowAlias*/ false);
2669 }
2670 
2671 void OperationPrinter::print(Operation *op) {
2672   // Track the location of this operation.
2673   state->registerOperationLocation(op, newLine.curLine, currentIndent);
2674 
2675   os.indent(currentIndent);
2676   printOperation(op);
2677   printTrailingLocation(op->getLoc());
2678   if (printerFlags.shouldPrintValueUsers())
2679     printUsersComment(op);
2680 }
2681 
2682 void OperationPrinter::printOperation(Operation *op) {
2683   if (size_t numResults = op->getNumResults()) {
2684     auto printResultGroup = [&](size_t resultNo, size_t resultCount) {
2685       printValueID(op->getResult(resultNo), /*printResultNo=*/false);
2686       if (resultCount > 1)
2687         os << ':' << resultCount;
2688     };
2689 
2690     // Check to see if this operation has multiple result groups.
2691     ArrayRef<int> resultGroups = state->getSSANameState().getOpResultGroups(op);
2692     if (!resultGroups.empty()) {
2693       // Interleave the groups excluding the last one, this one will be handled
2694       // separately.
2695       interleaveComma(llvm::seq<int>(0, resultGroups.size() - 1), [&](int i) {
2696         printResultGroup(resultGroups[i],
2697                          resultGroups[i + 1] - resultGroups[i]);
2698       });
2699       os << ", ";
2700       printResultGroup(resultGroups.back(), numResults - resultGroups.back());
2701 
2702     } else {
2703       printResultGroup(/*resultNo=*/0, /*resultCount=*/numResults);
2704     }
2705 
2706     os << " = ";
2707   }
2708 
2709   // If requested, always print the generic form.
2710   if (!printerFlags.shouldPrintGenericOpForm()) {
2711     // Check to see if this is a known operation. If so, use the registered
2712     // custom printer hook.
2713     if (auto opInfo = op->getRegisteredInfo()) {
2714       opInfo->printAssembly(op, *this, defaultDialectStack.back());
2715       return;
2716     }
2717     // Otherwise try to dispatch to the dialect, if available.
2718     if (Dialect *dialect = op->getDialect()) {
2719       if (auto opPrinter = dialect->getOperationPrinter(op)) {
2720         // Print the op name first.
2721         StringRef name = op->getName().getStringRef();
2722         name.consume_front((defaultDialectStack.back() + ".").str());
2723         printEscapedString(name, os);
2724         // Print the rest of the op now.
2725         opPrinter(op, *this);
2726         return;
2727       }
2728     }
2729   }
2730 
2731   // Otherwise print with the generic assembly form.
2732   printGenericOp(op, /*printOpName=*/true);
2733 }
2734 
2735 void OperationPrinter::printUsersComment(Operation *op) {
2736   unsigned numResults = op->getNumResults();
2737   if (!numResults && op->getNumOperands()) {
2738     os << " // id: ";
2739     printOperationID(op);
2740   } else if (numResults && op->use_empty()) {
2741     os << " // unused";
2742   } else if (numResults && !op->use_empty()) {
2743     // Print "user" if the operation has one result used to compute one other
2744     // result, or is used in one operation with no result.
2745     unsigned usedInNResults = 0;
2746     unsigned usedInNOperations = 0;
2747     SmallPtrSet<Operation *, 1> userSet;
2748     for (Operation *user : op->getUsers()) {
2749       if (userSet.insert(user).second) {
2750         ++usedInNOperations;
2751         usedInNResults += user->getNumResults();
2752       }
2753     }
2754 
2755     // We already know that users is not empty.
2756     bool exactlyOneUniqueUse =
2757         usedInNResults <= 1 && usedInNOperations <= 1 && numResults == 1;
2758     os << " // " << (exactlyOneUniqueUse ? "user" : "users") << ": ";
2759     bool shouldPrintBrackets = numResults > 1;
2760     auto printOpResult = [&](OpResult opResult) {
2761       if (shouldPrintBrackets)
2762         os << "(";
2763       printValueUsers(opResult);
2764       if (shouldPrintBrackets)
2765         os << ")";
2766     };
2767 
2768     interleaveComma(op->getResults(), printOpResult);
2769   }
2770 }
2771 
2772 void OperationPrinter::printUsersComment(BlockArgument arg) {
2773   os << "// ";
2774   printValueID(arg);
2775   if (arg.use_empty()) {
2776     os << " is unused";
2777   } else {
2778     os << " is used by ";
2779     printValueUsers(arg);
2780   }
2781   os << newLine;
2782 }
2783 
2784 void OperationPrinter::printValueUsers(Value value) {
2785   if (value.use_empty())
2786     os << "unused";
2787 
2788   // One value might be used as the operand of an operation more than once.
2789   // Only print the operations results once in that case.
2790   SmallPtrSet<Operation *, 1> userSet;
2791   for (auto &indexedUser : enumerate(value.getUsers())) {
2792     if (userSet.insert(indexedUser.value()).second)
2793       printUserIDs(indexedUser.value(), indexedUser.index());
2794   }
2795 }
2796 
2797 void OperationPrinter::printUserIDs(Operation *user, bool prefixComma) {
2798   if (prefixComma)
2799     os << ", ";
2800 
2801   if (!user->getNumResults()) {
2802     printOperationID(user);
2803   } else {
2804     interleaveComma(user->getResults(),
2805                     [this](Value result) { printValueID(result); });
2806   }
2807 }
2808 
2809 void OperationPrinter::printGenericOp(Operation *op, bool printOpName) {
2810   if (printOpName) {
2811     os << '"';
2812     printEscapedString(op->getName().getStringRef(), os);
2813     os << '"';
2814   }
2815   os << '(';
2816   interleaveComma(op->getOperands(), [&](Value value) { printValueID(value); });
2817   os << ')';
2818 
2819   // For terminators, print the list of successors and their operands.
2820   if (op->getNumSuccessors() != 0) {
2821     os << '[';
2822     interleaveComma(op->getSuccessors(),
2823                     [&](Block *successor) { printBlockName(successor); });
2824     os << ']';
2825   }
2826 
2827   // Print regions.
2828   if (op->getNumRegions() != 0) {
2829     os << " (";
2830     interleaveComma(op->getRegions(), [&](Region &region) {
2831       printRegion(region, /*printEntryBlockArgs=*/true,
2832                   /*printBlockTerminators=*/true, /*printEmptyBlock=*/true);
2833     });
2834     os << ')';
2835   }
2836 
2837   auto attrs = op->getAttrs();
2838   printOptionalAttrDict(attrs);
2839 
2840   // Print the type signature of the operation.
2841   os << " : ";
2842   printFunctionalType(op);
2843 }
2844 
2845 void OperationPrinter::printBlockName(Block *block) {
2846   os << state->getSSANameState().getBlockInfo(block).name;
2847 }
2848 
2849 void OperationPrinter::print(Block *block, bool printBlockArgs,
2850                              bool printBlockTerminator) {
2851   // Print the block label and argument list if requested.
2852   if (printBlockArgs) {
2853     os.indent(currentIndent);
2854     printBlockName(block);
2855 
2856     // Print the argument list if non-empty.
2857     if (!block->args_empty()) {
2858       os << '(';
2859       interleaveComma(block->getArguments(), [&](BlockArgument arg) {
2860         printValueID(arg);
2861         os << ": ";
2862         printType(arg.getType());
2863         // TODO: We should allow location aliases on block arguments.
2864         printTrailingLocation(arg.getLoc(), /*allowAlias*/ false);
2865       });
2866       os << ')';
2867     }
2868     os << ':';
2869 
2870     // Print out some context information about the predecessors of this block.
2871     if (!block->getParent()) {
2872       os << "  // block is not in a region!";
2873     } else if (block->hasNoPredecessors()) {
2874       if (!block->isEntryBlock())
2875         os << "  // no predecessors";
2876     } else if (auto *pred = block->getSinglePredecessor()) {
2877       os << "  // pred: ";
2878       printBlockName(pred);
2879     } else {
2880       // We want to print the predecessors in a stable order, not in
2881       // whatever order the use-list is in, so gather and sort them.
2882       SmallVector<BlockInfo, 4> predIDs;
2883       for (auto *pred : block->getPredecessors())
2884         predIDs.push_back(state->getSSANameState().getBlockInfo(pred));
2885       llvm::sort(predIDs, [](BlockInfo lhs, BlockInfo rhs) {
2886         return lhs.ordering < rhs.ordering;
2887       });
2888 
2889       os << "  // " << predIDs.size() << " preds: ";
2890 
2891       interleaveComma(predIDs, [&](BlockInfo pred) { os << pred.name; });
2892     }
2893     os << newLine;
2894   }
2895 
2896   currentIndent += indentWidth;
2897 
2898   if (printerFlags.shouldPrintValueUsers()) {
2899     for (BlockArgument arg : block->getArguments()) {
2900       os.indent(currentIndent);
2901       printUsersComment(arg);
2902     }
2903   }
2904 
2905   bool hasTerminator =
2906       !block->empty() && block->back().hasTrait<OpTrait::IsTerminator>();
2907   auto range = llvm::make_range(
2908       block->begin(),
2909       std::prev(block->end(),
2910                 (!hasTerminator || printBlockTerminator) ? 0 : 1));
2911   for (auto &op : range) {
2912     print(&op);
2913     os << newLine;
2914   }
2915   currentIndent -= indentWidth;
2916 }
2917 
2918 void OperationPrinter::printValueID(Value value, bool printResultNo,
2919                                     raw_ostream *streamOverride) const {
2920   state->getSSANameState().printValueID(value, printResultNo,
2921                                         streamOverride ? *streamOverride : os);
2922 }
2923 
2924 void OperationPrinter::printOperationID(Operation *op,
2925                                         raw_ostream *streamOverride) const {
2926   state->getSSANameState().printOperationID(op, streamOverride ? *streamOverride
2927                                                                : os);
2928 }
2929 
2930 void OperationPrinter::printSuccessor(Block *successor) {
2931   printBlockName(successor);
2932 }
2933 
2934 void OperationPrinter::printSuccessorAndUseList(Block *successor,
2935                                                 ValueRange succOperands) {
2936   printBlockName(successor);
2937   if (succOperands.empty())
2938     return;
2939 
2940   os << '(';
2941   interleaveComma(succOperands,
2942                   [this](Value operand) { printValueID(operand); });
2943   os << " : ";
2944   interleaveComma(succOperands,
2945                   [this](Value operand) { printType(operand.getType()); });
2946   os << ')';
2947 }
2948 
2949 void OperationPrinter::printRegion(Region &region, bool printEntryBlockArgs,
2950                                    bool printBlockTerminators,
2951                                    bool printEmptyBlock) {
2952   os << "{" << newLine;
2953   if (!region.empty()) {
2954     auto restoreDefaultDialect =
2955         llvm::make_scope_exit([&]() { defaultDialectStack.pop_back(); });
2956     if (auto iface = dyn_cast<OpAsmOpInterface>(region.getParentOp()))
2957       defaultDialectStack.push_back(iface.getDefaultDialect());
2958     else
2959       defaultDialectStack.push_back("");
2960 
2961     auto *entryBlock = &region.front();
2962     // Force printing the block header if printEmptyBlock is set and the block
2963     // is empty or if printEntryBlockArgs is set and there are arguments to
2964     // print.
2965     bool shouldAlwaysPrintBlockHeader =
2966         (printEmptyBlock && entryBlock->empty()) ||
2967         (printEntryBlockArgs && entryBlock->getNumArguments() != 0);
2968     print(entryBlock, shouldAlwaysPrintBlockHeader, printBlockTerminators);
2969     for (auto &b : llvm::drop_begin(region.getBlocks(), 1))
2970       print(&b);
2971   }
2972   os.indent(currentIndent) << "}";
2973 }
2974 
2975 void OperationPrinter::printAffineMapOfSSAIds(AffineMapAttr mapAttr,
2976                                               ValueRange operands) {
2977   AffineMap map = mapAttr.getValue();
2978   unsigned numDims = map.getNumDims();
2979   auto printValueName = [&](unsigned pos, bool isSymbol) {
2980     unsigned index = isSymbol ? numDims + pos : pos;
2981     assert(index < operands.size());
2982     if (isSymbol)
2983       os << "symbol(";
2984     printValueID(operands[index]);
2985     if (isSymbol)
2986       os << ')';
2987   };
2988 
2989   interleaveComma(map.getResults(), [&](AffineExpr expr) {
2990     printAffineExpr(expr, printValueName);
2991   });
2992 }
2993 
2994 void OperationPrinter::printAffineExprOfSSAIds(AffineExpr expr,
2995                                                ValueRange dimOperands,
2996                                                ValueRange symOperands) {
2997   auto printValueName = [&](unsigned pos, bool isSymbol) {
2998     if (!isSymbol)
2999       return printValueID(dimOperands[pos]);
3000     os << "symbol(";
3001     printValueID(symOperands[pos]);
3002     os << ')';
3003   };
3004   printAffineExpr(expr, printValueName);
3005 }
3006 
3007 //===----------------------------------------------------------------------===//
3008 // print and dump methods
3009 //===----------------------------------------------------------------------===//
3010 
3011 void Attribute::print(raw_ostream &os) const {
3012   AsmPrinter::Impl(os).printAttribute(*this);
3013 }
3014 
3015 void Attribute::dump() const {
3016   print(llvm::errs());
3017   llvm::errs() << "\n";
3018 }
3019 
3020 void Type::print(raw_ostream &os) const {
3021   AsmPrinter::Impl(os).printType(*this);
3022 }
3023 
3024 void Type::dump() const { print(llvm::errs()); }
3025 
3026 void AffineMap::dump() const {
3027   print(llvm::errs());
3028   llvm::errs() << "\n";
3029 }
3030 
3031 void IntegerSet::dump() const {
3032   print(llvm::errs());
3033   llvm::errs() << "\n";
3034 }
3035 
3036 void AffineExpr::print(raw_ostream &os) const {
3037   if (!expr) {
3038     os << "<<NULL AFFINE EXPR>>";
3039     return;
3040   }
3041   AsmPrinter::Impl(os).printAffineExpr(*this);
3042 }
3043 
3044 void AffineExpr::dump() const {
3045   print(llvm::errs());
3046   llvm::errs() << "\n";
3047 }
3048 
3049 void AffineMap::print(raw_ostream &os) const {
3050   if (!map) {
3051     os << "<<NULL AFFINE MAP>>";
3052     return;
3053   }
3054   AsmPrinter::Impl(os).printAffineMap(*this);
3055 }
3056 
3057 void IntegerSet::print(raw_ostream &os) const {
3058   AsmPrinter::Impl(os).printIntegerSet(*this);
3059 }
3060 
3061 void Value::print(raw_ostream &os) { print(os, OpPrintingFlags()); }
3062 void Value::print(raw_ostream &os, const OpPrintingFlags &flags) {
3063   if (!impl) {
3064     os << "<<NULL VALUE>>";
3065     return;
3066   }
3067 
3068   if (auto *op = getDefiningOp())
3069     return op->print(os, flags);
3070   // TODO: Improve BlockArgument print'ing.
3071   BlockArgument arg = this->cast<BlockArgument>();
3072   os << "<block argument> of type '" << arg.getType()
3073      << "' at index: " << arg.getArgNumber();
3074 }
3075 void Value::print(raw_ostream &os, AsmState &state) {
3076   if (!impl) {
3077     os << "<<NULL VALUE>>";
3078     return;
3079   }
3080 
3081   if (auto *op = getDefiningOp())
3082     return op->print(os, state);
3083 
3084   // TODO: Improve BlockArgument print'ing.
3085   BlockArgument arg = this->cast<BlockArgument>();
3086   os << "<block argument> of type '" << arg.getType()
3087      << "' at index: " << arg.getArgNumber();
3088 }
3089 
3090 void Value::dump() {
3091   print(llvm::errs());
3092   llvm::errs() << "\n";
3093 }
3094 
3095 void Value::printAsOperand(raw_ostream &os, AsmState &state) {
3096   // TODO: This doesn't necessarily capture all potential cases.
3097   // Currently, region arguments can be shadowed when printing the main
3098   // operation. If the IR hasn't been printed, this will produce the old SSA
3099   // name and not the shadowed name.
3100   state.getImpl().getSSANameState().printValueID(*this, /*printResultNo=*/true,
3101                                                  os);
3102 }
3103 
3104 void Operation::print(raw_ostream &os, const OpPrintingFlags &printerFlags) {
3105   // If this is a top level operation, we also print aliases.
3106   if (!getParent() && !printerFlags.shouldUseLocalScope()) {
3107     AsmState state(this, printerFlags);
3108     state.getImpl().initializeAliases(this);
3109     print(os, state);
3110     return;
3111   }
3112 
3113   // Find the operation to number from based upon the provided flags.
3114   Operation *op = this;
3115   bool shouldUseLocalScope = printerFlags.shouldUseLocalScope();
3116   do {
3117     // If we are printing local scope, stop at the first operation that is
3118     // isolated from above.
3119     if (shouldUseLocalScope && op->hasTrait<OpTrait::IsIsolatedFromAbove>())
3120       break;
3121 
3122     // Otherwise, traverse up to the next parent.
3123     Operation *parentOp = op->getParentOp();
3124     if (!parentOp)
3125       break;
3126     op = parentOp;
3127   } while (true);
3128 
3129   AsmState state(op, printerFlags);
3130   print(os, state);
3131 }
3132 void Operation::print(raw_ostream &os, AsmState &state) {
3133   OperationPrinter printer(os, state.getImpl());
3134   if (!getParent() && !state.getPrinterFlags().shouldUseLocalScope())
3135     printer.printTopLevelOperation(this);
3136   else
3137     printer.print(this);
3138 }
3139 
3140 void Operation::dump() {
3141   print(llvm::errs(), OpPrintingFlags().useLocalScope());
3142   llvm::errs() << "\n";
3143 }
3144 
3145 void Block::print(raw_ostream &os) {
3146   Operation *parentOp = getParentOp();
3147   if (!parentOp) {
3148     os << "<<UNLINKED BLOCK>>\n";
3149     return;
3150   }
3151   // Get the top-level op.
3152   while (auto *nextOp = parentOp->getParentOp())
3153     parentOp = nextOp;
3154 
3155   AsmState state(parentOp);
3156   print(os, state);
3157 }
3158 void Block::print(raw_ostream &os, AsmState &state) {
3159   OperationPrinter(os, state.getImpl()).print(this);
3160 }
3161 
3162 void Block::dump() { print(llvm::errs()); }
3163 
3164 /// Print out the name of the block without printing its body.
3165 void Block::printAsOperand(raw_ostream &os, bool printType) {
3166   Operation *parentOp = getParentOp();
3167   if (!parentOp) {
3168     os << "<<UNLINKED BLOCK>>\n";
3169     return;
3170   }
3171   AsmState state(parentOp);
3172   printAsOperand(os, state);
3173 }
3174 void Block::printAsOperand(raw_ostream &os, AsmState &state) {
3175   OperationPrinter printer(os, state.getImpl());
3176   printer.printBlockName(this);
3177 }
3178