15#include "mlir-c/Bindings/Python/Interop.h"
17#include "mlir/Bindings/Python/NanobindAdaptors.h"
18#include "mlir/CAPI/IR.h"
19#include "mlir/CAPI/Support.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SmallVector.h"
25#include <nanobind/nanobind.h>
39 MlirLogicalResult
run(MlirOperation reqOp, MlirOperation declOp,
40 MlirOperation recOp) {
41 nb::gil_scoped_acquire acquire;
42 nb::object rc =
genFunc(reqOp, declOp, recOp);
43 return nb::cast<bool>(rc) ? mlirLogicalResultSuccess()
44 : mlirLogicalResultFailure();
56 MlirOperation recOp,
void *userData) {
57 std::string *name =
static_cast<std::string *
>(userData);
60 return mlirLogicalResultFailure();
61 return iter->getSecond().run(reqOp, declOp, recOp);
65 std::string *n =
new std::string(name);
81 MlirLocation loc)
const {
84 if (path.ptr ==
nullptr)
86 return nb::cast(path);
93using namespace mlir::python::nanobind_adaptors;
96 m.doc() =
"ESI Python Native Extension";
101 m.def(
"cleanup", cleanup,
102 "Cleanup various references. Must be called before the module is "
103 "unloaded in order to not leak.");
106 "Register a service generator for a given service name.",
107 nb::arg(
"impl_type"), nb::arg(
"generator"));
112 [](nb::object cls, MlirType inner, uint32_t signaling = 0,
113 uint64_t dataDelay = 0) {
118 nb::arg(
"cls"), nb::arg(
"inner"), nb::arg(
"signaling") = 0,
119 nb::arg(
"dataDelay") = 0)
120 .def_property_readonly(
122 .def_property_readonly(
125 .def_property_readonly(
"data_delay", [](MlirType self) {
132 [](nb::object cls, MlirContext ctxt) {
135 nb::arg(
"self"), nb::arg(
"ctxt") =
nullptr);
140 [](nb::object cls, MlirType inner) {
143 nb::arg(
"cls"), nb::arg(
"inner"))
144 .def_property_readonly(
"element_type", [](MlirType self) {
148 nb::enum_<ChannelDirection>(m,
"ChannelDirection")
149 .value(
"TO", ChannelDirection::to)
150 .value(
"FROM", ChannelDirection::from);
154 [](nb::object cls, std::vector<nb::tuple> channelTuples,
155 bool resettable, MlirContext ctxt) {
156 llvm::SmallVector<CirctESIBundleTypeBundleChannel, 4> channels(
157 llvm::map_range(channelTuples, [ctxt](nb::tuple t) {
158 std::string name = nb::cast<std::string>(t[0]);
160 mlirIdentifierGet(ctxt, mlirStringRefCreate(
161 name.data(), name.length())),
162 (uint32_t)nb::cast<ChannelDirection>(t[1]),
163 nb::cast<MlirType>(t[2])};
166 channels.data(), resettable));
168 nb::arg(
"cls"), nb::arg(
"channels"), nb::arg(
"resettable"),
169 nb::arg(
"ctxt") =
nullptr)
171 .def_property_readonly(
"channels", [](MlirType bundleType) {
172 std::vector<nb::tuple> channels;
174 for (
size_t i = 0; i < numChannels; ++i) {
177 MlirStringRef name = mlirIdentifierStr(channel.
name);
178 channels.push_back(nb::make_tuple(nb::str(name.data, name.length),
188 [](nb::object cls, MlirAttribute name, MlirType into,
189 std::vector<MlirType> frames, MlirContext ctxt) {
193 throw nb::type_error(
"'into' type must be a hw.StructType");
196 for (
const auto &frame : frames) {
198 throw nb::type_error(
"All frames must be WindowFrameTypes");
204 nb::arg(
"cls"), nb::arg(
"name"), nb::arg(
"into"), nb::arg(
"frames"),
205 nb::arg(
"ctxt") =
nullptr)
208 .def_property_readonly(
210 [](MlirType windowType) {
211 std::vector<MlirType> frames;
213 for (
size_t i = 0; i < numFrames; ++i)
222 [](nb::object cls, MlirAttribute name, std::vector<MlirType> members,
225 for (
const auto &member : members) {
227 throw nb::type_error(
"All members must be WindowFieldTypes");
233 nb::arg(
"cls"), nb::arg(
"name"), nb::arg(
"members"),
234 nb::arg(
"ctxt") =
nullptr)
236 .def_property_readonly(
"members", [](MlirType frameType) {
237 std::vector<MlirType> members;
239 for (
size_t i = 0; i < numMembers; ++i)
247 [](nb::object cls, MlirAttribute fieldName, uint64_t numItems,
251 nb::arg(
"cls"), nb::arg(
"field_name"), nb::arg(
"num_items") = 0,
252 nb::arg(
"ctxt") =
nullptr)
259 [](nb::object cls, std::string name, std::optional<uint64_t> index,
261 if (index.has_value())
265 "Create an AppID attribute", nb::arg(
"cls"), nb::arg(
"name"),
266 nb::arg(
"index") = nb::none(), nb::arg(
"context") = nb::none())
267 .def_property_readonly(
"name",
268 [](MlirAttribute self) {
269 llvm::StringRef name =
271 return std::string(name.data(), name.size());
273 .def_property_readonly(
"index", [](MlirAttribute self) -> nb::object {
276 return nb::cast(index);
280 mlir_attribute_subclass(m,
"AppIDPathAttr",
284 [](nb::object cls, MlirAttribute root,
285 std::vector<MlirAttribute> path, MlirContext ctxt) {
289 "Create an AppIDPath attribute", nb::arg(
"cls"), nb::arg(
"root"),
290 nb::arg(
"path"), nb::arg(
"context") = nb::none())
296 "Check that two types match, allowing for AnyType in 'expected'.",
297 nb::arg(
"expected"), nb::arg(
"actual"));
299 nb::class_<PyAppIDIndex>(m,
"AppIDIndex")
300 .def(nb::init<MlirOperation>(), nb::arg(
"root"))
302 "Return a dictionary of AppIDAttrs to ArrayAttr of InnerRefAttrs "
303 "containing the relative paths to the leaf of the particular "
304 "AppIDAttr. Argument MUST be HWModuleLike.",
307 "Return an array of InnerNameRefAttrs representing the relative "
308 "path to 'appid' from 'fromMod'.",
309 nb::arg(
"from_mod"), nb::arg(
"appid"),
310 nb::arg(
"query_site") = nb::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, nb::object genFunc)
llvm::DenseMap< std::string *, ServiceGenFunc > serviceGenFuncLookup
MLIR_CAPI_EXPORTED MlirType circtESIWindowFrameTypeGet(MlirContext cctxt, MlirAttribute name, size_t numMembers, const MlirType *members)
MLIR_CAPI_EXPORTED bool circtESITypeIsAWindowType(MlirType type)
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 size_t circtESIWindowTypeGetNumFrames(MlirType window)
MLIR_CAPI_EXPORTED MlirType circtESIWindowTypeGetFrame(MlirType window, size_t idx)
MLIR_CAPI_EXPORTED bool circtESITypeIsAWindowFrameType(MlirType type)
MLIR_CAPI_EXPORTED MlirAttribute circtESIWindowFrameTypeGetName(MlirType frame)
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 MlirAttribute circtESIWindowFieldTypeGetFieldName(MlirType field)
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 MlirType circtESIWindowTypeGetLoweredType(MlirType window)
MLIR_CAPI_EXPORTED MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirType circtESIChannelTypeGet(MlirType inner, uint32_t signaling, uint64_t dataDelay)
MLIR_CAPI_EXPORTED MlirType circtESIWindowFrameTypeGetMember(MlirType frame, size_t idx)
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 MlirType circtESIWindowTypeGet(MlirContext cctxt, MlirAttribute name, MlirType into, size_t numFrames, const MlirType *frames)
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr, uint64_t index)
MLIR_CAPI_EXPORTED MlirType circtESIWindowTypeGetInto(MlirType window)
MLIR_CAPI_EXPORTED MlirType circtESIWindowFieldTypeGet(MlirContext cctxt, MlirAttribute fieldName, uint64_t numItems)
MLIR_CAPI_EXPORTED uint64_t circtESIWindowFieldTypeGetNumItems(MlirType field)
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 bool circtESITypeIsAWindowFieldType(MlirType type)
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 size_t circtESIWindowFrameTypeGetNumMembers(MlirType frame)
MLIR_CAPI_EXPORTED bool circtESICheckInnerTypeMatch(MlirType to, MlirType from)
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute)
MLIR_CAPI_EXPORTED MlirAttribute circtESIWindowTypeGetName(MlirType window)
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)
MLIR_CAPI_EXPORTED bool hwTypeIsATypeAliasType(MlirType)
If the type is an HW type alias.
MLIR_CAPI_EXPORTED MlirType hwTypeAliasTypeGetInnerType(MlirType typeAlias)
MLIR_CAPI_EXPORTED bool hwTypeIsAStructType(MlirType)
If the type is an HW struct.
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
PyAppIDIndex(const PyAppIDIndex &)=delete
PyAppIDIndex(MlirOperation root)
nb::object getAppIDPathAttr(MlirOperation fromMod, MlirAttribute appid, MlirLocation loc) const
MlirAttribute getChildAppIDsOf(MlirOperation op) const
Container for a Python function that will be called to generate a service.
MlirLogicalResult run(MlirOperation reqOp, MlirOperation declOp, MlirOperation recOp)
ServiceGenFunc(nb::object genFunc)
void populateDialectESISubmodule(nanobind::module_ &m)