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