Skip to content

Commit 4488f49

Browse files
author
Matteo Franciolini
authored
[mlir][bytecode] Add bytecode writer config API to skip serialization of resources (#71991)
When serializing to bytecode, users can select the option to elide resources from the bytecode file. This will instruct the bytecode writer to serialize only the key and resource kind, while skipping serialization of the data buffer. At parsing, the IR is built in memory with valid (but empty) resource handlers.
1 parent 08e8dac commit 4488f49

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

mlir/include/mlir/Bytecode/BytecodeWriter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class BytecodeWriterConfig {
152152
// Resources
153153
//===--------------------------------------------------------------------===//
154154

155+
/// Set a boolean flag to skip emission of resources into the bytecode file.
156+
void setElideResourceDataFlag(bool shouldElideResourceData = true);
157+
155158
/// Attach the given resource printer to the writer configuration.
156159
void attachResourcePrinter(std::unique_ptr<AsmResourcePrinter> printer);
157160

mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class MlirOptMainConfig {
8282
return *this;
8383
}
8484
bool shouldEmitBytecode() const { return emitBytecodeFlag; }
85+
bool shouldElideResourceDataFromBytecode() const {
86+
return elideResourceDataFromBytecodeFlag;
87+
}
8588

8689
/// Set the IRDL file to load before processing the input.
8790
MlirOptMainConfig &setIrdlFile(StringRef file) {
@@ -185,6 +188,9 @@ class MlirOptMainConfig {
185188
/// Emit bytecode instead of textual assembly when generating output.
186189
bool emitBytecodeFlag = false;
187190

191+
/// Elide resources when generating bytecode.
192+
bool elideResourceDataFromBytecodeFlag = false;
193+
188194
/// Enable the Debugger action hook: Debugger can intercept MLIR Actions.
189195
bool enableDebuggerActionHookFlag = false;
190196

mlir/lib/Bytecode/Writer/BytecodeWriter.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ struct BytecodeWriterConfig::Impl {
3939
/// Note: This only differs from kVersion if a specific version is set.
4040
int64_t bytecodeVersion = bytecode::kVersion;
4141

42+
/// A flag specifying whether to elide emission of resources into the bytecode
43+
/// file.
44+
bool shouldElideResourceData = false;
45+
4246
/// A map containing dialect version information for each dialect to emit.
4347
llvm::StringMap<std::unique_ptr<DialectVersion>> dialectVersionMap;
4448

@@ -89,6 +93,11 @@ void BytecodeWriterConfig::attachResourcePrinter(
8993
impl->externalResourcePrinters.emplace_back(std::move(printer));
9094
}
9195

96+
void BytecodeWriterConfig::setElideResourceDataFlag(
97+
bool shouldElideResourceData) {
98+
impl->shouldElideResourceData = shouldElideResourceData;
99+
}
100+
92101
void BytecodeWriterConfig::setDesiredBytecodeVersion(int64_t bytecodeVersion) {
93102
impl->bytecodeVersion = bytecodeVersion;
94103
}
@@ -1170,29 +1179,33 @@ class ResourceBuilder : public AsmResourceBuilder {
11701179
using PostProcessFn = function_ref<void(StringRef, AsmResourceEntryKind)>;
11711180

11721181
ResourceBuilder(EncodingEmitter &emitter, StringSectionBuilder &stringSection,
1173-
PostProcessFn postProcessFn)
1182+
PostProcessFn postProcessFn, bool shouldElideData)
11741183
: emitter(emitter), stringSection(stringSection),
1175-
postProcessFn(postProcessFn) {}
1184+
postProcessFn(postProcessFn), shouldElideData(shouldElideData) {}
11761185
~ResourceBuilder() override = default;
11771186

11781187
void buildBlob(StringRef key, ArrayRef<char> data,
11791188
uint32_t dataAlignment) final {
1180-
emitter.emitOwnedBlobAndAlignment(data, dataAlignment);
1189+
if (!shouldElideData)
1190+
emitter.emitOwnedBlobAndAlignment(data, dataAlignment);
11811191
postProcessFn(key, AsmResourceEntryKind::Blob);
11821192
}
11831193
void buildBool(StringRef key, bool data) final {
1184-
emitter.emitByte(data);
1194+
if (!shouldElideData)
1195+
emitter.emitByte(data);
11851196
postProcessFn(key, AsmResourceEntryKind::Bool);
11861197
}
11871198
void buildString(StringRef key, StringRef data) final {
1188-
emitter.emitVarInt(stringSection.insert(data));
1199+
if (!shouldElideData)
1200+
emitter.emitVarInt(stringSection.insert(data));
11891201
postProcessFn(key, AsmResourceEntryKind::String);
11901202
}
11911203

11921204
private:
11931205
EncodingEmitter &emitter;
11941206
StringSectionBuilder &stringSection;
11951207
PostProcessFn postProcessFn;
1208+
bool shouldElideData = false;
11961209
};
11971210
} // namespace
11981211

@@ -1225,7 +1238,8 @@ void BytecodeWriter::writeResourceSection(Operation *op,
12251238

12261239
// Builder used to emit resources.
12271240
ResourceBuilder entryBuilder(resourceEmitter, stringSection,
1228-
appendResourceOffset);
1241+
appendResourceOffset,
1242+
config.shouldElideResourceData);
12291243

12301244
// Emit the external resource entries.
12311245
resourceOffsetEmitter.emitVarInt(config.externalResourcePrinters.size());

mlir/lib/Tools/mlir-opt/MlirOptMain.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
9090
"emit-bytecode", cl::desc("Emit bytecode when generating output"),
9191
cl::location(emitBytecodeFlag), cl::init(false));
9292

93+
static cl::opt<bool, /*ExternalStorage=*/true> elideResourcesFromBytecode(
94+
"elide-resource-data-from-bytecode",
95+
cl::desc("Elide resources when generating bytecode"),
96+
cl::location(elideResourceDataFromBytecodeFlag), cl::init(false));
97+
9398
static cl::opt<std::optional<int64_t>, /*ExternalStorage=*/true,
9499
BytecodeVersionParser>
95100
bytecodeVersion(
@@ -385,6 +390,8 @@ performActions(raw_ostream &os,
385390
BytecodeWriterConfig writerConfig(fallbackResourceMap);
386391
if (auto v = config.bytecodeVersionToEmit())
387392
writerConfig.setDesiredBytecodeVersion(*v);
393+
if (config.shouldElideResourceDataFromBytecode())
394+
writerConfig.setElideResourceDataFlag();
388395
return writeBytecodeToFile(op.get(), os, writerConfig);
389396
}
390397

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: mlir-opt -emit-bytecode -elide-resource-data-from-bytecode %s | mlir-opt | FileCheck %s
2+
3+
// CHECK-LABEL: @TestDialectResources
4+
module @TestDialectResources attributes {
5+
// CHECK: bytecode.test = dense_resource<decl_resource> : tensor<2xui32>
6+
// CHECK: bytecode.test2 = dense_resource<resource> : tensor<4xf64>
7+
// CHECK: bytecode.test3 = dense_resource<resource_2> : tensor<4xf64>
8+
bytecode.test = dense_resource<decl_resource> : tensor<2xui32>,
9+
bytecode.test2 = dense_resource<resource> : tensor<4xf64>,
10+
bytecode.test3 = dense_resource<resource_2> : tensor<4xf64>
11+
} {}
12+
13+
// CHECK-NOT: dialect_resources
14+
{-#
15+
dialect_resources: {
16+
builtin: {
17+
resource: "0x08000000010000000000000002000000000000000300000000000000",
18+
resource_2: "0x08000000010000000000000002000000000000000300000000000000"
19+
}
20+
}
21+
#-}

0 commit comments

Comments
 (0)