10#ifndef MLIR_BINDINGS_PYTHON_PYBINDUTILS_H
11#define MLIR_BINDINGS_PYTHON_PYBINDUTILS_H
27struct std::iterator_traits<
nanobind::detail::fast_iterator> {
42 typedef std::unique_ptr<T> (*
F)();
47 if (T *
result = output.load()) {
55 std::unique_ptr<T> m = initFn();
57 nanobind::ft_lock_guard lock(mu);
58 if (T *
result = output.load()) {
68 nanobind::ft_mutex mu;
69 std::atomic<T *> output{
nullptr};
100template <
typename DerivedTy,
typename T>
123template <
typename... Ts>
124inline std::string
join(
const Ts &...args) {
125 std::ostringstream oss;
126 (oss << ... << args);
130template <
typename DefaultingTy>
132 NB_TYPE_CASTER(DefaultingTy, const_name(DefaultingTy::kTypeDescription))
138 value = DefaultingTy{DefaultingTy::resolve()};
148 value = DefaultingTy{
149 nanobind::cast<typename DefaultingTy::ReferrentTy &>(src)};
151 }
catch (std::exception &) {
156 static handle
from_cpp(DefaultingTy src, rv_policy policy,
157 cleanup_list *cleanup)
noexcept {
158 return nanobind::cast(src, policy);
181 nanobind::str pyPart(part.
data,
183 printAccum->
parts.append(std::move(pyPart));
188 nanobind::str delim(
"", 0);
189 return nanobind::cast<nanobind::str>(delim.attr(
"join")(
parts));
210 std::string filePath;
211 if (nanobind::try_cast<std::string>(fileOrStringObject, filePath)) {
212 std::string errorMessage;
213 auto errorCallback = +[](
MlirStringRef message,
void *userData) {
214 auto *storage =
static_cast<std::string *
>(userData);
215 storage->assign(message.
data, message.
length);
218 filePath.c_str(), binary, errorCallback, &errorMessage);
220 throw nanobind::value_error(
221 (std::string(
"Unable to open file for writing: ") + errorMessage)
226 writeTarget.emplace<nanobind::object>(fileOrStringObject.attr(
"write"));
231 return writeTarget.index() == 0 ? getPyWriteCallback()
232 : getOStreamCallback();
240 nanobind::gil_scoped_acquire acquire;
244 nanobind::bytes pyBytes(part.
data, part.
length);
245 std::get<nanobind::object>(accum->writeTarget)(pyBytes);
247 nanobind::str pyStr(part.
data,
249 std::get<nanobind::object>(accum->writeTarget)(pyStr);
258 std::get<RAIIMlirLlvmRawFdOStream>(accum->writeTarget), part);
262 std::variant<nanobind::object, RAIIMlirLlvmRawFdOStream> writeTarget;
276 assert(!accum->invoked &&
277 "PySinglePartStringAccumulator called back multiple times");
278 accum->invoked =
true;
279 accum->value = nanobind::str(part.
data, part.
length);
284 assert(invoked &&
"PySinglePartStringAccumulator not called back");
285 return std::move(value);
290 bool invoked =
false;
321template <
typename Derived,
typename ElementTy>
331 if (index < 0 || index >=
length)
339 assert(linearIndex >= 0 &&
340 linearIndex <
static_cast<Derived *
>(
this)->getRawNumElements() &&
341 "linear index out of bounds, the slice is ill-formed");
347 template <
typename T,
typename =
void>
350 template <
typename T>
361 PyErr_SetString(PyExc_IndexError,
"index out of range");
365 if constexpr (has_maybe_downcast<ElementTy>::value)
366 return static_cast<Derived *
>(
this)
370 return nanobind::cast(
377 Py_ssize_t start, stop, extraStep, sliceLength;
378 if (PySlice_GetIndicesEx(slice,
length, &start, &stop, &extraStep,
379 &sliceLength) != 0) {
380 PyErr_SetString(PyExc_IndexError,
"index out of range");
383 return nanobind::cast(
static_cast<Derived *
>(
this)->slice(
390 assert(
length >= 0 &&
"expected non-negative slice length");
399 throw nanobind::index_error(
"index out of range");
412 std::vector<ElementTy> elements;
413 elements.reserve(
length + other.length);
415 elements.push_back(
static_cast<Derived *
>(
this)->
getElement(i));
417 for (
intptr_t i = 0; i < other.length; ++i) {
418 elements.push_back(
static_cast<Derived *
>(&other)->
getElement(i));
434 static void bind(nanobind::module_ &m) {
438 static PyType_Slot sequenceSlots[] = {
439 {Py_sq_length, (
void *)(+[](PyObject *rawSelf) -> Py_ssize_t {
440 auto self = nanobind::cast<Derived *>(nanobind::handle(rawSelf));
446 (
void *)(+[](PyObject *rawSelf, Py_ssize_t
index) -> PyObject * {
447 auto self = nanobind::cast<Derived *>(nanobind::handle(rawSelf));
448 return self->getItem(
index).release().ptr();
452 (
void *)(+[](PyObject *rawSelf, PyObject *rawSubscript) -> PyObject * {
453 auto self = nanobind::cast<Derived *>(nanobind::handle(rawSelf));
455 PyNumber_AsSsize_t(rawSubscript, PyExc_IndexError);
456 if (!PyErr_Occurred()) {
458 return self->getItem(
index).release().ptr();
463 if (PySlice_Check(rawSubscript)) {
464 return self->getItemSlice(rawSubscript).release().ptr();
467 PyErr_SetString(PyExc_ValueError,
"expected integer or slice");
471 const std::type_info &elemTy =
typeid(ElementTy);
472 PyObject *elemTyInfo = nanobind::detail::nb_type_lookup(&elemTy);
474 "expected nb_type_lookup to succeed for Sliceable elemTy");
475 nanobind::handle elemTyName = nanobind::detail::nb_type_name(elemTyInfo);
476 std::string sig = std::string(
"class ") + Derived::pyClassName +
477 "(collections.abc.Sequence[" +
478 nanobind::cast<std::string>(elemTyName) +
"])";
479 auto clazz = nanobind::class_<Derived>(m, Derived::pyClassName,
480 nanobind::type_slots(sequenceSlots),
481 nanobind::sig(sig.c_str()))
483 Derived::bindDerived(clazz);
Accumulates into a file, either writing text (default) or binary.
PyFileAccumulator(const nanobind::object &fileOrStringObject, bool binary)
MlirStringCallback getCallback()
nanobind::typed< nanobind::object, ElementTy > getItem(intptr_t index)
Returns the element at the given slice index.
intptr_t linearizeIndex(intptr_t index)
Computes the linear index given the current slice properties.
static void bind(nanobind::module_ &m)
Binds the indexing and length methods in the Python class.
std::vector< ElementTy > dunderAdd(Derived &other)
Returns a new vector (mapped to Python list) containing elements from two slices.
ElementTy getElement(intptr_t index)
Returns the index-th element in the slice, supports negative indices.
nanobind::object getItemSlice(PyObject *slice)
Returns a new instance of the pseudo-container restricted to the given slice.
nanobind::class_< PyOpResultList > ClassTy
static void bindDerived(ClassTy &)
Hook for derived classes willing to bind more methods.
Sliceable(intptr_t startIndex, intptr_t length, intptr_t step)
intptr_t wrapIndex(intptr_t index)
Transforms index into a legal value to access the underlying sequence.
intptr_t size()
Returns the size of slice.
ReferrentTy * operator->()
Defaulting(ReferrentTy &referrent)
ReferrentTy * get() const
Defaulting()=default
Type casters require the type to be default constructible, but using such an instance is illegal.
std::unique_ptr< T >(* F)()
MLIR_CAPI_EXPORTED bool mlirLlvmRawFdOStreamIsNull(MlirLlvmRawFdOStream stream)
Checks if a raw_fd_ostream is null.
MLIR_CAPI_EXPORTED void mlirLlvmRawFdOStreamWrite(MlirLlvmRawFdOStream stream, MlirStringRef string)
Write a string to a raw_fd_ostream created with mlirLlvmRawFdOStreamCreate.
MLIR_CAPI_EXPORTED size_t mlirTypeIDHashValue(MlirTypeID typeID)
Returns the hash value of the type id.
MLIR_CAPI_EXPORTED void mlirLlvmRawFdOStreamDestroy(MlirLlvmRawFdOStream stream)
Destroy a raw_fd_ostream created with mlirLlvmRawFdOStreamCreate.
struct MlirStringRef MlirStringRef
MLIR_CAPI_EXPORTED bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2)
Checks if two type ids are equal.
void(* MlirStringCallback)(MlirStringRef, void *)
A callback for returning string references.
MLIR_CAPI_EXPORTED MlirLlvmRawFdOStream mlirLlvmRawFdOStreamCreate(const char *path, bool binary, MlirStringCallback errorCallback, void *userData)
Create a raw_fd_ostream for the given path.
Include the generated interface declarations.
std::string join(const Ts &...args)
Helper function to concatenate arguments into a std::string.
A pointer to a sized fragment of a string, not necessarily null-terminated.
const char * data
Pointer to the first symbol.
size_t length
Length of the fragment.
Accumulates into a python string from a method that accepts an MlirStringCallback.
MlirStringCallback getCallback()
Accumulates into a python string from a method that is expected to make one (no more,...
nanobind::str takeValue()
MlirStringCallback getCallback()
RAII wrapper for MlirLlvmRawFdOStream that ensures destruction on scope exit.
RAIIMlirLlvmRawFdOStream & operator=(const RAIIMlirLlvmRawFdOStream &)=delete
RAIIMlirLlvmRawFdOStream(MlirLlvmRawFdOStream stream)
~RAIIMlirLlvmRawFdOStream()
RAIIMlirLlvmRawFdOStream(const RAIIMlirLlvmRawFdOStream &)=delete
Trait to check if T provides a maybeDownCast method.
bool operator()(MlirTypeID lhs, MlirTypeID rhs) const
size_t operator()(MlirTypeID typeID) const
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup)
static handle from_cpp(DefaultingTy src, rv_policy policy, cleanup_list *cleanup) noexcept
const value_type reference
std::ptrdiff_t difference_type
std::forward_iterator_tag iterator_category
nanobind::handle value_type