| 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/BuiltinAttributes.h" |
| 20 | #include "mlir/IR/BuiltinDialect.h" |
| 21 | #include "mlir/IR/BuiltinTypeInterfaces.h" |
| 22 | #include "mlir/IR/BuiltinTypes.h" |
| 23 | #include "mlir/IR/Dialect.h" |
| 24 | #include "mlir/IR/DialectImplementation.h" |
| 25 | #include "mlir/IR/DialectResourceBlobManager.h" |
| 26 | #include "mlir/IR/IntegerSet.h" |
| 27 | #include "mlir/IR/MLIRContext.h" |
| 28 | #include "mlir/IR/OpImplementation.h" |
| 29 | #include "mlir/IR/Operation.h" |
| 30 | #include "mlir/IR/Verifier.h" |
| 31 | #include "llvm/ADT/APFloat.h" |
| 32 | #include "llvm/ADT/ArrayRef.h" |
| 33 | #include "llvm/ADT/DenseMap.h" |
| 34 | #include "llvm/ADT/MapVector.h" |
| 35 | #include "llvm/ADT/STLExtras.h" |
| 36 | #include "llvm/ADT/ScopeExit.h" |
| 37 | #include "llvm/ADT/ScopedHashTable.h" |
| 38 | #include "llvm/ADT/SetVector.h" |
| 39 | #include "llvm/ADT/StringExtras.h" |
| 40 | #include "llvm/ADT/StringSet.h" |
| 41 | #include "llvm/ADT/TypeSwitch.h" |
| 42 | #include "llvm/Support/CommandLine.h" |
| 43 | #include "llvm/Support/DebugLog.h" |
| 44 | #include "llvm/Support/Endian.h" |
| 45 | #include "llvm/Support/ManagedStatic.h" |
| 46 | #include "llvm/Support/Regex.h" |
| 47 | #include "llvm/Support/SaveAndRestore.h" |
| 48 | #include "llvm/Support/Threading.h" |
| 49 | #include "llvm/Support/raw_ostream.h" |
| 50 | #include <type_traits> |
| 51 | |
| 52 | #include <optional> |
| 53 | #include <tuple> |
| 54 | |
| 55 | using namespace mlir; |
| 56 | using namespace mlir::detail; |
| 57 | |
| 58 | #define DEBUG_TYPE "mlir-asm-printer" |
| 59 | |
| 60 | void OperationName::print(raw_ostream &os) const { os << getStringRef(); } |
| 61 | |
| 62 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 63 | void OperationName::dump() const { print(llvm::errs()); } |
| 64 | #endif |
| 65 | |
| 66 | //===--------------------------------------------------------------------===// |
| 67 | // AsmParser |
| 68 | //===--------------------------------------------------------------------===// |
| 69 | |
| 70 | AsmParser::~AsmParser() = default; |
| 71 | DialectAsmParser::~DialectAsmParser() = default; |
| 72 | OpAsmParser::~OpAsmParser() = default; |
| 73 | |
| 74 | MLIRContext *AsmParser::getContext() const { return getBuilder().getContext(); } |
| 75 | |
| 76 | /// Parse a type list. |
| 77 | /// This is out-of-line to work-around |
| 78 | /// https://github.com/llvm/llvm-project/issues/62918 |
| 79 | ParseResult AsmParser::parseTypeList(SmallVectorImpl<Type> &result) { |
| 80 | return parseCommaSeparatedList( |
| 81 | parseElementFn: [&]() { return parseType(result&: result.emplace_back()); }); |
| 82 | } |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // DialectAsmPrinter |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | DialectAsmPrinter::~DialectAsmPrinter() = default; |
| 89 | |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | // OpAsmPrinter |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | |
| 94 | OpAsmPrinter::~OpAsmPrinter() = default; |
| 95 | |
| 96 | void OpAsmPrinter::printOperands(OperandRange operands) { |
| 97 | llvm::interleaveComma(c: operands, os&: getStream(), |
| 98 | each_fn: [this](Value value) { printOperand(value); }); |
| 99 | } |
| 100 | |
| 101 | void OpAsmPrinter::printOperandTypes(ValueTypeRange<OperandRange> types) { |
| 102 | llvm::interleaveComma(c: types, os&: *this); |
| 103 | } |
| 104 | |
| 105 | void OpAsmPrinter::printFunctionalType(Operation *op) { |
| 106 | auto &os = getStream(); |
| 107 | os << '('; |
| 108 | llvm::interleaveComma(c: op->getOperands(), os, each_fn: [&](Value operand) { |
| 109 | // Print the types of null values as <<NULL TYPE>>. |
| 110 | *this << (operand ? operand.getType() : Type()); |
| 111 | }); |
| 112 | os << ") -> " ; |
| 113 | |
| 114 | // Print the result list. We don't parenthesize single result types unless |
| 115 | // it is a function (avoiding a grammar ambiguity). |
| 116 | bool wrapped = op->getNumResults() != 1; |
| 117 | if (!wrapped && op->getResult(idx: 0).getType() && |
| 118 | isa<FunctionType>(Val: op->getResult(idx: 0).getType())) |
| 119 | wrapped = true; |
| 120 | |
| 121 | if (wrapped) |
| 122 | os << '('; |
| 123 | |
| 124 | llvm::interleaveComma(c: op->getResults(), os, each_fn: [&](const OpResult &result) { |
| 125 | // Print the types of null values as <<NULL TYPE>>. |
| 126 | *this << (result ? result.getType() : Type()); |
| 127 | }); |
| 128 | |
| 129 | if (wrapped) |
| 130 | os << ')'; |
| 131 | } |
| 132 | |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | // Operation OpAsm interface. |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | |
| 137 | /// The OpAsmOpInterface, see OpAsmInterface.td for more details. |
| 138 | #include "mlir/IR/OpAsmAttrInterface.cpp.inc" |
| 139 | #include "mlir/IR/OpAsmOpInterface.cpp.inc" |
| 140 | #include "mlir/IR/OpAsmTypeInterface.cpp.inc" |
| 141 | |
| 142 | LogicalResult |
| 143 | OpAsmDialectInterface::parseResource(AsmParsedResourceEntry &entry) const { |
| 144 | return entry.emitError() << "unknown 'resource' key '" << entry.getKey() |
| 145 | << "' for dialect '" << getDialect()->getNamespace() |
| 146 | << "'" ; |
| 147 | } |
| 148 | |
| 149 | //===----------------------------------------------------------------------===// |
| 150 | // OpPrintingFlags |
| 151 | //===----------------------------------------------------------------------===// |
| 152 | |
| 153 | namespace { |
| 154 | /// This struct contains command line options that can be used to initialize |
| 155 | /// various bits of the AsmPrinter. This uses a struct wrapper to avoid the need |
| 156 | /// for global command line options. |
| 157 | struct AsmPrinterOptions { |
| 158 | llvm::cl::opt<int64_t> printElementsAttrWithHexIfLarger{ |
| 159 | "mlir-print-elementsattrs-with-hex-if-larger" , |
| 160 | llvm::cl::desc( |
| 161 | "Print DenseElementsAttrs with a hex string that have " |
| 162 | "more elements than the given upper limit (use -1 to disable)" )}; |
| 163 | |
| 164 | llvm::cl::opt<unsigned> elideElementsAttrIfLarger{ |
| 165 | "mlir-elide-elementsattrs-if-larger" , |
| 166 | llvm::cl::desc("Elide ElementsAttrs with \"...\" that have " |
| 167 | "more elements than the given upper limit" )}; |
| 168 | |
| 169 | llvm::cl::opt<unsigned> elideResourceStringsIfLarger{ |
| 170 | "mlir-elide-resource-strings-if-larger" , |
| 171 | llvm::cl::desc( |
| 172 | "Elide printing value of resources if string is too long in chars." )}; |
| 173 | |
| 174 | llvm::cl::opt<bool> printDebugInfoOpt{ |
| 175 | "mlir-print-debuginfo" , llvm::cl::init(Val: false), |
| 176 | llvm::cl::desc("Print debug info in MLIR output" )}; |
| 177 | |
| 178 | llvm::cl::opt<bool> printPrettyDebugInfoOpt{ |
| 179 | "mlir-pretty-debuginfo" , llvm::cl::init(Val: false), |
| 180 | llvm::cl::desc("Print pretty debug info in MLIR output" )}; |
| 181 | |
| 182 | // Use the generic op output form in the operation printer even if the custom |
| 183 | // form is defined. |
| 184 | llvm::cl::opt<bool> printGenericOpFormOpt{ |
| 185 | "mlir-print-op-generic" , llvm::cl::init(Val: false), |
| 186 | llvm::cl::desc("Print the generic op form" ), llvm::cl::Hidden}; |
| 187 | |
| 188 | llvm::cl::opt<bool> assumeVerifiedOpt{ |
| 189 | "mlir-print-assume-verified" , llvm::cl::init(Val: false), |
| 190 | llvm::cl::desc("Skip op verification when using custom printers" ), |
| 191 | llvm::cl::Hidden}; |
| 192 | |
| 193 | llvm::cl::opt<bool> printLocalScopeOpt{ |
| 194 | "mlir-print-local-scope" , llvm::cl::init(Val: false), |
| 195 | llvm::cl::desc("Print with local scope and inline information (eliding " |
| 196 | "aliases for attributes, types, and locations)" )}; |
| 197 | |
| 198 | llvm::cl::opt<bool> skipRegionsOpt{ |
| 199 | "mlir-print-skip-regions" , llvm::cl::init(Val: false), |
| 200 | llvm::cl::desc("Skip regions when printing ops." )}; |
| 201 | |
| 202 | llvm::cl::opt<bool> printValueUsers{ |
| 203 | "mlir-print-value-users" , llvm::cl::init(Val: false), |
| 204 | llvm::cl::desc( |
| 205 | "Print users of operation results and block arguments as a comment" )}; |
| 206 | |
| 207 | llvm::cl::opt<bool> printUniqueSSAIDs{ |
| 208 | "mlir-print-unique-ssa-ids" , llvm::cl::init(Val: false), |
| 209 | llvm::cl::desc("Print unique SSA ID numbers for values, block arguments " |
| 210 | "and naming conflicts across all regions" )}; |
| 211 | |
| 212 | llvm::cl::opt<bool> useNameLocAsPrefix{ |
| 213 | "mlir-use-nameloc-as-prefix" , llvm::cl::init(Val: false), |
| 214 | llvm::cl::desc("Print SSA IDs using NameLocs as prefixes" )}; |
| 215 | |
| 216 | llvm::cl::opt<bool> alwaysUseAliasesOpt{ |
| 217 | "mlir-print-always-use-aliases" , llvm::cl::init(Val: false), |
| 218 | llvm::cl::desc("Always use aliases even for non top-level ops" )}; |
| 219 | }; |
| 220 | } // namespace |
| 221 | |
| 222 | static llvm::ManagedStatic<AsmPrinterOptions> clOptions; |
| 223 | |
| 224 | /// Register a set of useful command-line options that can be used to configure |
| 225 | /// various flags within the AsmPrinter. |
| 226 | void mlir::registerAsmPrinterCLOptions() { |
| 227 | // Make sure that the options struct has been initialized. |
| 228 | *clOptions; |
| 229 | } |
| 230 | |
| 231 | /// Initialize the printing flags with default supplied by the cl::opts above. |
| 232 | OpPrintingFlags::OpPrintingFlags() |
| 233 | : printDebugInfoFlag(false), printDebugInfoPrettyFormFlag(false), |
| 234 | printGenericOpFormFlag(false), skipRegionsFlag(false), |
| 235 | assumeVerifiedFlag(false), alwaysUseAliasesFlag(false), |
| 236 | printLocalScope(false), printValueUsersFlag(false), |
| 237 | printUniqueSSAIDsFlag(false), useNameLocAsPrefix(false) { |
| 238 | // Initialize based upon command line options, if they are available. |
| 239 | if (!clOptions.isConstructed()) |
| 240 | return; |
| 241 | if (clOptions->elideElementsAttrIfLarger.getNumOccurrences()) |
| 242 | elementsAttrElementLimit = clOptions->elideElementsAttrIfLarger; |
| 243 | if (clOptions->printElementsAttrWithHexIfLarger.getNumOccurrences()) |
| 244 | elementsAttrHexElementLimit = |
| 245 | clOptions->printElementsAttrWithHexIfLarger.getValue(); |
| 246 | if (clOptions->elideResourceStringsIfLarger.getNumOccurrences()) |
| 247 | resourceStringCharLimit = clOptions->elideResourceStringsIfLarger; |
| 248 | printDebugInfoFlag = clOptions->printDebugInfoOpt; |
| 249 | printDebugInfoPrettyFormFlag = clOptions->printPrettyDebugInfoOpt; |
| 250 | printGenericOpFormFlag = clOptions->printGenericOpFormOpt; |
| 251 | assumeVerifiedFlag = clOptions->assumeVerifiedOpt; |
| 252 | printLocalScope = clOptions->printLocalScopeOpt; |
| 253 | skipRegionsFlag = clOptions->skipRegionsOpt; |
| 254 | printValueUsersFlag = clOptions->printValueUsers; |
| 255 | printUniqueSSAIDsFlag = clOptions->printUniqueSSAIDs; |
| 256 | useNameLocAsPrefix = clOptions->useNameLocAsPrefix; |
| 257 | alwaysUseAliasesFlag = clOptions->alwaysUseAliasesOpt; |
| 258 | } |
| 259 | |
| 260 | /// Enable the elision of large elements attributes, by printing a '...' |
| 261 | /// instead of the element data, when the number of elements is greater than |
| 262 | /// `largeElementLimit`. Note: The IR generated with this option is not |
| 263 | /// parsable. |
| 264 | OpPrintingFlags & |
| 265 | OpPrintingFlags::elideLargeElementsAttrs(int64_t largeElementLimit) { |
| 266 | elementsAttrElementLimit = largeElementLimit; |
| 267 | return *this; |
| 268 | } |
| 269 | |
| 270 | OpPrintingFlags & |
| 271 | OpPrintingFlags::printLargeElementsAttrWithHex(int64_t largeElementLimit) { |
| 272 | elementsAttrHexElementLimit = largeElementLimit; |
| 273 | return *this; |
| 274 | } |
| 275 | |
| 276 | OpPrintingFlags & |
| 277 | OpPrintingFlags::elideLargeResourceString(int64_t largeResourceLimit) { |
| 278 | resourceStringCharLimit = largeResourceLimit; |
| 279 | return *this; |
| 280 | } |
| 281 | |
| 282 | /// Enable printing of debug information. If 'prettyForm' is set to true, |
| 283 | /// debug information is printed in a more readable 'pretty' form. |
| 284 | OpPrintingFlags &OpPrintingFlags::enableDebugInfo(bool enable, |
| 285 | bool prettyForm) { |
| 286 | printDebugInfoFlag = enable; |
| 287 | printDebugInfoPrettyFormFlag = prettyForm; |
| 288 | return *this; |
| 289 | } |
| 290 | |
| 291 | /// Always print operations in the generic form. |
| 292 | OpPrintingFlags &OpPrintingFlags::printGenericOpForm(bool enable) { |
| 293 | printGenericOpFormFlag = enable; |
| 294 | return *this; |
| 295 | } |
| 296 | |
| 297 | /// Always skip Regions. |
| 298 | OpPrintingFlags &OpPrintingFlags::skipRegions(bool skip) { |
| 299 | skipRegionsFlag = skip; |
| 300 | return *this; |
| 301 | } |
| 302 | |
| 303 | /// Do not verify the operation when using custom operation printers. |
| 304 | OpPrintingFlags &OpPrintingFlags::assumeVerified(bool enable) { |
| 305 | assumeVerifiedFlag = enable; |
| 306 | return *this; |
| 307 | } |
| 308 | |
| 309 | OpPrintingFlags &OpPrintingFlags::alwaysUseAliases(bool enable) { |
| 310 | alwaysUseAliasesFlag = enable; |
| 311 | return *this; |
| 312 | } |
| 313 | |
| 314 | /// Use local scope when printing the operation. This allows for using the |
| 315 | /// printer in a more localized and thread-safe setting, but may not necessarily |
| 316 | /// be identical of what the IR will look like when dumping the full module. |
| 317 | OpPrintingFlags &OpPrintingFlags::useLocalScope(bool enable) { |
| 318 | printLocalScope = enable; |
| 319 | return *this; |
| 320 | } |
| 321 | |
| 322 | /// Print users of values as comments. |
| 323 | OpPrintingFlags &OpPrintingFlags::printValueUsers(bool enable) { |
| 324 | printValueUsersFlag = enable; |
| 325 | return *this; |
| 326 | } |
| 327 | |
| 328 | /// Print unique SSA ID numbers for values, block arguments and naming conflicts |
| 329 | /// across all regions |
| 330 | OpPrintingFlags &OpPrintingFlags::printUniqueSSAIDs(bool enable) { |
| 331 | printUniqueSSAIDsFlag = enable; |
| 332 | return *this; |
| 333 | } |
| 334 | |
| 335 | /// Return if the given ElementsAttr should be elided. |
| 336 | bool OpPrintingFlags::shouldElideElementsAttr(ElementsAttr attr) const { |
| 337 | return elementsAttrElementLimit && |
| 338 | *elementsAttrElementLimit < int64_t(attr.getNumElements()) && |
| 339 | !llvm::isa<SplatElementsAttr>(Val: attr); |
| 340 | } |
| 341 | |
| 342 | /// Return if the given ElementsAttr should be printed as hex string. |
| 343 | bool OpPrintingFlags::shouldPrintElementsAttrWithHex(ElementsAttr attr) const { |
| 344 | // -1 is used to disable hex printing. |
| 345 | return (elementsAttrHexElementLimit != -1) && |
| 346 | (elementsAttrHexElementLimit < int64_t(attr.getNumElements())) && |
| 347 | !llvm::isa<SplatElementsAttr>(Val: attr); |
| 348 | } |
| 349 | |
| 350 | OpPrintingFlags &OpPrintingFlags::printNameLocAsPrefix(bool enable) { |
| 351 | useNameLocAsPrefix = enable; |
| 352 | return *this; |
| 353 | } |
| 354 | |
| 355 | /// Return the size limit for printing large ElementsAttr. |
| 356 | std::optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const { |
| 357 | return elementsAttrElementLimit; |
| 358 | } |
| 359 | |
| 360 | /// Return the size limit for printing large ElementsAttr as hex string. |
| 361 | int64_t OpPrintingFlags::getLargeElementsAttrHexLimit() const { |
| 362 | return elementsAttrHexElementLimit; |
| 363 | } |
| 364 | |
| 365 | /// Return the size limit for printing large ElementsAttr. |
| 366 | std::optional<uint64_t> OpPrintingFlags::getLargeResourceStringLimit() const { |
| 367 | return resourceStringCharLimit; |
| 368 | } |
| 369 | |
| 370 | /// Return if debug information should be printed. |
| 371 | bool OpPrintingFlags::shouldPrintDebugInfo() const { |
| 372 | return printDebugInfoFlag; |
| 373 | } |
| 374 | |
| 375 | /// Return if debug information should be printed in the pretty form. |
| 376 | bool OpPrintingFlags::shouldPrintDebugInfoPrettyForm() const { |
| 377 | return printDebugInfoPrettyFormFlag; |
| 378 | } |
| 379 | |
| 380 | /// Return if operations should be printed in the generic form. |
| 381 | bool OpPrintingFlags::shouldPrintGenericOpForm() const { |
| 382 | return printGenericOpFormFlag; |
| 383 | } |
| 384 | |
| 385 | /// Return if Region should be skipped. |
| 386 | bool OpPrintingFlags::shouldSkipRegions() const { return skipRegionsFlag; } |
| 387 | |
| 388 | /// Return if operation verification should be skipped. |
| 389 | bool OpPrintingFlags::shouldAssumeVerified() const { |
| 390 | return assumeVerifiedFlag; |
| 391 | } |
| 392 | |
| 393 | bool OpPrintingFlags::shouldAlwaysUseAliases() const { |
| 394 | return alwaysUseAliasesFlag; |
| 395 | } |
| 396 | |
| 397 | /// Return if the printer should use local scope when dumping the IR. |
| 398 | bool OpPrintingFlags::shouldUseLocalScope() const { return printLocalScope; } |
| 399 | |
| 400 | /// Return if the printer should print users of values. |
| 401 | bool OpPrintingFlags::shouldPrintValueUsers() const { |
| 402 | return printValueUsersFlag; |
| 403 | } |
| 404 | |
| 405 | /// Return if the printer should use unique IDs. |
| 406 | bool OpPrintingFlags::shouldPrintUniqueSSAIDs() const { |
| 407 | return printUniqueSSAIDsFlag || shouldPrintGenericOpForm(); |
| 408 | } |
| 409 | |
| 410 | /// Return if the printer should use NameLocs as prefixes when printing SSA IDs. |
| 411 | bool OpPrintingFlags::shouldUseNameLocAsPrefix() const { |
| 412 | return useNameLocAsPrefix; |
| 413 | } |
| 414 | |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | // NewLineCounter |
| 417 | //===----------------------------------------------------------------------===// |
| 418 | |
| 419 | namespace { |
| 420 | /// This class is a simple formatter that emits a new line when inputted into a |
| 421 | /// stream, that enables counting the number of newlines emitted. This class |
| 422 | /// should be used whenever emitting newlines in the printer. |
| 423 | struct NewLineCounter { |
| 424 | unsigned curLine = 1; |
| 425 | }; |
| 426 | |
| 427 | static raw_ostream &operator<<(raw_ostream &os, NewLineCounter &newLine) { |
| 428 | ++newLine.curLine; |
| 429 | return os << '\n'; |
| 430 | } |
| 431 | } // namespace |
| 432 | |
| 433 | //===----------------------------------------------------------------------===// |
| 434 | // AsmPrinter::Impl |
| 435 | //===----------------------------------------------------------------------===// |
| 436 | |
| 437 | namespace mlir { |
| 438 | class AsmPrinter::Impl { |
| 439 | public: |
| 440 | Impl(raw_ostream &os, AsmStateImpl &state); |
| 441 | explicit Impl(Impl &other) : Impl(other.os, other.state) {} |
| 442 | |
| 443 | /// Returns the output stream of the printer. |
| 444 | raw_ostream &getStream() { return os; } |
| 445 | |
| 446 | /// Print a newline and indent the printer to the start of the current |
| 447 | /// operation/attribute/type. |
| 448 | /// Note: For attributes and types this method should only be used in |
| 449 | /// custom dialects. Usage in MLIR dialects is disallowed. |
| 450 | void printNewline() { |
| 451 | os << newLine; |
| 452 | os.indent(NumSpaces: currentIndent); |
| 453 | } |
| 454 | |
| 455 | /// Increase indentation. |
| 456 | void increaseIndent() { currentIndent += indentWidth; } |
| 457 | |
| 458 | /// Decrease indentation. |
| 459 | void decreaseIndent() { currentIndent -= indentWidth; } |
| 460 | |
| 461 | template <typename Container, typename UnaryFunctor> |
| 462 | inline void interleaveComma(const Container &c, UnaryFunctor eachFn) const { |
| 463 | llvm::interleaveComma(c, os, eachFn); |
| 464 | } |
| 465 | |
| 466 | /// This enum describes the different kinds of elision for the type of an |
| 467 | /// attribute when printing it. |
| 468 | enum class AttrTypeElision { |
| 469 | /// The type must not be elided, |
| 470 | Never, |
| 471 | /// The type may be elided when it matches the default used in the parser |
| 472 | /// (for example i64 is the default for integer attributes). |
| 473 | May, |
| 474 | /// The type must be elided. |
| 475 | Must |
| 476 | }; |
| 477 | |
| 478 | /// Print the given attribute or an alias. |
| 479 | void printAttribute(Attribute attr, |
| 480 | AttrTypeElision typeElision = AttrTypeElision::Never); |
| 481 | /// Print the given attribute without considering an alias. |
| 482 | void printAttributeImpl(Attribute attr, |
| 483 | AttrTypeElision typeElision = AttrTypeElision::Never); |
| 484 | void printNamedAttribute(NamedAttribute attr); |
| 485 | |
| 486 | /// Print the alias for the given attribute, return failure if no alias could |
| 487 | /// be printed. |
| 488 | LogicalResult printAlias(Attribute attr); |
| 489 | |
| 490 | /// Print the given type or an alias. |
| 491 | void printType(Type type); |
| 492 | /// Print the given type. |
| 493 | void printTypeImpl(Type type); |
| 494 | |
| 495 | /// Print the alias for the given type, return failure if no alias could |
| 496 | /// be printed. |
| 497 | LogicalResult printAlias(Type type); |
| 498 | |
| 499 | /// Print the given location to the stream. If `allowAlias` is true, this |
| 500 | /// allows for the internal location to use an attribute alias. |
| 501 | void printLocation(LocationAttr loc, bool allowAlias = false); |
| 502 | |
| 503 | /// Print a reference to the given resource that is owned by the given |
| 504 | /// dialect. |
| 505 | void printResourceHandle(const AsmDialectResourceHandle &resource); |
| 506 | |
| 507 | void printAffineMap(AffineMap map); |
| 508 | void |
| 509 | printAffineExpr(AffineExpr expr, |
| 510 | function_ref<void(unsigned, bool)> printValueName = nullptr); |
| 511 | void printAffineConstraint(AffineExpr expr, bool isEq); |
| 512 | void printIntegerSet(IntegerSet set); |
| 513 | |
| 514 | LogicalResult pushCyclicPrinting(const void *opaquePointer); |
| 515 | |
| 516 | void popCyclicPrinting(); |
| 517 | |
| 518 | void printDimensionList(ArrayRef<int64_t> shape); |
| 519 | |
| 520 | protected: |
| 521 | void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs, |
| 522 | ArrayRef<StringRef> elidedAttrs = {}, |
| 523 | bool withKeyword = false); |
| 524 | void printTrailingLocation(Location loc, bool allowAlias = true); |
| 525 | void printLocationInternal(LocationAttr loc, bool pretty = false, |
| 526 | bool isTopLevel = false); |
| 527 | |
| 528 | /// Print a dense elements attribute. If 'allowHex' is true, a hex string is |
| 529 | /// used instead of individual elements when the elements attr is large. |
| 530 | void printDenseElementsAttr(DenseElementsAttr attr, bool allowHex); |
| 531 | |
| 532 | /// Print a dense string elements attribute. |
| 533 | void printDenseStringElementsAttr(DenseStringElementsAttr attr); |
| 534 | |
| 535 | /// Print a dense elements attribute in the literal-first syntax. If |
| 536 | /// 'allowHex' is true, a hex string is used instead of individual elements |
| 537 | /// when the elements attr is large. |
| 538 | void printDenseTypedElementsAttr(DenseTypedElementsAttr attr, bool allowHex); |
| 539 | |
| 540 | /// Print a dense elements attribute using the type-first syntax and the |
| 541 | /// DenseElementTypeInterface, which provides the attribute printer for each |
| 542 | /// element. |
| 543 | void printTypeFirstDenseElementsAttr(DenseElementsAttr attr, |
| 544 | DenseElementType denseEltType); |
| 545 | |
| 546 | /// Print a dense array attribute. |
| 547 | void printDenseArrayAttr(DenseArrayAttr attr); |
| 548 | |
| 549 | void printDialectAttribute(Attribute attr); |
| 550 | void printDialectType(Type type); |
| 551 | |
| 552 | /// Print an escaped string, wrapped with "". |
| 553 | void printEscapedString(StringRef str); |
| 554 | |
| 555 | /// Print a hex string, wrapped with "". |
| 556 | void printHexString(StringRef str); |
| 557 | void printHexString(ArrayRef<char> data); |
| 558 | |
| 559 | /// This enum is used to represent the binding strength of the enclosing |
| 560 | /// context that an AffineExprStorage is being printed in, so we can |
| 561 | /// intelligently produce parens. |
| 562 | enum class BindingStrength { |
| 563 | Weak, // + and - |
| 564 | Strong, // All other binary operators. |
| 565 | }; |
| 566 | void printAffineExprInternal( |
| 567 | AffineExpr expr, BindingStrength enclosingTightness, |
| 568 | function_ref<void(unsigned, bool)> printValueName = nullptr); |
| 569 | |
| 570 | /// The output stream for the printer. |
| 571 | raw_ostream &os; |
| 572 | |
| 573 | /// An underlying assembly printer state. |
| 574 | AsmStateImpl &state; |
| 575 | |
| 576 | /// A set of flags to control the printer's behavior. |
| 577 | OpPrintingFlags printerFlags; |
| 578 | |
| 579 | /// A tracker for the number of new lines emitted during printing. |
| 580 | NewLineCounter newLine; |
| 581 | |
| 582 | /// The number of spaces used as an indent. |
| 583 | const static unsigned indentWidth = 2; |
| 584 | |
| 585 | /// This is the current indentation level for nested structures. |
| 586 | unsigned currentIndent = 0; |
| 587 | }; |
| 588 | } // namespace mlir |
| 589 | |
| 590 | //===----------------------------------------------------------------------===// |
| 591 | // AliasInitializer |
| 592 | //===----------------------------------------------------------------------===// |
| 593 | |
| 594 | namespace { |
| 595 | /// This class represents a specific instance of a symbol Alias. |
| 596 | class SymbolAlias { |
| 597 | public: |
| 598 | SymbolAlias(StringRef name, uint32_t suffixIndex, bool isType, |
| 599 | bool isDeferrable) |
| 600 | : name(name), suffixIndex(suffixIndex), isType(isType), |
| 601 | isDeferrable(isDeferrable) {} |
| 602 | |
| 603 | /// Print this alias to the given stream. |
| 604 | void print(raw_ostream &os) const { |
| 605 | os << (isType ? "!" : "#" ) << name; |
| 606 | if (suffixIndex) { |
| 607 | if (isdigit(name.back())) |
| 608 | os << '_'; |
| 609 | os << suffixIndex; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /// Returns true if this is a type alias. |
| 614 | bool isTypeAlias() const { return isType; } |
| 615 | |
| 616 | /// Returns true if this alias supports deferred resolution when parsing. |
| 617 | bool canBeDeferred() const { return isDeferrable; } |
| 618 | |
| 619 | private: |
| 620 | /// The main name of the alias. |
| 621 | StringRef name; |
| 622 | /// The suffix index of the alias. |
| 623 | uint32_t suffixIndex : 30; |
| 624 | /// A flag indicating whether this alias is for a type. |
| 625 | bool isType : 1; |
| 626 | /// A flag indicating whether this alias may be deferred or not. |
| 627 | bool isDeferrable : 1; |
| 628 | |
| 629 | public: |
| 630 | /// Used to avoid printing incomplete aliases for recursive types. |
| 631 | bool isPrinted = false; |
| 632 | }; |
| 633 | |
| 634 | /// This class represents a utility that initializes the set of attribute and |
| 635 | /// type aliases, without the need to store the extra information within the |
| 636 | /// main AliasState class or pass it around via function arguments. |
| 637 | class AliasInitializer { |
| 638 | public: |
| 639 | AliasInitializer( |
| 640 | DialectInterfaceCollection<OpAsmDialectInterface> &interfaces, |
| 641 | llvm::BumpPtrAllocator &aliasAllocator) |
| 642 | : interfaces(interfaces), aliasAllocator(aliasAllocator), |
| 643 | aliasOS(aliasBuffer) {} |
| 644 | |
| 645 | void initialize(Operation *op, const OpPrintingFlags &printerFlags, |
| 646 | llvm::MapVector<const void *, SymbolAlias> &attrTypeToAlias); |
| 647 | |
| 648 | /// Visit the given attribute to see if it has an alias. `canBeDeferred` is |
| 649 | /// set to true if the originator of this attribute can resolve the alias |
| 650 | /// after parsing has completed (e.g. in the case of operation locations). |
| 651 | /// `elideType` indicates if the type of the attribute should be skipped when |
| 652 | /// looking for nested aliases. Returns the maximum alias depth of the |
| 653 | /// attribute, and the alias index of this attribute. |
| 654 | std::pair<size_t, size_t> visit(Attribute attr, bool canBeDeferred = false, |
| 655 | bool elideType = false) { |
| 656 | return visitImpl(value: attr, aliases, canBeDeferred, printArgs&: elideType); |
| 657 | } |
| 658 | |
| 659 | /// Visit the given type to see if it has an alias. `canBeDeferred` is |
| 660 | /// set to true if the originator of this attribute can resolve the alias |
| 661 | /// after parsing has completed. Returns the maximum alias depth of the type, |
| 662 | /// and the alias index of this type. |
| 663 | std::pair<size_t, size_t> visit(Type type, bool canBeDeferred = false) { |
| 664 | return visitImpl(value: type, aliases, canBeDeferred); |
| 665 | } |
| 666 | |
| 667 | private: |
| 668 | struct InProgressAliasInfo { |
| 669 | InProgressAliasInfo() |
| 670 | : aliasDepth(0), isType(false), canBeDeferred(false) {} |
| 671 | InProgressAliasInfo(StringRef alias) |
| 672 | : alias(alias), aliasDepth(1), isType(false), canBeDeferred(false) {} |
| 673 | |
| 674 | bool operator<(const InProgressAliasInfo &rhs) const { |
| 675 | // Order first by depth, then by attr/type kind, and then by name. |
| 676 | if (aliasDepth != rhs.aliasDepth) |
| 677 | return aliasDepth < rhs.aliasDepth; |
| 678 | if (isType != rhs.isType) |
| 679 | return isType; |
| 680 | return alias < rhs.alias; |
| 681 | } |
| 682 | |
| 683 | /// The alias for the attribute or type, or std::nullopt if the value has no |
| 684 | /// alias. |
| 685 | std::optional<StringRef> alias; |
| 686 | /// The alias depth of this attribute or type, i.e. an indication of the |
| 687 | /// relative ordering of when to print this alias. |
| 688 | unsigned aliasDepth : 30; |
| 689 | /// If this alias represents a type or an attribute. |
| 690 | bool isType : 1; |
| 691 | /// If this alias can be deferred or not. |
| 692 | bool canBeDeferred : 1; |
| 693 | /// Indices for child aliases. |
| 694 | SmallVector<size_t> childIndices; |
| 695 | }; |
| 696 | |
| 697 | /// Visit the given attribute or type to see if it has an alias. |
| 698 | /// `canBeDeferred` is set to true if the originator of this value can resolve |
| 699 | /// the alias after parsing has completed (e.g. in the case of operation |
| 700 | /// locations). Returns the maximum alias depth of the value, and its alias |
| 701 | /// index. |
| 702 | template <typename T, typename... PrintArgs> |
| 703 | std::pair<size_t, size_t> |
| 704 | visitImpl(T value, |
| 705 | llvm::MapVector<const void *, InProgressAliasInfo> &aliases, |
| 706 | bool canBeDeferred, PrintArgs &&...printArgs); |
| 707 | |
| 708 | /// Mark the given alias as non-deferrable. |
| 709 | void markAliasNonDeferrable(size_t aliasIndex); |
| 710 | |
| 711 | /// Try to generate an alias for the provided symbol. If an alias is |
| 712 | /// generated, the provided alias mapping and reverse mapping are updated. |
| 713 | template <typename T> |
| 714 | void generateAlias(T symbol, InProgressAliasInfo &alias, bool canBeDeferred); |
| 715 | |
| 716 | /// Uniques the given alias name within the printer by generating name index |
| 717 | /// used as alias name suffix. |
| 718 | static unsigned |
| 719 | uniqueAliasNameIndex(StringRef alias, llvm::StringMap<unsigned> &nameCounts, |
| 720 | llvm::StringSet<llvm::BumpPtrAllocator &> &usedAliases); |
| 721 | |
| 722 | /// Given a collection of aliases and symbols, initialize a mapping from a |
| 723 | /// symbol to a given alias. |
| 724 | static void initializeAliases( |
| 725 | llvm::MapVector<const void *, InProgressAliasInfo> &visitedSymbols, |
| 726 | llvm::MapVector<const void *, SymbolAlias> &symbolToAlias); |
| 727 | |
| 728 | /// The set of asm interfaces within the context. |
| 729 | DialectInterfaceCollection<OpAsmDialectInterface> &interfaces; |
| 730 | |
| 731 | /// An allocator used for alias names. |
| 732 | llvm::BumpPtrAllocator &aliasAllocator; |
| 733 | |
| 734 | /// The set of built aliases. |
| 735 | llvm::MapVector<const void *, InProgressAliasInfo> aliases; |
| 736 | |
| 737 | /// Storage and stream used when generating an alias. |
| 738 | SmallString<32> aliasBuffer; |
| 739 | llvm::raw_svector_ostream aliasOS; |
| 740 | }; |
| 741 | |
| 742 | /// This class implements a dummy OpAsmPrinter that doesn't print any output, |
| 743 | /// and merely collects the attributes and types that *would* be printed in a |
| 744 | /// normal print invocation so that we can generate proper aliases. This allows |
| 745 | /// for us to generate aliases only for the attributes and types that would be |
| 746 | /// in the output, and trims down unnecessary output. |
| 747 | class DummyAliasOperationPrinter : private OpAsmPrinter { |
| 748 | public: |
| 749 | explicit DummyAliasOperationPrinter(const OpPrintingFlags &printerFlags, |
| 750 | AliasInitializer &initializer) |
| 751 | : printerFlags(printerFlags), initializer(initializer) {} |
| 752 | |
| 753 | /// Prints the entire operation with the custom assembly form, if available, |
| 754 | /// or the generic assembly form, otherwise. |
| 755 | void printCustomOrGenericOp(Operation *op) override { |
| 756 | // Visit the operation location. |
| 757 | if (printerFlags.shouldPrintDebugInfo()) |
| 758 | initializer.visit(attr: op->getLoc(), /*canBeDeferred=*/true); |
| 759 | |
| 760 | // If requested, always print the generic form. |
| 761 | if (!printerFlags.shouldPrintGenericOpForm()) { |
| 762 | op->getName().printAssembly(op, p&: *this, /*defaultDialect=*/"" ); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | // Otherwise print with the generic assembly form. |
| 767 | printGenericOp(op); |
| 768 | } |
| 769 | |
| 770 | private: |
| 771 | /// Print the given operation in the generic form. |
| 772 | void printGenericOp(Operation *op, bool printOpName = true) override { |
| 773 | // Consider nested operations for aliases. |
| 774 | if (!printerFlags.shouldSkipRegions()) { |
| 775 | for (Region ®ion : op->getRegions()) |
| 776 | printRegion(region, /*printEntryBlockArgs=*/true, |
| 777 | /*printBlockTerminators=*/true); |
| 778 | } |
| 779 | |
| 780 | // Visit all the types used in the operation. Null operands/types can |
| 781 | // occur when operating on invalid IR (e.g., with |
| 782 | // --mlir-very-unsafe-disable-verifier-on-parsing), so guard against them. |
| 783 | for (Value operand : op->getOperands()) |
| 784 | if (operand && operand.getType()) |
| 785 | printType(type: operand.getType()); |
| 786 | for (Type type : op->getResultTypes()) |
| 787 | printType(type); |
| 788 | |
| 789 | // Consider the attributes of the operation for aliases. |
| 790 | for (const NamedAttribute &attr : op->getRawDictionaryAttrs()) |
| 791 | printAttribute(attr: attr.getValue()); |
| 792 | op->getName().walkInherentAttrs( |
| 793 | op, visitor: [&](StringRef, Attribute &attr) { printAttribute(attr); }); |
| 794 | } |
| 795 | |
| 796 | /// Print the given block. If 'printBlockArgs' is false, the arguments of the |
| 797 | /// block are not printed. If 'printBlockTerminator' is false, the terminator |
| 798 | /// operation of the block is not printed. |
| 799 | void print(Block *block, bool printBlockArgs = true, |
| 800 | bool printBlockTerminator = true) { |
| 801 | // Consider the types of the block arguments for aliases if 'printBlockArgs' |
| 802 | // is set to true. |
| 803 | if (printBlockArgs) { |
| 804 | for (BlockArgument arg : block->getArguments()) { |
| 805 | printType(type: arg.getType()); |
| 806 | |
| 807 | // Visit the argument location. |
| 808 | if (printerFlags.shouldPrintDebugInfo()) |
| 809 | // TODO: Allow deferring argument locations. |
| 810 | initializer.visit(attr: arg.getLoc(), /*canBeDeferred=*/false); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | // Consider the operations within this block, ignoring the terminator if |
| 815 | // requested. |
| 816 | bool hasTerminator = |
| 817 | !block->empty() && block->back().hasTrait<OpTrait::IsTerminator>(); |
| 818 | auto range = llvm::make_range( |
| 819 | x: block->begin(), |
| 820 | y: std::prev(x: block->end(), |
| 821 | n: (!hasTerminator || printBlockTerminator) ? 0 : 1)); |
| 822 | for (Operation &op : range) |
| 823 | printCustomOrGenericOp(op: &op); |
| 824 | } |
| 825 | |
| 826 | /// Print the given region. |
| 827 | void printRegion(Region ®ion, bool printEntryBlockArgs, |
| 828 | bool printBlockTerminators, |
| 829 | bool printEmptyBlock = false) override { |
| 830 | if (region.empty()) |
| 831 | return; |
| 832 | if (printerFlags.shouldSkipRegions()) { |
| 833 | os << "{...}" ; |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | auto *entryBlock = ®ion.front(); |
| 838 | print(block: entryBlock, printBlockArgs: printEntryBlockArgs, printBlockTerminator: printBlockTerminators); |
| 839 | for (Block &b : llvm::drop_begin(RangeOrContainer&: region, N: 1)) |
| 840 | print(block: &b); |
| 841 | } |
| 842 | |
| 843 | void printRegionArgument(BlockArgument arg, ArrayRef<NamedAttribute> argAttrs, |
| 844 | bool omitType) override { |
| 845 | printType(type: arg.getType()); |
| 846 | // Visit the argument location. |
| 847 | if (printerFlags.shouldPrintDebugInfo()) |
| 848 | // TODO: Allow deferring argument locations. |
| 849 | initializer.visit(attr: arg.getLoc(), /*canBeDeferred=*/false); |
| 850 | } |
| 851 | |
| 852 | /// Consider the given type to be printed for an alias. |
| 853 | void printType(Type type) override { |
| 854 | if (type) |
| 855 | initializer.visit(type); |
| 856 | } |
| 857 | |
| 858 | /// Consider the given attribute to be printed for an alias. |
| 859 | void printAttribute(Attribute attr) override { initializer.visit(attr); } |
| 860 | void printAttributeWithoutType(Attribute attr) override { |
| 861 | printAttribute(attr); |
| 862 | } |
| 863 | void printNamedAttribute(NamedAttribute attr) override { |
| 864 | printAttribute(attr: attr.getValue()); |
| 865 | } |
| 866 | |
| 867 | LogicalResult printAlias(Attribute attr) override { |
| 868 | initializer.visit(attr); |
| 869 | return success(); |
| 870 | } |
| 871 | LogicalResult printAlias(Type type) override { |
| 872 | initializer.visit(type); |
| 873 | return success(); |
| 874 | } |
| 875 | |
| 876 | /// Consider the given location to be printed for an alias. |
| 877 | void printOptionalLocationSpecifier(Location loc) override { |
| 878 | printAttribute(attr: loc); |
| 879 | } |
| 880 | |
| 881 | /// Print the given set of attributes with names not included within |
| 882 | /// 'elidedAttrs'. |
| 883 | void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs, |
| 884 | ArrayRef<StringRef> elidedAttrs = {}) override { |
| 885 | if (attrs.empty()) |
| 886 | return; |
| 887 | if (elidedAttrs.empty()) { |
| 888 | for (const NamedAttribute &attr : attrs) |
| 889 | printAttribute(attr: attr.getValue()); |
| 890 | return; |
| 891 | } |
| 892 | llvm::SmallDenseSet<StringRef> (elidedAttrs.begin(), |
| 893 | elidedAttrs.end()); |
| 894 | for (const NamedAttribute &attr : attrs) |
| 895 | if (!elidedAttrsSet.contains(V: attr.getName().strref())) |
| 896 | printAttribute(attr: attr.getValue()); |
| 897 | } |
| 898 | void printOptionalAttrDictWithKeyword( |
| 899 | ArrayRef<NamedAttribute> attrs, |
| 900 | ArrayRef<StringRef> elidedAttrs = {}) override { |
| 901 | printOptionalAttrDict(attrs, elidedAttrs); |
| 902 | } |
| 903 | |
| 904 | /// Return a null stream as the output stream, this will ignore any data fed |
| 905 | /// to it. |
| 906 | raw_ostream &getStream() const override { return os; } |
| 907 | |
| 908 | /// The following are hooks of `OpAsmPrinter` that are not necessary for |
| 909 | /// determining potential aliases. |
| 910 | void printFloat(const APFloat &) override {} |
| 911 | void printAffineMapOfSSAIds(AffineMapAttr, ValueRange) override {} |
| 912 | void printAffineExprOfSSAIds(AffineExpr, ValueRange, ValueRange) override {} |
| 913 | void printNewline() override {} |
| 914 | void increaseIndent() override {} |
| 915 | void decreaseIndent() override {} |
| 916 | void printOperand(Value) override {} |
| 917 | void printOperand(Value, raw_ostream &os) override { |
| 918 | // Users expect the output string to have at least the prefixed % to signal |
| 919 | // a value name. To maintain this invariant, emit a name even if it is |
| 920 | // guaranteed to go unused. |
| 921 | os << "%" ; |
| 922 | } |
| 923 | void printKeywordOrString(StringRef) override {} |
| 924 | void printString(StringRef) override {} |
| 925 | void printResourceHandle(const AsmDialectResourceHandle &) override {} |
| 926 | void printSymbolName(StringRef) override {} |
| 927 | void printSuccessor(Block *) override {} |
| 928 | void printSuccessorAndUseList(Block *, ValueRange) override {} |
| 929 | void shadowRegionArgs(Region &, ValueRange) override {} |
| 930 | |
| 931 | /// The printer flags to use when determining potential aliases. |
| 932 | const OpPrintingFlags &printerFlags; |
| 933 | |
| 934 | /// The initializer to use when identifying aliases. |
| 935 | AliasInitializer &initializer; |
| 936 | |
| 937 | /// A dummy output stream. |
| 938 | mutable llvm::raw_null_ostream os; |
| 939 | }; |
| 940 | |
| 941 | class DummyAliasDialectAsmPrinter : public DialectAsmPrinter { |
| 942 | public: |
| 943 | explicit DummyAliasDialectAsmPrinter(AliasInitializer &initializer, |
| 944 | bool canBeDeferred, |
| 945 | SmallVectorImpl<size_t> &childIndices) |
| 946 | : initializer(initializer), canBeDeferred(canBeDeferred), |
| 947 | childIndices(childIndices) {} |
| 948 | |
| 949 | /// Print the given attribute/type, visiting any nested aliases that would be |
| 950 | /// generated as part of printing. Returns the maximum alias depth found while |
| 951 | /// printing the given value. |
| 952 | template <typename T, typename... PrintArgs> |
| 953 | size_t printAndVisitNestedAliases(T value, PrintArgs &&...printArgs) { |
| 954 | printAndVisitNestedAliasesImpl(value, printArgs...); |
| 955 | return maxAliasDepth; |
| 956 | } |
| 957 | |
| 958 | private: |
| 959 | /// Print the given attribute/type, visiting any nested aliases that would be |
| 960 | /// generated as part of printing. |
| 961 | void printAndVisitNestedAliasesImpl(Attribute attr, bool elideType) { |
| 962 | if (!isa<BuiltinDialect>(Val: attr.getDialect())) { |
| 963 | attr.getDialect().printAttribute(attr, *this); |
| 964 | |
| 965 | // Process the builtin attributes. |
| 966 | } else if (llvm::isa<AffineMapAttr, DenseArrayAttr, FloatAttr, IntegerAttr, |
| 967 | IntegerSetAttr, UnitAttr>(Val: attr)) { |
| 968 | return; |
| 969 | } else if (auto distinctAttr = dyn_cast<DistinctAttr>(Val&: attr)) { |
| 970 | printAttribute(attr: distinctAttr.getReferencedAttr()); |
| 971 | } else if (auto dictAttr = dyn_cast<DictionaryAttr>(Val&: attr)) { |
| 972 | for (const NamedAttribute &nestedAttr : dictAttr.getValue()) { |
| 973 | printAttribute(attr: nestedAttr.getName()); |
| 974 | printAttribute(attr: nestedAttr.getValue()); |
| 975 | } |
| 976 | } else if (auto arrayAttr = dyn_cast<ArrayAttr>(Val&: attr)) { |
| 977 | for (Attribute nestedAttr : arrayAttr.getValue()) |
| 978 | printAttribute(attr: nestedAttr); |
| 979 | } else if (auto typeAttr = dyn_cast<TypeAttr>(Val&: attr)) { |
| 980 | printType(type: typeAttr.getValue()); |
| 981 | } else if (auto locAttr = dyn_cast<OpaqueLoc>(Val&: attr)) { |
| 982 | printAttribute(attr: locAttr.getFallbackLocation()); |
| 983 | } else if (auto locAttr = dyn_cast<NameLoc>(Val&: attr)) { |
| 984 | if (!isa<UnknownLoc>(Val: locAttr.getChildLoc())) |
| 985 | printAttribute(attr: locAttr.getChildLoc()); |
| 986 | } else if (auto locAttr = dyn_cast<CallSiteLoc>(Val&: attr)) { |
| 987 | printAttribute(attr: locAttr.getCallee()); |
| 988 | printAttribute(attr: locAttr.getCaller()); |
| 989 | } else if (auto locAttr = dyn_cast<FusedLoc>(Val&: attr)) { |
| 990 | if (Attribute metadata = locAttr.getMetadata()) |
| 991 | printAttribute(attr: metadata); |
| 992 | for (Location nestedLoc : locAttr.getLocations()) |
| 993 | printAttribute(attr: nestedLoc); |
| 994 | } |
| 995 | |
| 996 | // Don't print the type if we must elide it, or if it is a None type. |
| 997 | if (!elideType) { |
| 998 | if (auto typedAttr = llvm::dyn_cast<TypedAttr>(Val&: attr)) { |
| 999 | Type attrType = typedAttr.getType(); |
| 1000 | if (!llvm::isa<NoneType>(Val: attrType)) |
| 1001 | printType(type: attrType); |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | void printAndVisitNestedAliasesImpl(Type type) { |
| 1006 | if (!type) |
| 1007 | return; |
| 1008 | if (!isa<BuiltinDialect>(Val: type.getDialect())) |
| 1009 | return type.getDialect().printType(type, *this); |
| 1010 | |
| 1011 | // Only visit the layout of memref if it isn't the identity. |
| 1012 | if (auto memrefTy = llvm::dyn_cast<MemRefType>(Val&: type)) { |
| 1013 | printType(type: memrefTy.getElementType()); |
| 1014 | MemRefLayoutAttrInterface layout = memrefTy.getLayout(); |
| 1015 | if (!llvm::isa<AffineMapAttr>(Val: layout) || !layout.isIdentity()) |
| 1016 | printAttribute(attr: memrefTy.getLayout()); |
| 1017 | if (memrefTy.getMemorySpace()) |
| 1018 | printAttribute(attr: memrefTy.getMemorySpace()); |
| 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | // For most builtin types, we can simply walk the sub elements. |
| 1023 | auto visitFn = [&](auto element) { |
| 1024 | if (element) |
| 1025 | (void)printAlias(element); |
| 1026 | }; |
| 1027 | type.walkImmediateSubElements(walkAttrsFn: visitFn, walkTypesFn: visitFn); |
| 1028 | } |
| 1029 | |
| 1030 | /// Consider the given type to be printed for an alias. |
| 1031 | void printType(Type type) override { |
| 1032 | recordAliasResult(aliasDepthAndIndex: initializer.visit(type, canBeDeferred)); |
| 1033 | } |
| 1034 | |
| 1035 | /// Consider the given attribute to be printed for an alias. |
| 1036 | void printAttribute(Attribute attr) override { |
| 1037 | recordAliasResult(aliasDepthAndIndex: initializer.visit(attr, canBeDeferred)); |
| 1038 | } |
| 1039 | void printAttributeWithoutType(Attribute attr) override { |
| 1040 | recordAliasResult( |
| 1041 | aliasDepthAndIndex: initializer.visit(attr, canBeDeferred, /*elideType=*/true)); |
| 1042 | } |
| 1043 | void printNamedAttribute(NamedAttribute attr) override { |
| 1044 | printAttribute(attr: attr.getValue()); |
| 1045 | } |
| 1046 | |
| 1047 | LogicalResult printAlias(Attribute attr) override { |
| 1048 | printAttribute(attr); |
| 1049 | return success(); |
| 1050 | } |
| 1051 | LogicalResult printAlias(Type type) override { |
| 1052 | printType(type); |
| 1053 | return success(); |
| 1054 | } |
| 1055 | |
| 1056 | /// Record the alias result of a child element. |
| 1057 | void recordAliasResult(std::pair<size_t, size_t> aliasDepthAndIndex) { |
| 1058 | childIndices.push_back(Elt: aliasDepthAndIndex.second); |
| 1059 | if (aliasDepthAndIndex.first > maxAliasDepth) |
| 1060 | maxAliasDepth = aliasDepthAndIndex.first; |
| 1061 | } |
| 1062 | |
| 1063 | /// Return a null stream as the output stream, this will ignore any data fed |
| 1064 | /// to it. |
| 1065 | raw_ostream &getStream() const override { return os; } |
| 1066 | |
| 1067 | /// The following are hooks of `DialectAsmPrinter` that are not necessary for |
| 1068 | /// determining potential aliases. |
| 1069 | void printNewline() override {} |
| 1070 | void increaseIndent() override {} |
| 1071 | void decreaseIndent() override {} |
| 1072 | void printFloat(const APFloat &) override {} |
| 1073 | void printKeywordOrString(StringRef) override {} |
| 1074 | void printString(StringRef) override {} |
| 1075 | void printSymbolName(StringRef) override {} |
| 1076 | void printResourceHandle(const AsmDialectResourceHandle &) override {} |
| 1077 | |
| 1078 | LogicalResult pushCyclicPrinting(const void *opaquePointer) override { |
| 1079 | return success(IsSuccess: cyclicPrintingStack.insert(X: opaquePointer)); |
| 1080 | } |
| 1081 | |
| 1082 | void popCyclicPrinting() override { cyclicPrintingStack.pop_back(); } |
| 1083 | |
| 1084 | /// Stack of potentially cyclic mutable attributes or type currently being |
| 1085 | /// printed. |
| 1086 | SetVector<const void *> cyclicPrintingStack; |
| 1087 | |
| 1088 | /// The initializer to use when identifying aliases. |
| 1089 | AliasInitializer &initializer; |
| 1090 | |
| 1091 | /// If the aliases visited by this printer can be deferred. |
| 1092 | bool canBeDeferred; |
| 1093 | |
| 1094 | /// The indices of child aliases. |
| 1095 | SmallVectorImpl<size_t> &childIndices; |
| 1096 | |
| 1097 | /// The maximum alias depth found by the printer. |
| 1098 | size_t maxAliasDepth = 0; |
| 1099 | |
| 1100 | /// A dummy output stream. |
| 1101 | mutable llvm::raw_null_ostream os; |
| 1102 | }; |
| 1103 | } // namespace |
| 1104 | |
| 1105 | /// Sanitize the given name such that it can be used as a valid identifier. If |
| 1106 | /// the string needs to be modified in any way, the provided buffer is used to |
| 1107 | /// store the new copy, |
| 1108 | static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer, |
| 1109 | StringRef allowedPunctChars = "$._-" ) { |
| 1110 | assert(!name.empty() && "Shouldn't have an empty name here" ); |
| 1111 | |
| 1112 | auto validChar = [&](char ch) { |
| 1113 | return llvm::isAlnum(C: ch) || allowedPunctChars.contains(C: ch); |
| 1114 | }; |
| 1115 | |
| 1116 | auto copyNameToBuffer = [&] { |
| 1117 | for (char ch : name) { |
| 1118 | if (validChar(ch)) |
| 1119 | buffer.push_back(Elt: ch); |
| 1120 | else if (ch == ' ') |
| 1121 | buffer.push_back(Elt: '_'); |
| 1122 | else |
| 1123 | buffer.append(RHS: llvm::utohexstr(X: (unsigned char)ch)); |
| 1124 | } |
| 1125 | }; |
| 1126 | |
| 1127 | // Check to see if this name is valid. If it starts with a digit, then it |
| 1128 | // could conflict with the autogenerated numeric ID's, so add an underscore |
| 1129 | // prefix to avoid problems. |
| 1130 | if (isdigit(name[0]) || (!validChar(name[0]) && name[0] != ' ')) { |
| 1131 | buffer.push_back(Elt: '_'); |
| 1132 | copyNameToBuffer(); |
| 1133 | return buffer; |
| 1134 | } |
| 1135 | |
| 1136 | // Check to see that the name consists of only valid identifier characters. |
| 1137 | for (char ch : name) { |
| 1138 | if (!validChar(ch)) { |
| 1139 | copyNameToBuffer(); |
| 1140 | return buffer; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | // If there are no invalid characters, return the original name. |
| 1145 | return name; |
| 1146 | } |
| 1147 | |
| 1148 | unsigned AliasInitializer::uniqueAliasNameIndex( |
| 1149 | StringRef alias, llvm::StringMap<unsigned> &nameCounts, |
| 1150 | llvm::StringSet<llvm::BumpPtrAllocator &> &usedAliases) { |
| 1151 | if (!usedAliases.count(Key: alias)) { |
| 1152 | usedAliases.insert(key: alias); |
| 1153 | // 0 is not printed in SymbolAlias. |
| 1154 | return 0; |
| 1155 | } |
| 1156 | // Otherwise, we had a conflict - probe until we find a unique name. |
| 1157 | SmallString<64> probeAlias(alias); |
| 1158 | size_t probeSize = probeAlias.size(); |
| 1159 | // alias with trailing digit will be printed as _N |
| 1160 | if (isdigit(alias.back())) { |
| 1161 | probeAlias.push_back(Elt: '_'); |
| 1162 | probeSize++; |
| 1163 | } |
| 1164 | // nameCounts start from 1 because 0 is not printed in SymbolAlias. |
| 1165 | if (nameCounts[probeAlias] == 0) |
| 1166 | nameCounts[probeAlias] = 1; |
| 1167 | // This is guaranteed to terminate (and usually in a single iteration) |
| 1168 | // because it generates new names by incrementing nameCounts. |
| 1169 | while (true) { |
| 1170 | unsigned nameIndex = nameCounts[probeAlias]++; |
| 1171 | probeAlias += llvm::utostr(X: nameIndex); |
| 1172 | if (!usedAliases.count(Key: probeAlias)) { |
| 1173 | usedAliases.insert(key: probeAlias); |
| 1174 | return nameIndex; |
| 1175 | } |
| 1176 | // Reset probeAlias to the original alias for the next iteration. |
| 1177 | probeAlias.resize(N: probeSize); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /// Given a collection of aliases and symbols, initialize a mapping from a |
| 1182 | /// symbol to a given alias. |
| 1183 | void AliasInitializer::initializeAliases( |
| 1184 | llvm::MapVector<const void *, InProgressAliasInfo> &visitedSymbols, |
| 1185 | llvm::MapVector<const void *, SymbolAlias> &symbolToAlias) { |
| 1186 | SmallVector<std::pair<const void *, InProgressAliasInfo>, 0> |
| 1187 | unprocessedAliases = visitedSymbols.takeVector(); |
| 1188 | llvm::stable_sort(Range&: unprocessedAliases, C: llvm::less_second()); |
| 1189 | |
| 1190 | // This keeps track of all of the non-numeric names that are in flight, |
| 1191 | // allowing us to check for duplicates. |
| 1192 | llvm::BumpPtrAllocator usedAliasAllocator; |
| 1193 | llvm::StringSet<llvm::BumpPtrAllocator &> usedAliases(usedAliasAllocator); |
| 1194 | |
| 1195 | llvm::StringMap<unsigned> nameCounts; |
| 1196 | for (auto &[symbol, aliasInfo] : unprocessedAliases) { |
| 1197 | if (!aliasInfo.alias) |
| 1198 | continue; |
| 1199 | StringRef alias = *aliasInfo.alias; |
| 1200 | unsigned nameIndex = uniqueAliasNameIndex(alias, nameCounts, usedAliases); |
| 1201 | symbolToAlias.insert( |
| 1202 | KV: {symbol, SymbolAlias(alias, nameIndex, aliasInfo.isType, |
| 1203 | aliasInfo.canBeDeferred)}); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | void AliasInitializer::initialize( |
| 1208 | Operation *op, const OpPrintingFlags &printerFlags, |
| 1209 | llvm::MapVector<const void *, SymbolAlias> &attrTypeToAlias) { |
| 1210 | // Use a dummy printer when walking the IR so that we can collect the |
| 1211 | // attributes/types that will actually be used during printing when |
| 1212 | // considering aliases. |
| 1213 | DummyAliasOperationPrinter aliasPrinter(printerFlags, *this); |
| 1214 | aliasPrinter.printCustomOrGenericOp(op); |
| 1215 | |
| 1216 | // Initialize the aliases. |
| 1217 | initializeAliases(visitedSymbols&: aliases, symbolToAlias&: attrTypeToAlias); |
| 1218 | } |
| 1219 | |
| 1220 | template <typename T, typename... PrintArgs> |
| 1221 | std::pair<size_t, size_t> AliasInitializer::visitImpl( |
| 1222 | T value, llvm::MapVector<const void *, InProgressAliasInfo> &aliases, |
| 1223 | bool canBeDeferred, PrintArgs &&...printArgs) { |
| 1224 | auto [it, inserted] = aliases.try_emplace(value.getAsOpaquePointer()); |
| 1225 | size_t aliasIndex = std::distance(aliases.begin(), it); |
| 1226 | if (!inserted) { |
| 1227 | // Make sure that the alias isn't deferred if we don't permit it. |
| 1228 | if (!canBeDeferred) |
| 1229 | markAliasNonDeferrable(aliasIndex); |
| 1230 | return {static_cast<size_t>(it->second.aliasDepth), aliasIndex}; |
| 1231 | } |
| 1232 | |
| 1233 | // Try to generate an alias for this value. |
| 1234 | generateAlias(value, it->second, canBeDeferred); |
| 1235 | it->second.isType = std::is_base_of_v<Type, T>; |
| 1236 | it->second.canBeDeferred = canBeDeferred; |
| 1237 | |
| 1238 | // Print the value, capturing any nested elements that require aliases. |
| 1239 | SmallVector<size_t> childAliases; |
| 1240 | DummyAliasDialectAsmPrinter printer(*this, canBeDeferred, childAliases); |
| 1241 | size_t maxAliasDepth = |
| 1242 | printer.printAndVisitNestedAliases(value, printArgs...); |
| 1243 | |
| 1244 | // Make sure to recompute `it` in case the map was reallocated. |
| 1245 | it = std::next(x: aliases.begin(), n: aliasIndex); |
| 1246 | |
| 1247 | // If we had sub elements, update to account for the depth. |
| 1248 | it->second.childIndices = std::move(childAliases); |
| 1249 | if (maxAliasDepth) |
| 1250 | it->second.aliasDepth = maxAliasDepth + 1; |
| 1251 | |
| 1252 | // Propagate the alias depth of the value. |
| 1253 | return {(size_t)it->second.aliasDepth, aliasIndex}; |
| 1254 | } |
| 1255 | |
| 1256 | void AliasInitializer::markAliasNonDeferrable(size_t aliasIndex) { |
| 1257 | auto *it = std::next(x: aliases.begin(), n: aliasIndex); |
| 1258 | |
| 1259 | // If already marked non-deferrable stop the recursion. |
| 1260 | // All children should already be marked non-deferrable as well. |
| 1261 | if (!it->second.canBeDeferred) |
| 1262 | return; |
| 1263 | |
| 1264 | it->second.canBeDeferred = false; |
| 1265 | |
| 1266 | // Propagate the non-deferrable flag to any child aliases. |
| 1267 | for (size_t childIndex : it->second.childIndices) |
| 1268 | markAliasNonDeferrable(aliasIndex: childIndex); |
| 1269 | } |
| 1270 | |
| 1271 | template <typename T> |
| 1272 | void AliasInitializer::generateAlias(T symbol, InProgressAliasInfo &alias, |
| 1273 | bool canBeDeferred) { |
| 1274 | SmallString<32> nameBuffer; |
| 1275 | |
| 1276 | OpAsmDialectInterface::AliasResult symbolInterfaceResult = |
| 1277 | OpAsmDialectInterface::AliasResult::NoAlias; |
| 1278 | using InterfaceT = std::conditional_t<std::is_base_of_v<Attribute, T>, |
| 1279 | OpAsmAttrInterface, OpAsmTypeInterface>; |
| 1280 | if (auto symbolInterface = dyn_cast<InterfaceT>(symbol)) { |
| 1281 | symbolInterfaceResult = symbolInterface.getAlias(aliasOS); |
| 1282 | if (symbolInterfaceResult != OpAsmDialectInterface::AliasResult::NoAlias) { |
| 1283 | nameBuffer = std::move(aliasBuffer); |
| 1284 | assert(!nameBuffer.empty() && "expected valid alias name" ); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | if (symbolInterfaceResult != OpAsmDialectInterface::AliasResult::FinalAlias) { |
| 1289 | for (const auto &interface : interfaces) { |
| 1290 | OpAsmDialectInterface::AliasResult result = |
| 1291 | interface.getAlias(symbol, aliasOS); |
| 1292 | if (result == OpAsmDialectInterface::AliasResult::NoAlias) |
| 1293 | continue; |
| 1294 | nameBuffer = std::move(aliasBuffer); |
| 1295 | assert(!nameBuffer.empty() && "expected valid alias name" ); |
| 1296 | if (result == OpAsmDialectInterface::AliasResult::FinalAlias) |
| 1297 | break; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | if (nameBuffer.empty()) |
| 1302 | return; |
| 1303 | |
| 1304 | SmallString<16> tempBuffer; |
| 1305 | StringRef name = |
| 1306 | sanitizeIdentifier(name: nameBuffer, buffer&: tempBuffer, /*allowedPunctChars=*/"$_-" ); |
| 1307 | name = name.copy(A&: aliasAllocator); |
| 1308 | alias = InProgressAliasInfo(name); |
| 1309 | } |
| 1310 | |
| 1311 | //===----------------------------------------------------------------------===// |
| 1312 | // AliasState |
| 1313 | //===----------------------------------------------------------------------===// |
| 1314 | |
| 1315 | namespace { |
| 1316 | /// This class manages the state for type and attribute aliases. |
| 1317 | class AliasState { |
| 1318 | public: |
| 1319 | // Initialize the internal aliases. |
| 1320 | void |
| 1321 | initialize(Operation *op, const OpPrintingFlags &printerFlags, |
| 1322 | DialectInterfaceCollection<OpAsmDialectInterface> &interfaces); |
| 1323 | |
| 1324 | /// Get an alias for the given attribute if it has one and print it in `os`. |
| 1325 | /// Returns success if an alias was printed, failure otherwise. |
| 1326 | LogicalResult getAlias(Attribute attr, raw_ostream &os) const; |
| 1327 | |
| 1328 | /// Get an alias for the given type if it has one and print it in `os`. |
| 1329 | /// Returns success if an alias was printed, failure otherwise. |
| 1330 | LogicalResult getAlias(Type ty, raw_ostream &os) const; |
| 1331 | |
| 1332 | /// Print all of the referenced aliases that can not be resolved in a deferred |
| 1333 | /// manner. |
| 1334 | void printNonDeferredAliases(AsmPrinter::Impl &p, NewLineCounter &newLine) { |
| 1335 | printAliases(p, newLine, /*isDeferred=*/false); |
| 1336 | } |
| 1337 | |
| 1338 | /// Print all of the referenced aliases that support deferred resolution. |
| 1339 | void printDeferredAliases(AsmPrinter::Impl &p, NewLineCounter &newLine) { |
| 1340 | printAliases(p, newLine, /*isDeferred=*/true); |
| 1341 | } |
| 1342 | |
| 1343 | private: |
| 1344 | /// Print all of the referenced aliases that support the provided resolution |
| 1345 | /// behavior. |
| 1346 | void printAliases(AsmPrinter::Impl &p, NewLineCounter &newLine, |
| 1347 | bool isDeferred); |
| 1348 | |
| 1349 | /// Mapping between attribute/type and alias. |
| 1350 | llvm::MapVector<const void *, SymbolAlias> attrTypeToAlias; |
| 1351 | |
| 1352 | /// An allocator used for alias names. |
| 1353 | llvm::BumpPtrAllocator aliasAllocator; |
| 1354 | }; |
| 1355 | } // namespace |
| 1356 | |
| 1357 | void AliasState::initialize( |
| 1358 | Operation *op, const OpPrintingFlags &printerFlags, |
| 1359 | DialectInterfaceCollection<OpAsmDialectInterface> &interfaces) { |
| 1360 | // Each initialization starts fresh: a state reused across prints must not |
| 1361 | // carry the previous op's aliases. |
| 1362 | attrTypeToAlias.clear(); |
| 1363 | AliasInitializer initializer(interfaces, aliasAllocator); |
| 1364 | initializer.initialize(op, printerFlags, attrTypeToAlias); |
| 1365 | } |
| 1366 | |
| 1367 | LogicalResult AliasState::getAlias(Attribute attr, raw_ostream &os) const { |
| 1368 | const auto *it = attrTypeToAlias.find(Key: attr.getAsOpaquePointer()); |
| 1369 | if (it == attrTypeToAlias.end()) |
| 1370 | return failure(); |
| 1371 | it->second.print(os); |
| 1372 | return success(); |
| 1373 | } |
| 1374 | |
| 1375 | LogicalResult AliasState::getAlias(Type ty, raw_ostream &os) const { |
| 1376 | const auto *it = attrTypeToAlias.find(Key: ty.getAsOpaquePointer()); |
| 1377 | if (it == attrTypeToAlias.end()) |
| 1378 | return failure(); |
| 1379 | if (!it->second.isPrinted) |
| 1380 | return failure(); |
| 1381 | |
| 1382 | it->second.print(os); |
| 1383 | return success(); |
| 1384 | } |
| 1385 | |
| 1386 | void AliasState::printAliases(AsmPrinter::Impl &p, NewLineCounter &newLine, |
| 1387 | bool isDeferred) { |
| 1388 | auto filterFn = [=](const auto &aliasIt) { |
| 1389 | return aliasIt.second.canBeDeferred() == isDeferred; |
| 1390 | }; |
| 1391 | for (auto &[opaqueSymbol, alias] : |
| 1392 | llvm::make_filter_range(Range&: attrTypeToAlias, Pred: filterFn)) { |
| 1393 | alias.print(os&: p.getStream()); |
| 1394 | p.getStream() << " = " ; |
| 1395 | |
| 1396 | if (alias.isTypeAlias()) { |
| 1397 | Type type = Type::getFromOpaquePointer(pointer: opaqueSymbol); |
| 1398 | p.printTypeImpl(type); |
| 1399 | alias.isPrinted = true; |
| 1400 | } else { |
| 1401 | // TODO: Support nested aliases in mutable attributes. |
| 1402 | Attribute attr = Attribute::getFromOpaquePointer(ptr: opaqueSymbol); |
| 1403 | if (attr.hasTrait<AttributeTrait::IsMutable>()) |
| 1404 | p.getStream() << attr; |
| 1405 | else |
| 1406 | p.printAttributeImpl(attr); |
| 1407 | } |
| 1408 | |
| 1409 | p.getStream() << newLine; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | //===----------------------------------------------------------------------===// |
| 1414 | // SSANameState |
| 1415 | //===----------------------------------------------------------------------===// |
| 1416 | |
| 1417 | namespace { |
| 1418 | /// Info about block printing: a number which is its position in the visitation |
| 1419 | /// order, and a name that is used to print reference to it, e.g. ^bb42. |
| 1420 | struct BlockInfo { |
| 1421 | int ordering; |
| 1422 | StringRef name; |
| 1423 | }; |
| 1424 | |
| 1425 | /// This class manages the state of SSA value names. |
| 1426 | class SSANameState { |
| 1427 | public: |
| 1428 | /// A sentinel value used for values with names set. |
| 1429 | enum : unsigned { NameSentinel = ~0U }; |
| 1430 | |
| 1431 | SSANameState(Operation *op, const OpPrintingFlags &printerFlags); |
| 1432 | SSANameState() = default; |
| 1433 | |
| 1434 | /// Print the SSA identifier for the given value to 'stream'. If |
| 1435 | /// 'printResultNo' is true, it also presents the result number ('#' number) |
| 1436 | /// of this value. |
| 1437 | void printValueID(Value value, bool printResultNo, raw_ostream &stream) const; |
| 1438 | |
| 1439 | /// Print the operation identifier. |
| 1440 | void printOperationID(Operation *op, raw_ostream &stream) const; |
| 1441 | |
| 1442 | /// Return the result indices for each of the result groups registered by this |
| 1443 | /// operation, or empty if none exist. |
| 1444 | ArrayRef<int> getOpResultGroups(Operation *op); |
| 1445 | |
| 1446 | /// Get the info for the given block. |
| 1447 | BlockInfo getBlockInfo(Block *block); |
| 1448 | |
| 1449 | /// Renumber the arguments for the specified region to the same names as the |
| 1450 | /// SSA values in namesToUse. See OperationPrinter::shadowRegionArgs for |
| 1451 | /// details. |
| 1452 | void shadowRegionArgs(Region ®ion, ValueRange namesToUse); |
| 1453 | |
| 1454 | private: |
| 1455 | /// Number the SSA values within the given IR unit. |
| 1456 | void numberValuesInRegion(Region ®ion); |
| 1457 | void numberValuesInBlock(Block &block); |
| 1458 | void numberValuesInOp(Operation &op); |
| 1459 | |
| 1460 | /// Given a result of an operation 'result', find the result group head |
| 1461 | /// 'lookupValue' and the result of 'result' within that group in |
| 1462 | /// 'lookupResultNo'. 'lookupResultNo' is only filled in if the result group |
| 1463 | /// has more than 1 result. |
| 1464 | void getResultIDAndNumber(OpResult result, Value &lookupValue, |
| 1465 | std::optional<int> &lookupResultNo) const; |
| 1466 | |
| 1467 | /// Set a special value name for the given value. |
| 1468 | void setValueName(Value value, StringRef name); |
| 1469 | |
| 1470 | /// Uniques the given value name within the printer. If the given name |
| 1471 | /// conflicts, it is automatically renamed. |
| 1472 | StringRef uniqueValueName(StringRef name); |
| 1473 | |
| 1474 | /// This is the value ID for each SSA value. If this returns NameSentinel, |
| 1475 | /// then the valueID has an entry in valueNames. |
| 1476 | DenseMap<Value, unsigned> valueIDs; |
| 1477 | DenseMap<Value, StringRef> valueNames; |
| 1478 | |
| 1479 | /// When printing users of values, an operation without a result might |
| 1480 | /// be the user. This map holds ids for such operations. |
| 1481 | DenseMap<Operation *, unsigned> operationIDs; |
| 1482 | |
| 1483 | /// This is a map of operations that contain multiple named result groups, |
| 1484 | /// i.e. there may be multiple names for the results of the operation. The |
| 1485 | /// value of this map are the result numbers that start a result group. |
| 1486 | DenseMap<Operation *, SmallVector<int, 1>> opResultGroups; |
| 1487 | |
| 1488 | /// This maps blocks to there visitation number in the current region as well |
| 1489 | /// as the string representing their name. |
| 1490 | DenseMap<Block *, BlockInfo> blockNames; |
| 1491 | |
| 1492 | /// This keeps track of all of the non-numeric names that are in flight, |
| 1493 | /// allowing us to check for duplicates. |
| 1494 | /// Note: the value of the map is unused. |
| 1495 | llvm::ScopedHashTable<StringRef, char> usedNames; |
| 1496 | llvm::BumpPtrAllocator usedNameAllocator; |
| 1497 | |
| 1498 | /// This is the next value ID to assign in numbering. |
| 1499 | unsigned nextValueID = 0; |
| 1500 | /// This is the next ID to assign to a region entry block argument. |
| 1501 | unsigned nextArgumentID = 0; |
| 1502 | /// This is the next ID to assign when a name conflict is detected. |
| 1503 | unsigned nextConflictID = 0; |
| 1504 | |
| 1505 | /// These are the printing flags. They control, eg., whether to print in |
| 1506 | /// generic form. |
| 1507 | OpPrintingFlags printerFlags; |
| 1508 | }; |
| 1509 | } // namespace |
| 1510 | |
| 1511 | SSANameState::SSANameState(Operation *op, const OpPrintingFlags &printerFlags) |
| 1512 | : printerFlags(printerFlags) { |
| 1513 | llvm::SaveAndRestore valueIDSaver(nextValueID); |
| 1514 | llvm::SaveAndRestore argumentIDSaver(nextArgumentID); |
| 1515 | llvm::SaveAndRestore conflictIDSaver(nextConflictID); |
| 1516 | |
| 1517 | // The naming context includes `nextValueID`, `nextArgumentID`, |
| 1518 | // `nextConflictID` and `usedNames` scoped HashTable. This information is |
| 1519 | // carried from the parent region. |
| 1520 | using UsedNamesScopeTy = llvm::ScopedHashTable<StringRef, char>::ScopeTy; |
| 1521 | using NamingContext = |
| 1522 | std::tuple<Region *, unsigned, unsigned, unsigned, UsedNamesScopeTy *>; |
| 1523 | |
| 1524 | // Allocator for UsedNamesScopeTy |
| 1525 | llvm::BumpPtrAllocator allocator; |
| 1526 | |
| 1527 | // Add a scope for the top level operation. |
| 1528 | auto *topLevelNamesScope = |
| 1529 | new (allocator.Allocate<UsedNamesScopeTy>()) UsedNamesScopeTy(usedNames); |
| 1530 | |
| 1531 | SmallVector<NamingContext, 8> nameContext; |
| 1532 | for (Region ®ion : op->getRegions()) |
| 1533 | nameContext.push_back(Elt: std::make_tuple(args: ®ion, args&: nextValueID, args&: nextArgumentID, |
| 1534 | args&: nextConflictID, args&: topLevelNamesScope)); |
| 1535 | |
| 1536 | numberValuesInOp(op&: *op); |
| 1537 | |
| 1538 | while (!nameContext.empty()) { |
| 1539 | Region *region; |
| 1540 | UsedNamesScopeTy *parentScope; |
| 1541 | |
| 1542 | if (printerFlags.shouldPrintUniqueSSAIDs()) |
| 1543 | // To print unique SSA IDs, ignore saved ID counts from parent regions |
| 1544 | std::tie(args&: region, args: std::ignore, args: std::ignore, args: std::ignore, args&: parentScope) = |
| 1545 | nameContext.pop_back_val(); |
| 1546 | else |
| 1547 | std::tie(args&: region, args&: nextValueID, args&: nextArgumentID, args&: nextConflictID, |
| 1548 | args&: parentScope) = nameContext.pop_back_val(); |
| 1549 | |
| 1550 | // When we switch from one subtree to another, pop the scopes(needless) |
| 1551 | // until the parent scope. |
| 1552 | while (usedNames.getCurScope() != parentScope) { |
| 1553 | usedNames.getCurScope()->~UsedNamesScopeTy(); |
| 1554 | assert((usedNames.getCurScope() != nullptr || parentScope == nullptr) && |
| 1555 | "top level parentScope must be a nullptr" ); |
| 1556 | } |
| 1557 | |
| 1558 | // Add a scope for the current region. |
| 1559 | auto *curNamesScope = new (allocator.Allocate<UsedNamesScopeTy>()) |
| 1560 | UsedNamesScopeTy(usedNames); |
| 1561 | |
| 1562 | numberValuesInRegion(region&: *region); |
| 1563 | |
| 1564 | for (Operation &op : region->getOps()) |
| 1565 | for (Region ®ion : op.getRegions()) |
| 1566 | nameContext.push_back(Elt: std::make_tuple(args: ®ion, args&: nextValueID, |
| 1567 | args&: nextArgumentID, args&: nextConflictID, |
| 1568 | args&: curNamesScope)); |
| 1569 | } |
| 1570 | |
| 1571 | // Manually remove all the scopes. |
| 1572 | while (usedNames.getCurScope() != nullptr) |
| 1573 | usedNames.getCurScope()->~UsedNamesScopeTy(); |
| 1574 | } |
| 1575 | |
| 1576 | void SSANameState::printValueID(Value value, bool printResultNo, |
| 1577 | raw_ostream &stream) const { |
| 1578 | if (!value) { |
| 1579 | stream << "<<NULL VALUE>>" ; |
| 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | std::optional<int> resultNo; |
| 1584 | auto lookupValue = value; |
| 1585 | |
| 1586 | // If this is an operation result, collect the head lookup value of the result |
| 1587 | // group and the result number of 'result' within that group. |
| 1588 | if (OpResult result = dyn_cast<OpResult>(Val&: value)) |
| 1589 | getResultIDAndNumber(result, lookupValue, lookupResultNo&: resultNo); |
| 1590 | |
| 1591 | auto it = valueIDs.find(Val: lookupValue); |
| 1592 | if (it == valueIDs.end()) { |
| 1593 | stream << "<<UNKNOWN SSA VALUE>>" ; |
| 1594 | return; |
| 1595 | } |
| 1596 | |
| 1597 | stream << '%'; |
| 1598 | if (it->second != NameSentinel) { |
| 1599 | stream << it->second; |
| 1600 | } else { |
| 1601 | auto nameIt = valueNames.find(Val: lookupValue); |
| 1602 | assert(nameIt != valueNames.end() && "Didn't have a name entry?" ); |
| 1603 | stream << nameIt->second; |
| 1604 | } |
| 1605 | |
| 1606 | if (resultNo && printResultNo) |
| 1607 | stream << '#' << *resultNo; |
| 1608 | } |
| 1609 | |
| 1610 | void SSANameState::printOperationID(Operation *op, raw_ostream &stream) const { |
| 1611 | auto it = operationIDs.find(Val: op); |
| 1612 | if (it == operationIDs.end()) { |
| 1613 | stream << "<<UNKNOWN OPERATION>>" ; |
| 1614 | } else { |
| 1615 | stream << '%' << it->second; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | ArrayRef<int> SSANameState::getOpResultGroups(Operation *op) { |
| 1620 | auto it = opResultGroups.find(Val: op); |
| 1621 | return it == opResultGroups.end() ? ArrayRef<int>() : it->second; |
| 1622 | } |
| 1623 | |
| 1624 | BlockInfo SSANameState::getBlockInfo(Block *block) { |
| 1625 | auto it = blockNames.find(Val: block); |
| 1626 | BlockInfo invalidBlock{.ordering: -1, .name: "INVALIDBLOCK" }; |
| 1627 | return it != blockNames.end() ? it->second : invalidBlock; |
| 1628 | } |
| 1629 | |
| 1630 | void SSANameState::shadowRegionArgs(Region ®ion, ValueRange namesToUse) { |
| 1631 | assert(!region.empty() && "cannot shadow arguments of an empty region" ); |
| 1632 | assert(region.getNumArguments() == namesToUse.size() && |
| 1633 | "incorrect number of names passed in" ); |
| 1634 | assert(region.getParentOp()->hasTrait<OpTrait::IsIsolatedFromAbove>() && |
| 1635 | "only KnownIsolatedFromAbove ops can shadow names" ); |
| 1636 | |
| 1637 | SmallVector<char, 16> nameStr; |
| 1638 | for (unsigned i = 0, e = namesToUse.size(); i != e; ++i) { |
| 1639 | auto nameToUse = namesToUse[i]; |
| 1640 | if (nameToUse == nullptr) |
| 1641 | continue; |
| 1642 | auto nameToReplace = region.getArgument(i); |
| 1643 | |
| 1644 | nameStr.clear(); |
| 1645 | llvm::raw_svector_ostream nameStream(nameStr); |
| 1646 | printValueID(value: nameToUse, /*printResultNo=*/true, stream&: nameStream); |
| 1647 | |
| 1648 | // Entry block arguments should already have a pretty "arg" name. |
| 1649 | assert(valueIDs[nameToReplace] == NameSentinel); |
| 1650 | |
| 1651 | // Use the name without the leading %. |
| 1652 | auto name = StringRef(nameStream.str()).drop_front(); |
| 1653 | |
| 1654 | // Overwrite the name. |
| 1655 | valueNames[nameToReplace] = name.copy(A&: usedNameAllocator); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | namespace { |
| 1660 | /// Try to get value name from value's location, fallback to `name`. |
| 1661 | StringRef maybeGetValueNameFromLoc(Value value, StringRef name) { |
| 1662 | if (auto maybeNameLoc = value.getLoc()->findInstanceOf<NameLoc>()) |
| 1663 | return maybeNameLoc.getName(); |
| 1664 | return name; |
| 1665 | } |
| 1666 | } // namespace |
| 1667 | |
| 1668 | void SSANameState::numberValuesInRegion(Region ®ion) { |
| 1669 | // Indicates whether OpAsmOpInterface set a name. |
| 1670 | bool opAsmOpInterfaceUsed = false; |
| 1671 | auto setBlockArgNameFn = [&](Value arg, StringRef name) { |
| 1672 | assert(!valueIDs.count(arg) && "arg numbered multiple times" ); |
| 1673 | assert(llvm::cast<BlockArgument>(arg).getOwner()->getParent() == ®ion && |
| 1674 | "arg not defined in current region" ); |
| 1675 | opAsmOpInterfaceUsed = true; |
| 1676 | if (LLVM_UNLIKELY(printerFlags.shouldUseNameLocAsPrefix())) |
| 1677 | name = maybeGetValueNameFromLoc(value: arg, name); |
| 1678 | setValueName(value: arg, name); |
| 1679 | }; |
| 1680 | |
| 1681 | if (!printerFlags.shouldPrintGenericOpForm()) { |
| 1682 | if (Operation *op = region.getParentOp()) { |
| 1683 | if (auto asmInterface = dyn_cast<OpAsmOpInterface>(Val: op)) |
| 1684 | asmInterface.getAsmBlockArgumentNames(region, setNameFn: setBlockArgNameFn); |
| 1685 | // If the OpAsmOpInterface didn't set a name, get name from the type. |
| 1686 | if (!opAsmOpInterfaceUsed) { |
| 1687 | for (BlockArgument arg : region.getArguments()) { |
| 1688 | if (auto interface = dyn_cast<OpAsmTypeInterface>(Val: arg.getType())) { |
| 1689 | interface.getAsmName( |
| 1690 | setNameFn: [&](StringRef name) { setBlockArgNameFn(arg, name); }); |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | // Number the values within this region in a breadth-first order. |
| 1698 | unsigned nextBlockID = 0; |
| 1699 | for (auto &block : region) { |
| 1700 | // Each block gets a unique ID, and all of the operations within it get |
| 1701 | // numbered as well. |
| 1702 | auto blockInfoIt = blockNames.insert(KV: {&block, {.ordering: -1, .name: "" }}); |
| 1703 | if (blockInfoIt.second) { |
| 1704 | // This block hasn't been named through `getAsmBlockArgumentNames`, use |
| 1705 | // default `^bbNNN` format. |
| 1706 | std::string name; |
| 1707 | llvm::raw_string_ostream(name) << "^bb" << nextBlockID; |
| 1708 | blockInfoIt.first->second.name = StringRef(name).copy(A&: usedNameAllocator); |
| 1709 | } |
| 1710 | blockInfoIt.first->second.ordering = nextBlockID++; |
| 1711 | |
| 1712 | numberValuesInBlock(block); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | void SSANameState::numberValuesInBlock(Block &block) { |
| 1717 | // Number the block arguments. We give entry block arguments a special name |
| 1718 | // 'arg'. |
| 1719 | bool isEntryBlock = block.isEntryBlock(); |
| 1720 | SmallString<32> specialNameBuffer(isEntryBlock ? "arg" : "" ); |
| 1721 | llvm::raw_svector_ostream specialName(specialNameBuffer); |
| 1722 | for (auto arg : block.getArguments()) { |
| 1723 | if (valueIDs.count(Val: arg)) |
| 1724 | continue; |
| 1725 | if (isEntryBlock) { |
| 1726 | specialNameBuffer.resize(N: strlen(s: "arg" )); |
| 1727 | specialName << nextArgumentID++; |
| 1728 | } |
| 1729 | StringRef specialNameStr = specialName.str(); |
| 1730 | if (LLVM_UNLIKELY(printerFlags.shouldUseNameLocAsPrefix())) |
| 1731 | specialNameStr = maybeGetValueNameFromLoc(value: arg, name: specialNameStr); |
| 1732 | setValueName(value: arg, name: specialNameStr); |
| 1733 | } |
| 1734 | |
| 1735 | // Number the operations in this block. |
| 1736 | for (auto &op : block) |
| 1737 | numberValuesInOp(op); |
| 1738 | } |
| 1739 | |
| 1740 | void SSANameState::numberValuesInOp(Operation &op) { |
| 1741 | // Function used to set the special result names for the operation. |
| 1742 | SmallVector<int, 2> resultGroups(/*Size=*/1, /*Value=*/0); |
| 1743 | // Indicates whether OpAsmOpInterface set a name. |
| 1744 | bool opAsmOpInterfaceUsed = false; |
| 1745 | auto setResultNameFn = [&](Value result, StringRef name) { |
| 1746 | assert(!valueIDs.count(result) && "result numbered multiple times" ); |
| 1747 | assert(result.getDefiningOp() == &op && "result not defined by 'op'" ); |
| 1748 | opAsmOpInterfaceUsed = true; |
| 1749 | if (LLVM_UNLIKELY(printerFlags.shouldUseNameLocAsPrefix())) |
| 1750 | name = maybeGetValueNameFromLoc(value: result, name); |
| 1751 | setValueName(value: result, name); |
| 1752 | |
| 1753 | // Record the result number for groups not anchored at 0. |
| 1754 | if (int resultNo = llvm::cast<OpResult>(Val&: result).getResultNumber()) |
| 1755 | resultGroups.push_back(Elt: resultNo); |
| 1756 | }; |
| 1757 | // Operations can customize the printing of block names in OpAsmOpInterface. |
| 1758 | auto setBlockNameFn = [&](Block *block, StringRef name) { |
| 1759 | assert(block->getParentOp() == &op && |
| 1760 | "getAsmBlockArgumentNames callback invoked on a block not directly " |
| 1761 | "nested under the current operation" ); |
| 1762 | assert(!blockNames.count(block) && "block numbered multiple times" ); |
| 1763 | SmallString<16> tmpBuffer{"^" }; |
| 1764 | name = sanitizeIdentifier(name, buffer&: tmpBuffer); |
| 1765 | if (name.data() != tmpBuffer.data()) { |
| 1766 | tmpBuffer.append(RHS: name); |
| 1767 | name = tmpBuffer.str(); |
| 1768 | } |
| 1769 | name = name.copy(A&: usedNameAllocator); |
| 1770 | blockNames[block] = {.ordering: -1, .name: name}; |
| 1771 | }; |
| 1772 | |
| 1773 | if (!printerFlags.shouldPrintGenericOpForm()) { |
| 1774 | if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(Val: &op)) { |
| 1775 | asmInterface.getAsmBlockNames(setNameFn: setBlockNameFn); |
| 1776 | asmInterface.getAsmResultNames(setNameFn: setResultNameFn); |
| 1777 | } |
| 1778 | if (!opAsmOpInterfaceUsed) { |
| 1779 | // If the OpAsmOpInterface didn't set a name, and all results have |
| 1780 | // OpAsmTypeInterface, get names from types. |
| 1781 | bool allHaveOpAsmTypeInterface = |
| 1782 | llvm::all_of(Range: op.getResultTypes(), P: [&](Type type) { |
| 1783 | return isa<OpAsmTypeInterface>(Val: type); |
| 1784 | }); |
| 1785 | if (allHaveOpAsmTypeInterface) { |
| 1786 | for (OpResult result : op.getResults()) { |
| 1787 | auto interface = cast<OpAsmTypeInterface>(Val: result.getType()); |
| 1788 | interface.getAsmName( |
| 1789 | setNameFn: [&](StringRef name) { setResultNameFn(result, name); }); |
| 1790 | } |
| 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | unsigned numResults = op.getNumResults(); |
| 1796 | if (numResults == 0) { |
| 1797 | // If value users should be printed, operations with no result need an id. |
| 1798 | if (printerFlags.shouldPrintValueUsers()) { |
| 1799 | if (operationIDs.try_emplace(Key: &op, Args&: nextValueID).second) |
| 1800 | ++nextValueID; |
| 1801 | } |
| 1802 | return; |
| 1803 | } |
| 1804 | Value resultBegin = op.getResult(idx: 0); |
| 1805 | |
| 1806 | if (printerFlags.shouldUseNameLocAsPrefix() && !valueIDs.count(Val: resultBegin)) { |
| 1807 | if (auto nameLoc = resultBegin.getLoc()->findInstanceOf<NameLoc>()) { |
| 1808 | setValueName(value: resultBegin, name: nameLoc.getName()); |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | // If the first result wasn't numbered, give it a default number. |
| 1813 | if (valueIDs.try_emplace(Key: resultBegin, Args&: nextValueID).second) |
| 1814 | ++nextValueID; |
| 1815 | |
| 1816 | // If this operation has multiple result groups, mark it. |
| 1817 | if (resultGroups.size() != 1) { |
| 1818 | llvm::array_pod_sort(Start: resultGroups.begin(), End: resultGroups.end()); |
| 1819 | opResultGroups.try_emplace(Key: &op, Args: std::move(resultGroups)); |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | void SSANameState::getResultIDAndNumber( |
| 1824 | OpResult result, Value &lookupValue, |
| 1825 | std::optional<int> &lookupResultNo) const { |
| 1826 | Operation *owner = result.getOwner(); |
| 1827 | if (owner->getNumResults() == 1) |
| 1828 | return; |
| 1829 | int resultNo = result.getResultNumber(); |
| 1830 | |
| 1831 | // If this operation has multiple result groups, we will need to find the |
| 1832 | // one corresponding to this result. |
| 1833 | auto resultGroupIt = opResultGroups.find(Val: owner); |
| 1834 | if (resultGroupIt == opResultGroups.end()) { |
| 1835 | // If not, just use the first result. |
| 1836 | lookupResultNo = resultNo; |
| 1837 | lookupValue = owner->getResult(idx: 0); |
| 1838 | return; |
| 1839 | } |
| 1840 | |
| 1841 | // Find the correct index using a binary search, as the groups are ordered. |
| 1842 | ArrayRef<int> resultGroups = resultGroupIt->second; |
| 1843 | const auto *it = llvm::upper_bound(Range&: resultGroups, Value&: resultNo); |
| 1844 | int groupResultNo = 0, groupSize = 0; |
| 1845 | |
| 1846 | // If there are no smaller elements, the last result group is the lookup. |
| 1847 | if (it == resultGroups.end()) { |
| 1848 | groupResultNo = resultGroups.back(); |
| 1849 | groupSize = static_cast<int>(owner->getNumResults()) - resultGroups.back(); |
| 1850 | } else { |
| 1851 | // Otherwise, the previous element is the lookup. |
| 1852 | groupResultNo = *std::prev(x: it); |
| 1853 | groupSize = *it - groupResultNo; |
| 1854 | } |
| 1855 | |
| 1856 | // We only record the result number for a group of size greater than 1. |
| 1857 | if (groupSize != 1) |
| 1858 | lookupResultNo = resultNo - groupResultNo; |
| 1859 | lookupValue = owner->getResult(idx: groupResultNo); |
| 1860 | } |
| 1861 | |
| 1862 | void SSANameState::setValueName(Value value, StringRef name) { |
| 1863 | // If the name is empty, the value uses the default numbering. |
| 1864 | if (name.empty()) { |
| 1865 | valueIDs[value] = nextValueID++; |
| 1866 | return; |
| 1867 | } |
| 1868 | |
| 1869 | valueIDs[value] = NameSentinel; |
| 1870 | valueNames[value] = uniqueValueName(name); |
| 1871 | } |
| 1872 | |
| 1873 | StringRef SSANameState::uniqueValueName(StringRef name) { |
| 1874 | SmallString<16> tmpBuffer; |
| 1875 | name = sanitizeIdentifier(name, buffer&: tmpBuffer); |
| 1876 | |
| 1877 | // Check to see if this name is already unique. |
| 1878 | if (!usedNames.count(Key: name)) { |
| 1879 | name = name.copy(A&: usedNameAllocator); |
| 1880 | } else { |
| 1881 | // Otherwise, we had a conflict - probe until we find a unique name. This |
| 1882 | // is guaranteed to terminate (and usually in a single iteration) because it |
| 1883 | // generates new names by incrementing nextConflictID. |
| 1884 | SmallString<64> probeName(name); |
| 1885 | probeName.push_back(Elt: '_'); |
| 1886 | while (true) { |
| 1887 | probeName += llvm::utostr(X: nextConflictID++); |
| 1888 | if (!usedNames.count(Key: probeName)) { |
| 1889 | name = probeName.str().copy(A&: usedNameAllocator); |
| 1890 | break; |
| 1891 | } |
| 1892 | probeName.resize(N: name.size() + 1); |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | usedNames.insert(Key: name, Val: char()); |
| 1897 | return name; |
| 1898 | } |
| 1899 | |
| 1900 | //===----------------------------------------------------------------------===// |
| 1901 | // DistinctState |
| 1902 | //===----------------------------------------------------------------------===// |
| 1903 | |
| 1904 | namespace { |
| 1905 | /// This class manages the state for distinct attributes. |
| 1906 | class DistinctState { |
| 1907 | public: |
| 1908 | /// Returns a unique identifier for the given distinct attribute. |
| 1909 | uint64_t getId(DistinctAttr distinctAttr); |
| 1910 | |
| 1911 | private: |
| 1912 | uint64_t distinctCounter = 0; |
| 1913 | DenseMap<DistinctAttr, uint64_t> distinctAttrMap; |
| 1914 | }; |
| 1915 | } // namespace |
| 1916 | |
| 1917 | uint64_t DistinctState::getId(DistinctAttr distinctAttr) { |
| 1918 | auto [it, inserted] = |
| 1919 | distinctAttrMap.try_emplace(Key: distinctAttr, Args&: distinctCounter); |
| 1920 | if (inserted) |
| 1921 | distinctCounter++; |
| 1922 | return it->getSecond(); |
| 1923 | } |
| 1924 | |
| 1925 | //===----------------------------------------------------------------------===// |
| 1926 | // Resources |
| 1927 | //===----------------------------------------------------------------------===// |
| 1928 | |
| 1929 | AsmParsedResourceEntry::~AsmParsedResourceEntry() = default; |
| 1930 | AsmResourceBuilder::~AsmResourceBuilder() = default; |
| 1931 | AsmResourceParser::~AsmResourceParser() = default; |
| 1932 | AsmResourcePrinter::~AsmResourcePrinter() = default; |
| 1933 | |
| 1934 | StringRef mlir::toString(AsmResourceEntryKind kind) { |
| 1935 | switch (kind) { |
| 1936 | case AsmResourceEntryKind::Blob: |
| 1937 | return "blob" ; |
| 1938 | case AsmResourceEntryKind::Bool: |
| 1939 | return "bool" ; |
| 1940 | case AsmResourceEntryKind::String: |
| 1941 | return "string" ; |
| 1942 | } |
| 1943 | llvm_unreachable("unknown AsmResourceEntryKind" ); |
| 1944 | } |
| 1945 | |
| 1946 | AsmResourceParser &FallbackAsmResourceMap::getParserFor(StringRef key) { |
| 1947 | std::unique_ptr<ResourceCollection> &collection = keyToResources[key.str()]; |
| 1948 | if (!collection) |
| 1949 | collection = std::make_unique<ResourceCollection>(args&: key); |
| 1950 | return *collection; |
| 1951 | } |
| 1952 | |
| 1953 | std::vector<std::unique_ptr<AsmResourcePrinter>> |
| 1954 | FallbackAsmResourceMap::getPrinters() { |
| 1955 | std::vector<std::unique_ptr<AsmResourcePrinter>> printers; |
| 1956 | for (auto &it : keyToResources) { |
| 1957 | ResourceCollection *collection = it.second.get(); |
| 1958 | auto buildValues = [=](Operation *op, AsmResourceBuilder &builder) { |
| 1959 | return collection->buildResources(op, builder); |
| 1960 | }; |
| 1961 | printers.emplace_back( |
| 1962 | args: AsmResourcePrinter::fromCallable(name: collection->getName(), printFn&: buildValues)); |
| 1963 | } |
| 1964 | return printers; |
| 1965 | } |
| 1966 | |
| 1967 | LogicalResult FallbackAsmResourceMap::ResourceCollection::parseResource( |
| 1968 | AsmParsedResourceEntry &entry) { |
| 1969 | switch (entry.getKind()) { |
| 1970 | case AsmResourceEntryKind::Blob: { |
| 1971 | FailureOr<AsmResourceBlob> blob = entry.parseAsBlob(); |
| 1972 | if (failed(Result: blob)) |
| 1973 | return failure(); |
| 1974 | resources.emplace_back(Args: entry.getKey(), Args: std::move(*blob)); |
| 1975 | return success(); |
| 1976 | } |
| 1977 | case AsmResourceEntryKind::Bool: { |
| 1978 | FailureOr<bool> value = entry.parseAsBool(); |
| 1979 | if (failed(Result: value)) |
| 1980 | return failure(); |
| 1981 | resources.emplace_back(Args: entry.getKey(), Args&: *value); |
| 1982 | break; |
| 1983 | } |
| 1984 | case AsmResourceEntryKind::String: { |
| 1985 | FailureOr<std::string> str = entry.parseAsString(); |
| 1986 | if (failed(Result: str)) |
| 1987 | return failure(); |
| 1988 | resources.emplace_back(Args: entry.getKey(), Args: std::move(*str)); |
| 1989 | break; |
| 1990 | } |
| 1991 | } |
| 1992 | return success(); |
| 1993 | } |
| 1994 | |
| 1995 | void FallbackAsmResourceMap::ResourceCollection::buildResources( |
| 1996 | Operation *op, AsmResourceBuilder &builder) const { |
| 1997 | for (const auto &entry : resources) { |
| 1998 | if (const auto *value = std::get_if<AsmResourceBlob>(ptr: &entry.value)) |
| 1999 | builder.buildBlob(key: entry.key, blob: *value); |
| 2000 | else if (const auto *value = std::get_if<bool>(ptr: &entry.value)) |
| 2001 | builder.buildBool(key: entry.key, data: *value); |
| 2002 | else if (const auto *value = std::get_if<std::string>(ptr: &entry.value)) |
| 2003 | builder.buildString(key: entry.key, data: *value); |
| 2004 | else |
| 2005 | llvm_unreachable("unknown AsmResourceEntryKind" ); |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | //===----------------------------------------------------------------------===// |
| 2010 | // AsmState |
| 2011 | //===----------------------------------------------------------------------===// |
| 2012 | |
| 2013 | namespace mlir { |
| 2014 | namespace detail { |
| 2015 | class AsmStateImpl { |
| 2016 | public: |
| 2017 | explicit AsmStateImpl(Operation *op, const OpPrintingFlags &printerFlags, |
| 2018 | AsmState::LocationMap *locationMap) |
| 2019 | : interfaces(op->getContext()), nameState(op, printerFlags), |
| 2020 | printerFlags(printerFlags), locationMap(locationMap) {} |
| 2021 | explicit AsmStateImpl(MLIRContext *ctx, const OpPrintingFlags &printerFlags, |
| 2022 | AsmState::LocationMap *locationMap) |
| 2023 | : interfaces(ctx), printerFlags(printerFlags), locationMap(locationMap) {} |
| 2024 | |
| 2025 | /// Initialize the alias state to enable the printing of aliases. |
| 2026 | void initializeAliases(Operation *op) { |
| 2027 | // A state reused across prints must not re-print the previous op's |
| 2028 | // dialect resources. |
| 2029 | dialectResources.clear(); |
| 2030 | aliasState.initialize(op, printerFlags, interfaces); |
| 2031 | } |
| 2032 | |
| 2033 | /// Get the state used for aliases. |
| 2034 | AliasState &getAliasState() { return aliasState; } |
| 2035 | |
| 2036 | /// Get the state used for SSA names. |
| 2037 | SSANameState &getSSANameState() { return nameState; } |
| 2038 | |
| 2039 | /// Get the state used for distinct attribute identifiers. |
| 2040 | DistinctState &getDistinctState() { return distinctState; } |
| 2041 | |
| 2042 | /// Return the dialects within the context that implement |
| 2043 | /// OpAsmDialectInterface. |
| 2044 | DialectInterfaceCollection<OpAsmDialectInterface> &getDialectInterfaces() { |
| 2045 | return interfaces; |
| 2046 | } |
| 2047 | |
| 2048 | /// Return the non-dialect resource printers. |
| 2049 | auto getResourcePrinters() { |
| 2050 | return llvm::make_pointee_range(Range&: externalResourcePrinters); |
| 2051 | } |
| 2052 | |
| 2053 | /// Get the printer flags. |
| 2054 | const OpPrintingFlags &getPrinterFlags() const { return printerFlags; } |
| 2055 | |
| 2056 | /// Register the location, line and column, within the buffer that the given |
| 2057 | /// operation was printed at. |
| 2058 | void registerOperationLocation(Operation *op, unsigned line, unsigned col) { |
| 2059 | if (locationMap) |
| 2060 | (*locationMap)[op] = std::make_pair(x&: line, y&: col); |
| 2061 | } |
| 2062 | |
| 2063 | /// Return the referenced dialect resources within the printer. |
| 2064 | DenseMap<Dialect *, SetVector<AsmDialectResourceHandle>> & |
| 2065 | getDialectResources() { |
| 2066 | return dialectResources; |
| 2067 | } |
| 2068 | |
| 2069 | LogicalResult pushCyclicPrinting(const void *opaquePointer) { |
| 2070 | return success(IsSuccess: cyclicPrintingStack.insert(X: opaquePointer)); |
| 2071 | } |
| 2072 | |
| 2073 | void popCyclicPrinting() { cyclicPrintingStack.pop_back(); } |
| 2074 | |
| 2075 | private: |
| 2076 | /// Collection of OpAsm interfaces implemented in the context. |
| 2077 | DialectInterfaceCollection<OpAsmDialectInterface> interfaces; |
| 2078 | |
| 2079 | /// A collection of non-dialect resource printers. |
| 2080 | SmallVector<std::unique_ptr<AsmResourcePrinter>> externalResourcePrinters; |
| 2081 | |
| 2082 | /// A set of dialect resources that were referenced during printing. |
| 2083 | DenseMap<Dialect *, SetVector<AsmDialectResourceHandle>> dialectResources; |
| 2084 | |
| 2085 | /// The state used for attribute and type aliases. |
| 2086 | AliasState aliasState; |
| 2087 | |
| 2088 | /// The state used for SSA value names. |
| 2089 | SSANameState nameState; |
| 2090 | |
| 2091 | /// The state used for distinct attribute identifiers. |
| 2092 | DistinctState distinctState; |
| 2093 | |
| 2094 | /// Flags that control op output. |
| 2095 | OpPrintingFlags printerFlags; |
| 2096 | |
| 2097 | /// An optional location map to be populated. |
| 2098 | AsmState::LocationMap *locationMap; |
| 2099 | |
| 2100 | /// Stack of potentially cyclic mutable attributes or type currently being |
| 2101 | /// printed. |
| 2102 | SetVector<const void *> cyclicPrintingStack; |
| 2103 | |
| 2104 | // Allow direct access to the impl fields. |
| 2105 | friend AsmState; |
| 2106 | }; |
| 2107 | |
| 2108 | template <typename Range> |
| 2109 | static void printDimensionList(raw_ostream &stream, Range &&shape) { |
| 2110 | llvm::interleave( |
| 2111 | shape, stream, |
| 2112 | [&stream](const auto &dimSize) { |
| 2113 | if (ShapedType::isDynamic(dValue: dimSize)) |
| 2114 | stream << "?" ; |
| 2115 | else |
| 2116 | stream << dimSize; |
| 2117 | }, |
| 2118 | "x" ); |
| 2119 | } |
| 2120 | |
| 2121 | } // namespace detail |
| 2122 | } // namespace mlir |
| 2123 | |
| 2124 | /// Verifies the operation and switches to generic op printing if verification |
| 2125 | /// fails. We need to do this because custom print functions may fail for |
| 2126 | /// invalid ops. |
| 2127 | static OpPrintingFlags verifyOpAndAdjustFlags(Operation *op, |
| 2128 | OpPrintingFlags printerFlags) { |
| 2129 | if (printerFlags.shouldPrintGenericOpForm() || |
| 2130 | printerFlags.shouldAssumeVerified()) |
| 2131 | return printerFlags; |
| 2132 | |
| 2133 | // Ignore errors emitted by the verifier. We check the thread id to avoid |
| 2134 | // consuming other threads' errors. |
| 2135 | auto parentThreadId = llvm::get_threadid(); |
| 2136 | ScopedDiagnosticHandler diagHandler(op->getContext(), [&](Diagnostic &diag) { |
| 2137 | if (parentThreadId == llvm::get_threadid()) { |
| 2138 | LLVM_DEBUG({ |
| 2139 | diag.print(llvm::dbgs()); |
| 2140 | llvm::dbgs() << "\n" ; |
| 2141 | }); |
| 2142 | return success(); |
| 2143 | } |
| 2144 | return failure(); |
| 2145 | }); |
| 2146 | if (failed(Result: verify(op))) { |
| 2147 | LDBG() << op->getName() |
| 2148 | << "' failed to verify and will be printed in generic form" ; |
| 2149 | printerFlags.printGenericOpForm(); |
| 2150 | } |
| 2151 | |
| 2152 | return printerFlags; |
| 2153 | } |
| 2154 | |
| 2155 | AsmState::AsmState(Operation *op, const OpPrintingFlags &printerFlags, |
| 2156 | LocationMap *locationMap, FallbackAsmResourceMap *map) |
| 2157 | : impl(std::make_unique<AsmStateImpl>( |
| 2158 | args&: op, args: verifyOpAndAdjustFlags(op, printerFlags), args&: locationMap)) { |
| 2159 | if (map) |
| 2160 | attachFallbackResourcePrinter(map&: *map); |
| 2161 | } |
| 2162 | AsmState::AsmState(MLIRContext *ctx, const OpPrintingFlags &printerFlags, |
| 2163 | LocationMap *locationMap, FallbackAsmResourceMap *map) |
| 2164 | : impl(std::make_unique<AsmStateImpl>(args&: ctx, args: printerFlags, args&: locationMap)) { |
| 2165 | if (map) |
| 2166 | attachFallbackResourcePrinter(map&: *map); |
| 2167 | } |
| 2168 | AsmState::~AsmState() = default; |
| 2169 | |
| 2170 | const OpPrintingFlags &AsmState::getPrinterFlags() const { |
| 2171 | return impl->getPrinterFlags(); |
| 2172 | } |
| 2173 | |
| 2174 | void AsmState::attachResourcePrinter( |
| 2175 | std::unique_ptr<AsmResourcePrinter> printer) { |
| 2176 | impl->externalResourcePrinters.emplace_back(Args: std::move(printer)); |
| 2177 | } |
| 2178 | |
| 2179 | DenseMap<Dialect *, SetVector<AsmDialectResourceHandle>> & |
| 2180 | AsmState::getDialectResources() const { |
| 2181 | return impl->getDialectResources(); |
| 2182 | } |
| 2183 | |
| 2184 | //===----------------------------------------------------------------------===// |
| 2185 | // AsmPrinter::Impl |
| 2186 | //===----------------------------------------------------------------------===// |
| 2187 | |
| 2188 | AsmPrinter::Impl::Impl(raw_ostream &os, AsmStateImpl &state) |
| 2189 | : os(os), state(state), printerFlags(state.getPrinterFlags()) {} |
| 2190 | |
| 2191 | void AsmPrinter::Impl::printTrailingLocation(Location loc, bool allowAlias) { |
| 2192 | // Check to see if we are printing debug information. |
| 2193 | if (!printerFlags.shouldPrintDebugInfo()) |
| 2194 | return; |
| 2195 | |
| 2196 | os << " " ; |
| 2197 | printLocation(loc, /*allowAlias=*/allowAlias); |
| 2198 | } |
| 2199 | |
| 2200 | void AsmPrinter::Impl::printLocationInternal(LocationAttr loc, bool pretty, |
| 2201 | bool isTopLevel) { |
| 2202 | // If this isn't a top-level location, check for an alias. |
| 2203 | if (!isTopLevel && succeeded(Result: state.getAliasState().getAlias(attr: loc, os))) |
| 2204 | return; |
| 2205 | |
| 2206 | TypeSwitch<LocationAttr>(loc) |
| 2207 | .Case(caseFn: [&](OpaqueLoc loc) { |
| 2208 | printLocationInternal(loc: loc.getFallbackLocation(), pretty); |
| 2209 | }) |
| 2210 | .Case(caseFn: [&](UnknownLoc loc) { |
| 2211 | if (pretty) |
| 2212 | os << "[unknown]" ; |
| 2213 | else |
| 2214 | os << "unknown" ; |
| 2215 | }) |
| 2216 | .Case(caseFn: [&](FileLineColRange loc) { |
| 2217 | if (pretty) |
| 2218 | os << loc.getFilename().getValue(); |
| 2219 | else |
| 2220 | printEscapedString(str: loc.getFilename()); |
| 2221 | if (loc.getEndColumn() == loc.getStartColumn() && |
| 2222 | loc.getStartLine() == loc.getEndLine()) { |
| 2223 | os << ':' << loc.getStartLine() << ':' << loc.getStartColumn(); |
| 2224 | return; |
| 2225 | } |
| 2226 | if (loc.getStartLine() == loc.getEndLine()) { |
| 2227 | os << ':' << loc.getStartLine() << ':' << loc.getStartColumn() |
| 2228 | << " to :" << loc.getEndColumn(); |
| 2229 | return; |
| 2230 | } |
| 2231 | os << ':' << loc.getStartLine() << ':' << loc.getStartColumn() << " to " |
| 2232 | << loc.getEndLine() << ':' << loc.getEndColumn(); |
| 2233 | }) |
| 2234 | .Case(caseFn: [&](NameLoc loc) { |
| 2235 | printEscapedString(str: loc.getName()); |
| 2236 | |
| 2237 | // Print the child if it isn't unknown. |
| 2238 | auto childLoc = loc.getChildLoc(); |
| 2239 | if (!llvm::isa<UnknownLoc>(Val: childLoc)) { |
| 2240 | os << '('; |
| 2241 | printLocationInternal(loc: childLoc, pretty); |
| 2242 | os << ')'; |
| 2243 | } |
| 2244 | }) |
| 2245 | .Case(caseFn: [&](CallSiteLoc loc) { |
| 2246 | Location caller = loc.getCaller(); |
| 2247 | Location callee = loc.getCallee(); |
| 2248 | if (!pretty) |
| 2249 | os << "callsite(" ; |
| 2250 | printLocationInternal(loc: callee, pretty); |
| 2251 | if (pretty) { |
| 2252 | if (llvm::isa<NameLoc>(Val: callee)) { |
| 2253 | if (llvm::isa<FileLineColLoc>(Val: caller)) { |
| 2254 | os << " at " ; |
| 2255 | } else { |
| 2256 | os << newLine << " at " ; |
| 2257 | } |
| 2258 | } else { |
| 2259 | os << newLine << " at " ; |
| 2260 | } |
| 2261 | } else { |
| 2262 | os << " at " ; |
| 2263 | } |
| 2264 | printLocationInternal(loc: caller, pretty); |
| 2265 | if (!pretty) |
| 2266 | os << ")" ; |
| 2267 | }) |
| 2268 | .Case(caseFn: [&](FusedLoc loc) { |
| 2269 | if (!pretty) |
| 2270 | os << "fused" ; |
| 2271 | if (Attribute metadata = loc.getMetadata()) { |
| 2272 | os << '<'; |
| 2273 | printAttribute(attr: metadata); |
| 2274 | os << '>'; |
| 2275 | } |
| 2276 | os << '['; |
| 2277 | interleaveComma(c: loc.getLocations(), eachFn: [&](Location loc) { |
| 2278 | printLocationInternal(loc, pretty); |
| 2279 | }); |
| 2280 | os << ']'; |
| 2281 | }) |
| 2282 | .Default(defaultFn: [&](LocationAttr loc) { |
| 2283 | // Assumes that this is a dialect-specific attribute and prints it |
| 2284 | // directly. |
| 2285 | printAttribute(attr: loc); |
| 2286 | }); |
| 2287 | } |
| 2288 | |
| 2289 | /// Print a floating point value in a way that the parser will be able to |
| 2290 | /// round-trip losslessly. |
| 2291 | static void printFloatValue(const APFloat &apValue, raw_ostream &os, |
| 2292 | bool *printedHex = nullptr) { |
| 2293 | // We would like to output the FP constant value in exponential notation, |
| 2294 | // but we cannot do this if doing so will lose precision. Check here to |
| 2295 | // make sure that we only output it in exponential format if we can parse |
| 2296 | // the value back and get the same value. |
| 2297 | bool isInf = apValue.isInfinity(); |
| 2298 | bool isNaN = apValue.isNaN(); |
| 2299 | if (!isInf && !isNaN) { |
| 2300 | SmallString<128> strValue; |
| 2301 | bool isLossless = apValue.toStringRoundTrip(Str&: strValue, |
| 2302 | /*FormatPrecision=*/6, |
| 2303 | /*FormatMaxPadding=*/0, |
| 2304 | /*TruncateZero=*/false); |
| 2305 | |
| 2306 | // Check to make sure that the stringized number is not some string like |
| 2307 | // "Inf" or NaN, that atof will accept, but the lexer will not. Check |
| 2308 | // that the string matches the "[-+]?[0-9]" regex. |
| 2309 | assert(((strValue[0] >= '0' && strValue[0] <= '9') || |
| 2310 | ((strValue[0] == '-' || strValue[0] == '+') && |
| 2311 | (strValue[1] >= '0' && strValue[1] <= '9'))) && |
| 2312 | "[-+]?[0-9] regex does not match!" ); |
| 2313 | |
| 2314 | // Lossless if it parses back to the same value. |
| 2315 | if (isLossless) { |
| 2316 | os << strValue; |
| 2317 | return; |
| 2318 | } |
| 2319 | |
| 2320 | // If it is not, use the default format of APFloat instead of the |
| 2321 | // exponential notation. |
| 2322 | strValue.clear(); |
| 2323 | apValue.toString(Str&: strValue); |
| 2324 | |
| 2325 | // Make sure that we can parse the default form as a float. |
| 2326 | if (strValue.str().contains(C: '.')) { |
| 2327 | os << strValue; |
| 2328 | return; |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | // Print special values in hexadecimal format. The sign bit should be included |
| 2333 | // in the literal. |
| 2334 | if (printedHex) |
| 2335 | *printedHex = true; |
| 2336 | SmallVector<char, 16> str; |
| 2337 | APInt apInt = apValue.bitcastToAPInt(); |
| 2338 | apInt.toString(Str&: str, /*Radix=*/16, /*Signed=*/false, |
| 2339 | /*formatAsCLiteral=*/true); |
| 2340 | os << str; |
| 2341 | } |
| 2342 | |
| 2343 | void AsmPrinter::Impl::printLocation(LocationAttr loc, bool allowAlias) { |
| 2344 | if (printerFlags.shouldPrintDebugInfoPrettyForm()) |
| 2345 | return printLocationInternal(loc, /*pretty=*/true, /*isTopLevel=*/true); |
| 2346 | |
| 2347 | os << "loc(" ; |
| 2348 | if (!allowAlias || failed(Result: printAlias(attr: loc))) |
| 2349 | printLocationInternal(loc, /*pretty=*/false, /*isTopLevel=*/true); |
| 2350 | os << ')'; |
| 2351 | } |
| 2352 | |
| 2353 | /// Returns true if the given dialect symbol data is simple enough to print in |
| 2354 | /// the pretty form. This is essentially when the symbol takes the form: |
| 2355 | /// identifier (`<` body `>`)? |
| 2356 | static bool isDialectSymbolSimpleEnoughForPrettyForm(StringRef symName) { |
| 2357 | // The name must start with an identifier. |
| 2358 | if (symName.empty() || !isalpha(symName.front())) |
| 2359 | return false; |
| 2360 | |
| 2361 | // Ignore all the characters that are valid in an identifier in the symbol |
| 2362 | // name. |
| 2363 | symName = symName.drop_while( |
| 2364 | F: [](char c) { return llvm::isAlnum(C: c) || c == '.' || c == '_'; }); |
| 2365 | if (symName.empty()) |
| 2366 | return true; |
| 2367 | |
| 2368 | // If we got to an unexpected character, then it must be a <>. Check that the |
| 2369 | // rest of the symbol is wrapped within <>. |
| 2370 | return symName.front() == '<' && symName.back() == '>'; |
| 2371 | } |
| 2372 | |
| 2373 | /// Print the given dialect symbol to the stream. |
| 2374 | static void printDialectSymbol(raw_ostream &os, StringRef symPrefix, |
| 2375 | StringRef dialectName, StringRef symString) { |
| 2376 | os << symPrefix << dialectName; |
| 2377 | |
| 2378 | // If this symbol name is simple enough, print it directly in pretty form, |
| 2379 | // otherwise, we print it as an escaped string. |
| 2380 | if (isDialectSymbolSimpleEnoughForPrettyForm(symName: symString)) { |
| 2381 | os << '.' << symString; |
| 2382 | return; |
| 2383 | } |
| 2384 | |
| 2385 | os << '<' << symString << '>'; |
| 2386 | } |
| 2387 | |
| 2388 | /// Returns true if the given string can be represented as a bare identifier. |
| 2389 | static bool isBareIdentifier(StringRef name) { |
| 2390 | // By making this unsigned, the value passed in to isalnum will always be |
| 2391 | // in the range 0-255. This is important when building with MSVC because |
| 2392 | // its implementation will assert. This situation can arise when dealing |
| 2393 | // with UTF-8 multibyte characters. |
| 2394 | if (name.empty() || (!isalpha(name[0]) && name[0] != '_')) |
| 2395 | return false; |
| 2396 | return llvm::all_of(Range: name.drop_front(), P: [](unsigned char c) { |
| 2397 | return isalnum(c) || c == '_' || c == '$' || c == '.'; |
| 2398 | }); |
| 2399 | } |
| 2400 | |
| 2401 | /// Print the given string as a keyword, or a quoted and escaped string if it |
| 2402 | /// has any special or non-printable characters in it. |
| 2403 | static void printKeywordOrString(StringRef keyword, raw_ostream &os) { |
| 2404 | // If it can be represented as a bare identifier, write it directly. |
| 2405 | if (isBareIdentifier(name: keyword)) { |
| 2406 | os << keyword; |
| 2407 | return; |
| 2408 | } |
| 2409 | |
| 2410 | // Otherwise, output the keyword wrapped in quotes with proper escaping. |
| 2411 | os << "\"" ; |
| 2412 | printEscapedString(Name: keyword, Out&: os); |
| 2413 | os << '"'; |
| 2414 | } |
| 2415 | |
| 2416 | /// Print the given string as a symbol reference. A symbol reference is |
| 2417 | /// represented as a string prefixed with '@'. The reference is surrounded with |
| 2418 | /// ""'s and escaped if it has any special or non-printable characters in it. |
| 2419 | static void printSymbolReference(StringRef symbolRef, raw_ostream &os) { |
| 2420 | if (symbolRef.empty()) { |
| 2421 | os << "@<<INVALID EMPTY SYMBOL>>" ; |
| 2422 | return; |
| 2423 | } |
| 2424 | os << '@'; |
| 2425 | printKeywordOrString(keyword: symbolRef, os); |
| 2426 | } |
| 2427 | |
| 2428 | // Print out a valid ElementsAttr that is succinct and can represent any |
| 2429 | // potential shape/type, for use when eliding a large ElementsAttr. |
| 2430 | // |
| 2431 | // We choose to use a dense resource ElementsAttr literal with conspicuous |
| 2432 | // content to hopefully alert readers to the fact that this has been elided. |
| 2433 | static void printElidedElementsAttr(raw_ostream &os) { |
| 2434 | os << R"(dense_resource<__elided__>)" ; |
| 2435 | } |
| 2436 | |
| 2437 | void AsmPrinter::Impl::printResourceHandle( |
| 2438 | const AsmDialectResourceHandle &resource) { |
| 2439 | auto *interface = cast<OpAsmDialectInterface>(Val: resource.getDialect()); |
| 2440 | ::printKeywordOrString(keyword: interface->getResourceKey(handle: resource), os); |
| 2441 | state.getDialectResources()[resource.getDialect()].insert(X: resource); |
| 2442 | } |
| 2443 | |
| 2444 | LogicalResult AsmPrinter::Impl::printAlias(Attribute attr) { |
| 2445 | return state.getAliasState().getAlias(attr, os); |
| 2446 | } |
| 2447 | |
| 2448 | LogicalResult AsmPrinter::Impl::printAlias(Type type) { |
| 2449 | return state.getAliasState().getAlias(ty: type, os); |
| 2450 | } |
| 2451 | |
| 2452 | void AsmPrinter::Impl::printAttribute(Attribute attr, |
| 2453 | AttrTypeElision typeElision) { |
| 2454 | if (!attr) { |
| 2455 | os << "<<NULL ATTRIBUTE>>" ; |
| 2456 | return; |
| 2457 | } |
| 2458 | |
| 2459 | // Try to print an alias for this attribute. |
| 2460 | if (succeeded(Result: printAlias(attr))) |
| 2461 | return; |
| 2462 | return printAttributeImpl(attr, typeElision); |
| 2463 | } |
| 2464 | void AsmPrinter::Impl::printAttributeImpl(Attribute attr, |
| 2465 | AttrTypeElision typeElision) { |
| 2466 | if (!isa<BuiltinDialect>(Val: attr.getDialect())) { |
| 2467 | printDialectAttribute(attr); |
| 2468 | } else if (auto opaqueAttr = llvm::dyn_cast<OpaqueAttr>(Val&: attr)) { |
| 2469 | printDialectSymbol(os, symPrefix: "#" , dialectName: opaqueAttr.getDialectNamespace(), |
| 2470 | symString: opaqueAttr.getAttrData()); |
| 2471 | } else if (llvm::isa<UnitAttr>(Val: attr)) { |
| 2472 | os << "unit" ; |
| 2473 | return; |
| 2474 | } else if (auto distinctAttr = llvm::dyn_cast<DistinctAttr>(Val&: attr)) { |
| 2475 | os << "distinct[" << state.getDistinctState().getId(distinctAttr) << "]<" ; |
| 2476 | if (!llvm::isa<UnitAttr>(Val: distinctAttr.getReferencedAttr())) { |
| 2477 | printAttribute(attr: distinctAttr.getReferencedAttr()); |
| 2478 | } |
| 2479 | os << '>'; |
| 2480 | return; |
| 2481 | } else if (auto dictAttr = llvm::dyn_cast<DictionaryAttr>(Val&: attr)) { |
| 2482 | os << '{'; |
| 2483 | interleaveComma(c: dictAttr.getValue(), |
| 2484 | eachFn: [&](NamedAttribute attr) { printNamedAttribute(attr); }); |
| 2485 | os << '}'; |
| 2486 | |
| 2487 | } else if (auto intAttr = llvm::dyn_cast<IntegerAttr>(Val&: attr)) { |
| 2488 | Type intType = intAttr.getType(); |
| 2489 | if (intType.isSignlessInteger(width: 1)) { |
| 2490 | os << (intAttr.getValue().getBoolValue() ? "true" : "false" ); |
| 2491 | |
| 2492 | // Boolean integer attributes always elides the type. |
| 2493 | return; |
| 2494 | } |
| 2495 | |
| 2496 | // Only print attributes as unsigned if they are explicitly unsigned or are |
| 2497 | // signless 1-bit values. Indexes, signed values, and multi-bit signless |
| 2498 | // values print as signed. |
| 2499 | bool isUnsigned = |
| 2500 | intType.isUnsignedInteger() || intType.isSignlessInteger(width: 1); |
| 2501 | intAttr.getValue().print(OS&: os, isSigned: !isUnsigned); |
| 2502 | |
| 2503 | // IntegerAttr elides the type if I64. |
| 2504 | if (typeElision == AttrTypeElision::May && intType.isSignlessInteger(width: 64)) |
| 2505 | return; |
| 2506 | |
| 2507 | } else if (auto floatAttr = llvm::dyn_cast<FloatAttr>(Val&: attr)) { |
| 2508 | bool printedHex = false; |
| 2509 | printFloatValue(apValue: floatAttr.getValue(), os, printedHex: &printedHex); |
| 2510 | |
| 2511 | // FloatAttr elides the type if F64. |
| 2512 | if (typeElision == AttrTypeElision::May && floatAttr.getType().isF64() && |
| 2513 | !printedHex) |
| 2514 | return; |
| 2515 | |
| 2516 | } else if (auto strAttr = llvm::dyn_cast<StringAttr>(Val&: attr)) { |
| 2517 | printEscapedString(str: strAttr.getValue()); |
| 2518 | |
| 2519 | } else if (auto arrayAttr = llvm::dyn_cast<ArrayAttr>(Val&: attr)) { |
| 2520 | os << '['; |
| 2521 | interleaveComma(c: arrayAttr.getValue(), eachFn: [&](Attribute attr) { |
| 2522 | printAttribute(attr, typeElision: AttrTypeElision::May); |
| 2523 | }); |
| 2524 | os << ']'; |
| 2525 | |
| 2526 | } else if (auto affineMapAttr = llvm::dyn_cast<AffineMapAttr>(Val&: attr)) { |
| 2527 | os << "affine_map<" ; |
| 2528 | affineMapAttr.getValue().print(os); |
| 2529 | os << '>'; |
| 2530 | |
| 2531 | // AffineMap always elides the type. |
| 2532 | return; |
| 2533 | |
| 2534 | } else if (auto integerSetAttr = llvm::dyn_cast<IntegerSetAttr>(Val&: attr)) { |
| 2535 | os << "affine_set<" ; |
| 2536 | integerSetAttr.getValue().print(os); |
| 2537 | os << '>'; |
| 2538 | |
| 2539 | // IntegerSet always elides the type. |
| 2540 | return; |
| 2541 | |
| 2542 | } else if (auto typeAttr = llvm::dyn_cast<TypeAttr>(Val&: attr)) { |
| 2543 | printType(type: typeAttr.getValue()); |
| 2544 | |
| 2545 | } else if (auto refAttr = llvm::dyn_cast<SymbolRefAttr>(Val&: attr)) { |
| 2546 | printSymbolReference(symbolRef: refAttr.getRootReference().getValue(), os); |
| 2547 | for (FlatSymbolRefAttr nestedRef : refAttr.getNestedReferences()) { |
| 2548 | os << "::" ; |
| 2549 | printSymbolReference(symbolRef: nestedRef.getValue(), os); |
| 2550 | } |
| 2551 | |
| 2552 | } else if (auto intOrFpEltAttr = |
| 2553 | llvm::dyn_cast<DenseTypedElementsAttr>(Val&: attr)) { |
| 2554 | if (printerFlags.shouldElideElementsAttr(attr: intOrFpEltAttr)) { |
| 2555 | printElidedElementsAttr(os); |
| 2556 | } else { |
| 2557 | os << "dense<" ; |
| 2558 | // Check if the element type implements DenseElementTypeInterface and is |
| 2559 | // not a built-in type. Built-in types (int, float, index, complex) use |
| 2560 | // the existing printing format for backwards compatibility. |
| 2561 | Type eltType = intOrFpEltAttr.getElementType(); |
| 2562 | if (isa<FloatType, IntegerType, IndexType, ComplexType>(Val: eltType)) { |
| 2563 | printDenseTypedElementsAttr(attr: intOrFpEltAttr, /*allowHex=*/true); |
| 2564 | } else { |
| 2565 | printTypeFirstDenseElementsAttr(attr: intOrFpEltAttr, |
| 2566 | denseEltType: cast<DenseElementType>(Val&: eltType)); |
| 2567 | typeElision = AttrTypeElision::Must; |
| 2568 | } |
| 2569 | os << '>'; |
| 2570 | } |
| 2571 | |
| 2572 | } else if (auto strEltAttr = llvm::dyn_cast<DenseStringElementsAttr>(Val&: attr)) { |
| 2573 | if (printerFlags.shouldElideElementsAttr(attr: strEltAttr)) { |
| 2574 | printElidedElementsAttr(os); |
| 2575 | } else { |
| 2576 | os << "dense<" ; |
| 2577 | printDenseStringElementsAttr(attr: strEltAttr); |
| 2578 | os << '>'; |
| 2579 | } |
| 2580 | |
| 2581 | } else if (auto sparseEltAttr = llvm::dyn_cast<SparseElementsAttr>(Val&: attr)) { |
| 2582 | if (printerFlags.shouldElideElementsAttr(attr: sparseEltAttr.getIndices()) || |
| 2583 | printerFlags.shouldElideElementsAttr(attr: sparseEltAttr.getValues())) { |
| 2584 | printElidedElementsAttr(os); |
| 2585 | } else { |
| 2586 | os << "sparse<" ; |
| 2587 | DenseIntElementsAttr indices = sparseEltAttr.getIndices(); |
| 2588 | if (indices.getNumElements() != 0) { |
| 2589 | printDenseTypedElementsAttr(attr: indices, /*allowHex=*/false); |
| 2590 | os << ", " ; |
| 2591 | printDenseElementsAttr(attr: sparseEltAttr.getValues(), /*allowHex=*/true); |
| 2592 | } |
| 2593 | os << '>'; |
| 2594 | } |
| 2595 | } else if (auto stridedLayoutAttr = llvm::dyn_cast<StridedLayoutAttr>(Val&: attr)) { |
| 2596 | stridedLayoutAttr.print(os); |
| 2597 | } else if (auto denseArrayAttr = llvm::dyn_cast<DenseArrayAttr>(Val&: attr)) { |
| 2598 | os << "array<" ; |
| 2599 | printType(type: denseArrayAttr.getElementType()); |
| 2600 | if (!denseArrayAttr.empty()) { |
| 2601 | os << ": " ; |
| 2602 | printDenseArrayAttr(attr: denseArrayAttr); |
| 2603 | } |
| 2604 | os << ">" ; |
| 2605 | return; |
| 2606 | } else if (auto resourceAttr = |
| 2607 | llvm::dyn_cast<DenseResourceElementsAttr>(Val&: attr)) { |
| 2608 | os << "dense_resource<" ; |
| 2609 | printResourceHandle(resource: resourceAttr.getRawHandle()); |
| 2610 | os << ">" ; |
| 2611 | } else if (auto locAttr = llvm::dyn_cast<LocationAttr>(Val&: attr)) { |
| 2612 | printLocation(loc: locAttr); |
| 2613 | } else { |
| 2614 | llvm::report_fatal_error(reason: "Unknown builtin attribute" ); |
| 2615 | } |
| 2616 | // Don't print the type if we must elide it, or if it is a None type. |
| 2617 | if (typeElision != AttrTypeElision::Must) { |
| 2618 | if (auto typedAttr = llvm::dyn_cast<TypedAttr>(Val&: attr)) { |
| 2619 | Type attrType = typedAttr.getType(); |
| 2620 | if (!llvm::isa<NoneType>(Val: attrType)) { |
| 2621 | os << " : " ; |
| 2622 | printType(type: attrType); |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | /// Print the integer element of a DenseElementsAttr. |
| 2629 | static void printDenseIntElement(const APInt &value, raw_ostream &os, |
| 2630 | Type type) { |
| 2631 | if (type.isInteger(width: 1)) |
| 2632 | os << (value.getBoolValue() ? "true" : "false" ); |
| 2633 | else |
| 2634 | value.print(OS&: os, isSigned: !type.isUnsignedInteger()); |
| 2635 | } |
| 2636 | |
| 2637 | static void |
| 2638 | printDenseElementsAttrImpl(bool isSplat, ShapedType type, raw_ostream &os, |
| 2639 | function_ref<void(unsigned)> printEltFn) { |
| 2640 | // Special case for 0-d and splat tensors. |
| 2641 | if (isSplat) |
| 2642 | return printEltFn(0); |
| 2643 | |
| 2644 | // Special case for degenerate tensors. |
| 2645 | auto numElements = type.getNumElements(); |
| 2646 | if (numElements == 0) |
| 2647 | return; |
| 2648 | |
| 2649 | // We use a mixed-radix counter to iterate through the shape. When we bump a |
| 2650 | // non-least-significant digit, we emit a close bracket. When we next emit an |
| 2651 | // element we re-open all closed brackets. |
| 2652 | |
| 2653 | // The mixed-radix counter, with radices in 'shape'. |
| 2654 | int64_t rank = type.getRank(); |
| 2655 | SmallVector<unsigned, 4> counter(rank, 0); |
| 2656 | // The number of brackets that have been opened and not closed. |
| 2657 | unsigned openBrackets = 0; |
| 2658 | |
| 2659 | auto shape = type.getShape(); |
| 2660 | auto bumpCounter = [&] { |
| 2661 | // Bump the least significant digit. |
| 2662 | ++counter[rank - 1]; |
| 2663 | // Iterate backwards bubbling back the increment. |
| 2664 | for (unsigned i = rank - 1; i > 0; --i) |
| 2665 | if (counter[i] >= shape[i]) { |
| 2666 | // Index 'i' is rolled over. Bump (i-1) and close a bracket. |
| 2667 | counter[i] = 0; |
| 2668 | ++counter[i - 1]; |
| 2669 | --openBrackets; |
| 2670 | os << ']'; |
| 2671 | } |
| 2672 | }; |
| 2673 | |
| 2674 | for (unsigned idx = 0, e = numElements; idx != e; ++idx) { |
| 2675 | if (idx != 0) |
| 2676 | os << ", " ; |
| 2677 | while (openBrackets++ < rank) |
| 2678 | os << '['; |
| 2679 | openBrackets = rank; |
| 2680 | printEltFn(idx); |
| 2681 | bumpCounter(); |
| 2682 | } |
| 2683 | while (openBrackets-- > 0) |
| 2684 | os << ']'; |
| 2685 | } |
| 2686 | |
| 2687 | void AsmPrinter::Impl::printDenseElementsAttr(DenseElementsAttr attr, |
| 2688 | bool allowHex) { |
| 2689 | if (auto stringAttr = llvm::dyn_cast<DenseStringElementsAttr>(Val&: attr)) |
| 2690 | return printDenseStringElementsAttr(attr: stringAttr); |
| 2691 | |
| 2692 | printDenseTypedElementsAttr(attr: llvm::cast<DenseTypedElementsAttr>(Val&: attr), |
| 2693 | allowHex); |
| 2694 | } |
| 2695 | |
| 2696 | void AsmPrinter::Impl::printDenseTypedElementsAttr(DenseTypedElementsAttr attr, |
| 2697 | bool allowHex) { |
| 2698 | auto type = attr.getType(); |
| 2699 | auto elementType = type.getElementType(); |
| 2700 | |
| 2701 | // Check to see if we should format this attribute as a hex string. |
| 2702 | if (allowHex && |
| 2703 | (elementType.isIntOrIndexOrFloat() || |
| 2704 | llvm::isa<ComplexType>(Val: elementType)) && |
| 2705 | printerFlags.shouldPrintElementsAttrWithHex(attr)) { |
| 2706 | ArrayRef<char> rawData = attr.getRawData(); |
| 2707 | if (llvm::endianness::native == llvm::endianness::big) { |
| 2708 | // Convert endianess in big-endian(BE) machines. `rawData` is BE in BE |
| 2709 | // machines. It is converted here to print in LE format. |
| 2710 | SmallVector<char, 64> outDataVec(rawData.size()); |
| 2711 | MutableArrayRef<char> convRawData(outDataVec); |
| 2712 | DenseTypedElementsAttr::convertEndianOfArrayRefForBEmachine( |
| 2713 | inRawData: rawData, outRawData: convRawData, type); |
| 2714 | printHexString(data: convRawData); |
| 2715 | } else { |
| 2716 | printHexString(data: rawData); |
| 2717 | } |
| 2718 | |
| 2719 | return; |
| 2720 | } |
| 2721 | |
| 2722 | if (ComplexType complexTy = llvm::dyn_cast<ComplexType>(Val&: elementType)) { |
| 2723 | Type complexElementType = complexTy.getElementType(); |
| 2724 | // Note: The if and else below had a common lambda function which invoked |
| 2725 | // printDenseElementsAttrImpl. This lambda was hitting a bug in gcc 9.1,9.2 |
| 2726 | // and hence was replaced. |
| 2727 | if (llvm::isa<IntegerType>(Val: complexElementType)) { |
| 2728 | auto valueIt = attr.value_begin<mlir::Complex<APInt>>(); |
| 2729 | printDenseElementsAttrImpl(isSplat: attr.isSplat(), type, os, printEltFn: [&](unsigned index) { |
| 2730 | auto complexValue = *(valueIt + index); |
| 2731 | os << "(" ; |
| 2732 | printDenseIntElement(value: complexValue.real(), os, type: complexElementType); |
| 2733 | os << "," ; |
| 2734 | printDenseIntElement(value: complexValue.imag(), os, type: complexElementType); |
| 2735 | os << ")" ; |
| 2736 | }); |
| 2737 | } else { |
| 2738 | auto valueIt = attr.value_begin<mlir::Complex<APFloat>>(); |
| 2739 | printDenseElementsAttrImpl(isSplat: attr.isSplat(), type, os, printEltFn: [&](unsigned index) { |
| 2740 | auto complexValue = *(valueIt + index); |
| 2741 | os << "(" ; |
| 2742 | printFloatValue(apValue: complexValue.real(), os); |
| 2743 | os << "," ; |
| 2744 | printFloatValue(apValue: complexValue.imag(), os); |
| 2745 | os << ")" ; |
| 2746 | }); |
| 2747 | } |
| 2748 | } else if (elementType.isIntOrIndex()) { |
| 2749 | auto valueIt = attr.value_begin<APInt>(); |
| 2750 | printDenseElementsAttrImpl(isSplat: attr.isSplat(), type, os, printEltFn: [&](unsigned index) { |
| 2751 | printDenseIntElement(value: *(valueIt + index), os, type: elementType); |
| 2752 | }); |
| 2753 | } else if (llvm::isa<MXI8Type>(Val: elementType)) { |
| 2754 | auto valueIt = attr.value_begin<Attribute>(); |
| 2755 | printDenseElementsAttrImpl(isSplat: attr.isSplat(), type, os, printEltFn: [&](unsigned index) { |
| 2756 | auto value = llvm::cast<IntegerAttr>(Val: *(valueIt + index)); |
| 2757 | printDenseIntElement(value: value.getValue(), os, type: value.getType()); |
| 2758 | }); |
| 2759 | } else { |
| 2760 | assert(llvm::isa<FloatType>(elementType) && "unexpected element type" ); |
| 2761 | auto valueIt = attr.value_begin<APFloat>(); |
| 2762 | printDenseElementsAttrImpl(isSplat: attr.isSplat(), type, os, printEltFn: [&](unsigned index) { |
| 2763 | printFloatValue(apValue: *(valueIt + index), os); |
| 2764 | }); |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | void AsmPrinter::Impl::printDenseStringElementsAttr( |
| 2769 | DenseStringElementsAttr attr) { |
| 2770 | ArrayRef<StringRef> data = attr.getRawStringData(); |
| 2771 | auto printFn = [&](unsigned index) { printEscapedString(str: data[index]); }; |
| 2772 | printDenseElementsAttrImpl(isSplat: attr.isSplat(), type: attr.getType(), os, printEltFn: printFn); |
| 2773 | } |
| 2774 | |
| 2775 | void AsmPrinter::Impl::printTypeFirstDenseElementsAttr( |
| 2776 | DenseElementsAttr attr, DenseElementType denseEltType) { |
| 2777 | // Print the type first: dense<TYPE : [ELEMENTS]> |
| 2778 | printType(type: attr.getType()); |
| 2779 | os << " : " ; |
| 2780 | |
| 2781 | ArrayRef<char> rawData = attr.getRawData(); |
| 2782 | // Storage is byte-aligned: align bit size up to next byte boundary. |
| 2783 | size_t bitSize = denseEltType.getDenseElementBitSize(); |
| 2784 | size_t byteSize = llvm::divideCeil(Numerator: bitSize, Denominator: static_cast<size_t>(CHAR_BIT)); |
| 2785 | |
| 2786 | // Print elements: convert raw bytes to attribute, then print attribute. |
| 2787 | printDenseElementsAttrImpl( |
| 2788 | isSplat: attr.isSplat(), type: attr.getType(), os, printEltFn: [&](unsigned index) { |
| 2789 | size_t offset = attr.isSplat() ? 0 : index * byteSize; |
| 2790 | ArrayRef<char> elemData = rawData.slice(N: offset, M: byteSize); |
| 2791 | Attribute elemAttr = denseEltType.convertToAttribute(rawData: elemData); |
| 2792 | printAttributeImpl(attr: elemAttr); |
| 2793 | }); |
| 2794 | } |
| 2795 | |
| 2796 | void AsmPrinter::Impl::printDenseArrayAttr(DenseArrayAttr attr) { |
| 2797 | Type type = attr.getElementType(); |
| 2798 | unsigned bitwidth = type.isInteger(width: 1) ? 8 : type.getIntOrFloatBitWidth(); |
| 2799 | unsigned byteSize = bitwidth / 8; |
| 2800 | ArrayRef<char> data = attr.getRawData(); |
| 2801 | |
| 2802 | auto printElementAt = [&](unsigned i) { |
| 2803 | APInt value(bitwidth, 0); |
| 2804 | if (bitwidth) { |
| 2805 | llvm::LoadIntFromMemory( |
| 2806 | IntVal&: value, Src: reinterpret_cast<const uint8_t *>(data.begin() + byteSize * i), |
| 2807 | LoadBytes: byteSize); |
| 2808 | } |
| 2809 | // Print the data as-is or as a float. |
| 2810 | if (type.isIntOrIndex()) { |
| 2811 | printDenseIntElement(value, os&: getStream(), type); |
| 2812 | } else { |
| 2813 | APFloat fltVal(llvm::cast<FloatType>(Val&: type).getFloatSemantics(), value); |
| 2814 | printFloatValue(apValue: fltVal, os&: getStream()); |
| 2815 | } |
| 2816 | }; |
| 2817 | llvm::interleaveComma(c: llvm::seq<unsigned>(Begin: 0, End: attr.size()), os&: getStream(), |
| 2818 | each_fn: printElementAt); |
| 2819 | } |
| 2820 | |
| 2821 | void AsmPrinter::Impl::printType(Type type) { |
| 2822 | if (!type) { |
| 2823 | os << "<<NULL TYPE>>" ; |
| 2824 | return; |
| 2825 | } |
| 2826 | |
| 2827 | // Try to print an alias for this type. |
| 2828 | if (succeeded(Result: printAlias(type))) |
| 2829 | return; |
| 2830 | return printTypeImpl(type); |
| 2831 | } |
| 2832 | |
| 2833 | void AsmPrinter::Impl::printTypeImpl(Type type) { |
| 2834 | TypeSwitch<Type>(type) |
| 2835 | .Case(caseFn: [&](OpaqueType opaqueTy) { |
| 2836 | printDialectSymbol(os, symPrefix: "!" , dialectName: opaqueTy.getDialectNamespace(), |
| 2837 | symString: opaqueTy.getTypeData()); |
| 2838 | }) |
| 2839 | .Case<IndexType>(caseFn: [&](Type) { os << "index" ; }) |
| 2840 | .Case<Float4E2M1FNType>(caseFn: [&](Type) { os << "f4E2M1FN" ; }) |
| 2841 | .Case<Float6E2M3FNType>(caseFn: [&](Type) { os << "f6E2M3FN" ; }) |
| 2842 | .Case<Float6E3M2FNType>(caseFn: [&](Type) { os << "f6E3M2FN" ; }) |
| 2843 | .Case<Float8E5M2Type>(caseFn: [&](Type) { os << "f8E5M2" ; }) |
| 2844 | .Case<Float8E4M3Type>(caseFn: [&](Type) { os << "f8E4M3" ; }) |
| 2845 | .Case<Float8E4M3FNType>(caseFn: [&](Type) { os << "f8E4M3FN" ; }) |
| 2846 | .Case<Float8E5M2FNUZType>(caseFn: [&](Type) { os << "f8E5M2FNUZ" ; }) |
| 2847 | .Case<Float8E4M3FNUZType>(caseFn: [&](Type) { os << "f8E4M3FNUZ" ; }) |
| 2848 | .Case<Float8E4M3B11FNUZType>(caseFn: [&](Type) { os << "f8E4M3B11FNUZ" ; }) |
| 2849 | .Case<Float8E3M4Type>(caseFn: [&](Type) { os << "f8E3M4" ; }) |
| 2850 | .Case<Float8E8M0FNUType>(caseFn: [&](Type) { os << "f8E8M0FNU" ; }) |
| 2851 | .Case<Float8E5M3FNUType>(caseFn: [&](Type) { os << "f8E5M3FNU" ; }) |
| 2852 | .Case<BFloat16Type>(caseFn: [&](Type) { os << "bf16" ; }) |
| 2853 | .Case<Float16Type>(caseFn: [&](Type) { os << "f16" ; }) |
| 2854 | .Case<FloatTF32Type>(caseFn: [&](Type) { os << "tf32" ; }) |
| 2855 | .Case<Float32Type>(caseFn: [&](Type) { os << "f32" ; }) |
| 2856 | .Case<Float64Type>(caseFn: [&](Type) { os << "f64" ; }) |
| 2857 | .Case<Float80Type>(caseFn: [&](Type) { os << "f80" ; }) |
| 2858 | .Case<Float128Type>(caseFn: [&](Type) { os << "f128" ; }) |
| 2859 | .Case<MXI8Type>(caseFn: [&](Type) { os << "mxi8" ; }) |
| 2860 | .Case(caseFn: [&](IntegerType integerTy) { |
| 2861 | if (integerTy.isSigned()) |
| 2862 | os << 's'; |
| 2863 | else if (integerTy.isUnsigned()) |
| 2864 | os << 'u'; |
| 2865 | os << 'i' << integerTy.getWidth(); |
| 2866 | }) |
| 2867 | .Case(caseFn: [&](FunctionType funcTy) { |
| 2868 | os << '('; |
| 2869 | interleaveComma(c: funcTy.getInputs(), eachFn: [&](Type ty) { printType(type: ty); }); |
| 2870 | os << ") -> " ; |
| 2871 | ArrayRef<Type> results = funcTy.getResults(); |
| 2872 | if (results.size() == 1 && !llvm::isa<FunctionType>(Val: results[0])) { |
| 2873 | printType(type: results[0]); |
| 2874 | } else { |
| 2875 | os << '('; |
| 2876 | interleaveComma(c: results, eachFn: [&](Type ty) { printType(type: ty); }); |
| 2877 | os << ')'; |
| 2878 | } |
| 2879 | }) |
| 2880 | .Case(caseFn: [&](VectorType vectorTy) { |
| 2881 | auto scalableDims = vectorTy.getScalableDims(); |
| 2882 | os << "vector<" ; |
| 2883 | auto vShape = vectorTy.getShape(); |
| 2884 | unsigned lastDim = vShape.size(); |
| 2885 | unsigned dimIdx = 0; |
| 2886 | for (dimIdx = 0; dimIdx < lastDim; dimIdx++) { |
| 2887 | if (!scalableDims.empty() && scalableDims[dimIdx]) |
| 2888 | os << '['; |
| 2889 | os << vShape[dimIdx]; |
| 2890 | if (!scalableDims.empty() && scalableDims[dimIdx]) |
| 2891 | os << ']'; |
| 2892 | os << 'x'; |
| 2893 | } |
| 2894 | printType(type: vectorTy.getElementType()); |
| 2895 | os << '>'; |
| 2896 | }) |
| 2897 | .Case(caseFn: [&](RankedTensorType tensorTy) { |
| 2898 | os << "tensor<" ; |
| 2899 | printDimensionList(shape: tensorTy.getShape()); |
| 2900 | if (!tensorTy.getShape().empty()) |
| 2901 | os << 'x'; |
| 2902 | printType(type: tensorTy.getElementType()); |
| 2903 | // Only print the encoding attribute value if set. |
| 2904 | if (tensorTy.getEncoding()) { |
| 2905 | os << ", " ; |
| 2906 | printAttribute(attr: tensorTy.getEncoding()); |
| 2907 | } |
| 2908 | os << '>'; |
| 2909 | }) |
| 2910 | .Case(caseFn: [&](UnrankedTensorType tensorTy) { |
| 2911 | os << "tensor<*x" ; |
| 2912 | printType(type: tensorTy.getElementType()); |
| 2913 | os << '>'; |
| 2914 | }) |
| 2915 | .Case(caseFn: [&](MemRefType memrefTy) { |
| 2916 | os << "memref<" ; |
| 2917 | printDimensionList(shape: memrefTy.getShape()); |
| 2918 | if (!memrefTy.getShape().empty()) |
| 2919 | os << 'x'; |
| 2920 | printType(type: memrefTy.getElementType()); |
| 2921 | MemRefLayoutAttrInterface layout = memrefTy.getLayout(); |
| 2922 | if (!llvm::isa<AffineMapAttr>(Val: layout) || !layout.isIdentity()) { |
| 2923 | os << ", " ; |
| 2924 | printAttribute(attr: memrefTy.getLayout(), typeElision: AttrTypeElision::May); |
| 2925 | } |
| 2926 | // Only print the memory space if it is the non-default one. |
| 2927 | if (memrefTy.getMemorySpace()) { |
| 2928 | os << ", " ; |
| 2929 | printAttribute(attr: memrefTy.getMemorySpace(), typeElision: AttrTypeElision::May); |
| 2930 | } |
| 2931 | os << '>'; |
| 2932 | }) |
| 2933 | .Case(caseFn: [&](UnrankedMemRefType memrefTy) { |
| 2934 | os << "memref<*x" ; |
| 2935 | printType(type: memrefTy.getElementType()); |
| 2936 | // Only print the memory space if it is the non-default one. |
| 2937 | if (memrefTy.getMemorySpace()) { |
| 2938 | os << ", " ; |
| 2939 | printAttribute(attr: memrefTy.getMemorySpace(), typeElision: AttrTypeElision::May); |
| 2940 | } |
| 2941 | os << '>'; |
| 2942 | }) |
| 2943 | .Case(caseFn: [&](ComplexType complexTy) { |
| 2944 | os << "complex<" ; |
| 2945 | printType(type: complexTy.getElementType()); |
| 2946 | os << '>'; |
| 2947 | }) |
| 2948 | .Case(caseFn: [&](TupleType tupleTy) { |
| 2949 | os << "tuple<" ; |
| 2950 | interleaveComma(c: tupleTy.getTypes(), |
| 2951 | eachFn: [&](Type type) { printType(type); }); |
| 2952 | os << '>'; |
| 2953 | }) |
| 2954 | .Case<NoneType>(caseFn: [&](Type) { os << "none" ; }) |
| 2955 | .Case<TokenType>(caseFn: [&](Type) { os << "token" ; }) |
| 2956 | .Case(caseFn: [&](GraphType graphTy) { |
| 2957 | os << '('; |
| 2958 | interleaveComma(c: graphTy.getInputs(), eachFn: [&](Type ty) { printType(type: ty); }); |
| 2959 | os << ") -> " ; |
| 2960 | ArrayRef<Type> results = graphTy.getResults(); |
| 2961 | if (results.size() == 1 && !isa<FunctionType, GraphType>(Val: results[0])) { |
| 2962 | printType(type: results[0]); |
| 2963 | } else { |
| 2964 | os << '('; |
| 2965 | interleaveComma(c: results, eachFn: [&](Type ty) { printType(type: ty); }); |
| 2966 | os << ')'; |
| 2967 | } |
| 2968 | }) |
| 2969 | .Default(defaultFn: [&](Type type) { return printDialectType(type); }); |
| 2970 | } |
| 2971 | |
| 2972 | void AsmPrinter::Impl::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs, |
| 2973 | ArrayRef<StringRef> elidedAttrs, |
| 2974 | bool withKeyword) { |
| 2975 | // If there are no attributes, then there is nothing to be done. |
| 2976 | if (attrs.empty()) |
| 2977 | return; |
| 2978 | |
| 2979 | // Functor used to print a filtered attribute list. |
| 2980 | auto printFilteredAttributesFn = [&](auto filteredAttrs) { |
| 2981 | // Print the 'attributes' keyword if necessary. |
| 2982 | if (withKeyword) |
| 2983 | os << " attributes" ; |
| 2984 | |
| 2985 | // Otherwise, print them all out in braces. |
| 2986 | os << " {" ; |
| 2987 | interleaveComma(filteredAttrs, |
| 2988 | [&](NamedAttribute attr) { printNamedAttribute(attr); }); |
| 2989 | os << '}'; |
| 2990 | }; |
| 2991 | |
| 2992 | // If no attributes are elided, we can directly print with no filtering. |
| 2993 | if (elidedAttrs.empty()) |
| 2994 | return printFilteredAttributesFn(attrs); |
| 2995 | |
| 2996 | // Otherwise, filter out any attributes that shouldn't be included. |
| 2997 | llvm::SmallDenseSet<StringRef> (elidedAttrs.begin(), |
| 2998 | elidedAttrs.end()); |
| 2999 | auto filteredAttrs = llvm::make_filter_range(Range&: attrs, Pred: [&](NamedAttribute attr) { |
| 3000 | return !elidedAttrsSet.contains(V: attr.getName().strref()); |
| 3001 | }); |
| 3002 | if (!filteredAttrs.empty()) |
| 3003 | printFilteredAttributesFn(filteredAttrs); |
| 3004 | } |
| 3005 | void AsmPrinter::Impl::printNamedAttribute(NamedAttribute attr) { |
| 3006 | // Print the name without quotes if possible. |
| 3007 | ::printKeywordOrString(keyword: attr.getName().strref(), os); |
| 3008 | |
| 3009 | // Pretty printing elides the attribute value for unit attributes. |
| 3010 | if (llvm::isa<UnitAttr>(Val: attr.getValue())) |
| 3011 | return; |
| 3012 | |
| 3013 | os << " = " ; |
| 3014 | printAttribute(attr: attr.getValue()); |
| 3015 | } |
| 3016 | |
| 3017 | void AsmPrinter::Impl::printDialectAttribute(Attribute attr) { |
| 3018 | auto &dialect = attr.getDialect(); |
| 3019 | |
| 3020 | // Ask the dialect to serialize the attribute to a string. |
| 3021 | std::string attrName; |
| 3022 | { |
| 3023 | llvm::raw_string_ostream attrNameStr(attrName); |
| 3024 | Impl subPrinter(attrNameStr, state); |
| 3025 | |
| 3026 | // The values of currentIndent and newLine are assigned to the created |
| 3027 | // subprinter, so that the indent level and number of printed lines can be |
| 3028 | // tracked. |
| 3029 | subPrinter.currentIndent = currentIndent; |
| 3030 | subPrinter.newLine = newLine; |
| 3031 | |
| 3032 | DialectAsmPrinter printer(subPrinter); |
| 3033 | dialect.printAttribute(attr, printer); |
| 3034 | } |
| 3035 | printDialectSymbol(os, symPrefix: "#" , dialectName: dialect.getNamespace(), symString: attrName); |
| 3036 | } |
| 3037 | |
| 3038 | void AsmPrinter::Impl::printDialectType(Type type) { |
| 3039 | auto &dialect = type.getDialect(); |
| 3040 | |
| 3041 | // Ask the dialect to serialize the type to a string. |
| 3042 | std::string typeName; |
| 3043 | { |
| 3044 | llvm::raw_string_ostream typeNameStr(typeName); |
| 3045 | Impl subPrinter(typeNameStr, state); |
| 3046 | |
| 3047 | // The values of currentIndent and newLine are assigned to the created |
| 3048 | // subprinter, so that the indent level and number of printed lines can be |
| 3049 | // tracked. |
| 3050 | subPrinter.currentIndent = currentIndent; |
| 3051 | subPrinter.newLine = newLine; |
| 3052 | |
| 3053 | DialectAsmPrinter printer(subPrinter); |
| 3054 | dialect.printType(type, printer); |
| 3055 | } |
| 3056 | printDialectSymbol(os, symPrefix: "!" , dialectName: dialect.getNamespace(), symString: typeName); |
| 3057 | } |
| 3058 | |
| 3059 | void AsmPrinter::Impl::printEscapedString(StringRef str) { |
| 3060 | os << "\"" ; |
| 3061 | llvm::printEscapedString(Name: str, Out&: os); |
| 3062 | os << "\"" ; |
| 3063 | } |
| 3064 | |
| 3065 | void AsmPrinter::Impl::printHexString(StringRef str) { |
| 3066 | os << "\"0x" << llvm::toHex(Input: str) << "\"" ; |
| 3067 | } |
| 3068 | void AsmPrinter::Impl::printHexString(ArrayRef<char> data) { |
| 3069 | printHexString(str: StringRef(data.data(), data.size())); |
| 3070 | } |
| 3071 | |
| 3072 | LogicalResult AsmPrinter::Impl::pushCyclicPrinting(const void *opaquePointer) { |
| 3073 | return state.pushCyclicPrinting(opaquePointer); |
| 3074 | } |
| 3075 | |
| 3076 | void AsmPrinter::Impl::popCyclicPrinting() { state.popCyclicPrinting(); } |
| 3077 | |
| 3078 | void AsmPrinter::Impl::printDimensionList(ArrayRef<int64_t> shape) { |
| 3079 | detail::printDimensionList(stream&: os, shape); |
| 3080 | } |
| 3081 | |
| 3082 | //===--------------------------------------------------------------------===// |
| 3083 | // AsmPrinter |
| 3084 | //===--------------------------------------------------------------------===// |
| 3085 | |
| 3086 | AsmPrinter::~AsmPrinter() = default; |
| 3087 | |
| 3088 | raw_ostream &AsmPrinter::getStream() const { |
| 3089 | assert(impl && "expected AsmPrinter::getStream to be overriden" ); |
| 3090 | return impl->getStream(); |
| 3091 | } |
| 3092 | |
| 3093 | void AsmPrinter::printNewline() { |
| 3094 | assert(impl && "expected AsmPrinter::printNewLine to be overriden" ); |
| 3095 | impl->printNewline(); |
| 3096 | } |
| 3097 | |
| 3098 | void AsmPrinter::increaseIndent() { |
| 3099 | assert(impl && "expected AsmPrinter::increaseIndent to be overriden" ); |
| 3100 | impl->increaseIndent(); |
| 3101 | } |
| 3102 | |
| 3103 | void AsmPrinter::decreaseIndent() { |
| 3104 | assert(impl && "expected AsmPrinter::decreaseIndent to be overriden" ); |
| 3105 | impl->decreaseIndent(); |
| 3106 | } |
| 3107 | |
| 3108 | /// Print the given floating point value in a stablized form. |
| 3109 | void AsmPrinter::printFloat(const APFloat &value) { |
| 3110 | assert(impl && "expected AsmPrinter::printFloat to be overriden" ); |
| 3111 | printFloatValue(apValue: value, os&: impl->getStream()); |
| 3112 | } |
| 3113 | |
| 3114 | void AsmPrinter::printType(Type type) { |
| 3115 | assert(impl && "expected AsmPrinter::printType to be overriden" ); |
| 3116 | impl->printType(type); |
| 3117 | } |
| 3118 | |
| 3119 | void AsmPrinter::printAttribute(Attribute attr) { |
| 3120 | assert(impl && "expected AsmPrinter::printAttribute to be overriden" ); |
| 3121 | impl->printAttribute(attr); |
| 3122 | } |
| 3123 | |
| 3124 | LogicalResult AsmPrinter::printAlias(Attribute attr) { |
| 3125 | assert(impl && "expected AsmPrinter::printAlias to be overriden" ); |
| 3126 | return impl->printAlias(attr); |
| 3127 | } |
| 3128 | |
| 3129 | LogicalResult AsmPrinter::printAlias(Type type) { |
| 3130 | assert(impl && "expected AsmPrinter::printAlias to be overriden" ); |
| 3131 | return impl->printAlias(type); |
| 3132 | } |
| 3133 | |
| 3134 | void AsmPrinter::printAttributeWithoutType(Attribute attr) { |
| 3135 | assert(impl && |
| 3136 | "expected AsmPrinter::printAttributeWithoutType to be overriden" ); |
| 3137 | impl->printAttribute(attr, typeElision: Impl::AttrTypeElision::Must); |
| 3138 | } |
| 3139 | |
| 3140 | void AsmPrinter::printNamedAttribute(NamedAttribute attr) { |
| 3141 | assert(impl && "expected AsmPrinter::printNamedAttribute to be overriden" ); |
| 3142 | impl->printNamedAttribute(attr); |
| 3143 | } |
| 3144 | |
| 3145 | void AsmPrinter::printKeywordOrString(StringRef keyword) { |
| 3146 | assert(impl && "expected AsmPrinter::printKeywordOrString to be overriden" ); |
| 3147 | ::printKeywordOrString(keyword, os&: impl->getStream()); |
| 3148 | } |
| 3149 | |
| 3150 | void AsmPrinter::printString(StringRef keyword) { |
| 3151 | assert(impl && "expected AsmPrinter::printString to be overriden" ); |
| 3152 | *this << '"'; |
| 3153 | printEscapedString(Name: keyword, Out&: getStream()); |
| 3154 | *this << '"'; |
| 3155 | } |
| 3156 | |
| 3157 | void AsmPrinter::printSymbolName(StringRef symbolRef) { |
| 3158 | assert(impl && "expected AsmPrinter::printSymbolName to be overriden" ); |
| 3159 | ::printSymbolReference(symbolRef, os&: impl->getStream()); |
| 3160 | } |
| 3161 | |
| 3162 | void AsmPrinter::printResourceHandle(const AsmDialectResourceHandle &resource) { |
| 3163 | assert(impl && "expected AsmPrinter::printResourceHandle to be overriden" ); |
| 3164 | impl->printResourceHandle(resource); |
| 3165 | } |
| 3166 | |
| 3167 | void AsmPrinter::printDimensionList(ArrayRef<int64_t> shape) { |
| 3168 | detail::printDimensionList(stream&: getStream(), shape); |
| 3169 | } |
| 3170 | |
| 3171 | LogicalResult AsmPrinter::pushCyclicPrinting(const void *opaquePointer) { |
| 3172 | return impl->pushCyclicPrinting(opaquePointer); |
| 3173 | } |
| 3174 | |
| 3175 | void AsmPrinter::popCyclicPrinting() { impl->popCyclicPrinting(); } |
| 3176 | |
| 3177 | //===----------------------------------------------------------------------===// |
| 3178 | // Affine expressions and maps |
| 3179 | //===----------------------------------------------------------------------===// |
| 3180 | |
| 3181 | void AsmPrinter::Impl::printAffineExpr( |
| 3182 | AffineExpr expr, function_ref<void(unsigned, bool)> printValueName) { |
| 3183 | printAffineExprInternal(expr, enclosingTightness: BindingStrength::Weak, printValueName); |
| 3184 | } |
| 3185 | |
| 3186 | void AsmPrinter::Impl::printAffineExprInternal( |
| 3187 | AffineExpr expr, BindingStrength enclosingTightness, |
| 3188 | function_ref<void(unsigned, bool)> printValueName) { |
| 3189 | const char *binopSpelling = nullptr; |
| 3190 | switch (expr.getKind()) { |
| 3191 | case AffineExprKind::SymbolId: { |
| 3192 | unsigned pos = cast<AffineSymbolExpr>(Val&: expr).getPosition(); |
| 3193 | if (printValueName) |
| 3194 | printValueName(pos, /*isSymbol=*/true); |
| 3195 | else |
| 3196 | os << 's' << pos; |
| 3197 | return; |
| 3198 | } |
| 3199 | case AffineExprKind::DimId: { |
| 3200 | unsigned pos = cast<AffineDimExpr>(Val&: expr).getPosition(); |
| 3201 | if (printValueName) |
| 3202 | printValueName(pos, /*isSymbol=*/false); |
| 3203 | else |
| 3204 | os << 'd' << pos; |
| 3205 | return; |
| 3206 | } |
| 3207 | case AffineExprKind::Constant: |
| 3208 | os << cast<AffineConstantExpr>(Val&: expr).getValue(); |
| 3209 | return; |
| 3210 | case AffineExprKind::Add: |
| 3211 | binopSpelling = " + " ; |
| 3212 | break; |
| 3213 | case AffineExprKind::Mul: |
| 3214 | binopSpelling = " * " ; |
| 3215 | break; |
| 3216 | case AffineExprKind::FloorDiv: |
| 3217 | binopSpelling = " floordiv " ; |
| 3218 | break; |
| 3219 | case AffineExprKind::CeilDiv: |
| 3220 | binopSpelling = " ceildiv " ; |
| 3221 | break; |
| 3222 | case AffineExprKind::Mod: |
| 3223 | binopSpelling = " mod " ; |
| 3224 | break; |
| 3225 | } |
| 3226 | |
| 3227 | auto binOp = cast<AffineBinaryOpExpr>(Val&: expr); |
| 3228 | AffineExpr lhsExpr = binOp.getLHS(); |
| 3229 | AffineExpr rhsExpr = binOp.getRHS(); |
| 3230 | |
| 3231 | // Handle tightly binding binary operators. |
| 3232 | if (binOp.getKind() != AffineExprKind::Add) { |
| 3233 | if (enclosingTightness == BindingStrength::Strong) |
| 3234 | os << '('; |
| 3235 | |
| 3236 | // Pretty print multiplication with -1. |
| 3237 | auto rhsConst = dyn_cast<AffineConstantExpr>(Val&: rhsExpr); |
| 3238 | if (rhsConst && binOp.getKind() == AffineExprKind::Mul && |
| 3239 | rhsConst.getValue() == -1) { |
| 3240 | os << "-" ; |
| 3241 | printAffineExprInternal(expr: lhsExpr, enclosingTightness: BindingStrength::Strong, printValueName); |
| 3242 | if (enclosingTightness == BindingStrength::Strong) |
| 3243 | os << ')'; |
| 3244 | return; |
| 3245 | } |
| 3246 | |
| 3247 | printAffineExprInternal(expr: lhsExpr, enclosingTightness: BindingStrength::Strong, printValueName); |
| 3248 | |
| 3249 | os << binopSpelling; |
| 3250 | printAffineExprInternal(expr: rhsExpr, enclosingTightness: BindingStrength::Strong, printValueName); |
| 3251 | |
| 3252 | if (enclosingTightness == BindingStrength::Strong) |
| 3253 | os << ')'; |
| 3254 | return; |
| 3255 | } |
| 3256 | |
| 3257 | // Print out special "pretty" forms for add. |
| 3258 | if (enclosingTightness == BindingStrength::Strong) |
| 3259 | os << '('; |
| 3260 | |
| 3261 | // Pretty print addition to a product that has a negative operand as a |
| 3262 | // subtraction. |
| 3263 | if (auto rhs = dyn_cast<AffineBinaryOpExpr>(Val&: rhsExpr)) { |
| 3264 | if (rhs.getKind() == AffineExprKind::Mul) { |
| 3265 | AffineExpr rrhsExpr = rhs.getRHS(); |
| 3266 | if (auto rrhs = dyn_cast<AffineConstantExpr>(Val&: rrhsExpr)) { |
| 3267 | if (rrhs.getValue() == -1) { |
| 3268 | printAffineExprInternal(expr: lhsExpr, enclosingTightness: BindingStrength::Weak, |
| 3269 | printValueName); |
| 3270 | os << " - " ; |
| 3271 | if (rhs.getLHS().getKind() == AffineExprKind::Add) { |
| 3272 | printAffineExprInternal(expr: rhs.getLHS(), enclosingTightness: BindingStrength::Strong, |
| 3273 | printValueName); |
| 3274 | } else { |
| 3275 | printAffineExprInternal(expr: rhs.getLHS(), enclosingTightness: BindingStrength::Weak, |
| 3276 | printValueName); |
| 3277 | } |
| 3278 | |
| 3279 | if (enclosingTightness == BindingStrength::Strong) |
| 3280 | os << ')'; |
| 3281 | return; |
| 3282 | } |
| 3283 | |
| 3284 | if (rrhs.getValue() < -1) { |
| 3285 | printAffineExprInternal(expr: lhsExpr, enclosingTightness: BindingStrength::Weak, |
| 3286 | printValueName); |
| 3287 | os << " - " ; |
| 3288 | printAffineExprInternal(expr: rhs.getLHS(), enclosingTightness: BindingStrength::Strong, |
| 3289 | printValueName); |
| 3290 | // Use unsigned negation to avoid signed integer overflow for |
| 3291 | // INT64_MIN. |
| 3292 | os << " * " << -static_cast<uint64_t>(rrhs.getValue()); |
| 3293 | if (enclosingTightness == BindingStrength::Strong) |
| 3294 | os << ')'; |
| 3295 | return; |
| 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | // Pretty print addition to a negative number as a subtraction. |
| 3302 | if (auto rhsConst = dyn_cast<AffineConstantExpr>(Val&: rhsExpr)) { |
| 3303 | if (rhsConst.getValue() < 0) { |
| 3304 | printAffineExprInternal(expr: lhsExpr, enclosingTightness: BindingStrength::Weak, printValueName); |
| 3305 | // Use unsigned negation to avoid signed integer overflow for INT64_MIN. |
| 3306 | os << " - " << -static_cast<uint64_t>(rhsConst.getValue()); |
| 3307 | if (enclosingTightness == BindingStrength::Strong) |
| 3308 | os << ')'; |
| 3309 | return; |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | printAffineExprInternal(expr: lhsExpr, enclosingTightness: BindingStrength::Weak, printValueName); |
| 3314 | |
| 3315 | os << " + " ; |
| 3316 | printAffineExprInternal(expr: rhsExpr, enclosingTightness: BindingStrength::Weak, printValueName); |
| 3317 | |
| 3318 | if (enclosingTightness == BindingStrength::Strong) |
| 3319 | os << ')'; |
| 3320 | } |
| 3321 | |
| 3322 | void AsmPrinter::Impl::printAffineConstraint(AffineExpr expr, bool isEq) { |
| 3323 | printAffineExprInternal(expr, enclosingTightness: BindingStrength::Weak); |
| 3324 | isEq ? os << " == 0" : os << " >= 0" ; |
| 3325 | } |
| 3326 | |
| 3327 | void AsmPrinter::Impl::printAffineMap(AffineMap map) { |
| 3328 | // Dimension identifiers. |
| 3329 | os << '('; |
| 3330 | for (int i = 0; i < (int)map.getNumDims() - 1; ++i) |
| 3331 | os << 'd' << i << ", " ; |
| 3332 | if (map.getNumDims() >= 1) |
| 3333 | os << 'd' << map.getNumDims() - 1; |
| 3334 | os << ')'; |
| 3335 | |
| 3336 | // Symbolic identifiers. |
| 3337 | if (map.getNumSymbols() != 0) { |
| 3338 | os << '['; |
| 3339 | for (unsigned i = 0; i < map.getNumSymbols() - 1; ++i) |
| 3340 | os << 's' << i << ", " ; |
| 3341 | if (map.getNumSymbols() >= 1) |
| 3342 | os << 's' << map.getNumSymbols() - 1; |
| 3343 | os << ']'; |
| 3344 | } |
| 3345 | |
| 3346 | // Result affine expressions. |
| 3347 | os << " -> (" ; |
| 3348 | interleaveComma(c: map.getResults(), |
| 3349 | eachFn: [&](AffineExpr expr) { printAffineExpr(expr); }); |
| 3350 | os << ')'; |
| 3351 | } |
| 3352 | |
| 3353 | void AsmPrinter::Impl::printIntegerSet(IntegerSet set) { |
| 3354 | // Dimension identifiers. |
| 3355 | os << '('; |
| 3356 | for (unsigned i = 1; i < set.getNumDims(); ++i) |
| 3357 | os << 'd' << i - 1 << ", " ; |
| 3358 | if (set.getNumDims() >= 1) |
| 3359 | os << 'd' << set.getNumDims() - 1; |
| 3360 | os << ')'; |
| 3361 | |
| 3362 | // Symbolic identifiers. |
| 3363 | if (set.getNumSymbols() != 0) { |
| 3364 | os << '['; |
| 3365 | for (unsigned i = 0; i < set.getNumSymbols() - 1; ++i) |
| 3366 | os << 's' << i << ", " ; |
| 3367 | if (set.getNumSymbols() >= 1) |
| 3368 | os << 's' << set.getNumSymbols() - 1; |
| 3369 | os << ']'; |
| 3370 | } |
| 3371 | |
| 3372 | // Print constraints. |
| 3373 | os << " : (" ; |
| 3374 | int numConstraints = set.getNumConstraints(); |
| 3375 | for (int i = 1; i < numConstraints; ++i) { |
| 3376 | printAffineConstraint(expr: set.getConstraint(idx: i - 1), isEq: set.isEq(idx: i - 1)); |
| 3377 | os << ", " ; |
| 3378 | } |
| 3379 | if (numConstraints >= 1) |
| 3380 | printAffineConstraint(expr: set.getConstraint(idx: numConstraints - 1), |
| 3381 | isEq: set.isEq(idx: numConstraints - 1)); |
| 3382 | os << ')'; |
| 3383 | } |
| 3384 | |
| 3385 | //===----------------------------------------------------------------------===// |
| 3386 | // OperationPrinter |
| 3387 | //===----------------------------------------------------------------------===// |
| 3388 | |
| 3389 | namespace { |
| 3390 | /// This class contains the logic for printing operations, regions, and blocks. |
| 3391 | class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter { |
| 3392 | public: |
| 3393 | using Impl = AsmPrinter::Impl; |
| 3394 | using Impl::printType; |
| 3395 | |
| 3396 | explicit OperationPrinter(raw_ostream &os, AsmStateImpl &state) |
| 3397 | : Impl(os, state), OpAsmPrinter(static_cast<Impl &>(*this)) {} |
| 3398 | |
| 3399 | /// Print the given top-level operation. |
| 3400 | void printTopLevelOperation(Operation *op); |
| 3401 | |
| 3402 | /// Print the given operation, including its left-hand side and its right-hand |
| 3403 | /// side, with its indent and location. |
| 3404 | void printFullOpWithIndentAndLoc(Operation *op); |
| 3405 | /// Print the given operation, including its left-hand side and its right-hand |
| 3406 | /// side, but not including indentation and location. |
| 3407 | void printFullOp(Operation *op); |
| 3408 | /// Print the right-hand size of the given operation in the custom or generic |
| 3409 | /// form. |
| 3410 | void printCustomOrGenericOp(Operation *op) override; |
| 3411 | /// Print the right-hand side of the given operation in the generic form. |
| 3412 | void printGenericOp(Operation *op, bool printOpName) override; |
| 3413 | |
| 3414 | /// Print the name of the given block. |
| 3415 | void printBlockName(Block *block); |
| 3416 | |
| 3417 | /// Print the given block. If 'printBlockArgs' is false, the arguments of the |
| 3418 | /// block are not printed. If 'printBlockTerminator' is false, the terminator |
| 3419 | /// operation of the block is not printed. |
| 3420 | void print(Block *block, bool printBlockArgs = true, |
| 3421 | bool printBlockTerminator = true); |
| 3422 | |
| 3423 | /// Print the ID of the given value, optionally with its result number. |
| 3424 | void printValueID(Value value, bool printResultNo = true, |
| 3425 | raw_ostream *streamOverride = nullptr) const; |
| 3426 | |
| 3427 | /// Print the ID of the given operation. |
| 3428 | void printOperationID(Operation *op, |
| 3429 | raw_ostream *streamOverride = nullptr) const; |
| 3430 | |
| 3431 | //===--------------------------------------------------------------------===// |
| 3432 | // OpAsmPrinter methods |
| 3433 | //===--------------------------------------------------------------------===// |
| 3434 | |
| 3435 | /// Print a loc(...) specifier if printing debug info is enabled. Locations |
| 3436 | /// may be deferred with an alias. |
| 3437 | void printOptionalLocationSpecifier(Location loc) override { |
| 3438 | printTrailingLocation(loc); |
| 3439 | } |
| 3440 | |
| 3441 | /// Print a block argument in the usual format of: |
| 3442 | /// %ssaName : type {attr1=42} loc("here") |
| 3443 | /// where location printing is controlled by the standard internal option. |
| 3444 | /// You may pass omitType=true to not print a type, and pass an empty |
| 3445 | /// attribute list if you don't care for attributes. |
| 3446 | void printRegionArgument(BlockArgument arg, |
| 3447 | ArrayRef<NamedAttribute> argAttrs = {}, |
| 3448 | bool omitType = false) override; |
| 3449 | |
| 3450 | /// Print the ID for the given value. |
| 3451 | void printOperand(Value value) override { printValueID(value); } |
| 3452 | void printOperand(Value value, raw_ostream &os) override { |
| 3453 | printValueID(value, /*printResultNo=*/true, streamOverride: &os); |
| 3454 | } |
| 3455 | |
| 3456 | /// Print an optional attribute dictionary with a given set of elided values. |
| 3457 | void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs, |
| 3458 | ArrayRef<StringRef> elidedAttrs = {}) override { |
| 3459 | Impl::printOptionalAttrDict(attrs, elidedAttrs); |
| 3460 | } |
| 3461 | void printOptionalAttrDictWithKeyword( |
| 3462 | ArrayRef<NamedAttribute> attrs, |
| 3463 | ArrayRef<StringRef> elidedAttrs = {}) override { |
| 3464 | Impl::printOptionalAttrDict(attrs, elidedAttrs, |
| 3465 | /*withKeyword=*/true); |
| 3466 | } |
| 3467 | |
| 3468 | /// Print the given successor. |
| 3469 | void printSuccessor(Block *successor) override; |
| 3470 | |
| 3471 | /// Print an operation successor with the operands used for the block |
| 3472 | /// arguments. |
| 3473 | void printSuccessorAndUseList(Block *successor, |
| 3474 | ValueRange succOperands) override; |
| 3475 | |
| 3476 | /// Print the given region. |
| 3477 | void printRegion(Region ®ion, bool printEntryBlockArgs, |
| 3478 | bool printBlockTerminators, bool printEmptyBlock) override; |
| 3479 | |
| 3480 | /// Renumber the arguments for the specified region to the same names as the |
| 3481 | /// SSA values in namesToUse. This may only be used for IsolatedFromAbove |
| 3482 | /// operations. If any entry in namesToUse is null, the corresponding |
| 3483 | /// argument name is left alone. |
| 3484 | void shadowRegionArgs(Region ®ion, ValueRange namesToUse) override { |
| 3485 | state.getSSANameState().shadowRegionArgs(region, namesToUse); |
| 3486 | } |
| 3487 | |
| 3488 | /// Print the given affine map with the symbol and dimension operands printed |
| 3489 | /// inline with the map. |
| 3490 | void printAffineMapOfSSAIds(AffineMapAttr mapAttr, |
| 3491 | ValueRange operands) override; |
| 3492 | |
| 3493 | /// Print the given affine expression with the symbol and dimension operands |
| 3494 | /// printed inline with the expression. |
| 3495 | void printAffineExprOfSSAIds(AffineExpr expr, ValueRange dimOperands, |
| 3496 | ValueRange symOperands) override; |
| 3497 | |
| 3498 | /// Print users of this operation or id of this operation if it has no result. |
| 3499 | void printUsersComment(Operation *op); |
| 3500 | |
| 3501 | /// Print users of this block arg. |
| 3502 | void printUsersComment(BlockArgument arg); |
| 3503 | |
| 3504 | /// Print the users of a value. |
| 3505 | void printValueUsers(Value value); |
| 3506 | |
| 3507 | /// Print either the ids of the result values or the id of the operation if |
| 3508 | /// the operation has no results. |
| 3509 | void printUserIDs(Operation *user, bool prefixComma = false); |
| 3510 | |
| 3511 | private: |
| 3512 | /// This class represents a resource builder implementation for the MLIR |
| 3513 | /// textual assembly format. |
| 3514 | class ResourceBuilder : public AsmResourceBuilder { |
| 3515 | public: |
| 3516 | using ValueFn = function_ref<void(raw_ostream &)>; |
| 3517 | // `sizeHint` is the exact number of characters `valueFn` will write, or -1 |
| 3518 | // if unknown, so the char limit can be applied before paying the cost of |
| 3519 | // invoking `valueFn` (e.g. hex-encoding a large blob). |
| 3520 | using PrintFn = function_ref<void(StringRef, ValueFn, int64_t sizeHint)>; |
| 3521 | |
| 3522 | ResourceBuilder(PrintFn printFn) : printFn(printFn) {} |
| 3523 | ~ResourceBuilder() override = default; |
| 3524 | |
| 3525 | void buildBool(StringRef key, bool data) final { |
| 3526 | printFn( |
| 3527 | key, [&](raw_ostream &os) { os << (data ? "true" : "false" ); }, |
| 3528 | /*sizeHint=*/-1); |
| 3529 | } |
| 3530 | |
| 3531 | void buildString(StringRef key, StringRef data) final { |
| 3532 | printFn( |
| 3533 | key, |
| 3534 | [&](raw_ostream &os) { |
| 3535 | os << "\"" ; |
| 3536 | llvm::printEscapedString(Name: data, Out&: os); |
| 3537 | os << "\"" ; |
| 3538 | }, |
| 3539 | /*sizeHint=*/-1); |
| 3540 | } |
| 3541 | |
| 3542 | void buildBlob(StringRef key, ArrayRef<char> data, |
| 3543 | uint32_t dataAlignment) final { |
| 3544 | // Two hex chars per byte of the alignment word and the data, plus the |
| 3545 | // `"0x`/`"` wrapping; exact, so the limit can be checked pre-encoding. |
| 3546 | int64_t sizeHint = 2 * int64_t(sizeof(dataAlignment) + data.size()) + 4; |
| 3547 | printFn( |
| 3548 | key, |
| 3549 | [&](raw_ostream &os) { |
| 3550 | // Store the blob in a hex string containing the alignment and the |
| 3551 | // data. |
| 3552 | llvm::support::ulittle32_t dataAlignmentLE(dataAlignment); |
| 3553 | os << "\"0x" |
| 3554 | << llvm::toHex( |
| 3555 | Input: StringRef(reinterpret_cast<char *>(&dataAlignmentLE), |
| 3556 | sizeof(dataAlignment))) |
| 3557 | << llvm::toHex(Input: StringRef(data.data(), data.size())) << "\"" ; |
| 3558 | }, |
| 3559 | sizeHint); |
| 3560 | } |
| 3561 | |
| 3562 | private: |
| 3563 | PrintFn printFn; |
| 3564 | }; |
| 3565 | |
| 3566 | /// Print the metadata dictionary for the file, eliding it if it is empty. |
| 3567 | void printFileMetadataDictionary(Operation *op); |
| 3568 | |
| 3569 | /// Print the resource sections for the file metadata dictionary. |
| 3570 | /// `checkAddMetadataDict` is used to indicate that metadata is going to be |
| 3571 | /// added, and the file metadata dictionary should be started if it hasn't |
| 3572 | /// yet. |
| 3573 | void printResourceFileMetadata(function_ref<void()> checkAddMetadataDict, |
| 3574 | Operation *op); |
| 3575 | |
| 3576 | // Contains the stack of default dialects to use when printing regions. |
| 3577 | // A new dialect is pushed to the stack before parsing regions nested under an |
| 3578 | // operation implementing `OpAsmOpInterface`, and popped when done. At the |
| 3579 | // top-level we start with "builtin" as the default, so that the top-level |
| 3580 | // `module` operation prints as-is. |
| 3581 | SmallVector<StringRef> defaultDialectStack{"builtin" }; |
| 3582 | }; |
| 3583 | } // namespace |
| 3584 | |
| 3585 | void OperationPrinter::printTopLevelOperation(Operation *op) { |
| 3586 | // Output the aliases at the top level that can't be deferred. |
| 3587 | state.getAliasState().printNonDeferredAliases(p&: *this, newLine); |
| 3588 | |
| 3589 | // Print the module. |
| 3590 | printFullOpWithIndentAndLoc(op); |
| 3591 | os << newLine; |
| 3592 | |
| 3593 | // Output the aliases at the top level that can be deferred. |
| 3594 | state.getAliasState().printDeferredAliases(p&: *this, newLine); |
| 3595 | |
| 3596 | // Output any file level metadata. |
| 3597 | printFileMetadataDictionary(op); |
| 3598 | } |
| 3599 | |
| 3600 | void OperationPrinter::printFileMetadataDictionary(Operation *op) { |
| 3601 | bool sawMetadataEntry = false; |
| 3602 | auto checkAddMetadataDict = [&] { |
| 3603 | if (!std::exchange(obj&: sawMetadataEntry, new_val: true)) |
| 3604 | os << newLine << "{-#" << newLine; |
| 3605 | }; |
| 3606 | |
| 3607 | // Add the various types of metadata. |
| 3608 | printResourceFileMetadata(checkAddMetadataDict, op); |
| 3609 | |
| 3610 | // If the file dictionary exists, close it. |
| 3611 | if (sawMetadataEntry) |
| 3612 | os << newLine << "#-}" << newLine; |
| 3613 | } |
| 3614 | |
| 3615 | void OperationPrinter::printResourceFileMetadata( |
| 3616 | function_ref<void()> checkAddMetadataDict, Operation *op) { |
| 3617 | // Functor used to add data entries to the file metadata dictionary. |
| 3618 | bool hadResource = false; |
| 3619 | bool needResourceComma = false; |
| 3620 | bool needEntryComma = false; |
| 3621 | auto processProvider = [&](StringRef dictName, StringRef name, auto &provider, |
| 3622 | auto &&...providerArgs) { |
| 3623 | bool hadEntry = false; |
| 3624 | auto printFn = [&](StringRef key, ResourceBuilder::ValueFn valueFn, |
| 3625 | int64_t sizeHint) { |
| 3626 | checkAddMetadataDict(); |
| 3627 | |
| 3628 | std::string resourceStr; |
| 3629 | auto printResourceStr = [&](raw_ostream &os) { os << resourceStr; }; |
| 3630 | std::optional<uint64_t> charLimit = |
| 3631 | printerFlags.getLargeResourceStringLimit(); |
| 3632 | if (charLimit.has_value()) { |
| 3633 | // Don't compute resourceStr when charLimit is 0. |
| 3634 | if (charLimit.value() == 0) |
| 3635 | return; |
| 3636 | |
| 3637 | // Skip serializing entirely if the exact size already exceeds the |
| 3638 | // limit, e.g. hex-encoding a large blob. |
| 3639 | if (sizeHint >= 0 && uint64_t(sizeHint) > charLimit.value()) |
| 3640 | return; |
| 3641 | |
| 3642 | llvm::raw_string_ostream ss(resourceStr); |
| 3643 | valueFn(ss); |
| 3644 | |
| 3645 | // Only print entry if its string is small enough. |
| 3646 | if (resourceStr.size() > charLimit.value()) |
| 3647 | return; |
| 3648 | |
| 3649 | // Don't recompute resourceStr when valueFn is called below. |
| 3650 | valueFn = printResourceStr; |
| 3651 | } |
| 3652 | |
| 3653 | // Emit the top-level resource entry if we haven't yet. |
| 3654 | if (!std::exchange(obj&: hadResource, new_val: true)) { |
| 3655 | if (needResourceComma) |
| 3656 | os << "," << newLine; |
| 3657 | os << " " << dictName << "_resources: {" << newLine; |
| 3658 | } |
| 3659 | // Emit the parent resource entry if we haven't yet. |
| 3660 | if (!std::exchange(obj&: hadEntry, new_val: true)) { |
| 3661 | if (needEntryComma) |
| 3662 | os << "," << newLine; |
| 3663 | os << " " << name << ": {" << newLine; |
| 3664 | } else { |
| 3665 | os << "," << newLine; |
| 3666 | } |
| 3667 | os << " " ; |
| 3668 | ::printKeywordOrString(keyword: key, os); |
| 3669 | os << ": " ; |
| 3670 | // Call printResourceStr or original valueFn, depending on charLimit. |
| 3671 | valueFn(os); |
| 3672 | }; |
| 3673 | ResourceBuilder entryBuilder(printFn); |
| 3674 | provider.buildResources(op, providerArgs..., entryBuilder); |
| 3675 | |
| 3676 | needEntryComma |= hadEntry; |
| 3677 | if (hadEntry) |
| 3678 | os << newLine << " }" ; |
| 3679 | }; |
| 3680 | |
| 3681 | // Print the `dialect_resources` section if we have any dialects with |
| 3682 | // resources. |
| 3683 | for (const OpAsmDialectInterface &interface : state.getDialectInterfaces()) { |
| 3684 | auto &dialectResources = state.getDialectResources(); |
| 3685 | StringRef name = interface.getDialect()->getNamespace(); |
| 3686 | auto it = dialectResources.find(Val: interface.getDialect()); |
| 3687 | if (it != dialectResources.end()) |
| 3688 | processProvider("dialect" , name, interface, it->second); |
| 3689 | else |
| 3690 | processProvider("dialect" , name, interface, |
| 3691 | SetVector<AsmDialectResourceHandle>()); |
| 3692 | } |
| 3693 | if (hadResource) |
| 3694 | os << newLine << " }" ; |
| 3695 | |
| 3696 | // Print the `external_resources` section if we have any external clients with |
| 3697 | // resources. |
| 3698 | needEntryComma = false; |
| 3699 | needResourceComma = hadResource; |
| 3700 | hadResource = false; |
| 3701 | for (const auto &printer : state.getResourcePrinters()) |
| 3702 | processProvider("external" , printer.getName(), printer); |
| 3703 | if (hadResource) |
| 3704 | os << newLine << " }" ; |
| 3705 | } |
| 3706 | |
| 3707 | /// Print a block argument in the usual format of: |
| 3708 | /// %ssaName : type {attr1=42} loc("here") |
| 3709 | /// where location printing is controlled by the standard internal option. |
| 3710 | /// You may pass omitType=true to not print a type, and pass an empty |
| 3711 | /// attribute list if you don't care for attributes. |
| 3712 | void OperationPrinter::printRegionArgument(BlockArgument arg, |
| 3713 | ArrayRef<NamedAttribute> argAttrs, |
| 3714 | bool omitType) { |
| 3715 | printOperand(value: arg); |
| 3716 | if (!omitType) { |
| 3717 | os << ": " ; |
| 3718 | printType(type: arg.getType()); |
| 3719 | } |
| 3720 | printOptionalAttrDict(attrs: argAttrs); |
| 3721 | // TODO: We should allow location aliases on block arguments. |
| 3722 | printTrailingLocation(loc: arg.getLoc(), /*allowAlias*/ false); |
| 3723 | } |
| 3724 | |
| 3725 | void OperationPrinter::printFullOpWithIndentAndLoc(Operation *op) { |
| 3726 | // Track the location of this operation. |
| 3727 | state.registerOperationLocation(op, line: newLine.curLine, col: currentIndent); |
| 3728 | |
| 3729 | os.indent(NumSpaces: currentIndent); |
| 3730 | printFullOp(op); |
| 3731 | printTrailingLocation(loc: op->getLoc()); |
| 3732 | if (printerFlags.shouldPrintValueUsers()) |
| 3733 | printUsersComment(op); |
| 3734 | } |
| 3735 | |
| 3736 | void OperationPrinter::printFullOp(Operation *op) { |
| 3737 | if (size_t numResults = op->getNumResults()) { |
| 3738 | auto printResultGroup = [&](size_t resultNo, size_t resultCount) { |
| 3739 | printValueID(value: op->getResult(idx: resultNo), /*printResultNo=*/false); |
| 3740 | if (resultCount > 1) |
| 3741 | os << ':' << resultCount; |
| 3742 | }; |
| 3743 | |
| 3744 | // Check to see if this operation has multiple result groups. |
| 3745 | ArrayRef<int> resultGroups = state.getSSANameState().getOpResultGroups(op); |
| 3746 | if (!resultGroups.empty()) { |
| 3747 | // Interleave the groups excluding the last one, this one will be handled |
| 3748 | // separately. |
| 3749 | interleaveComma(c: llvm::seq<int>(Begin: 0, End: resultGroups.size() - 1), eachFn: [&](int i) { |
| 3750 | printResultGroup(resultGroups[i], |
| 3751 | resultGroups[i + 1] - resultGroups[i]); |
| 3752 | }); |
| 3753 | os << ", " ; |
| 3754 | printResultGroup(resultGroups.back(), numResults - resultGroups.back()); |
| 3755 | |
| 3756 | } else { |
| 3757 | printResultGroup(/*resultNo=*/0, /*resultCount=*/numResults); |
| 3758 | } |
| 3759 | |
| 3760 | os << " = " ; |
| 3761 | } |
| 3762 | |
| 3763 | printCustomOrGenericOp(op); |
| 3764 | } |
| 3765 | |
| 3766 | void OperationPrinter::(Operation *op) { |
| 3767 | unsigned numResults = op->getNumResults(); |
| 3768 | if (!numResults && op->getNumOperands()) { |
| 3769 | os << " // id: " ; |
| 3770 | printOperationID(op); |
| 3771 | } else if (numResults && op->use_empty()) { |
| 3772 | os << " // unused" ; |
| 3773 | } else if (numResults && !op->use_empty()) { |
| 3774 | // Print "user" if the operation has one result used to compute one other |
| 3775 | // result, or is used in one operation with no result. |
| 3776 | unsigned usedInNResults = 0; |
| 3777 | unsigned usedInNOperations = 0; |
| 3778 | SmallPtrSet<Operation *, 1> userSet; |
| 3779 | for (Operation *user : op->getUsers()) { |
| 3780 | if (userSet.insert(Ptr: user).second) { |
| 3781 | ++usedInNOperations; |
| 3782 | usedInNResults += user->getNumResults(); |
| 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | // We already know that users is not empty. |
| 3787 | bool exactlyOneUniqueUse = |
| 3788 | usedInNResults <= 1 && usedInNOperations <= 1 && numResults == 1; |
| 3789 | os << " // " << (exactlyOneUniqueUse ? "user" : "users" ) << ": " ; |
| 3790 | bool shouldPrintBrackets = numResults > 1; |
| 3791 | auto printOpResult = [&](OpResult opResult) { |
| 3792 | if (shouldPrintBrackets) |
| 3793 | os << "(" ; |
| 3794 | printValueUsers(value: opResult); |
| 3795 | if (shouldPrintBrackets) |
| 3796 | os << ")" ; |
| 3797 | }; |
| 3798 | |
| 3799 | interleaveComma(c: op->getResults(), eachFn: printOpResult); |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | void OperationPrinter::(BlockArgument arg) { |
| 3804 | os << "// " ; |
| 3805 | printValueID(value: arg); |
| 3806 | if (arg.use_empty()) { |
| 3807 | os << " is unused" ; |
| 3808 | } else { |
| 3809 | os << " is used by " ; |
| 3810 | printValueUsers(value: arg); |
| 3811 | } |
| 3812 | os << newLine; |
| 3813 | } |
| 3814 | |
| 3815 | void OperationPrinter::printValueUsers(Value value) { |
| 3816 | if (value.use_empty()) |
| 3817 | os << "unused" ; |
| 3818 | |
| 3819 | // One value might be used as the operand of an operation more than once. |
| 3820 | // Only print the operations results once in that case. |
| 3821 | SmallPtrSet<Operation *, 1> userSet; |
| 3822 | for (auto [index, user] : enumerate(First: value.getUsers())) { |
| 3823 | if (userSet.insert(Ptr: user).second) |
| 3824 | printUserIDs(user, prefixComma: index); |
| 3825 | } |
| 3826 | } |
| 3827 | |
| 3828 | void OperationPrinter::printUserIDs(Operation *user, bool prefixComma) { |
| 3829 | if (prefixComma) |
| 3830 | os << ", " ; |
| 3831 | |
| 3832 | if (!user->getNumResults()) { |
| 3833 | printOperationID(op: user); |
| 3834 | } else { |
| 3835 | interleaveComma(c: user->getResults(), |
| 3836 | eachFn: [this](Value result) { printValueID(value: result); }); |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | void OperationPrinter::printCustomOrGenericOp(Operation *op) { |
| 3841 | // If requested, always print the generic form. |
| 3842 | if (!printerFlags.shouldPrintGenericOpForm()) { |
| 3843 | // Check to see if this is a known operation. If so, use the registered |
| 3844 | // custom printer hook. |
| 3845 | if (auto opInfo = op->getRegisteredInfo()) { |
| 3846 | opInfo->printAssembly(op, p&: *this, defaultDialect: defaultDialectStack.back()); |
| 3847 | return; |
| 3848 | } |
| 3849 | // Otherwise try to dispatch to the dialect, if available. |
| 3850 | if (Dialect *dialect = op->getDialect()) { |
| 3851 | if (auto opPrinter = dialect->getOperationPrinter(op)) { |
| 3852 | // Print the op name first. |
| 3853 | StringRef name = op->getName().getStringRef(); |
| 3854 | // Only drop the default dialect prefix when it cannot lead to |
| 3855 | // ambiguities. |
| 3856 | if (name.count(C: '.') == 1) |
| 3857 | name.consume_front(Prefix: (defaultDialectStack.back() + "." ).str()); |
| 3858 | os << name; |
| 3859 | |
| 3860 | // Print the rest of the op now. |
| 3861 | opPrinter(op, *this); |
| 3862 | return; |
| 3863 | } |
| 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | // Otherwise print with the generic assembly form. |
| 3868 | printGenericOp(op, /*printOpName=*/true); |
| 3869 | } |
| 3870 | |
| 3871 | void OperationPrinter::printGenericOp(Operation *op, bool printOpName) { |
| 3872 | if (printOpName) |
| 3873 | printEscapedString(str: op->getName().getStringRef()); |
| 3874 | os << '('; |
| 3875 | interleaveComma(c: op->getOperands(), eachFn: [&](Value value) { printValueID(value); }); |
| 3876 | os << ')'; |
| 3877 | |
| 3878 | // For terminators, print the list of successors and their operands. |
| 3879 | if (op->getNumSuccessors() != 0) { |
| 3880 | os << '['; |
| 3881 | interleaveComma(c: op->getSuccessors(), |
| 3882 | eachFn: [&](Block *successor) { printBlockName(block: successor); }); |
| 3883 | os << ']'; |
| 3884 | } |
| 3885 | |
| 3886 | // Print the properties. |
| 3887 | if (Attribute prop = op->getPropertiesAsAttribute()) { |
| 3888 | os << " <" ; |
| 3889 | Impl::printAttribute(attr: prop); |
| 3890 | os << '>'; |
| 3891 | } |
| 3892 | |
| 3893 | // Print regions. |
| 3894 | if (op->getNumRegions() != 0) { |
| 3895 | os << " (" ; |
| 3896 | interleaveComma(c: op->getRegions(), eachFn: [&](Region ®ion) { |
| 3897 | printRegion(region, /*printEntryBlockArgs=*/true, |
| 3898 | /*printBlockTerminators=*/true, /*printEmptyBlock=*/true); |
| 3899 | }); |
| 3900 | os << ')'; |
| 3901 | } |
| 3902 | |
| 3903 | printOptionalAttrDict(attrs: op->getRawDictionaryAttrs().getValue()); |
| 3904 | |
| 3905 | // Print the type signature of the operation. |
| 3906 | os << " : " ; |
| 3907 | printFunctionalType(op); |
| 3908 | } |
| 3909 | |
| 3910 | void OperationPrinter::printBlockName(Block *block) { |
| 3911 | os << state.getSSANameState().getBlockInfo(block).name; |
| 3912 | } |
| 3913 | |
| 3914 | void OperationPrinter::print(Block *block, bool printBlockArgs, |
| 3915 | bool printBlockTerminator) { |
| 3916 | // Print the block label and argument list if requested. |
| 3917 | if (printBlockArgs) { |
| 3918 | os.indent(NumSpaces: currentIndent); |
| 3919 | printBlockName(block); |
| 3920 | |
| 3921 | // Print the argument list if non-empty. |
| 3922 | if (!block->args_empty()) { |
| 3923 | os << '('; |
| 3924 | interleaveComma(c: block->getArguments(), eachFn: [&](BlockArgument arg) { |
| 3925 | printValueID(value: arg); |
| 3926 | os << ": " ; |
| 3927 | printType(type: arg.getType()); |
| 3928 | // TODO: We should allow location aliases on block arguments. |
| 3929 | printTrailingLocation(loc: arg.getLoc(), /*allowAlias*/ false); |
| 3930 | }); |
| 3931 | os << ')'; |
| 3932 | } |
| 3933 | os << ':'; |
| 3934 | |
| 3935 | // Print out some context information about the predecessors of this block. |
| 3936 | if (!block->getParent()) { |
| 3937 | os << " // block is not in a region!" ; |
| 3938 | } else if (block->hasNoPredecessors()) { |
| 3939 | if (!block->isEntryBlock()) |
| 3940 | os << " // no predecessors" ; |
| 3941 | } else if (auto *pred = block->getSinglePredecessor()) { |
| 3942 | os << " // pred: " ; |
| 3943 | printBlockName(block: pred); |
| 3944 | } else { |
| 3945 | // We want to print the predecessors in a stable order, not in |
| 3946 | // whatever order the use-list is in, so gather and sort them. |
| 3947 | SmallVector<BlockInfo, 4> predIDs; |
| 3948 | for (auto *pred : block->getPredecessors()) |
| 3949 | predIDs.push_back(Elt: state.getSSANameState().getBlockInfo(block: pred)); |
| 3950 | llvm::sort(C&: predIDs, Comp: [](BlockInfo lhs, BlockInfo rhs) { |
| 3951 | return lhs.ordering < rhs.ordering; |
| 3952 | }); |
| 3953 | |
| 3954 | os << " // " << predIDs.size() << " preds: " ; |
| 3955 | |
| 3956 | interleaveComma(c: predIDs, eachFn: [&](BlockInfo pred) { os << pred.name; }); |
| 3957 | } |
| 3958 | os << newLine; |
| 3959 | } |
| 3960 | |
| 3961 | currentIndent += indentWidth; |
| 3962 | |
| 3963 | if (printerFlags.shouldPrintValueUsers()) { |
| 3964 | for (BlockArgument arg : block->getArguments()) { |
| 3965 | os.indent(NumSpaces: currentIndent); |
| 3966 | printUsersComment(arg); |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | bool hasTerminator = |
| 3971 | !block->empty() && block->back().hasTrait<OpTrait::IsTerminator>(); |
| 3972 | auto range = llvm::make_range( |
| 3973 | x: block->begin(), |
| 3974 | y: std::prev(x: block->end(), |
| 3975 | n: (!hasTerminator || printBlockTerminator) ? 0 : 1)); |
| 3976 | for (auto &op : range) { |
| 3977 | printFullOpWithIndentAndLoc(op: &op); |
| 3978 | os << newLine; |
| 3979 | } |
| 3980 | currentIndent -= indentWidth; |
| 3981 | } |
| 3982 | |
| 3983 | void OperationPrinter::printValueID(Value value, bool printResultNo, |
| 3984 | raw_ostream *streamOverride) const { |
| 3985 | state.getSSANameState().printValueID(value, printResultNo, |
| 3986 | stream&: streamOverride ? *streamOverride : os); |
| 3987 | } |
| 3988 | |
| 3989 | void OperationPrinter::printOperationID(Operation *op, |
| 3990 | raw_ostream *streamOverride) const { |
| 3991 | state.getSSANameState().printOperationID(op, stream&: streamOverride ? *streamOverride |
| 3992 | : os); |
| 3993 | } |
| 3994 | |
| 3995 | void OperationPrinter::printSuccessor(Block *successor) { |
| 3996 | printBlockName(block: successor); |
| 3997 | } |
| 3998 | |
| 3999 | void OperationPrinter::printSuccessorAndUseList(Block *successor, |
| 4000 | ValueRange succOperands) { |
| 4001 | printBlockName(block: successor); |
| 4002 | if (succOperands.empty()) |
| 4003 | return; |
| 4004 | |
| 4005 | os << '('; |
| 4006 | interleaveComma(c: succOperands, |
| 4007 | eachFn: [this](Value operand) { printValueID(value: operand); }); |
| 4008 | os << " : " ; |
| 4009 | interleaveComma(c: succOperands, |
| 4010 | eachFn: [this](Value operand) { printType(type: operand.getType()); }); |
| 4011 | os << ')'; |
| 4012 | } |
| 4013 | |
| 4014 | void OperationPrinter::printRegion(Region ®ion, bool printEntryBlockArgs, |
| 4015 | bool printBlockTerminators, |
| 4016 | bool printEmptyBlock) { |
| 4017 | if (printerFlags.shouldSkipRegions()) { |
| 4018 | os << "{...}" ; |
| 4019 | return; |
| 4020 | } |
| 4021 | os << "{" << newLine; |
| 4022 | if (!region.empty()) { |
| 4023 | llvm::scope_exit restoreDefaultDialect( |
| 4024 | [&]() { defaultDialectStack.pop_back(); }); |
| 4025 | if (auto iface = dyn_cast<OpAsmOpInterface>(Val: region.getParentOp())) |
| 4026 | defaultDialectStack.push_back(Elt: iface.getDefaultDialect()); |
| 4027 | else |
| 4028 | defaultDialectStack.push_back(Elt: "" ); |
| 4029 | |
| 4030 | auto *entryBlock = ®ion.front(); |
| 4031 | // Force printing the block header if printEmptyBlock is set and the block |
| 4032 | // is empty or if printEntryBlockArgs is set and there are arguments to |
| 4033 | // print. |
| 4034 | bool = |
| 4035 | (printEmptyBlock && entryBlock->empty()) || |
| 4036 | (printEntryBlockArgs && entryBlock->getNumArguments() != 0); |
| 4037 | print(block: entryBlock, printBlockArgs: shouldAlwaysPrintBlockHeader, printBlockTerminator: printBlockTerminators); |
| 4038 | for (auto &b : llvm::drop_begin(RangeOrContainer&: region.getBlocks(), N: 1)) |
| 4039 | print(block: &b); |
| 4040 | } |
| 4041 | os.indent(NumSpaces: currentIndent) << "}" ; |
| 4042 | } |
| 4043 | |
| 4044 | void OperationPrinter::printAffineMapOfSSAIds(AffineMapAttr mapAttr, |
| 4045 | ValueRange operands) { |
| 4046 | if (!mapAttr) { |
| 4047 | os << "<<NULL AFFINE MAP>>" ; |
| 4048 | return; |
| 4049 | } |
| 4050 | AffineMap map = mapAttr.getValue(); |
| 4051 | unsigned numDims = map.getNumDims(); |
| 4052 | auto printValueName = [&](unsigned pos, bool isSymbol) { |
| 4053 | unsigned index = isSymbol ? numDims + pos : pos; |
| 4054 | assert(index < operands.size()); |
| 4055 | if (isSymbol) |
| 4056 | os << "symbol(" ; |
| 4057 | printValueID(value: operands[index]); |
| 4058 | if (isSymbol) |
| 4059 | os << ')'; |
| 4060 | }; |
| 4061 | |
| 4062 | interleaveComma(c: map.getResults(), eachFn: [&](AffineExpr expr) { |
| 4063 | printAffineExpr(expr, printValueName); |
| 4064 | }); |
| 4065 | } |
| 4066 | |
| 4067 | void OperationPrinter::printAffineExprOfSSAIds(AffineExpr expr, |
| 4068 | ValueRange dimOperands, |
| 4069 | ValueRange symOperands) { |
| 4070 | auto printValueName = [&](unsigned pos, bool isSymbol) { |
| 4071 | if (!isSymbol) |
| 4072 | return printValueID(value: dimOperands[pos]); |
| 4073 | os << "symbol(" ; |
| 4074 | printValueID(value: symOperands[pos]); |
| 4075 | os << ')'; |
| 4076 | }; |
| 4077 | printAffineExpr(expr, printValueName); |
| 4078 | } |
| 4079 | |
| 4080 | //===----------------------------------------------------------------------===// |
| 4081 | // print and dump methods |
| 4082 | //===----------------------------------------------------------------------===// |
| 4083 | |
| 4084 | void Attribute::print(raw_ostream &os, bool elideType) const { |
| 4085 | if (!*this) { |
| 4086 | os << "<<NULL ATTRIBUTE>>" ; |
| 4087 | return; |
| 4088 | } |
| 4089 | |
| 4090 | AsmState state(getContext()); |
| 4091 | print(os, state, elideType); |
| 4092 | } |
| 4093 | void Attribute::print(raw_ostream &os, AsmState &state, bool elideType) const { |
| 4094 | using AttrTypeElision = AsmPrinter::Impl::AttrTypeElision; |
| 4095 | AsmPrinter::Impl(os, state.getImpl()) |
| 4096 | .printAttribute(attr: *this, typeElision: elideType ? AttrTypeElision::Must |
| 4097 | : AttrTypeElision::Never); |
| 4098 | } |
| 4099 | |
| 4100 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 4101 | void Attribute::dump() const { |
| 4102 | print(llvm::errs()); |
| 4103 | llvm::errs() << "\n" ; |
| 4104 | } |
| 4105 | #endif |
| 4106 | |
| 4107 | void Attribute::printStripped(raw_ostream &os, AsmState &state) const { |
| 4108 | if (!*this) { |
| 4109 | os << "<<NULL ATTRIBUTE>>" ; |
| 4110 | return; |
| 4111 | } |
| 4112 | |
| 4113 | AsmPrinter::Impl subPrinter(os, state.getImpl()); |
| 4114 | if (succeeded(Result: subPrinter.printAlias(attr: *this))) |
| 4115 | return; |
| 4116 | |
| 4117 | auto &dialect = this->getDialect(); |
| 4118 | uint64_t posPrior = os.tell(); |
| 4119 | DialectAsmPrinter printer(subPrinter); |
| 4120 | dialect.printAttribute(*this, printer); |
| 4121 | if (posPrior != os.tell()) |
| 4122 | return; |
| 4123 | |
| 4124 | // Fallback to printing with prefix if the above failed to write anything |
| 4125 | // to the output stream. |
| 4126 | print(os, state); |
| 4127 | } |
| 4128 | void Attribute::printStripped(raw_ostream &os) const { |
| 4129 | if (!*this) { |
| 4130 | os << "<<NULL ATTRIBUTE>>" ; |
| 4131 | return; |
| 4132 | } |
| 4133 | |
| 4134 | AsmState state(getContext()); |
| 4135 | printStripped(os, state); |
| 4136 | } |
| 4137 | |
| 4138 | void Type::print(raw_ostream &os) const { |
| 4139 | if (!*this) { |
| 4140 | os << "<<NULL TYPE>>" ; |
| 4141 | return; |
| 4142 | } |
| 4143 | |
| 4144 | AsmState state(getContext()); |
| 4145 | print(os, state); |
| 4146 | } |
| 4147 | void Type::print(raw_ostream &os, AsmState &state) const { |
| 4148 | AsmPrinter::Impl(os, state.getImpl()).printType(type: *this); |
| 4149 | } |
| 4150 | |
| 4151 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 4152 | void Type::dump() const { |
| 4153 | print(llvm::errs()); |
| 4154 | llvm::errs() << "\n" ; |
| 4155 | } |
| 4156 | |
| 4157 | void AffineMap::dump() const { |
| 4158 | print(llvm::errs()); |
| 4159 | llvm::errs() << "\n" ; |
| 4160 | } |
| 4161 | |
| 4162 | void IntegerSet::dump() const { |
| 4163 | print(llvm::errs()); |
| 4164 | llvm::errs() << "\n" ; |
| 4165 | } |
| 4166 | #endif |
| 4167 | |
| 4168 | void AffineExpr::print(raw_ostream &os) const { |
| 4169 | if (!expr) { |
| 4170 | os << "<<NULL AFFINE EXPR>>" ; |
| 4171 | return; |
| 4172 | } |
| 4173 | AsmState state(getContext()); |
| 4174 | AsmPrinter::Impl(os, state.getImpl()).printAffineExpr(expr: *this); |
| 4175 | } |
| 4176 | |
| 4177 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 4178 | void AffineExpr::dump() const { |
| 4179 | print(llvm::errs()); |
| 4180 | llvm::errs() << "\n" ; |
| 4181 | } |
| 4182 | #endif |
| 4183 | |
| 4184 | void AffineMap::print(raw_ostream &os) const { |
| 4185 | if (!map) { |
| 4186 | os << "<<NULL AFFINE MAP>>" ; |
| 4187 | return; |
| 4188 | } |
| 4189 | AsmState state(getContext()); |
| 4190 | AsmPrinter::Impl(os, state.getImpl()).printAffineMap(map: *this); |
| 4191 | } |
| 4192 | |
| 4193 | void IntegerSet::print(raw_ostream &os) const { |
| 4194 | AsmState state(getContext()); |
| 4195 | AsmPrinter::Impl(os, state.getImpl()).printIntegerSet(set: *this); |
| 4196 | } |
| 4197 | |
| 4198 | void Value::print(raw_ostream &os) const { print(os, flags: OpPrintingFlags()); } |
| 4199 | void Value::print(raw_ostream &os, const OpPrintingFlags &flags) const { |
| 4200 | if (!impl) { |
| 4201 | os << "<<NULL VALUE>>" ; |
| 4202 | return; |
| 4203 | } |
| 4204 | |
| 4205 | if (auto *op = getDefiningOp()) |
| 4206 | return op->print(os, flags); |
| 4207 | // TODO: Improve BlockArgument print'ing. |
| 4208 | BlockArgument arg = llvm::cast<BlockArgument>(Val: *this); |
| 4209 | os << "<block argument> of type '" << arg.getType() |
| 4210 | << "' at index: " << arg.getArgNumber(); |
| 4211 | } |
| 4212 | void Value::print(raw_ostream &os, AsmState &state) const { |
| 4213 | if (!impl) { |
| 4214 | os << "<<NULL VALUE>>" ; |
| 4215 | return; |
| 4216 | } |
| 4217 | |
| 4218 | if (auto *op = getDefiningOp()) |
| 4219 | return op->print(os, state); |
| 4220 | |
| 4221 | // TODO: Improve BlockArgument print'ing. |
| 4222 | BlockArgument arg = llvm::cast<BlockArgument>(Val: *this); |
| 4223 | os << "<block argument> of type '" << arg.getType() |
| 4224 | << "' at index: " << arg.getArgNumber(); |
| 4225 | } |
| 4226 | |
| 4227 | raw_ostream &mlir::operator<<(raw_ostream &os, Value value) { |
| 4228 | value.print(os, flags: OpPrintingFlags().useLocalScope()); |
| 4229 | return os; |
| 4230 | } |
| 4231 | |
| 4232 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 4233 | void Value::dump() const { |
| 4234 | print(llvm::errs(), OpPrintingFlags().useLocalScope()); |
| 4235 | llvm::errs() << "\n" ; |
| 4236 | } |
| 4237 | #endif |
| 4238 | |
| 4239 | void Value::printAsOperand(raw_ostream &os, AsmState &state) const { |
| 4240 | // TODO: This doesn't necessarily capture all potential cases. |
| 4241 | // Currently, region arguments can be shadowed when printing the main |
| 4242 | // operation. If the IR hasn't been printed, this will produce the old SSA |
| 4243 | // name and not the shadowed name. |
| 4244 | state.getImpl().getSSANameState().printValueID(value: *this, /*printResultNo=*/true, |
| 4245 | stream&: os); |
| 4246 | } |
| 4247 | |
| 4248 | static Operation *findParent(Operation *op, bool shouldUseLocalScope) { |
| 4249 | do { |
| 4250 | // If we are printing local scope, stop at the first operation that is |
| 4251 | // isolated from above. |
| 4252 | if (shouldUseLocalScope && op->hasTrait<OpTrait::IsIsolatedFromAbove>()) |
| 4253 | break; |
| 4254 | |
| 4255 | // Otherwise, traverse up to the next parent. |
| 4256 | Operation *parentOp = op->getParentOp(); |
| 4257 | if (!parentOp) |
| 4258 | break; |
| 4259 | op = parentOp; |
| 4260 | } while (true); |
| 4261 | return op; |
| 4262 | } |
| 4263 | |
| 4264 | void Value::printAsOperand(raw_ostream &os, |
| 4265 | const OpPrintingFlags &flags) const { |
| 4266 | Operation *op; |
| 4267 | if (auto result = llvm::dyn_cast<OpResult>(Val: *this)) { |
| 4268 | op = result.getOwner(); |
| 4269 | } else { |
| 4270 | op = llvm::cast<BlockArgument>(Val: *this).getOwner()->getParentOp(); |
| 4271 | if (!op) { |
| 4272 | os << "<<UNKNOWN SSA VALUE>>" ; |
| 4273 | return; |
| 4274 | } |
| 4275 | } |
| 4276 | op = findParent(op, shouldUseLocalScope: flags.shouldUseLocalScope()); |
| 4277 | AsmState state(op, flags); |
| 4278 | printAsOperand(os, state); |
| 4279 | } |
| 4280 | |
| 4281 | void Operation::print(raw_ostream &os, const OpPrintingFlags &printerFlags) { |
| 4282 | // Find the operation to number from based upon the provided flags. |
| 4283 | Operation *op = findParent(op: this, shouldUseLocalScope: printerFlags.shouldUseLocalScope()); |
| 4284 | AsmState state(op, printerFlags); |
| 4285 | print(os, state); |
| 4286 | } |
| 4287 | void Operation::print(raw_ostream &os, AsmState &state) { |
| 4288 | OperationPrinter printer(os, state.getImpl()); |
| 4289 | // alwaysUseAliases takes precedence over local scope, not just as a |
| 4290 | // tiebreak: pass instrumentation prints nested ops with local scope, so |
| 4291 | // without precedence the per-function dumps this flag targets would never |
| 4292 | // use aliases. The two compose: aliases stay consistent within each dump. |
| 4293 | if (state.getPrinterFlags().shouldAlwaysUseAliases() || |
| 4294 | (!getParent() && !state.getPrinterFlags().shouldUseLocalScope())) { |
| 4295 | state.getImpl().initializeAliases(op: this); |
| 4296 | printer.printTopLevelOperation(op: this); |
| 4297 | } else { |
| 4298 | printer.printFullOpWithIndentAndLoc(op: this); |
| 4299 | } |
| 4300 | } |
| 4301 | |
| 4302 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 4303 | void Operation::dump() { |
| 4304 | print(llvm::errs(), OpPrintingFlags().useLocalScope()); |
| 4305 | llvm::errs() << "\n" ; |
| 4306 | } |
| 4307 | #endif |
| 4308 | |
| 4309 | void Operation::dumpPretty() { |
| 4310 | print(os&: llvm::errs(), printerFlags: OpPrintingFlags().useLocalScope().assumeVerified()); |
| 4311 | llvm::errs() << "\n" ; |
| 4312 | } |
| 4313 | |
| 4314 | void Block::print(raw_ostream &os) { |
| 4315 | Operation *parentOp = getParentOp(); |
| 4316 | if (!parentOp) { |
| 4317 | os << "<<UNLINKED BLOCK>>\n" ; |
| 4318 | return; |
| 4319 | } |
| 4320 | // Get the top-level op. |
| 4321 | while (auto *nextOp = parentOp->getParentOp()) |
| 4322 | parentOp = nextOp; |
| 4323 | |
| 4324 | AsmState state(parentOp); |
| 4325 | print(os, state); |
| 4326 | } |
| 4327 | void Block::print(raw_ostream &os, AsmState &state) { |
| 4328 | OperationPrinter(os, state.getImpl()).print(block: this); |
| 4329 | } |
| 4330 | |
| 4331 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 4332 | void Block::dump() { print(llvm::errs()); } |
| 4333 | #endif |
| 4334 | |
| 4335 | /// Print out the name of the block without printing its body. |
| 4336 | void Block::printAsOperand(raw_ostream &os, bool printType) { |
| 4337 | Operation *parentOp = getParentOp(); |
| 4338 | if (!parentOp) { |
| 4339 | os << "<<UNLINKED BLOCK>>\n" ; |
| 4340 | return; |
| 4341 | } |
| 4342 | AsmState state(parentOp); |
| 4343 | printAsOperand(os, state); |
| 4344 | } |
| 4345 | void Block::printAsOperand(raw_ostream &os, AsmState &state) { |
| 4346 | OperationPrinter printer(os, state.getImpl()); |
| 4347 | printer.printBlockName(block: this); |
| 4348 | } |
| 4349 | |
| 4350 | raw_ostream &mlir::operator<<(raw_ostream &os, Block &block) { |
| 4351 | block.print(os); |
| 4352 | return os; |
| 4353 | } |
| 4354 | |
| 4355 | //===--------------------------------------------------------------------===// |
| 4356 | // Custom printers |
| 4357 | //===--------------------------------------------------------------------===// |
| 4358 | namespace mlir { |
| 4359 | |
| 4360 | void printDimensionList(OpAsmPrinter &printer, Operation *op, |
| 4361 | ArrayRef<int64_t> dimensions) { |
| 4362 | if (dimensions.empty()) |
| 4363 | printer << "[" ; |
| 4364 | printer.printDimensionList(shape: dimensions); |
| 4365 | if (dimensions.empty()) |
| 4366 | printer << "]" ; |
| 4367 | } |
| 4368 | |
| 4369 | ParseResult parseDimensionList(OpAsmParser &parser, |
| 4370 | DenseI64ArrayAttr &dimensions) { |
| 4371 | // Empty list case denoted by "[]". |
| 4372 | if (succeeded(Result: parser.parseOptionalLSquare())) { |
| 4373 | if (failed(Result: parser.parseRSquare())) { |
| 4374 | return parser.emitError(loc: parser.getCurrentLocation()) |
| 4375 | << "Failed parsing dimension list." ; |
| 4376 | } |
| 4377 | dimensions = |
| 4378 | DenseI64ArrayAttr::get(context: parser.getContext(), content: ArrayRef<int64_t>()); |
| 4379 | return success(); |
| 4380 | } |
| 4381 | |
| 4382 | // Non-empty list case. |
| 4383 | SmallVector<int64_t> shapeArr; |
| 4384 | if (failed(Result: parser.parseDimensionList(dimensions&: shapeArr, allowDynamic: true, withTrailingX: false))) { |
| 4385 | return parser.emitError(loc: parser.getCurrentLocation()) |
| 4386 | << "Failed parsing dimension list." ; |
| 4387 | } |
| 4388 | if (shapeArr.empty()) { |
| 4389 | return parser.emitError(loc: parser.getCurrentLocation()) |
| 4390 | << "Failed parsing dimension list. Did you mean an empty list? It " |
| 4391 | "must be denoted by \"[]\"." ; |
| 4392 | } |
| 4393 | dimensions = DenseI64ArrayAttr::get(context: parser.getContext(), content: shapeArr); |
| 4394 | return success(); |
| 4395 | } |
| 4396 | |
| 4397 | } // namespace mlir |
| 4398 | |