Skip to content

Commit

Permalink
[mlir] Escape strings of opaque attributes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zero9178 authored and arichardson committed Sep 13, 2021
2 parents a923e84 + a96911c commit cd6b1d5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions mlir/test/IR/parser.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}
Expand Down

0 comments on commit cd6b1d5

Please sign in to comment.