-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir] Python: write bytecode to a file path #127118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b6d637b
3be708c
0545484
4ea6161
1299a7c
8fe1552
7cfe3b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,21 +7,24 @@ | |
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <optional> | ||
| #include <system_error> | ||
| #include <utility> | ||
|
|
||
| #include "Globals.h" | ||
| #include "IRModule.h" | ||
| #include "NanobindUtils.h" | ||
| #include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind. | ||
| #include "mlir-c/BuiltinAttributes.h" | ||
| #include "mlir-c/Debug.h" | ||
| #include "mlir-c/Diagnostics.h" | ||
| #include "mlir-c/IR.h" | ||
| #include "mlir-c/Support.h" | ||
| #include "mlir/Bindings/Python/Nanobind.h" | ||
| #include "mlir/Bindings/Python/NanobindAdaptors.h" | ||
| #include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind. | ||
| #include "nanobind/nanobind.h" | ||
| #include "llvm/ADT/ArrayRef.h" | ||
| #include "llvm/ADT/SmallVector.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
|
|
||
| namespace nb = nanobind; | ||
| using namespace nb::literals; | ||
|
|
@@ -1329,20 +1332,18 @@ void PyOperationBase::print(PyAsmState &state, nb::object fileObject, | |
| accum.getUserData()); | ||
| } | ||
|
|
||
| void PyOperationBase::writeBytecode(const nb::object &fileObject, | ||
| std::optional<int64_t> bytecodeVersion) { | ||
| PyOperation &operation = getOperation(); | ||
| operation.checkValid(); | ||
| PyFileAccumulator accum(fileObject, /*binary=*/true); | ||
|
|
||
| template <typename T> | ||
| static void | ||
| writeBytecodeForOperation(T &accumulator, MlirOperation operation, | ||
| const std::optional<int64_t> &bytecodeVersion) { | ||
| if (!bytecodeVersion.has_value()) | ||
| return mlirOperationWriteBytecode(operation, accum.getCallback(), | ||
| accum.getUserData()); | ||
| return mlirOperationWriteBytecode(operation, accumulator.getCallback(), | ||
| accumulator.getUserData()); | ||
|
|
||
| MlirBytecodeWriterConfig config = mlirBytecodeWriterConfigCreate(); | ||
| mlirBytecodeWriterConfigDesiredEmitVersion(config, *bytecodeVersion); | ||
| MlirLogicalResult res = mlirOperationWriteBytecodeWithConfig( | ||
| operation, config, accum.getCallback(), accum.getUserData()); | ||
| operation, config, accumulator.getCallback(), accumulator.getUserData()); | ||
| mlirBytecodeWriterConfigDestroy(config); | ||
| if (mlirLogicalResultIsFailure(res)) | ||
| throw nb::value_error((Twine("Unable to honor desired bytecode version ") + | ||
|
|
@@ -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); | ||
|
||
| } | ||
| } | ||
|
|
||
| void PyOperationBase::walk( | ||
| std::function<MlirWalkResult(MlirOperation)> callback, | ||
| MlirWalkOrder walkOrder) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.