Skip to content

Commit e15e839

Browse files
committed
[TVMScript] Use triple-quoted python strings for metadata
Prior to this commit, all metadata was output as a single-quoted python string with escaped newlines. This single line could become extremely long, which can cause some text editors to run slowly (e.g. emacs). This commit updates the TVMScript printer to instead output the metadata using a triple-quoted python string with embedded newlines. Because the metadata is a pretty-printed JSON string, it contains frequent newlines that prevent the line length from causing editor lag.
1 parent fba10d7 commit e15e839

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

src/script/printer/utils.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ inline std::string Docsify(const ObjectRef& obj, const IRDocsifier& d, const Fra
8080
std::ostringstream os;
8181
if (!d->metadata.empty()) {
8282
if (d->cfg->show_meta) {
83-
os << "metadata = tvm.ir.load_json(\""
83+
os << "metadata = tvm.ir.load_json(\"\"\""
8484
<< support::StrEscape(
85-
SaveJSON(Map<String, ObjectRef>(d->metadata.begin(), d->metadata.end())))
86-
<< "\")\n";
85+
SaveJSON(Map<String, ObjectRef>(d->metadata.begin(), d->metadata.end())), false,
86+
false)
87+
<< "\"\"\")\n";
8788
} else {
8889
f->stmts.push_back(
8990
CommentDoc("Metadata omitted. Use show_meta=True in script() method to show it."));

src/support/str_escape.h

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,64 @@ namespace support {
3333

3434
/*!
3535
* \brief Create a stream with escape.
36+
*
3637
* \param data The data
38+
*
3739
* \param size The size of the string.
40+
*
3841
* \param use_octal_escape True to use octal escapes instead of hex. If producing C
3942
* strings, use octal escapes to avoid ambiguously-long hex escapes.
43+
*
44+
* \param escape_whitespace_special_chars If True (default), escape
45+
* any tab, newline, and carriage returns that occur in the string.
46+
* If False, do not escape these characters.
47+
*
4048
* \return the Result string.
4149
*/
42-
inline std::string StrEscape(const char* data, size_t size, bool use_octal_escape = false) {
50+
inline std::string StrEscape(const char* data, size_t size, bool use_octal_escape = false,
51+
bool escape_whitespace_special_chars = true) {
4352
std::ostringstream stream;
4453
for (size_t i = 0; i < size; ++i) {
4554
unsigned char c = data[i];
4655
if (c >= ' ' && c <= '~' && c != '\\' && c != '"') {
4756
stream << c;
4857
} else {
49-
stream << '\\';
5058
switch (c) {
5159
case '"':
52-
stream << '"';
60+
stream << '\\' << '"';
5361
break;
5462
case '\\':
55-
stream << '\\';
63+
stream << '\\' << '\\';
5664
break;
5765
case '\t':
58-
stream << 't';
66+
if (escape_whitespace_special_chars) {
67+
stream << '\\' << 't';
68+
} else {
69+
stream << c;
70+
}
5971
break;
6072
case '\r':
61-
stream << 'r';
73+
if (escape_whitespace_special_chars) {
74+
stream << '\\' << 'r';
75+
} else {
76+
stream << c;
77+
}
6278
break;
6379
case '\n':
64-
stream << 'n';
80+
if (escape_whitespace_special_chars) {
81+
stream << '\\' << 'n';
82+
} else {
83+
stream << c;
84+
}
6585
break;
6686
default:
6787
if (use_octal_escape) {
68-
stream << static_cast<unsigned char>('0' + ((c >> 6) & 0x03))
88+
stream << '\\' << static_cast<unsigned char>('0' + ((c >> 6) & 0x03))
6989
<< static_cast<unsigned char>('0' + ((c >> 3) & 0x07))
7090
<< static_cast<unsigned char>('0' + (c & 0x07));
7191
} else {
7292
const char* hex_digits = "0123456789ABCDEF";
73-
stream << 'x' << hex_digits[c >> 4] << hex_digits[c & 0xf];
93+
stream << '\\' << 'x' << hex_digits[c >> 4] << hex_digits[c & 0xf];
7494
}
7595
}
7696
}
@@ -80,11 +100,23 @@ inline std::string StrEscape(const char* data, size_t size, bool use_octal_escap
80100

81101
/*!
82102
* \brief Create a stream with escape.
83-
* \param data The data
84-
* \param size The size of the string.
103+
*
104+
* \param val The C++ string
105+
*
106+
* \param use_octal_escape True to use octal escapes instead of hex. If producing C
107+
* strings, use octal escapes to avoid ambiguously-long hex escapes.
108+
*
109+
* \param escape_whitespace_special_chars If True (default), escape
110+
* any tab, newline, and carriage returns that occur in the string.
111+
* If False, do not escape these characters. If producing python
112+
* strings with """triple quotes""", do not escape these characters.
113+
*
85114
* \return the Result string.
86115
*/
87-
inline std::string StrEscape(const std::string& val) { return StrEscape(val.data(), val.length()); }
116+
inline std::string StrEscape(const std::string& val, bool use_octal_escape = false,
117+
bool escape_whitespace_special_chars = true) {
118+
return StrEscape(val.data(), val.length(), use_octal_escape, escape_whitespace_special_chars);
119+
}
88120

89121
} // namespace support
90122
} // namespace tvm

0 commit comments

Comments
 (0)