13 #include "mlir-c/BuiltinAttributes.h"
14 #include "mlir/Bindings/Python/PybindAdaptors.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/raw_ostream.h"
19 #include "mlir-c/Support.h"
20 #include <pybind11/pybind11.h>
21 #include <pybind11/pytypes.h>
22 #include <pybind11/stl.h>
25 using namespace circt;
26 using namespace mlir::python::adaptors;
30 m.doc() =
"HW dialect Python native extension";
35 .def_classmethod(
"get",
39 .def_property_readonly(
"element_type", [](MlirType
self) {
44 .def_classmethod(
"get",
45 [](py::object cls, MlirType
elementType, intptr_t size) {
48 .def_property_readonly(
51 .def_property_readonly(
54 py::enum_<HWModulePortDirection>(m,
"ModulePortDirection")
60 py::class_<HWModulePort>(m,
"ModulePort")
61 .def(py::init<MlirAttribute, MlirType, HWModulePortDirection>());
66 [](py::object cls, py::list pyModulePorts, MlirContext ctx) {
67 std::vector<HWModulePort> modulePorts;
68 for (
auto pyModulePort : pyModulePorts)
69 modulePorts.push_back(pyModulePort.cast<
HWModulePort>());
74 py::arg(
"cls"), py::arg(
"ports"), py::arg(
"context") = py::none())
75 .def_property_readonly(
80 for (intptr_t i = 0; i < numInputs; ++i)
84 .def_property_readonly(
87 std::vector<std::string> inputNames;
89 for (intptr_t i = 0; i < numInputs; ++i) {
91 inputNames.emplace_back(name.data, name.length);
95 .def_property_readonly(
100 for (intptr_t i = 0; i < numOutputs; ++i)
104 .def_property_readonly(
"output_names", [](MlirType
self) {
105 std::vector<std::string> outputNames;
107 for (intptr_t i = 0; i < numOutputs; ++i) {
109 outputNames.emplace_back(name.data, name.length);
117 [](py::object cls, MlirContext ctx, MlirAttribute param) {
120 .def_property_readonly(
"width", [](MlirType
self) {
127 [](py::object cls, py::list pyFieldInfos) {
128 llvm::SmallVector<HWStructFieldInfo> mlirFieldInfos;
133 llvm::SmallVector<llvm::SmallString<8>> names;
134 for (
size_t i = 0, e = pyFieldInfos.size(); i < e; ++i) {
135 auto tuple = pyFieldInfos[i].cast<py::tuple>();
136 auto type = tuple[1].cast<MlirType>();
137 ctx = mlirTypeGetContext(type);
138 names.emplace_back(tuple[0].cast<std::string>());
140 mlirStringRefCreate(names[i].data(), names[i].size());
141 mlirFieldInfos.push_back(HWStructFieldInfo{
142 mlirIdentifierGet(ctx, nameStringRef), type});
145 mlirFieldInfos.data()));
148 [](MlirType
self, std::string fieldName) {
150 self, mlirStringRefCreateFromCString(fieldName.c_str()));
152 .def(
"get_field_index",
153 [](MlirType
self,
const std::string &fieldName) {
155 self, mlirStringRefCreateFromCString(fieldName.c_str()));
157 .def(
"get_fields", [](MlirType
self) {
160 for (intptr_t i = 0; i < num_fields; ++i) {
162 auto fieldName = mlirIdentifierStr(field.name);
163 std::string name(fieldName.data, fieldName.length);
164 fields.append(py::make_tuple(name, field.type));
170 .def_classmethod(
"get",
171 [](py::object cls, std::string scope, std::string name,
174 mlirStringRefCreateFromCString(scope.c_str()),
175 mlirStringRefCreateFromCString(name.c_str()),
178 .def_property_readonly(
181 .def_property_readonly(
184 .def_property_readonly(
"name",
188 return std::string(cStr.data, cStr.length);
190 .def_property_readonly(
"scope", [](MlirType
self) {
192 return std::string(cStr.data, cStr.length);
198 [](py::object cls, std::string name, MlirType type,
199 MlirAttribute value) {
201 mlirStringRefCreateFromCString(name.c_str()), type, value));
203 .def_classmethod(
"get_nodefault",
204 [](py::object cls, std::string name, MlirType type) {
206 mlirStringRefCreateFromCString(name.c_str()), type,
207 MlirAttribute{nullptr}));
209 .def_property_readonly(
212 .def_property_readonly(
215 .def_property_readonly(
"name", [](MlirAttribute
self) {
217 return std::string(cStr.data, cStr.length);
223 [](py::object cls, MlirContext ctx, std::string name) {
225 ctx, mlirStringRefCreateFromCString(name.c_str())));
227 .def_property_readonly(
230 .def_property_readonly(
"name", [](MlirAttribute
self) {
232 return std::string(cStr.data, cStr.length);
236 .def_classmethod(
"get", [](py::object cls, MlirAttribute text) {
241 .def_classmethod(
"get_from_filename", [](py::object cls,
242 MlirAttribute fileName,
243 bool excludeFromFileList,
244 bool includeReplicatedOp) {
246 includeReplicatedOp));
250 .def_classmethod(
"get",
251 [](py::object cls, MlirAttribute symName) {
254 .def_property_readonly(
"symName", [](MlirAttribute
self) {
261 [](py::object cls, MlirAttribute moduleName, MlirAttribute innerSym) {
264 .def_property_readonly(
267 .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)
mlir::Type innerType(mlir::Type type)
void populateDialectHWSubmodule(pybind11::module &m)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.