[Mlir-commits] [mlir] [mlir] Enable specifying bytecode producer in mlir-opt. (PR #182846)

Jacques Pienaar llvmlistbot at llvm.org
Fri Feb 27 05:49:38 PST 2026


https://github.com/jpienaar updated https://github.com/llvm/llvm-project/pull/182846

>From 8a402a57c589bb36d42102c30428a0e9866a541b Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Mon, 23 Feb 2026 15:34:42 +0200
Subject: [PATCH 1/2] [mlir] Enable specifying bytecode producer in mlir-opt.

---
 mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h | 14 ++++++++++++++
 mlir/lib/Tools/mlir-opt/MlirOptMain.cpp        |  9 ++++++++-
 mlir/test/Bytecode/bytecode_producer.mlir      |  5 +++++
 3 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 mlir/test/Bytecode/bytecode_producer.mlir

diff --git a/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h b/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
index 79dfd7a2795f0..7e0b7024608c9 100644
--- a/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
+++ b/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
@@ -128,6 +128,17 @@ class MlirOptMainConfig {
     return emitBytecodeVersion;
   }
 
+  /// Set the bytecode producer to use.
+  MlirOptMainConfig &emitBytecodeProducer(StringRef producer) {
+    emitBytecodeProducerFlag = producer.str();
+    return *this;
+  }
+  std::optional<StringRef> bytecodeProducerToEmit() const {
+    if (emitBytecodeProducerFlag.empty())
+      return std::nullopt;
+    return emitBytecodeProducerFlag;
+  }
+
   /// Set the callback to populate the pass manager.
   MlirOptMainConfig &
   setPassPipelineSetupFn(std::function<LogicalResult(PassManager &)> callback) {
@@ -309,6 +320,9 @@ class MlirOptMainConfig {
   /// Emit bytecode at given version.
   std::optional<int64_t> emitBytecodeVersion = std::nullopt;
 
+  /// Emit bytecode with given producer.
+  std::string emitBytecodeProducerFlag = "";
+
   /// The callback to populate the pass manager.
   std::function<LogicalResult(PassManager &)> passPipelineCallback;
 
diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
index 560ef6effd2fb..7973f6992621f 100644
--- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
+++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
@@ -94,6 +94,11 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
         cl::desc("Elide resources when generating bytecode"),
         cl::location(elideResourceDataFromBytecodeFlag), cl::init(false));
 
+    static cl::opt<std::string, /*ExternalStorage=*/true> emitBytecodeProducer(
+        "emit-bytecode-producer",
+        cl::desc("Use specified producer when generating bytecode output"),
+        cl::location(emitBytecodeProducerFlag), cl::init(""));
+
     static cl::opt<std::optional<int64_t>, /*ExternalStorage=*/true,
                    BytecodeVersionParser>
         bytecodeVersion(
@@ -602,7 +607,9 @@ performActions(raw_ostream &os,
   // Print the output.
   TimingScope outputTiming = timing.nest("Output");
   if (config.shouldEmitBytecode()) {
-    BytecodeWriterConfig writerConfig(fallbackResourceMap);
+    std::optional<StringRef> producer = config.bytecodeProducerToEmit();
+    BytecodeWriterConfig writerConfig(
+        fallbackResourceMap, producer.value_or("MLIR" LLVM_VERSION_STRING));
     if (auto v = config.bytecodeVersionToEmit())
       writerConfig.setDesiredBytecodeVersion(*v);
     if (config.shouldElideResourceDataFromBytecode())
diff --git a/mlir/test/Bytecode/bytecode_producer.mlir b/mlir/test/Bytecode/bytecode_producer.mlir
new file mode 100644
index 0000000000000..5b47e7d210a91
--- /dev/null
+++ b/mlir/test/Bytecode/bytecode_producer.mlir
@@ -0,0 +1,5 @@
+// RUN: mlir-opt %s -emit-bytecode -emit-bytecode-producer="MyCustomProducer" | strings | FileCheck %s
+
+// CHECK: MyCustomProducer
+
+module {}

>From 8eda07eb4b2b260ec3e01c0d653af8c32203e7f4 Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Fri, 27 Feb 2026 15:49:20 +0200
Subject: [PATCH 2/2] Avoid repeated default producer string

---
 mlir/lib/Tools/mlir-opt/MlirOptMain.cpp   | 5 +++--
 mlir/test/Bytecode/bytecode_producer.mlir | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
index 7973f6992621f..f90abda3463ab 100644
--- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
+++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
@@ -608,8 +608,9 @@ performActions(raw_ostream &os,
   TimingScope outputTiming = timing.nest("Output");
   if (config.shouldEmitBytecode()) {
     std::optional<StringRef> producer = config.bytecodeProducerToEmit();
-    BytecodeWriterConfig writerConfig(
-        fallbackResourceMap, producer.value_or("MLIR" LLVM_VERSION_STRING));
+    BytecodeWriterConfig writerConfig =
+        producer ? BytecodeWriterConfig(fallbackResourceMap, producer.value())
+                 : BytecodeWriterConfig(fallbackResourceMap);
     if (auto v = config.bytecodeVersionToEmit())
       writerConfig.setDesiredBytecodeVersion(*v);
     if (config.shouldElideResourceDataFromBytecode())
diff --git a/mlir/test/Bytecode/bytecode_producer.mlir b/mlir/test/Bytecode/bytecode_producer.mlir
index 5b47e7d210a91..ba13dd3de9dd9 100644
--- a/mlir/test/Bytecode/bytecode_producer.mlir
+++ b/mlir/test/Bytecode/bytecode_producer.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -emit-bytecode -emit-bytecode-producer="MyCustomProducer" | strings | FileCheck %s
+// RUN: mlir-opt %s -emit-bytecode -emit-bytecode-producer="MyCustomProducer" | llvm-strings | FileCheck %s
 
 // CHECK: MyCustomProducer
 



More information about the Mlir-commits mailing list