enum : unsigned { NameSentinel = ~0U };
SSANameState(Operation *op, const OpPrintingFlags &printerFlags);
+ SSANameState() = default;
/// Print the SSA identifier for the given value to 'stream'. If
/// 'printResultNo' is true, it also presents the result number ('#' number)
AsmState::LocationMap *locationMap)
: interfaces(op->getContext()), nameState(op, printerFlags),
printerFlags(printerFlags), locationMap(locationMap) {}
+ explicit AsmStateImpl(MLIRContext *ctx, const OpPrintingFlags &printerFlags,
+ AsmState::LocationMap *locationMap)
+ : interfaces(ctx), printerFlags(printerFlags), locationMap(locationMap) {}
/// Initialize the alias state to enable the printing of aliases.
void initializeAliases(Operation *op) {
(*locationMap)[op] = std::make_pair(line, col);
}
+ /// Return the referenced dialect resources within the printer.
+ DenseMap<Dialect *, SetVector<AsmDialectResourceHandle>> &
+ getDialectResources() {
+ return dialectResources;
+ }
+
private:
/// Collection of OpAsm interfaces implemented in the context.
DialectInterfaceCollection<OpAsmDialectInterface> interfaces;
/// A collection of non-dialect resource printers.
SmallVector<std::unique_ptr<AsmResourcePrinter>> externalResourcePrinters;
+ /// A set of dialect resources that were referenced during printing.
+ DenseMap<Dialect *, SetVector<AsmDialectResourceHandle>> dialectResources;
+
/// The state used for attribute and type aliases.
AliasState aliasState;
LocationMap *locationMap)
: impl(std::make_unique<AsmStateImpl>(
op, verifyOpAndAdjustFlags(op, printerFlags), locationMap)) {}
+AsmState::AsmState(MLIRContext *ctx, const OpPrintingFlags &printerFlags,
+ LocationMap *locationMap)
+ : impl(std::make_unique<AsmStateImpl>(ctx, printerFlags, locationMap)) {}
AsmState::~AsmState() = default;
const OpPrintingFlags &AsmState::getPrinterFlags() const {
impl->externalResourcePrinters.emplace_back(std::move(printer));
}
+DenseMap<Dialect *, SetVector<AsmDialectResourceHandle>> &
+AsmState::getDialectResources() const {
+ return impl->getDialectResources();
+}
+
//===----------------------------------------------------------------------===//
// AsmPrinter::Impl
//===----------------------------------------------------------------------===//
namespace mlir {
class AsmPrinter::Impl {
public:
- Impl(raw_ostream &os, OpPrintingFlags flags = llvm::None,
- AsmStateImpl *state = nullptr)
- : os(os), printerFlags(flags), state(state) {}
- explicit Impl(Impl &other)
- : Impl(other.os, other.printerFlags, other.state) {}
+ Impl(raw_ostream &os, AsmStateImpl &state)
+ : os(os), state(state), printerFlags(state.getPrinterFlags()) {}
+ explicit Impl(Impl &other) : Impl(other.os, other.state) {}
/// Returns the output stream of the printer.
raw_ostream &getStream() { return os; }
void printResourceHandle(const AsmDialectResourceHandle &resource) {
auto *interface = cast<OpAsmDialectInterface>(resource.getDialect());
os << interface->getResourceKey(resource);
- dialectResources[resource.getDialect()].insert(resource);
+ state.getDialectResources()[resource.getDialect()].insert(resource);
}
void printAffineMap(AffineMap map);
/// The output stream for the printer.
raw_ostream &os;
+ /// An underlying assembly printer state.
+ AsmStateImpl &state;
+
/// A set of flags to control the printer's behavior.
OpPrintingFlags printerFlags;
- /// An optional printer state for the module.
- AsmStateImpl *state;
-
/// A tracker for the number of new lines emitted during printing.
NewLineCounter newLine;
-
- /// A set of dialect resources that were referenced during printing.
- DenseMap<Dialect *, SetVector<AsmDialectResourceHandle>> dialectResources;
};
} // namespace mlir
return printLocationInternal(loc, /*pretty=*/true);
os << "loc(";
- if (!allowAlias || !state || failed(state->getAliasState().getAlias(loc, os)))
+ if (!allowAlias || failed(printAlias(loc)))
printLocationInternal(loc);
os << ')';
}
}
LogicalResult AsmPrinter::Impl::printAlias(Attribute attr) {
- return success(state && succeeded(state->getAliasState().getAlias(attr, os)));
+ return state.getAliasState().getAlias(attr, os);
}
LogicalResult AsmPrinter::Impl::printAlias(Type type) {
- return success(state && succeeded(state->getAliasState().getAlias(type, os)));
+ return state.getAliasState().getAlias(type, os);
}
void AsmPrinter::Impl::printAttribute(Attribute attr,
}
// Try to print an alias for this type.
- if (state && succeeded(state->getAliasState().getAlias(type, os)))
+ if (succeeded(printAlias(type)))
return;
TypeSwitch<Type>(type)
std::string attrName;
{
llvm::raw_string_ostream attrNameStr(attrName);
- Impl subPrinter(attrNameStr, printerFlags, state);
+ Impl subPrinter(attrNameStr, state);
DialectAsmPrinter printer(subPrinter);
dialect.printAttribute(attr, printer);
-
- // FIXME: Delete this when we no longer require a nested printer.
- for (auto &it : subPrinter.dialectResources)
- for (const auto &resource : it.second)
- dialectResources[it.first].insert(resource);
}
printDialectSymbol(os, "#", dialect.getNamespace(), attrName);
}
std::string typeName;
{
llvm::raw_string_ostream typeNameStr(typeName);
- Impl subPrinter(typeNameStr, printerFlags, state);
+ Impl subPrinter(typeNameStr, state);
DialectAsmPrinter printer(subPrinter);
dialect.printType(type, printer);
-
- // FIXME: Delete this when we no longer require a nested printer.
- for (auto &it : subPrinter.dialectResources)
- for (const auto &resource : it.second)
- dialectResources[it.first].insert(resource);
}
printDialectSymbol(os, "!", dialect.getNamespace(), typeName);
}
using Impl::printType;
explicit OperationPrinter(raw_ostream &os, AsmStateImpl &state)
- : Impl(os, state.getPrinterFlags(), &state),
- OpAsmPrinter(static_cast<Impl &>(*this)) {}
+ : Impl(os, state), OpAsmPrinter(static_cast<Impl &>(*this)) {}
/// Print the given top-level operation.
void printTopLevelOperation(Operation *op);
/// operations. If any entry in namesToUse is null, the corresponding
/// argument name is left alone.
void shadowRegionArgs(Region ®ion, ValueRange namesToUse) override {
- state->getSSANameState().shadowRegionArgs(region, namesToUse);
+ state.getSSANameState().shadowRegionArgs(region, namesToUse);
}
/// Print the given affine map with the symbol and dimension operands printed
void OperationPrinter::printTopLevelOperation(Operation *op) {
// Output the aliases at the top level that can't be deferred.
- state->getAliasState().printNonDeferredAliases(os, newLine);
+ state.getAliasState().printNonDeferredAliases(os, newLine);
// Print the module.
print(op);
os << newLine;
// Output the aliases at the top level that can be deferred.
- state->getAliasState().printDeferredAliases(os, newLine);
+ state.getAliasState().printDeferredAliases(os, newLine);
// Output any file level metadata.
printFileMetadataDictionary(op);
// Print the `dialect_resources` section if we have any dialects with
// resources.
- for (const OpAsmDialectInterface &interface : state->getDialectInterfaces()) {
+ for (const OpAsmDialectInterface &interface : state.getDialectInterfaces()) {
+ auto &dialectResources = state.getDialectResources();
StringRef name = interface.getDialect()->getNamespace();
auto it = dialectResources.find(interface.getDialect());
if (it != dialectResources.end())
// Print the `external_resources` section if we have any external clients with
// resources.
hadResource = false;
- for (const auto &printer : state->getResourcePrinters())
+ for (const auto &printer : state.getResourcePrinters())
processProvider("external", printer.getName(), printer);
if (hadResource)
os << newLine << " }";
void OperationPrinter::print(Operation *op) {
// Track the location of this operation.
- state->registerOperationLocation(op, newLine.curLine, currentIndent);
+ state.registerOperationLocation(op, newLine.curLine, currentIndent);
os.indent(currentIndent);
printOperation(op);
};
// Check to see if this operation has multiple result groups.
- ArrayRef<int> resultGroups = state->getSSANameState().getOpResultGroups(op);
+ ArrayRef<int> resultGroups = state.getSSANameState().getOpResultGroups(op);
if (!resultGroups.empty()) {
// Interleave the groups excluding the last one, this one will be handled
// separately.
}
void OperationPrinter::printBlockName(Block *block) {
- os << state->getSSANameState().getBlockInfo(block).name;
+ os << state.getSSANameState().getBlockInfo(block).name;
}
void OperationPrinter::print(Block *block, bool printBlockArgs,
// whatever order the use-list is in, so gather and sort them.
SmallVector<BlockInfo, 4> predIDs;
for (auto *pred : block->getPredecessors())
- predIDs.push_back(state->getSSANameState().getBlockInfo(pred));
+ predIDs.push_back(state.getSSANameState().getBlockInfo(pred));
llvm::sort(predIDs, [](BlockInfo lhs, BlockInfo rhs) {
return lhs.ordering < rhs.ordering;
});
void OperationPrinter::printValueID(Value value, bool printResultNo,
raw_ostream *streamOverride) const {
- state->getSSANameState().printValueID(value, printResultNo,
- streamOverride ? *streamOverride : os);
+ state.getSSANameState().printValueID(value, printResultNo,
+ streamOverride ? *streamOverride : os);
}
void OperationPrinter::printOperationID(Operation *op,
raw_ostream *streamOverride) const {
- state->getSSANameState().printOperationID(op, streamOverride ? *streamOverride
- : os);
+ state.getSSANameState().printOperationID(op, streamOverride ? *streamOverride
+ : os);
}
void OperationPrinter::printSuccessor(Block *successor) {
//===----------------------------------------------------------------------===//
void Attribute::print(raw_ostream &os) const {
- AsmPrinter::Impl(os).printAttribute(*this);
+ if (!*this) {
+ os << "<<NULL ATTRIBUTE>>";
+ return;
+ }
+
+ AsmState state(getContext());
+ print(os, state);
+}
+void Attribute::print(raw_ostream &os, AsmState &state) const {
+ AsmPrinter::Impl(os, state.getImpl()).printAttribute(*this);
}
void Attribute::dump() const {
}
void Type::print(raw_ostream &os) const {
- AsmPrinter::Impl(os).printType(*this);
+ if (!*this) {
+ os << "<<NULL TYPE>>";
+ return;
+ }
+
+ AsmState state(getContext());
+ print(os, state);
+}
+void Type::print(raw_ostream &os, AsmState &state) const {
+ AsmPrinter::Impl(os, state.getImpl()).printType(*this);
}
void Type::dump() const { print(llvm::errs()); }
os << "<<NULL AFFINE EXPR>>";
return;
}
- AsmPrinter::Impl(os).printAffineExpr(*this);
+ AsmState state(getContext());
+ AsmPrinter::Impl(os, state.getImpl()).printAffineExpr(*this);
}
void AffineExpr::dump() const {
os << "<<NULL AFFINE MAP>>";
return;
}
- AsmPrinter::Impl(os).printAffineMap(*this);
+ AsmState state(getContext());
+ AsmPrinter::Impl(os, state.getImpl()).printAffineMap(*this);
}
void IntegerSet::print(raw_ostream &os) const {
- AsmPrinter::Impl(os).printIntegerSet(*this);
+ AsmState state(getContext());
+ AsmPrinter::Impl(os, state.getImpl()).printIntegerSet(*this);
}
void Value::print(raw_ostream &os) { print(os, OpPrintingFlags()); }