[llvm] e166d2e - [llvm-remarkutil] NFC: Combine parsing + serializing for bitstream2yaml
Jessica Paquette via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 13 11:36:01 PDT 2022
Author: Jessica Paquette
Date: 2022-09-13T11:34:33-07:00
New Revision: e166d2e00bc0624cd83d9c790bc4b8c80446d126
URL: https://github.com/llvm/llvm-project/commit/e166d2e00bc0624cd83d9c790bc4b8c80446d126
DIFF: https://github.com/llvm/llvm-project/commit/e166d2e00bc0624cd83d9c790bc4b8c80446d126.diff
LOG: [llvm-remarkutil] NFC: Combine parsing + serializing for bitstream2yaml
We don't need to populate a string table, so we don't need to pre-parse the
remarks.
Split the bitstream2yaml and yaml2bitstream cases up so that we can avoid the
extra remark traversal in the bitstream2yaml case.
This kills the TODO.
Added:
Modified:
llvm/tools/llvm-remarkutil/RemarkUtil.cpp
Removed:
################################################################################
diff --git a/llvm/tools/llvm-remarkutil/RemarkUtil.cpp b/llvm/tools/llvm-remarkutil/RemarkUtil.cpp
index 09ba70c633cf..ecd56b5d46cb 100644
--- a/llvm/tools/llvm-remarkutil/RemarkUtil.cpp
+++ b/llvm/tools/llvm-remarkutil/RemarkUtil.cpp
@@ -75,14 +75,30 @@ getInputMemoryBuffer(StringRef InputFileName) {
return std::move(*MaybeBuf);
}
-/// Parses all remarks in the input file.
+/// \returns A ToolOutputFile which can be used for writing remarks on success,
+/// and an Error otherwise.
+static Expected<std::unique_ptr<ToolOutputFile>>
+getOutputFile(StringRef OutputFileName, Format OutputFormat) {
+ if (OutputFileName == "")
+ OutputFileName = "-";
+ auto Flags = OutputFormat == Format::YAML ? sys::fs::OF_TextWithCRLF
+ : sys::fs::OF_None;
+ std::error_code ErrorCode;
+ auto OF = std::make_unique<ToolOutputFile>(OutputFileName, ErrorCode, Flags);
+ if (ErrorCode)
+ return errorCodeToError(ErrorCode);
+ return std::move(OF);
+}
+
+namespace yaml2bitstream {
+/// Parses all remarks in the input YAML file.
/// \p [out] ParsedRemarks - Filled with remarks parsed from the input file.
/// \p [out] StrTab - A string table populated for later remark serialization.
/// \returns Error::success() if all remarks were successfully parsed, and an
/// Error otherwise.
-static Error tryParseRemarksFromInputFile(
- StringRef InputFileName, Format InputFormat,
- std::vector<std::unique_ptr<Remark>> &ParsedRemarks, StringTable &StrTab) {
+static Error
+tryParseRemarksFromYAMLFile(std::vector<std::unique_ptr<Remark>> &ParsedRemarks,
+ StringTable &StrTab) {
auto MaybeBuf = getInputMemoryBuffer(InputFileName);
if (!MaybeBuf)
return MaybeBuf.takeError();
@@ -91,8 +107,6 @@ static Error tryParseRemarksFromInputFile(
return MaybeParser.takeError();
auto &Parser = **MaybeParser;
auto MaybeRemark = Parser.next();
- // TODO: If we are converting from bitstream to YAML, we don't need to parse
- // early because the string table is not necessary.
for (; MaybeRemark; MaybeRemark = Parser.next()) {
StrTab.internalize(**MaybeRemark);
ParsedRemarks.push_back(std::move(*MaybeRemark));
@@ -104,28 +118,11 @@ static Error tryParseRemarksFromInputFile(
return Error::success();
}
-/// \returns A ToolOutputFile which can be used for writing remarks on success,
-/// and an Error otherwise.
-static Expected<std::unique_ptr<ToolOutputFile>>
-getOutputFile(StringRef OutputFileName, Format OutputFormat) {
- if (OutputFileName == "")
- OutputFileName = "-";
- auto Flags = OutputFormat == Format::YAML ? sys::fs::OF_TextWithCRLF
- : sys::fs::OF_None;
- std::error_code ErrorCode;
- auto OF = std::make_unique<ToolOutputFile>(OutputFileName, ErrorCode, Flags);
- if (ErrorCode)
- return errorCodeToError(ErrorCode);
- return std::move(OF);
-}
-
-/// Reserialize a list of remarks into the desired output format, and output
-/// to the user-specified output file.
+/// Reserialize a list of parsed YAML remarks into bitstream remarks.
/// \p ParsedRemarks - A list of remarks.
/// \p StrTab - The string table for the remarks.
/// \returns Error::success() on success.
-static Error tryReserializeParsedRemarks(
- StringRef OutputFileName, Format OutputFormat,
+static Error tryReserializeYAML2Bitstream(
const std::vector<std::unique_ptr<Remark>> &ParsedRemarks,
StringTable &StrTab) {
auto MaybeOF = getOutputFile(OutputFileName, OutputFormat);
@@ -143,45 +140,60 @@ static Error tryReserializeParsedRemarks(
return Error::success();
}
-/// Parses remarks in the input format, and reserializes them in the desired
-/// output format.
+/// Parse YAML remarks and reserialize as bitstream remarks.
/// \returns Error::success() on success, and an Error otherwise.
-static Error tryReserialize(StringRef InputFileName, StringRef OutputFileName,
- Format InputFormat, Format OutputFormat) {
+static Error tryYAML2Bitstream() {
StringTable StrTab;
std::vector<std::unique_ptr<Remark>> ParsedRemarks;
- ExitOnErr(tryParseRemarksFromInputFile(InputFileName, InputFormat,
- ParsedRemarks, StrTab));
- return tryReserializeParsedRemarks(OutputFileName, OutputFormat,
- ParsedRemarks, StrTab);
+ ExitOnErr(tryParseRemarksFromYAMLFile(ParsedRemarks, StrTab));
+ return tryReserializeYAML2Bitstream(ParsedRemarks, StrTab);
}
+} // namespace yaml2bitstream
-/// Reserialize bitstream remarks as YAML remarks.
+namespace bitstream2yaml {
+/// Parse bitstream remarks and reserialize as YAML remarks.
/// \returns An Error if reserialization fails, or Error::success() on success.
static Error tryBitstream2YAML() {
- // Use the namespace to get the correct command line globals.
- using namespace bitstream2yaml;
- return tryReserialize(InputFileName, OutputFileName, InputFormat,
- OutputFormat);
-}
+ // Create the serializer.
+ auto MaybeOF = getOutputFile(OutputFileName, OutputFormat);
+ if (!MaybeOF)
+ return MaybeOF.takeError();
+ auto OF = std::move(*MaybeOF);
+ auto MaybeSerializer = createRemarkSerializer(
+ OutputFormat, SerializerMode::Standalone, OF->os());
+ if (!MaybeSerializer)
+ return MaybeSerializer.takeError();
-/// Reserialize YAML remarks as bitstream remarks.
-/// \returns An Error if reserialization fails, or Error::success() on success.
-static Error tryYAML2Bitstream() {
- // Use the namespace to get the correct command line globals.
- using namespace yaml2bitstream;
- return tryReserialize(InputFileName, OutputFileName, InputFormat,
- OutputFormat);
+ // Create the parser.
+ auto MaybeBuf = getInputMemoryBuffer(InputFileName);
+ if (!MaybeBuf)
+ return MaybeBuf.takeError();
+ auto Serializer = std::move(*MaybeSerializer);
+ auto MaybeParser = createRemarkParser(InputFormat, (*MaybeBuf)->getBuffer());
+ if (!MaybeParser)
+ return MaybeParser.takeError();
+ auto &Parser = **MaybeParser;
+
+ // Parse + reserialize all remarks.
+ auto MaybeRemark = Parser.next();
+ for (; MaybeRemark; MaybeRemark = Parser.next())
+ Serializer->emit(**MaybeRemark);
+ auto E = MaybeRemark.takeError();
+ if (!E.isA<EndOfFileError>())
+ return E;
+ consumeError(std::move(E));
+ return Error::success();
}
+} // namespace bitstream2yaml
/// Handle user-specified suboptions (e.g. yaml2bitstream, bitstream2yaml).
/// \returns An Error if the specified suboption fails or if no suboption was
/// specified. Otherwise, Error::success().
static Error handleSuboptions() {
if (subopts::Bitstream2YAML)
- return tryBitstream2YAML();
+ return bitstream2yaml::tryBitstream2YAML();
if (subopts::YAML2Bitstream)
- return tryYAML2Bitstream();
+ return yaml2bitstream::tryYAML2Bitstream();
return make_error<StringError>(
"Please specify a subcommand. (See -help for options)",
inconvertibleErrorCode());
More information about the llvm-commits
mailing list