[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

( 65b13610a5 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.
This commit is contained in:
JOE1994
2024-09-16 18:46:51 -04:00
parent 64aaf0559d
commit 884221eddb
20 changed files with 30 additions and 33 deletions

View File

@@ -58,6 +58,6 @@ void ActionProfiler::print(const ActionActiveStack *action,
if (printComma)
os << ",\n";
printComma = true;
os << event.str();
os << str;
os.flush();
}

View File

@@ -1299,7 +1299,7 @@ static void annotateOpsWithAliasSets(Operation *op,
std::string buffer;
llvm::raw_string_ostream stream(buffer);
alias.printAsOperand(stream, asmState);
aliases.push_back(b.getStringAttr(stream.str()));
aliases.push_back(b.getStringAttr(buffer));
});
return b.getArrayAttr(aliases);
};

View File

@@ -326,7 +326,7 @@ std::string TargetFeaturesAttr::getFeaturesString() const {
llvm::raw_string_ostream ss(featuresString);
llvm::interleave(
getFeatures(), ss, [&](auto &feature) { ss << feature.strref(); }, ",");
return ss.str();
return featuresString;
}
TargetFeaturesAttr TargetFeaturesAttr::featuresAt(Operation *op) {

View File

@@ -3247,7 +3247,7 @@ LogicalResult LLVMDialect::verifyDataLayoutString(
std::string message;
llvm::raw_string_ostream messageStream(message);
llvm::logAllUnhandledErrors(maybeDataLayout.takeError(), messageStream);
reportError("invalid data layout descriptor: " + messageStream.str());
reportError("invalid data layout descriptor: " + message);
return failure();
}

View File

@@ -521,7 +521,7 @@ LogicalResult MmaOp::verify() {
}
errorStream << "but got ";
llvm::interleaveComma(operandTySeg, errorStream);
return emitOpError(errorStream.str());
return emitOpError(errorMessage);
}
}
@@ -533,7 +533,7 @@ LogicalResult MmaOp::verify() {
<< "Could not match allowed types for the result; expected one of ";
llvm::interleaveComma(expectedResult, errorStream);
errorStream << " but got " << getResult().getType();
return emitOpError(errorStream.str());
return emitOpError(errorMessage);
}
// Ensure that binary MMA variants have a b1 MMA operation defined.
@@ -967,7 +967,6 @@ std::string NVVM::WgmmaMmaAsyncOp::getPtx() {
}
ss << ";\n"
<< "}\n";
ss.flush();
return ptx;
}

View File

@@ -2334,9 +2334,8 @@ std::string mlir::linalg::generateLibraryCallName(Operation *op) {
return std::string();
ss << "_";
}
std::string res = ss.str();
res.pop_back();
return res;
name.pop_back();
return name;
}
//===----------------------------------------------------------------------===//

View File

@@ -209,7 +209,7 @@ DiagnosedSilenceableFailure transform::MatchStructuredBodyOp::matchOperation(
os);
if (result)
return DiagnosedSilenceableFailure::success();
return emitSilenceableError() << "contraction: " << os.str();
return emitSilenceableError() << "contraction: " << message;
}
return emitDefiniteFailure() << "unknown body condition";
}
@@ -226,7 +226,7 @@ LogicalResult transform::MatchStructuredBodyOp::verify() {
getElementwiseAttrName(),
getContractionAttrName()},
os);
return emitOpError() << "only one of {" << os.str() << "} is allowed";
return emitOpError() << "only one of {" << attributeNames << "} is allowed";
}
if (std::optional<ArrayAttr> contractionAttr = getContraction()) {

View File

@@ -1258,7 +1258,7 @@ static void printCaptureType(OpAsmPrinter &p, Operation *op,
typeCap << "VLAType";
if (mapCaptureType.getValue() == mlir::omp::VariableCaptureKind::This)
typeCap << "This";
p << typeCap.str();
p << typeCapStr;
}
static ParseResult parseCaptureType(OpAsmParser &parser,

View File

@@ -29,7 +29,7 @@ std::string Var::str() const {
std::string str;
llvm::raw_string_ostream os(str);
print(os);
return os.str();
return str;
}
void Var::print(AsmPrinter &printer) const { print(printer.getStream()); }

View File

@@ -223,7 +223,7 @@ static std::string getShapeString(ArrayRef<int64_t> shape) {
},
"x");
ss << '\'';
return ss.str();
return ret;
}
LogicalResult OpTrait::impl::verifyCompatibleOperandBroadcast(Operation *op) {

View File

@@ -60,10 +60,10 @@ DiagnosedSilenceableFailure transform::DebugEmitParamAsRemarkOp::apply(
os << *getMessage() << " ";
llvm::interleaveComma(state.getParams(getParam()), os);
if (!getAnchor()) {
emitRemark() << os.str();
emitRemark() << str;
return DiagnosedSilenceableFailure::success();
}
for (Operation *payload : state.getPayloadOps(getAnchor()))
::mlir::emitRemark(payload->getLoc()) << os.str();
::mlir::emitRemark(payload->getLoc()) << str;
return DiagnosedSilenceableFailure::success();
}

View File

@@ -1967,7 +1967,7 @@ transform::MatchParamCmpIOp::apply(transform::TransformRewriter &rewriter,
std::string str;
llvm::raw_string_ostream os(str);
value.print(os, /*isSigned=*/true);
return os.str();
return str;
};
ArrayRef<Attribute> params = state.getParams(getParam());

View File

@@ -432,7 +432,7 @@ Expected<void *> ExecutionEngine::lookup(StringRef name) const {
llvm::raw_string_ostream os(errorMessage);
llvm::handleAllErrors(expectedSymbol.takeError(),
[&os](llvm::ErrorInfoBase &ei) { ei.log(os); });
return makeStringError(os.str());
return makeStringError(errorMessage);
}
if (void *fptr = expectedSymbol->toPtr<void *>())

View File

@@ -146,7 +146,7 @@ Diagnostic &Diagnostic::appendOp(Operation &op, const OpPrintingFlags &flags) {
// multiple lines.
if (str.find('\n') != std::string::npos)
*this << '\n';
return *this << os.str();
return *this << str;
}
/// Stream in a Value.
@@ -154,7 +154,7 @@ Diagnostic &Diagnostic::operator<<(Value val) {
std::string str;
llvm::raw_string_ostream os(str);
val.print(os, adjustPrintingFlags(OpPrintingFlags(), severity));
return *this << os.str();
return *this << str;
}
/// Outputs this diagnostic to a stream.
@@ -168,7 +168,7 @@ std::string Diagnostic::str() const {
std::string str;
llvm::raw_string_ostream os(str);
print(os);
return os.str();
return str;
}
/// Attaches a note to this diagnostic. A new location may be optionally
@@ -451,7 +451,7 @@ void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message,
if (!llvm::isa<UnknownLoc>(loc))
strOS << loc << ": ";
strOS << message;
return mgr.PrintMessage(os, SMLoc(), getDiagKind(kind), strOS.str());
return mgr.PrintMessage(os, SMLoc(), getDiagKind(kind), str);
}
// Otherwise if we are displaying the source line, try to convert the file
@@ -469,7 +469,7 @@ void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message,
llvm::raw_string_ostream locOS(locStr);
locOS << fileLoc.getFilename().getValue() << ":" << fileLoc.getLine() << ":"
<< fileLoc.getColumn();
llvm::SMDiagnostic diag(locOS.str(), getDiagKind(kind), message.str());
llvm::SMDiagnostic diag(locStr, getDiagKind(kind), message.str());
diag.print(nullptr, os);
}
@@ -637,7 +637,7 @@ struct ExpectedDiag {
regexOS << '(' << regexStr << ')';
strToProcess = strToProcess.drop_front(regexEndIt + 2);
}
substringRegex = llvm::Regex(regexOS.str());
substringRegex = llvm::Regex(regexStr);
return success();
}

View File

@@ -29,7 +29,7 @@ using namespace mlir;
os << "neither the scoping op nor the type class provide data layout "
"information for "
<< type;
llvm::report_fatal_error(Twine(os.str()));
llvm::report_fatal_error(Twine(message));
}
/// Returns the bitwidth of the index type if specified in the param list.

View File

@@ -30,7 +30,7 @@ RuntimeVerifiableOpInterface::generateErrorMessage(Operation *op,
stream << "\n^ " << msg;
stream << "\nLocation: ";
op->getLoc().print(stream);
return stream.str();
return buffer;
}
} // namespace mlir

View File

@@ -66,7 +66,6 @@ LogicalResult Pass::initializeOptions(
std::string errStr;
llvm::raw_string_ostream os(errStr);
if (failed(passOptions.parseFromString(options, os))) {
os.flush();
return errorHandler(errStr);
}
return success();
@@ -700,7 +699,7 @@ std::string OpToOpPassAdaptor::getAdaptorName() {
os << '\'' << pm.getOpAnchorName() << '\'';
});
os << ']';
return os.str();
return name;
}
void OpToOpPassAdaptor::runOnOperation() {

View File

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

View File

@@ -124,7 +124,7 @@ RegistryManager::getMatcherCompletions(llvm::ArrayRef<ArgKind> acceptedTypes,
else if (argKinds[0][0] == ArgKind::String)
typedText += "\"";
completions.emplace_back(typedText, os.str());
completions.emplace_back(typedText, decl);
}
return completions;

View File

@@ -123,7 +123,7 @@ makeInvalidQueryFromDiagnostics(const matcher::internal::Diagnostics &diag) {
std::string errStr;
llvm::raw_string_ostream os(errStr);
diag.print(os);
return new InvalidQuery(os.str());
return new InvalidQuery(errStr);
}
} // namespace