[Mlir-commits] [mlir] a96911c - [mlir] Escape strings of opaque attributes

Markus Böck llvmlistbot at llvm.org
Mon Jul 5 03:13:52 PDT 2021


Author: Markus Böck
Date: 2021-07-05T12:13:36+02:00
New Revision: a96911c49bff75b22e980b87ac7375c62f671d15

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

LOG: [mlir] Escape strings of opaque attributes

Opaque attributes that currently contain string literals can't currently be properly roundtripped as they are not printed as escaped strings. This leads to incorrect tokens being generated and the parser to almost certainly fail. This patch simply uses llvm::printEscapedString from LLVM. It escapes all non printable characters and quotes to \xx hex literals, and backslashes to two backslashes. This syntax is supported by MLIRs Lexer as well. The same function is also currently in use for the same purpose in printSymbolReference, printAttribute for StringAttr and many more in AsmPrinter.cpp.

Differential Revision: https://reviews.llvm.org/D105405

Added: 
    

Modified: 
    mlir/lib/IR/AsmPrinter.cpp
    mlir/test/IR/parser.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index 7bc9bec2f927..401ee1af1113 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -1516,8 +1516,9 @@ static void printDialectSymbol(raw_ostream &os, StringRef symPrefix,
     return;
   }
 
-  // TODO: escape the symbol name, it could contain " characters.
-  os << "<\"" << symString << "\">";
+  os << "<\"";
+  llvm::printEscapedString(symString, os);
+  os << "\">";
 }
 
 /// Returns true if the given string can be represented as a bare identifier.

diff  --git a/mlir/test/IR/parser.mlir b/mlir/test/IR/parser.mlir
index 373fde0e89c9..eb833ec6d74e 100644
--- a/mlir/test/IR/parser.mlir
+++ b/mlir/test/IR/parser.mlir
@@ -1239,6 +1239,12 @@ func @"\"_string_symbol_reference\""() {
   return
 }
 
+// CHECK-LABEL: func private @parse_opaque_attr_escape
+func private @parse_opaque_attr_escape() {
+    // CHECK: value = #foo<"\22escaped\\\0A\22">
+    "foo.constant"() {value = #foo<"\"escaped\\\n\"">} : () -> ()
+}
+
 // CHECK-LABEL: func private @string_attr_name
 // CHECK-SAME: {"0 . 0", nested = {"0 . 0"}}
 func private @string_attr_name() attributes {"0 . 0", nested = {"0 . 0"}}


        


More information about the Mlir-commits mailing list