diff --git a/mlir/include/mlir/TableGen/CodeGenHelpers.h b/mlir/include/mlir/TableGen/CodeGenHelpers.h --- a/mlir/include/mlir/TableGen/CodeGenHelpers.h +++ b/mlir/include/mlir/TableGen/CodeGenHelpers.h @@ -146,6 +146,9 @@ llvm::DenseMap localTypeConstraints; }; +// Escape a string using C++ encoding. E.g. foo"bar -> foo\x22bar. +std::string escapeString(StringRef value); + } // namespace tblgen } // namespace mlir diff --git a/mlir/test/mlir-tblgen/predicate.td b/mlir/test/mlir-tblgen/predicate.td --- a/mlir/test/mlir-tblgen/predicate.td +++ b/mlir/test/mlir-tblgen/predicate.td @@ -111,3 +111,14 @@ // CHECK: auto valueGroup0 = getODSOperands(0); // CHECK: for (::mlir::Value v : valueGroup0) { // CHECK: if (::mlir::failed([[$TENSOR_INTEGER_FLOAT_CONSTRAINT]] + +def OpL : NS_Op<"op_for_StringEscaping", []> { + let arguments = (ins + StringBasedAttr().getValue() == \"foo\"">, + "only value \"foo\" is allowed">:$s + ); +} + +// CHECK-LABEL: OpLAdaptor::verify +// CHECK: getValue() == "foo" +// CHECK-SAME: only value \"foo\" is allowed diff --git a/mlir/tools/mlir-tblgen/CodeGenHelpers.cpp b/mlir/tools/mlir-tblgen/CodeGenHelpers.cpp --- a/mlir/tools/mlir-tblgen/CodeGenHelpers.cpp +++ b/mlir/tools/mlir-tblgen/CodeGenHelpers.cpp @@ -137,3 +137,10 @@ os.unindent() << "}\n\n"; } } + +std::string mlir::tblgen::escapeString(StringRef value) { + std::string ret; + llvm::raw_string_ostream os(ret); + os.write_escaped(value); + return os.str(); +} diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -151,14 +151,6 @@ return str; } -// Escape a string using LLVM/MLIR encoding. E.g. foo"bar -> foo\22bar. -static std::string escapeString(StringRef value) { - std::string ret; - llvm::raw_string_ostream os(ret); - llvm::printEscapedString(value, os); - return os.str(); -} - // Returns whether the record has a value of the given name that can be returned // via getValueAsString. static inline bool hasStringAttribute(const Record &record, diff --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp --- a/mlir/tools/mlir-tblgen/RewriterGen.cpp +++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp @@ -12,6 +12,7 @@ #include "mlir/Support/IndentedOstream.h" #include "mlir/TableGen/Attribute.h" +#include "mlir/TableGen/CodeGenHelpers.h" #include "mlir/TableGen/Format.h" #include "mlir/TableGen/GenInfo.h" #include "mlir/TableGen/Operator.h" @@ -50,15 +51,6 @@ }; } // end namespace llvm -// Escape a string for use inside a C++ literal. -// E.g. foo"bar -> foo\x22bar. -static std::string escapeString(StringRef value) { - std::string ret; - llvm::raw_string_ostream os(ret); - os.write_escaped(value, /*use_hex_escapes=*/true); - return os.str(); -} - //===----------------------------------------------------------------------===// // PatternEmitter //===----------------------------------------------------------------------===//