Skip to content

Commit 884221e

Browse files
committed
[mlir] Tidy uses of llvm::raw_stream_ostream (NFC)
As specified in the docs, 1) raw_string_ostream is always unbuffered and 2) the underlying buffer may be used directly ( 65b1361 for further reference ) * Don't call raw_string_ostream::flush(), which is essentially a no-op. * Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
1 parent 64aaf05 commit 884221e

File tree

20 files changed

+30
-33
lines changed

20 files changed

+30
-33
lines changed

mlir/lib/Debug/Observers/ActionProfiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ void ActionProfiler::print(const ActionActiveStack *action,
5858
if (printComma)
5959
os << ",\n";
6060
printComma = true;
61-
os << event.str();
61+
os << str;
6262
os.flush();
6363
}

mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ static void annotateOpsWithAliasSets(Operation *op,
12991299
std::string buffer;
13001300
llvm::raw_string_ostream stream(buffer);
13011301
alias.printAsOperand(stream, asmState);
1302-
aliases.push_back(b.getStringAttr(stream.str()));
1302+
aliases.push_back(b.getStringAttr(buffer));
13031303
});
13041304
return b.getArrayAttr(aliases);
13051305
};

mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ std::string TargetFeaturesAttr::getFeaturesString() const {
326326
llvm::raw_string_ostream ss(featuresString);
327327
llvm::interleave(
328328
getFeatures(), ss, [&](auto &feature) { ss << feature.strref(); }, ",");
329-
return ss.str();
329+
return featuresString;
330330
}
331331

332332
TargetFeaturesAttr TargetFeaturesAttr::featuresAt(Operation *op) {

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3247,7 +3247,7 @@ LogicalResult LLVMDialect::verifyDataLayoutString(
32473247
std::string message;
32483248
llvm::raw_string_ostream messageStream(message);
32493249
llvm::logAllUnhandledErrors(maybeDataLayout.takeError(), messageStream);
3250-
reportError("invalid data layout descriptor: " + messageStream.str());
3250+
reportError("invalid data layout descriptor: " + message);
32513251
return failure();
32523252
}
32533253

mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ LogicalResult MmaOp::verify() {
521521
}
522522
errorStream << "but got ";
523523
llvm::interleaveComma(operandTySeg, errorStream);
524-
return emitOpError(errorStream.str());
524+
return emitOpError(errorMessage);
525525
}
526526
}
527527

@@ -533,7 +533,7 @@ LogicalResult MmaOp::verify() {
533533
<< "Could not match allowed types for the result; expected one of ";
534534
llvm::interleaveComma(expectedResult, errorStream);
535535
errorStream << " but got " << getResult().getType();
536-
return emitOpError(errorStream.str());
536+
return emitOpError(errorMessage);
537537
}
538538

539539
// Ensure that binary MMA variants have a b1 MMA operation defined.
@@ -967,7 +967,6 @@ std::string NVVM::WgmmaMmaAsyncOp::getPtx() {
967967
}
968968
ss << ";\n"
969969
<< "}\n";
970-
ss.flush();
971970
return ptx;
972971
}
973972

mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,9 +2334,8 @@ std::string mlir::linalg::generateLibraryCallName(Operation *op) {
23342334
return std::string();
23352335
ss << "_";
23362336
}
2337-
std::string res = ss.str();
2338-
res.pop_back();
2339-
return res;
2337+
name.pop_back();
2338+
return name;
23402339
}
23412340

23422341
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/Linalg/TransformOps/LinalgMatchOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ DiagnosedSilenceableFailure transform::MatchStructuredBodyOp::matchOperation(
209209
os);
210210
if (result)
211211
return DiagnosedSilenceableFailure::success();
212-
return emitSilenceableError() << "contraction: " << os.str();
212+
return emitSilenceableError() << "contraction: " << message;
213213
}
214214
return emitDefiniteFailure() << "unknown body condition";
215215
}
@@ -226,7 +226,7 @@ LogicalResult transform::MatchStructuredBodyOp::verify() {
226226
getElementwiseAttrName(),
227227
getContractionAttrName()},
228228
os);
229-
return emitOpError() << "only one of {" << os.str() << "} is allowed";
229+
return emitOpError() << "only one of {" << attributeNames << "} is allowed";
230230
}
231231

232232
if (std::optional<ArrayAttr> contractionAttr = getContraction()) {

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ static void printCaptureType(OpAsmPrinter &p, Operation *op,
12581258
typeCap << "VLAType";
12591259
if (mapCaptureType.getValue() == mlir::omp::VariableCaptureKind::This)
12601260
typeCap << "This";
1261-
p << typeCap.str();
1261+
p << typeCapStr;
12621262
}
12631263

12641264
static ParseResult parseCaptureType(OpAsmParser &parser,

mlir/lib/Dialect/SparseTensor/IR/Detail/Var.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ std::string Var::str() const {
2929
std::string str;
3030
llvm::raw_string_ostream os(str);
3131
print(os);
32-
return os.str();
32+
return str;
3333
}
3434

3535
void Var::print(AsmPrinter &printer) const { print(printer.getStream()); }

mlir/lib/Dialect/Traits.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static std::string getShapeString(ArrayRef<int64_t> shape) {
223223
},
224224
"x");
225225
ss << '\'';
226-
return ss.str();
226+
return ret;
227227
}
228228

229229
LogicalResult OpTrait::impl::verifyCompatibleOperandBroadcast(Operation *op) {

mlir/lib/Dialect/Transform/DebugExtension/DebugExtensionOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ DiagnosedSilenceableFailure transform::DebugEmitParamAsRemarkOp::apply(
6060
os << *getMessage() << " ";
6161
llvm::interleaveComma(state.getParams(getParam()), os);
6262
if (!getAnchor()) {
63-
emitRemark() << os.str();
63+
emitRemark() << str;
6464
return DiagnosedSilenceableFailure::success();
6565
}
6666
for (Operation *payload : state.getPayloadOps(getAnchor()))
67-
::mlir::emitRemark(payload->getLoc()) << os.str();
67+
::mlir::emitRemark(payload->getLoc()) << str;
6868
return DiagnosedSilenceableFailure::success();
6969
}

mlir/lib/Dialect/Transform/IR/TransformOps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1967,7 +1967,7 @@ transform::MatchParamCmpIOp::apply(transform::TransformRewriter &rewriter,
19671967
std::string str;
19681968
llvm::raw_string_ostream os(str);
19691969
value.print(os, /*isSigned=*/true);
1970-
return os.str();
1970+
return str;
19711971
};
19721972

19731973
ArrayRef<Attribute> params = state.getParams(getParam());

mlir/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ Expected<void *> ExecutionEngine::lookup(StringRef name) const {
432432
llvm::raw_string_ostream os(errorMessage);
433433
llvm::handleAllErrors(expectedSymbol.takeError(),
434434
[&os](llvm::ErrorInfoBase &ei) { ei.log(os); });
435-
return makeStringError(os.str());
435+
return makeStringError(errorMessage);
436436
}
437437

438438
if (void *fptr = expectedSymbol->toPtr<void *>())

mlir/lib/IR/Diagnostics.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ Diagnostic &Diagnostic::appendOp(Operation &op, const OpPrintingFlags &flags) {
146146
// multiple lines.
147147
if (str.find('\n') != std::string::npos)
148148
*this << '\n';
149-
return *this << os.str();
149+
return *this << str;
150150
}
151151

152152
/// Stream in a Value.
153153
Diagnostic &Diagnostic::operator<<(Value val) {
154154
std::string str;
155155
llvm::raw_string_ostream os(str);
156156
val.print(os, adjustPrintingFlags(OpPrintingFlags(), severity));
157-
return *this << os.str();
157+
return *this << str;
158158
}
159159

160160
/// Outputs this diagnostic to a stream.
@@ -168,7 +168,7 @@ std::string Diagnostic::str() const {
168168
std::string str;
169169
llvm::raw_string_ostream os(str);
170170
print(os);
171-
return os.str();
171+
return str;
172172
}
173173

174174
/// Attaches a note to this diagnostic. A new location may be optionally
@@ -451,7 +451,7 @@ void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message,
451451
if (!llvm::isa<UnknownLoc>(loc))
452452
strOS << loc << ": ";
453453
strOS << message;
454-
return mgr.PrintMessage(os, SMLoc(), getDiagKind(kind), strOS.str());
454+
return mgr.PrintMessage(os, SMLoc(), getDiagKind(kind), str);
455455
}
456456

457457
// Otherwise if we are displaying the source line, try to convert the file
@@ -469,7 +469,7 @@ void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message,
469469
llvm::raw_string_ostream locOS(locStr);
470470
locOS << fileLoc.getFilename().getValue() << ":" << fileLoc.getLine() << ":"
471471
<< fileLoc.getColumn();
472-
llvm::SMDiagnostic diag(locOS.str(), getDiagKind(kind), message.str());
472+
llvm::SMDiagnostic diag(locStr, getDiagKind(kind), message.str());
473473
diag.print(nullptr, os);
474474
}
475475

@@ -637,7 +637,7 @@ struct ExpectedDiag {
637637
regexOS << '(' << regexStr << ')';
638638
strToProcess = strToProcess.drop_front(regexEndIt + 2);
639639
}
640-
substringRegex = llvm::Regex(regexOS.str());
640+
substringRegex = llvm::Regex(regexStr);
641641
return success();
642642
}
643643

mlir/lib/Interfaces/DataLayoutInterfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ using namespace mlir;
2929
os << "neither the scoping op nor the type class provide data layout "
3030
"information for "
3131
<< type;
32-
llvm::report_fatal_error(Twine(os.str()));
32+
llvm::report_fatal_error(Twine(message));
3333
}
3434

3535
/// Returns the bitwidth of the index type if specified in the param list.

mlir/lib/Interfaces/RuntimeVerifiableOpInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ RuntimeVerifiableOpInterface::generateErrorMessage(Operation *op,
3030
stream << "\n^ " << msg;
3131
stream << "\nLocation: ";
3232
op->getLoc().print(stream);
33-
return stream.str();
33+
return buffer;
3434
}
3535
} // namespace mlir
3636

mlir/lib/Pass/Pass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ LogicalResult Pass::initializeOptions(
6666
std::string errStr;
6767
llvm::raw_string_ostream os(errStr);
6868
if (failed(passOptions.parseFromString(options, os))) {
69-
os.flush();
7069
return errorHandler(errStr);
7170
}
7271
return success();
@@ -700,7 +699,7 @@ std::string OpToOpPassAdaptor::getAdaptorName() {
700699
os << '\'' << pm.getOpAnchorName() << '\'';
701700
});
702701
os << ']';
703-
return os.str();
702+
return name;
704703
}
705704

706705
void OpToOpPassAdaptor::runOnOperation() {

mlir/lib/Pass/PassCrashRecovery.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ void PassCrashReproducerGenerator::prepareReproducerFor(Pass *pass,
321321
passOS << ")";
322322

323323
impl->activeContexts.push_back(std::make_unique<RecoveryReproducerContext>(
324-
passOS.str(), op, impl->streamFactory, impl->pmFlagVerifyPasses));
324+
passStr, op, impl->streamFactory, impl->pmFlagVerifyPasses));
325325
}
326326
void PassCrashReproducerGenerator::prepareReproducerFor(
327327
iterator_range<PassManager::pass_iterator> passes, Operation *op) {
@@ -331,7 +331,7 @@ void PassCrashReproducerGenerator::prepareReproducerFor(
331331
passes, passOS, [&](Pass &pass) { pass.printAsTextualPipeline(passOS); });
332332

333333
impl->activeContexts.push_back(std::make_unique<RecoveryReproducerContext>(
334-
passOS.str(), op, impl->streamFactory, impl->pmFlagVerifyPasses));
334+
passStr, op, impl->streamFactory, impl->pmFlagVerifyPasses));
335335
}
336336

337337
void PassCrashReproducerGenerator::removeLastReproducerFor(Pass *pass,

mlir/lib/Query/Matcher/RegistryManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ RegistryManager::getMatcherCompletions(llvm::ArrayRef<ArgKind> acceptedTypes,
124124
else if (argKinds[0][0] == ArgKind::String)
125125
typedText += "\"";
126126

127-
completions.emplace_back(typedText, os.str());
127+
completions.emplace_back(typedText, decl);
128128
}
129129

130130
return completions;

mlir/lib/Query/QueryParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ makeInvalidQueryFromDiagnostics(const matcher::internal::Diagnostics &diag) {
123123
std::string errStr;
124124
llvm::raw_string_ostream os(errStr);
125125
diag.print(os);
126-
return new InvalidQuery(os.str());
126+
return new InvalidQuery(errStr);
127127
}
128128
} // namespace
129129

0 commit comments

Comments
 (0)