Skip to content

Commit ffc80de

Browse files
committed
[mlir] Nits on uses of llvm::raw_string_ostream (NFC)
* Don't call raw_string_ostream::flush(), which is essentially a no-op * Strip unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
1 parent 7deca85 commit ffc80de

File tree

8 files changed

+45
-44
lines changed

8 files changed

+45
-44
lines changed

mlir/tools/mlir-linalg-ods-gen/mlir-linalg-ods-yaml-gen.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ static std::string generateCppExpression(SerializedAffineMap self,
378378
std::string printedStr;
379379
llvm::raw_string_ostream printedSs(printedStr);
380380
self.affineMapAttr.print(printedSs);
381-
printedSs.flush();
382381

383382
static const char exprFormat[] =
384383
R"FMT(llvm::cast<AffineMapAttr>(mlir::parseAttribute("{0}", {1})).getValue())FMT";
@@ -391,7 +390,6 @@ static std::string interleaveToString(Container &container,
391390
std::string result;
392391
llvm::raw_string_ostream ss(result);
393392
llvm::interleave(container, ss, separator);
394-
ss.flush();
395393
return result;
396394
}
397395

@@ -827,7 +825,6 @@ generateNamedGenericOpDefns(LinalgOpConfig &opConfig,
827825
break;
828826
}
829827
});
830-
ss.flush();
831828
os << llvm::formatv(structuredOpIteratorTypesFormat, className,
832829
iteratorsStr);
833830
} else {
@@ -892,7 +889,6 @@ exprs.push_back(getAffineConstantExpr(cst{1}, context));
892889
std::string symbolBindingsStr;
893890
llvm::raw_string_ostream symbolBindingsSs(symbolBindingsStr);
894891
llvm::interleave(symbolBindings, symbolBindingsSs, "\n");
895-
symbolBindingsSs.flush();
896892

897893
os << llvm::formatv(structuredOpSymbolBindingsFormat, className,
898894
symbolBindingsStr);
@@ -913,7 +909,6 @@ exprs.push_back(getAffineConstantExpr(cst{1}, context));
913909
llvm::raw_string_ostream dimIdentsSs(dimIdentsStr);
914910
llvm::interleaveComma(dimIndices, dimIdentsSs,
915911
[&](unsigned i) { dimIdentsSs << "d" << i; });
916-
dimIdentsSs.flush();
917912

918913
// Statements to add and simplify each affine map.
919914
SmallVector<std::string> stmts;

mlir/tools/mlir-pdll/mlir-pdll.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ int main(int argc, char **argv) {
207207
// any.
208208
if (auto existingOrErr =
209209
llvm::MemoryBuffer::getFile(outputFilename, /*IsText=*/true))
210-
if (std::move(existingOrErr.get())->getBuffer() == outputStrOS.str())
210+
if (std::move(existingOrErr.get())->getBuffer() == outputStr)
211211
shouldWriteOutput = false;
212212
}
213213

@@ -219,7 +219,7 @@ int main(int argc, char **argv) {
219219
llvm::errs() << errorMessage << "\n";
220220
return 1;
221221
}
222-
outputFile->os() << outputStrOS.str();
222+
outputFile->os() << outputStr;
223223
outputFile->keep();
224224
}
225225

mlir/tools/mlir-src-sharder/mlir-src-sharder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int main(int argc, char **argv) {
9898
// any.
9999
if (auto existingOrErr =
100100
llvm::MemoryBuffer::getFile(outputFilename, /*IsText=*/true))
101-
if (std::move(existingOrErr.get())->getBuffer() == os.str())
101+
if (std::move(existingOrErr.get())->getBuffer() == outputStr)
102102
shouldWriteOutput = false;
103103
}
104104

mlir/tools/mlir-tblgen/OpDocGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static void emitOpTraitsDoc(const Operator &op, raw_ostream &os) {
162162
os << effect << " on " << rec->getValueAsString("resource");
163163
});
164164
os << "}";
165-
effects.insert(backticks(os.str()));
165+
effects.insert(backticks(effectStr));
166166
name.append(llvm::formatv(" ({0})", traitName).str());
167167
}
168168
interfaces.insert(backticks(name));
@@ -433,7 +433,7 @@ static void maybeNest(bool nest, llvm::function_ref<void(raw_ostream &os)> fn,
433433
std::string str;
434434
llvm::raw_string_ostream ss(str);
435435
fn(ss);
436-
for (StringRef x : llvm::split(ss.str(), "\n")) {
436+
for (StringRef x : llvm::split(str, "\n")) {
437437
if (nest && x.starts_with("#"))
438438
os << "#";
439439
os << x << "\n";

mlir/unittests/Bytecode/BytecodeTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ TEST(Bytecode, MultiModuleWithResource) {
4949
std::string buffer;
5050
llvm::raw_string_ostream ostream(buffer);
5151
ASSERT_TRUE(succeeded(writeBytecodeToFile(module.get(), ostream)));
52-
ostream.flush();
5352

5453
// Create copy of buffer which is aligned to requested resource alignment.
5554
constexpr size_t kAlignment = 0x20;
@@ -139,7 +138,7 @@ TEST(Bytecode, OpWithoutProperties) {
139138
ASSERT_TRUE(succeeded(writeBytecodeToFile(op.get(), os)));
140139
std::unique_ptr<Block> block = std::make_unique<Block>();
141140
ASSERT_TRUE(succeeded(readBytecodeFile(
142-
llvm::MemoryBufferRef(os.str(), "string-buffer"), block.get(), config)));
141+
llvm::MemoryBufferRef(bytecode, "string-buffer"), block.get(), config)));
143142
Operation *roundtripped = &block->front();
144143
EXPECT_EQ(roundtripped->getAttrs().size(), 2u);
145144
EXPECT_TRUE(roundtripped->getInherentAttr("inherent_attr") != std::nullopt);

mlir/unittests/IR/AttributeTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ TEST(CopyCountAttr, PrintStripped) {
498498
os << "|" << res << "|";
499499
res.printStripped(os << "[");
500500
os << "]";
501-
EXPECT_EQ(os.str(), "|#test.copy_count<hello>|[copy_count<hello>]");
501+
EXPECT_EQ(str, "|#test.copy_count<hello>|[copy_count<hello>]");
502502
}
503503

504504
} // namespace

mlir/unittests/IR/OpPropertiesTest.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ TEST(OpPropertiesTest, Properties) {
191191
"array = array<i64: 40, 41>, "
192192
"b = -4.200000e+01 : f32, "
193193
"label = \"bar foo\"}> : () -> ()\n",
194-
os.str().c_str());
194+
output.c_str());
195195
}
196196
// Get a mutable reference to the properties for this operation and modify it
197197
// in place one member at a time.
@@ -201,40 +201,44 @@ TEST(OpPropertiesTest, Properties) {
201201
std::string output;
202202
llvm::raw_string_ostream os(output);
203203
opWithProp.print(os);
204-
EXPECT_TRUE(StringRef(os.str()).contains("a = 42"));
205-
EXPECT_TRUE(StringRef(os.str()).contains("b = -4.200000e+01"));
206-
EXPECT_TRUE(StringRef(os.str()).contains("array = array<i64: 40, 41>"));
207-
EXPECT_TRUE(StringRef(os.str()).contains("label = \"bar foo\""));
204+
StringRef view(output);
205+
EXPECT_TRUE(view.contains("a = 42"));
206+
EXPECT_TRUE(view.contains("b = -4.200000e+01"));
207+
EXPECT_TRUE(view.contains("array = array<i64: 40, 41>"));
208+
EXPECT_TRUE(view.contains("label = \"bar foo\""));
208209
}
209210
prop.b = 42.;
210211
{
211212
std::string output;
212213
llvm::raw_string_ostream os(output);
213214
opWithProp.print(os);
214-
EXPECT_TRUE(StringRef(os.str()).contains("a = 42"));
215-
EXPECT_TRUE(StringRef(os.str()).contains("b = 4.200000e+01"));
216-
EXPECT_TRUE(StringRef(os.str()).contains("array = array<i64: 40, 41>"));
217-
EXPECT_TRUE(StringRef(os.str()).contains("label = \"bar foo\""));
215+
StringRef view(output);
216+
EXPECT_TRUE(view.contains("a = 42"));
217+
EXPECT_TRUE(view.contains("b = 4.200000e+01"));
218+
EXPECT_TRUE(view.contains("array = array<i64: 40, 41>"));
219+
EXPECT_TRUE(view.contains("label = \"bar foo\""));
218220
}
219221
prop.array.push_back(42);
220222
{
221223
std::string output;
222224
llvm::raw_string_ostream os(output);
223225
opWithProp.print(os);
224-
EXPECT_TRUE(StringRef(os.str()).contains("a = 42"));
225-
EXPECT_TRUE(StringRef(os.str()).contains("b = 4.200000e+01"));
226-
EXPECT_TRUE(StringRef(os.str()).contains("array = array<i64: 40, 41, 42>"));
227-
EXPECT_TRUE(StringRef(os.str()).contains("label = \"bar foo\""));
226+
StringRef view(output);
227+
EXPECT_TRUE(view.contains("a = 42"));
228+
EXPECT_TRUE(view.contains("b = 4.200000e+01"));
229+
EXPECT_TRUE(view.contains("array = array<i64: 40, 41, 42>"));
230+
EXPECT_TRUE(view.contains("label = \"bar foo\""));
228231
}
229232
prop.label = std::make_shared<std::string>("foo bar");
230233
{
231234
std::string output;
232235
llvm::raw_string_ostream os(output);
233236
opWithProp.print(os);
234-
EXPECT_TRUE(StringRef(os.str()).contains("a = 42"));
235-
EXPECT_TRUE(StringRef(os.str()).contains("b = 4.200000e+01"));
236-
EXPECT_TRUE(StringRef(os.str()).contains("array = array<i64: 40, 41, 42>"));
237-
EXPECT_TRUE(StringRef(os.str()).contains("label = \"foo bar\""));
237+
StringRef view(output);
238+
EXPECT_TRUE(view.contains("a = 42"));
239+
EXPECT_TRUE(view.contains("b = 4.200000e+01"));
240+
EXPECT_TRUE(view.contains("array = array<i64: 40, 41, 42>"));
241+
EXPECT_TRUE(view.contains("label = \"foo bar\""));
238242
}
239243
}
240244

@@ -297,9 +301,10 @@ TEST(OpPropertiesTest, DefaultValues) {
297301
std::string output;
298302
llvm::raw_string_ostream os(output);
299303
op->print(os);
300-
EXPECT_TRUE(StringRef(os.str()).contains("a = -1"));
301-
EXPECT_TRUE(StringRef(os.str()).contains("b = -1"));
302-
EXPECT_TRUE(StringRef(os.str()).contains("array = array<i64: -33>"));
304+
StringRef view(output);
305+
EXPECT_TRUE(view.contains("a = -1"));
306+
EXPECT_TRUE(view.contains("b = -1"));
307+
EXPECT_TRUE(view.contains("array = array<i64: -33>"));
303308
}
304309
op->erase();
305310
}
@@ -371,9 +376,10 @@ TEST(OpPropertiesTest, getOrAddProperties) {
371376
std::string output;
372377
llvm::raw_string_ostream os(output);
373378
op->print(os);
374-
EXPECT_TRUE(StringRef(os.str()).contains("a = 1"));
375-
EXPECT_TRUE(StringRef(os.str()).contains("b = 2"));
376-
EXPECT_TRUE(StringRef(os.str()).contains("array = array<i64: 3, 4, 5>"));
379+
StringRef view(output);
380+
EXPECT_TRUE(view.contains("a = 1"));
381+
EXPECT_TRUE(view.contains("b = 2"));
382+
EXPECT_TRUE(view.contains("array = array<i64: 3, 4, 5>"));
377383
}
378384
op->erase();
379385
}
@@ -400,8 +406,9 @@ TEST(OpPropertiesTest, withoutPropertiesDiscardableAttrs) {
400406
std::string output;
401407
llvm::raw_string_ostream os(output);
402408
op->print(os);
403-
EXPECT_TRUE(StringRef(os.str()).contains("inherent_attr = 42"));
404-
EXPECT_TRUE(StringRef(os.str()).contains("other_attr = 56"));
409+
StringRef view(output);
410+
EXPECT_TRUE(view.contains("inherent_attr = 42"));
411+
EXPECT_TRUE(view.contains("other_attr = 56"));
405412

406413
OwningOpRef<Operation *> reparsed = parseSourceString(os.str(), config);
407414
auto trivialHash = [](Value v) { return hash_value(v); };

mlir/unittests/Support/IndentedOstreamTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ TEST(FormatTest, SingleLine) {
1818
raw_indented_ostream ros(os);
1919
ros << 10;
2020
ros.flush();
21-
EXPECT_THAT(os.str(), StrEq("10"));
21+
EXPECT_THAT(str, StrEq("10"));
2222
}
2323

2424
TEST(FormatTest, SimpleMultiLine) {
@@ -31,7 +31,7 @@ TEST(FormatTest, SimpleMultiLine) {
3131
ros << "c";
3232
ros << "\n";
3333
ros.flush();
34-
EXPECT_THAT(os.str(), StrEq("ab\nc\n"));
34+
EXPECT_THAT(str, StrEq("ab\nc\n"));
3535
}
3636

3737
TEST(FormatTest, SimpleMultiLineIndent) {
@@ -44,7 +44,7 @@ TEST(FormatTest, SimpleMultiLineIndent) {
4444
ros << "c";
4545
ros << "\n";
4646
ros.flush();
47-
EXPECT_THAT(os.str(), StrEq(" a b\n c\n"));
47+
EXPECT_THAT(str, StrEq(" a b\n c\n"));
4848
}
4949

5050
TEST(FormatTest, SingleRegion) {
@@ -71,7 +71,7 @@ TEST(FormatTest, SingleRegion) {
7171
inner inner
7272
}
7373
after)";
74-
EXPECT_THAT(os.str(), StrEq(expected));
74+
EXPECT_THAT(str, StrEq(expected));
7575

7676
// Repeat the above with inline form.
7777
str.clear();
@@ -106,7 +106,7 @@ TEST(FormatTest, Reindent) {
106106
107107
108108
)";
109-
EXPECT_THAT(os.str(), StrEq(expected));
109+
EXPECT_THAT(str, StrEq(expected));
110110
}
111111

112112
TEST(FormatTest, ReindentLineEndings) {
@@ -122,5 +122,5 @@ TEST(FormatTest, ReindentLineEndings) {
122122
ros.printReindented(desc);
123123
ros.flush();
124124
const auto *expected = "First line\r\n second line";
125-
EXPECT_THAT(os.str(), StrEq(expected));
125+
EXPECT_THAT(str, StrEq(expected));
126126
}

0 commit comments

Comments
 (0)