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>
39 MlirOperation recOp,
void *userData) {
40 std::string *name =
static_cast<std::string *
>(userData);
42 py::gil_scoped_acquire();
43 py::object rc = genFunc(reqOp, declOp, recOp);
44 return rc.cast<
bool>() ? mlirLogicalResultSuccess()
45 : mlirLogicalResultFailure();
49 std::string *n =
new std::string(name);
66 MlirLocation loc)
const {
69 if (path.ptr ==
nullptr)
71 return py::cast(path);
78 using namespace mlir::python::adaptors;
81 m.doc() =
"ESI Python Native Extension";
85 "Register a service generator for a given service name.",
86 py::arg(
"impl_type"), py::arg(
"generator"));
91 [](py::object cls, MlirType inner, uint32_t signaling = 0,
92 uint64_t dataDelay = 0) {
97 py::arg(
"cls"), py::arg(
"inner"), py::arg(
"signaling") = 0,
98 py::arg(
"dataDelay") = 0)
99 .def_property_readonly(
101 .def_property_readonly(
104 .def_property_readonly(
"data_delay", [](MlirType
self) {
111 [](py::object cls, MlirContext
ctxt) {
114 py::arg(
"self"), py::arg(
"ctxt") =
nullptr);
119 [](py::object cls, MlirType inner) {
122 py::arg(
"cls"), py::arg(
"inner"))
123 .def_property_readonly(
"element_type", [](MlirType
self) {
127 py::enum_<ChannelDirection>(m,
"ChannelDirection")
128 .value(
"TO", ChannelDirection::to)
129 .value(
"FROM", ChannelDirection::from);
133 [](py::object cls, std::vector<py::tuple> channelTuples,
134 bool resettable, MlirContext
ctxt) {
135 llvm::SmallVector<CirctESIBundleTypeBundleChannel, 4> channels(
136 llvm::map_range(channelTuples, [
ctxt](py::tuple t) {
137 std::string name = py::cast<std::string>(t[0]);
139 mlirIdentifierGet(
ctxt, mlirStringRefCreate(
140 name.data(), name.length())),
141 (uint32_t)py::cast<ChannelDirection>(t[1]),
142 py::cast<MlirType>(t[2])};
145 channels.data(), resettable));
147 py::arg(
"cls"), py::arg(
"channels"), py::arg(
"resettable"),
148 py::arg(
"ctxt") =
nullptr)
150 .def_property_readonly(
"channels", [](MlirType bundleType) {
151 std::vector<py::tuple> channels;
153 for (
size_t i = 0; i < numChannels; ++i) {
156 MlirStringRef name = mlirIdentifierStr(channel.
name);
157 channels.push_back(py::make_tuple(py::str(name.data, name.length),
167 [](py::object cls, std::string name, std::optional<uint64_t> index,
169 if (index.has_value())
173 "Create an AppID attribute", py::arg(
"cls"), py::arg(
"name"),
174 py::arg(
"index") = py::none(), py::arg(
"context") = py::none())
175 .def_property_readonly(
"name",
176 [](MlirAttribute
self) {
177 llvm::StringRef name =
179 return std::string(name.data(), name.size());
181 .def_property_readonly(
"index", [](MlirAttribute
self) -> py::object {
184 return py::cast(index);
188 mlir_attribute_subclass(m,
"AppIDPathAttr",
192 [](py::object cls, MlirAttribute root,
193 std::vector<MlirAttribute> path, MlirContext
ctxt) {
197 "Create an AppIDPath attribute", py::arg(
"cls"), py::arg(
"root"),
198 py::arg(
"path"), py::arg(
"context") = py::none())
203 py::class_<PyAppIDIndex>(m,
"AppIDIndex")
204 .def(py::init<MlirOperation>(), py::arg(
"root"))
206 "Return a dictionary of AppIDAttrs to ArrayAttr of InnerRefAttrs "
207 "containing the relative paths to the leaf of the particular "
208 "AppIDAttr. Argument MUST be HWModuleLike.",
211 "Return an array of InnerNameRefAttrs representing the relative "
212 "path to 'appid' from 'fromMod'.",
213 py::arg(
"from_mod"), py::arg(
"appid"),
214 py::arg(
"query_site") = py::none());
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
static MlirLogicalResult serviceGenFunc(MlirOperation reqOp, MlirOperation declOp, MlirOperation recOp, void *userData)
void registerServiceGenerator(std::string name, py::object genFunc)
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 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 MlirType circtESIChannelTypeGet(MlirType inner, uint32_t signaling, uint64_t dataDelay)
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 uint64_t circtESIChannelGetDataDelay(MlirType channelType)
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)