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