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