2018-06-24 07:03:42 +08:00
|
|
|
//===- AsmPrinter.cpp - MLIR Assembly Printer Implementation --------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-06-24 07:03:42 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-24 07:03:42 +08:00
|
|
|
//
|
|
|
|
// This file implements the MLIR AsmPrinter class, which is used to implement
|
|
|
|
// the various print() methods on the core IR objects.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-30 09:09:29 +08:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
2020-01-15 07:23:05 +08:00
|
|
|
#include "mlir/IR/AsmState.h"
|
2018-07-05 11:45:39 +08:00
|
|
|
#include "mlir/IR/Attributes.h"
|
2019-03-02 08:58:00 +08:00
|
|
|
#include "mlir/IR/Dialect.h"
|
2019-11-02 05:47:42 +08:00
|
|
|
#include "mlir/IR/DialectImplementation.h"
|
2018-12-28 03:07:34 +08:00
|
|
|
#include "mlir/IR/Function.h"
|
2018-08-08 05:24:38 +08:00
|
|
|
#include "mlir/IR/IntegerSet.h"
|
2019-01-11 14:08:39 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2018-06-24 07:03:42 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
2018-07-25 07:07:22 +08:00
|
|
|
#include "mlir/IR/OpImplementation.h"
|
2019-03-27 05:45:38 +08:00
|
|
|
#include "mlir/IR/Operation.h"
|
2019-01-04 06:29:52 +08:00
|
|
|
#include "mlir/IR/StandardTypes.h"
|
2018-08-16 00:09:54 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2018-06-24 07:03:42 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2019-05-07 01:36:32 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2018-11-13 06:58:53 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2019-07-10 01:40:29 +08:00
|
|
|
#include "llvm/ADT/ScopedHashTable.h"
|
2019-01-11 14:08:39 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2018-08-02 01:43:18 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringSet.h"
|
2019-01-24 05:11:23 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2019-01-11 14:08:39 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2020-01-10 04:39:26 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2018-06-24 07:03:42 +08:00
|
|
|
using namespace mlir;
|
2020-01-15 07:23:05 +08:00
|
|
|
using namespace mlir::detail;
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void Identifier::print(raw_ostream &os) const { os << str(); }
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void Identifier::dump() const { print(llvm::errs()); }
|
2018-07-05 11:45:39 +08:00
|
|
|
|
2018-10-10 13:08:52 +08:00
|
|
|
void OperationName::print(raw_ostream &os) const { os << getStringRef(); }
|
|
|
|
|
|
|
|
void OperationName::dump() const { print(llvm::errs()); }
|
|
|
|
|
2019-11-02 05:47:42 +08:00
|
|
|
DialectAsmPrinter::~DialectAsmPrinter() {}
|
|
|
|
|
2018-07-25 07:07:22 +08:00
|
|
|
OpAsmPrinter::~OpAsmPrinter() {}
|
|
|
|
|
2019-11-21 02:19:01 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Operation OpAsm interface.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// The OpAsmOpInterface, see OpAsmInterface.td for more details.
|
|
|
|
#include "mlir/IR/OpAsmInterface.cpp.inc"
|
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-10-08 04:54:16 +08:00
|
|
|
// OpPrintingFlags
|
2018-07-18 07:56:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-12 14:11:51 +08:00
|
|
|
namespace {
|
|
|
|
/// This struct contains command line options that can be used to initialize
|
|
|
|
/// various bits of the AsmPrinter. This uses a struct wrapper to avoid the need
|
|
|
|
/// for global command line options.
|
|
|
|
struct AsmPrinterOptions {
|
|
|
|
llvm::cl::opt<int64_t> printElementsAttrWithHexIfLarger{
|
|
|
|
"mlir-print-elementsattrs-with-hex-if-larger",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Print DenseElementsAttrs with a hex string that have "
|
|
|
|
"more elements than the given upper limit (use -1 to disable)")};
|
|
|
|
|
|
|
|
llvm::cl::opt<unsigned> elideElementsAttrIfLarger{
|
|
|
|
"mlir-elide-elementsattrs-if-larger",
|
|
|
|
llvm::cl::desc("Elide ElementsAttrs with \"...\" that have "
|
|
|
|
"more elements than the given upper limit")};
|
|
|
|
|
|
|
|
llvm::cl::opt<bool> printDebugInfoOpt{
|
|
|
|
"mlir-print-debuginfo", llvm::cl::init(false),
|
|
|
|
llvm::cl::desc("Print debug info in MLIR output")};
|
|
|
|
|
|
|
|
llvm::cl::opt<bool> printPrettyDebugInfoOpt{
|
|
|
|
"mlir-pretty-debuginfo", llvm::cl::init(false),
|
|
|
|
llvm::cl::desc("Print pretty debug info in MLIR output")};
|
|
|
|
|
|
|
|
// Use the generic op output form in the operation printer even if the custom
|
|
|
|
// form is defined.
|
|
|
|
llvm::cl::opt<bool> printGenericOpFormOpt{
|
|
|
|
"mlir-print-op-generic", llvm::cl::init(false),
|
|
|
|
llvm::cl::desc("Print the generic op form"), llvm::cl::Hidden};
|
|
|
|
|
|
|
|
llvm::cl::opt<bool> printLocalScopeOpt{
|
|
|
|
"mlir-print-local-scope", llvm::cl::init(false),
|
|
|
|
llvm::cl::desc("Print assuming in local scope by default"),
|
|
|
|
llvm::cl::Hidden};
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static llvm::ManagedStatic<AsmPrinterOptions> clOptions;
|
|
|
|
|
|
|
|
/// Register a set of useful command-line options that can be used to configure
|
|
|
|
/// various flags within the AsmPrinter.
|
|
|
|
void mlir::registerAsmPrinterCLOptions() {
|
|
|
|
// Make sure that the options struct has been initialized.
|
|
|
|
*clOptions;
|
|
|
|
}
|
2019-11-14 06:21:16 +08:00
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
/// Initialize the printing flags with default supplied by the cl::opts above.
|
|
|
|
OpPrintingFlags::OpPrintingFlags()
|
2020-04-12 14:11:51 +08:00
|
|
|
: printDebugInfoFlag(false), printDebugInfoPrettyFormFlag(false),
|
|
|
|
printGenericOpFormFlag(false), printLocalScope(false) {
|
|
|
|
// Initialize based upon command line options, if they are available.
|
|
|
|
if (!clOptions.isConstructed())
|
|
|
|
return;
|
|
|
|
if (clOptions->elideElementsAttrIfLarger.getNumOccurrences())
|
|
|
|
elementsAttrElementLimit = clOptions->elideElementsAttrIfLarger;
|
|
|
|
printDebugInfoFlag = clOptions->printDebugInfoOpt;
|
|
|
|
printDebugInfoPrettyFormFlag = clOptions->printPrettyDebugInfoOpt;
|
|
|
|
printGenericOpFormFlag = clOptions->printGenericOpFormOpt;
|
|
|
|
printLocalScope = clOptions->printLocalScopeOpt;
|
|
|
|
}
|
2019-10-08 04:54:16 +08:00
|
|
|
|
2019-10-08 08:18:54 +08:00
|
|
|
/// Enable the elision of large elements attributes, by printing a '...'
|
|
|
|
/// instead of the element data, when the number of elements is greater than
|
|
|
|
/// `largeElementLimit`. Note: The IR generated with this option is not
|
|
|
|
/// parsable.
|
|
|
|
OpPrintingFlags &
|
|
|
|
OpPrintingFlags::elideLargeElementsAttrs(int64_t largeElementLimit) {
|
|
|
|
elementsAttrElementLimit = largeElementLimit;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
/// Enable printing of debug information. If 'prettyForm' is set to true,
|
|
|
|
/// debug information is printed in a more readable 'pretty' form.
|
|
|
|
OpPrintingFlags &OpPrintingFlags::enableDebugInfo(bool prettyForm) {
|
|
|
|
printDebugInfoFlag = true;
|
|
|
|
printDebugInfoPrettyFormFlag = prettyForm;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Always print operations in the generic form.
|
|
|
|
OpPrintingFlags &OpPrintingFlags::printGenericOpForm() {
|
|
|
|
printGenericOpFormFlag = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:36:40 +08:00
|
|
|
/// Use local scope when printing the operation. This allows for using the
|
|
|
|
/// printer in a more localized and thread-safe setting, but may not necessarily
|
|
|
|
/// be identical of what the IR will look like when dumping the full module.
|
|
|
|
OpPrintingFlags &OpPrintingFlags::useLocalScope() {
|
|
|
|
printLocalScope = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-10-08 08:18:54 +08:00
|
|
|
/// Return if the given ElementsAttr should be elided.
|
|
|
|
bool OpPrintingFlags::shouldElideElementsAttr(ElementsAttr attr) const {
|
|
|
|
return elementsAttrElementLimit.hasValue() &&
|
|
|
|
*elementsAttrElementLimit < int64_t(attr.getNumElements());
|
|
|
|
}
|
|
|
|
|
2020-04-09 03:57:02 +08:00
|
|
|
/// Return the size limit for printing large ElementsAttr.
|
|
|
|
Optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const {
|
|
|
|
return elementsAttrElementLimit;
|
|
|
|
}
|
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
/// Return if debug information should be printed.
|
|
|
|
bool OpPrintingFlags::shouldPrintDebugInfo() const {
|
|
|
|
return printDebugInfoFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return if debug information should be printed in the pretty form.
|
|
|
|
bool OpPrintingFlags::shouldPrintDebugInfoPrettyForm() const {
|
|
|
|
return printDebugInfoPrettyFormFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return if operations should be printed in the generic form.
|
|
|
|
bool OpPrintingFlags::shouldPrintGenericOpForm() const {
|
|
|
|
return printGenericOpFormFlag;
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:36:40 +08:00
|
|
|
/// Return if the printer should use local scope when dumping the IR.
|
|
|
|
bool OpPrintingFlags::shouldUseLocalScope() const { return printLocalScope; }
|
|
|
|
|
2020-04-12 14:11:51 +08:00
|
|
|
/// Returns true if an ElementsAttr with the given number of elements should be
|
|
|
|
/// printed with hex.
|
|
|
|
static bool shouldPrintElementsAttrWithHex(int64_t numElements) {
|
|
|
|
// Check to see if a command line option was provided for the limit.
|
|
|
|
if (clOptions.isConstructed()) {
|
|
|
|
if (clOptions->printElementsAttrWithHexIfLarger.getNumOccurrences()) {
|
|
|
|
// -1 is used to disable hex printing.
|
|
|
|
if (clOptions->printElementsAttrWithHexIfLarger == -1)
|
|
|
|
return false;
|
|
|
|
return numElements > clOptions->printElementsAttrWithHexIfLarger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, default to printing with hex if the number of elements is >100.
|
|
|
|
return numElements > 100;
|
|
|
|
}
|
|
|
|
|
2020-02-09 07:01:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NewLineCounter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// This class is a simple formatter that emits a new line when inputted into a
|
|
|
|
/// stream, that enables counting the number of newlines emitted. This class
|
|
|
|
/// should be used whenever emitting newlines in the printer.
|
|
|
|
struct NewLineCounter {
|
|
|
|
unsigned curLine = 1;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static raw_ostream &operator<<(raw_ostream &os, NewLineCounter &newLine) {
|
|
|
|
++newLine.curLine;
|
|
|
|
return os << '\n';
|
|
|
|
}
|
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-01-09 02:11:56 +08:00
|
|
|
// AliasState
|
2019-10-08 04:54:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-02-05 05:01:06 +08:00
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
namespace {
|
2020-01-09 02:11:56 +08:00
|
|
|
/// This class manages the state for type and attribute aliases.
|
|
|
|
class AliasState {
|
2018-07-19 01:16:05 +08:00
|
|
|
public:
|
2020-01-09 02:11:56 +08:00
|
|
|
// Initialize the internal aliases.
|
|
|
|
void
|
|
|
|
initialize(Operation *op,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces);
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
/// Return a name used for an attribute alias, or empty if there is no alias.
|
|
|
|
Twine getAttributeAlias(Attribute attr) const;
|
2018-08-08 05:24:38 +08:00
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
/// Print all of the referenced attribute aliases.
|
2020-02-09 07:01:34 +08:00
|
|
|
void printAttributeAliases(raw_ostream &os, NewLineCounter &newLine) const;
|
2019-01-11 14:08:39 +08:00
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
/// Return a string to use as an alias for the given type, or empty if there
|
|
|
|
/// is no alias recorded.
|
|
|
|
StringRef getTypeAlias(Type ty) const;
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
/// Print all of the referenced type aliases.
|
2020-02-09 07:01:34 +08:00
|
|
|
void printTypeAliases(raw_ostream &os, NewLineCounter &newLine) const;
|
2019-08-22 07:50:30 +08:00
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
private:
|
2020-01-09 02:11:56 +08:00
|
|
|
/// A special index constant used for non-kind attribute aliases.
|
|
|
|
enum { NonAttrKindAlias = -1 };
|
2019-05-07 01:36:32 +08:00
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
/// Record a reference to the given attribute.
|
|
|
|
void recordAttributeReference(Attribute attr);
|
2018-08-08 05:24:38 +08:00
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
/// Record a reference to the given type.
|
|
|
|
void recordTypeReference(Type ty);
|
2019-01-11 14:08:39 +08:00
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
// Visit functions.
|
2019-03-28 23:24:38 +08:00
|
|
|
void visitOperation(Operation *op);
|
2018-10-31 05:59:22 +08:00
|
|
|
void visitType(Type type);
|
2018-10-26 06:46:10 +08:00
|
|
|
void visitAttribute(Attribute attr);
|
2018-07-19 07:29:21 +08:00
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
/// Set of attributes known to be used within the module.
|
|
|
|
llvm::SetVector<Attribute> usedAttributes;
|
2018-08-08 05:24:38 +08:00
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
/// Mapping between attribute and a pair comprised of a base alias name and a
|
|
|
|
/// count suffix. If the suffix is set to -1, it is not displayed.
|
|
|
|
llvm::MapVector<Attribute, std::pair<StringRef, int>> attrToAlias;
|
2019-01-11 14:08:39 +08:00
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
/// Mapping between attribute kind and a pair comprised of a base alias name
|
|
|
|
/// and a unique list of attributes belonging to this kind sorted by location
|
|
|
|
/// seen in the module.
|
|
|
|
llvm::MapVector<unsigned, std::pair<StringRef, std::vector<Attribute>>>
|
|
|
|
attrKindToAlias;
|
|
|
|
|
|
|
|
/// Set of types known to be used within the module.
|
2019-01-11 14:08:39 +08:00
|
|
|
llvm::SetVector<Type> usedTypes;
|
2019-05-07 01:36:32 +08:00
|
|
|
|
|
|
|
/// A mapping between a type and a given alias.
|
2019-01-11 14:08:39 +08:00
|
|
|
DenseMap<Type, StringRef> typeToAlias;
|
2018-07-18 07:56:54 +08:00
|
|
|
};
|
2018-07-24 02:44:40 +08:00
|
|
|
} // end anonymous namespace
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2019-01-11 14:08:39 +08:00
|
|
|
// Utility to generate a function to register a symbol alias.
|
2019-05-07 01:36:32 +08:00
|
|
|
static bool canRegisterAlias(StringRef name, llvm::StringSet<> &usedAliases) {
|
2019-01-11 14:08:39 +08:00
|
|
|
assert(!name.empty() && "expected alias name to be non-empty");
|
|
|
|
// TODO(riverriddle) Assert that the provided alias name can be lexed as
|
|
|
|
// an identifier.
|
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
// Check that the alias doesn't contain a '.' character and the name is not
|
|
|
|
// already in use.
|
|
|
|
return !name.contains('.') && usedAliases.insert(name).second;
|
2019-01-11 14:08:39 +08:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
void AliasState::initialize(
|
|
|
|
Operation *op,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces) {
|
2019-01-11 14:08:39 +08:00
|
|
|
// Track the identifiers in use for each symbol so that the same identifier
|
|
|
|
// isn't used twice.
|
|
|
|
llvm::StringSet<> usedAliases;
|
|
|
|
|
|
|
|
// Collect the set of aliases from each dialect.
|
2019-05-07 01:36:32 +08:00
|
|
|
SmallVector<std::pair<unsigned, StringRef>, 8> attributeKindAliases;
|
|
|
|
SmallVector<std::pair<Attribute, StringRef>, 8> attributeAliases;
|
|
|
|
SmallVector<std::pair<Type, StringRef>, 16> typeAliases;
|
|
|
|
|
|
|
|
// AffineMap/Integer set have specific kind aliases.
|
2019-05-14 03:34:42 +08:00
|
|
|
attributeKindAliases.emplace_back(StandardAttributes::AffineMap, "map");
|
|
|
|
attributeKindAliases.emplace_back(StandardAttributes::IntegerSet, "set");
|
2019-05-07 01:36:32 +08:00
|
|
|
|
2019-08-22 00:41:37 +08:00
|
|
|
for (auto &interface : interfaces) {
|
|
|
|
interface.getAttributeKindAliases(attributeKindAliases);
|
|
|
|
interface.getAttributeAliases(attributeAliases);
|
|
|
|
interface.getTypeAliases(typeAliases);
|
2019-01-11 14:08:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
// Setup the attribute kind aliases.
|
|
|
|
StringRef alias;
|
|
|
|
unsigned attrKind;
|
|
|
|
for (auto &attrAliasPair : attributeKindAliases) {
|
|
|
|
std::tie(attrKind, alias) = attrAliasPair;
|
|
|
|
assert(!alias.empty() && "expected non-empty alias string");
|
|
|
|
if (!usedAliases.count(alias) && !alias.contains('.'))
|
|
|
|
attrKindToAlias.insert({attrKind, {alias, {}}});
|
2019-01-11 14:08:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
// Clear the set of used identifiers so that the attribute kind aliases are
|
|
|
|
// just a prefix and not the full alias, i.e. there may be some overlap.
|
|
|
|
usedAliases.clear();
|
|
|
|
|
|
|
|
// Register the attribute aliases.
|
|
|
|
// Create a regex for the attribute kind alias names, these have a prefix with
|
|
|
|
// a counter appended to the end. We prevent normal aliases from having these
|
|
|
|
// names to avoid collisions.
|
|
|
|
llvm::Regex reservedAttrNames("[0-9]+$");
|
|
|
|
|
|
|
|
// Attribute value aliases.
|
|
|
|
Attribute attr;
|
|
|
|
for (auto &attrAliasPair : attributeAliases) {
|
|
|
|
std::tie(attr, alias) = attrAliasPair;
|
|
|
|
if (!reservedAttrNames.match(alias) && canRegisterAlias(alias, usedAliases))
|
2020-01-09 02:11:56 +08:00
|
|
|
attrToAlias.insert({attr, {alias, NonAttrKindAlias}});
|
2019-01-11 14:08:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the set of used identifiers as types can have the same identifiers as
|
|
|
|
// affine structures.
|
|
|
|
usedAliases.clear();
|
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
// Type aliases.
|
2019-01-11 14:08:39 +08:00
|
|
|
for (auto &typeAliasPair : typeAliases)
|
2019-05-07 01:36:32 +08:00
|
|
|
if (canRegisterAlias(typeAliasPair.second, usedAliases))
|
|
|
|
typeToAlias.insert(typeAliasPair);
|
2020-01-09 02:11:56 +08:00
|
|
|
|
|
|
|
// Traverse the given IR to generate the set of used attributes/types.
|
|
|
|
op->walk([&](Operation *op) { visitOperation(op); });
|
2019-01-11 14:08:39 +08:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
/// Return a name used for an attribute alias, or empty if there is no alias.
|
|
|
|
Twine AliasState::getAttributeAlias(Attribute attr) const {
|
|
|
|
auto alias = attrToAlias.find(attr);
|
|
|
|
if (alias == attrToAlias.end())
|
|
|
|
return Twine();
|
2019-05-07 01:36:32 +08:00
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
// Return the alias for this attribute, along with the index if this was
|
|
|
|
// generated by a kind alias.
|
|
|
|
int kindIndex = alias->second.second;
|
|
|
|
return alias->second.first +
|
|
|
|
(kindIndex == NonAttrKindAlias ? Twine() : Twine(kindIndex));
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
/// Print all of the referenced attribute aliases.
|
2020-02-09 07:01:34 +08:00
|
|
|
void AliasState::printAttributeAliases(raw_ostream &os,
|
|
|
|
NewLineCounter &newLine) const {
|
2020-01-09 02:11:56 +08:00
|
|
|
auto printAlias = [&](StringRef alias, Attribute attr, int index) {
|
|
|
|
os << '#' << alias;
|
|
|
|
if (index != NonAttrKindAlias)
|
|
|
|
os << index;
|
2020-02-09 07:01:34 +08:00
|
|
|
os << " = " << attr << newLine;
|
2020-01-09 02:11:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Print all of the attribute kind aliases.
|
|
|
|
for (auto &kindAlias : attrKindToAlias) {
|
|
|
|
auto &aliasAttrsPair = kindAlias.second;
|
|
|
|
for (unsigned i = 0, e = aliasAttrsPair.second.size(); i != e; ++i)
|
|
|
|
printAlias(aliasAttrsPair.first, aliasAttrsPair.second[i], i);
|
2020-02-09 07:01:34 +08:00
|
|
|
os << newLine;
|
2020-01-09 02:11:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// In a second pass print all of the remaining attribute aliases that aren't
|
|
|
|
// kind aliases.
|
|
|
|
for (Attribute attr : usedAttributes) {
|
|
|
|
auto alias = attrToAlias.find(attr);
|
|
|
|
if (alias != attrToAlias.end() && alias->second.second == NonAttrKindAlias)
|
|
|
|
printAlias(alias->second.first, attr, alias->second.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a string to use as an alias for the given type, or empty if there
|
|
|
|
/// is no alias recorded.
|
|
|
|
StringRef AliasState::getTypeAlias(Type ty) const {
|
|
|
|
return typeToAlias.lookup(ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print all of the referenced type aliases.
|
2020-02-09 07:01:34 +08:00
|
|
|
void AliasState::printTypeAliases(raw_ostream &os,
|
|
|
|
NewLineCounter &newLine) const {
|
2020-01-09 02:11:56 +08:00
|
|
|
for (Type type : usedTypes) {
|
|
|
|
auto alias = typeToAlias.find(type);
|
|
|
|
if (alias != typeToAlias.end())
|
2020-02-09 07:01:34 +08:00
|
|
|
os << '!' << alias->second << " = type " << type << newLine;
|
2020-01-09 02:11:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Record a reference to the given attribute.
|
|
|
|
void AliasState::recordAttributeReference(Attribute attr) {
|
|
|
|
// Don't recheck attributes that have already been seen or those that
|
|
|
|
// already have an alias.
|
|
|
|
if (!usedAttributes.insert(attr) || attrToAlias.count(attr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If this attribute kind has an alias, then record one for this attribute.
|
|
|
|
auto alias = attrKindToAlias.find(static_cast<unsigned>(attr.getKind()));
|
|
|
|
if (alias == attrKindToAlias.end())
|
|
|
|
return;
|
|
|
|
std::pair<StringRef, int> attrAlias(alias->second.first,
|
|
|
|
alias->second.second.size());
|
|
|
|
attrToAlias.insert({attr, attrAlias});
|
|
|
|
alias->second.second.push_back(attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Record a reference to the given type.
|
|
|
|
void AliasState::recordTypeReference(Type ty) { usedTypes.insert(ty); }
|
|
|
|
|
|
|
|
// TODO Support visiting other types/operations when implemented.
|
|
|
|
void AliasState::visitType(Type type) {
|
|
|
|
recordTypeReference(type);
|
|
|
|
|
|
|
|
if (auto funcType = type.dyn_cast<FunctionType>()) {
|
|
|
|
// Visit input and result types for functions.
|
|
|
|
for (auto input : funcType.getInputs())
|
|
|
|
visitType(input);
|
|
|
|
for (auto result : funcType.getResults())
|
|
|
|
visitType(result);
|
|
|
|
} else if (auto shapedType = type.dyn_cast<ShapedType>()) {
|
|
|
|
visitType(shapedType.getElementType());
|
|
|
|
|
|
|
|
// Visit affine maps in memref type.
|
|
|
|
if (auto memref = type.dyn_cast<MemRefType>())
|
|
|
|
for (auto map : memref.getAffineMaps())
|
|
|
|
recordAttributeReference(AffineMapAttr::get(map));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AliasState::visitAttribute(Attribute attr) {
|
|
|
|
recordAttributeReference(attr);
|
|
|
|
|
|
|
|
if (auto arrayAttr = attr.dyn_cast<ArrayAttr>()) {
|
|
|
|
for (auto elt : arrayAttr.getValue())
|
|
|
|
visitAttribute(elt);
|
|
|
|
} else if (auto typeAttr = attr.dyn_cast<TypeAttr>()) {
|
|
|
|
visitType(typeAttr.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AliasState::visitOperation(Operation *op) {
|
|
|
|
// Visit all the types used in the operation.
|
|
|
|
for (auto type : op->getOperandTypes())
|
|
|
|
visitType(type);
|
|
|
|
for (auto type : op->getResultTypes())
|
|
|
|
visitType(type);
|
|
|
|
for (auto ®ion : op->getRegions())
|
|
|
|
for (auto &block : region)
|
|
|
|
for (auto arg : block.getArguments())
|
2020-01-12 00:54:04 +08:00
|
|
|
visitType(arg.getType());
|
2020-01-09 02:11:56 +08:00
|
|
|
|
|
|
|
// Visit each of the attributes.
|
|
|
|
for (auto elt : op->getAttrs())
|
|
|
|
visitAttribute(elt.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-01-10 04:39:26 +08:00
|
|
|
// SSANameState
|
2020-01-09 02:11:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2020-01-10 04:39:26 +08:00
|
|
|
/// This class manages the state of SSA value names.
|
|
|
|
class SSANameState {
|
2020-01-09 02:11:56 +08:00
|
|
|
public:
|
2020-01-20 11:14:37 +08:00
|
|
|
/// A sentinel value used for values with names set.
|
2020-01-10 04:39:26 +08:00
|
|
|
enum : unsigned { NameSentinel = ~0U };
|
2020-01-09 02:11:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
SSANameState(Operation *op,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces);
|
2020-01-09 02:11:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the SSA identifier for the given value to 'stream'. If
|
|
|
|
/// 'printResultNo' is true, it also presents the result number ('#' number)
|
|
|
|
/// of this value.
|
|
|
|
void printValueID(Value value, bool printResultNo, raw_ostream &stream) const;
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Return the result indices for each of the result groups registered by this
|
|
|
|
/// operation, or empty if none exist.
|
|
|
|
ArrayRef<int> getOpResultGroups(Operation *op);
|
2019-11-02 05:47:42 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Get the ID for the given block.
|
|
|
|
unsigned getBlockID(Block *block);
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Renumber the arguments for the specified region to the same names as the
|
|
|
|
/// SSA values in namesToUse. See OperationPrinter::shadowRegionArgs for
|
|
|
|
/// details.
|
|
|
|
void shadowRegionArgs(Region ®ion, ValueRange namesToUse);
|
2019-06-27 09:29:25 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
private:
|
|
|
|
/// Number the SSA values within the given IR unit.
|
|
|
|
void numberValuesInRegion(
|
|
|
|
Region ®ion,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces);
|
|
|
|
void numberValuesInBlock(
|
|
|
|
Block &block,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces);
|
|
|
|
void numberValuesInOp(
|
|
|
|
Operation &op,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces);
|
2019-02-05 23:12:46 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Given a result of an operation 'result', find the result group head
|
|
|
|
/// 'lookupValue' and the result of 'result' within that group in
|
|
|
|
/// 'lookupResultNo'. 'lookupResultNo' is only filled in if the result group
|
|
|
|
/// has more than 1 result.
|
|
|
|
void getResultIDAndNumber(OpResult result, Value &lookupValue,
|
|
|
|
Optional<int> &lookupResultNo) const;
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Set a special value name for the given value.
|
|
|
|
void setValueName(Value value, StringRef name);
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Uniques the given value name within the printer. If the given name
|
|
|
|
/// conflicts, it is automatically renamed.
|
|
|
|
StringRef uniqueValueName(StringRef name);
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// This is the value ID for each SSA value. If this returns NameSentinel,
|
|
|
|
/// then the valueID has an entry in valueNames.
|
|
|
|
DenseMap<Value, unsigned> valueIDs;
|
|
|
|
DenseMap<Value, StringRef> valueNames;
|
2019-11-02 05:47:42 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// This is a map of operations that contain multiple named result groups,
|
|
|
|
/// i.e. there may be multiple names for the results of the operation. The
|
|
|
|
/// value of this map are the result numbers that start a result group.
|
|
|
|
DenseMap<Operation *, SmallVector<int, 1>> opResultGroups;
|
2019-08-22 03:16:23 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// This is the block ID for each block in the current.
|
|
|
|
DenseMap<Block *, unsigned> blockIDs;
|
2019-08-22 03:16:23 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// This keeps track of all of the non-numeric names that are in flight,
|
|
|
|
/// allowing us to check for duplicates.
|
|
|
|
/// Note: the value of the map is unused.
|
|
|
|
llvm::ScopedHashTable<StringRef, char> usedNames;
|
|
|
|
llvm::BumpPtrAllocator usedNameAllocator;
|
2019-10-08 04:54:16 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// This is the next value ID to assign in numbering.
|
|
|
|
unsigned nextValueID = 0;
|
|
|
|
/// This is the next ID to assign to a region entry block argument.
|
|
|
|
unsigned nextArgumentID = 0;
|
|
|
|
/// This is the next ID to assign when a name conflict is detected.
|
|
|
|
unsigned nextConflictID = 0;
|
2018-07-21 00:35:47 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
SSANameState::SSANameState(
|
|
|
|
Operation *op,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces) {
|
|
|
|
llvm::ScopedHashTable<StringRef, char>::ScopeTy usedNamesScope(usedNames);
|
|
|
|
numberValuesInOp(*op, interfaces);
|
2019-01-24 05:11:23 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
for (auto ®ion : op->getRegions())
|
|
|
|
numberValuesInRegion(region, interfaces);
|
2019-01-24 05:11:23 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void SSANameState::printValueID(Value value, bool printResultNo,
|
|
|
|
raw_ostream &stream) const {
|
|
|
|
if (!value) {
|
|
|
|
stream << "<<NULL>>";
|
|
|
|
return;
|
2019-01-24 05:11:23 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
Optional<int> resultNo;
|
|
|
|
auto lookupValue = value;
|
2018-08-16 00:09:54 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// If this is an operation result, collect the head lookup value of the result
|
|
|
|
// group and the result number of 'result' within that group.
|
|
|
|
if (OpResult result = value.dyn_cast<OpResult>())
|
|
|
|
getResultIDAndNumber(result, lookupValue, resultNo);
|
2019-07-31 05:05:49 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
auto it = valueIDs.find(lookupValue);
|
|
|
|
if (it == valueIDs.end()) {
|
|
|
|
stream << "<<UNKNOWN SSA VALUE>>";
|
2019-07-31 05:05:49 +08:00
|
|
|
return;
|
2018-08-16 00:09:54 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
stream << '%';
|
|
|
|
if (it->second != NameSentinel) {
|
|
|
|
stream << it->second;
|
2019-01-26 12:37:33 +08:00
|
|
|
} else {
|
2020-01-10 04:39:26 +08:00
|
|
|
auto nameIt = valueNames.find(lookupValue);
|
|
|
|
assert(nameIt != valueNames.end() && "Didn't have a name entry?");
|
|
|
|
stream << nameIt->second;
|
2019-01-26 12:37:33 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
if (resultNo.hasValue() && printResultNo)
|
|
|
|
stream << '#' << resultNo;
|
2019-01-24 05:11:23 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
ArrayRef<int> SSANameState::getOpResultGroups(Operation *op) {
|
|
|
|
auto it = opResultGroups.find(op);
|
|
|
|
return it == opResultGroups.end() ? ArrayRef<int>() : it->second;
|
|
|
|
}
|
2019-05-16 00:10:52 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
unsigned SSANameState::getBlockID(Block *block) {
|
|
|
|
auto it = blockIDs.find(block);
|
|
|
|
return it != blockIDs.end() ? it->second : NameSentinel;
|
|
|
|
}
|
2019-05-16 00:10:52 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void SSANameState::shadowRegionArgs(Region ®ion, ValueRange namesToUse) {
|
|
|
|
assert(!region.empty() && "cannot shadow arguments of an empty region");
|
|
|
|
assert(region.front().getNumArguments() == namesToUse.size() &&
|
|
|
|
"incorrect number of names passed in");
|
|
|
|
assert(region.getParentOp()->isKnownIsolatedFromAbove() &&
|
|
|
|
"only KnownIsolatedFromAbove ops can shadow names");
|
2019-05-16 00:10:52 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
SmallVector<char, 16> nameStr;
|
|
|
|
for (unsigned i = 0, e = namesToUse.size(); i != e; ++i) {
|
|
|
|
auto nameToUse = namesToUse[i];
|
|
|
|
if (nameToUse == nullptr)
|
2019-05-16 00:10:52 +08:00
|
|
|
continue;
|
2020-01-10 04:39:26 +08:00
|
|
|
auto nameToReplace = region.front().getArgument(i);
|
2019-05-16 00:10:52 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
nameStr.clear();
|
|
|
|
llvm::raw_svector_ostream nameStream(nameStr);
|
|
|
|
printValueID(nameToUse, /*printResultNo=*/true, nameStream);
|
2019-05-16 00:10:52 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Entry block arguments should already have a pretty "arg" name.
|
|
|
|
assert(valueIDs[nameToReplace] == NameSentinel);
|
2019-05-16 00:10:52 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Use the name without the leading %.
|
|
|
|
auto name = StringRef(nameStream.str()).drop_front();
|
2019-05-16 00:10:52 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Overwrite the name.
|
|
|
|
valueNames[nameToReplace] = name.copy(usedNameAllocator);
|
2019-05-16 00:10:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void SSANameState::numberValuesInRegion(
|
|
|
|
Region ®ion,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces) {
|
|
|
|
// Save the current value ids to allow for numbering values in sibling regions
|
|
|
|
// the same.
|
|
|
|
llvm::SaveAndRestore<unsigned> valueIDSaver(nextValueID);
|
|
|
|
llvm::SaveAndRestore<unsigned> argumentIDSaver(nextArgumentID);
|
|
|
|
llvm::SaveAndRestore<unsigned> conflictIDSaver(nextConflictID);
|
2019-10-09 08:44:39 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Push a new used names scope.
|
|
|
|
llvm::ScopedHashTable<StringRef, char>::ScopeTy usedNamesScope(usedNames);
|
2019-10-09 08:44:39 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Number the values within this region in a breadth-first order.
|
|
|
|
unsigned nextBlockID = 0;
|
|
|
|
for (auto &block : region) {
|
|
|
|
// Each block gets a unique ID, and all of the operations within it get
|
|
|
|
// numbered as well.
|
|
|
|
blockIDs[&block] = nextBlockID++;
|
|
|
|
numberValuesInBlock(block, interfaces);
|
2019-10-09 08:44:39 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// After that we traverse the nested regions.
|
|
|
|
// TODO: Rework this loop to not use recursion.
|
|
|
|
for (auto &block : region) {
|
|
|
|
for (auto &op : block)
|
|
|
|
for (auto &nestedRegion : op.getRegions())
|
|
|
|
numberValuesInRegion(nestedRegion, interfaces);
|
|
|
|
}
|
2019-10-09 08:44:39 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void SSANameState::numberValuesInBlock(
|
|
|
|
Block &block,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces) {
|
|
|
|
auto setArgNameFn = [&](Value arg, StringRef name) {
|
|
|
|
assert(!valueIDs.count(arg) && "arg numbered multiple times");
|
2020-01-12 00:54:04 +08:00
|
|
|
assert(arg.cast<BlockArgument>().getOwner() == &block &&
|
2020-01-10 04:39:26 +08:00
|
|
|
"arg not defined in 'block'");
|
|
|
|
setValueName(arg, name);
|
|
|
|
};
|
2019-12-05 02:19:20 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
bool isEntryBlock = block.isEntryBlock();
|
|
|
|
if (isEntryBlock) {
|
|
|
|
if (auto *op = block.getParentOp()) {
|
|
|
|
if (auto asmInterface = interfaces.getInterfaceFor(op->getDialect()))
|
|
|
|
asmInterface->getAsmBlockArgumentNames(&block, setArgNameFn);
|
|
|
|
}
|
2018-11-30 10:47:39 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Number the block arguments. We give entry block arguments a special name
|
|
|
|
// 'arg'.
|
|
|
|
SmallString<32> specialNameBuffer(isEntryBlock ? "arg" : "");
|
|
|
|
llvm::raw_svector_ostream specialName(specialNameBuffer);
|
|
|
|
for (auto arg : block.getArguments()) {
|
|
|
|
if (valueIDs.count(arg))
|
|
|
|
continue;
|
|
|
|
if (isEntryBlock) {
|
|
|
|
specialNameBuffer.resize(strlen("arg"));
|
|
|
|
specialName << nextArgumentID++;
|
2019-08-22 03:16:23 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
setValueName(arg, specialName.str());
|
2019-05-07 01:36:32 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Number the operations in this block.
|
|
|
|
for (auto &op : block)
|
|
|
|
numberValuesInOp(op, interfaces);
|
|
|
|
}
|
2019-06-27 09:29:25 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void SSANameState::numberValuesInOp(
|
|
|
|
Operation &op,
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> &interfaces) {
|
|
|
|
unsigned numResults = op.getNumResults();
|
|
|
|
if (numResults == 0)
|
2019-06-27 09:29:25 +08:00
|
|
|
return;
|
2020-01-10 04:39:26 +08:00
|
|
|
Value resultBegin = op.getResult(0);
|
2019-10-22 02:01:38 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Function used to set the special result names for the operation.
|
|
|
|
SmallVector<int, 2> resultGroups(/*Size=*/1, /*Value=*/0);
|
|
|
|
auto setResultNameFn = [&](Value result, StringRef name) {
|
|
|
|
assert(!valueIDs.count(result) && "result numbered multiple times");
|
2020-01-12 00:54:04 +08:00
|
|
|
assert(result.getDefiningOp() == &op && "result not defined by 'op'");
|
2020-01-10 04:39:26 +08:00
|
|
|
setValueName(result, name);
|
2019-10-22 02:01:38 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Record the result number for groups not anchored at 0.
|
2020-01-12 00:54:04 +08:00
|
|
|
if (int resultNo = result.cast<OpResult>().getResultNumber())
|
2020-01-10 04:39:26 +08:00
|
|
|
resultGroups.push_back(resultNo);
|
|
|
|
};
|
|
|
|
if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(&op))
|
|
|
|
asmInterface.getAsmResultNames(setResultNameFn);
|
|
|
|
else if (auto *asmInterface = interfaces.getInterfaceFor(op.getDialect()))
|
|
|
|
asmInterface->getAsmResultNames(&op, setResultNameFn);
|
2019-06-27 09:29:25 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// If the first result wasn't numbered, give it a default number.
|
|
|
|
if (valueIDs.try_emplace(resultBegin, nextValueID).second)
|
|
|
|
++nextValueID;
|
2019-06-27 09:29:25 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// If this operation has multiple result groups, mark it.
|
|
|
|
if (resultGroups.size() != 1) {
|
|
|
|
llvm::array_pod_sort(resultGroups.begin(), resultGroups.end());
|
|
|
|
opResultGroups.try_emplace(&op, std::move(resultGroups));
|
2019-02-05 23:12:46 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSANameState::getResultIDAndNumber(OpResult result, Value &lookupValue,
|
|
|
|
Optional<int> &lookupResultNo) const {
|
2020-01-12 00:54:04 +08:00
|
|
|
Operation *owner = result.getOwner();
|
2020-01-10 04:39:26 +08:00
|
|
|
if (owner->getNumResults() == 1)
|
|
|
|
return;
|
2020-01-12 00:54:04 +08:00
|
|
|
int resultNo = result.getResultNumber();
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
// If this operation has multiple result groups, we will need to find the
|
|
|
|
// one corresponding to this result.
|
|
|
|
auto resultGroupIt = opResultGroups.find(owner);
|
|
|
|
if (resultGroupIt == opResultGroups.end()) {
|
|
|
|
// If not, just use the first result.
|
|
|
|
lookupResultNo = resultNo;
|
|
|
|
lookupValue = owner->getResult(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the correct index using a binary search, as the groups are ordered.
|
|
|
|
ArrayRef<int> resultGroups = resultGroupIt->second;
|
|
|
|
auto it = llvm::upper_bound(resultGroups, resultNo);
|
|
|
|
int groupResultNo = 0, groupSize = 0;
|
|
|
|
|
|
|
|
// If there are no smaller elements, the last result group is the lookup.
|
|
|
|
if (it == resultGroups.end()) {
|
|
|
|
groupResultNo = resultGroups.back();
|
|
|
|
groupSize = static_cast<int>(owner->getNumResults()) - resultGroups.back();
|
|
|
|
} else {
|
|
|
|
// Otherwise, the previous element is the lookup.
|
|
|
|
groupResultNo = *std::prev(it);
|
|
|
|
groupSize = *it - groupResultNo;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only record the result number for a group of size greater than 1.
|
|
|
|
if (groupSize != 1)
|
|
|
|
lookupResultNo = resultNo - groupResultNo;
|
|
|
|
lookupValue = owner->getResult(groupResultNo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSANameState::setValueName(Value value, StringRef name) {
|
|
|
|
// If the name is empty, the value uses the default numbering.
|
|
|
|
if (name.empty()) {
|
|
|
|
valueIDs[value] = nextValueID++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
valueIDs[value] = NameSentinel;
|
|
|
|
valueNames[value] = uniqueValueName(name);
|
|
|
|
}
|
|
|
|
|
2020-03-13 13:26:44 +08:00
|
|
|
/// Returns true if 'c' is an allowable punctuation character: [$._-]
|
|
|
|
/// Returns false otherwise.
|
Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75919
2020-03-10 21:24:11 +08:00
|
|
|
static bool isPunct(char c) {
|
|
|
|
return c == '$' || c == '.' || c == '_' || c == '-';
|
|
|
|
}
|
Add support for custom op parser/printer hooks to know about result names.
Summary:
This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:
- Adds a new 'getResultName' method to OpAsmParser that allows a parser
implementation to get information about its result names, along with
a getNumResults() method that allows op parser impls to know how many
results are expected.
- Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
- Adds a test.string_attr_pretty_name operation that uses these hooks to
do fancy things with the result name.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76205
2020-03-16 08:13:59 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
StringRef SSANameState::uniqueValueName(StringRef name) {
|
Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75919
2020-03-10 21:24:11 +08:00
|
|
|
assert(!name.empty() && "Shouldn't have an empty name here");
|
Add support for custom op parser/printer hooks to know about result names.
Summary:
This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:
- Adds a new 'getResultName' method to OpAsmParser that allows a parser
implementation to get information about its result names, along with
a getNumResults() method that allows op parser impls to know how many
results are expected.
- Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
- Adds a test.string_attr_pretty_name operation that uses these hooks to
do fancy things with the result name.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76205
2020-03-16 08:13:59 +08:00
|
|
|
|
Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75919
2020-03-10 21:24:11 +08:00
|
|
|
// Check to see if this name is valid. If it starts with a digit, then it
|
|
|
|
// could conflict with the autogenerated numeric ID's (we unique them in a
|
|
|
|
// different map), so add an underscore prefix to avoid problems.
|
|
|
|
if (isdigit(name[0])) {
|
|
|
|
SmallString<16> tmpName("_");
|
|
|
|
tmpName += name;
|
|
|
|
return uniqueValueName(tmpName);
|
|
|
|
}
|
Add support for custom op parser/printer hooks to know about result names.
Summary:
This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:
- Adds a new 'getResultName' method to OpAsmParser that allows a parser
implementation to get information about its result names, along with
a getNumResults() method that allows op parser impls to know how many
results are expected.
- Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
- Adds a test.string_attr_pretty_name operation that uses these hooks to
do fancy things with the result name.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76205
2020-03-16 08:13:59 +08:00
|
|
|
|
Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75919
2020-03-10 21:24:11 +08:00
|
|
|
// Check to see if the name consists of all-valid identifiers. If not, we
|
|
|
|
// need to escape them.
|
2020-03-13 13:26:44 +08:00
|
|
|
for (char ch : name) {
|
Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75919
2020-03-10 21:24:11 +08:00
|
|
|
if (isalpha(ch) || isPunct(ch) || isdigit(ch))
|
|
|
|
continue;
|
Add support for custom op parser/printer hooks to know about result names.
Summary:
This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:
- Adds a new 'getResultName' method to OpAsmParser that allows a parser
implementation to get information about its result names, along with
a getNumResults() method that allows op parser impls to know how many
results are expected.
- Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
- Adds a test.string_attr_pretty_name operation that uses these hooks to
do fancy things with the result name.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76205
2020-03-16 08:13:59 +08:00
|
|
|
|
Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75919
2020-03-10 21:24:11 +08:00
|
|
|
SmallString<16> tmpName;
|
2020-03-13 13:26:44 +08:00
|
|
|
for (char ch : name) {
|
Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75919
2020-03-10 21:24:11 +08:00
|
|
|
if (isalpha(ch) || isPunct(ch) || isdigit(ch))
|
|
|
|
tmpName += ch;
|
|
|
|
else if (ch == ' ')
|
|
|
|
tmpName += '_';
|
|
|
|
else {
|
|
|
|
tmpName += llvm::utohexstr((unsigned char)ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uniqueValueName(tmpName);
|
|
|
|
}
|
Add support for custom op parser/printer hooks to know about result names.
Summary:
This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:
- Adds a new 'getResultName' method to OpAsmParser that allows a parser
implementation to get information about its result names, along with
a getNumResults() method that allows op parser impls to know how many
results are expected.
- Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
- Adds a test.string_attr_pretty_name operation that uses these hooks to
do fancy things with the result name.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76205
2020-03-16 08:13:59 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Check to see if this name is already unique.
|
|
|
|
if (!usedNames.count(name)) {
|
|
|
|
name = name.copy(usedNameAllocator);
|
|
|
|
} else {
|
|
|
|
// Otherwise, we had a conflict - probe until we find a unique name. This
|
|
|
|
// is guaranteed to terminate (and usually in a single iteration) because it
|
|
|
|
// generates new names by incrementing nextConflictID.
|
|
|
|
SmallString<64> probeName(name);
|
|
|
|
probeName.push_back('_');
|
|
|
|
while (true) {
|
|
|
|
probeName.resize(name.size() + 1);
|
|
|
|
probeName += llvm::utostr(nextConflictID++);
|
|
|
|
if (!usedNames.count(probeName)) {
|
|
|
|
name = StringRef(probeName).copy(usedNameAllocator);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usedNames.insert(name, char());
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-01-15 07:23:05 +08:00
|
|
|
// AsmState
|
2020-01-10 04:39:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-01-15 07:23:05 +08:00
|
|
|
namespace mlir {
|
|
|
|
namespace detail {
|
|
|
|
class AsmStateImpl {
|
2020-01-10 04:39:26 +08:00
|
|
|
public:
|
2020-02-09 07:01:34 +08:00
|
|
|
explicit AsmStateImpl(Operation *op, AsmState::LocationMap *locationMap)
|
|
|
|
: interfaces(op->getContext()), nameState(op, interfaces),
|
|
|
|
locationMap(locationMap) {}
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
/// Initialize the alias state to enable the printing of aliases.
|
|
|
|
void initializeAliases(Operation *op) {
|
|
|
|
aliasState.initialize(op, interfaces);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an instance of the OpAsmDialectInterface for the given dialect, or
|
|
|
|
/// null if one wasn't registered.
|
|
|
|
const OpAsmDialectInterface *getOpAsmInterface(Dialect *dialect) {
|
|
|
|
return interfaces.getInterfaceFor(dialect);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the state used for aliases.
|
|
|
|
AliasState &getAliasState() { return aliasState; }
|
|
|
|
|
|
|
|
/// Get the state used for SSA names.
|
|
|
|
SSANameState &getSSANameState() { return nameState; }
|
|
|
|
|
2020-02-09 07:01:34 +08:00
|
|
|
/// Register the location, line and column, within the buffer that the given
|
|
|
|
/// operation was printed at.
|
|
|
|
void registerOperationLocation(Operation *op, unsigned line, unsigned col) {
|
|
|
|
if (locationMap)
|
|
|
|
(*locationMap)[op] = std::make_pair(line, col);
|
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
private:
|
|
|
|
/// Collection of OpAsm interfaces implemented in the context.
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> interfaces;
|
|
|
|
|
|
|
|
/// The state used for attribute and type aliases.
|
|
|
|
AliasState aliasState;
|
|
|
|
|
|
|
|
/// The state used for SSA value names.
|
|
|
|
SSANameState nameState;
|
2020-02-09 07:01:34 +08:00
|
|
|
|
|
|
|
/// An optional location map to be populated.
|
|
|
|
AsmState::LocationMap *locationMap;
|
2020-01-10 04:39:26 +08:00
|
|
|
};
|
2020-01-15 07:23:05 +08:00
|
|
|
} // end namespace detail
|
|
|
|
} // end namespace mlir
|
|
|
|
|
2020-02-09 07:01:34 +08:00
|
|
|
AsmState::AsmState(Operation *op, LocationMap *locationMap)
|
|
|
|
: impl(std::make_unique<AsmStateImpl>(op, locationMap)) {}
|
2020-01-15 07:23:05 +08:00
|
|
|
AsmState::~AsmState() {}
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ModulePrinter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ModulePrinter {
|
|
|
|
public:
|
|
|
|
ModulePrinter(raw_ostream &os, OpPrintingFlags flags = llvm::None,
|
2020-01-15 07:23:05 +08:00
|
|
|
AsmStateImpl *state = nullptr)
|
2020-01-10 04:39:26 +08:00
|
|
|
: os(os), printerFlags(flags), state(state) {}
|
|
|
|
explicit ModulePrinter(ModulePrinter &printer)
|
|
|
|
: os(printer.os), printerFlags(printer.printerFlags),
|
|
|
|
state(printer.state) {}
|
|
|
|
|
|
|
|
/// Returns the output stream of the printer.
|
|
|
|
raw_ostream &getStream() { return os; }
|
|
|
|
|
|
|
|
template <typename Container, typename UnaryFunctor>
|
|
|
|
inline void interleaveComma(const Container &c, UnaryFunctor each_fn) const {
|
2020-04-15 05:53:28 +08:00
|
|
|
llvm::interleaveComma(c, os, each_fn);
|
2020-01-10 04:39:26 +08:00
|
|
|
}
|
|
|
|
|
[mlir] NFC: fix trivial typo in source files
Summary: fix trivial typos in the source files
Reviewers: mravishankar, antiagainst, nicolasvasilache, herhut, rriddle, aartbik
Reviewed By: antiagainst, rriddle
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, csigg, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, bader, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76876
2020-03-27 02:51:37 +08:00
|
|
|
/// This enum describes the different kinds of elision for the type of an
|
2020-02-09 02:01:17 +08:00
|
|
|
/// attribute when printing it.
|
|
|
|
enum class AttrTypeElision {
|
|
|
|
/// The type must not be elided,
|
|
|
|
Never,
|
|
|
|
/// The type may be elided when it matches the default used in the parser
|
|
|
|
/// (for example i64 is the default for integer attributes).
|
|
|
|
May,
|
|
|
|
/// The type must be elided.
|
|
|
|
Must
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Print the given attribute.
|
|
|
|
void printAttribute(Attribute attr,
|
|
|
|
AttrTypeElision typeElision = AttrTypeElision::Never);
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
void printType(Type type);
|
|
|
|
void printLocation(LocationAttr loc);
|
|
|
|
|
|
|
|
void printAffineMap(AffineMap map);
|
|
|
|
void
|
|
|
|
printAffineExpr(AffineExpr expr,
|
|
|
|
function_ref<void(unsigned, bool)> printValueName = nullptr);
|
|
|
|
void printAffineConstraint(AffineExpr expr, bool isEq);
|
|
|
|
void printIntegerSet(IntegerSet set);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
|
|
|
ArrayRef<StringRef> elidedAttrs = {},
|
|
|
|
bool withKeyword = false);
|
2020-03-12 04:22:19 +08:00
|
|
|
void printNamedAttribute(NamedAttribute attr);
|
2020-01-10 04:39:26 +08:00
|
|
|
void printTrailingLocation(Location loc);
|
|
|
|
void printLocationInternal(LocationAttr loc, bool pretty = false);
|
2020-02-21 06:37:25 +08:00
|
|
|
|
|
|
|
/// Print a dense elements attribute. If 'allowHex' is true, a hex string is
|
|
|
|
/// used instead of individual elements when the elements attr is large.
|
|
|
|
void printDenseElementsAttr(DenseElementsAttr attr, bool allowHex);
|
2020-01-10 04:39:26 +08:00
|
|
|
|
2020-04-24 10:01:51 +08:00
|
|
|
/// Print a dense string elements attribute.
|
|
|
|
void printDenseStringElementsAttr(DenseStringElementsAttr attr);
|
|
|
|
|
2020-04-25 16:17:09 +08:00
|
|
|
/// Print a dense elements attribute. If 'allowHex' is true, a hex string is
|
|
|
|
/// used instead of individual elements when the elements attr is large.
|
|
|
|
void printDenseIntOrFPElementsAttr(DenseIntOrFPElementsAttr attr,
|
|
|
|
bool allowHex);
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void printDialectAttribute(Attribute attr);
|
|
|
|
void printDialectType(Type type);
|
|
|
|
|
|
|
|
/// This enum is used to represent the binding strength of the enclosing
|
|
|
|
/// context that an AffineExprStorage is being printed in, so we can
|
|
|
|
/// intelligently produce parens.
|
|
|
|
enum class BindingStrength {
|
|
|
|
Weak, // + and -
|
|
|
|
Strong, // All other binary operators.
|
|
|
|
};
|
|
|
|
void printAffineExprInternal(
|
|
|
|
AffineExpr expr, BindingStrength enclosingTightness,
|
|
|
|
function_ref<void(unsigned, bool)> printValueName = nullptr);
|
|
|
|
|
|
|
|
/// The output stream for the printer.
|
|
|
|
raw_ostream &os;
|
|
|
|
|
|
|
|
/// A set of flags to control the printer's behavior.
|
|
|
|
OpPrintingFlags printerFlags;
|
|
|
|
|
|
|
|
/// An optional printer state for the module.
|
2020-01-15 07:23:05 +08:00
|
|
|
AsmStateImpl *state;
|
2020-02-09 07:01:34 +08:00
|
|
|
|
|
|
|
/// A tracker for the number of new lines emitted during printing.
|
|
|
|
NewLineCounter newLine;
|
2020-01-10 04:39:26 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void ModulePrinter::printTrailingLocation(Location loc) {
|
|
|
|
// Check to see if we are printing debug information.
|
|
|
|
if (!printerFlags.shouldPrintDebugInfo())
|
|
|
|
return;
|
|
|
|
|
|
|
|
os << " ";
|
|
|
|
printLocation(loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModulePrinter::printLocationInternal(LocationAttr loc, bool pretty) {
|
|
|
|
switch (loc.getKind()) {
|
|
|
|
case StandardAttributes::OpaqueLocation:
|
|
|
|
printLocationInternal(loc.cast<OpaqueLoc>().getFallbackLocation(), pretty);
|
|
|
|
break;
|
|
|
|
case StandardAttributes::UnknownLocation:
|
|
|
|
if (pretty)
|
|
|
|
os << "[unknown]";
|
|
|
|
else
|
|
|
|
os << "unknown";
|
|
|
|
break;
|
|
|
|
case StandardAttributes::FileLineColLocation: {
|
|
|
|
auto fileLoc = loc.cast<FileLineColLoc>();
|
|
|
|
auto mayQuote = pretty ? "" : "\"";
|
|
|
|
os << mayQuote << fileLoc.getFilename() << mayQuote << ':'
|
|
|
|
<< fileLoc.getLine() << ':' << fileLoc.getColumn();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StandardAttributes::NameLocation: {
|
|
|
|
auto nameLoc = loc.cast<NameLoc>();
|
|
|
|
os << '\"' << nameLoc.getName() << '\"';
|
|
|
|
|
|
|
|
// Print the child if it isn't unknown.
|
|
|
|
auto childLoc = nameLoc.getChildLoc();
|
|
|
|
if (!childLoc.isa<UnknownLoc>()) {
|
|
|
|
os << '(';
|
|
|
|
printLocationInternal(childLoc, pretty);
|
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StandardAttributes::CallSiteLocation: {
|
|
|
|
auto callLocation = loc.cast<CallSiteLoc>();
|
|
|
|
auto caller = callLocation.getCaller();
|
|
|
|
auto callee = callLocation.getCallee();
|
|
|
|
if (!pretty)
|
|
|
|
os << "callsite(";
|
|
|
|
printLocationInternal(callee, pretty);
|
|
|
|
if (pretty) {
|
|
|
|
if (callee.isa<NameLoc>()) {
|
|
|
|
if (caller.isa<FileLineColLoc>()) {
|
|
|
|
os << " at ";
|
|
|
|
} else {
|
2020-02-09 07:01:34 +08:00
|
|
|
os << newLine << " at ";
|
2020-01-10 04:39:26 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-09 07:01:34 +08:00
|
|
|
os << newLine << " at ";
|
2020-01-10 04:39:26 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
os << " at ";
|
|
|
|
}
|
|
|
|
printLocationInternal(caller, pretty);
|
|
|
|
if (!pretty)
|
|
|
|
os << ")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StandardAttributes::FusedLocation: {
|
|
|
|
auto fusedLoc = loc.cast<FusedLoc>();
|
|
|
|
if (!pretty)
|
|
|
|
os << "fused";
|
|
|
|
if (auto metadata = fusedLoc.getMetadata())
|
|
|
|
os << '<' << metadata << '>';
|
|
|
|
os << '[';
|
|
|
|
interleave(
|
|
|
|
fusedLoc.getLocations(),
|
|
|
|
[&](Location loc) { printLocationInternal(loc, pretty); },
|
|
|
|
[&]() { os << ", "; });
|
|
|
|
os << ']';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print a floating point value in a way that the parser will be able to
|
|
|
|
/// round-trip losslessly.
|
|
|
|
static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
|
|
|
|
// We would like to output the FP constant value in exponential notation,
|
|
|
|
// but we cannot do this if doing so will lose precision. Check here to
|
|
|
|
// make sure that we only output it in exponential format if we can parse
|
|
|
|
// the value back and get the same value.
|
|
|
|
bool isInf = apValue.isInfinity();
|
|
|
|
bool isNaN = apValue.isNaN();
|
|
|
|
if (!isInf && !isNaN) {
|
|
|
|
SmallString<128> strValue;
|
2020-02-07 10:05:32 +08:00
|
|
|
apValue.toString(strValue, /*FormatPrecision=*/6, /*FormatMaxPadding=*/0,
|
|
|
|
/*TruncateZero=*/false);
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
// Check to make sure that the stringized number is not some string like
|
|
|
|
// "Inf" or NaN, that atof will accept, but the lexer will not. Check
|
|
|
|
// that the string matches the "[-+]?[0-9]" regex.
|
|
|
|
assert(((strValue[0] >= '0' && strValue[0] <= '9') ||
|
|
|
|
((strValue[0] == '-' || strValue[0] == '+') &&
|
|
|
|
(strValue[1] >= '0' && strValue[1] <= '9'))) &&
|
|
|
|
"[-+]?[0-9] regex does not match!");
|
|
|
|
|
|
|
|
// Parse back the stringized version and check that the value is equal
|
2020-02-07 10:05:32 +08:00
|
|
|
// (i.e., there is no precision loss).
|
|
|
|
if (APFloat(apValue.getSemantics(), strValue).bitwiseIsEqual(apValue)) {
|
|
|
|
os << strValue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is not, use the default format of APFloat instead of the
|
|
|
|
// exponential notation.
|
|
|
|
strValue.clear();
|
|
|
|
apValue.toString(strValue);
|
|
|
|
|
|
|
|
// Make sure that we can parse the default form as a float.
|
|
|
|
if (StringRef(strValue).contains('.')) {
|
|
|
|
os << strValue;
|
|
|
|
return;
|
2020-01-10 04:39:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:05:32 +08:00
|
|
|
// Print special values in hexadecimal format. The sign bit should be included
|
|
|
|
// in the literal.
|
2020-01-10 04:39:26 +08:00
|
|
|
SmallVector<char, 16> str;
|
|
|
|
APInt apInt = apValue.bitcastToAPInt();
|
|
|
|
apInt.toString(str, /*Radix=*/16, /*Signed=*/false,
|
|
|
|
/*formatAsCLiteral=*/true);
|
|
|
|
os << str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModulePrinter::printLocation(LocationAttr loc) {
|
|
|
|
if (printerFlags.shouldPrintDebugInfoPrettyForm()) {
|
|
|
|
printLocationInternal(loc, /*pretty=*/true);
|
|
|
|
} else {
|
|
|
|
os << "loc(";
|
|
|
|
printLocationInternal(loc);
|
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns if the given dialect symbol data is simple enough to print in the
|
|
|
|
/// pretty form, i.e. without the enclosing "".
|
|
|
|
static bool isDialectSymbolSimpleEnoughForPrettyForm(StringRef symName) {
|
|
|
|
// The name must start with an identifier.
|
|
|
|
if (symName.empty() || !isalpha(symName.front()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Ignore all the characters that are valid in an identifier in the symbol
|
|
|
|
// name.
|
|
|
|
symName = symName.drop_while(
|
|
|
|
[](char c) { return llvm::isAlnum(c) || c == '.' || c == '_'; });
|
|
|
|
if (symName.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If we got to an unexpected character, then it must be a <>. Check those
|
|
|
|
// recursively.
|
|
|
|
if (symName.front() != '<' || symName.back() != '>')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<char, 8> nestedPunctuation;
|
|
|
|
do {
|
|
|
|
// If we ran out of characters, then we had a punctuation mismatch.
|
|
|
|
if (symName.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto c = symName.front();
|
|
|
|
symName = symName.drop_front();
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
// We never allow null characters. This is an EOF indicator for the lexer
|
|
|
|
// which we could handle, but isn't important for any known dialect.
|
|
|
|
case '\0':
|
|
|
|
return false;
|
|
|
|
case '<':
|
|
|
|
case '[':
|
|
|
|
case '(':
|
|
|
|
case '{':
|
|
|
|
nestedPunctuation.push_back(c);
|
|
|
|
continue;
|
|
|
|
case '-':
|
|
|
|
// Treat `->` as a special token.
|
|
|
|
if (!symName.empty() && symName.front() == '>') {
|
|
|
|
symName = symName.drop_front();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Reject types with mismatched brackets.
|
|
|
|
case '>':
|
|
|
|
if (nestedPunctuation.pop_back_val() != '<')
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
if (nestedPunctuation.pop_back_val() != '[')
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
if (nestedPunctuation.pop_back_val() != '(')
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
if (nestedPunctuation.pop_back_val() != '{')
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're done when the punctuation is fully matched.
|
|
|
|
} while (!nestedPunctuation.empty());
|
|
|
|
|
|
|
|
// If there were extra characters, then we failed.
|
|
|
|
return symName.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the given dialect symbol to the stream.
|
|
|
|
static void printDialectSymbol(raw_ostream &os, StringRef symPrefix,
|
|
|
|
StringRef dialectName, StringRef symString) {
|
|
|
|
os << symPrefix << dialectName;
|
|
|
|
|
|
|
|
// If this symbol name is simple enough, print it directly in pretty form,
|
|
|
|
// otherwise, we print it as an escaped string.
|
|
|
|
if (isDialectSymbolSimpleEnoughForPrettyForm(symString)) {
|
|
|
|
os << '.' << symString;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: escape the symbol name, it could contain " characters.
|
|
|
|
os << "<\"" << symString << "\">";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns if the given string can be represented as a bare identifier.
|
|
|
|
static bool isBareIdentifier(StringRef name) {
|
|
|
|
assert(!name.empty() && "invalid name");
|
|
|
|
|
|
|
|
// By making this unsigned, the value passed in to isalnum will always be
|
|
|
|
// in the range 0-255. This is important when building with MSVC because
|
|
|
|
// its implementation will assert. This situation can arise when dealing
|
|
|
|
// with UTF-8 multibyte characters.
|
|
|
|
unsigned char firstChar = static_cast<unsigned char>(name[0]);
|
|
|
|
if (!isalpha(firstChar) && firstChar != '_')
|
|
|
|
return false;
|
|
|
|
return llvm::all_of(name.drop_front(), [](unsigned char c) {
|
|
|
|
return isalnum(c) || c == '_' || c == '$' || c == '.';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the given string as a symbol reference. A symbol reference is
|
|
|
|
/// represented as a string prefixed with '@'. The reference is surrounded with
|
|
|
|
/// ""'s and escaped if it has any special or non-printable characters in it.
|
|
|
|
static void printSymbolReference(StringRef symbolRef, raw_ostream &os) {
|
|
|
|
assert(!symbolRef.empty() && "expected valid symbol reference");
|
|
|
|
|
|
|
|
// If the symbol can be represented as a bare identifier, write it directly.
|
|
|
|
if (isBareIdentifier(symbolRef)) {
|
|
|
|
os << '@' << symbolRef;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, output the reference wrapped in quotes with proper escaping.
|
|
|
|
os << "@\"";
|
|
|
|
printEscapedString(symbolRef, os);
|
|
|
|
os << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out a valid ElementsAttr that is succinct and can represent any
|
|
|
|
// potential shape/type, for use when eliding a large ElementsAttr.
|
|
|
|
//
|
|
|
|
// We choose to use an opaque ElementsAttr literal with conspicuous content to
|
|
|
|
// hopefully alert readers to the fact that this has been elided.
|
|
|
|
//
|
|
|
|
// Unfortunately, neither of the strings of an opaque ElementsAttr literal will
|
|
|
|
// accept the string "elided". The first string must be a registered dialect
|
|
|
|
// name and the latter must be a hex constant.
|
|
|
|
static void printElidedElementsAttr(raw_ostream &os) {
|
|
|
|
os << R"(opaque<"", "0xDEADBEEF">)";
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:01:17 +08:00
|
|
|
void ModulePrinter::printAttribute(Attribute attr,
|
|
|
|
AttrTypeElision typeElision) {
|
2020-01-10 04:39:26 +08:00
|
|
|
if (!attr) {
|
|
|
|
os << "<<NULL ATTRIBUTE>>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for an alias for this attribute.
|
|
|
|
if (state) {
|
|
|
|
Twine alias = state->getAliasState().getAttributeAlias(attr);
|
|
|
|
if (!alias.isTriviallyEmpty()) {
|
|
|
|
os << '#' << alias;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:01:17 +08:00
|
|
|
auto attrType = attr.getType();
|
2020-01-10 04:39:26 +08:00
|
|
|
switch (attr.getKind()) {
|
|
|
|
default:
|
|
|
|
return printDialectAttribute(attr);
|
|
|
|
|
|
|
|
case StandardAttributes::Opaque: {
|
|
|
|
auto opaqueAttr = attr.cast<OpaqueAttr>();
|
|
|
|
printDialectSymbol(os, "#", opaqueAttr.getDialectNamespace(),
|
|
|
|
opaqueAttr.getAttrData());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StandardAttributes::Unit:
|
|
|
|
os << "unit";
|
|
|
|
break;
|
|
|
|
case StandardAttributes::Bool:
|
|
|
|
os << (attr.cast<BoolAttr>().getValue() ? "true" : "false");
|
|
|
|
|
|
|
|
// BoolAttr always elides the type.
|
|
|
|
return;
|
|
|
|
case StandardAttributes::Dictionary:
|
|
|
|
os << '{';
|
|
|
|
interleaveComma(attr.cast<DictionaryAttr>().getValue(),
|
2020-03-12 04:22:19 +08:00
|
|
|
[&](NamedAttribute attr) { printNamedAttribute(attr); });
|
2020-01-10 04:39:26 +08:00
|
|
|
os << '}';
|
|
|
|
break;
|
|
|
|
case StandardAttributes::Integer: {
|
|
|
|
auto intAttr = attr.cast<IntegerAttr>();
|
Allow single-bit integer types to have signs. A signed one bit integer is either 0 or -1.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, grosul1, frgossen, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77832
2020-04-10 05:58:28 +08:00
|
|
|
// Only print attributes as unsigned if they are explicitly unsigned or are
|
|
|
|
// signless 1-bit values. Indexes, signed values, and multi-bit signless
|
|
|
|
// values print as signed.
|
|
|
|
bool isUnsigned =
|
|
|
|
attrType.isUnsignedInteger() || attrType.isSignlessInteger(1);
|
|
|
|
intAttr.getValue().print(os, !isUnsigned);
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
// IntegerAttr elides the type if I64.
|
2020-01-11 03:48:24 +08:00
|
|
|
if (typeElision == AttrTypeElision::May && attrType.isSignlessInteger(64))
|
2020-01-10 04:39:26 +08:00
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StandardAttributes::Float: {
|
|
|
|
auto floatAttr = attr.cast<FloatAttr>();
|
|
|
|
printFloatValue(floatAttr.getValue(), os);
|
|
|
|
|
|
|
|
// FloatAttr elides the type if F64.
|
2020-02-09 02:01:17 +08:00
|
|
|
if (typeElision == AttrTypeElision::May && attrType.isF64())
|
2020-01-10 04:39:26 +08:00
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StandardAttributes::String:
|
|
|
|
os << '"';
|
|
|
|
printEscapedString(attr.cast<StringAttr>().getValue(), os);
|
|
|
|
os << '"';
|
|
|
|
break;
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::Array:
|
2018-07-19 07:29:21 +08:00
|
|
|
os << '[';
|
2019-06-27 09:29:25 +08:00
|
|
|
interleaveComma(attr.cast<ArrayAttr>().getValue(), [&](Attribute attr) {
|
2020-02-09 02:01:17 +08:00
|
|
|
printAttribute(attr, AttrTypeElision::May);
|
2019-06-27 09:29:25 +08:00
|
|
|
});
|
2018-07-19 07:29:21 +08:00
|
|
|
os << ']';
|
|
|
|
break;
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::AffineMap:
|
2020-01-14 05:12:37 +08:00
|
|
|
os << "affine_map<";
|
2019-05-07 01:36:32 +08:00
|
|
|
attr.cast<AffineMapAttr>().getValue().print(os);
|
2020-01-14 05:12:37 +08:00
|
|
|
os << '>';
|
2019-06-27 09:29:25 +08:00
|
|
|
|
|
|
|
// AffineMap always elides the type.
|
|
|
|
return;
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::IntegerSet:
|
2020-01-14 05:12:37 +08:00
|
|
|
os << "affine_set<";
|
2019-05-07 01:36:32 +08:00
|
|
|
attr.cast<IntegerSetAttr>().getValue().print(os);
|
2020-01-14 05:12:37 +08:00
|
|
|
os << '>';
|
|
|
|
|
|
|
|
// IntegerSet always elides the type.
|
|
|
|
return;
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::Type:
|
2018-10-26 06:46:10 +08:00
|
|
|
printType(attr.cast<TypeAttr>().getValue());
|
2018-07-19 07:29:21 +08:00
|
|
|
break;
|
2019-11-12 10:18:02 +08:00
|
|
|
case StandardAttributes::SymbolRef: {
|
|
|
|
auto refAttr = attr.dyn_cast<SymbolRefAttr>();
|
|
|
|
printSymbolReference(refAttr.getRootReference(), os);
|
|
|
|
for (FlatSymbolRefAttr nestedRef : refAttr.getNestedReferences()) {
|
|
|
|
os << "::";
|
|
|
|
printSymbolReference(nestedRef.getValue(), os);
|
|
|
|
}
|
2018-08-20 12:17:22 +08:00
|
|
|
break;
|
2019-11-12 10:18:02 +08:00
|
|
|
}
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::OpaqueElements: {
|
2018-10-26 06:46:10 +08:00
|
|
|
auto eltsAttr = attr.cast<OpaqueElementsAttr>();
|
2019-12-05 02:19:20 +08:00
|
|
|
if (printerFlags.shouldElideElementsAttr(eltsAttr)) {
|
|
|
|
printElidedElementsAttr(os);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-26 07:06:13 +08:00
|
|
|
os << "opaque<\"" << eltsAttr.getDialect()->getNamespace() << "\", ";
|
2019-12-05 02:19:20 +08:00
|
|
|
os << '"' << "0x" << llvm::toHex(eltsAttr.getValue()) << "\">";
|
2018-10-24 04:44:04 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-24 10:01:51 +08:00
|
|
|
case StandardAttributes::DenseIntOrFPElements: {
|
2020-04-25 16:17:09 +08:00
|
|
|
auto eltsAttr = attr.cast<DenseIntOrFPElementsAttr>();
|
2019-12-05 02:19:20 +08:00
|
|
|
if (printerFlags.shouldElideElementsAttr(eltsAttr)) {
|
|
|
|
printElidedElementsAttr(os);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-19 04:54:44 +08:00
|
|
|
os << "dense<";
|
2020-04-25 16:17:09 +08:00
|
|
|
printDenseIntOrFPElementsAttr(eltsAttr, /*allowHex=*/true);
|
2019-06-27 09:29:25 +08:00
|
|
|
os << '>';
|
2018-10-19 04:54:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-24 10:01:51 +08:00
|
|
|
case StandardAttributes::DenseStringElements: {
|
|
|
|
auto eltsAttr = attr.cast<DenseStringElementsAttr>();
|
|
|
|
if (printerFlags.shouldElideElementsAttr(eltsAttr)) {
|
|
|
|
printElidedElementsAttr(os);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
os << "dense<";
|
|
|
|
printDenseStringElementsAttr(eltsAttr);
|
|
|
|
os << '>';
|
|
|
|
break;
|
|
|
|
}
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::SparseElements: {
|
2018-10-26 06:46:10 +08:00
|
|
|
auto elementsAttr = attr.cast<SparseElementsAttr>();
|
2019-12-05 02:19:20 +08:00
|
|
|
if (printerFlags.shouldElideElementsAttr(elementsAttr.getIndices()) ||
|
|
|
|
printerFlags.shouldElideElementsAttr(elementsAttr.getValues())) {
|
|
|
|
printElidedElementsAttr(os);
|
|
|
|
break;
|
|
|
|
}
|
Add support to constant sparse tensor / vector attribute
The SparseElementsAttr uses (COO) Coordinate List encoding to represents a
sparse tensor / vector. Specifically, the coordinates and values are stored as
two dense elements attributes. The first dense elements attribute is a 2-D
attribute with shape [N, ndims], which contains the indices of the elements
with nonzero values in the constant vector/tensor. The second elements
attribute is a 1-D attribute list with shape [N], which supplies the values for
each element in the first elements attribute. ndims is the rank of the
vector/tensor and N is the total nonzero elements.
The syntax is:
`sparse<` (tensor-type | vector-type)`, ` indices-attribute-list, values-attribute-list `>`
Example: a sparse tensor
sparse<vector<3x4xi32>, [[0, 0], [1, 2]], [1, 2]> represents the dense tensor
[[1, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 0]]
PiperOrigin-RevId: 217764319
2018-10-19 05:02:20 +08:00
|
|
|
os << "sparse<";
|
2020-04-25 16:17:09 +08:00
|
|
|
printDenseIntOrFPElementsAttr(elementsAttr.getIndices(),
|
|
|
|
/*allowHex=*/false);
|
Add support to constant sparse tensor / vector attribute
The SparseElementsAttr uses (COO) Coordinate List encoding to represents a
sparse tensor / vector. Specifically, the coordinates and values are stored as
two dense elements attributes. The first dense elements attribute is a 2-D
attribute with shape [N, ndims], which contains the indices of the elements
with nonzero values in the constant vector/tensor. The second elements
attribute is a 1-D attribute list with shape [N], which supplies the values for
each element in the first elements attribute. ndims is the rank of the
vector/tensor and N is the total nonzero elements.
The syntax is:
`sparse<` (tensor-type | vector-type)`, ` indices-attribute-list, values-attribute-list `>`
Example: a sparse tensor
sparse<vector<3x4xi32>, [[0, 0], [1, 2]], [1, 2]> represents the dense tensor
[[1, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 0]]
PiperOrigin-RevId: 217764319
2018-10-19 05:02:20 +08:00
|
|
|
os << ", ";
|
2020-02-21 06:37:25 +08:00
|
|
|
printDenseElementsAttr(elementsAttr.getValues(), /*allowHex=*/true);
|
2019-06-27 09:29:25 +08:00
|
|
|
os << '>';
|
Add support to constant sparse tensor / vector attribute
The SparseElementsAttr uses (COO) Coordinate List encoding to represents a
sparse tensor / vector. Specifically, the coordinates and values are stored as
two dense elements attributes. The first dense elements attribute is a 2-D
attribute with shape [N, ndims], which contains the indices of the elements
with nonzero values in the constant vector/tensor. The second elements
attribute is a 1-D attribute list with shape [N], which supplies the values for
each element in the first elements attribute. ndims is the rank of the
vector/tensor and N is the total nonzero elements.
The syntax is:
`sparse<` (tensor-type | vector-type)`, ` indices-attribute-list, values-attribute-list `>`
Example: a sparse tensor
sparse<vector<3x4xi32>, [[0, 0], [1, 2]], [1, 2]> represents the dense tensor
[[1, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 0]]
PiperOrigin-RevId: 217764319
2018-10-19 05:02:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-06-22 09:27:49 +08:00
|
|
|
|
|
|
|
// Location attributes.
|
|
|
|
case StandardAttributes::CallSiteLocation:
|
|
|
|
case StandardAttributes::FileLineColLocation:
|
|
|
|
case StandardAttributes::FusedLocation:
|
|
|
|
case StandardAttributes::NameLocation:
|
2019-10-07 20:05:07 +08:00
|
|
|
case StandardAttributes::OpaqueLocation:
|
2019-06-22 09:27:49 +08:00
|
|
|
case StandardAttributes::UnknownLocation:
|
|
|
|
printLocation(attr.cast<LocationAttr>());
|
|
|
|
break;
|
2018-07-19 07:29:21 +08:00
|
|
|
}
|
2019-06-27 09:29:25 +08:00
|
|
|
|
2020-02-09 02:01:17 +08:00
|
|
|
// Don't print the type if we must elide it, or if it is a None type.
|
|
|
|
if (typeElision != AttrTypeElision::Must && !attrType.isa<NoneType>()) {
|
2019-06-27 09:29:25 +08:00
|
|
|
os << " : ";
|
|
|
|
printType(attrType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:39:22 +08:00
|
|
|
/// Print the integer element of a DenseElementsAttr.
|
|
|
|
static void printDenseIntElement(const APInt &value, raw_ostream &os,
|
|
|
|
bool isSigned) {
|
2019-07-19 00:24:41 +08:00
|
|
|
if (value.getBitWidth() == 1)
|
|
|
|
os << (value.getBoolValue() ? "true" : "false");
|
|
|
|
else
|
2020-03-03 01:18:45 +08:00
|
|
|
value.print(os, isSigned);
|
2019-06-27 09:29:25 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 03:39:12 +08:00
|
|
|
static void
|
|
|
|
printDenseElementsAttrImpl(bool isSplat, ShapedType type, raw_ostream &os,
|
|
|
|
function_ref<void(unsigned)> printEltFn) {
|
2019-06-13 06:05:45 +08:00
|
|
|
// Special case for 0-d and splat tensors.
|
2020-05-06 03:39:12 +08:00
|
|
|
if (isSplat)
|
|
|
|
return printEltFn(0);
|
2019-04-02 01:01:47 +08:00
|
|
|
|
2018-10-19 04:54:44 +08:00
|
|
|
// Special case for degenerate tensors.
|
2019-06-27 09:29:25 +08:00
|
|
|
auto numElements = type.getNumElements();
|
2020-05-06 03:39:12 +08:00
|
|
|
int64_t rank = type.getRank();
|
2019-06-27 09:29:25 +08:00
|
|
|
if (numElements == 0) {
|
2018-10-19 04:54:44 +08:00
|
|
|
for (int i = 0; i < rank; ++i)
|
|
|
|
os << '[';
|
|
|
|
for (int i = 0; i < rank; ++i)
|
|
|
|
os << ']';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use a mixed-radix counter to iterate through the shape. When we bump a
|
|
|
|
// non-least-significant digit, we emit a close bracket. When we next emit an
|
|
|
|
// element we re-open all closed brackets.
|
|
|
|
|
|
|
|
// The mixed-radix counter, with radices in 'shape'.
|
|
|
|
SmallVector<unsigned, 4> counter(rank, 0);
|
|
|
|
// The number of brackets that have been opened and not closed.
|
|
|
|
unsigned openBrackets = 0;
|
|
|
|
|
2020-05-06 03:39:12 +08:00
|
|
|
auto shape = type.getShape();
|
|
|
|
auto bumpCounter = [&] {
|
2018-10-19 04:54:44 +08:00
|
|
|
// Bump the least significant digit.
|
|
|
|
++counter[rank - 1];
|
|
|
|
// Iterate backwards bubbling back the increment.
|
|
|
|
for (unsigned i = rank - 1; i > 0; --i)
|
|
|
|
if (counter[i] >= shape[i]) {
|
|
|
|
// Index 'i' is rolled over. Bump (i-1) and close a bracket.
|
|
|
|
counter[i] = 0;
|
|
|
|
++counter[i - 1];
|
|
|
|
--openBrackets;
|
|
|
|
os << ']';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-27 09:29:25 +08:00
|
|
|
for (unsigned idx = 0, e = numElements; idx != e; ++idx) {
|
2018-10-19 04:54:44 +08:00
|
|
|
if (idx != 0)
|
|
|
|
os << ", ";
|
|
|
|
while (openBrackets++ < rank)
|
|
|
|
os << '[';
|
|
|
|
openBrackets = rank;
|
2020-05-06 03:39:12 +08:00
|
|
|
printEltFn(idx);
|
2018-10-19 04:54:44 +08:00
|
|
|
bumpCounter();
|
|
|
|
}
|
|
|
|
while (openBrackets-- > 0)
|
|
|
|
os << ']';
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:39:12 +08:00
|
|
|
void ModulePrinter::printDenseElementsAttr(DenseElementsAttr attr,
|
|
|
|
bool allowHex) {
|
|
|
|
if (auto stringAttr = attr.dyn_cast<DenseStringElementsAttr>())
|
|
|
|
return printDenseStringElementsAttr(stringAttr);
|
2020-04-24 10:01:51 +08:00
|
|
|
|
2020-05-06 03:39:12 +08:00
|
|
|
printDenseIntOrFPElementsAttr(attr.cast<DenseIntOrFPElementsAttr>(),
|
|
|
|
allowHex);
|
|
|
|
}
|
2020-04-24 10:01:51 +08:00
|
|
|
|
2020-05-06 03:39:12 +08:00
|
|
|
void ModulePrinter::printDenseIntOrFPElementsAttr(DenseIntOrFPElementsAttr attr,
|
|
|
|
bool allowHex) {
|
|
|
|
auto type = attr.getType();
|
|
|
|
auto elementType = type.getElementType();
|
|
|
|
|
|
|
|
// Check to see if we should format this attribute as a hex string.
|
2020-04-24 10:01:51 +08:00
|
|
|
auto numElements = type.getNumElements();
|
2020-05-06 03:39:22 +08:00
|
|
|
if (!attr.isSplat() && allowHex &&
|
|
|
|
shouldPrintElementsAttrWithHex(numElements)) {
|
2020-05-06 03:39:12 +08:00
|
|
|
ArrayRef<char> rawData = attr.getRawData();
|
|
|
|
os << '"' << "0x" << llvm::toHex(StringRef(rawData.data(), rawData.size()))
|
|
|
|
<< "\"";
|
2020-04-24 10:01:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:39:22 +08:00
|
|
|
if (ComplexType complexTy = elementType.dyn_cast<ComplexType>()) {
|
|
|
|
auto printComplexValue = [&](auto complexValues, auto printFn,
|
|
|
|
raw_ostream &os, auto &&... params) {
|
|
|
|
printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
|
|
|
|
auto complexValue = *(complexValues.begin() + index);
|
|
|
|
os << "(";
|
|
|
|
printFn(complexValue.real(), os, params...);
|
|
|
|
os << ",";
|
|
|
|
printFn(complexValue.imag(), os, params...);
|
|
|
|
os << ")";
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Type complexElementType = complexTy.getElementType();
|
|
|
|
if (complexElementType.isa<IntegerType>())
|
|
|
|
printComplexValue(attr.getComplexIntValues(), printDenseIntElement, os,
|
|
|
|
/*isSigned=*/!complexElementType.isUnsignedInteger());
|
|
|
|
else
|
|
|
|
printComplexValue(attr.getComplexFloatValues(), printFloatValue, os);
|
|
|
|
} else if (elementType.isIntOrIndex()) {
|
2020-05-06 03:39:12 +08:00
|
|
|
bool isSigned = !elementType.isUnsignedInteger();
|
2020-05-06 03:39:22 +08:00
|
|
|
auto intValues = attr.getIntValues();
|
2020-05-06 03:39:12 +08:00
|
|
|
printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
|
2020-05-06 03:39:22 +08:00
|
|
|
printDenseIntElement(*(intValues.begin() + index), os, isSigned);
|
2020-05-06 03:39:12 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
assert(elementType.isa<FloatType>() && "unexpected element type");
|
2020-05-06 03:39:22 +08:00
|
|
|
auto floatValues = attr.getFloatValues();
|
2020-05-06 03:39:12 +08:00
|
|
|
printDenseElementsAttrImpl(attr.isSplat(), type, os, [&](unsigned index) {
|
2020-05-06 03:39:22 +08:00
|
|
|
printFloatValue(*(floatValues.begin() + index), os);
|
2020-05-06 03:39:12 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 10:01:51 +08:00
|
|
|
|
2020-05-06 03:39:12 +08:00
|
|
|
void ModulePrinter::printDenseStringElementsAttr(DenseStringElementsAttr attr) {
|
|
|
|
ArrayRef<StringRef> data = attr.getRawStringData();
|
|
|
|
auto printFn = [&](unsigned index) {
|
|
|
|
os << "\"";
|
|
|
|
printEscapedString(data[index], os);
|
|
|
|
os << "\"";
|
2020-04-24 10:01:51 +08:00
|
|
|
};
|
2020-05-06 03:39:12 +08:00
|
|
|
printDenseElementsAttrImpl(attr.isSplat(), attr.getType(), os, printFn);
|
2020-04-24 10:01:51 +08:00
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
void ModulePrinter::printType(Type type) {
|
2020-04-05 11:01:21 +08:00
|
|
|
if (!type) {
|
|
|
|
os << "<<NULL TYPE>>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-11 14:08:39 +08:00
|
|
|
// Check for an alias for this type.
|
2019-08-22 03:16:23 +08:00
|
|
|
if (state) {
|
2020-01-09 02:11:56 +08:00
|
|
|
StringRef alias = state->getAliasState().getTypeAlias(type);
|
2019-08-22 03:16:23 +08:00
|
|
|
if (!alias.empty()) {
|
|
|
|
os << '!' << alias;
|
|
|
|
return;
|
|
|
|
}
|
2019-01-11 14:08:39 +08:00
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
switch (type.getKind()) {
|
2019-11-02 05:47:42 +08:00
|
|
|
default:
|
|
|
|
return printDialectType(type);
|
2019-04-06 14:56:49 +08:00
|
|
|
|
2019-04-04 07:49:01 +08:00
|
|
|
case Type::Kind::Opaque: {
|
|
|
|
auto opaqueTy = type.cast<OpaqueType>();
|
2019-05-16 00:10:52 +08:00
|
|
|
printDialectSymbol(os, "!", opaqueTy.getDialectNamespace(),
|
|
|
|
opaqueTy.getTypeData());
|
2019-01-08 01:58:34 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-02-13 03:08:04 +08:00
|
|
|
case StandardTypes::Index:
|
2018-10-07 08:21:53 +08:00
|
|
|
os << "index";
|
2018-07-19 01:16:05 +08:00
|
|
|
return;
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::BF16:
|
2018-07-19 01:16:05 +08:00
|
|
|
os << "bf16";
|
|
|
|
return;
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::F16:
|
2018-07-19 01:16:05 +08:00
|
|
|
os << "f16";
|
|
|
|
return;
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::F32:
|
2018-07-19 01:16:05 +08:00
|
|
|
os << "f32";
|
|
|
|
return;
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::F64:
|
2018-07-19 01:16:05 +08:00
|
|
|
os << "f64";
|
|
|
|
return;
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::Integer: {
|
2018-10-31 05:59:22 +08:00
|
|
|
auto integer = type.cast<IntegerType>();
|
2020-01-11 03:48:24 +08:00
|
|
|
if (integer.isSigned())
|
|
|
|
os << 's';
|
|
|
|
else if (integer.isUnsigned())
|
|
|
|
os << 'u';
|
2018-10-31 05:59:22 +08:00
|
|
|
os << 'i' << integer.getWidth();
|
2018-07-18 07:56:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Type::Kind::Function: {
|
2018-10-31 05:59:22 +08:00
|
|
|
auto func = type.cast<FunctionType>();
|
2018-07-18 07:56:54 +08:00
|
|
|
os << '(';
|
2018-10-31 05:59:22 +08:00
|
|
|
interleaveComma(func.getInputs(), [&](Type type) { printType(type); });
|
2018-07-18 07:56:54 +08:00
|
|
|
os << ") -> ";
|
2018-10-31 05:59:22 +08:00
|
|
|
auto results = func.getResults();
|
2019-02-06 03:47:02 +08:00
|
|
|
if (results.size() == 1 && !results[0].isa<FunctionType>())
|
2018-10-31 05:59:22 +08:00
|
|
|
os << results[0];
|
2018-07-18 07:56:54 +08:00
|
|
|
else {
|
|
|
|
os << '(';
|
2018-10-31 05:59:22 +08:00
|
|
|
interleaveComma(results, [&](Type type) { printType(type); });
|
2018-07-18 07:56:54 +08:00
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::Vector: {
|
2018-10-31 05:59:22 +08:00
|
|
|
auto v = type.cast<VectorType>();
|
2018-07-18 07:56:54 +08:00
|
|
|
os << "vector<";
|
2018-10-31 05:59:22 +08:00
|
|
|
for (auto dim : v.getShape())
|
2018-07-24 02:44:40 +08:00
|
|
|
os << dim << 'x';
|
2018-10-31 05:59:22 +08:00
|
|
|
os << v.getElementType() << '>';
|
2018-07-18 07:56:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::RankedTensor: {
|
2018-10-31 05:59:22 +08:00
|
|
|
auto v = type.cast<RankedTensorType>();
|
2018-07-18 07:56:54 +08:00
|
|
|
os << "tensor<";
|
2018-10-31 05:59:22 +08:00
|
|
|
for (auto dim : v.getShape()) {
|
2018-07-18 07:56:54 +08:00
|
|
|
if (dim < 0)
|
|
|
|
os << '?';
|
|
|
|
else
|
|
|
|
os << dim;
|
|
|
|
os << 'x';
|
|
|
|
}
|
2018-10-31 05:59:22 +08:00
|
|
|
os << v.getElementType() << '>';
|
2018-07-18 07:56:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::UnrankedTensor: {
|
2018-10-31 05:59:22 +08:00
|
|
|
auto v = type.cast<UnrankedTensorType>();
|
2018-09-14 01:43:35 +08:00
|
|
|
os << "tensor<*x";
|
2018-10-31 05:59:22 +08:00
|
|
|
printType(v.getElementType());
|
2018-07-26 03:55:50 +08:00
|
|
|
os << '>';
|
2018-07-18 07:56:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-01-04 06:29:52 +08:00
|
|
|
case StandardTypes::MemRef: {
|
2018-10-31 05:59:22 +08:00
|
|
|
auto v = type.cast<MemRefType>();
|
2018-07-18 07:56:54 +08:00
|
|
|
os << "memref<";
|
2018-10-31 05:59:22 +08:00
|
|
|
for (auto dim : v.getShape()) {
|
2018-07-18 07:56:54 +08:00
|
|
|
if (dim < 0)
|
|
|
|
os << '?';
|
|
|
|
else
|
|
|
|
os << dim;
|
2020-01-10 04:39:26 +08:00
|
|
|
os << 'x';
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
printType(v.getElementType());
|
|
|
|
for (auto map : v.getAffineMaps()) {
|
|
|
|
os << ", ";
|
|
|
|
printAttribute(AffineMapAttr::get(map));
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
// Only print the memory space if it is the non-default one.
|
|
|
|
if (v.getMemorySpace())
|
|
|
|
os << ", " << v.getMemorySpace();
|
|
|
|
os << '>';
|
|
|
|
return;
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
case StandardTypes::UnrankedMemRef: {
|
|
|
|
auto v = type.cast<UnrankedMemRefType>();
|
|
|
|
os << "memref<*x";
|
|
|
|
printType(v.getElementType());
|
|
|
|
os << '>';
|
|
|
|
return;
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
case StandardTypes::Complex:
|
|
|
|
os << "complex<";
|
|
|
|
printType(type.cast<ComplexType>().getElementType());
|
|
|
|
os << '>';
|
|
|
|
return;
|
|
|
|
case StandardTypes::Tuple: {
|
|
|
|
auto tuple = type.cast<TupleType>();
|
|
|
|
os << "tuple<";
|
|
|
|
interleaveComma(tuple.getTypes(), [&](Type type) { printType(type); });
|
|
|
|
os << '>';
|
|
|
|
return;
|
2018-08-08 05:24:38 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
case StandardTypes::None:
|
|
|
|
os << "none";
|
|
|
|
return;
|
2018-08-08 05:24:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 07:36:26 +08:00
|
|
|
void ModulePrinter::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
2019-11-06 09:58:16 +08:00
|
|
|
ArrayRef<StringRef> elidedAttrs,
|
|
|
|
bool withKeyword) {
|
2018-09-19 07:36:26 +08:00
|
|
|
// If there are no attributes, then there is nothing to be done.
|
|
|
|
if (attrs.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Filter out any attributes that shouldn't be included.
|
2019-11-06 09:58:16 +08:00
|
|
|
SmallVector<NamedAttribute, 8> filteredAttrs(
|
|
|
|
llvm::make_filter_range(attrs, [&](NamedAttribute attr) {
|
|
|
|
return !llvm::is_contained(elidedAttrs, attr.first.strref());
|
|
|
|
}));
|
2018-09-19 07:36:26 +08:00
|
|
|
|
|
|
|
// If there are no attributes left to print after filtering, then we're done.
|
|
|
|
if (filteredAttrs.empty())
|
|
|
|
return;
|
|
|
|
|
2019-11-06 09:58:16 +08:00
|
|
|
// Print the 'attributes' keyword if necessary.
|
|
|
|
if (withKeyword)
|
2019-11-06 15:39:21 +08:00
|
|
|
os << " attributes";
|
2019-11-06 09:58:16 +08:00
|
|
|
|
2018-09-19 07:36:26 +08:00
|
|
|
// Otherwise, print them all out in braces.
|
|
|
|
os << " {";
|
2020-03-12 04:22:19 +08:00
|
|
|
interleaveComma(filteredAttrs,
|
|
|
|
[&](NamedAttribute attr) { printNamedAttribute(attr); });
|
|
|
|
os << '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModulePrinter::printNamedAttribute(NamedAttribute attr) {
|
|
|
|
if (isBareIdentifier(attr.first)) {
|
2019-04-26 00:56:09 +08:00
|
|
|
os << attr.first;
|
2020-03-12 04:22:19 +08:00
|
|
|
} else {
|
|
|
|
os << '"';
|
|
|
|
printEscapedString(attr.first.strref(), os);
|
|
|
|
os << '"';
|
|
|
|
}
|
2019-04-26 00:56:09 +08:00
|
|
|
|
2020-03-12 04:22:19 +08:00
|
|
|
// Pretty printing elides the attribute value for unit attributes.
|
|
|
|
if (attr.second.isa<UnitAttr>())
|
|
|
|
return;
|
2019-04-26 00:56:09 +08:00
|
|
|
|
2020-03-12 04:22:19 +08:00
|
|
|
os << " = ";
|
|
|
|
printAttribute(attr.second);
|
2018-09-19 07:36:26 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CustomDialectAsmPrinter
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-10 08:42:46 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
namespace {
|
|
|
|
/// This class provides the main specialization of the DialectAsmPrinter that is
|
|
|
|
/// used to provide support for print attributes and types. This hooks allows
|
|
|
|
/// for dialects to hook into the main ModulePrinter.
|
|
|
|
struct CustomDialectAsmPrinter : public DialectAsmPrinter {
|
2018-07-10 08:42:46 +08:00
|
|
|
public:
|
2020-01-10 04:39:26 +08:00
|
|
|
CustomDialectAsmPrinter(ModulePrinter &printer) : printer(printer) {}
|
|
|
|
~CustomDialectAsmPrinter() override {}
|
2019-07-10 01:40:29 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
raw_ostream &getStream() const override { return printer.getStream(); }
|
2018-07-10 08:42:46 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the given attribute to the stream.
|
|
|
|
void printAttribute(Attribute attr) override { printer.printAttribute(attr); }
|
2019-12-20 14:15:31 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the given floating point value in a stablized form.
|
|
|
|
void printFloat(const APFloat &value) override {
|
|
|
|
printFloatValue(value, getStream());
|
2019-12-20 14:15:31 +08:00
|
|
|
}
|
2019-11-21 02:19:01 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the given type to the stream.
|
|
|
|
void printType(Type type) override { printer.printType(type); }
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// The main module printer.
|
|
|
|
ModulePrinter &printer;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void ModulePrinter::printDialectAttribute(Attribute attr) {
|
|
|
|
auto &dialect = attr.getDialect();
|
2019-11-21 02:19:01 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Ask the dialect to serialize the attribute to a string.
|
|
|
|
std::string attrName;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream attrNameStr(attrName);
|
|
|
|
ModulePrinter subPrinter(attrNameStr, printerFlags, state);
|
|
|
|
CustomDialectAsmPrinter printer(subPrinter);
|
|
|
|
dialect.printAttribute(attr, printer);
|
|
|
|
}
|
|
|
|
printDialectSymbol(os, "#", dialect.getNamespace(), attrName);
|
|
|
|
}
|
2019-11-21 02:19:01 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void ModulePrinter::printDialectType(Type type) {
|
|
|
|
auto &dialect = type.getDialect();
|
2018-07-23 06:45:24 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Ask the dialect to serialize the type to a string.
|
|
|
|
std::string typeName;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream typeNameStr(typeName);
|
|
|
|
ModulePrinter subPrinter(typeNameStr, printerFlags, state);
|
|
|
|
CustomDialectAsmPrinter printer(subPrinter);
|
|
|
|
dialect.printType(type, printer);
|
2018-07-23 06:45:24 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
printDialectSymbol(os, "!", dialect.getNamespace(), typeName);
|
|
|
|
}
|
2018-07-28 02:10:12 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Affine expressions and maps
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-10-15 07:21:17 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void ModulePrinter::printAffineExpr(
|
|
|
|
AffineExpr expr, function_ref<void(unsigned, bool)> printValueName) {
|
|
|
|
printAffineExprInternal(expr, BindingStrength::Weak, printValueName);
|
2019-11-21 02:19:01 +08:00
|
|
|
}
|
2019-10-15 07:21:17 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void ModulePrinter::printAffineExprInternal(
|
|
|
|
AffineExpr expr, BindingStrength enclosingTightness,
|
|
|
|
function_ref<void(unsigned, bool)> printValueName) {
|
|
|
|
const char *binopSpelling = nullptr;
|
|
|
|
switch (expr.getKind()) {
|
|
|
|
case AffineExprKind::SymbolId: {
|
|
|
|
unsigned pos = expr.cast<AffineSymbolExpr>().getPosition();
|
|
|
|
if (printValueName)
|
|
|
|
printValueName(pos, /*isSymbol=*/true);
|
|
|
|
else
|
|
|
|
os << 's' << pos;
|
2019-11-21 02:19:01 +08:00
|
|
|
return;
|
2018-07-28 02:10:12 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
case AffineExprKind::DimId: {
|
|
|
|
unsigned pos = expr.cast<AffineDimExpr>().getPosition();
|
|
|
|
if (printValueName)
|
|
|
|
printValueName(pos, /*isSymbol=*/false);
|
|
|
|
else
|
|
|
|
os << 'd' << pos;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case AffineExprKind::Constant:
|
|
|
|
os << expr.cast<AffineConstantExpr>().getValue();
|
|
|
|
return;
|
|
|
|
case AffineExprKind::Add:
|
|
|
|
binopSpelling = " + ";
|
|
|
|
break;
|
|
|
|
case AffineExprKind::Mul:
|
|
|
|
binopSpelling = " * ";
|
|
|
|
break;
|
|
|
|
case AffineExprKind::FloorDiv:
|
|
|
|
binopSpelling = " floordiv ";
|
|
|
|
break;
|
|
|
|
case AffineExprKind::CeilDiv:
|
|
|
|
binopSpelling = " ceildiv ";
|
|
|
|
break;
|
|
|
|
case AffineExprKind::Mod:
|
|
|
|
binopSpelling = " mod ";
|
|
|
|
break;
|
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
auto binOp = expr.cast<AffineBinaryOpExpr>();
|
|
|
|
AffineExpr lhsExpr = binOp.getLHS();
|
|
|
|
AffineExpr rhsExpr = binOp.getRHS();
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Handle tightly binding binary operators.
|
|
|
|
if (binOp.getKind() != AffineExprKind::Add) {
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << '(';
|
|
|
|
|
|
|
|
// Pretty print multiplication with -1.
|
|
|
|
auto rhsConst = rhsExpr.dyn_cast<AffineConstantExpr>();
|
|
|
|
if (rhsConst && binOp.getKind() == AffineExprKind::Mul &&
|
|
|
|
rhsConst.getValue() == -1) {
|
|
|
|
os << "-";
|
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Strong, printValueName);
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
|
|
|
return;
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
2019-07-10 01:40:29 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Strong, printValueName);
|
2018-07-27 09:09:20 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
os << binopSpelling;
|
|
|
|
printAffineExprInternal(rhsExpr, BindingStrength::Strong, printValueName);
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
2018-12-29 03:41:56 +08:00
|
|
|
os << ')';
|
2020-01-10 04:39:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Print out special "pretty" forms for add.
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << '(';
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Pretty print addition to a product that has a negative operand as a
|
|
|
|
// subtraction.
|
|
|
|
if (auto rhs = rhsExpr.dyn_cast<AffineBinaryOpExpr>()) {
|
|
|
|
if (rhs.getKind() == AffineExprKind::Mul) {
|
|
|
|
AffineExpr rrhsExpr = rhs.getRHS();
|
|
|
|
if (auto rrhs = rrhsExpr.dyn_cast<AffineConstantExpr>()) {
|
|
|
|
if (rrhs.getValue() == -1) {
|
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Weak,
|
|
|
|
printValueName);
|
|
|
|
os << " - ";
|
|
|
|
if (rhs.getLHS().getKind() == AffineExprKind::Add) {
|
|
|
|
printAffineExprInternal(rhs.getLHS(), BindingStrength::Strong,
|
|
|
|
printValueName);
|
|
|
|
} else {
|
|
|
|
printAffineExprInternal(rhs.getLHS(), BindingStrength::Weak,
|
|
|
|
printValueName);
|
|
|
|
}
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rrhs.getValue() < -1) {
|
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Weak,
|
|
|
|
printValueName);
|
|
|
|
os << " - ";
|
|
|
|
printAffineExprInternal(rhs.getLHS(), BindingStrength::Strong,
|
|
|
|
printValueName);
|
|
|
|
os << " * " << -rrhs.getValue();
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Pretty print addition to a negative number as a subtraction.
|
|
|
|
if (auto rhsConst = rhsExpr.dyn_cast<AffineConstantExpr>()) {
|
|
|
|
if (rhsConst.getValue() < 0) {
|
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Weak, printValueName);
|
|
|
|
os << " - " << -rhsConst.getValue();
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
|
|
|
return;
|
|
|
|
}
|
2018-07-15 15:06:54 +08:00
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Weak, printValueName);
|
|
|
|
|
|
|
|
os << " + ";
|
|
|
|
printAffineExprInternal(rhsExpr, BindingStrength::Weak, printValueName);
|
|
|
|
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
2018-07-14 04:03:13 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void ModulePrinter::printAffineConstraint(AffineExpr expr, bool isEq) {
|
|
|
|
printAffineExprInternal(expr, BindingStrength::Weak);
|
|
|
|
isEq ? os << " == 0" : os << " >= 0";
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
2018-07-04 08:51:28 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void ModulePrinter::printAffineMap(AffineMap map) {
|
|
|
|
// Dimension identifiers.
|
|
|
|
os << '(';
|
|
|
|
for (int i = 0; i < (int)map.getNumDims() - 1; ++i)
|
|
|
|
os << 'd' << i << ", ";
|
|
|
|
if (map.getNumDims() >= 1)
|
|
|
|
os << 'd' << map.getNumDims() - 1;
|
|
|
|
os << ')';
|
2019-11-21 02:19:01 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Symbolic identifiers.
|
|
|
|
if (map.getNumSymbols() != 0) {
|
|
|
|
os << '[';
|
|
|
|
for (unsigned i = 0; i < map.getNumSymbols() - 1; ++i)
|
|
|
|
os << 's' << i << ", ";
|
|
|
|
if (map.getNumSymbols() >= 1)
|
|
|
|
os << 's' << map.getNumSymbols() - 1;
|
|
|
|
os << ']';
|
2019-11-21 02:19:01 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Result affine expressions.
|
|
|
|
os << " -> (";
|
|
|
|
interleaveComma(map.getResults(),
|
|
|
|
[&](AffineExpr expr) { printAffineExpr(expr); });
|
|
|
|
os << ')';
|
|
|
|
}
|
2019-11-21 02:19:01 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void ModulePrinter::printIntegerSet(IntegerSet set) {
|
|
|
|
// Dimension identifiers.
|
|
|
|
os << '(';
|
|
|
|
for (unsigned i = 1; i < set.getNumDims(); ++i)
|
|
|
|
os << 'd' << i - 1 << ", ";
|
|
|
|
if (set.getNumDims() >= 1)
|
|
|
|
os << 'd' << set.getNumDims() - 1;
|
|
|
|
os << ')';
|
|
|
|
|
|
|
|
// Symbolic identifiers.
|
|
|
|
if (set.getNumSymbols() != 0) {
|
|
|
|
os << '[';
|
|
|
|
for (unsigned i = 0; i < set.getNumSymbols() - 1; ++i)
|
|
|
|
os << 's' << i << ", ";
|
|
|
|
if (set.getNumSymbols() >= 1)
|
|
|
|
os << 's' << set.getNumSymbols() - 1;
|
|
|
|
os << ']';
|
2019-11-21 02:19:01 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// Print constraints.
|
|
|
|
os << " : (";
|
|
|
|
int numConstraints = set.getNumConstraints();
|
|
|
|
for (int i = 1; i < numConstraints; ++i) {
|
|
|
|
printAffineConstraint(set.getConstraint(i - 1), set.isEq(i - 1));
|
|
|
|
os << ", ";
|
|
|
|
}
|
|
|
|
if (numConstraints >= 1)
|
|
|
|
printAffineConstraint(set.getConstraint(numConstraints - 1),
|
|
|
|
set.isEq(numConstraints - 1));
|
|
|
|
os << ')';
|
2019-11-21 02:19:01 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// OperationPrinter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// This class contains the logic for printing operations, regions, and blocks.
|
|
|
|
class OperationPrinter : public ModulePrinter, private OpAsmPrinter {
|
|
|
|
public:
|
2020-01-15 07:23:05 +08:00
|
|
|
explicit OperationPrinter(raw_ostream &os, OpPrintingFlags flags,
|
|
|
|
AsmStateImpl &state)
|
|
|
|
: ModulePrinter(os, flags, &state) {}
|
2019-08-30 03:14:02 +08:00
|
|
|
|
2020-01-15 07:23:05 +08:00
|
|
|
/// Print the given top-level module.
|
|
|
|
void print(ModuleOp op);
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the given operation with its indent and location.
|
|
|
|
void print(Operation *op);
|
|
|
|
/// Print the bare location, not including indentation/location/etc.
|
|
|
|
void printOperation(Operation *op);
|
|
|
|
/// Print the given operation in the generic form.
|
|
|
|
void printGenericOp(Operation *op) override;
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the name of the given block.
|
|
|
|
void printBlockName(Block *block);
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the given block. If 'printBlockArgs' is false, the arguments of the
|
|
|
|
/// block are not printed. If 'printBlockTerminator' is false, the terminator
|
|
|
|
/// operation of the block is not printed.
|
|
|
|
void print(Block *block, bool printBlockArgs = true,
|
|
|
|
bool printBlockTerminator = true);
|
|
|
|
|
|
|
|
/// Print the ID of the given value, optionally with its result number.
|
Add support for custom op parser/printer hooks to know about result names.
Summary:
This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:
- Adds a new 'getResultName' method to OpAsmParser that allows a parser
implementation to get information about its result names, along with
a getNumResults() method that allows op parser impls to know how many
results are expected.
- Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
- Adds a test.string_attr_pretty_name operation that uses these hooks to
do fancy things with the result name.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76205
2020-03-16 08:13:59 +08:00
|
|
|
void printValueID(Value value, bool printResultNo = true,
|
|
|
|
raw_ostream *streamOverride = nullptr) const;
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// OpAsmPrinter methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Return the current stream of the printer.
|
|
|
|
raw_ostream &getStream() const override { return os; }
|
|
|
|
|
|
|
|
/// Print the given type.
|
|
|
|
void printType(Type type) override { ModulePrinter::printType(type); }
|
|
|
|
|
|
|
|
/// Print the given attribute.
|
|
|
|
void printAttribute(Attribute attr) override {
|
|
|
|
ModulePrinter::printAttribute(attr);
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 02:01:17 +08:00
|
|
|
/// Print the given attribute without its type. The corresponding parser must
|
|
|
|
/// provide a valid type for the attribute.
|
|
|
|
void printAttributeWithoutType(Attribute attr) override {
|
|
|
|
ModulePrinter::printAttribute(attr, AttrTypeElision::Must);
|
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the ID for the given value.
|
|
|
|
void printOperand(Value value) override { printValueID(value); }
|
Add support for custom op parser/printer hooks to know about result names.
Summary:
This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:
- Adds a new 'getResultName' method to OpAsmParser that allows a parser
implementation to get information about its result names, along with
a getNumResults() method that allows op parser impls to know how many
results are expected.
- Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
- Adds a test.string_attr_pretty_name operation that uses these hooks to
do fancy things with the result name.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76205
2020-03-16 08:13:59 +08:00
|
|
|
void printOperand(Value value, raw_ostream &os) override {
|
|
|
|
printValueID(value, /*printResultNo=*/true, &os);
|
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
/// Print an optional attribute dictionary with a given set of elided values.
|
|
|
|
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
|
|
|
ArrayRef<StringRef> elidedAttrs = {}) override {
|
|
|
|
ModulePrinter::printOptionalAttrDict(attrs, elidedAttrs);
|
|
|
|
}
|
|
|
|
void printOptionalAttrDictWithKeyword(
|
|
|
|
ArrayRef<NamedAttribute> attrs,
|
|
|
|
ArrayRef<StringRef> elidedAttrs = {}) override {
|
|
|
|
ModulePrinter::printOptionalAttrDict(attrs, elidedAttrs,
|
|
|
|
/*withKeyword=*/true);
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
|
2020-03-06 04:48:28 +08:00
|
|
|
/// Print the given successor.
|
|
|
|
void printSuccessor(Block *successor) override;
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print an operation successor with the operands used for the block
|
|
|
|
/// arguments.
|
2020-03-06 04:48:28 +08:00
|
|
|
void printSuccessorAndUseList(Block *successor,
|
|
|
|
ValueRange succOperands) override;
|
2019-08-24 01:35:24 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the given region.
|
|
|
|
void printRegion(Region ®ion, bool printEntryBlockArgs,
|
|
|
|
bool printBlockTerminators) override;
|
2019-08-24 01:35:24 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Renumber the arguments for the specified region to the same names as the
|
|
|
|
/// SSA values in namesToUse. This may only be used for IsolatedFromAbove
|
|
|
|
/// operations. If any entry in namesToUse is null, the corresponding
|
|
|
|
/// argument name is left alone.
|
|
|
|
void shadowRegionArgs(Region ®ion, ValueRange namesToUse) override {
|
|
|
|
state->getSSANameState().shadowRegionArgs(region, namesToUse);
|
|
|
|
}
|
2019-08-24 01:35:24 +08:00
|
|
|
|
[mlir] NFC: fix trivial typo in source files
Summary: fix trivial typos in the source files
Reviewers: mravishankar, antiagainst, nicolasvasilache, herhut, rriddle, aartbik
Reviewed By: antiagainst, rriddle
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, csigg, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, bader, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76876
2020-03-27 02:51:37 +08:00
|
|
|
/// Print the given affine map with the symbol and dimension operands printed
|
2020-01-10 04:39:26 +08:00
|
|
|
/// inline with the map.
|
|
|
|
void printAffineMapOfSSAIds(AffineMapAttr mapAttr,
|
|
|
|
ValueRange operands) override;
|
2019-08-24 01:35:24 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
/// Print the given string as a symbol reference.
|
|
|
|
void printSymbolName(StringRef symbolRef) override {
|
|
|
|
::printSymbolReference(symbolRef, os);
|
|
|
|
}
|
2019-08-24 01:35:24 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
private:
|
|
|
|
/// The number of spaces used for indenting nested operations.
|
|
|
|
const static unsigned indentWidth = 2;
|
2019-08-24 01:35:24 +08:00
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
// This is the current indentation level for nested structures.
|
|
|
|
unsigned currentIndent = 0;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2019-08-24 01:35:24 +08:00
|
|
|
|
2020-01-15 07:23:05 +08:00
|
|
|
void OperationPrinter::print(ModuleOp op) {
|
|
|
|
// Output the aliases at the top level.
|
2020-02-09 07:01:34 +08:00
|
|
|
state->getAliasState().printAttributeAliases(os, newLine);
|
|
|
|
state->getAliasState().printTypeAliases(os, newLine);
|
2020-01-15 07:23:05 +08:00
|
|
|
|
|
|
|
// Print the module.
|
|
|
|
print(op.getOperation());
|
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void OperationPrinter::print(Operation *op) {
|
2020-02-09 07:01:34 +08:00
|
|
|
// Track the location of this operation.
|
|
|
|
state->registerOperationLocation(op, newLine.curLine, currentIndent);
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
os.indent(currentIndent);
|
|
|
|
printOperation(op);
|
|
|
|
printTrailingLocation(op->getLoc());
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void OperationPrinter::printOperation(Operation *op) {
|
2019-03-29 05:58:52 +08:00
|
|
|
if (size_t numResults = op->getNumResults()) {
|
2019-11-21 02:19:01 +08:00
|
|
|
auto printResultGroup = [&](size_t resultNo, size_t resultCount) {
|
|
|
|
printValueID(op->getResult(resultNo), /*printResultNo=*/false);
|
|
|
|
if (resultCount > 1)
|
|
|
|
os << ':' << resultCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check to see if this operation has multiple result groups.
|
2020-01-10 04:39:26 +08:00
|
|
|
ArrayRef<int> resultGroups = state->getSSANameState().getOpResultGroups(op);
|
|
|
|
if (!resultGroups.empty()) {
|
2019-11-21 02:19:01 +08:00
|
|
|
// Interleave the groups excluding the last one, this one will be handled
|
|
|
|
// separately.
|
|
|
|
interleaveComma(llvm::seq<int>(0, resultGroups.size() - 1), [&](int i) {
|
|
|
|
printResultGroup(resultGroups[i],
|
|
|
|
resultGroups[i + 1] - resultGroups[i]);
|
|
|
|
});
|
|
|
|
os << ", ";
|
|
|
|
printResultGroup(resultGroups.back(), numResults - resultGroups.back());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
printResultGroup(/*resultNo=*/0, /*resultCount=*/numResults);
|
|
|
|
}
|
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
os << " = ";
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:09:39 +08:00
|
|
|
// If requested, always print the generic form.
|
|
|
|
if (!printerFlags.shouldPrintGenericOpForm()) {
|
|
|
|
// Check to see if this is a known operation. If so, use the registered
|
|
|
|
// custom printer hook.
|
|
|
|
if (auto *opInfo = op->getAbstractOperation()) {
|
|
|
|
opInfo->printAssembly(op, *this);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
|
2019-01-24 03:26:56 +08:00
|
|
|
// Otherwise print with the generic assembly form.
|
|
|
|
printGenericOp(op);
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void OperationPrinter::printGenericOp(Operation *op) {
|
2018-12-29 03:41:56 +08:00
|
|
|
os << '"';
|
|
|
|
printEscapedString(op->getName().getStringRef(), os);
|
|
|
|
os << "\"(";
|
2020-03-06 04:48:28 +08:00
|
|
|
interleaveComma(op->getOperands(), [&](Value value) { printValueID(value); });
|
2018-12-29 03:41:56 +08:00
|
|
|
os << ')';
|
2019-01-10 04:28:30 +08:00
|
|
|
|
|
|
|
// For terminators, print the list of successors and their operands.
|
2020-03-06 04:48:28 +08:00
|
|
|
if (op->getNumSuccessors() != 0) {
|
2019-01-10 04:28:30 +08:00
|
|
|
os << '[';
|
2020-03-06 04:48:28 +08:00
|
|
|
interleaveComma(op->getSuccessors(),
|
|
|
|
[&](Block *successor) { printBlockName(successor); });
|
2019-01-10 04:28:30 +08:00
|
|
|
os << ']';
|
|
|
|
}
|
|
|
|
|
2019-05-06 16:40:13 +08:00
|
|
|
// Print regions.
|
|
|
|
if (op->getNumRegions() != 0) {
|
|
|
|
os << " (";
|
|
|
|
interleaveComma(op->getRegions(), [&](Region ®ion) {
|
|
|
|
printRegion(region, /*printEntryBlockArgs=*/true,
|
|
|
|
/*printBlockTerminators=*/true);
|
|
|
|
});
|
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
auto attrs = op->getAttrs();
|
|
|
|
printOptionalAttrDict(attrs);
|
|
|
|
|
|
|
|
// Print the type signature of the operation.
|
2019-06-18 06:31:53 +08:00
|
|
|
os << " : ";
|
|
|
|
printFunctionalType(op);
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void OperationPrinter::printBlockName(Block *block) {
|
|
|
|
auto id = state->getSSANameState().getBlockID(block);
|
|
|
|
if (id != SSANameState::NameSentinel)
|
|
|
|
os << "^bb" << id;
|
|
|
|
else
|
|
|
|
os << "^INVALIDBLOCK";
|
|
|
|
}
|
|
|
|
|
|
|
|
void OperationPrinter::print(Block *block, bool printBlockArgs,
|
|
|
|
bool printBlockTerminator) {
|
|
|
|
// Print the block label and argument list if requested.
|
|
|
|
if (printBlockArgs) {
|
|
|
|
os.indent(currentIndent);
|
|
|
|
printBlockName(block);
|
|
|
|
|
|
|
|
// Print the argument list if non-empty.
|
|
|
|
if (!block->args_empty()) {
|
|
|
|
os << '(';
|
|
|
|
interleaveComma(block->getArguments(), [&](BlockArgument arg) {
|
|
|
|
printValueID(arg);
|
|
|
|
os << ": ";
|
2020-01-12 00:54:04 +08:00
|
|
|
printType(arg.getType());
|
2020-01-10 04:39:26 +08:00
|
|
|
});
|
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
os << ':';
|
|
|
|
|
|
|
|
// Print out some context information about the predecessors of this block.
|
|
|
|
if (!block->getParent()) {
|
2020-05-05 09:30:48 +08:00
|
|
|
os << " // block is not in a region!";
|
2020-01-10 04:39:26 +08:00
|
|
|
} else if (block->hasNoPredecessors()) {
|
2020-05-05 09:30:48 +08:00
|
|
|
os << " // no predecessors";
|
2020-01-10 04:39:26 +08:00
|
|
|
} else if (auto *pred = block->getSinglePredecessor()) {
|
2020-05-05 09:30:48 +08:00
|
|
|
os << " // pred: ";
|
2020-01-10 04:39:26 +08:00
|
|
|
printBlockName(pred);
|
|
|
|
} else {
|
|
|
|
// We want to print the predecessors in increasing numeric order, not in
|
|
|
|
// whatever order the use-list is in, so gather and sort them.
|
|
|
|
SmallVector<std::pair<unsigned, Block *>, 4> predIDs;
|
|
|
|
for (auto *pred : block->getPredecessors())
|
|
|
|
predIDs.push_back({state->getSSANameState().getBlockID(pred), pred});
|
|
|
|
llvm::array_pod_sort(predIDs.begin(), predIDs.end());
|
|
|
|
|
2020-05-05 09:30:48 +08:00
|
|
|
os << " // " << predIDs.size() << " preds: ";
|
2020-01-10 04:39:26 +08:00
|
|
|
|
|
|
|
interleaveComma(predIDs, [&](std::pair<unsigned, Block *> pred) {
|
|
|
|
printBlockName(pred.second);
|
|
|
|
});
|
|
|
|
}
|
2020-02-09 07:01:34 +08:00
|
|
|
os << newLine;
|
2020-01-10 04:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
currentIndent += indentWidth;
|
|
|
|
auto range = llvm::make_range(
|
|
|
|
block->getOperations().begin(),
|
|
|
|
std::prev(block->getOperations().end(), printBlockTerminator ? 0 : 1));
|
|
|
|
for (auto &op : range) {
|
|
|
|
print(&op);
|
2020-02-09 07:01:34 +08:00
|
|
|
os << newLine;
|
2020-01-10 04:39:26 +08:00
|
|
|
}
|
|
|
|
currentIndent -= indentWidth;
|
|
|
|
}
|
|
|
|
|
Add support for custom op parser/printer hooks to know about result names.
Summary:
This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:
- Adds a new 'getResultName' method to OpAsmParser that allows a parser
implementation to get information about its result names, along with
a getNumResults() method that allows op parser impls to know how many
results are expected.
- Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
- Adds a test.string_attr_pretty_name operation that uses these hooks to
do fancy things with the result name.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76205
2020-03-16 08:13:59 +08:00
|
|
|
void OperationPrinter::printValueID(Value value, bool printResultNo,
|
|
|
|
raw_ostream *streamOverride) const {
|
|
|
|
state->getSSANameState().printValueID(value, printResultNo,
|
|
|
|
streamOverride ? *streamOverride : os);
|
2020-01-10 04:39:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-06 04:48:28 +08:00
|
|
|
void OperationPrinter::printSuccessor(Block *successor) {
|
|
|
|
printBlockName(successor);
|
|
|
|
}
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-03-06 04:48:28 +08:00
|
|
|
void OperationPrinter::printSuccessorAndUseList(Block *successor,
|
|
|
|
ValueRange succOperands) {
|
|
|
|
printBlockName(successor);
|
|
|
|
if (succOperands.empty())
|
2018-12-29 03:41:56 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
os << '(';
|
|
|
|
interleaveComma(succOperands,
|
2019-12-24 06:45:01 +08:00
|
|
|
[this](Value operand) { printValueID(operand); });
|
2018-12-29 03:41:56 +08:00
|
|
|
os << " : ";
|
2019-03-24 06:09:06 +08:00
|
|
|
interleaveComma(succOperands,
|
2020-01-12 00:54:04 +08:00
|
|
|
[this](Value operand) { printType(operand.getType()); });
|
2018-12-29 03:41:56 +08:00
|
|
|
os << ')';
|
2018-07-04 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
2020-01-10 04:39:26 +08:00
|
|
|
void OperationPrinter::printRegion(Region ®ion, bool printEntryBlockArgs,
|
|
|
|
bool printBlockTerminators) {
|
2020-02-09 07:01:34 +08:00
|
|
|
os << " {" << newLine;
|
2020-01-10 04:39:26 +08:00
|
|
|
if (!region.empty()) {
|
|
|
|
auto *entryBlock = ®ion.front();
|
|
|
|
print(entryBlock, printEntryBlockArgs && entryBlock->getNumArguments() != 0,
|
|
|
|
printBlockTerminators);
|
|
|
|
for (auto &b : llvm::drop_begin(region.getBlocks(), 1))
|
|
|
|
print(&b);
|
|
|
|
}
|
|
|
|
os.indent(currentIndent) << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
void OperationPrinter::printAffineMapOfSSAIds(AffineMapAttr mapAttr,
|
|
|
|
ValueRange operands) {
|
|
|
|
AffineMap map = mapAttr.getValue();
|
|
|
|
unsigned numDims = map.getNumDims();
|
|
|
|
auto printValueName = [&](unsigned pos, bool isSymbol) {
|
|
|
|
unsigned index = isSymbol ? numDims + pos : pos;
|
|
|
|
assert(index < operands.size());
|
|
|
|
if (isSymbol)
|
|
|
|
os << "symbol(";
|
|
|
|
printValueID(operands[index]);
|
|
|
|
if (isSymbol)
|
|
|
|
os << ')';
|
|
|
|
};
|
|
|
|
|
|
|
|
interleaveComma(map.getResults(), [&](AffineExpr expr) {
|
|
|
|
printAffineExpr(expr, printValueName);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-04 08:51:28 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// print and dump methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-29 11:45:33 +08:00
|
|
|
|
2018-07-19 07:29:21 +08:00
|
|
|
void Attribute::print(raw_ostream &os) const {
|
2019-08-22 03:16:23 +08:00
|
|
|
ModulePrinter(os).printAttribute(*this);
|
2018-07-19 07:29:21 +08:00
|
|
|
}
|
|
|
|
|
2019-04-17 07:47:41 +08:00
|
|
|
void Attribute::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
2018-07-19 07:29:21 +08:00
|
|
|
|
2019-08-22 03:16:23 +08:00
|
|
|
void Type::print(raw_ostream &os) { ModulePrinter(os).printType(*this); }
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2019-05-15 22:54:28 +08:00
|
|
|
void Type::dump() { print(llvm::errs()); }
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
void AffineMap::dump() const {
|
2018-07-17 00:45:22 +08:00
|
|
|
print(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
2018-07-10 00:00:25 +08:00
|
|
|
|
2018-10-11 00:45:59 +08:00
|
|
|
void IntegerSet::dump() const {
|
2018-08-08 05:24:38 +08:00
|
|
|
print(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
|
|
|
|
2018-10-10 01:59:27 +08:00
|
|
|
void AffineExpr::print(raw_ostream &os) const {
|
2020-04-06 03:13:07 +08:00
|
|
|
if (!expr) {
|
|
|
|
os << "<<NULL AFFINE EXPR>>";
|
2019-01-08 09:34:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-08-22 03:16:23 +08:00
|
|
|
ModulePrinter(os).printAffineExpr(*this);
|
2018-10-10 01:59:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AffineExpr::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
void AffineMap::print(raw_ostream &os) const {
|
2020-04-06 03:13:07 +08:00
|
|
|
if (!map) {
|
|
|
|
os << "<<NULL AFFINE MAP>>";
|
2019-01-08 09:34:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-08-22 03:16:23 +08:00
|
|
|
ModulePrinter(os).printAffineMap(*this);
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
2018-07-12 12:31:07 +08:00
|
|
|
|
2018-10-11 00:45:59 +08:00
|
|
|
void IntegerSet::print(raw_ostream &os) const {
|
2019-08-22 03:16:23 +08:00
|
|
|
ModulePrinter(os).printIntegerSet(*this);
|
2018-08-08 05:24:38 +08:00
|
|
|
}
|
|
|
|
|
2019-03-24 06:09:06 +08:00
|
|
|
void Value::print(raw_ostream &os) {
|
2019-10-15 07:21:17 +08:00
|
|
|
if (auto *op = getDefiningOp())
|
2019-10-15 21:50:34 +08:00
|
|
|
return op->print(os);
|
2019-10-15 07:21:17 +08:00
|
|
|
// TODO: Improve this.
|
2019-12-23 13:59:55 +08:00
|
|
|
assert(isa<BlockArgument>());
|
2019-10-15 07:21:17 +08:00
|
|
|
os << "<block argument>\n";
|
2018-08-03 08:16:58 +08:00
|
|
|
}
|
2020-01-15 07:23:05 +08:00
|
|
|
void Value::print(raw_ostream &os, AsmState &state) {
|
|
|
|
if (auto *op = getDefiningOp())
|
|
|
|
return op->print(os, state);
|
|
|
|
|
|
|
|
// TODO: Improve this.
|
|
|
|
assert(isa<BlockArgument>());
|
|
|
|
os << "<block argument>\n";
|
|
|
|
}
|
2018-08-03 08:16:58 +08:00
|
|
|
|
2019-09-28 07:20:04 +08:00
|
|
|
void Value::dump() {
|
|
|
|
print(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
2018-08-03 08:16:58 +08:00
|
|
|
|
2020-01-15 07:23:05 +08:00
|
|
|
void Value::printAsOperand(raw_ostream &os, AsmState &state) {
|
|
|
|
// TODO(riverriddle) This doesn't necessarily capture all potential cases.
|
|
|
|
// Currently, region arguments can be shadowed when printing the main
|
|
|
|
// operation. If the IR hasn't been printed, this will produce the old SSA
|
|
|
|
// name and not the shadowed name.
|
|
|
|
state.getImpl().getSSANameState().printValueID(*this, /*printResultNo=*/true,
|
|
|
|
os);
|
|
|
|
}
|
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
void Operation::print(raw_ostream &os, OpPrintingFlags flags) {
|
2020-04-07 02:04:22 +08:00
|
|
|
// Find the operation to number from based upon the provided flags.
|
|
|
|
Operation *printedOp = this;
|
|
|
|
bool shouldUseLocalScope = flags.shouldUseLocalScope();
|
|
|
|
do {
|
|
|
|
// If we are printing local scope, stop at the first operation that is
|
|
|
|
// isolated from above.
|
|
|
|
if (shouldUseLocalScope && printedOp->isKnownIsolatedFromAbove())
|
|
|
|
break;
|
2019-07-04 04:21:24 +08:00
|
|
|
|
2020-04-07 02:04:22 +08:00
|
|
|
// Otherwise, traverse up to the next parent.
|
|
|
|
Operation *parentOp = printedOp->getParentOp();
|
|
|
|
if (!parentOp)
|
|
|
|
break;
|
|
|
|
printedOp = parentOp;
|
|
|
|
} while (true);
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2020-04-07 02:04:22 +08:00
|
|
|
AsmState state(printedOp);
|
2020-01-15 07:23:05 +08:00
|
|
|
print(os, state, flags);
|
|
|
|
}
|
|
|
|
void Operation::print(raw_ostream &os, AsmState &state, OpPrintingFlags flags) {
|
|
|
|
OperationPrinter(os, flags, state.getImpl()).print(this);
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
2018-07-12 12:31:07 +08:00
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
void Operation::dump() {
|
2019-11-13 01:36:40 +08:00
|
|
|
print(llvm::errs(), OpPrintingFlags().useLocalScope());
|
2018-07-21 00:35:47 +08:00
|
|
|
llvm::errs() << "\n";
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-22 08:53:00 +08:00
|
|
|
void Block::print(raw_ostream &os) {
|
2020-01-10 04:39:26 +08:00
|
|
|
Operation *parentOp = getParentOp();
|
|
|
|
if (!parentOp) {
|
2018-08-04 02:12:34 +08:00
|
|
|
os << "<<UNLINKED BLOCK>>\n";
|
|
|
|
return;
|
|
|
|
}
|
2020-01-10 04:39:26 +08:00
|
|
|
// Get the top-level op.
|
|
|
|
while (auto *nextOp = parentOp->getParentOp())
|
|
|
|
parentOp = nextOp;
|
2018-12-28 03:07:34 +08:00
|
|
|
|
2020-01-15 07:23:05 +08:00
|
|
|
AsmState state(parentOp);
|
|
|
|
print(os, state);
|
|
|
|
}
|
|
|
|
void Block::print(raw_ostream &os, AsmState &state) {
|
|
|
|
OperationPrinter(os, /*flags=*/llvm::None, state.getImpl()).print(this);
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2019-03-22 08:53:00 +08:00
|
|
|
void Block::dump() { print(llvm::errs()); }
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-12-29 13:24:30 +08:00
|
|
|
/// Print out the name of the block without printing its body.
|
2018-12-29 05:07:39 +08:00
|
|
|
void Block::printAsOperand(raw_ostream &os, bool printType) {
|
2020-01-10 04:39:26 +08:00
|
|
|
Operation *parentOp = getParentOp();
|
|
|
|
if (!parentOp) {
|
2018-09-22 05:40:36 +08:00
|
|
|
os << "<<UNLINKED BLOCK>>\n";
|
|
|
|
return;
|
|
|
|
}
|
2020-01-15 07:23:05 +08:00
|
|
|
AsmState state(parentOp);
|
|
|
|
printAsOperand(os, state);
|
|
|
|
}
|
|
|
|
void Block::printAsOperand(raw_ostream &os, AsmState &state) {
|
|
|
|
OperationPrinter printer(os, /*flags=*/llvm::None, state.getImpl());
|
|
|
|
printer.printBlockName(this);
|
2019-07-04 04:21:24 +08:00
|
|
|
}
|
2018-07-19 01:16:05 +08:00
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
void ModuleOp::print(raw_ostream &os, OpPrintingFlags flags) {
|
2020-01-15 07:23:05 +08:00
|
|
|
AsmState state(*this);
|
2020-01-10 04:39:26 +08:00
|
|
|
|
2020-01-09 02:11:56 +08:00
|
|
|
// Don't populate aliases when printing at local scope.
|
2019-11-14 06:21:16 +08:00
|
|
|
if (!flags.shouldUseLocalScope())
|
2020-01-15 07:23:05 +08:00
|
|
|
state.getImpl().initializeAliases(*this);
|
|
|
|
print(os, state, flags);
|
|
|
|
}
|
|
|
|
void ModuleOp::print(raw_ostream &os, AsmState &state, OpPrintingFlags flags) {
|
|
|
|
OperationPrinter(os, flags, state.getImpl()).print(*this);
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void ModuleOp::dump() { print(llvm::errs()); }
|