[Mlir-commits] [mlir] b2cc40f - [mlir:Printer][NFC] Add utility methods for printing escaped/hex strings

River Riddle llvmlistbot at llvm.org
Wed May 25 21:22:36 PDT 2022


Author: River Riddle
Date: 2022-05-25T20:54:27-07:00
New Revision: b2cc40fd67f6a8d909a0b4b0e8fe0dd7c45562d7

URL: https://github.com/llvm/llvm-project/commit/b2cc40fd67f6a8d909a0b4b0e8fe0dd7c45562d7
DIFF: https://github.com/llvm/llvm-project/commit/b2cc40fd67f6a8d909a0b4b0e8fe0dd7c45562d7.diff

LOG: [mlir:Printer][NFC] Add utility methods for printing escaped/hex strings

This simplifies quite a few cases where we manually duplicate the
escaping logic.

Added: 
    

Modified: 
    mlir/lib/IR/AsmPrinter.cpp

Removed: 
    


################################################################################
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 @@ class AsmPrinter::Impl {
   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<char> 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>([&](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>([&](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<StringAttr>()) {
-    os << '"';
-    printEscapedString(strAttr.getValue(), os);
-    os << '"';
+    printEscapedString(strAttr.getValue());
 
   } else if (auto arrayAttr = attr.dyn_cast<ArrayAttr>()) {
     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<DenseIntOrFPElementsAttr>()) {
@@ -1974,12 +1975,9 @@ void AsmPrinter::Impl::printDenseIntOrFPElementsAttr(
       MutableArrayRef<char> 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<StringRef> 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<char> 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 << ')';


        


More information about the Mlir-commits mailing list