2018-06-24 07:03:42 +08:00
|
|
|
//===- AsmPrinter.cpp - MLIR Assembly Printer Implementation --------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// 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"
|
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-06-24 07:03:42 +08:00
|
|
|
#include "mlir/Support/STLExtras.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"
|
2018-06-24 07:03:42 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
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() {}
|
|
|
|
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-10-08 08:18:54 +08:00
|
|
|
static llvm::cl::opt<unsigned> elideElementsAttrIfLarger(
|
|
|
|
"mlir-elide-elementsattrs-if-larger",
|
|
|
|
llvm::cl::desc("Elide ElementsAttrs with \"...\" that have "
|
2019-10-09 01:03:54 +08:00
|
|
|
"more elements than the given upper limit"));
|
2019-10-08 08:18:54 +08:00
|
|
|
|
2019-01-24 05:11:23 +08:00
|
|
|
static llvm::cl::opt<bool>
|
2019-10-08 04:54:16 +08:00
|
|
|
printDebugInfoOpt("mlir-print-debuginfo",
|
|
|
|
llvm::cl::desc("Print debug info in MLIR output"),
|
|
|
|
llvm::cl::init(false));
|
2019-01-24 05:11:23 +08:00
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
static llvm::cl::opt<bool> printPrettyDebugInfoOpt(
|
2019-01-26 12:37:33 +08:00
|
|
|
"mlir-pretty-debuginfo",
|
|
|
|
llvm::cl::desc("Print pretty debug info in MLIR output"),
|
|
|
|
llvm::cl::init(false));
|
|
|
|
|
2019-07-10 01:40:29 +08:00
|
|
|
// Use the generic op output form in the operation printer even if the custom
|
2019-02-05 05:01:06 +08:00
|
|
|
// form is defined.
|
|
|
|
static llvm::cl::opt<bool>
|
2019-10-08 04:54:16 +08:00
|
|
|
printGenericOpFormOpt("mlir-print-op-generic",
|
|
|
|
llvm::cl::desc("Print the generic op form"),
|
|
|
|
llvm::cl::init(false), llvm::cl::Hidden);
|
|
|
|
|
|
|
|
/// Initialize the printing flags with default supplied by the cl::opts above.
|
|
|
|
OpPrintingFlags::OpPrintingFlags()
|
2019-10-08 08:18:54 +08:00
|
|
|
: elementsAttrElementLimit(
|
|
|
|
elideElementsAttrIfLarger.getNumOccurrences()
|
|
|
|
? Optional<int64_t>(elideElementsAttrIfLarger)
|
|
|
|
: Optional<int64_t>()),
|
|
|
|
printDebugInfoFlag(printDebugInfoOpt),
|
2019-10-08 04:54:16 +08:00
|
|
|
printDebugInfoPrettyFormFlag(printPrettyDebugInfoOpt),
|
|
|
|
printGenericOpFormFlag(printGenericOpFormOpt) {}
|
|
|
|
|
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-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());
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ModuleState
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-02-05 05:01:06 +08:00
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
namespace {
|
2019-05-10 03:08:13 +08:00
|
|
|
/// A special index constant used for non-kind attribute aliases.
|
|
|
|
static constexpr int kNonAttrKindAlias = -1;
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
class ModuleState {
|
|
|
|
public:
|
2019-08-22 03:16:23 +08:00
|
|
|
explicit ModuleState(MLIRContext *context) : interfaces(context) {}
|
2019-07-04 04:21:24 +08:00
|
|
|
void initialize(Operation *op);
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
Twine getAttributeAlias(Attribute attr) const {
|
|
|
|
auto alias = attrToAlias.find(attr);
|
|
|
|
if (alias == attrToAlias.end())
|
|
|
|
return Twine();
|
|
|
|
|
|
|
|
// 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 == kNonAttrKindAlias ? Twine() : Twine(kindIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
void printAttributeAliases(raw_ostream &os) const {
|
|
|
|
auto printAlias = [&](StringRef alias, Attribute attr, int index) {
|
|
|
|
os << '#' << alias;
|
|
|
|
if (index != kNonAttrKindAlias)
|
|
|
|
os << index;
|
|
|
|
os << " = " << attr << '\n';
|
|
|
|
};
|
|
|
|
|
|
|
|
// Print all of the attribute kind aliases.
|
|
|
|
for (auto &kindAlias : attrKindToAlias) {
|
|
|
|
for (unsigned i = 0, e = kindAlias.second.second.size(); i != e; ++i)
|
|
|
|
printAlias(kindAlias.second.first, kindAlias.second.second[i], i);
|
|
|
|
os << "\n";
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2019-05-07 01:36:32 +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 == kNonAttrKindAlias)
|
|
|
|
printAlias(alias->second.first, attr, alias->second.second);
|
2018-08-08 05:24:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 14:08:39 +08:00
|
|
|
StringRef getTypeAlias(Type ty) const { return typeToAlias.lookup(ty); }
|
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
void printTypeAliases(raw_ostream &os) const {
|
|
|
|
for (Type type : usedTypes) {
|
|
|
|
auto alias = typeToAlias.find(type);
|
|
|
|
if (alias != typeToAlias.end())
|
|
|
|
os << '!' << alias->second << " = type " << type << '\n';
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 07:50:30 +08:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
private:
|
|
|
|
void 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);
|
2018-08-08 05:24:38 +08:00
|
|
|
}
|
|
|
|
|
2019-01-11 14:08:39 +08:00
|
|
|
void recordTypeReference(Type ty) { usedTypes.insert(ty); }
|
|
|
|
|
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-01-11 14:08:39 +08:00
|
|
|
// Initialize symbol aliases.
|
|
|
|
void initializeSymbolAliases();
|
|
|
|
|
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;
|
2019-08-22 00:41:37 +08:00
|
|
|
|
|
|
|
/// Collection of OpAsm interfaces implemented in the context.
|
|
|
|
DialectInterfaceCollection<OpAsmDialectInterface> interfaces;
|
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-03-28 23:24:38 +08:00
|
|
|
// TODO Support visiting other types/operations when implemented.
|
2018-10-31 05:59:22 +08:00
|
|
|
void ModuleState::visitType(Type type) {
|
2019-01-11 14:08:39 +08:00
|
|
|
recordTypeReference(type);
|
2018-10-31 05:59:22 +08:00
|
|
|
if (auto funcType = type.dyn_cast<FunctionType>()) {
|
2018-07-18 07:56:54 +08:00
|
|
|
// Visit input and result types for functions.
|
2018-10-31 05:59:22 +08:00
|
|
|
for (auto input : funcType.getInputs())
|
2018-07-18 07:56:54 +08:00
|
|
|
visitType(input);
|
2018-10-31 05:59:22 +08:00
|
|
|
for (auto result : funcType.getResults())
|
2018-07-18 07:56:54 +08:00
|
|
|
visitType(result);
|
2019-05-18 06:23:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auto memref = type.dyn_cast<MemRefType>()) {
|
2018-07-18 07:56:54 +08:00
|
|
|
// Visit affine maps in memref type.
|
2019-05-07 01:36:32 +08:00
|
|
|
for (auto map : memref.getAffineMaps())
|
|
|
|
recordAttributeReference(AffineMapAttr::get(map));
|
2019-05-18 06:23:44 +08:00
|
|
|
}
|
|
|
|
if (auto shapedType = type.dyn_cast<ShapedType>()) {
|
|
|
|
visitType(shapedType.getElementType());
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
void ModuleState::visitAttribute(Attribute attr) {
|
2019-05-07 01:36:32 +08:00
|
|
|
recordAttributeReference(attr);
|
2019-07-04 04:21:24 +08:00
|
|
|
if (auto arrayAttr = attr.dyn_cast<ArrayAttr>()) {
|
2019-05-07 01:36:32 +08:00
|
|
|
for (auto elt : arrayAttr.getValue())
|
2018-07-19 07:29:21 +08:00
|
|
|
visitAttribute(elt);
|
2019-07-04 04:21:24 +08:00
|
|
|
} else if (auto typeAttr = attr.dyn_cast<TypeAttr>()) {
|
|
|
|
visitType(typeAttr.getValue());
|
|
|
|
}
|
2018-07-19 07:29:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-28 23:24:38 +08:00
|
|
|
void ModuleState::visitOperation(Operation *op) {
|
2018-12-30 07:33:43 +08:00
|
|
|
// Visit all the types used in the operation.
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto type : op->getOperandTypes())
|
|
|
|
visitType(type);
|
|
|
|
for (auto type : op->getResultTypes())
|
|
|
|
visitType(type);
|
2019-07-04 04:21:24 +08:00
|
|
|
for (auto ®ion : op->getRegions())
|
|
|
|
for (auto &block : region)
|
|
|
|
for (auto *arg : block.getArguments())
|
|
|
|
visitType(arg->getType());
|
2018-12-30 07:33:43 +08:00
|
|
|
|
|
|
|
// Visit each of the attributes.
|
2019-03-28 23:24:38 +08:00
|
|
|
for (auto elt : op->getAttrs())
|
2018-12-30 07:33:43 +08:00
|
|
|
visitAttribute(elt.second);
|
2018-08-08 05:24:38 +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
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleState::initializeSymbolAliases() {
|
|
|
|
// 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))
|
|
|
|
attrToAlias.insert({attr, {alias, kNonAttrKindAlias}});
|
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);
|
2019-01-11 14:08:39 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void ModuleState::initialize(Operation *op) {
|
2019-05-07 01:36:32 +08:00
|
|
|
// Initialize the symbol aliases.
|
|
|
|
initializeSymbolAliases();
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
// Visit each of the nested operations.
|
|
|
|
op->walk([&](Operation *op) { visitOperation(op); });
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ModulePrinter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ModulePrinter {
|
|
|
|
public:
|
2019-10-08 04:54:16 +08:00
|
|
|
ModulePrinter(raw_ostream &os, OpPrintingFlags flags = llvm::None,
|
|
|
|
ModuleState *state = nullptr)
|
|
|
|
: os(os), printerFlags(flags), state(state) {}
|
2019-03-21 12:14:59 +08:00
|
|
|
explicit ModulePrinter(ModulePrinter &printer)
|
2019-10-08 04:54:16 +08:00
|
|
|
: os(printer.os), printerFlags(printer.printerFlags),
|
|
|
|
state(printer.state) {}
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2019-11-02 05:47:42 +08:00
|
|
|
/// Returns the output stream of the printer.
|
|
|
|
raw_ostream &getStream() { return os; }
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
template <typename Container, typename UnaryFunctor>
|
|
|
|
inline void interleaveComma(const Container &c, UnaryFunctor each_fn) const {
|
2019-10-08 12:52:36 +08:00
|
|
|
mlir::interleaveComma(c, os, each_fn);
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void print(ModuleOp module);
|
2019-06-27 09:29:25 +08:00
|
|
|
|
|
|
|
/// Print the given attribute. If 'mayElideType' is true, some attributes are
|
|
|
|
/// printed without the type when the type matches the default used in the
|
|
|
|
/// parser (for example i64 is the default for integer attributes).
|
|
|
|
void printAttribute(Attribute attr, bool mayElideType = false);
|
2019-02-05 23:12:46 +08:00
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
void printType(Type type);
|
2019-06-22 09:27:49 +08:00
|
|
|
void printLocation(LocationAttr loc);
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
void printAffineMap(AffineMap map);
|
2019-06-26 01:29:53 +08:00
|
|
|
void printAffineExpr(
|
|
|
|
AffineExpr expr,
|
|
|
|
llvm::function_ref<void(unsigned, bool)> printValueName = nullptr);
|
2018-10-09 04:47:18 +08:00
|
|
|
void printAffineConstraint(AffineExpr expr, bool isEq);
|
2018-10-11 00:45:59 +08:00
|
|
|
void printIntegerSet(IntegerSet set);
|
2018-07-21 00:35:47 +08:00
|
|
|
|
|
|
|
protected:
|
2018-09-19 07:36:26 +08:00
|
|
|
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
2019-11-06 09:58:16 +08:00
|
|
|
ArrayRef<StringRef> elidedAttrs = {},
|
|
|
|
bool withKeyword = false);
|
2019-01-24 05:11:23 +08:00
|
|
|
void printTrailingLocation(Location loc);
|
2019-06-22 09:27:49 +08:00
|
|
|
void printLocationInternal(LocationAttr loc, bool pretty = false);
|
2018-10-26 06:46:10 +08:00
|
|
|
void printDenseElementsAttr(DenseElementsAttr attr);
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2019-11-02 05:47:42 +08:00
|
|
|
void printDialectAttribute(Attribute attr);
|
|
|
|
void printDialectType(Type type);
|
|
|
|
|
2019-10-04 19:37:14 +08:00
|
|
|
/// This enum is used to represent the binding strength of the enclosing
|
2018-10-10 01:59:27 +08:00
|
|
|
/// context that an AffineExprStorage is being printed in, so we can
|
2018-10-09 04:47:18 +08:00
|
|
|
/// intelligently produce parens.
|
2018-08-01 07:21:36 +08:00
|
|
|
enum class BindingStrength {
|
|
|
|
Weak, // + and -
|
|
|
|
Strong, // All other binary operators.
|
|
|
|
};
|
2019-06-26 01:29:53 +08:00
|
|
|
void printAffineExprInternal(
|
|
|
|
AffineExpr expr, BindingStrength enclosingTightness,
|
|
|
|
llvm::function_ref<void(unsigned, bool)> printValueName = nullptr);
|
2019-08-22 03:16:23 +08:00
|
|
|
|
|
|
|
/// The output stream for the printer.
|
|
|
|
raw_ostream &os;
|
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
/// A set of flags to control the printer's behavior.
|
|
|
|
OpPrintingFlags printerFlags;
|
|
|
|
|
2019-08-22 03:16:23 +08:00
|
|
|
/// An optional printer state for the module.
|
|
|
|
ModuleState *state;
|
2018-07-21 00:35:47 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2019-01-24 05:11:23 +08:00
|
|
|
void ModulePrinter::printTrailingLocation(Location loc) {
|
|
|
|
// Check to see if we are printing debug information.
|
2019-10-08 04:54:16 +08:00
|
|
|
if (!printerFlags.shouldPrintDebugInfo())
|
2019-01-24 05:11:23 +08:00
|
|
|
return;
|
|
|
|
|
2019-01-24 08:37:45 +08:00
|
|
|
os << " ";
|
|
|
|
printLocation(loc);
|
2019-01-24 05:11:23 +08:00
|
|
|
}
|
|
|
|
|
2019-06-22 09:27:49 +08:00
|
|
|
void ModulePrinter::printLocationInternal(LocationAttr loc, bool pretty) {
|
2019-01-24 05:11:23 +08:00
|
|
|
switch (loc.getKind()) {
|
2019-10-07 20:05:07 +08:00
|
|
|
case StandardAttributes::OpaqueLocation:
|
|
|
|
printLocationInternal(loc.cast<OpaqueLoc>().getFallbackLocation(), pretty);
|
|
|
|
break;
|
2019-06-22 09:27:49 +08:00
|
|
|
case StandardAttributes::UnknownLocation:
|
2019-01-26 12:37:33 +08:00
|
|
|
if (pretty)
|
|
|
|
os << "[unknown]";
|
|
|
|
else
|
|
|
|
os << "unknown";
|
2019-01-24 05:11:23 +08:00
|
|
|
break;
|
2019-06-22 09:27:49 +08:00
|
|
|
case StandardAttributes::FileLineColLocation: {
|
2019-01-24 05:11:23 +08:00
|
|
|
auto fileLoc = loc.cast<FileLineColLoc>();
|
2019-01-26 12:37:33 +08:00
|
|
|
auto mayQuote = pretty ? "" : "\"";
|
|
|
|
os << mayQuote << fileLoc.getFilename() << mayQuote << ':'
|
|
|
|
<< fileLoc.getLine() << ':' << fileLoc.getColumn();
|
2019-01-24 05:11:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-06-22 09:27:49 +08:00
|
|
|
case StandardAttributes::NameLocation: {
|
2019-05-14 05:45:48 +08:00
|
|
|
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 << ')';
|
|
|
|
}
|
2019-01-24 05:11:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-06-22 09:27:49 +08:00
|
|
|
case StandardAttributes::CallSiteLocation: {
|
2019-01-24 05:11:23 +08:00
|
|
|
auto callLocation = loc.cast<CallSiteLoc>();
|
|
|
|
auto caller = callLocation.getCaller();
|
2019-01-26 12:37:33 +08:00
|
|
|
auto callee = callLocation.getCallee();
|
|
|
|
if (!pretty)
|
|
|
|
os << "callsite(";
|
|
|
|
printLocationInternal(callee, pretty);
|
|
|
|
if (pretty) {
|
|
|
|
if (callee.isa<NameLoc>()) {
|
|
|
|
if (caller.isa<FileLineColLoc>()) {
|
|
|
|
os << " at ";
|
|
|
|
} else {
|
|
|
|
os << "\n at ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
os << "\n at ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
os << " at ";
|
|
|
|
}
|
|
|
|
printLocationInternal(caller, pretty);
|
|
|
|
if (!pretty)
|
|
|
|
os << ")";
|
2019-01-24 05:11:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-06-22 09:27:49 +08:00
|
|
|
case StandardAttributes::FusedLocation: {
|
2019-01-24 05:11:23 +08:00
|
|
|
auto fusedLoc = loc.cast<FusedLoc>();
|
2019-01-26 12:37:33 +08:00
|
|
|
if (!pretty)
|
|
|
|
os << "fused";
|
2019-01-24 05:11:23 +08:00
|
|
|
if (auto metadata = fusedLoc.getMetadata())
|
|
|
|
os << '<' << metadata << '>';
|
|
|
|
os << '[';
|
|
|
|
interleave(
|
|
|
|
fusedLoc.getLocations(),
|
2019-01-26 12:37:33 +08:00
|
|
|
[&](Location loc) { printLocationInternal(loc, pretty); },
|
2019-01-24 05:11:23 +08:00
|
|
|
[&]() { os << ", "; });
|
|
|
|
os << ']';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 00:09:54 +08:00
|
|
|
/// Print a floating point value in a way that the parser will be able to
|
|
|
|
/// round-trip losslessly.
|
2018-10-21 09:31:49 +08:00
|
|
|
static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
|
2018-08-16 00:09:54 +08:00
|
|
|
// 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;
|
|
|
|
apValue.toString(strValue, 6, 0, false);
|
|
|
|
|
|
|
|
// 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!");
|
2019-07-31 05:05:49 +08:00
|
|
|
|
|
|
|
// Parse back the stringized version and check that the value is equal
|
|
|
|
// (i.e., there is no precision loss). If it is not, use the default format
|
|
|
|
// of APFloat instead of the exponential notation.
|
|
|
|
if (!APFloat(apValue.getSemantics(), strValue).bitwiseIsEqual(apValue)) {
|
|
|
|
strValue.clear();
|
|
|
|
apValue.toString(strValue);
|
2018-08-16 00:09:54 +08:00
|
|
|
}
|
2019-07-31 05:05:49 +08:00
|
|
|
os << strValue;
|
|
|
|
return;
|
2018-08-16 00:09:54 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 05:05:49 +08:00
|
|
|
// Print special values in hexadecimal format. The sign bit should be
|
|
|
|
// included in the literal.
|
2018-10-21 09:31:49 +08:00
|
|
|
SmallVector<char, 16> str;
|
2019-07-31 05:05:49 +08:00
|
|
|
APInt apInt = apValue.bitcastToAPInt();
|
|
|
|
apInt.toString(str, /*Radix=*/16, /*Signed=*/false,
|
|
|
|
/*formatAsCLiteral=*/true);
|
2018-10-21 09:31:49 +08:00
|
|
|
os << str;
|
2018-08-16 00:09:54 +08:00
|
|
|
}
|
|
|
|
|
2019-06-22 09:27:49 +08:00
|
|
|
void ModulePrinter::printLocation(LocationAttr loc) {
|
2019-10-08 04:54:16 +08:00
|
|
|
if (printerFlags.shouldPrintDebugInfoPrettyForm()) {
|
2019-01-26 12:37:33 +08:00
|
|
|
printLocationInternal(loc, /*pretty=*/true);
|
|
|
|
} else {
|
|
|
|
os << "loc(";
|
|
|
|
printLocationInternal(loc);
|
|
|
|
os << ')';
|
|
|
|
}
|
2019-01-24 05:11:23 +08:00
|
|
|
}
|
|
|
|
|
2019-05-16 00:10:52 +08:00
|
|
|
/// 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.
|
2019-10-18 03:23:47 +08:00
|
|
|
symName = symName.drop_while(
|
|
|
|
[](char c) { return llvm::isAlnum(c) || c == '.' || c == '_'; });
|
2019-05-16 00:10:52 +08:00
|
|
|
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;
|
2019-08-29 04:55:11 +08:00
|
|
|
case '-':
|
|
|
|
// Treat `->` as a special token.
|
|
|
|
if (!symName.empty() && symName.front() == '>') {
|
|
|
|
symName = symName.drop_front();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2019-05-16 00:10:52 +08:00
|
|
|
// 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 << "\">";
|
|
|
|
}
|
|
|
|
|
2019-10-09 08:44:39 +08:00
|
|
|
/// 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 << '"';
|
|
|
|
}
|
|
|
|
|
2019-06-27 09:29:25 +08:00
|
|
|
void ModulePrinter::printAttribute(Attribute attr, bool mayElideType) {
|
2018-11-30 10:47:39 +08:00
|
|
|
if (!attr) {
|
|
|
|
os << "<<NULL ATTRIBUTE>>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:36:32 +08:00
|
|
|
// Check for an alias for this attribute.
|
2019-08-22 03:16:23 +08:00
|
|
|
if (state) {
|
|
|
|
Twine alias = state->getAttributeAlias(attr);
|
|
|
|
if (!alias.isTriviallyEmpty()) {
|
|
|
|
os << '#' << alias;
|
|
|
|
return;
|
|
|
|
}
|
2019-05-07 01:36:32 +08:00
|
|
|
}
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
switch (attr.getKind()) {
|
2019-11-02 05:47:42 +08:00
|
|
|
default:
|
|
|
|
return printDialectAttribute(attr);
|
2019-05-14 03:34:42 +08:00
|
|
|
|
2019-05-16 00:10:52 +08:00
|
|
|
case StandardAttributes::Opaque: {
|
|
|
|
auto opaqueAttr = attr.cast<OpaqueAttr>();
|
|
|
|
printDialectSymbol(os, "#", opaqueAttr.getDialectNamespace(),
|
|
|
|
opaqueAttr.getAttrData());
|
|
|
|
break;
|
|
|
|
}
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::Unit:
|
2019-04-26 00:56:09 +08:00
|
|
|
os << "unit";
|
|
|
|
break;
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::Bool:
|
2018-10-26 06:46:10 +08:00
|
|
|
os << (attr.cast<BoolAttr>().getValue() ? "true" : "false");
|
2019-06-27 09:29:25 +08:00
|
|
|
|
|
|
|
// BoolAttr always elides the type.
|
|
|
|
return;
|
2019-06-01 00:24:48 +08:00
|
|
|
case StandardAttributes::Dictionary:
|
|
|
|
os << '{';
|
|
|
|
interleaveComma(attr.cast<DictionaryAttr>().getValue(),
|
|
|
|
[&](NamedAttribute attr) {
|
2019-10-22 02:01:38 +08:00
|
|
|
os << attr.first;
|
|
|
|
|
|
|
|
// The value of a UnitAttr is elided within a dictionary.
|
|
|
|
if (attr.second.isa<UnitAttr>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
os << " = ";
|
2019-06-01 00:24:48 +08:00
|
|
|
printAttribute(attr.second);
|
|
|
|
});
|
|
|
|
os << '}';
|
|
|
|
break;
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::Integer: {
|
2018-11-16 09:53:51 +08:00
|
|
|
auto intAttr = attr.cast<IntegerAttr>();
|
|
|
|
// Print all integer attributes as signed unless i1.
|
2018-12-18 02:05:56 +08:00
|
|
|
bool isSigned = intAttr.getType().isIndex() ||
|
|
|
|
intAttr.getType().getIntOrFloatBitWidth() != 1;
|
2018-11-16 09:53:51 +08:00
|
|
|
intAttr.getValue().print(os, isSigned);
|
2019-06-27 09:29:25 +08:00
|
|
|
|
|
|
|
// IntegerAttr elides the type if I64.
|
|
|
|
if (mayElideType && intAttr.getType().isInteger(64))
|
|
|
|
return;
|
2018-07-19 07:29:21 +08:00
|
|
|
break;
|
2018-11-16 09:53:51 +08:00
|
|
|
}
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::Float: {
|
2019-02-05 23:12:46 +08:00
|
|
|
auto floatAttr = attr.cast<FloatAttr>();
|
|
|
|
printFloatValue(floatAttr.getValue(), os);
|
2019-06-27 09:29:25 +08:00
|
|
|
|
|
|
|
// FloatAttr elides the type if F64.
|
|
|
|
if (mayElideType && floatAttr.getType().isF64())
|
|
|
|
return;
|
2018-07-19 07:29:21 +08:00
|
|
|
break;
|
2019-02-05 23:12:46 +08:00
|
|
|
}
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::String:
|
2018-08-16 00:09:54 +08:00
|
|
|
os << '"';
|
2018-10-26 06:46:10 +08:00
|
|
|
printEscapedString(attr.cast<StringAttr>().getValue(), os);
|
2018-08-16 00:09:54 +08:00
|
|
|
os << '"';
|
2018-07-19 07:29:21 +08:00
|
|
|
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) {
|
|
|
|
printAttribute(attr, /*mayElideType=*/true);
|
|
|
|
});
|
2018-07-19 07:29:21 +08:00
|
|
|
os << ']';
|
|
|
|
break;
|
2019-05-14 03:34:42 +08:00
|
|
|
case StandardAttributes::AffineMap:
|
2019-05-07 01:36:32 +08:00
|
|
|
attr.cast<AffineMapAttr>().getValue().print(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:
|
2019-05-07 01:36:32 +08:00
|
|
|
attr.cast<IntegerSetAttr>().getValue().print(os);
|
2018-10-26 13:13:03 +08:00
|
|
|
break;
|
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-07-12 02:41:04 +08:00
|
|
|
case StandardAttributes::SymbolRef:
|
2019-10-09 08:44:39 +08:00
|
|
|
printSymbolReference(attr.cast<SymbolRefAttr>().getValue(), os);
|
2018-08-20 12:17:22 +08:00
|
|
|
break;
|
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-06-26 07:06:13 +08:00
|
|
|
os << "opaque<\"" << eltsAttr.getDialect()->getNamespace() << "\", ";
|
2019-10-08 08:18:54 +08:00
|
|
|
os << '"' << "0x";
|
|
|
|
|
|
|
|
// Check for large ElementsAttr elision.
|
|
|
|
if (printerFlags.shouldElideElementsAttr(eltsAttr))
|
|
|
|
os << "...";
|
|
|
|
else
|
|
|
|
os << llvm::toHex(eltsAttr.getValue());
|
|
|
|
os << "\">";
|
2018-10-24 04:44:04 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-06-07 07:15:42 +08:00
|
|
|
case StandardAttributes::DenseElements: {
|
2018-10-26 06:46:10 +08:00
|
|
|
auto eltsAttr = attr.cast<DenseElementsAttr>();
|
2018-10-19 04:54:44 +08:00
|
|
|
os << "dense<";
|
|
|
|
printDenseElementsAttr(eltsAttr);
|
2019-06-27 09:29:25 +08:00
|
|
|
os << '>';
|
2018-10-19 04:54:44 +08:00
|
|
|
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>();
|
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<";
|
2018-10-26 06:46:10 +08:00
|
|
|
printDenseElementsAttr(elementsAttr.getIndices());
|
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 << ", ";
|
2018-10-26 06:46:10 +08:00
|
|
|
printDenseElementsAttr(elementsAttr.getValues());
|
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
|
|
|
|
|
|
|
// Print the type if it isn't a 'none' type.
|
|
|
|
auto attrType = attr.getType();
|
|
|
|
if (!attrType.isa<NoneType>()) {
|
|
|
|
os << " : ";
|
|
|
|
printType(attrType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the integer element of the given DenseElementsAttr at 'index'.
|
|
|
|
static void printDenseIntElement(DenseElementsAttr attr, raw_ostream &os,
|
|
|
|
unsigned index) {
|
2019-08-11 08:26:35 +08:00
|
|
|
APInt value = *std::next(attr.int_value_begin(), index);
|
2019-07-19 00:24:41 +08:00
|
|
|
if (value.getBitWidth() == 1)
|
|
|
|
os << (value.getBoolValue() ? "true" : "false");
|
|
|
|
else
|
|
|
|
value.print(os, /*isSigned=*/true);
|
2019-06-27 09:29:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the float element of the given DenseElementsAttr at 'index'.
|
|
|
|
static void printDenseFloatElement(DenseElementsAttr attr, raw_ostream &os,
|
|
|
|
unsigned index) {
|
2019-08-11 08:26:35 +08:00
|
|
|
APFloat value = *std::next(attr.float_value_begin(), index);
|
2019-06-27 09:29:25 +08:00
|
|
|
printFloatValue(value, os);
|
2018-07-19 07:29:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
void ModulePrinter::printDenseElementsAttr(DenseElementsAttr attr) {
|
2018-10-31 05:59:22 +08:00
|
|
|
auto type = attr.getType();
|
|
|
|
auto shape = type.getShape();
|
|
|
|
auto rank = type.getRank();
|
2018-10-19 04:54:44 +08:00
|
|
|
|
2019-06-27 09:29:25 +08:00
|
|
|
// The function used to print elements of this attribute.
|
|
|
|
auto printEltFn = type.getElementType().isa<IntegerType>()
|
|
|
|
? printDenseIntElement
|
|
|
|
: printDenseFloatElement;
|
2018-10-19 04:54:44 +08:00
|
|
|
|
2019-06-13 06:05:45 +08:00
|
|
|
// Special case for 0-d and splat tensors.
|
|
|
|
if (attr.isSplat()) {
|
2019-06-27 09:29:25 +08:00
|
|
|
printEltFn(attr, os, 0);
|
2019-04-02 01:01:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-08 08:18:54 +08:00
|
|
|
// Check for large elements attr elision. We explicitly check *after* splat,
|
|
|
|
// as the splat printing is already elided.
|
|
|
|
if (printerFlags.shouldElideElementsAttr(attr)) {
|
|
|
|
os << "...";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
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;
|
|
|
|
|
|
|
|
auto bumpCounter = [&]() {
|
|
|
|
// 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;
|
2019-06-27 09:29:25 +08:00
|
|
|
printEltFn(attr, os, idx);
|
2018-10-19 04:54:44 +08:00
|
|
|
bumpCounter();
|
|
|
|
}
|
|
|
|
while (openBrackets-- > 0)
|
|
|
|
os << ']';
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
void ModulePrinter::printType(Type type) {
|
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) {
|
|
|
|
StringRef alias = state->getTypeAlias(type);
|
|
|
|
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>();
|
|
|
|
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;
|
|
|
|
os << 'x';
|
|
|
|
}
|
2018-10-31 05:59:22 +08:00
|
|
|
printType(v.getElementType());
|
|
|
|
for (auto map : v.getAffineMaps()) {
|
2018-07-18 07:56:54 +08:00
|
|
|
os << ", ";
|
2019-05-07 01:36:32 +08:00
|
|
|
printAttribute(AffineMapAttr::get(map));
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
2018-07-26 03:55:50 +08:00
|
|
|
// Only print the memory space if it is the non-default one.
|
2018-10-31 05:59:22 +08:00
|
|
|
if (v.getMemorySpace())
|
|
|
|
os << ", " << v.getMemorySpace();
|
2018-07-18 07:56:54 +08:00
|
|
|
os << '>';
|
|
|
|
return;
|
|
|
|
}
|
2019-03-30 13:23:34 +08:00
|
|
|
case StandardTypes::Complex:
|
|
|
|
os << "complex<";
|
|
|
|
printType(type.cast<ComplexType>().getElementType());
|
|
|
|
os << '>';
|
|
|
|
return;
|
2019-03-20 01:59:02 +08:00
|
|
|
case StandardTypes::Tuple: {
|
|
|
|
auto tuple = type.cast<TupleType>();
|
|
|
|
os << "tuple<";
|
|
|
|
interleaveComma(tuple.getTypes(), [&](Type type) { printType(type); });
|
|
|
|
os << '>';
|
|
|
|
return;
|
|
|
|
}
|
2019-04-28 09:35:04 +08:00
|
|
|
case StandardTypes::None:
|
|
|
|
os << "none";
|
|
|
|
return;
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 05:47:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CustomDialectAsmPrinter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// This class provides the main specialication 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 {
|
|
|
|
public:
|
|
|
|
CustomDialectAsmPrinter(ModulePrinter &printer) : printer(printer) {}
|
|
|
|
~CustomDialectAsmPrinter() override {}
|
|
|
|
|
|
|
|
raw_ostream &getStream() const override { return printer.getStream(); }
|
|
|
|
|
|
|
|
/// Print the given attribute to the stream.
|
|
|
|
void printAttribute(Attribute attr) override { printer.printAttribute(attr); }
|
|
|
|
|
|
|
|
/// Print the given floating point value in a stablized form.
|
|
|
|
void printFloat(const APFloat &value) override {
|
|
|
|
printFloatValue(value, getStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the given type to the stream.
|
|
|
|
void printType(Type type) override { printer.printType(type); }
|
|
|
|
|
|
|
|
/// The main module printer.
|
|
|
|
ModulePrinter &printer;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void ModulePrinter::printDialectAttribute(Attribute attr) {
|
|
|
|
auto &dialect = attr.getDialect();
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModulePrinter::printDialectType(Type type) {
|
|
|
|
auto &dialect = type.getDialect();
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
printDialectSymbol(os, "!", dialect.getNamespace(), typeName);
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Affine expressions and maps
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-06-26 01:29:53 +08:00
|
|
|
void ModulePrinter::printAffineExpr(
|
|
|
|
AffineExpr expr, llvm::function_ref<void(unsigned, bool)> printValueName) {
|
|
|
|
printAffineExprInternal(expr, BindingStrength::Weak, printValueName);
|
2018-08-01 07:21:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModulePrinter::printAffineExprInternal(
|
2019-06-24 22:31:52 +08:00
|
|
|
AffineExpr expr, BindingStrength enclosingTightness,
|
2019-06-26 01:29:53 +08:00
|
|
|
llvm::function_ref<void(unsigned, bool)> printValueName) {
|
2018-08-01 07:21:36 +08:00
|
|
|
const char *binopSpelling = nullptr;
|
2018-10-10 01:59:27 +08:00
|
|
|
switch (expr.getKind()) {
|
2019-06-24 22:31:52 +08:00
|
|
|
case AffineExprKind::SymbolId: {
|
|
|
|
unsigned pos = expr.cast<AffineSymbolExpr>().getPosition();
|
2019-06-26 01:29:53 +08:00
|
|
|
if (printValueName)
|
|
|
|
printValueName(pos, /*isSymbol=*/true);
|
2019-06-24 22:31:52 +08:00
|
|
|
else
|
|
|
|
os << 's' << pos;
|
2018-07-21 00:35:47 +08:00
|
|
|
return;
|
2019-06-24 22:31:52 +08:00
|
|
|
}
|
|
|
|
case AffineExprKind::DimId: {
|
|
|
|
unsigned pos = expr.cast<AffineDimExpr>().getPosition();
|
2019-06-26 01:29:53 +08:00
|
|
|
if (printValueName)
|
|
|
|
printValueName(pos, /*isSymbol=*/false);
|
2019-06-24 22:31:52 +08:00
|
|
|
else
|
|
|
|
os << 'd' << pos;
|
2018-07-21 00:35:47 +08:00
|
|
|
return;
|
2019-06-24 22:31:52 +08:00
|
|
|
}
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::Constant:
|
2018-10-10 01:59:27 +08:00
|
|
|
os << expr.cast<AffineConstantExpr>().getValue();
|
2018-07-21 00:35:47 +08:00
|
|
|
return;
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::Add:
|
2018-08-01 07:21:36 +08:00
|
|
|
binopSpelling = " + ";
|
|
|
|
break;
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::Mul:
|
2018-08-01 07:21:36 +08:00
|
|
|
binopSpelling = " * ";
|
|
|
|
break;
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::FloorDiv:
|
2018-08-01 07:21:36 +08:00
|
|
|
binopSpelling = " floordiv ";
|
|
|
|
break;
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::CeilDiv:
|
2018-08-01 07:21:36 +08:00
|
|
|
binopSpelling = " ceildiv ";
|
|
|
|
break;
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::Mod:
|
2018-08-01 07:21:36 +08:00
|
|
|
binopSpelling = " mod ";
|
|
|
|
break;
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
|
|
|
|
2018-10-09 04:47:18 +08:00
|
|
|
auto binOp = expr.cast<AffineBinaryOpExpr>();
|
2019-01-21 22:59:58 +08:00
|
|
|
AffineExpr lhsExpr = binOp.getLHS();
|
|
|
|
AffineExpr rhsExpr = binOp.getRHS();
|
2018-07-21 00:35:47 +08:00
|
|
|
|
2018-08-01 07:21:36 +08:00
|
|
|
// Handle tightly binding binary operators.
|
2018-10-10 01:59:27 +08:00
|
|
|
if (binOp.getKind() != AffineExprKind::Add) {
|
2018-08-01 07:21:36 +08:00
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << '(';
|
|
|
|
|
2019-01-21 22:59:58 +08:00
|
|
|
// Pretty print multiplication with -1.
|
|
|
|
auto rhsConst = rhsExpr.dyn_cast<AffineConstantExpr>();
|
|
|
|
if (rhsConst && rhsConst.getValue() == -1) {
|
|
|
|
os << "-";
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Strong, printValueName);
|
2019-01-21 22:59:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Strong, printValueName);
|
|
|
|
|
2018-08-01 07:21:36 +08:00
|
|
|
os << binopSpelling;
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExprInternal(rhsExpr, BindingStrength::Strong, printValueName);
|
2018-08-01 07:21:36 +08:00
|
|
|
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
2018-07-21 00:35:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out special "pretty" forms for add.
|
2018-08-01 07:21:36 +08:00
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << '(';
|
2018-07-21 00:35:47 +08:00
|
|
|
|
|
|
|
// Pretty print addition to a product that has a negative operand as a
|
|
|
|
// subtraction.
|
2018-10-09 04:47:18 +08:00
|
|
|
if (auto rhs = rhsExpr.dyn_cast<AffineBinaryOpExpr>()) {
|
2018-10-10 01:59:27 +08:00
|
|
|
if (rhs.getKind() == AffineExprKind::Mul) {
|
|
|
|
AffineExpr rrhsExpr = rhs.getRHS();
|
2018-10-09 04:47:18 +08:00
|
|
|
if (auto rrhs = rrhsExpr.dyn_cast<AffineConstantExpr>()) {
|
2018-10-10 01:59:27 +08:00
|
|
|
if (rrhs.getValue() == -1) {
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Weak,
|
|
|
|
printValueName);
|
2018-08-01 07:21:36 +08:00
|
|
|
os << " - ";
|
2018-10-19 09:18:04 +08:00
|
|
|
if (rhs.getLHS().getKind() == AffineExprKind::Add) {
|
2019-06-24 22:31:52 +08:00
|
|
|
printAffineExprInternal(rhs.getLHS(), BindingStrength::Strong,
|
2019-06-26 01:29:53 +08:00
|
|
|
printValueName);
|
2018-10-19 09:18:04 +08:00
|
|
|
} else {
|
2019-06-24 22:31:52 +08:00
|
|
|
printAffineExprInternal(rhs.getLHS(), BindingStrength::Weak,
|
2019-06-26 01:29:53 +08:00
|
|
|
printValueName);
|
2018-10-19 09:18:04 +08:00
|
|
|
}
|
2018-08-01 07:21:36 +08:00
|
|
|
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-10 01:59:27 +08:00
|
|
|
if (rrhs.getValue() < -1) {
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Weak,
|
|
|
|
printValueName);
|
2018-08-02 13:02:00 +08:00
|
|
|
os << " - ";
|
2019-06-24 22:31:52 +08:00
|
|
|
printAffineExprInternal(rhs.getLHS(), BindingStrength::Strong,
|
2019-06-26 01:29:53 +08:00
|
|
|
printValueName);
|
2018-10-10 01:59:27 +08:00
|
|
|
os << " * " << -rrhs.getValue();
|
2018-08-01 07:21:36 +08:00
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
2018-07-21 00:35:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pretty print addition to a negative number as a subtraction.
|
2019-01-21 22:59:58 +08:00
|
|
|
if (auto rhsConst = rhsExpr.dyn_cast<AffineConstantExpr>()) {
|
|
|
|
if (rhsConst.getValue() < 0) {
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Weak, printValueName);
|
2019-01-21 22:59:58 +08:00
|
|
|
os << " - " << -rhsConst.getValue();
|
2018-08-08 05:24:38 +08:00
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
2018-07-21 00:35:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExprInternal(lhsExpr, BindingStrength::Weak, printValueName);
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
os << " + ";
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExprInternal(rhsExpr, BindingStrength::Weak, printValueName);
|
2018-08-01 07:21:36 +08:00
|
|
|
|
|
|
|
if (enclosingTightness == BindingStrength::Strong)
|
|
|
|
os << ')';
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
|
|
|
|
2018-10-09 04:47:18 +08:00
|
|
|
void ModulePrinter::printAffineConstraint(AffineExpr expr, bool isEq) {
|
2018-08-08 05:24:38 +08:00
|
|
|
printAffineExprInternal(expr, BindingStrength::Weak);
|
|
|
|
isEq ? os << " == 0" : os << " >= 0";
|
|
|
|
}
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
void ModulePrinter::printAffineMap(AffineMap map) {
|
2018-07-21 00:35:47 +08:00
|
|
|
// Dimension identifiers.
|
|
|
|
os << '(';
|
2018-10-10 07:39:24 +08:00
|
|
|
for (int i = 0; i < (int)map.getNumDims() - 1; ++i)
|
2018-07-30 05:13:03 +08:00
|
|
|
os << 'd' << i << ", ";
|
2018-10-10 07:39:24 +08:00
|
|
|
if (map.getNumDims() >= 1)
|
|
|
|
os << 'd' << map.getNumDims() - 1;
|
2018-07-30 05:13:03 +08:00
|
|
|
os << ')';
|
2018-07-21 00:35:47 +08:00
|
|
|
|
|
|
|
// Symbolic identifiers.
|
2018-10-10 07:39:24 +08:00
|
|
|
if (map.getNumSymbols() != 0) {
|
2018-07-30 05:13:03 +08:00
|
|
|
os << '[';
|
2018-10-10 07:39:24 +08:00
|
|
|
for (unsigned i = 0; i < map.getNumSymbols() - 1; ++i)
|
2018-07-30 05:13:03 +08:00
|
|
|
os << 's' << i << ", ";
|
2018-10-10 07:39:24 +08:00
|
|
|
if (map.getNumSymbols() >= 1)
|
|
|
|
os << 's' << map.getNumSymbols() - 1;
|
2018-07-30 05:13:03 +08:00
|
|
|
os << ']';
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Result affine expressions.
|
|
|
|
os << " -> (";
|
2018-10-10 07:39:24 +08:00
|
|
|
interleaveComma(map.getResults(),
|
2018-10-09 04:47:18 +08:00
|
|
|
[&](AffineExpr expr) { printAffineExpr(expr); });
|
2018-07-30 05:13:03 +08:00
|
|
|
os << ')';
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 00:45:59 +08:00
|
|
|
void ModulePrinter::printIntegerSet(IntegerSet set) {
|
2018-08-08 05:24:38 +08:00
|
|
|
// Dimension identifiers.
|
|
|
|
os << '(';
|
2018-10-11 00:45:59 +08:00
|
|
|
for (unsigned i = 1; i < set.getNumDims(); ++i)
|
2018-08-08 05:24:38 +08:00
|
|
|
os << 'd' << i - 1 << ", ";
|
2018-10-11 00:45:59 +08:00
|
|
|
if (set.getNumDims() >= 1)
|
|
|
|
os << 'd' << set.getNumDims() - 1;
|
2018-08-08 05:24:38 +08:00
|
|
|
os << ')';
|
|
|
|
|
|
|
|
// Symbolic identifiers.
|
2018-10-11 00:45:59 +08:00
|
|
|
if (set.getNumSymbols() != 0) {
|
2018-08-08 05:24:38 +08:00
|
|
|
os << '[';
|
2018-10-11 00:45:59 +08:00
|
|
|
for (unsigned i = 0; i < set.getNumSymbols() - 1; ++i)
|
2018-08-08 05:24:38 +08:00
|
|
|
os << 's' << i << ", ";
|
2018-10-11 00:45:59 +08:00
|
|
|
if (set.getNumSymbols() >= 1)
|
|
|
|
os << 's' << set.getNumSymbols() - 1;
|
2018-08-08 05:24:38 +08:00
|
|
|
os << ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print constraints.
|
|
|
|
os << " : (";
|
2019-05-04 10:48:57 +08:00
|
|
|
int numConstraints = set.getNumConstraints();
|
2018-08-08 05:24:38 +08:00
|
|
|
for (int i = 1; i < numConstraints; ++i) {
|
2018-10-11 00:45:59 +08:00
|
|
|
printAffineConstraint(set.getConstraint(i - 1), set.isEq(i - 1));
|
2018-08-08 05:24:38 +08:00
|
|
|
os << ", ";
|
|
|
|
}
|
|
|
|
if (numConstraints >= 1)
|
2018-10-11 00:45:59 +08:00
|
|
|
printAffineConstraint(set.getConstraint(numConstraints - 1),
|
|
|
|
set.isEq(numConstraints - 1));
|
2018-08-08 05:24:38 +08:00
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-07-10 01:40:29 +08:00
|
|
|
// Operation printing
|
2018-06-24 07:03:42 +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)
|
|
|
|
os << " attributes ";
|
|
|
|
|
2018-09-19 07:36:26 +08:00
|
|
|
// Otherwise, print them all out in braces.
|
|
|
|
os << " {";
|
|
|
|
interleaveComma(filteredAttrs, [&](NamedAttribute attr) {
|
2019-04-26 00:56:09 +08:00
|
|
|
os << attr.first;
|
|
|
|
|
|
|
|
// Pretty printing elides the attribute value for unit attributes.
|
|
|
|
if (attr.second.isa<UnitAttr>())
|
|
|
|
return;
|
|
|
|
|
2019-06-26 10:06:06 +08:00
|
|
|
os << " = ";
|
2019-06-27 09:29:25 +08:00
|
|
|
printAttribute(attr.second);
|
2018-09-19 07:36:26 +08:00
|
|
|
});
|
|
|
|
os << '}';
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:42:46 +08:00
|
|
|
namespace {
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
// OperationPrinter contains common functionality for printing operations.
|
|
|
|
class OperationPrinter : public ModulePrinter, private OpAsmPrinter {
|
2018-07-10 08:42:46 +08:00
|
|
|
public:
|
2019-07-04 04:21:24 +08:00
|
|
|
OperationPrinter(Operation *op, ModulePrinter &other);
|
|
|
|
OperationPrinter(Region *region, ModulePrinter &other);
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2019-03-28 23:24:38 +08:00
|
|
|
// Methods to print operations.
|
|
|
|
void print(Operation *op);
|
2019-03-27 20:11:58 +08:00
|
|
|
void print(Block *block, bool printBlockArgs = true,
|
|
|
|
bool printBlockTerminator = true);
|
2018-07-10 08:42:46 +08:00
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
void printOperation(Operation *op);
|
2019-05-10 13:45:38 +08:00
|
|
|
void printGenericOp(Operation *op) override;
|
2018-07-25 07:07:22 +08:00
|
|
|
|
|
|
|
// Implement OpAsmPrinter.
|
2019-05-10 13:45:38 +08:00
|
|
|
raw_ostream &getStream() const override { return os; }
|
|
|
|
void printType(Type type) override { ModulePrinter::printType(type); }
|
|
|
|
void printAttribute(Attribute attr) override {
|
|
|
|
ModulePrinter::printAttribute(attr);
|
|
|
|
}
|
|
|
|
void printOperand(Value *value) override { printValueID(value); }
|
2018-08-04 02:12:34 +08:00
|
|
|
|
2018-08-03 07:54:36 +08:00
|
|
|
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
2019-05-10 13:45:38 +08:00
|
|
|
ArrayRef<StringRef> elidedAttrs = {}) override {
|
2019-11-06 09:58:16 +08:00
|
|
|
ModulePrinter::printOptionalAttrDict(attrs, elidedAttrs);
|
|
|
|
}
|
|
|
|
void printOptionalAttrDictWithKeyword(
|
|
|
|
ArrayRef<NamedAttribute> attrs,
|
|
|
|
ArrayRef<StringRef> elidedAttrs = {}) override {
|
|
|
|
ModulePrinter::printOptionalAttrDict(attrs, elidedAttrs,
|
|
|
|
/*withKeyword=*/true);
|
|
|
|
}
|
2018-07-10 08:42:46 +08:00
|
|
|
|
2018-08-02 01:43:18 +08:00
|
|
|
enum { nameSentinel = ~0U };
|
|
|
|
|
2019-03-22 08:53:00 +08:00
|
|
|
void printBlockName(Block *block) {
|
2018-12-31 08:22:50 +08:00
|
|
|
auto id = getBlockID(block);
|
|
|
|
if (id != ~0U)
|
|
|
|
os << "^bb" << id;
|
|
|
|
else
|
|
|
|
os << "^INVALIDBLOCK";
|
|
|
|
}
|
2018-08-02 01:43:18 +08:00
|
|
|
|
2019-03-22 08:53:00 +08:00
|
|
|
unsigned getBlockID(Block *block) {
|
2018-12-29 05:07:39 +08:00
|
|
|
auto it = blockIDs.find(block);
|
2018-12-31 08:22:50 +08:00
|
|
|
return it != blockIDs.end() ? it->second : ~0U;
|
2018-07-21 00:28:54 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
void printSuccessorAndUseList(Operation *term, unsigned index) override;
|
2018-07-21 09:41:34 +08:00
|
|
|
|
2019-03-15 01:38:44 +08:00
|
|
|
/// Print a region.
|
2019-03-27 20:11:58 +08:00
|
|
|
void printRegion(Region &blocks, bool printEntryBlockArgs,
|
|
|
|
bool printBlockTerminators) override {
|
2019-01-26 04:48:25 +08:00
|
|
|
os << " {\n";
|
2019-01-27 04:40:12 +08:00
|
|
|
if (!blocks.empty()) {
|
|
|
|
auto *entryBlock = &blocks.front();
|
|
|
|
print(entryBlock,
|
2019-03-27 20:11:58 +08:00
|
|
|
printEntryBlockArgs && entryBlock->getNumArguments() != 0,
|
|
|
|
printBlockTerminators);
|
2019-01-27 04:40:12 +08:00
|
|
|
for (auto &b : llvm::drop_begin(blocks.getBlocks(), 1))
|
|
|
|
print(&b);
|
|
|
|
}
|
2019-01-26 04:48:25 +08:00
|
|
|
os.indent(currentIndent) << "}";
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:35:24 +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, ArrayRef<Value *> namesToUse) override;
|
|
|
|
|
2019-06-24 22:31:52 +08:00
|
|
|
void printAffineMapOfSSAIds(AffineMapAttr mapAttr,
|
2019-07-09 00:35:19 +08:00
|
|
|
ArrayRef<Value *> operands) override {
|
2019-06-24 22:31:52 +08:00
|
|
|
AffineMap map = mapAttr.getValue();
|
|
|
|
unsigned numDims = map.getNumDims();
|
2019-06-26 01:29:53 +08:00
|
|
|
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 << ')';
|
|
|
|
};
|
|
|
|
|
2019-06-24 22:31:52 +08:00
|
|
|
interleaveComma(map.getResults(), [&](AffineExpr expr) {
|
2019-06-26 01:29:53 +08:00
|
|
|
printAffineExpr(expr, printValueName);
|
2019-06-24 22:31:52 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-09 08:44:39 +08:00
|
|
|
/// Print the given string as a symbol reference.
|
|
|
|
void printSymbolName(StringRef symbolRef) override {
|
|
|
|
::printSymbolReference(symbolRef, os);
|
|
|
|
}
|
|
|
|
|
2019-03-28 23:24:38 +08:00
|
|
|
// Number of spaces used for indenting nested operations.
|
2018-12-29 03:41:56 +08:00
|
|
|
const static unsigned indentWidth = 2;
|
2018-07-31 06:18:10 +08:00
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
protected:
|
2019-03-24 06:09:06 +08:00
|
|
|
void numberValueID(Value *value);
|
2019-07-10 01:40:29 +08:00
|
|
|
void numberValuesInRegion(Region ®ion);
|
2019-03-22 08:53:00 +08:00
|
|
|
void numberValuesInBlock(Block &block);
|
2019-08-24 01:35:24 +08:00
|
|
|
void printValueID(Value *value, bool printResultNo = true) const {
|
|
|
|
printValueIDImpl(value, printResultNo, os);
|
|
|
|
}
|
2018-07-21 00:28:54 +08:00
|
|
|
|
|
|
|
private:
|
2019-08-24 01:35:24 +08:00
|
|
|
void printValueIDImpl(Value *value, bool printResultNo,
|
|
|
|
raw_ostream &stream) const;
|
|
|
|
|
2019-07-10 01:40:29 +08:00
|
|
|
/// Uniques the given value name within the printer. If the given name
|
|
|
|
/// conflicts, it is automatically renamed.
|
|
|
|
StringRef uniqueValueName(StringRef name);
|
|
|
|
|
|
|
|
/// This is the value ID for each SSA value. If this returns ~0, then the
|
|
|
|
/// valueID has an entry in valueNames.
|
2019-03-24 06:09:06 +08:00
|
|
|
DenseMap<Value *, unsigned> valueIDs;
|
|
|
|
DenseMap<Value *, StringRef> valueNames;
|
2018-08-02 01:43:18 +08:00
|
|
|
|
2019-07-10 01:40:29 +08:00
|
|
|
/// This is the block ID for each block in the current.
|
2019-03-22 08:53:00 +08:00
|
|
|
DenseMap<Block *, unsigned> blockIDs;
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2018-08-02 01:43:18 +08:00
|
|
|
/// This keeps track of all of the non-numeric names that are in flight,
|
|
|
|
/// allowing us to check for duplicates.
|
2019-07-10 01:40:29 +08:00
|
|
|
/// Note: the value of the map is unused.
|
|
|
|
llvm::ScopedHashTable<StringRef, char> usedNames;
|
|
|
|
llvm::BumpPtrAllocator usedNameAllocator;
|
2018-08-02 01:43:18 +08:00
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
// This is the current indentation level for nested structures.
|
|
|
|
unsigned currentIndent = 0;
|
|
|
|
|
2018-08-02 01:43:18 +08:00
|
|
|
/// This is the next value ID to assign in numbering.
|
2018-07-21 00:28:54 +08:00
|
|
|
unsigned nextValueID = 0;
|
2019-07-10 01:40:29 +08:00
|
|
|
/// This is the next ID to assign to a region entry block argument.
|
2018-08-02 01:43:18 +08:00
|
|
|
unsigned nextArgumentID = 0;
|
|
|
|
/// This is the next ID to assign when a name conflict is detected.
|
|
|
|
unsigned nextConflictID = 0;
|
2018-07-10 08:42:46 +08:00
|
|
|
};
|
2018-07-24 02:44:40 +08:00
|
|
|
} // end anonymous namespace
|
2018-07-10 08:42:46 +08:00
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
OperationPrinter::OperationPrinter(Operation *op, ModulePrinter &other)
|
|
|
|
: ModulePrinter(other) {
|
2019-07-10 01:40:29 +08:00
|
|
|
if (op->getNumResults() != 0)
|
|
|
|
numberValueID(op->getResult(0));
|
2019-07-04 04:21:24 +08:00
|
|
|
for (auto ®ion : op->getRegions())
|
2019-07-10 01:40:29 +08:00
|
|
|
numberValuesInRegion(region);
|
2019-07-04 04:21:24 +08:00
|
|
|
}
|
2018-07-10 08:42:46 +08:00
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
OperationPrinter::OperationPrinter(Region *region, ModulePrinter &other)
|
|
|
|
: ModulePrinter(other) {
|
2019-07-10 01:40:29 +08:00
|
|
|
numberValuesInRegion(*region);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Number all of the SSA values in the specified region.
|
|
|
|
void OperationPrinter::numberValuesInRegion(Region ®ion) {
|
|
|
|
// Save the current value ids to allow for numbering values in sibling regions
|
|
|
|
// the same.
|
|
|
|
unsigned curValueID = nextValueID;
|
|
|
|
unsigned curArgumentID = nextArgumentID;
|
|
|
|
unsigned curConflictID = nextConflictID;
|
|
|
|
|
|
|
|
// Push a new used names scope.
|
|
|
|
llvm::ScopedHashTable<StringRef, char>::ScopeTy usedNamesScope(usedNames);
|
|
|
|
|
|
|
|
// 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++;
|
2018-12-29 03:41:56 +08:00
|
|
|
numberValuesInBlock(block);
|
2019-07-10 01:40:29 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the original value ids.
|
|
|
|
nextValueID = curValueID;
|
|
|
|
nextArgumentID = curArgumentID;
|
|
|
|
nextConflictID = curConflictID;
|
2018-07-10 08:42:46 +08:00
|
|
|
}
|
|
|
|
|
2019-07-10 01:40:29 +08:00
|
|
|
/// Number all of the SSA values in the specified block, without traversing
|
|
|
|
/// nested regions.
|
2019-07-04 04:21:24 +08:00
|
|
|
void OperationPrinter::numberValuesInBlock(Block &block) {
|
2019-07-10 01:40:29 +08:00
|
|
|
// Number the block arguments.
|
2018-12-29 03:41:56 +08:00
|
|
|
for (auto *arg : block.getArguments())
|
2018-07-23 06:45:24 +08:00
|
|
|
numberValueID(arg);
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2019-07-10 01:40:29 +08:00
|
|
|
// We number operation that have results, and we only number the first result.
|
|
|
|
for (auto &op : block)
|
2019-03-28 23:24:38 +08:00
|
|
|
if (op.getNumResults() != 0)
|
|
|
|
numberValueID(op.getResult(0));
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void OperationPrinter::numberValueID(Value *value) {
|
2018-12-29 03:41:56 +08:00
|
|
|
assert(!valueIDs.count(value) && "Value numbered multiple times");
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
SmallString<32> specialNameBuffer;
|
|
|
|
llvm::raw_svector_ostream specialName(specialNameBuffer);
|
2018-07-23 06:45:24 +08:00
|
|
|
|
2019-08-22 07:50:30 +08:00
|
|
|
// Check to see if this value requested a special name.
|
|
|
|
auto *op = value->getDefiningOp();
|
|
|
|
if (state && op) {
|
|
|
|
if (auto *interface = state->getOpAsmInterface(op->getDialect()))
|
|
|
|
interface->getOpResultName(op, specialName);
|
2018-07-23 06:45:24 +08:00
|
|
|
}
|
2018-07-28 02:10:12 +08:00
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
if (specialNameBuffer.empty()) {
|
2019-10-15 07:21:17 +08:00
|
|
|
auto *blockArg = dyn_cast<BlockArgument>(value);
|
|
|
|
if (!blockArg) {
|
|
|
|
// This is an uninteresting operation result, give it a boring number and
|
|
|
|
// be done with it.
|
2018-12-29 03:41:56 +08:00
|
|
|
valueIDs[value] = nextValueID++;
|
|
|
|
return;
|
2019-10-15 07:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if this is an argument to the entry block of a region, give it
|
|
|
|
// an 'arg' name.
|
|
|
|
if (auto *block = blockArg->getOwner()) {
|
|
|
|
auto *parentRegion = block->getParent();
|
|
|
|
if (parentRegion && block == &parentRegion->front())
|
|
|
|
specialName << "arg" << nextArgumentID++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise number it normally.
|
|
|
|
if (specialNameBuffer.empty()) {
|
2018-12-29 03:41:56 +08:00
|
|
|
valueIDs[value] = nextValueID++;
|
|
|
|
return;
|
|
|
|
}
|
2018-07-28 02:10:12 +08:00
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
// Ok, this value had an interesting name. Remember it with a sentinel.
|
|
|
|
valueIDs[value] = nameSentinel;
|
2019-07-10 01:40:29 +08:00
|
|
|
valueNames[value] = uniqueValueName(specialName.str());
|
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2019-07-10 01:40:29 +08:00
|
|
|
/// Uniques the given value name within the printer. If the given name
|
|
|
|
/// conflicts, it is automatically renamed.
|
|
|
|
StringRef OperationPrinter::uniqueValueName(StringRef name) {
|
|
|
|
// 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('_');
|
2019-09-23 17:33:51 +08:00
|
|
|
while (true) {
|
2019-07-10 01:40:29 +08:00
|
|
|
probeName.resize(name.size() + 1);
|
|
|
|
probeName += llvm::utostr(nextConflictID++);
|
|
|
|
if (!usedNames.count(probeName)) {
|
|
|
|
name = StringRef(probeName).copy(usedNameAllocator);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
2018-11-16 01:56:06 +08:00
|
|
|
}
|
2019-07-10 01:40:29 +08:00
|
|
|
|
|
|
|
usedNames.insert(name, char());
|
|
|
|
return name;
|
2018-07-27 09:09:20 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void OperationPrinter::print(Block *block, bool printBlockArgs,
|
|
|
|
bool printBlockTerminator) {
|
2019-01-29 13:23:53 +08:00
|
|
|
// Print the block label and argument list if requested.
|
2019-01-27 04:40:12 +08:00
|
|
|
if (printBlockArgs) {
|
2018-12-29 03:41:56 +08:00
|
|
|
os.indent(currentIndent);
|
2018-12-29 05:07:39 +08:00
|
|
|
printBlockName(block);
|
2018-12-29 03:41:56 +08:00
|
|
|
|
|
|
|
// Print the argument list if non-empty.
|
|
|
|
if (!block->args_empty()) {
|
|
|
|
os << '(';
|
2019-03-22 08:53:00 +08:00
|
|
|
interleaveComma(block->getArguments(), [&](BlockArgument *arg) {
|
2018-12-29 03:41:56 +08:00
|
|
|
printValueID(arg);
|
|
|
|
os << ": ";
|
|
|
|
printType(arg->getType());
|
|
|
|
});
|
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
os << ':';
|
|
|
|
|
|
|
|
// Print out some context information about the predecessors of this block.
|
2019-07-09 02:20:26 +08:00
|
|
|
if (!block->getParent()) {
|
|
|
|
os << "\t// block is not in a region!";
|
2018-12-29 03:41:56 +08:00
|
|
|
} else if (block->hasNoPredecessors()) {
|
2018-12-30 05:56:57 +08:00
|
|
|
os << "\t// no predecessors";
|
2018-12-29 03:41:56 +08:00
|
|
|
} else if (auto *pred = block->getSinglePredecessor()) {
|
|
|
|
os << "\t// pred: ";
|
2018-12-29 05:07:39 +08:00
|
|
|
printBlockName(pred);
|
2018-12-29 03:41:56 +08:00
|
|
|
} 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.
|
2019-03-22 08:53:00 +08:00
|
|
|
SmallVector<std::pair<unsigned, Block *>, 4> predIDs;
|
2018-12-29 03:41:56 +08:00
|
|
|
for (auto *pred : block->getPredecessors())
|
2018-12-31 08:22:50 +08:00
|
|
|
predIDs.push_back({getBlockID(pred), pred});
|
2018-12-29 03:41:56 +08:00
|
|
|
llvm::array_pod_sort(predIDs.begin(), predIDs.end());
|
|
|
|
|
|
|
|
os << "\t// " << predIDs.size() << " preds: ";
|
|
|
|
|
2019-03-22 08:53:00 +08:00
|
|
|
interleaveComma(predIDs, [&](std::pair<unsigned, Block *> pred) {
|
2018-12-31 08:22:50 +08:00
|
|
|
printBlockName(pred.second);
|
|
|
|
});
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
os << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
currentIndent += indentWidth;
|
2019-03-27 20:11:58 +08:00
|
|
|
auto range = llvm::make_range(
|
|
|
|
block->getOperations().begin(),
|
|
|
|
std::prev(block->getOperations().end(), printBlockTerminator ? 0 : 1));
|
2019-03-28 23:24:38 +08:00
|
|
|
for (auto &op : range) {
|
|
|
|
print(&op);
|
2018-12-29 03:41:56 +08:00
|
|
|
os << '\n';
|
2018-07-15 15:06:54 +08:00
|
|
|
}
|
2018-12-29 03:41:56 +08:00
|
|
|
currentIndent -= indentWidth;
|
2018-07-14 04:03:13 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void OperationPrinter::print(Operation *op) {
|
2018-12-29 03:41:56 +08:00
|
|
|
os.indent(currentIndent);
|
2019-03-28 23:24:38 +08:00
|
|
|
printOperation(op);
|
|
|
|
printTrailingLocation(op->getLoc());
|
2018-07-21 00:35:47 +08:00
|
|
|
}
|
2018-07-04 08:51:28 +08:00
|
|
|
|
2019-08-24 01:35:24 +08:00
|
|
|
void OperationPrinter::printValueIDImpl(Value *value, bool printResultNo,
|
|
|
|
raw_ostream &stream) const {
|
2019-08-30 03:14:02 +08:00
|
|
|
if (!value) {
|
|
|
|
stream << "<<NULL>>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
int resultNo = -1;
|
|
|
|
auto lookupValue = value;
|
|
|
|
|
2019-03-28 23:24:38 +08:00
|
|
|
// If this is a reference to the result of a multi-result operation or
|
|
|
|
// operation, print out the # identifier and make sure to map our lookup
|
|
|
|
// to the first result of the operation.
|
2019-03-27 08:05:09 +08:00
|
|
|
if (auto *result = dyn_cast<OpResult>(value)) {
|
2018-12-29 03:41:56 +08:00
|
|
|
if (result->getOwner()->getNumResults() != 1) {
|
|
|
|
resultNo = result->getResultNumber();
|
|
|
|
lookupValue = result->getOwner()->getResult(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = valueIDs.find(lookupValue);
|
|
|
|
if (it == valueIDs.end()) {
|
2019-08-24 01:35:24 +08:00
|
|
|
stream << "<<INVALID SSA VALUE>>";
|
2018-12-29 03:41:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:35:24 +08:00
|
|
|
stream << '%';
|
2018-12-29 03:41:56 +08:00
|
|
|
if (it->second != nameSentinel) {
|
2019-08-24 01:35:24 +08:00
|
|
|
stream << it->second;
|
2018-12-29 03:41:56 +08:00
|
|
|
} else {
|
|
|
|
auto nameIt = valueNames.find(lookupValue);
|
|
|
|
assert(nameIt != valueNames.end() && "Didn't have a name entry?");
|
2019-08-24 01:35:24 +08:00
|
|
|
stream << nameIt->second;
|
2018-12-29 03:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resultNo != -1 && printResultNo)
|
2019-08-24 01:35:24 +08:00
|
|
|
stream << '#' << resultNo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 OperationPrinter::shadowRegionArgs(Region ®ion,
|
|
|
|
ArrayRef<Value *> 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");
|
|
|
|
|
|
|
|
SmallVector<char, 16> nameStr;
|
|
|
|
for (unsigned i = 0, e = namesToUse.size(); i != e; ++i) {
|
|
|
|
auto *nameToUse = namesToUse[i];
|
|
|
|
if (nameToUse == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto *nameToReplace = region.front().getArgument(i);
|
|
|
|
|
|
|
|
nameStr.clear();
|
|
|
|
llvm::raw_svector_ostream nameStream(nameStr);
|
|
|
|
printValueIDImpl(nameToUse, /*printResultNo=*/true, nameStream);
|
|
|
|
|
|
|
|
// Entry block arguments should already have a pretty "arg" name.
|
|
|
|
assert(valueIDs[nameToReplace] == nameSentinel);
|
|
|
|
|
|
|
|
// Use the name without the leading %.
|
|
|
|
auto name = StringRef(nameStream.str()).drop_front();
|
|
|
|
|
|
|
|
// Overwrite the name.
|
|
|
|
valueNames[nameToReplace] = name.copy(usedNameAllocator);
|
|
|
|
}
|
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()) {
|
2018-12-29 03:41:56 +08:00
|
|
|
printValueID(op->getResult(0), /*printResultNo=*/false);
|
2019-03-29 05:58:52 +08:00
|
|
|
if (numResults > 1)
|
|
|
|
os << ':' << numResults;
|
2018-12-29 03:41:56 +08:00
|
|
|
os << " = ";
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
// TODO(riverriddle): FuncOp cannot be round-tripped currently, as
|
|
|
|
// FunctionType cannot be used in a TypeAttr.
|
2019-10-08 04:54:16 +08:00
|
|
|
if (printerFlags.shouldPrintGenericOpForm() && !isa<FuncOp>(op))
|
2019-02-05 05:01:06 +08:00
|
|
|
return printGenericOp(op);
|
|
|
|
|
2018-12-29 03:41:56 +08:00
|
|
|
// Check to see if this is a known operation. If so, use the registered
|
|
|
|
// custom printer hook.
|
|
|
|
if (auto *opInfo = op->getAbstractOperation()) {
|
2019-09-21 11:43:02 +08:00
|
|
|
opInfo->printAssembly(op, *this);
|
2018-12-29 03:41:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 << "\"(";
|
|
|
|
|
2019-01-10 04:28:30 +08:00
|
|
|
// Get the list of operands that are not successor operands.
|
|
|
|
unsigned totalNumSuccessorOperands = 0;
|
|
|
|
unsigned numSuccessors = op->getNumSuccessors();
|
|
|
|
for (unsigned i = 0; i < numSuccessors; ++i)
|
|
|
|
totalNumSuccessorOperands += op->getNumSuccessorOperands(i);
|
|
|
|
unsigned numProperOperands = op->getNumOperands() - totalNumSuccessorOperands;
|
2019-03-24 06:09:06 +08:00
|
|
|
SmallVector<Value *, 8> properOperands(
|
2019-01-10 04:28:30 +08:00
|
|
|
op->operand_begin(), std::next(op->operand_begin(), numProperOperands));
|
|
|
|
|
2019-03-24 06:09:06 +08:00
|
|
|
interleaveComma(properOperands, [&](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.
|
2019-02-09 01:52:26 +08:00
|
|
|
if (numSuccessors != 0) {
|
2019-01-10 04:28:30 +08:00
|
|
|
os << '[';
|
|
|
|
for (unsigned i = 0; i < numSuccessors; ++i) {
|
|
|
|
if (i != 0)
|
|
|
|
os << ", ";
|
|
|
|
printSuccessorAndUseList(op, i);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void OperationPrinter::printSuccessorAndUseList(Operation *term,
|
|
|
|
unsigned index) {
|
2018-12-29 05:07:39 +08:00
|
|
|
printBlockName(term->getSuccessor(index));
|
2018-12-29 03:41:56 +08:00
|
|
|
|
|
|
|
auto succOperands = term->getSuccessorOperands(index);
|
|
|
|
if (succOperands.begin() == succOperands.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
os << '(';
|
|
|
|
interleaveComma(succOperands,
|
2019-03-24 06:09:06 +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,
|
|
|
|
[this](Value *operand) { printType(operand->getType()); });
|
2018-12-29 03:41:56 +08:00
|
|
|
os << ')';
|
2018-07-04 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void ModulePrinter::print(ModuleOp module) {
|
|
|
|
// Output the aliases at the top level.
|
2019-08-22 03:16:23 +08:00
|
|
|
if (state) {
|
|
|
|
state->printAttributeAliases(os);
|
|
|
|
state->printTypeAliases(os);
|
|
|
|
}
|
2019-07-04 04:21:24 +08:00
|
|
|
|
2019-07-11 06:16:52 +08:00
|
|
|
// Print the module.
|
|
|
|
OperationPrinter(module, *this).print(module);
|
|
|
|
os << '\n';
|
2019-07-04 04:21:24 +08:00
|
|
|
}
|
2018-07-18 07:56:54 +08:00
|
|
|
|
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 {
|
2019-01-08 09:34:26 +08:00
|
|
|
if (expr == nullptr) {
|
|
|
|
os << "null affine expr";
|
|
|
|
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 {
|
2019-01-08 09:34:26 +08:00
|
|
|
if (map == nullptr) {
|
|
|
|
os << "null affine map";
|
|
|
|
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.
|
|
|
|
assert(isa<BlockArgument>(*this));
|
|
|
|
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
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
void Operation::print(raw_ostream &os, OpPrintingFlags flags) {
|
2019-07-04 04:21:24 +08:00
|
|
|
// Handle top-level operations.
|
|
|
|
if (!getParent()) {
|
2019-10-08 04:54:16 +08:00
|
|
|
ModulePrinter modulePrinter(os, flags);
|
2019-07-04 04:21:24 +08:00
|
|
|
OperationPrinter(this, modulePrinter).print(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-10 11:07:25 +08:00
|
|
|
auto region = getParentRegion();
|
2019-07-04 04:21:24 +08:00
|
|
|
if (!region) {
|
2018-08-04 02:12:34 +08:00
|
|
|
os << "<<UNLINKED INSTRUCTION>>\n";
|
|
|
|
return;
|
|
|
|
}
|
2018-12-29 03:41:56 +08:00
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
// Get the top-level region.
|
2019-08-10 11:07:25 +08:00
|
|
|
while (auto *nextRegion = region->getParentRegion())
|
2019-07-04 04:21:24 +08:00
|
|
|
region = nextRegion;
|
|
|
|
|
2019-08-22 07:50:30 +08:00
|
|
|
ModuleState state(getContext());
|
2019-10-08 04:54:16 +08:00
|
|
|
ModulePrinter modulePrinter(os, flags, &state);
|
2019-07-04 04:21:24 +08:00
|
|
|
OperationPrinter(region, modulePrinter).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() {
|
2018-07-21 00:35:47 +08:00
|
|
|
print(llvm::errs());
|
|
|
|
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) {
|
2019-07-04 04:21:24 +08:00
|
|
|
auto region = getParent();
|
|
|
|
if (!region) {
|
2018-08-04 02:12:34 +08:00
|
|
|
os << "<<UNLINKED BLOCK>>\n";
|
|
|
|
return;
|
|
|
|
}
|
2018-12-28 03:07:34 +08:00
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
// Get the top-level region.
|
2019-08-10 11:07:25 +08:00
|
|
|
while (auto *nextRegion = region->getParentRegion())
|
2019-07-04 04:21:24 +08:00
|
|
|
region = nextRegion;
|
|
|
|
|
2019-08-22 07:50:30 +08:00
|
|
|
ModuleState state(region->getContext());
|
2019-10-08 04:54:16 +08:00
|
|
|
ModulePrinter modulePrinter(os, /*flags=*/llvm::None, &state);
|
2019-07-04 04:21:24 +08:00
|
|
|
OperationPrinter(region, modulePrinter).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) {
|
2019-07-04 04:21:24 +08:00
|
|
|
auto region = getParent();
|
|
|
|
if (!region) {
|
2018-09-22 05:40:36 +08:00
|
|
|
os << "<<UNLINKED BLOCK>>\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
// Get the top-level region.
|
2019-08-10 11:07:25 +08:00
|
|
|
while (auto *nextRegion = region->getParentRegion())
|
2019-07-04 04:21:24 +08:00
|
|
|
region = nextRegion;
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2019-08-22 03:16:23 +08:00
|
|
|
ModulePrinter modulePrinter(os);
|
2019-07-04 04:21:24 +08:00
|
|
|
OperationPrinter(region, modulePrinter).printBlockName(this);
|
|
|
|
}
|
2018-07-19 01:16:05 +08:00
|
|
|
|
2019-10-08 04:54:16 +08:00
|
|
|
void ModuleOp::print(raw_ostream &os, OpPrintingFlags flags) {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(getContext());
|
2019-07-03 01:49:17 +08:00
|
|
|
state.initialize(*this);
|
2019-10-08 04:54:16 +08:00
|
|
|
ModulePrinter(os, flags, &state).print(*this);
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
void ModuleOp::dump() { print(llvm::errs()); }
|