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"
20#include "mlir-c/Support.h"
21#include "mlir/Bindings/Python/IRCore.h"
22#include "mlir/Bindings/Python/NanobindAdaptors.h"
23#include <nanobind/nanobind.h>
27using namespace mlir::python::nanobind_adaptors;
28using mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyMlirContext;
32 m.doc() =
"HW dialect Python native extension";
37 .def_classmethod(
"get",
38 [](nb::object cls, MlirType innerType) {
41 .def_property_readonly(
"element_type", [](MlirType self) {
46 .def_classmethod(
"get",
47 [](nb::object cls, MlirType
elementType, intptr_t size) {
50 .def_property_readonly(
53 .def_property_readonly(
56 nb::enum_<HWModulePortDirection>(m,
"ModulePortDirection")
57 .value(
"INPUT", HWModulePortDirection::Input)
58 .value(
"OUTPUT", HWModulePortDirection::Output)
59 .value(
"INOUT", HWModulePortDirection::InOut)
62 nb::class_<HWModulePort>(m,
"ModulePort")
63 .def(nb::init<MlirAttribute, MlirType, HWModulePortDirection>());
68 [](nb::object cls, nb::list pyModulePorts, MlirContext ctx) {
69 std::vector<HWModulePort> modulePorts;
70 for (
auto pyModulePort : pyModulePorts)
76 nb::arg(
"cls"), nb::arg(
"ports"), nb::arg(
"context") = nb::none())
77 .def_property_readonly(
82 for (intptr_t i = 0; i < numInputs; ++i)
86 .def_property_readonly(
89 std::vector<std::string> inputNames;
91 for (intptr_t i = 0; i < numInputs; ++i) {
93 inputNames.emplace_back(name.data, name.length);
97 .def_property_readonly(
100 nb::list outputTypes;
102 for (intptr_t i = 0; i < numOutputs; ++i)
106 .def_property_readonly(
"output_names", [](MlirType self) {
107 std::vector<std::string> outputNames;
109 for (intptr_t i = 0; i < numOutputs; ++i) {
111 outputNames.emplace_back(name.data, name.length);
119 [](nb::object cls, MlirContext ctx, MlirAttribute param) {
122 .def_property_readonly(
"width", [](MlirType self) {
129 [](nb::object cls, nb::list pyFieldInfos,
130 DefaultingPyMlirContext
context) {
131 llvm::SmallVector<HWStructFieldInfo> mlirFieldInfos;
136 llvm::SmallVector<llvm::SmallString<8>> names;
137 for (
size_t i = 0, e = pyFieldInfos.size(); i < e; ++i) {
138 auto tuple = nb::cast<nb::tuple>(pyFieldInfos[i]);
139 auto type = nb::cast<MlirType>(tuple[1]);
141 if (mlirContextIsNull(ctx)) {
142 ctx = mlirTypeGetContext(type);
144 names.emplace_back(nb::cast<std::string>(tuple[0]));
146 mlirStringRefCreate(names[i].
data(), names[i].size());
148 mlirIdentifierGet(ctx, nameStringRef), type});
150 if (mlirContextIsNull(ctx)) {
151 throw std::invalid_argument(
152 "StructType requires a context if no fields provided.");
155 mlirFieldInfos.data()));
157 nb::arg(
"cls"), nb::arg(
"fields"), nb::arg(
"context") = nb::none())
159 [](MlirType self, std::string fieldName) {
161 self, mlirStringRefCreateFromCString(fieldName.c_str()));
163 .def(
"get_field_index",
164 [](MlirType self,
const std::string &fieldName) {
166 self, mlirStringRefCreateFromCString(fieldName.c_str()));
168 .def(
"get_fields", [](MlirType self) {
171 for (intptr_t i = 0; i < num_fields; ++i) {
173 auto fieldName = mlirIdentifierStr(field.name);
174 std::string name(fieldName.data, fieldName.length);
175 fields.append(nb::make_tuple(name, field.type));
183 [](nb::object cls, nb::list pyFieldInfos) {
184 llvm::SmallVector<HWUnionFieldInfo> mlirFieldInfos;
189 llvm::SmallVector<llvm::SmallString<8>> names;
190 for (
size_t i = 0, e = pyFieldInfos.size(); i < e; ++i) {
191 auto tuple = nb::cast<nb::tuple>(pyFieldInfos[i]);
192 if (tuple.size() < 3)
193 throw std::invalid_argument(
194 "UnionType field info must be a tuple of (name, type, "
196 auto type = nb::cast<MlirType>(tuple[1]);
197 size_t offset = nb::cast<size_t>(tuple[2]);
198 ctx = mlirTypeGetContext(type);
199 names.emplace_back(nb::cast<std::string>(tuple[0]));
201 mlirStringRefCreate(names[i].
data(), names[i].size());
203 mlirIdentifierGet(ctx, nameStringRef), type, offset});
206 mlirFieldInfos.data()));
209 [](MlirType self, std::string fieldName) {
211 self, mlirStringRefCreateFromCString(fieldName.c_str()));
213 .def(
"get_field_index",
214 [](MlirType self,
const std::string &fieldName) {
216 self, mlirStringRefCreateFromCString(fieldName.c_str()));
218 .def(
"get_fields", [](MlirType self) {
221 for (intptr_t i = 0; i < num_fields; ++i) {
223 auto fieldName = mlirIdentifierStr(field.name);
224 std::string name(fieldName.data, fieldName.length);
225 fields.append(nb::make_tuple(name, field.type, field.offset));
231 .def_classmethod(
"get",
232 [](nb::object cls, std::string scope, std::string name,
233 MlirType innerType) {
235 mlirStringRefCreateFromCString(scope.c_str()),
236 mlirStringRefCreateFromCString(name.c_str()),
239 .def_property_readonly(
242 .def_property_readonly(
245 .def_property_readonly(
"name",
249 return std::string(cStr.data, cStr.length);
251 .def_property_readonly(
"scope", [](MlirType self) {
253 return std::string(cStr.data, cStr.length);
259 [](nb::object cls, std::string name, MlirType type,
260 MlirAttribute value) {
262 mlirStringRefCreateFromCString(name.c_str()), type, value));
264 .def_classmethod(
"get_nodefault",
265 [](nb::object cls, std::string name, MlirType type) {
267 mlirStringRefCreateFromCString(name.c_str()), type,
268 MlirAttribute{nullptr}));
270 .def_property_readonly(
273 .def_property_readonly(
276 .def_property_readonly(
"name", [](MlirAttribute self) {
278 return std::string(cStr.data, cStr.length);
284 [](nb::object cls, MlirContext ctx, std::string name) {
286 ctx, mlirStringRefCreateFromCString(name.c_str())));
288 .def_property_readonly(
291 .def_property_readonly(
"name", [](MlirAttribute self) {
293 return std::string(cStr.data, cStr.length);
297 .def_classmethod(
"get", [](nb::object cls, MlirAttribute text) {
304 [](nb::object cls, MlirAttribute fileName,
bool excludeFromFileList,
305 bool includeReplicatedOp) {
307 fileName, excludeFromFileList, includeReplicatedOp));
309 .def_property_readonly(
"filename", [](MlirAttribute self) {
311 return std::string(cStr.data, cStr.length);
315 .def_classmethod(
"get",
316 [](nb::object cls, MlirAttribute symName) {
319 .def_property_readonly(
"symName", [](MlirAttribute self) {
326 [](nb::object cls, MlirAttribute moduleName, MlirAttribute innerSym) {
329 .def_property_readonly(
332 .def_property_readonly(
"name", [](MlirAttribute self) {
static std::unique_ptr< Context > context
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 MlirStringRef hwOutputFileGetFileName(MlirAttribute outputFile)
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 HWUnionFieldInfo hwUnionTypeGetFieldNum(MlirType unionType, unsigned idx)
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 hwUnionTypeGetFieldIndex(MlirType unionType, MlirStringRef fieldName)
MLIR_CAPI_EXPORTED MlirAttribute hwInnerSymAttrGet(MlirAttribute symName)
MLIR_CAPI_EXPORTED MlirType hwStructTypeGetField(MlirType structType, MlirStringRef fieldName)
MLIR_CAPI_EXPORTED intptr_t hwUnionTypeGetNumFields(MlirType unionType)
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 MlirType hwUnionTypeGetField(MlirType unionType, MlirStringRef fieldName)
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 hwUnionTypeGet(MlirContext ctx, intptr_t numElements, HWUnionFieldInfo const *elements)
Creates an HW union type in the context associated with the elements.
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 bool hwTypeIsAUnionType(MlirType)
If the type is an HW union.
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)
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
void populateDialectHWSubmodule(nanobind::module_ &m)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.