CIRCT 20.0.0git
Loading...
Searching...
No Matches
NanobindUtils.h
Go to the documentation of this file.
1//===- NanobindUtils.h - Utilities for interop with python ----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file copied from NPCOMP project. Omissions will be added.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_BINDINGS_PYTHON_PYBINDUTILS_H
14#define CIRCT_BINDINGS_PYTHON_PYBINDUTILS_H
15
16#include <string>
17
18#include <nanobind/nanobind.h>
19
20#include "mlir-c/Bindings/Python/Interop.h"
21#include "mlir-c/IR.h"
22#include "mlir-c/Pass.h"
23
24#include <optional>
25
26namespace circt {
27namespace python {
28
29/// Taken from NanobindUtils.h in MLIR.
30/// Accumulates into a python file-like object, either writing text (default)
31/// or binary.
33public:
34 PyFileAccumulator(nanobind::object fileObject, bool binary)
35 : pyWriteFunction(fileObject.attr("write")), binary(binary) {}
36
37 void *getUserData() { return this; }
38
39 MlirStringCallback getCallback() {
40 return [](MlirStringRef part, void *userData) {
41 nanobind::gil_scoped_acquire();
42 PyFileAccumulator *accum = static_cast<PyFileAccumulator *>(userData);
43 if (accum->binary) {
44 // Note: Still has to copy and not avoidable with this API.
45 nanobind::bytes pyBytes(part.data, part.length);
46 accum->pyWriteFunction(pyBytes);
47 } else {
48 nanobind::str pyStr(part.data,
49 part.length); // Decodes as UTF-8 by default.
50 accum->pyWriteFunction(pyStr);
51 }
52 };
53 }
54
55private:
56 nanobind::object pyWriteFunction;
57 bool binary;
58};
59} // namespace python
60} // namespace circt
61
62namespace nanobind {
63
64/// Raises a python exception with the given message.
65/// Correct usage:
66// throw RaiseValueError(PyExc_ValueError, "Foobar'd");
67inline nanobind::python_error raisePyError(PyObject *exc_class,
68 const char *message) {
69 PyErr_SetString(exc_class, message);
70 return nanobind::python_error();
71}
72
73/// Raises a value error with the given message.
74/// Correct usage:
75/// throw RaiseValueError("Foobar'd");
76inline nanobind::python_error raiseValueError(const char *message) {
77 return raisePyError(PyExc_ValueError, message);
78}
79
80/// Raises a value error with the given message.
81/// Correct usage:
82/// throw RaiseValueError(message);
83inline nanobind::python_error raiseValueError(const std::string &message) {
84 return raisePyError(PyExc_ValueError, message.c_str());
85}
86
87} // namespace nanobind
88
89#endif // CIRCT_BINDINGS_PYTHON_PYBINDUTILS_H
Taken from NanobindUtils.h in MLIR.
MlirStringCallback getCallback()
PyFileAccumulator(nanobind::object fileObject, bool binary)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
nanobind::python_error raisePyError(PyObject *exc_class, const char *message)
Raises a python exception with the given message.
nanobind::python_error raiseValueError(const char *message)
Raises a value error with the given message.