[Mlir-commits] [mlir] [mlir] Python: write bytecode to a file path (PR #127118)

Mehdi Amini llvmlistbot at llvm.org
Thu Feb 13 16:02:50 PST 2025


================
@@ -1351,6 +1352,27 @@ void PyOperationBase::writeBytecode(const nb::object &fileObject,
                               .c_str());
 }
 
+void PyOperationBase::writeBytecode(const nb::object &fileObject,
+                                    std::optional<int64_t> bytecodeVersion) {
+  PyOperation &operation = getOperation();
+  operation.checkValid();
+
+  std::string filePath;
+  if (nb::try_cast<std::string>(fileObject, filePath)) {
+    std::error_code ec;
+    llvm::raw_fd_ostream ostream(filePath, ec);
+    if (ec) {
+      throw nb::value_error("Unable to open file for writing");
+    }
+
+    OstreamAccumulator accum(ostream);
+    writeBytecodeForOperation(accum, operation, bytecodeVersion);
+  } else {
+    PyFileAccumulator accum(fileObject, /*binary=*/true);
+    writeBytecodeForOperation(accum, operation, bytecodeVersion);
----------------
joker-eph wrote:

Would this logic be something we could just move to the `PyFileAccumulator` itself so that every usage benefits from this optimization  instead of specializing it everywhere?

https://github.com/llvm/llvm-project/pull/127118


More information about the Mlir-commits mailing list