14 #include "mlir-c/Bindings/Python/Interop.h"
16 #include "mlir/Bindings/Python/PybindAdaptors.h"
17 #include "mlir/CAPI/IR.h"
18 #include "mlir/CAPI/Support.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallVector.h"
24 #include <pybind11/pybind11.h>
25 #include <pybind11/stl.h>
38 MlirOperation declOp,
void *userData) {
39 std::string *name =
static_cast<std::string *
>(userData);
41 py::gil_scoped_acquire();
42 py::object rc = genFunc(reqOp);
43 return rc.cast<
bool>() ? mlirLogicalResultSuccess()
44 : mlirLogicalResultFailure();
48 std::string *n =
new std::string(name);
65 MlirLocation loc)
const {
68 if (path.ptr ==
nullptr)
70 return py::cast(path);
77 using namespace mlir::python::adaptors;
80 m.doc() =
"ESI Python Native Extension";
84 "Register a service generator for a given service name.",
85 py::arg(
"impl_type"), py::arg(
"generator"));
90 [](py::object cls, MlirType inner, uint32_t signaling = 0) {
95 py::arg(
"cls"), py::arg(
"inner"), py::arg(
"signaling") = 0)
96 .def_property_readonly(
98 .def_property_readonly(
"signaling", [](MlirType
self) {
105 [](py::object cls, MlirContext
ctxt) {
108 py::arg(
"self"), py::arg(
"ctxt") =
nullptr);
113 [](py::object cls, MlirType inner) {
116 py::arg(
"cls"), py::arg(
"inner"))
117 .def_property_readonly(
"element_type", [](MlirType
self) {
121 py::enum_<ChannelDirection>(m,
"ChannelDirection")
122 .value(
"TO", ChannelDirection::to)
123 .value(
"FROM", ChannelDirection::from);
127 [](py::object cls, std::vector<py::tuple> channelTuples,
128 bool resettable, MlirContext
ctxt) {
129 llvm::SmallVector<CirctESIBundleTypeBundleChannel, 4> channels(
130 llvm::map_range(channelTuples, [
ctxt](py::tuple t) {
131 std::string name = py::cast<std::string>(t[0]);
133 mlirIdentifierGet(
ctxt, mlirStringRefCreate(
134 name.data(), name.length())),
135 (uint32_t)py::cast<ChannelDirection>(t[1]),
136 py::cast<MlirType>(t[2])};
139 channels.data(), resettable));
141 py::arg(
"cls"), py::arg(
"channels"), py::arg(
"resettable"),
142 py::arg(
"ctxt") =
nullptr)
144 .def_property_readonly(
"channels", [](MlirType bundleType) {
145 std::vector<py::tuple> channels;
147 for (
size_t i = 0; i < numChannels; ++i) {
150 MlirStringRef name = mlirIdentifierStr(channel.
name);
151 channels.push_back(py::make_tuple(py::str(name.data, name.length),
161 [](py::object cls, std::string name, std::optional<uint64_t> index,
163 if (index.has_value())
167 "Create an AppID attribute", py::arg(
"cls"), py::arg(
"name"),
168 py::arg(
"index") = py::none(), py::arg(
"context") = py::none())
169 .def_property_readonly(
"name",
170 [](MlirAttribute
self) {
171 llvm::StringRef name =
173 return std::string(name.data(), name.size());
175 .def_property_readonly(
"index", [](MlirAttribute
self) -> py::object {
178 return py::cast(index);
182 mlir_attribute_subclass(m,
"AppIDPathAttr",
186 [](py::object cls, MlirAttribute root,
187 std::vector<MlirAttribute> path, MlirContext
ctxt) {
191 "Create an AppIDPath attribute", py::arg(
"cls"), py::arg(
"root"),
192 py::arg(
"path"), py::arg(
"context") = py::none())
197 py::class_<PyAppIDIndex>(m,
"AppIDIndex")
198 .def(py::init<MlirOperation>(), py::arg(
"root"))
200 "Return a dictionary of AppIDAttrs to ArrayAttr of InnerRefAttrs "
201 "containing the relative paths to the leaf of the particular "
202 "AppIDAttr. Argument MUST be HWModuleLike.",
205 "Return an array of InnerNameRefAttrs representing the relative "
206 "path to 'appid' from 'fromMod'.",
207 py::arg(
"from_mod"), py::arg(
"appid"),
208 py::arg(
"query_site") = py::none());
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
void registerServiceGenerator(std::string name, py::object genFunc)
static MlirLogicalResult serviceGenFunc(MlirOperation reqOp, MlirOperation declOp, void *userData)
llvm::DenseMap< std::string *, PyObject * > serviceGenFuncLookup
MLIR_CAPI_EXPORTED uint32_t circtESIChannelGetSignaling(MlirType channelType)
MLIR_CAPI_EXPORTED MlirType circtESIBundleTypeGet(MlirContext, size_t numChannels, const CirctESIBundleTypeBundleChannel *channels, bool resettable)
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDAttrGet(MlirContext, MlirStringRef name, uint64_t index)
MLIR_CAPI_EXPORTED bool circtESIBundleTypeGetResettable(MlirType bundle)
MLIR_CAPI_EXPORTED bool circtESITypeIsAnAnyType(MlirType type)
MLIR_CAPI_EXPORTED void circtESIRegisterGlobalServiceGenerator(MlirStringRef impl_type, CirctESIServiceGeneratorFunc, void *userData)
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetChildAppIDsOf(CirctESIAppIDIndex, MlirOperation)
MLIR_CAPI_EXPORTED MlirType circtESIChannelTypeGet(MlirType inner, uint32_t signaling)
MLIR_CAPI_EXPORTED MlirType circtESIChannelGetInner(MlirType channelType)
MLIR_CAPI_EXPORTED CirctESIBundleTypeBundleChannel circtESIBundleTypeGetChannel(MlirType bundle, size_t idx)
MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex)
Free an AppIDIndex.
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name)
MLIR_CAPI_EXPORTED uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetAppIDPath(CirctESIAppIDIndex, MlirOperation fromMod, MlirAttribute appid, MlirLocation loc)
MLIR_CAPI_EXPORTED MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool circtESITypeIsAChannelType(MlirType type)
MLIR_CAPI_EXPORTED size_t circtESIBundleTypeGetNumChannels(MlirType bundle)
MLIR_CAPI_EXPORTED MlirType circtESIListTypeGetElementType(MlirType channelType)
MLIR_CAPI_EXPORTED MlirType circtESIAnyTypeGet(MlirContext)
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr, uint64_t index)
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDAttrPathGet(MlirContext, MlirAttribute root, intptr_t numElements, MlirAttribute const *elements)
MLIR_CAPI_EXPORTED MlirType circtESIListTypeGet(MlirType inner)
MLIR_CAPI_EXPORTED bool circtESITypeIsABundleType(MlirType type)
MLIR_CAPI_EXPORTED bool circtESIAppIDAttrGetIndex(MlirAttribute attr, uint64_t *index)
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute)
MLIR_CAPI_EXPORTED CirctESIAppIDIndex circtESIAppIDIndexGet(MlirOperation root)
Create an index of appids through which to do appid lookups efficiently.
MLIR_CAPI_EXPORTED bool circtESIAttributeIsAnAppIDAttr(MlirAttribute)
MLIR_CAPI_EXPORTED bool circtESITypeIsAListType(MlirType type)
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
PyAppIDIndex(const PyAppIDIndex &)=delete
py::object getAppIDPathAttr(MlirOperation fromMod, MlirAttribute appid, MlirLocation loc) const
PyAppIDIndex(MlirOperation root)
MlirAttribute getChildAppIDsOf(MlirOperation op) const
void populateDialectESISubmodule(pybind11::module &m)