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 &&
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 << " = " << 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 && 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   /// Print an escaped string, wrapped with "".
1437   void printEscapedString(StringRef str);
1438 
1439   /// Print a hex string, wrapped with "".
1440   void printHexString(StringRef str);
1441   void printHexString(ArrayRef<char> data);
1442 
1443   /// This enum is used to represent the binding strength of the enclosing
1444   /// context that an AffineExprStorage is being printed in, so we can
1445   /// intelligently produce parens.
1446   enum class BindingStrength {
1447     Weak,   // + and -
1448     Strong, // All other binary operators.
1449   };
1450   void printAffineExprInternal(
1451       AffineExpr expr, BindingStrength enclosingTightness,
1452       function_ref<void(unsigned, bool)> printValueName = nullptr);
1453 
1454   /// The output stream for the printer.
1455   raw_ostream &os;
1456 
1457   /// A set of flags to control the printer's behavior.
1458   OpPrintingFlags printerFlags;
1459 
1460   /// An optional printer state for the module.
1461   AsmStateImpl *state;
1462 
1463   /// A tracker for the number of new lines emitted during printing.
1464   NewLineCounter newLine;
1465 };
1466 } // namespace mlir
1467 
1468 void AsmPrinter::Impl::printTrailingLocation(Location loc, bool allowAlias) {
1469   // Check to see if we are printing debug information.
1470   if (!printerFlags.shouldPrintDebugInfo())
1471     return;
1472 
1473   os << " ";
1474   printLocation(loc, /*allowAlias=*/allowAlias);
1475 }
1476 
1477 void AsmPrinter::Impl::printLocationInternal(LocationAttr loc, bool pretty) {
1478   TypeSwitch<LocationAttr>(loc)
1479       .Case<OpaqueLoc>([&](OpaqueLoc loc) {
1480         printLocationInternal(loc.getFallbackLocation(), pretty);
1481       })
1482       .Case<UnknownLoc>([&](UnknownLoc loc) {
1483         if (pretty)
1484           os << "[unknown]";
1485         else
1486           os << "unknown";
1487       })
1488       .Case<FileLineColLoc>([&](FileLineColLoc loc) {
1489         if (pretty)
1490           os << loc.getFilename().getValue();
1491         else
1492           printEscapedString(loc.getFilename());
1493         os << ':' << loc.getLine() << ':' << loc.getColumn();
1494       })
1495       .Case<NameLoc>([&](NameLoc loc) {
1496         printEscapedString(loc.getName());
1497 
1498         // Print the child if it isn't unknown.
1499         auto childLoc = loc.getChildLoc();
1500         if (!childLoc.isa<UnknownLoc>()) {
1501           os << '(';
1502           printLocationInternal(childLoc, pretty);
1503           os << ')';
1504         }
1505       })
1506       .Case<CallSiteLoc>([&](CallSiteLoc loc) {
1507         Location caller = loc.getCaller();
1508         Location callee = loc.getCallee();
1509         if (!pretty)
1510           os << "callsite(";
1511         printLocationInternal(callee, pretty);
1512         if (pretty) {
1513           if (callee.isa<NameLoc>()) {
1514             if (caller.isa<FileLineColLoc>()) {
1515               os << " at ";
1516             } else {
1517               os << newLine << " at ";
1518             }
1519           } else {
1520             os << newLine << " at ";
1521           }
1522         } else {
1523           os << " at ";
1524         }
1525         printLocationInternal(caller, pretty);
1526         if (!pretty)
1527           os << ")";
1528       })
1529       .Case<FusedLoc>([&](FusedLoc loc) {
1530         if (!pretty)
1531           os << "fused";
1532         if (Attribute metadata = loc.getMetadata())
1533           os << '<' << metadata << '>';
1534         os << '[';
1535         interleave(
1536             loc.getLocations(),
1537             [&](Location loc) { printLocationInternal(loc, pretty); },
1538             [&]() { os << ", "; });
1539         os << ']';
1540       });
1541 }
1542 
1543 /// Print a floating point value in a way that the parser will be able to
1544 /// round-trip losslessly.
1545 static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
1546   // We would like to output the FP constant value in exponential notation,
1547   // but we cannot do this if doing so will lose precision.  Check here to
1548   // make sure that we only output it in exponential format if we can parse
1549   // the value back and get the same value.
1550   bool isInf = apValue.isInfinity();
1551   bool isNaN = apValue.isNaN();
1552   if (!isInf && !isNaN) {
1553     SmallString<128> strValue;
1554     apValue.toString(strValue, /*FormatPrecision=*/6, /*FormatMaxPadding=*/0,
1555                      /*TruncateZero=*/false);
1556 
1557     // Check to make sure that the stringized number is not some string like
1558     // "Inf" or NaN, that atof will accept, but the lexer will not.  Check
1559     // that the string matches the "[-+]?[0-9]" regex.
1560     assert(((strValue[0] >= '0' && strValue[0] <= '9') ||
1561             ((strValue[0] == '-' || strValue[0] == '+') &&
1562              (strValue[1] >= '0' && strValue[1] <= '9'))) &&
1563            "[-+]?[0-9] regex does not match!");
1564 
1565     // Parse back the stringized version and check that the value is equal
1566     // (i.e., there is no precision loss).
1567     if (APFloat(apValue.getSemantics(), strValue).bitwiseIsEqual(apValue)) {
1568       os << strValue;
1569       return;
1570     }
1571 
1572     // If it is not, use the default format of APFloat instead of the
1573     // exponential notation.
1574     strValue.clear();
1575     apValue.toString(strValue);
1576 
1577     // Make sure that we can parse the default form as a float.
1578     if (strValue.str().contains('.')) {
1579       os << strValue;
1580       return;
1581     }
1582   }
1583 
1584   // Print special values in hexadecimal format. The sign bit should be included
1585   // in the literal.
1586   SmallVector<char, 16> str;
1587   APInt apInt = apValue.bitcastToAPInt();
1588   apInt.toString(str, /*Radix=*/16, /*Signed=*/false,
1589                  /*formatAsCLiteral=*/true);
1590   os << str;
1591 }
1592 
1593 void AsmPrinter::Impl::printLocation(LocationAttr loc, bool allowAlias) {
1594   if (printerFlags.shouldPrintDebugInfoPrettyForm())
1595     return printLocationInternal(loc, /*pretty=*/true);
1596 
1597   os << "loc(";
1598   if (!allowAlias || !state || failed(state->getAliasState().getAlias(loc, os)))
1599     printLocationInternal(loc);
1600   os << ')';
1601 }
1602 
1603 /// Returns true if the given dialect symbol data is simple enough to print in
1604 /// the pretty form, i.e. without the enclosing "".
1605 static bool isDialectSymbolSimpleEnoughForPrettyForm(StringRef symName) {
1606   // The name must start with an identifier.
1607   if (symName.empty() || !isalpha(symName.front()))
1608     return false;
1609 
1610   // Ignore all the characters that are valid in an identifier in the symbol
1611   // name.
1612   symName = symName.drop_while(
1613       [](char c) { return llvm::isAlnum(c) || c == '.' || c == '_'; });
1614   if (symName.empty())
1615     return true;
1616 
1617   // If we got to an unexpected character, then it must be a <>.  Check those
1618   // recursively.
1619   if (symName.front() != '<' || symName.back() != '>')
1620     return false;
1621 
1622   SmallVector<char, 8> nestedPunctuation;
1623   do {
1624     // If we ran out of characters, then we had a punctuation mismatch.
1625     if (symName.empty())
1626       return false;
1627 
1628     auto c = symName.front();
1629     symName = symName.drop_front();
1630 
1631     switch (c) {
1632     // We never allow null characters. This is an EOF indicator for the lexer
1633     // which we could handle, but isn't important for any known dialect.
1634     case '\0':
1635       return false;
1636     case '<':
1637     case '[':
1638     case '(':
1639     case '{':
1640       nestedPunctuation.push_back(c);
1641       continue;
1642     case '-':
1643       // Treat `->` as a special token.
1644       if (!symName.empty() && symName.front() == '>') {
1645         symName = symName.drop_front();
1646         continue;
1647       }
1648       break;
1649     // Reject types with mismatched brackets.
1650     case '>':
1651       if (nestedPunctuation.pop_back_val() != '<')
1652         return false;
1653       break;
1654     case ']':
1655       if (nestedPunctuation.pop_back_val() != '[')
1656         return false;
1657       break;
1658     case ')':
1659       if (nestedPunctuation.pop_back_val() != '(')
1660         return false;
1661       break;
1662     case '}':
1663       if (nestedPunctuation.pop_back_val() != '{')
1664         return false;
1665       break;
1666     default:
1667       continue;
1668     }
1669 
1670     // We're done when the punctuation is fully matched.
1671   } while (!nestedPunctuation.empty());
1672 
1673   // If there were extra characters, then we failed.
1674   return symName.empty();
1675 }
1676 
1677 /// Print the given dialect symbol to the stream.
1678 static void printDialectSymbol(raw_ostream &os, StringRef symPrefix,
1679                                StringRef dialectName, StringRef symString) {
1680   os << symPrefix << dialectName;
1681 
1682   // If this symbol name is simple enough, print it directly in pretty form,
1683   // otherwise, we print it as an escaped string.
1684   if (isDialectSymbolSimpleEnoughForPrettyForm(symString)) {
1685     os << '.' << symString;
1686     return;
1687   }
1688 
1689   os << "<\"";
1690   llvm::printEscapedString(symString, os);
1691   os << "\">";
1692 }
1693 
1694 /// Returns true if the given string can be represented as a bare identifier.
1695 static bool isBareIdentifier(StringRef name) {
1696   // By making this unsigned, the value passed in to isalnum will always be
1697   // in the range 0-255. This is important when building with MSVC because
1698   // its implementation will assert. This situation can arise when dealing
1699   // with UTF-8 multibyte characters.
1700   if (name.empty() || (!isalpha(name[0]) && name[0] != '_'))
1701     return false;
1702   return llvm::all_of(name.drop_front(), [](unsigned char c) {
1703     return isalnum(c) || c == '_' || c == '$' || c == '.';
1704   });
1705 }
1706 
1707 /// Print the given string as a keyword, or a quoted and escaped string if it
1708 /// has any special or non-printable characters in it.
1709 static void printKeywordOrString(StringRef keyword, raw_ostream &os) {
1710   // If it can be represented as a bare identifier, write it directly.
1711   if (isBareIdentifier(keyword)) {
1712     os << keyword;
1713     return;
1714   }
1715 
1716   // Otherwise, output the keyword wrapped in quotes with proper escaping.
1717   os << "\"";
1718   printEscapedString(keyword, os);
1719   os << '"';
1720 }
1721 
1722 /// Print the given string as a symbol reference. A symbol reference is
1723 /// represented as a string prefixed with '@'. The reference is surrounded with
1724 /// ""'s and escaped if it has any special or non-printable characters in it.
1725 static void printSymbolReference(StringRef symbolRef, raw_ostream &os) {
1726   assert(!symbolRef.empty() && "expected valid symbol reference");
1727   os << '@';
1728   printKeywordOrString(symbolRef, os);
1729 }
1730 
1731 // Print out a valid ElementsAttr that is succinct and can represent any
1732 // potential shape/type, for use when eliding a large ElementsAttr.
1733 //
1734 // We choose to use an opaque ElementsAttr literal with conspicuous content to
1735 // hopefully alert readers to the fact that this has been elided.
1736 //
1737 // Unfortunately, neither of the strings of an opaque ElementsAttr literal will
1738 // accept the string "elided". The first string must be a registered dialect
1739 // name and the latter must be a hex constant.
1740 static void printElidedElementsAttr(raw_ostream &os) {
1741   os << R"(opaque<"elided_large_const", "0xDEADBEEF">)";
1742 }
1743 
1744 LogicalResult AsmPrinter::Impl::printAlias(Attribute attr) {
1745   return success(state && succeeded(state->getAliasState().getAlias(attr, os)));
1746 }
1747 
1748 LogicalResult AsmPrinter::Impl::printAlias(Type type) {
1749   return success(state && succeeded(state->getAliasState().getAlias(type, os)));
1750 }
1751 
1752 void AsmPrinter::Impl::printAttribute(Attribute attr,
1753                                       AttrTypeElision typeElision) {
1754   if (!attr) {
1755     os << "<<NULL ATTRIBUTE>>";
1756     return;
1757   }
1758 
1759   // Try to print an alias for this attribute.
1760   if (succeeded(printAlias(attr)))
1761     return;
1762 
1763   auto attrType = attr.getType();
1764   if (!isa<BuiltinDialect>(attr.getDialect())) {
1765     printDialectAttribute(attr);
1766   } else if (auto opaqueAttr = attr.dyn_cast<OpaqueAttr>()) {
1767     printDialectSymbol(os, "#", opaqueAttr.getDialectNamespace(),
1768                        opaqueAttr.getAttrData());
1769   } else if (attr.isa<UnitAttr>()) {
1770     os << "unit";
1771     return;
1772   } else if (auto dictAttr = attr.dyn_cast<DictionaryAttr>()) {
1773     os << '{';
1774     interleaveComma(dictAttr.getValue(),
1775                     [&](NamedAttribute attr) { printNamedAttribute(attr); });
1776     os << '}';
1777 
1778   } else if (auto intAttr = attr.dyn_cast<IntegerAttr>()) {
1779     if (attrType.isSignlessInteger(1)) {
1780       os << (intAttr.getValue().getBoolValue() ? "true" : "false");
1781 
1782       // Boolean integer attributes always elides the type.
1783       return;
1784     }
1785 
1786     // Only print attributes as unsigned if they are explicitly unsigned or are
1787     // signless 1-bit values.  Indexes, signed values, and multi-bit signless
1788     // values print as signed.
1789     bool isUnsigned =
1790         attrType.isUnsignedInteger() || attrType.isSignlessInteger(1);
1791     intAttr.getValue().print(os, !isUnsigned);
1792 
1793     // IntegerAttr elides the type if I64.
1794     if (typeElision == AttrTypeElision::May && attrType.isSignlessInteger(64))
1795       return;
1796 
1797   } else if (auto floatAttr = attr.dyn_cast<FloatAttr>()) {
1798     printFloatValue(floatAttr.getValue(), os);
1799 
1800     // FloatAttr elides the type if F64.
1801     if (typeElision == AttrTypeElision::May && attrType.isF64())
1802       return;
1803 
1804   } else if (auto strAttr = attr.dyn_cast<StringAttr>()) {
1805     printEscapedString(strAttr.getValue());
1806 
1807   } else if (auto arrayAttr = attr.dyn_cast<ArrayAttr>()) {
1808     os << '[';
1809     interleaveComma(arrayAttr.getValue(), [&](Attribute attr) {
1810       printAttribute(attr, AttrTypeElision::May);
1811     });
1812     os << ']';
1813 
1814   } else if (auto affineMapAttr = attr.dyn_cast<AffineMapAttr>()) {
1815     os << "affine_map<";
1816     affineMapAttr.getValue().print(os);
1817     os << '>';
1818 
1819     // AffineMap always elides the type.
1820     return;
1821 
1822   } else if (auto integerSetAttr = attr.dyn_cast<IntegerSetAttr>()) {
1823     os << "affine_set<";
1824     integerSetAttr.getValue().print(os);
1825     os << '>';
1826 
1827     // IntegerSet always elides the type.
1828     return;
1829 
1830   } else if (auto typeAttr = attr.dyn_cast<TypeAttr>()) {
1831     printType(typeAttr.getValue());
1832 
1833   } else if (auto refAttr = attr.dyn_cast<SymbolRefAttr>()) {
1834     printSymbolReference(refAttr.getRootReference().getValue(), os);
1835     for (FlatSymbolRefAttr nestedRef : refAttr.getNestedReferences()) {
1836       os << "::";
1837       printSymbolReference(nestedRef.getValue(), os);
1838     }
1839 
1840   } else if (auto opaqueAttr = attr.dyn_cast<OpaqueElementsAttr>()) {
1841     if (printerFlags.shouldElideElementsAttr(opaqueAttr)) {
1842       printElidedElementsAttr(os);
1843     } else {
1844       os << "opaque<" << opaqueAttr.getDialect() << ", ";
1845       printHexString(opaqueAttr.getValue());
1846       os << ">";
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   } else if (auto denseArrayAttr = attr.dyn_cast<DenseArrayBaseAttr>()) {
1882     typeElision = AttrTypeElision::Must;
1883     switch (denseArrayAttr.getElementType()) {
1884     case DenseArrayBaseAttr::EltType::I8:
1885       os << "[:i8 ";
1886       break;
1887     case DenseArrayBaseAttr::EltType::I16:
1888       os << "[:i16 ";
1889       break;
1890     case DenseArrayBaseAttr::EltType::I32:
1891       os << "[:i32 ";
1892       break;
1893     case DenseArrayBaseAttr::EltType::I64:
1894       os << "[:i64 ";
1895       break;
1896     case DenseArrayBaseAttr::EltType::F32:
1897       os << "[:f32 ";
1898       break;
1899     case DenseArrayBaseAttr::EltType::F64:
1900       os << "[:f64 ";
1901       break;
1902     }
1903     denseArrayAttr.printWithoutBraces(os);
1904     os << "]";
1905   } else if (auto locAttr = attr.dyn_cast<LocationAttr>()) {
1906     printLocation(locAttr);
1907   } else {
1908     llvm::report_fatal_error("Unknown builtin attribute");
1909   }
1910   // Don't print the type if we must elide it, or if it is a None type.
1911   if (typeElision != AttrTypeElision::Must && !attrType.isa<NoneType>()) {
1912     os << " : ";
1913     printType(attrType);
1914   }
1915 }
1916 
1917 /// Print the integer element of a DenseElementsAttr.
1918 static void printDenseIntElement(const APInt &value, raw_ostream &os,
1919                                  bool isSigned) {
1920   if (value.getBitWidth() == 1)
1921     os << (value.getBoolValue() ? "true" : "false");
1922   else
1923     value.print(os, isSigned);
1924 }
1925 
1926 static void
1927 printDenseElementsAttrImpl(bool isSplat, ShapedType type, raw_ostream &os,
1928                            function_ref<void(unsigned)> printEltFn) {
1929   // Special case for 0-d and splat tensors.
1930   if (isSplat)
1931     return printEltFn(0);
1932 
1933   // Special case for degenerate tensors.
1934   auto numElements = type.getNumElements();
1935   if (numElements == 0)
1936     return;
1937 
1938   // We use a mixed-radix counter to iterate through the shape. When we bump a
1939   // non-least-significant digit, we emit a close bracket. When we next emit an
1940   // element we re-open all closed brackets.
1941 
1942   // The mixed-radix counter, with radices in 'shape'.
1943   int64_t rank = type.getRank();
1944   SmallVector<unsigned, 4> counter(rank, 0);
1945   // The number of brackets that have been opened and not closed.
1946   unsigned openBrackets = 0;
1947 
1948   auto shape = type.getShape();
1949   auto bumpCounter = [&] {
1950     // Bump the least significant digit.
1951     ++counter[rank - 1];
1952     // Iterate backwards bubbling back the increment.
1953     for (unsigned i = rank - 1; i > 0; --i)
1954       if (counter[i] >= shape[i]) {
1955         // Index 'i' is rolled over. Bump (i-1) and close a bracket.
1956         counter[i] = 0;
1957         ++counter[i - 1];
1958         --openBrackets;
1959         os << ']';
1960       }
1961   };
1962 
1963   for (unsigned idx = 0, e = numElements; idx != e; ++idx) {
1964     if (idx != 0)
1965       os << ", ";
1966     while (openBrackets++ < rank)
1967       os << '[';
1968     openBrackets = rank;
1969     printEltFn(idx);
1970     bumpCounter();
1971   }
1972   while (openBrackets-- > 0)
1973     os << ']';
1974 }
1975 
1976 void AsmPrinter::Impl::printDenseElementsAttr(DenseElementsAttr attr,
1977                                               bool allowHex) {
1978   if (auto stringAttr = attr.dyn_cast<DenseStringElementsAttr>())
1979     return printDenseStringElementsAttr(stringAttr);
1980 
1981   printDenseIntOrFPElementsAttr(attr.cast<DenseIntOrFPElementsAttr>(),
1982                                 allowHex);
1983 }
1984 
1985 void AsmPrinter::Impl::printDenseIntOrFPElementsAttr(
1986     DenseIntOrFPElementsAttr attr, bool allowHex) {
1987   auto type = attr.getType();
1988   auto elementType = type.getElementType();
1989 
1990   // Check to see if we should format this attribute as a hex string.
1991   auto numElements = type.getNumElements();
1992   if (!attr.isSplat() && allowHex &&
1993       shouldPrintElementsAttrWithHex(numElements)) {
1994     ArrayRef<char> rawData = attr.getRawData();
1995     if (llvm::support::endian::system_endianness() ==
1996         llvm::support::endianness::big) {
1997       // Convert endianess in big-endian(BE) machines. `rawData` is BE in BE
1998       // machines. It is converted here to print in LE format.
1999       SmallVector<char, 64> outDataVec(rawData.size());
2000       MutableArrayRef<char> convRawData(outDataVec);
2001       DenseIntOrFPElementsAttr::convertEndianOfArrayRefForBEmachine(
2002           rawData, convRawData, type);
2003       printHexString(convRawData);
2004     } else {
2005       printHexString(rawData);
2006     }
2007 
2008     return;
2009   }
2010 
2011   if (ComplexType complexTy = elementType.dyn_cast<ComplexType>()) {
2012     Type complexElementType = complexTy.getElementType();
2013     // Note: The if and else below had a common lambda function which invoked
2014     // printDenseElementsAttrImpl. This lambda was hitting a bug in gcc 9.1,9.2
2015     // and hence was replaced.
2016     if (complexElementType.isa<IntegerType>()) {
2017       bool isSigned = !complexElementType.isUnsignedInteger();
2018       auto valueIt = attr.value_begin<std::complex<APInt>>();
2019       printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
2020         auto complexValue = *(valueIt + index);
2021         os << "(";
2022         printDenseIntElement(complexValue.real(), os, isSigned);
2023         os << ",";
2024         printDenseIntElement(complexValue.imag(), os, isSigned);
2025         os << ")";
2026       });
2027     } else {
2028       auto valueIt = attr.value_begin<std::complex<APFloat>>();
2029       printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
2030         auto complexValue = *(valueIt + index);
2031         os << "(";
2032         printFloatValue(complexValue.real(), os);
2033         os << ",";
2034         printFloatValue(complexValue.imag(), os);
2035         os << ")";
2036       });
2037     }
2038   } else if (elementType.isIntOrIndex()) {
2039     bool isSigned = !elementType.isUnsignedInteger();
2040     auto valueIt = attr.value_begin<APInt>();
2041     printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
2042       printDenseIntElement(*(valueIt + index), os, isSigned);
2043     });
2044   } else {
2045     assert(elementType.isa<FloatType>() && "unexpected element type");
2046     auto valueIt = attr.value_begin<APFloat>();
2047     printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
2048       printFloatValue(*(valueIt + index), os);
2049     });
2050   }
2051 }
2052 
2053 void AsmPrinter::Impl::printDenseStringElementsAttr(
2054     DenseStringElementsAttr attr) {
2055   ArrayRef<StringRef> data = attr.getRawStringData();
2056   auto printFn = [&](unsigned index) { printEscapedString(data[index]); };
2057   printDenseElementsAttrImpl(attr.isSplat(), attr.getType(), os, printFn);
2058 }
2059 
2060 void AsmPrinter::Impl::printType(Type type) {
2061   if (!type) {
2062     os << "<<NULL TYPE>>";
2063     return;
2064   }
2065 
2066   // Try to print an alias for this type.
2067   if (state && succeeded(state->getAliasState().getAlias(type, os)))
2068     return;
2069 
2070   TypeSwitch<Type>(type)
2071       .Case<OpaqueType>([&](OpaqueType opaqueTy) {
2072         printDialectSymbol(os, "!", opaqueTy.getDialectNamespace(),
2073                            opaqueTy.getTypeData());
2074       })
2075       .Case<IndexType>([&](Type) { os << "index"; })
2076       .Case<BFloat16Type>([&](Type) { os << "bf16"; })
2077       .Case<Float16Type>([&](Type) { os << "f16"; })
2078       .Case<Float32Type>([&](Type) { os << "f32"; })
2079       .Case<Float64Type>([&](Type) { os << "f64"; })
2080       .Case<Float80Type>([&](Type) { os << "f80"; })
2081       .Case<Float128Type>([&](Type) { os << "f128"; })
2082       .Case<IntegerType>([&](IntegerType integerTy) {
2083         if (integerTy.isSigned())
2084           os << 's';
2085         else if (integerTy.isUnsigned())
2086           os << 'u';
2087         os << 'i' << integerTy.getWidth();
2088       })
2089       .Case<FunctionType>([&](FunctionType funcTy) {
2090         os << '(';
2091         interleaveComma(funcTy.getInputs(), [&](Type ty) { printType(ty); });
2092         os << ") -> ";
2093         ArrayRef<Type> results = funcTy.getResults();
2094         if (results.size() == 1 && !results[0].isa<FunctionType>()) {
2095           printType(results[0]);
2096         } else {
2097           os << '(';
2098           interleaveComma(results, [&](Type ty) { printType(ty); });
2099           os << ')';
2100         }
2101       })
2102       .Case<VectorType>([&](VectorType vectorTy) {
2103         os << "vector<";
2104         auto vShape = vectorTy.getShape();
2105         unsigned lastDim = vShape.size();
2106         unsigned lastFixedDim = lastDim - vectorTy.getNumScalableDims();
2107         unsigned dimIdx = 0;
2108         for (dimIdx = 0; dimIdx < lastFixedDim; dimIdx++)
2109           os << vShape[dimIdx] << 'x';
2110         if (vectorTy.isScalable()) {
2111           os << '[';
2112           unsigned secondToLastDim = lastDim - 1;
2113           for (; dimIdx < secondToLastDim; dimIdx++)
2114             os << vShape[dimIdx] << 'x';
2115           os << vShape[dimIdx] << "]x";
2116         }
2117         printType(vectorTy.getElementType());
2118         os << '>';
2119       })
2120       .Case<RankedTensorType>([&](RankedTensorType tensorTy) {
2121         os << "tensor<";
2122         for (int64_t dim : tensorTy.getShape()) {
2123           if (ShapedType::isDynamic(dim))
2124             os << '?';
2125           else
2126             os << dim;
2127           os << 'x';
2128         }
2129         printType(tensorTy.getElementType());
2130         // Only print the encoding attribute value if set.
2131         if (tensorTy.getEncoding()) {
2132           os << ", ";
2133           printAttribute(tensorTy.getEncoding());
2134         }
2135         os << '>';
2136       })
2137       .Case<UnrankedTensorType>([&](UnrankedTensorType tensorTy) {
2138         os << "tensor<*x";
2139         printType(tensorTy.getElementType());
2140         os << '>';
2141       })
2142       .Case<MemRefType>([&](MemRefType memrefTy) {
2143         os << "memref<";
2144         for (int64_t dim : memrefTy.getShape()) {
2145           if (ShapedType::isDynamic(dim))
2146             os << '?';
2147           else
2148             os << dim;
2149           os << 'x';
2150         }
2151         printType(memrefTy.getElementType());
2152         if (!memrefTy.getLayout().isIdentity()) {
2153           os << ", ";
2154           printAttribute(memrefTy.getLayout(), AttrTypeElision::May);
2155         }
2156         // Only print the memory space if it is the non-default one.
2157         if (memrefTy.getMemorySpace()) {
2158           os << ", ";
2159           printAttribute(memrefTy.getMemorySpace(), AttrTypeElision::May);
2160         }
2161         os << '>';
2162       })
2163       .Case<UnrankedMemRefType>([&](UnrankedMemRefType memrefTy) {
2164         os << "memref<*x";
2165         printType(memrefTy.getElementType());
2166         // Only print the memory space if it is the non-default one.
2167         if (memrefTy.getMemorySpace()) {
2168           os << ", ";
2169           printAttribute(memrefTy.getMemorySpace(), AttrTypeElision::May);
2170         }
2171         os << '>';
2172       })
2173       .Case<ComplexType>([&](ComplexType complexTy) {
2174         os << "complex<";
2175         printType(complexTy.getElementType());
2176         os << '>';
2177       })
2178       .Case<TupleType>([&](TupleType tupleTy) {
2179         os << "tuple<";
2180         interleaveComma(tupleTy.getTypes(),
2181                         [&](Type type) { printType(type); });
2182         os << '>';
2183       })
2184       .Case<NoneType>([&](Type) { os << "none"; })
2185       .Default([&](Type type) { return printDialectType(type); });
2186 }
2187 
2188 void AsmPrinter::Impl::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
2189                                              ArrayRef<StringRef> elidedAttrs,
2190                                              bool withKeyword) {
2191   // If there are no attributes, then there is nothing to be done.
2192   if (attrs.empty())
2193     return;
2194 
2195   // Functor used to print a filtered attribute list.
2196   auto printFilteredAttributesFn = [&](auto filteredAttrs) {
2197     // Print the 'attributes' keyword if necessary.
2198     if (withKeyword)
2199       os << " attributes";
2200 
2201     // Otherwise, print them all out in braces.
2202     os << " {";
2203     interleaveComma(filteredAttrs,
2204                     [&](NamedAttribute attr) { printNamedAttribute(attr); });
2205     os << '}';
2206   };
2207 
2208   // If no attributes are elided, we can directly print with no filtering.
2209   if (elidedAttrs.empty())
2210     return printFilteredAttributesFn(attrs);
2211 
2212   // Otherwise, filter out any attributes that shouldn't be included.
2213   llvm::SmallDenseSet<StringRef> elidedAttrsSet(elidedAttrs.begin(),
2214                                                 elidedAttrs.end());
2215   auto filteredAttrs = llvm::make_filter_range(attrs, [&](NamedAttribute attr) {
2216     return !elidedAttrsSet.contains(attr.getName().strref());
2217   });
2218   if (!filteredAttrs.empty())
2219     printFilteredAttributesFn(filteredAttrs);
2220 }
2221 
2222 void AsmPrinter::Impl::printNamedAttribute(NamedAttribute attr) {
2223   // Print the name without quotes if possible.
2224   ::printKeywordOrString(attr.getName().strref(), os);
2225 
2226   // Pretty printing elides the attribute value for unit attributes.
2227   if (attr.getValue().isa<UnitAttr>())
2228     return;
2229 
2230   os << " = ";
2231   printAttribute(attr.getValue());
2232 }
2233 
2234 void AsmPrinter::Impl::printDialectAttribute(Attribute attr) {
2235   auto &dialect = attr.getDialect();
2236 
2237   // Ask the dialect to serialize the attribute to a string.
2238   std::string attrName;
2239   {
2240     llvm::raw_string_ostream attrNameStr(attrName);
2241     Impl subPrinter(attrNameStr, printerFlags, state);
2242     DialectAsmPrinter printer(subPrinter);
2243     dialect.printAttribute(attr, printer);
2244   }
2245   printDialectSymbol(os, "#", dialect.getNamespace(), attrName);
2246 }
2247 
2248 void AsmPrinter::Impl::printDialectType(Type type) {
2249   auto &dialect = type.getDialect();
2250 
2251   // Ask the dialect to serialize the type to a string.
2252   std::string typeName;
2253   {
2254     llvm::raw_string_ostream typeNameStr(typeName);
2255     Impl subPrinter(typeNameStr, printerFlags, state);
2256     DialectAsmPrinter printer(subPrinter);
2257     dialect.printType(type, printer);
2258   }
2259   printDialectSymbol(os, "!", dialect.getNamespace(), typeName);
2260 }
2261 
2262 void AsmPrinter::Impl::printEscapedString(StringRef str) {
2263   os << "\"";
2264   llvm::printEscapedString(str, os);
2265   os << "\"";
2266 }
2267 
2268 void AsmPrinter::Impl::printHexString(StringRef str) {
2269   os << "\"0x" << llvm::toHex(str) << "\"";
2270 }
2271 void AsmPrinter::Impl::printHexString(ArrayRef<char> data) {
2272   printHexString(StringRef(data.data(), data.size()));
2273 }
2274 
2275 //===--------------------------------------------------------------------===//
2276 // AsmPrinter
2277 //===--------------------------------------------------------------------===//
2278 
2279 AsmPrinter::~AsmPrinter() = default;
2280 
2281 raw_ostream &AsmPrinter::getStream() const {
2282   assert(impl && "expected AsmPrinter::getStream to be overriden");
2283   return impl->getStream();
2284 }
2285 
2286 /// Print the given floating point value in a stablized form.
2287 void AsmPrinter::printFloat(const APFloat &value) {
2288   assert(impl && "expected AsmPrinter::printFloat to be overriden");
2289   printFloatValue(value, impl->getStream());
2290 }
2291 
2292 void AsmPrinter::printType(Type type) {
2293   assert(impl && "expected AsmPrinter::printType to be overriden");
2294   impl->printType(type);
2295 }
2296 
2297 void AsmPrinter::printAttribute(Attribute attr) {
2298   assert(impl && "expected AsmPrinter::printAttribute to be overriden");
2299   impl->printAttribute(attr);
2300 }
2301 
2302 LogicalResult AsmPrinter::printAlias(Attribute attr) {
2303   assert(impl && "expected AsmPrinter::printAlias to be overriden");
2304   return impl->printAlias(attr);
2305 }
2306 
2307 LogicalResult AsmPrinter::printAlias(Type type) {
2308   assert(impl && "expected AsmPrinter::printAlias to be overriden");
2309   return impl->printAlias(type);
2310 }
2311 
2312 void AsmPrinter::printAttributeWithoutType(Attribute attr) {
2313   assert(impl &&
2314          "expected AsmPrinter::printAttributeWithoutType to be overriden");
2315   impl->printAttribute(attr, Impl::AttrTypeElision::Must);
2316 }
2317 
2318 void AsmPrinter::printKeywordOrString(StringRef keyword) {
2319   assert(impl && "expected AsmPrinter::printKeywordOrString to be overriden");
2320   ::printKeywordOrString(keyword, impl->getStream());
2321 }
2322 
2323 void AsmPrinter::printSymbolName(StringRef symbolRef) {
2324   assert(impl && "expected AsmPrinter::printSymbolName to be overriden");
2325   ::printSymbolReference(symbolRef, impl->getStream());
2326 }
2327 
2328 //===----------------------------------------------------------------------===//
2329 // Affine expressions and maps
2330 //===----------------------------------------------------------------------===//
2331 
2332 void AsmPrinter::Impl::printAffineExpr(
2333     AffineExpr expr, function_ref<void(unsigned, bool)> printValueName) {
2334   printAffineExprInternal(expr, BindingStrength::Weak, printValueName);
2335 }
2336 
2337 void AsmPrinter::Impl::printAffineExprInternal(
2338     AffineExpr expr, BindingStrength enclosingTightness,
2339     function_ref<void(unsigned, bool)> printValueName) {
2340   const char *binopSpelling = nullptr;
2341   switch (expr.getKind()) {
2342   case AffineExprKind::SymbolId: {
2343     unsigned pos = expr.cast<AffineSymbolExpr>().getPosition();
2344     if (printValueName)
2345       printValueName(pos, /*isSymbol=*/true);
2346     else
2347       os << 's' << pos;
2348     return;
2349   }
2350   case AffineExprKind::DimId: {
2351     unsigned pos = expr.cast<AffineDimExpr>().getPosition();
2352     if (printValueName)
2353       printValueName(pos, /*isSymbol=*/false);
2354     else
2355       os << 'd' << pos;
2356     return;
2357   }
2358   case AffineExprKind::Constant:
2359     os << expr.cast<AffineConstantExpr>().getValue();
2360     return;
2361   case AffineExprKind::Add:
2362     binopSpelling = " + ";
2363     break;
2364   case AffineExprKind::Mul:
2365     binopSpelling = " * ";
2366     break;
2367   case AffineExprKind::FloorDiv:
2368     binopSpelling = " floordiv ";
2369     break;
2370   case AffineExprKind::CeilDiv:
2371     binopSpelling = " ceildiv ";
2372     break;
2373   case AffineExprKind::Mod:
2374     binopSpelling = " mod ";
2375     break;
2376   }
2377 
2378   auto binOp = expr.cast<AffineBinaryOpExpr>();
2379   AffineExpr lhsExpr = binOp.getLHS();
2380   AffineExpr rhsExpr = binOp.getRHS();
2381 
2382   // Handle tightly binding binary operators.
2383   if (binOp.getKind() != AffineExprKind::Add) {
2384     if (enclosingTightness == BindingStrength::Strong)
2385       os << '(';
2386 
2387     // Pretty print multiplication with -1.
2388     auto rhsConst = rhsExpr.dyn_cast<AffineConstantExpr>();
2389     if (rhsConst && binOp.getKind() == AffineExprKind::Mul &&
2390         rhsConst.getValue() == -1) {
2391       os << "-";
2392       printAffineExprInternal(lhsExpr, BindingStrength::Strong, printValueName);
2393       if (enclosingTightness == BindingStrength::Strong)
2394         os << ')';
2395       return;
2396     }
2397 
2398     printAffineExprInternal(lhsExpr, BindingStrength::Strong, printValueName);
2399 
2400     os << binopSpelling;
2401     printAffineExprInternal(rhsExpr, BindingStrength::Strong, printValueName);
2402 
2403     if (enclosingTightness == BindingStrength::Strong)
2404       os << ')';
2405     return;
2406   }
2407 
2408   // Print out special "pretty" forms for add.
2409   if (enclosingTightness == BindingStrength::Strong)
2410     os << '(';
2411 
2412   // Pretty print addition to a product that has a negative operand as a
2413   // subtraction.
2414   if (auto rhs = rhsExpr.dyn_cast<AffineBinaryOpExpr>()) {
2415     if (rhs.getKind() == AffineExprKind::Mul) {
2416       AffineExpr rrhsExpr = rhs.getRHS();
2417       if (auto rrhs = rrhsExpr.dyn_cast<AffineConstantExpr>()) {
2418         if (rrhs.getValue() == -1) {
2419           printAffineExprInternal(lhsExpr, BindingStrength::Weak,
2420                                   printValueName);
2421           os << " - ";
2422           if (rhs.getLHS().getKind() == AffineExprKind::Add) {
2423             printAffineExprInternal(rhs.getLHS(), BindingStrength::Strong,
2424                                     printValueName);
2425           } else {
2426             printAffineExprInternal(rhs.getLHS(), BindingStrength::Weak,
2427                                     printValueName);
2428           }
2429 
2430           if (enclosingTightness == BindingStrength::Strong)
2431             os << ')';
2432           return;
2433         }
2434 
2435         if (rrhs.getValue() < -1) {
2436           printAffineExprInternal(lhsExpr, BindingStrength::Weak,
2437                                   printValueName);
2438           os << " - ";
2439           printAffineExprInternal(rhs.getLHS(), BindingStrength::Strong,
2440                                   printValueName);
2441           os << " * " << -rrhs.getValue();
2442           if (enclosingTightness == BindingStrength::Strong)
2443             os << ')';
2444           return;
2445         }
2446       }
2447     }
2448   }
2449 
2450   // Pretty print addition to a negative number as a subtraction.
2451   if (auto rhsConst = rhsExpr.dyn_cast<AffineConstantExpr>()) {
2452     if (rhsConst.getValue() < 0) {
2453       printAffineExprInternal(lhsExpr, BindingStrength::Weak, printValueName);
2454       os << " - " << -rhsConst.getValue();
2455       if (enclosingTightness == BindingStrength::Strong)
2456         os << ')';
2457       return;
2458     }
2459   }
2460 
2461   printAffineExprInternal(lhsExpr, BindingStrength::Weak, printValueName);
2462 
2463   os << " + ";
2464   printAffineExprInternal(rhsExpr, BindingStrength::Weak, printValueName);
2465 
2466   if (enclosingTightness == BindingStrength::Strong)
2467     os << ')';
2468 }
2469 
2470 void AsmPrinter::Impl::printAffineConstraint(AffineExpr expr, bool isEq) {
2471   printAffineExprInternal(expr, BindingStrength::Weak);
2472   isEq ? os << " == 0" : os << " >= 0";
2473 }
2474 
2475 void AsmPrinter::Impl::printAffineMap(AffineMap map) {
2476   // Dimension identifiers.
2477   os << '(';
2478   for (int i = 0; i < (int)map.getNumDims() - 1; ++i)
2479     os << 'd' << i << ", ";
2480   if (map.getNumDims() >= 1)
2481     os << 'd' << map.getNumDims() - 1;
2482   os << ')';
2483 
2484   // Symbolic identifiers.
2485   if (map.getNumSymbols() != 0) {
2486     os << '[';
2487     for (unsigned i = 0; i < map.getNumSymbols() - 1; ++i)
2488       os << 's' << i << ", ";
2489     if (map.getNumSymbols() >= 1)
2490       os << 's' << map.getNumSymbols() - 1;
2491     os << ']';
2492   }
2493 
2494   // Result affine expressions.
2495   os << " -> (";
2496   interleaveComma(map.getResults(),
2497                   [&](AffineExpr expr) { printAffineExpr(expr); });
2498   os << ')';
2499 }
2500 
2501 void AsmPrinter::Impl::printIntegerSet(IntegerSet set) {
2502   // Dimension identifiers.
2503   os << '(';
2504   for (unsigned i = 1; i < set.getNumDims(); ++i)
2505     os << 'd' << i - 1 << ", ";
2506   if (set.getNumDims() >= 1)
2507     os << 'd' << set.getNumDims() - 1;
2508   os << ')';
2509 
2510   // Symbolic identifiers.
2511   if (set.getNumSymbols() != 0) {
2512     os << '[';
2513     for (unsigned i = 0; i < set.getNumSymbols() - 1; ++i)
2514       os << 's' << i << ", ";
2515     if (set.getNumSymbols() >= 1)
2516       os << 's' << set.getNumSymbols() - 1;
2517     os << ']';
2518   }
2519 
2520   // Print constraints.
2521   os << " : (";
2522   int numConstraints = set.getNumConstraints();
2523   for (int i = 1; i < numConstraints; ++i) {
2524     printAffineConstraint(set.getConstraint(i - 1), set.isEq(i - 1));
2525     os << ", ";
2526   }
2527   if (numConstraints >= 1)
2528     printAffineConstraint(set.getConstraint(numConstraints - 1),
2529                           set.isEq(numConstraints - 1));
2530   os << ')';
2531 }
2532 
2533 //===----------------------------------------------------------------------===//
2534 // OperationPrinter
2535 //===----------------------------------------------------------------------===//
2536 
2537 namespace {
2538 /// This class contains the logic for printing operations, regions, and blocks.
2539 class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
2540 public:
2541   using Impl = AsmPrinter::Impl;
2542   using Impl::printType;
2543 
2544   explicit OperationPrinter(raw_ostream &os, AsmStateImpl &state)
2545       : Impl(os, state.getPrinterFlags(), &state),
2546         OpAsmPrinter(static_cast<Impl &>(*this)) {}
2547 
2548   /// Print the given top-level operation.
2549   void printTopLevelOperation(Operation *op);
2550 
2551   /// Print the given operation with its indent and location.
2552   void print(Operation *op);
2553   /// Print the bare location, not including indentation/location/etc.
2554   void printOperation(Operation *op);
2555   /// Print the given operation in the generic form.
2556   void printGenericOp(Operation *op, bool printOpName) override;
2557 
2558   /// Print the name of the given block.
2559   void printBlockName(Block *block);
2560 
2561   /// Print the given block. If 'printBlockArgs' is false, the arguments of the
2562   /// block are not printed. If 'printBlockTerminator' is false, the terminator
2563   /// operation of the block is not printed.
2564   void print(Block *block, bool printBlockArgs = true,
2565              bool printBlockTerminator = true);
2566 
2567   /// Print the ID of the given value, optionally with its result number.
2568   void printValueID(Value value, bool printResultNo = true,
2569                     raw_ostream *streamOverride = nullptr) const;
2570 
2571   /// Print the ID of the given operation.
2572   void printOperationID(Operation *op,
2573                         raw_ostream *streamOverride = nullptr) const;
2574 
2575   //===--------------------------------------------------------------------===//
2576   // OpAsmPrinter methods
2577   //===--------------------------------------------------------------------===//
2578 
2579   /// Print a newline and indent the printer to the start of the current
2580   /// operation.
2581   void printNewline() override {
2582     os << newLine;
2583     os.indent(currentIndent);
2584   }
2585 
2586   /// Print a block argument in the usual format of:
2587   ///   %ssaName : type {attr1=42} loc("here")
2588   /// where location printing is controlled by the standard internal option.
2589   /// You may pass omitType=true to not print a type, and pass an empty
2590   /// attribute list if you don't care for attributes.
2591   void printRegionArgument(BlockArgument arg,
2592                            ArrayRef<NamedAttribute> argAttrs = {},
2593                            bool omitType = false) override;
2594 
2595   /// Print the ID for the given value.
2596   void printOperand(Value value) override { printValueID(value); }
2597   void printOperand(Value value, raw_ostream &os) override {
2598     printValueID(value, /*printResultNo=*/true, &os);
2599   }
2600 
2601   /// Print an optional attribute dictionary with a given set of elided values.
2602   void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
2603                              ArrayRef<StringRef> elidedAttrs = {}) override {
2604     Impl::printOptionalAttrDict(attrs, elidedAttrs);
2605   }
2606   void printOptionalAttrDictWithKeyword(
2607       ArrayRef<NamedAttribute> attrs,
2608       ArrayRef<StringRef> elidedAttrs = {}) override {
2609     Impl::printOptionalAttrDict(attrs, elidedAttrs,
2610                                 /*withKeyword=*/true);
2611   }
2612 
2613   /// Print the given successor.
2614   void printSuccessor(Block *successor) override;
2615 
2616   /// Print an operation successor with the operands used for the block
2617   /// arguments.
2618   void printSuccessorAndUseList(Block *successor,
2619                                 ValueRange succOperands) override;
2620 
2621   /// Print the given region.
2622   void printRegion(Region &region, bool printEntryBlockArgs,
2623                    bool printBlockTerminators, bool printEmptyBlock) override;
2624 
2625   /// Renumber the arguments for the specified region to the same names as the
2626   /// SSA values in namesToUse. This may only be used for IsolatedFromAbove
2627   /// operations. If any entry in namesToUse is null, the corresponding
2628   /// argument name is left alone.
2629   void shadowRegionArgs(Region &region, ValueRange namesToUse) override {
2630     state->getSSANameState().shadowRegionArgs(region, namesToUse);
2631   }
2632 
2633   /// Print the given affine map with the symbol and dimension operands printed
2634   /// inline with the map.
2635   void printAffineMapOfSSAIds(AffineMapAttr mapAttr,
2636                               ValueRange operands) override;
2637 
2638   /// Print the given affine expression with the symbol and dimension operands
2639   /// printed inline with the expression.
2640   void printAffineExprOfSSAIds(AffineExpr expr, ValueRange dimOperands,
2641                                ValueRange symOperands) override;
2642 
2643   /// Print users of this operation or id of this operation if it has no result.
2644   void printUsersComment(Operation *op);
2645 
2646   /// Print users of this block arg.
2647   void printUsersComment(BlockArgument arg);
2648 
2649   /// Print the users of a value.
2650   void printValueUsers(Value value);
2651 
2652   /// Print either the ids of the result values or the id of the operation if
2653   /// the operation has no results.
2654   void printUserIDs(Operation *user, bool prefixComma = false);
2655 
2656 private:
2657   // Contains the stack of default dialects to use when printing regions.
2658   // A new dialect is pushed to the stack before parsing regions nested under an
2659   // operation implementing `OpAsmOpInterface`, and popped when done. At the
2660   // top-level we start with "builtin" as the default, so that the top-level
2661   // `module` operation prints as-is.
2662   SmallVector<StringRef> defaultDialectStack{"builtin"};
2663 
2664   /// The number of spaces used for indenting nested operations.
2665   const static unsigned indentWidth = 2;
2666 
2667   // This is the current indentation level for nested structures.
2668   unsigned currentIndent = 0;
2669 };
2670 } // namespace
2671 
2672 void OperationPrinter::printTopLevelOperation(Operation *op) {
2673   // Output the aliases at the top level that can't be deferred.
2674   state->getAliasState().printNonDeferredAliases(os, newLine);
2675 
2676   // Print the module.
2677   print(op);
2678   os << newLine;
2679 
2680   // Output the aliases at the top level that can be deferred.
2681   state->getAliasState().printDeferredAliases(os, newLine);
2682 }
2683 
2684 /// Print a block argument in the usual format of:
2685 ///   %ssaName : type {attr1=42} loc("here")
2686 /// where location printing is controlled by the standard internal option.
2687 /// You may pass omitType=true to not print a type, and pass an empty
2688 /// attribute list if you don't care for attributes.
2689 void OperationPrinter::printRegionArgument(BlockArgument arg,
2690                                            ArrayRef<NamedAttribute> argAttrs,
2691                                            bool omitType) {
2692   printOperand(arg);
2693   if (!omitType) {
2694     os << ": ";
2695     printType(arg.getType());
2696   }
2697   printOptionalAttrDict(argAttrs);
2698   // TODO: We should allow location aliases on block arguments.
2699   printTrailingLocation(arg.getLoc(), /*allowAlias*/ false);
2700 }
2701 
2702 void OperationPrinter::print(Operation *op) {
2703   // Track the location of this operation.
2704   state->registerOperationLocation(op, newLine.curLine, currentIndent);
2705 
2706   os.indent(currentIndent);
2707   printOperation(op);
2708   printTrailingLocation(op->getLoc());
2709   if (printerFlags.shouldPrintValueUsers())
2710     printUsersComment(op);
2711 }
2712 
2713 void OperationPrinter::printOperation(Operation *op) {
2714   if (size_t numResults = op->getNumResults()) {
2715     auto printResultGroup = [&](size_t resultNo, size_t resultCount) {
2716       printValueID(op->getResult(resultNo), /*printResultNo=*/false);
2717       if (resultCount > 1)
2718         os << ':' << resultCount;
2719     };
2720 
2721     // Check to see if this operation has multiple result groups.
2722     ArrayRef<int> resultGroups = state->getSSANameState().getOpResultGroups(op);
2723     if (!resultGroups.empty()) {
2724       // Interleave the groups excluding the last one, this one will be handled
2725       // separately.
2726       interleaveComma(llvm::seq<int>(0, resultGroups.size() - 1), [&](int i) {
2727         printResultGroup(resultGroups[i],
2728                          resultGroups[i + 1] - resultGroups[i]);
2729       });
2730       os << ", ";
2731       printResultGroup(resultGroups.back(), numResults - resultGroups.back());
2732 
2733     } else {
2734       printResultGroup(/*resultNo=*/0, /*resultCount=*/numResults);
2735     }
2736 
2737     os << " = ";
2738   }
2739 
2740   // If requested, always print the generic form.
2741   if (!printerFlags.shouldPrintGenericOpForm()) {
2742     // Check to see if this is a known operation. If so, use the registered
2743     // custom printer hook.
2744     if (auto opInfo = op->getRegisteredInfo()) {
2745       opInfo->printAssembly(op, *this, defaultDialectStack.back());
2746       return;
2747     }
2748     // Otherwise try to dispatch to the dialect, if available.
2749     if (Dialect *dialect = op->getDialect()) {
2750       if (auto opPrinter = dialect->getOperationPrinter(op)) {
2751         // Print the op name first.
2752         StringRef name = op->getName().getStringRef();
2753         // Only drop the default dialect prefix when it cannot lead to
2754         // ambiguities.
2755         if (name.count('.') == 1)
2756           name.consume_front((defaultDialectStack.back() + ".").str());
2757         os << name;
2758 
2759         // Print the rest of the op now.
2760         opPrinter(op, *this);
2761         return;
2762       }
2763     }
2764   }
2765 
2766   // Otherwise print with the generic assembly form.
2767   printGenericOp(op, /*printOpName=*/true);
2768 }
2769 
2770 void OperationPrinter::printUsersComment(Operation *op) {
2771   unsigned numResults = op->getNumResults();
2772   if (!numResults && op->getNumOperands()) {
2773     os << " // id: ";
2774     printOperationID(op);
2775   } else if (numResults && op->use_empty()) {
2776     os << " // unused";
2777   } else if (numResults && !op->use_empty()) {
2778     // Print "user" if the operation has one result used to compute one other
2779     // result, or is used in one operation with no result.
2780     unsigned usedInNResults = 0;
2781     unsigned usedInNOperations = 0;
2782     SmallPtrSet<Operation *, 1> userSet;
2783     for (Operation *user : op->getUsers()) {
2784       if (userSet.insert(user).second) {
2785         ++usedInNOperations;
2786         usedInNResults += user->getNumResults();
2787       }
2788     }
2789 
2790     // We already know that users is not empty.
2791     bool exactlyOneUniqueUse =
2792         usedInNResults <= 1 && usedInNOperations <= 1 && numResults == 1;
2793     os << " // " << (exactlyOneUniqueUse ? "user" : "users") << ": ";
2794     bool shouldPrintBrackets = numResults > 1;
2795     auto printOpResult = [&](OpResult opResult) {
2796       if (shouldPrintBrackets)
2797         os << "(";
2798       printValueUsers(opResult);
2799       if (shouldPrintBrackets)
2800         os << ")";
2801     };
2802 
2803     interleaveComma(op->getResults(), printOpResult);
2804   }
2805 }
2806 
2807 void OperationPrinter::printUsersComment(BlockArgument arg) {
2808   os << "// ";
2809   printValueID(arg);
2810   if (arg.use_empty()) {
2811     os << " is unused";
2812   } else {
2813     os << " is used by ";
2814     printValueUsers(arg);
2815   }
2816   os << newLine;
2817 }
2818 
2819 void OperationPrinter::printValueUsers(Value value) {
2820   if (value.use_empty())
2821     os << "unused";
2822 
2823   // One value might be used as the operand of an operation more than once.
2824   // Only print the operations results once in that case.
2825   SmallPtrSet<Operation *, 1> userSet;
2826   for (auto &indexedUser : enumerate(value.getUsers())) {
2827     if (userSet.insert(indexedUser.value()).second)
2828       printUserIDs(indexedUser.value(), indexedUser.index());
2829   }
2830 }
2831 
2832 void OperationPrinter::printUserIDs(Operation *user, bool prefixComma) {
2833   if (prefixComma)
2834     os << ", ";
2835 
2836   if (!user->getNumResults()) {
2837     printOperationID(user);
2838   } else {
2839     interleaveComma(user->getResults(),
2840                     [this](Value result) { printValueID(result); });
2841   }
2842 }
2843 
2844 void OperationPrinter::printGenericOp(Operation *op, bool printOpName) {
2845   if (printOpName)
2846     printEscapedString(op->getName().getStringRef());
2847   os << '(';
2848   interleaveComma(op->getOperands(), [&](Value value) { printValueID(value); });
2849   os << ')';
2850 
2851   // For terminators, print the list of successors and their operands.
2852   if (op->getNumSuccessors() != 0) {
2853     os << '[';
2854     interleaveComma(op->getSuccessors(),
2855                     [&](Block *successor) { printBlockName(successor); });
2856     os << ']';
2857   }
2858 
2859   // Print regions.
2860   if (op->getNumRegions() != 0) {
2861     os << " (";
2862     interleaveComma(op->getRegions(), [&](Region &region) {
2863       printRegion(region, /*printEntryBlockArgs=*/true,
2864                   /*printBlockTerminators=*/true, /*printEmptyBlock=*/true);
2865     });
2866     os << ')';
2867   }
2868 
2869   auto attrs = op->getAttrs();
2870   printOptionalAttrDict(attrs);
2871 
2872   // Print the type signature of the operation.
2873   os << " : ";
2874   printFunctionalType(op);
2875 }
2876 
2877 void OperationPrinter::printBlockName(Block *block) {
2878   os << state->getSSANameState().getBlockInfo(block).name;
2879 }
2880 
2881 void OperationPrinter::print(Block *block, bool printBlockArgs,
2882                              bool printBlockTerminator) {
2883   // Print the block label and argument list if requested.
2884   if (printBlockArgs) {
2885     os.indent(currentIndent);
2886     printBlockName(block);
2887 
2888     // Print the argument list if non-empty.
2889     if (!block->args_empty()) {
2890       os << '(';
2891       interleaveComma(block->getArguments(), [&](BlockArgument arg) {
2892         printValueID(arg);
2893         os << ": ";
2894         printType(arg.getType());
2895         // TODO: We should allow location aliases on block arguments.
2896         printTrailingLocation(arg.getLoc(), /*allowAlias*/ false);
2897       });
2898       os << ')';
2899     }
2900     os << ':';
2901 
2902     // Print out some context information about the predecessors of this block.
2903     if (!block->getParent()) {
2904       os << "  // block is not in a region!";
2905     } else if (block->hasNoPredecessors()) {
2906       if (!block->isEntryBlock())
2907         os << "  // no predecessors";
2908     } else if (auto *pred = block->getSinglePredecessor()) {
2909       os << "  // pred: ";
2910       printBlockName(pred);
2911     } else {
2912       // We want to print the predecessors in a stable order, not in
2913       // whatever order the use-list is in, so gather and sort them.
2914       SmallVector<BlockInfo, 4> predIDs;
2915       for (auto *pred : block->getPredecessors())
2916         predIDs.push_back(state->getSSANameState().getBlockInfo(pred));
2917       llvm::sort(predIDs, [](BlockInfo lhs, BlockInfo rhs) {
2918         return lhs.ordering < rhs.ordering;
2919       });
2920 
2921       os << "  // " << predIDs.size() << " preds: ";
2922 
2923       interleaveComma(predIDs, [&](BlockInfo pred) { os << pred.name; });
2924     }
2925     os << newLine;
2926   }
2927 
2928   currentIndent += indentWidth;
2929 
2930   if (printerFlags.shouldPrintValueUsers()) {
2931     for (BlockArgument arg : block->getArguments()) {
2932       os.indent(currentIndent);
2933       printUsersComment(arg);
2934     }
2935   }
2936 
2937   bool hasTerminator =
2938       !block->empty() && block->back().hasTrait<OpTrait::IsTerminator>();
2939   auto range = llvm::make_range(
2940       block->begin(),
2941       std::prev(block->end(),
2942                 (!hasTerminator || printBlockTerminator) ? 0 : 1));
2943   for (auto &op : range) {
2944     print(&op);
2945     os << newLine;
2946   }
2947   currentIndent -= indentWidth;
2948 }
2949 
2950 void OperationPrinter::printValueID(Value value, bool printResultNo,
2951                                     raw_ostream *streamOverride) const {
2952   state->getSSANameState().printValueID(value, printResultNo,
2953                                         streamOverride ? *streamOverride : os);
2954 }
2955 
2956 void OperationPrinter::printOperationID(Operation *op,
2957                                         raw_ostream *streamOverride) const {
2958   state->getSSANameState().printOperationID(op, streamOverride ? *streamOverride
2959                                                                : os);
2960 }
2961 
2962 void OperationPrinter::printSuccessor(Block *successor) {
2963   printBlockName(successor);
2964 }
2965 
2966 void OperationPrinter::printSuccessorAndUseList(Block *successor,
2967                                                 ValueRange succOperands) {
2968   printBlockName(successor);
2969   if (succOperands.empty())
2970     return;
2971 
2972   os << '(';
2973   interleaveComma(succOperands,
2974                   [this](Value operand) { printValueID(operand); });
2975   os << " : ";
2976   interleaveComma(succOperands,
2977                   [this](Value operand) { printType(operand.getType()); });
2978   os << ')';
2979 }
2980 
2981 void OperationPrinter::printRegion(Region &region, bool printEntryBlockArgs,
2982                                    bool printBlockTerminators,
2983                                    bool printEmptyBlock) {
2984   os << "{" << newLine;
2985   if (!region.empty()) {
2986     auto restoreDefaultDialect =
2987         llvm::make_scope_exit([&]() { defaultDialectStack.pop_back(); });
2988     if (auto iface = dyn_cast<OpAsmOpInterface>(region.getParentOp()))
2989       defaultDialectStack.push_back(iface.getDefaultDialect());
2990     else
2991       defaultDialectStack.push_back("");
2992 
2993     auto *entryBlock = &region.front();
2994     // Force printing the block header if printEmptyBlock is set and the block
2995     // is empty or if printEntryBlockArgs is set and there are arguments to
2996     // print.
2997     bool shouldAlwaysPrintBlockHeader =
2998         (printEmptyBlock && entryBlock->empty()) ||
2999         (printEntryBlockArgs && entryBlock->getNumArguments() != 0);
3000     print(entryBlock, shouldAlwaysPrintBlockHeader, printBlockTerminators);
3001     for (auto &b : llvm::drop_begin(region.getBlocks(), 1))
3002       print(&b);
3003   }
3004   os.indent(currentIndent) << "}";
3005 }
3006 
3007 void OperationPrinter::printAffineMapOfSSAIds(AffineMapAttr mapAttr,
3008                                               ValueRange operands) {
3009   AffineMap map = mapAttr.getValue();
3010   unsigned numDims = map.getNumDims();
3011   auto printValueName = [&](unsigned pos, bool isSymbol) {
3012     unsigned index = isSymbol ? numDims + pos : pos;
3013     assert(index < operands.size());
3014     if (isSymbol)
3015       os << "symbol(";
3016     printValueID(operands[index]);
3017     if (isSymbol)
3018       os << ')';
3019   };
3020 
3021   interleaveComma(map.getResults(), [&](AffineExpr expr) {
3022     printAffineExpr(expr, printValueName);
3023   });
3024 }
3025 
3026 void OperationPrinter::printAffineExprOfSSAIds(AffineExpr expr,
3027                                                ValueRange dimOperands,
3028                                                ValueRange symOperands) {
3029   auto printValueName = [&](unsigned pos, bool isSymbol) {
3030     if (!isSymbol)
3031       return printValueID(dimOperands[pos]);
3032     os << "symbol(";
3033     printValueID(symOperands[pos]);
3034     os << ')';
3035   };
3036   printAffineExpr(expr, printValueName);
3037 }
3038 
3039 //===----------------------------------------------------------------------===//
3040 // print and dump methods
3041 //===----------------------------------------------------------------------===//
3042 
3043 void Attribute::print(raw_ostream &os) const {
3044   AsmPrinter::Impl(os).printAttribute(*this);
3045 }
3046 
3047 void Attribute::dump() const {
3048   print(llvm::errs());
3049   llvm::errs() << "\n";
3050 }
3051 
3052 void Type::print(raw_ostream &os) const {
3053   AsmPrinter::Impl(os).printType(*this);
3054 }
3055 
3056 void Type::dump() const { print(llvm::errs()); }
3057 
3058 void AffineMap::dump() const {
3059   print(llvm::errs());
3060   llvm::errs() << "\n";
3061 }
3062 
3063 void IntegerSet::dump() const {
3064   print(llvm::errs());
3065   llvm::errs() << "\n";
3066 }
3067 
3068 void AffineExpr::print(raw_ostream &os) const {
3069   if (!expr) {
3070     os << "<<NULL AFFINE EXPR>>";
3071     return;
3072   }
3073   AsmPrinter::Impl(os).printAffineExpr(*this);
3074 }
3075 
3076 void AffineExpr::dump() const {
3077   print(llvm::errs());
3078   llvm::errs() << "\n";
3079 }
3080 
3081 void AffineMap::print(raw_ostream &os) const {
3082   if (!map) {
3083     os << "<<NULL AFFINE MAP>>";
3084     return;
3085   }
3086   AsmPrinter::Impl(os).printAffineMap(*this);
3087 }
3088 
3089 void IntegerSet::print(raw_ostream &os) const {
3090   AsmPrinter::Impl(os).printIntegerSet(*this);
3091 }
3092 
3093 void Value::print(raw_ostream &os) { print(os, OpPrintingFlags()); }
3094 void Value::print(raw_ostream &os, const OpPrintingFlags &flags) {
3095   if (!impl) {
3096     os << "<<NULL VALUE>>";
3097     return;
3098   }
3099 
3100   if (auto *op = getDefiningOp())
3101     return op->print(os, flags);
3102   // TODO: Improve BlockArgument print'ing.
3103   BlockArgument arg = this->cast<BlockArgument>();
3104   os << "<block argument> of type '" << arg.getType()
3105      << "' at index: " << arg.getArgNumber();
3106 }
3107 void Value::print(raw_ostream &os, AsmState &state) {
3108   if (!impl) {
3109     os << "<<NULL VALUE>>";
3110     return;
3111   }
3112 
3113   if (auto *op = getDefiningOp())
3114     return op->print(os, state);
3115 
3116   // TODO: Improve BlockArgument print'ing.
3117   BlockArgument arg = this->cast<BlockArgument>();
3118   os << "<block argument> of type '" << arg.getType()
3119      << "' at index: " << arg.getArgNumber();
3120 }
3121 
3122 void Value::dump() {
3123   print(llvm::errs());
3124   llvm::errs() << "\n";
3125 }
3126 
3127 void Value::printAsOperand(raw_ostream &os, AsmState &state) {
3128   // TODO: This doesn't necessarily capture all potential cases.
3129   // Currently, region arguments can be shadowed when printing the main
3130   // operation. If the IR hasn't been printed, this will produce the old SSA
3131   // name and not the shadowed name.
3132   state.getImpl().getSSANameState().printValueID(*this, /*printResultNo=*/true,
3133                                                  os);
3134 }
3135 
3136 void Operation::print(raw_ostream &os, const OpPrintingFlags &printerFlags) {
3137   // If this is a top level operation, we also print aliases.
3138   if (!getParent() && !printerFlags.shouldUseLocalScope()) {
3139     AsmState state(this, printerFlags);
3140     state.getImpl().initializeAliases(this);
3141     print(os, state);
3142     return;
3143   }
3144 
3145   // Find the operation to number from based upon the provided flags.
3146   Operation *op = this;
3147   bool shouldUseLocalScope = printerFlags.shouldUseLocalScope();
3148   do {
3149     // If we are printing local scope, stop at the first operation that is
3150     // isolated from above.
3151     if (shouldUseLocalScope && op->hasTrait<OpTrait::IsIsolatedFromAbove>())
3152       break;
3153 
3154     // Otherwise, traverse up to the next parent.
3155     Operation *parentOp = op->getParentOp();
3156     if (!parentOp)
3157       break;
3158     op = parentOp;
3159   } while (true);
3160 
3161   AsmState state(op, printerFlags);
3162   print(os, state);
3163 }
3164 void Operation::print(raw_ostream &os, AsmState &state) {
3165   OperationPrinter printer(os, state.getImpl());
3166   if (!getParent() && !state.getPrinterFlags().shouldUseLocalScope())
3167     printer.printTopLevelOperation(this);
3168   else
3169     printer.print(this);
3170 }
3171 
3172 void Operation::dump() {
3173   print(llvm::errs(), OpPrintingFlags().useLocalScope());
3174   llvm::errs() << "\n";
3175 }
3176 
3177 void Block::print(raw_ostream &os) {
3178   Operation *parentOp = getParentOp();
3179   if (!parentOp) {
3180     os << "<<UNLINKED BLOCK>>\n";
3181     return;
3182   }
3183   // Get the top-level op.
3184   while (auto *nextOp = parentOp->getParentOp())
3185     parentOp = nextOp;
3186 
3187   AsmState state(parentOp);
3188   print(os, state);
3189 }
3190 void Block::print(raw_ostream &os, AsmState &state) {
3191   OperationPrinter(os, state.getImpl()).print(this);
3192 }
3193 
3194 void Block::dump() { print(llvm::errs()); }
3195 
3196 /// Print out the name of the block without printing its body.
3197 void Block::printAsOperand(raw_ostream &os, bool printType) {
3198   Operation *parentOp = getParentOp();
3199   if (!parentOp) {
3200     os << "<<UNLINKED BLOCK>>\n";
3201     return;
3202   }
3203   AsmState state(parentOp);
3204   printAsOperand(os, state);
3205 }
3206 void Block::printAsOperand(raw_ostream &os, AsmState &state) {
3207   OperationPrinter printer(os, state.getImpl());
3208   printer.printBlockName(this);
3209 }
3210