summaryrefslogtreecommitdiff
path: root/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
diff options
context:
space:
mode:
authorRiver Riddle <riddleriver@gmail.com>2022-08-23 01:28:10 -0700
committerRiver Riddle <riddleriver@gmail.com>2022-08-23 16:56:03 -0700
commit83dc9999486fb3fb7e11d684312d034128d1b050 (patch)
tree032ee3dfd14419a7f62f3ea33cbc4c3e5d530ff3 /mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
parentd10c1b88f0a010975576584d8d1d4ac6a7e2454b (diff)
[mlir:Bytecode][NFC] Refactor string section writing and reading
This extracts the string section writer and reader into dedicated classes, which better separates the logic and will also simplify future patches that want to interact with the string section. Differential Revision: https://reviews.llvm.org/D132496
Diffstat (limited to 'mlir/lib/Bytecode/Writer/BytecodeWriter.cpp')
-rw-r--r--mlir/lib/Bytecode/Writer/BytecodeWriter.cpp66
1 files changed, 41 insertions, 25 deletions
diff --git a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
index 8bf37c18030b..ebf827fde41b 100644
--- a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
+++ b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
@@ -197,6 +197,41 @@ void EncodingEmitter::emitMultiByteVarInt(uint64_t value) {
}
//===----------------------------------------------------------------------===//
+// StringSectionBuilder
+//===----------------------------------------------------------------------===//
+
+namespace {
+/// This class is used to simplify the process of emitting the string section.
+class StringSectionBuilder {
+public:
+ /// Add the given string to the string section, and return the index of the
+ /// string within the section.
+ size_t insert(StringRef str) {
+ auto it = strings.insert({llvm::CachedHashStringRef(str), strings.size()});
+ return it.first->second;
+ }
+
+ /// Write the current set of strings to the given emitter.
+ void write(EncodingEmitter &emitter) {
+ emitter.emitVarInt(strings.size());
+
+ // Emit the sizes in reverse order, so that we don't need to backpatch an
+ // offset to the string data or have a separate section.
+ for (const auto &it : llvm::reverse(strings))
+ emitter.emitVarInt(it.first.size() + 1);
+ // Emit the string data itself.
+ for (const auto &it : strings)
+ emitter.emitNulTerminatedString(it.first.val());
+ }
+
+private:
+ /// A set of strings referenced within the bytecode. The value of the map is
+ /// unused.
+ llvm::MapVector<llvm::CachedHashStringRef, size_t> strings;
+};
+} // namespace
+
+//===----------------------------------------------------------------------===//
// Bytecode Writer
//===----------------------------------------------------------------------===//
@@ -232,19 +267,14 @@ private:
void writeStringSection(EncodingEmitter &emitter);
- /// Get the number for the given shared string, that is contained within the
- /// string section.
- size_t getSharedStringNumber(StringRef str);
-
//===--------------------------------------------------------------------===//
// Fields
+ /// The builder used for the string section.
+ StringSectionBuilder stringSection;
+
/// The IR numbering state generated for the root operation.
IRNumberingState numberingState;
-
- /// A set of strings referenced within the bytecode. The value of the map is
- /// unused.
- llvm::MapVector<llvm::CachedHashStringRef, size_t> strings;
};
} // namespace
@@ -314,11 +344,11 @@ void BytecodeWriter::writeDialectSection(EncodingEmitter &emitter) {
auto dialects = numberingState.getDialects();
dialectEmitter.emitVarInt(llvm::size(dialects));
for (DialectNumbering &dialect : dialects)
- dialectEmitter.emitVarInt(getSharedStringNumber(dialect.name));
+ dialectEmitter.emitVarInt(stringSection.insert(dialect.name));
// Emit the referenced operation names grouped by dialect.
auto emitOpName = [&](OpNameNumbering &name) {
- dialectEmitter.emitVarInt(getSharedStringNumber(name.name.stripDialect()));
+ dialectEmitter.emitVarInt(stringSection.insert(name.name.stripDialect()));
};
writeDialectGrouping(dialectEmitter, numberingState.getOpNames(), emitOpName);
@@ -491,24 +521,10 @@ void BytecodeWriter::writeIRSection(EncodingEmitter &emitter, Operation *op) {
void BytecodeWriter::writeStringSection(EncodingEmitter &emitter) {
EncodingEmitter stringEmitter;
- stringEmitter.emitVarInt(strings.size());
-
- // Emit the sizes in reverse order, so that we don't need to backpatch an
- // offset to the string data or have a separate section.
- for (const auto &it : llvm::reverse(strings))
- stringEmitter.emitVarInt(it.first.size() + 1);
- // Emit the string data itself.
- for (const auto &it : strings)
- stringEmitter.emitNulTerminatedString(it.first.val());
-
+ stringSection.write(stringEmitter);
emitter.emitSection(bytecode::Section::kString, std::move(stringEmitter));
}
-size_t BytecodeWriter::getSharedStringNumber(StringRef str) {
- auto it = strings.insert({llvm::CachedHashStringRef(str), strings.size()});
- return it.first->second;
-}
-
//===----------------------------------------------------------------------===//
// Entry Points
//===----------------------------------------------------------------------===//