aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatteo Franciolini <m_franciolini@apple.com>2023-11-13 12:59:30 -0600
committerGitHub <noreply@github.com>2023-11-13 12:59:30 -0600
commit4488f4933ee2a5326be643e8d1afec623c318fb9 (patch)
tree136c9a8dcd3f12c8aa3df0a334f835a766d8cfd4
parent08e8dacb4ad777ff06b747006c838c0fbab4ff51 (diff)
downloadllvm-4488f4933ee2a5326be643e8d1afec623c318fb9.zip
llvm-4488f4933ee2a5326be643e8d1afec623c318fb9.tar.gz
llvm-4488f4933ee2a5326be643e8d1afec623c318fb9.tar.bz2
[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.
-rw-r--r--mlir/include/mlir/Bytecode/BytecodeWriter.h3
-rw-r--r--mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h6
-rw-r--r--mlir/lib/Bytecode/Writer/BytecodeWriter.cpp26
-rw-r--r--mlir/lib/Tools/mlir-opt/MlirOptMain.cpp7
-rw-r--r--mlir/test/Bytecode/resources_elision.mlir21
5 files changed, 57 insertions, 6 deletions
diff --git a/mlir/include/mlir/Bytecode/BytecodeWriter.h b/mlir/include/mlir/Bytecode/BytecodeWriter.h
index b82d8dd..ea4b368 100644
--- a/mlir/include/mlir/Bytecode/BytecodeWriter.h
+++ b/mlir/include/mlir/Bytecode/BytecodeWriter.h
@@ -152,6 +152,9 @@ public:
// Resources
//===--------------------------------------------------------------------===//
+ /// Set a boolean flag to skip emission of resources into the bytecode file.
+ void setElideResourceDataFlag(bool shouldElideResourceData = true);
+
/// Attach the given resource printer to the writer configuration.
void attachResourcePrinter(std::unique_ptr<AsmResourcePrinter> printer);
diff --git a/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h b/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
index a153093..e255d9f 100644
--- a/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
+++ b/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
@@ -82,6 +82,9 @@ public:
return *this;
}
bool shouldEmitBytecode() const { return emitBytecodeFlag; }
+ bool shouldElideResourceDataFromBytecode() const {
+ return elideResourceDataFromBytecodeFlag;
+ }
/// Set the IRDL file to load before processing the input.
MlirOptMainConfig &setIrdlFile(StringRef file) {
@@ -185,6 +188,9 @@ protected:
/// Emit bytecode instead of textual assembly when generating output.
bool emitBytecodeFlag = false;
+ /// Elide resources when generating bytecode.
+ bool elideResourceDataFromBytecodeFlag = false;
+
/// Enable the Debugger action hook: Debugger can intercept MLIR Actions.
bool enableDebuggerActionHookFlag = false;
diff --git a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
index 01dcea1..6097f0e 100644
--- a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
+++ b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
@@ -39,6 +39,10 @@ struct BytecodeWriterConfig::Impl {
/// Note: This only differs from kVersion if a specific version is set.
int64_t bytecodeVersion = bytecode::kVersion;
+ /// A flag specifying whether to elide emission of resources into the bytecode
+ /// file.
+ bool shouldElideResourceData = false;
+
/// A map containing dialect version information for each dialect to emit.
llvm::StringMap<std::unique_ptr<DialectVersion>> dialectVersionMap;
@@ -89,6 +93,11 @@ void BytecodeWriterConfig::attachResourcePrinter(
impl->externalResourcePrinters.emplace_back(std::move(printer));
}
+void BytecodeWriterConfig::setElideResourceDataFlag(
+ bool shouldElideResourceData) {
+ impl->shouldElideResourceData = shouldElideResourceData;
+}
+
void BytecodeWriterConfig::setDesiredBytecodeVersion(int64_t bytecodeVersion) {
impl->bytecodeVersion = bytecodeVersion;
}
@@ -1170,22 +1179,25 @@ public:
using PostProcessFn = function_ref<void(StringRef, AsmResourceEntryKind)>;
ResourceBuilder(EncodingEmitter &emitter, StringSectionBuilder &stringSection,
- PostProcessFn postProcessFn)
+ PostProcessFn postProcessFn, bool shouldElideData)
: emitter(emitter), stringSection(stringSection),
- postProcessFn(postProcessFn) {}
+ postProcessFn(postProcessFn), shouldElideData(shouldElideData) {}
~ResourceBuilder() override = default;
void buildBlob(StringRef key, ArrayRef<char> data,
uint32_t dataAlignment) final {
- emitter.emitOwnedBlobAndAlignment(data, dataAlignment);
+ if (!shouldElideData)
+ emitter.emitOwnedBlobAndAlignment(data, dataAlignment);
postProcessFn(key, AsmResourceEntryKind::Blob);
}
void buildBool(StringRef key, bool data) final {
- emitter.emitByte(data);
+ if (!shouldElideData)
+ emitter.emitByte(data);
postProcessFn(key, AsmResourceEntryKind::Bool);
}
void buildString(StringRef key, StringRef data) final {
- emitter.emitVarInt(stringSection.insert(data));
+ if (!shouldElideData)
+ emitter.emitVarInt(stringSection.insert(data));
postProcessFn(key, AsmResourceEntryKind::String);
}
@@ -1193,6 +1205,7 @@ private:
EncodingEmitter &emitter;
StringSectionBuilder &stringSection;
PostProcessFn postProcessFn;
+ bool shouldElideData = false;
};
} // namespace
@@ -1225,7 +1238,8 @@ void BytecodeWriter::writeResourceSection(Operation *op,
// Builder used to emit resources.
ResourceBuilder entryBuilder(resourceEmitter, stringSection,
- appendResourceOffset);
+ appendResourceOffset,
+ config.shouldElideResourceData);
// Emit the external resource entries.
resourceOffsetEmitter.emitVarInt(config.externalResourcePrinters.size());
diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
index c36afae..d7d4761 100644
--- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
+++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
@@ -90,6 +90,11 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
"emit-bytecode", cl::desc("Emit bytecode when generating output"),
cl::location(emitBytecodeFlag), cl::init(false));
+ static cl::opt<bool, /*ExternalStorage=*/true> elideResourcesFromBytecode(
+ "elide-resource-data-from-bytecode",
+ cl::desc("Elide resources when generating bytecode"),
+ cl::location(elideResourceDataFromBytecodeFlag), cl::init(false));
+
static cl::opt<std::optional<int64_t>, /*ExternalStorage=*/true,
BytecodeVersionParser>
bytecodeVersion(
@@ -385,6 +390,8 @@ performActions(raw_ostream &os,
BytecodeWriterConfig writerConfig(fallbackResourceMap);
if (auto v = config.bytecodeVersionToEmit())
writerConfig.setDesiredBytecodeVersion(*v);
+ if (config.shouldElideResourceDataFromBytecode())
+ writerConfig.setElideResourceDataFlag();
return writeBytecodeToFile(op.get(), os, writerConfig);
}
diff --git a/mlir/test/Bytecode/resources_elision.mlir b/mlir/test/Bytecode/resources_elision.mlir
new file mode 100644
index 0000000..bca7001
--- /dev/null
+++ b/mlir/test/Bytecode/resources_elision.mlir
@@ -0,0 +1,21 @@
+// RUN: mlir-opt -emit-bytecode -elide-resource-data-from-bytecode %s | mlir-opt | FileCheck %s
+
+// CHECK-LABEL: @TestDialectResources
+module @TestDialectResources attributes {
+ // CHECK: bytecode.test = dense_resource<decl_resource> : tensor<2xui32>
+ // CHECK: bytecode.test2 = dense_resource<resource> : tensor<4xf64>
+ // CHECK: bytecode.test3 = dense_resource<resource_2> : tensor<4xf64>
+ bytecode.test = dense_resource<decl_resource> : tensor<2xui32>,
+ bytecode.test2 = dense_resource<resource> : tensor<4xf64>,
+ bytecode.test3 = dense_resource<resource_2> : tensor<4xf64>
+} {}
+
+// CHECK-NOT: dialect_resources
+{-#
+ dialect_resources: {
+ builtin: {
+ resource: "0x08000000010000000000000002000000000000000300000000000000",
+ resource_2: "0x08000000010000000000000002000000000000000300000000000000"
+ }
+ }
+#-}