From b2cc40fd67f6a8d909a0b4b0e8fe0dd7c45562d7 Mon Sep 17 00:00:00 2001 From: River Riddle Date: Tue, 24 May 2022 01:55:27 -0700 Subject: [PATCH] [mlir:Printer][NFC] Add utility methods for printing escaped/hex strings This simplifies quite a few cases where we manually duplicate the escaping logic. --- mlir/lib/IR/AsmPrinter.cpp | 65 ++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp index 62aab6dd9306..ed67c8eeb9b6 100644 --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -1433,6 +1433,13 @@ protected: void printDialectAttribute(Attribute attr); void printDialectType(Type type); + /// Print an escaped string, wrapped with "". + void printEscapedString(StringRef str); + + /// Print a hex string, wrapped with "". + void printHexString(StringRef str); + void printHexString(ArrayRef data); + /// This enum is used to represent the binding strength of the enclosing /// context that an AffineExprStorage is being printed in, so we can /// intelligently produce parens. @@ -1479,19 +1486,14 @@ void AsmPrinter::Impl::printLocationInternal(LocationAttr loc, bool pretty) { os << "unknown"; }) .Case([&](FileLineColLoc loc) { - if (pretty) { + if (pretty) os << loc.getFilename().getValue(); - } else { - os << "\""; - printEscapedString(loc.getFilename(), os); - os << "\""; - } + else + printEscapedString(loc.getFilename()); os << ':' << loc.getLine() << ':' << loc.getColumn(); }) .Case([&](NameLoc loc) { - os << '\"'; - printEscapedString(loc.getName(), os); - os << '\"'; + printEscapedString(loc.getName()); // Print the child if it isn't unknown. auto childLoc = loc.getChildLoc(); @@ -1800,9 +1802,7 @@ void AsmPrinter::Impl::printAttribute(Attribute attr, return; } else if (auto strAttr = attr.dyn_cast()) { - os << '"'; - printEscapedString(strAttr.getValue(), os); - os << '"'; + printEscapedString(strAttr.getValue()); } else if (auto arrayAttr = attr.dyn_cast()) { os << '['; @@ -1841,8 +1841,9 @@ void AsmPrinter::Impl::printAttribute(Attribute attr, if (printerFlags.shouldElideElementsAttr(opaqueAttr)) { printElidedElementsAttr(os); } else { - os << "opaque<" << opaqueAttr.getDialect() << ", \"0x" - << llvm::toHex(opaqueAttr.getValue()) << "\">"; + os << "opaque<" << opaqueAttr.getDialect() << ", "; + printHexString(opaqueAttr.getValue()); + os << ">"; } } else if (auto intOrFpEltAttr = attr.dyn_cast()) { @@ -1974,12 +1975,9 @@ void AsmPrinter::Impl::printDenseIntOrFPElementsAttr( MutableArrayRef convRawData(outDataVec); DenseIntOrFPElementsAttr::convertEndianOfArrayRefForBEmachine( rawData, convRawData, type); - os << '"' << "0x" - << llvm::toHex(StringRef(convRawData.data(), convRawData.size())) - << "\""; + printHexString(convRawData); } else { - os << '"' << "0x" - << llvm::toHex(StringRef(rawData.data(), rawData.size())) << "\""; + printHexString(rawData); } return; @@ -2030,11 +2028,7 @@ void AsmPrinter::Impl::printDenseIntOrFPElementsAttr( void AsmPrinter::Impl::printDenseStringElementsAttr( DenseStringElementsAttr attr) { ArrayRef data = attr.getRawStringData(); - auto printFn = [&](unsigned index) { - os << "\""; - printEscapedString(data[index], os); - os << "\""; - }; + auto printFn = [&](unsigned index) { printEscapedString(data[index]); }; printDenseElementsAttrImpl(attr.isSplat(), attr.getType(), os, printFn); } @@ -2240,6 +2234,19 @@ void AsmPrinter::Impl::printDialectType(Type type) { printDialectSymbol(os, "!", dialect.getNamespace(), typeName); } +void AsmPrinter::Impl::printEscapedString(StringRef str) { + os << "\""; + llvm::printEscapedString(str, os); + os << "\""; +} + +void AsmPrinter::Impl::printHexString(StringRef str) { + os << "\"0x" << llvm::toHex(str) << "\""; +} +void AsmPrinter::Impl::printHexString(ArrayRef data) { + printHexString(StringRef(data.data(), data.size())); +} + //===--------------------------------------------------------------------===// // AsmPrinter //===--------------------------------------------------------------------===// @@ -2722,7 +2729,8 @@ void OperationPrinter::printOperation(Operation *op) { // ambiguities. if (name.count('.') == 1) name.consume_front((defaultDialectStack.back() + ".").str()); - printEscapedString(name, os); + os << name; + // Print the rest of the op now. opPrinter(op, *this); return; @@ -2809,11 +2817,8 @@ void OperationPrinter::printUserIDs(Operation *user, bool prefixComma) { } void OperationPrinter::printGenericOp(Operation *op, bool printOpName) { - if (printOpName) { - os << '"'; - printEscapedString(op->getName().getStringRef(), os); - os << '"'; - } + if (printOpName) + printEscapedString(op->getName().getStringRef()); os << '('; interleaveComma(op->getOperands(), [&](Value value) { printValueID(value); }); os << ')';