13#include "mlir-c/BuiltinAttributes.h"
14#include "mlir/Bindings/Python/NanobindAdaptors.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/Support/raw_ostream.h"
19#include "mlir-c/Support.h"
20#include <nanobind/nanobind.h>
24using namespace mlir::python::nanobind_adaptors;
28 m.doc() =
"HW dialect Python native extension";
33 .def_classmethod(
"get",
34 [](nb::object cls, MlirType innerType) {
37 .def_property_readonly(
"element_type", [](MlirType self) {
42 .def_classmethod(
"get",
43 [](nb::object cls, MlirType
elementType, intptr_t size) {
46 .def_property_readonly(
49 .def_property_readonly(
52 nb::enum_<HWModulePortDirection>(m,
"ModulePortDirection")
53 .value(
"INPUT", HWModulePortDirection::Input)
54 .value(
"OUTPUT", HWModulePortDirection::Output)
55 .value(
"INOUT", HWModulePortDirection::InOut)
58 nb::class_<HWModulePort>(m,
"ModulePort")
59 .def(nb::init<MlirAttribute, MlirType, HWModulePortDirection>());
64 [](nb::object cls, nb::list pyModulePorts, MlirContext ctx) {
65 std::vector<HWModulePort> modulePorts;
66 for (
auto pyModulePort : pyModulePorts)
72 nb::arg(
"cls"), nb::arg(
"ports"), nb::arg(
"context") = nb::none())
73 .def_property_readonly(
78 for (intptr_t i = 0; i < numInputs; ++i)
82 .def_property_readonly(
85 std::vector<std::string> inputNames;
87 for (intptr_t i = 0; i < numInputs; ++i) {
89 inputNames.emplace_back(name.data, name.length);
93 .def_property_readonly(
98 for (intptr_t i = 0; i < numOutputs; ++i)
102 .def_property_readonly(
"output_names", [](MlirType self) {
103 std::vector<std::string> outputNames;
105 for (intptr_t i = 0; i < numOutputs; ++i) {
107 outputNames.emplace_back(name.data, name.length);
115 [](nb::object cls, MlirContext ctx, MlirAttribute param) {
118 .def_property_readonly(
"width", [](MlirType self) {
125 [](nb::object cls, nb::list pyFieldInfos) {
126 llvm::SmallVector<HWStructFieldInfo> mlirFieldInfos;
131 llvm::SmallVector<llvm::SmallString<8>> names;
132 for (
size_t i = 0, e = pyFieldInfos.size(); i < e; ++i) {
133 auto tuple = nb::cast<nb::tuple>(pyFieldInfos[i]);
134 auto type = nb::cast<MlirType>(tuple[1]);
135 ctx = mlirTypeGetContext(type);
136 names.emplace_back(nb::cast<std::string>(tuple[0]));
138 mlirStringRefCreate(names[i].
data(), names[i].size());
140 mlirIdentifierGet(ctx, nameStringRef), type});
143 mlirFieldInfos.data()));
146 [](MlirType self, std::string fieldName) {
148 self, mlirStringRefCreateFromCString(fieldName.c_str()));
150 .def(
"get_field_index",
151 [](MlirType self,
const std::string &fieldName) {
153 self, mlirStringRefCreateFromCString(fieldName.c_str()));
155 .def(
"get_fields", [](MlirType self) {
158 for (intptr_t i = 0; i < num_fields; ++i) {
160 auto fieldName = mlirIdentifierStr(field.name);
161 std::string name(fieldName.data, fieldName.length);
162 fields.append(nb::make_tuple(name, field.type));
168 .def_classmethod(
"get",
169 [](nb::object cls, std::string scope, std::string name,
170 MlirType innerType) {
172 mlirStringRefCreateFromCString(scope.c_str()),
173 mlirStringRefCreateFromCString(name.c_str()),
176 .def_property_readonly(
179 .def_property_readonly(
182 .def_property_readonly(
"name",
186 return std::string(cStr.data, cStr.length);
188 .def_property_readonly(
"scope", [](MlirType self) {
190 return std::string(cStr.data, cStr.length);
196 [](nb::object cls, std::string name, MlirType type,
197 MlirAttribute value) {
199 mlirStringRefCreateFromCString(name.c_str()), type, value));
201 .def_classmethod(
"get_nodefault",
202 [](nb::object cls, std::string name, MlirType type) {
204 mlirStringRefCreateFromCString(name.c_str()), type,
205 MlirAttribute{nullptr}));
207 .def_property_readonly(
210 .def_property_readonly(
213 .def_property_readonly(
"name", [](MlirAttribute self) {
215 return std::string(cStr.data, cStr.length);
221 [](nb::object cls, MlirContext ctx, std::string name) {
223 ctx, mlirStringRefCreateFromCString(name.c_str())));
225 .def_property_readonly(
228 .def_property_readonly(
"name", [](MlirAttribute self) {
230 return std::string(cStr.data, cStr.length);
234 .def_classmethod(
"get", [](nb::object cls, MlirAttribute text) {
239 .def_classmethod(
"get_from_filename", [](nb::object cls,
240 MlirAttribute fileName,
241 bool excludeFromFileList,
242 bool includeReplicatedOp) {
244 includeReplicatedOp));
248 .def_classmethod(
"get",
249 [](nb::object cls, MlirAttribute symName) {
252 .def_property_readonly(
"symName", [](MlirAttribute self) {
259 [](nb::object cls, MlirAttribute moduleName, MlirAttribute innerSym) {
262 .def_property_readonly(
265 .def_property_readonly(
"name", [](MlirAttribute self) {
MLIR_CAPI_EXPORTED MlirType hwStructTypeGet(MlirContext ctx, intptr_t numElements, HWStructFieldInfo const *elements)
Creates an HW struct type in the context associated with the elements.
MLIR_CAPI_EXPORTED intptr_t hwModuleTypeGetNumOutputs(MlirType type)
Get an HW module type's number of outputs.
MLIR_CAPI_EXPORTED MlirType hwParamDeclAttrGetType(MlirAttribute decl)
MLIR_CAPI_EXPORTED MlirAttribute hwOutputFileGetFromFileName(MlirAttribute text, bool excludeFromFileList, bool includeReplicatedOp)
MLIR_CAPI_EXPORTED MlirAttribute hwInnerRefAttrGet(MlirAttribute moduleName, MlirAttribute innerSym)
MLIR_CAPI_EXPORTED bool hwTypeIsAArrayType(MlirType)
If the type is an HW array.
MLIR_CAPI_EXPORTED MlirAttribute hwParamIntTypeGetWidthAttr(MlirType)
MLIR_CAPI_EXPORTED MlirAttribute hwInnerRefAttrGetModule(MlirAttribute)
MLIR_CAPI_EXPORTED MlirType hwModuleTypeGetInputType(MlirType type, intptr_t index)
Get an HW module type's input type at a specific index.
MLIR_CAPI_EXPORTED int64_t hwGetBitWidth(MlirType)
Return the hardware bit width of a type.
MLIR_CAPI_EXPORTED MlirType hwTypeAliasTypeGet(MlirStringRef scope, MlirStringRef name, MlirType innerType)
MLIR_CAPI_EXPORTED MlirType hwModuleTypeGet(MlirContext ctx, intptr_t numPorts, HWModulePort const *ports)
Creates an HW module type.
MLIR_CAPI_EXPORTED MlirAttribute hwInnerSymAttrGetSymName(MlirAttribute)
MLIR_CAPI_EXPORTED MlirAttribute hwStructTypeGetFieldIndex(MlirType structType, MlirStringRef fieldName)
MLIR_CAPI_EXPORTED HWStructFieldInfo hwStructTypeGetFieldNum(MlirType structType, unsigned idx)
MLIR_CAPI_EXPORTED MlirType hwArrayTypeGet(MlirType element, size_t size)
Creates a fixed-size HW array type in the context associated with element.
MLIR_CAPI_EXPORTED MlirType hwParamIntTypeGet(MlirAttribute parameter)
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGetValue(MlirAttribute decl)
MLIR_CAPI_EXPORTED bool hwTypeIsATypeAliasType(MlirType)
If the type is an HW type alias.
MLIR_CAPI_EXPORTED MlirType hwTypeAliasTypeGetCanonicalType(MlirType typeAlias)
MLIR_CAPI_EXPORTED MlirStringRef hwTypeAliasTypeGetName(MlirType typeAlias)
MLIR_CAPI_EXPORTED intptr_t hwModuleTypeGetNumInputs(MlirType type)
Get an HW module type's number of inputs.
MLIR_CAPI_EXPORTED bool hwTypeIsAIntType(MlirType)
If the type is an HW int.
MLIR_CAPI_EXPORTED MlirAttribute hwInnerRefAttrGetName(MlirAttribute)
MLIR_CAPI_EXPORTED bool hwAttrIsAInnerRefAttr(MlirAttribute)
MLIR_CAPI_EXPORTED MlirAttribute hwInnerSymAttrGet(MlirAttribute symName)
MLIR_CAPI_EXPORTED MlirType hwStructTypeGetField(MlirType structType, MlirStringRef fieldName)
MLIR_CAPI_EXPORTED MlirType hwTypeAliasTypeGetInnerType(MlirType typeAlias)
MLIR_CAPI_EXPORTED MlirStringRef hwModuleTypeGetInputName(MlirType type, intptr_t index)
Get an HW module type's input name at a specific index.
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclRefAttr(MlirAttribute)
MLIR_CAPI_EXPORTED bool hwAttrIsAOutputFileAttr(MlirAttribute)
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGet(MlirStringRef name, MlirType type, MlirAttribute value)
MLIR_CAPI_EXPORTED MlirStringRef hwTypeAliasTypeGetScope(MlirType typeAlias)
MLIR_CAPI_EXPORTED bool hwTypeIsAStructType(MlirType)
If the type is an HW struct.
MLIR_CAPI_EXPORTED bool hwAttrIsAInnerSymAttr(MlirAttribute)
MLIR_CAPI_EXPORTED bool hwTypeIsAInOut(MlirType type)
If the type is an HW inout.
MLIR_CAPI_EXPORTED MlirType hwInOutTypeGetElementType(MlirType)
Returns the element type of an inout type.
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclRefAttrGetName(MlirAttribute decl)
MLIR_CAPI_EXPORTED MlirStringRef hwModuleTypeGetOutputName(MlirType type, intptr_t index)
Get an HW module type's output name at a specific index.
MLIR_CAPI_EXPORTED MlirType hwInOutTypeGet(MlirType element)
Creates an HW inout type in the context associated with element.
MLIR_CAPI_EXPORTED MlirType hwArrayTypeGetElementType(MlirType)
returns the element type of an array type
MLIR_CAPI_EXPORTED bool hwAttrIsAParamVerbatimAttr(MlirAttribute)
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclAttr(MlirAttribute)
MLIR_CAPI_EXPORTED bool hwTypeIsAModuleType(MlirType type)
If the type is an HW module type.
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclRefAttrGet(MlirContext ctx, MlirStringRef cName)
MLIR_CAPI_EXPORTED intptr_t hwStructTypeGetNumFields(MlirType structType)
MLIR_CAPI_EXPORTED MlirType hwModuleTypeGetOutputType(MlirType type, intptr_t index)
Get an HW module type's output type at a specific index.
MLIR_CAPI_EXPORTED intptr_t hwArrayTypeGetSize(MlirType)
returns the size of an array type
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclAttrGetName(MlirAttribute decl)
MLIR_CAPI_EXPORTED MlirAttribute hwParamVerbatimAttrGet(MlirAttribute text)
MLIR_CAPI_EXPORTED MlirType hwParamDeclRefAttrGetType(MlirAttribute decl)
void populateDialectHWSubmodule(nanobind::module_ &m)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.