|
9 | 9 | #ifndef MLIR_BINDINGS_PYTHON_PYBINDUTILS_H
|
10 | 10 | #define MLIR_BINDINGS_PYTHON_PYBINDUTILS_H
|
11 | 11 |
|
| 12 | +#include "mlir-c/Support.h" |
| 13 | +#include "llvm/ADT/Optional.h" |
| 14 | +#include "llvm/ADT/Twine.h" |
| 15 | + |
12 | 16 | #include <pybind11/pybind11.h>
|
13 | 17 | #include <pybind11/stl.h>
|
14 | 18 |
|
15 |
| -#include "llvm/ADT/Optional.h" |
16 |
| -#include "llvm/ADT/Twine.h" |
17 | 19 |
|
18 | 20 | namespace mlir {
|
19 | 21 | namespace python {
|
@@ -99,4 +101,90 @@ struct type_caster<llvm::Optional<T>> : optional_caster<llvm::Optional<T>> {};
|
99 | 101 | } // namespace detail
|
100 | 102 | } // namespace pybind11
|
101 | 103 |
|
| 104 | +//------------------------------------------------------------------------------ |
| 105 | +// Conversion utilities. |
| 106 | +//------------------------------------------------------------------------------ |
| 107 | + |
| 108 | +namespace mlir { |
| 109 | + |
| 110 | +/// Accumulates into a python string from a method that accepts an |
| 111 | +/// MlirStringCallback. |
| 112 | +struct PyPrintAccumulator { |
| 113 | + pybind11::list parts; |
| 114 | + |
| 115 | + void *getUserData() { return this; } |
| 116 | + |
| 117 | + MlirStringCallback getCallback() { |
| 118 | + return [](const char *part, intptr_t size, void *userData) { |
| 119 | + PyPrintAccumulator *printAccum = |
| 120 | + static_cast<PyPrintAccumulator *>(userData); |
| 121 | + pybind11::str pyPart(part, size); // Decodes as UTF-8 by default. |
| 122 | + printAccum->parts.append(std::move(pyPart)); |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + pybind11::str join() { |
| 127 | + pybind11::str delim("", 0); |
| 128 | + return delim.attr("join")(parts); |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +/// Accumulates int a python file-like object, either writing text (default) |
| 133 | +/// or binary. |
| 134 | +class PyFileAccumulator { |
| 135 | +public: |
| 136 | + PyFileAccumulator(pybind11::object fileObject, bool binary) |
| 137 | + : pyWriteFunction(fileObject.attr("write")), binary(binary) {} |
| 138 | + |
| 139 | + void *getUserData() { return this; } |
| 140 | + |
| 141 | + MlirStringCallback getCallback() { |
| 142 | + return [](const char *part, intptr_t size, void *userData) { |
| 143 | + pybind11::gil_scoped_acquire(); |
| 144 | + PyFileAccumulator *accum = static_cast<PyFileAccumulator *>(userData); |
| 145 | + if (accum->binary) { |
| 146 | + // Note: Still has to copy and not avoidable with this API. |
| 147 | + pybind11::bytes pyBytes(part, size); |
| 148 | + accum->pyWriteFunction(pyBytes); |
| 149 | + } else { |
| 150 | + pybind11::str pyStr(part, size); // Decodes as UTF-8 by default. |
| 151 | + accum->pyWriteFunction(pyStr); |
| 152 | + } |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | +private: |
| 157 | + pybind11::object pyWriteFunction; |
| 158 | + bool binary; |
| 159 | +}; |
| 160 | + |
| 161 | +/// Accumulates into a python string from a method that is expected to make |
| 162 | +/// one (no more, no less) call to the callback (asserts internally on |
| 163 | +/// violation). |
| 164 | +struct PySinglePartStringAccumulator { |
| 165 | + void *getUserData() { return this; } |
| 166 | + |
| 167 | + MlirStringCallback getCallback() { |
| 168 | + return [](const char *part, intptr_t size, void *userData) { |
| 169 | + PySinglePartStringAccumulator *accum = |
| 170 | + static_cast<PySinglePartStringAccumulator *>(userData); |
| 171 | + assert(!accum->invoked && |
| 172 | + "PySinglePartStringAccumulator called back multiple times"); |
| 173 | + accum->invoked = true; |
| 174 | + accum->value = pybind11::str(part, size); |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + pybind11::str takeValue() { |
| 179 | + assert(invoked && "PySinglePartStringAccumulator not called back"); |
| 180 | + return std::move(value); |
| 181 | + } |
| 182 | + |
| 183 | +private: |
| 184 | + pybind11::str value; |
| 185 | + bool invoked = false; |
| 186 | +}; |
| 187 | + |
| 188 | +} // namespace mlir |
| 189 | + |
102 | 190 | #endif // MLIR_BINDINGS_PYTHON_PYBINDUTILS_H
|
0 commit comments