CIRCT  19.0.0git
HWModule.cpp
Go to the documentation of this file.
1 //===- HWModule.cpp - HW API pybind module --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DialectModules.h"
10 
11 #include "circt-c/Dialect/HW.h"
12 
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"
17 
18 #include "PybindUtils.h"
19 #include "mlir-c/Support.h"
20 #include <pybind11/pybind11.h>
21 #include <pybind11/pytypes.h>
22 #include <pybind11/stl.h>
23 namespace py = pybind11;
24 
25 using namespace circt;
26 using namespace mlir::python::adaptors;
27 
28 /// Populate the hw python module.
30  m.doc() = "HW dialect Python native extension";
31 
32  m.def("get_bitwidth", &hwGetBitWidth);
33 
34  mlir_type_subclass(m, "InOutType", hwTypeIsAInOut)
35  .def_classmethod("get",
36  [](py::object cls, MlirType innerType) {
37  return cls(hwInOutTypeGet(innerType));
38  })
39  .def_property_readonly("element_type", [](MlirType self) {
40  return hwInOutTypeGetElementType(self);
41  });
42 
43  mlir_type_subclass(m, "ArrayType", hwTypeIsAArrayType)
44  .def_classmethod("get",
45  [](py::object cls, MlirType elementType, intptr_t size) {
46  return cls(hwArrayTypeGet(elementType, size));
47  })
48  .def_property_readonly(
49  "element_type",
50  [](MlirType self) { return hwArrayTypeGetElementType(self); })
51  .def_property_readonly(
52  "size", [](MlirType self) { return hwArrayTypeGetSize(self); });
53 
54  py::enum_<HWModulePortDirection>(m, "ModulePortDirection")
55  .value("INPUT", HWModulePortDirection::Input)
56  .value("OUTPUT", HWModulePortDirection::Output)
57  .value("INOUT", HWModulePortDirection::InOut)
58  .export_values();
59 
60  py::class_<HWModulePort>(m, "ModulePort")
61  .def(py::init<MlirAttribute, MlirType, HWModulePortDirection>());
62 
63  mlir_type_subclass(m, "ModuleType", hwTypeIsAModuleType)
64  .def_classmethod(
65  "get",
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>());
70 
71  return cls(
72  hwModuleTypeGet(ctx, modulePorts.size(), modulePorts.data()));
73  },
74  py::arg("cls"), py::arg("ports"), py::arg("context") = py::none())
75  .def_property_readonly(
76  "input_types",
77  [](MlirType self) {
78  py::list inputTypes;
79  intptr_t numInputs = hwModuleTypeGetNumInputs(self);
80  for (intptr_t i = 0; i < numInputs; ++i)
81  inputTypes.append(hwModuleTypeGetInputType(self, i));
82  return inputTypes;
83  })
84  .def_property_readonly(
85  "input_names",
86  [](MlirType self) {
87  std::vector<std::string> inputNames;
88  intptr_t numInputs = hwModuleTypeGetNumInputs(self);
89  for (intptr_t i = 0; i < numInputs; ++i) {
90  auto name = hwModuleTypeGetInputName(self, i);
91  inputNames.emplace_back(name.data, name.length);
92  }
93  return inputNames;
94  })
95  .def_property_readonly(
96  "output_types",
97  [](MlirType self) {
98  py::list outputTypes;
99  intptr_t numOutputs = hwModuleTypeGetNumOutputs(self);
100  for (intptr_t i = 0; i < numOutputs; ++i)
101  outputTypes.append(hwModuleTypeGetOutputType(self, i));
102  return outputTypes;
103  })
104  .def_property_readonly("output_names", [](MlirType self) {
105  std::vector<std::string> outputNames;
106  intptr_t numOutputs = hwModuleTypeGetNumOutputs(self);
107  for (intptr_t i = 0; i < numOutputs; ++i) {
108  auto name = hwModuleTypeGetOutputName(self, i);
109  outputNames.emplace_back(name.data, name.length);
110  }
111  return outputNames;
112  });
113 
114  mlir_type_subclass(m, "ParamIntType", hwTypeIsAIntType)
115  .def_classmethod(
116  "get_from_param",
117  [](py::object cls, MlirContext ctx, MlirAttribute param) {
118  return cls(hwParamIntTypeGet(param));
119  })
120  .def_property_readonly("width", [](MlirType self) {
121  return hwParamIntTypeGetWidthAttr(self);
122  });
123 
124  mlir_type_subclass(m, "StructType", hwTypeIsAStructType)
125  .def_classmethod(
126  "get",
127  [](py::object cls, py::list pyFieldInfos) {
128  llvm::SmallVector<HWStructFieldInfo> mlirFieldInfos;
129  MlirContext ctx;
130 
131  // Since we're just passing string refs to the type constructor,
132  // copy them into a temporary vector to give them all new addresses.
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>());
139  auto nameStringRef =
140  mlirStringRefCreate(names[i].data(), names[i].size());
141  mlirFieldInfos.push_back(HWStructFieldInfo{
142  mlirIdentifierGet(ctx, nameStringRef), type});
143  }
144  return cls(hwStructTypeGet(ctx, mlirFieldInfos.size(),
145  mlirFieldInfos.data()));
146  })
147  .def("get_field",
148  [](MlirType self, std::string fieldName) {
149  return hwStructTypeGetField(
150  self, mlirStringRefCreateFromCString(fieldName.c_str()));
151  })
152  .def("get_field_index",
153  [](MlirType self, const std::string &fieldName) {
155  self, mlirStringRefCreateFromCString(fieldName.c_str()));
156  })
157  .def("get_fields", [](MlirType self) {
158  intptr_t num_fields = hwStructTypeGetNumFields(self);
159  py::list fields;
160  for (intptr_t i = 0; i < num_fields; ++i) {
161  auto field = hwStructTypeGetFieldNum(self, i);
162  auto fieldName = mlirIdentifierStr(field.name);
163  std::string name(fieldName.data, fieldName.length);
164  fields.append(py::make_tuple(name, field.type));
165  }
166  return fields;
167  });
168 
169  mlir_type_subclass(m, "TypeAliasType", hwTypeIsATypeAliasType)
170  .def_classmethod("get",
171  [](py::object cls, std::string scope, std::string name,
172  MlirType innerType) {
173  return cls(hwTypeAliasTypeGet(
174  mlirStringRefCreateFromCString(scope.c_str()),
175  mlirStringRefCreateFromCString(name.c_str()),
176  innerType));
177  })
178  .def_property_readonly(
179  "canonical_type",
180  [](MlirType self) { return hwTypeAliasTypeGetCanonicalType(self); })
181  .def_property_readonly(
182  "inner_type",
183  [](MlirType self) { return hwTypeAliasTypeGetInnerType(self); })
184  .def_property_readonly("name",
185  [](MlirType self) {
186  MlirStringRef cStr =
188  return std::string(cStr.data, cStr.length);
189  })
190  .def_property_readonly("scope", [](MlirType self) {
191  MlirStringRef cStr = hwTypeAliasTypeGetScope(self);
192  return std::string(cStr.data, cStr.length);
193  });
194 
195  mlir_attribute_subclass(m, "ParamDeclAttr", hwAttrIsAParamDeclAttr)
196  .def_classmethod(
197  "get",
198  [](py::object cls, std::string name, MlirType type,
199  MlirAttribute value) {
200  return cls(hwParamDeclAttrGet(
201  mlirStringRefCreateFromCString(name.c_str()), type, value));
202  })
203  .def_classmethod("get_nodefault",
204  [](py::object cls, std::string name, MlirType type) {
205  return cls(hwParamDeclAttrGet(
206  mlirStringRefCreateFromCString(name.c_str()), type,
207  MlirAttribute{nullptr}));
208  })
209  .def_property_readonly(
210  "value",
211  [](MlirAttribute self) { return hwParamDeclAttrGetValue(self); })
212  .def_property_readonly(
213  "param_type",
214  [](MlirAttribute self) { return hwParamDeclAttrGetType(self); })
215  .def_property_readonly("name", [](MlirAttribute self) {
216  MlirStringRef cStr = hwParamDeclAttrGetName(self);
217  return std::string(cStr.data, cStr.length);
218  });
219 
220  mlir_attribute_subclass(m, "ParamDeclRefAttr", hwAttrIsAParamDeclRefAttr)
221  .def_classmethod(
222  "get",
223  [](py::object cls, MlirContext ctx, std::string name) {
224  return cls(hwParamDeclRefAttrGet(
225  ctx, mlirStringRefCreateFromCString(name.c_str())));
226  })
227  .def_property_readonly(
228  "param_type",
229  [](MlirAttribute self) { return hwParamDeclRefAttrGetType(self); })
230  .def_property_readonly("name", [](MlirAttribute self) {
231  MlirStringRef cStr = hwParamDeclRefAttrGetName(self);
232  return std::string(cStr.data, cStr.length);
233  });
234 
235  mlir_attribute_subclass(m, "ParamVerbatimAttr", hwAttrIsAParamVerbatimAttr)
236  .def_classmethod("get", [](py::object cls, MlirAttribute text) {
237  return cls(hwParamVerbatimAttrGet(text));
238  });
239 
240  mlir_attribute_subclass(m, "OutputFileAttr", hwAttrIsAOutputFileAttr)
241  .def_classmethod("get_from_filename", [](py::object cls,
242  MlirAttribute fileName,
243  bool excludeFromFileList,
244  bool includeReplicatedOp) {
245  return cls(hwOutputFileGetFromFileName(fileName, excludeFromFileList,
246  includeReplicatedOp));
247  });
248 
249  mlir_attribute_subclass(m, "InnerSymAttr", hwAttrIsAInnerSymAttr)
250  .def_classmethod("get",
251  [](py::object cls, MlirAttribute symName) {
252  return cls(hwInnerSymAttrGet(symName));
253  })
254  .def_property_readonly("symName", [](MlirAttribute self) {
255  return hwInnerSymAttrGetSymName(self);
256  });
257 
258  mlir_attribute_subclass(m, "InnerRefAttr", hwAttrIsAInnerRefAttr)
259  .def_classmethod(
260  "get",
261  [](py::object cls, MlirAttribute moduleName, MlirAttribute innerSym) {
262  return cls(hwInnerRefAttrGet(moduleName, innerSym));
263  })
264  .def_property_readonly(
265  "module",
266  [](MlirAttribute self) { return hwInnerRefAttrGetModule(self); })
267  .def_property_readonly("name", [](MlirAttribute self) {
268  return hwInnerRefAttrGetName(self);
269  });
270 }
MlirType elementType
Definition: CHIRRTL.cpp:29
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.
Definition: HW.cpp:134
MLIR_CAPI_EXPORTED intptr_t hwModuleTypeGetNumOutputs(MlirType type)
Get an HW module type's number of outputs.
Definition: HW.cpp:118
MLIR_CAPI_EXPORTED MlirType hwParamDeclAttrGetType(MlirAttribute decl)
Definition: HW.cpp:258
MLIR_CAPI_EXPORTED MlirAttribute hwOutputFileGetFromFileName(MlirAttribute text, bool excludeFromFileList, bool includeReplicatedOp)
Definition: HW.cpp:297
MLIR_CAPI_EXPORTED MlirAttribute hwInnerRefAttrGet(MlirAttribute moduleName, MlirAttribute innerSym)
Definition: HW.cpp:229
MLIR_CAPI_EXPORTED bool hwTypeIsAArrayType(MlirType)
If the type is an HW array.
Definition: HW.cpp:40
MLIR_CAPI_EXPORTED MlirAttribute hwParamIntTypeGetWidthAttr(MlirType)
Definition: HW.cpp:60
MLIR_CAPI_EXPORTED MlirAttribute hwInnerRefAttrGetModule(MlirAttribute)
Definition: HW.cpp:240
MLIR_CAPI_EXPORTED MlirType hwModuleTypeGetInputType(MlirType type, intptr_t index)
Get an HW module type's input type at a specific index.
Definition: HW.cpp:110
MLIR_CAPI_EXPORTED int64_t hwGetBitWidth(MlirType)
Return the hardware bit width of a type.
Definition: HW.cpp:36
MLIR_CAPI_EXPORTED MlirType hwTypeAliasTypeGet(MlirStringRef scope, MlirStringRef name, MlirType innerType)
Definition: HW.cpp:176
MLIR_CAPI_EXPORTED MlirType hwModuleTypeGet(MlirContext ctx, intptr_t numPorts, HWModulePort const *ports)
Creates an HW module type.
Definition: HW.cpp:78
MLIR_CAPI_EXPORTED MlirAttribute hwInnerSymAttrGetSymName(MlirAttribute)
Definition: HW.cpp:220
MLIR_CAPI_EXPORTED MlirAttribute hwStructTypeGetFieldIndex(MlirType structType, MlirStringRef fieldName)
Definition: HW.cpp:150
MLIR_CAPI_EXPORTED HWStructFieldInfo hwStructTypeGetFieldNum(MlirType structType, unsigned idx)
Definition: HW.cpp:163
@ Input
Definition: HW.h:35
@ Output
Definition: HW.h:35
@ InOut
Definition: HW.h:35
MLIR_CAPI_EXPORTED MlirType hwArrayTypeGet(MlirType element, size_t size)
Creates a fixed-size HW array type in the context associated with element.
Definition: HW.cpp:42
MLIR_CAPI_EXPORTED MlirType hwParamIntTypeGet(MlirAttribute parameter)
Definition: HW.cpp:56
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGetValue(MlirAttribute decl)
Definition: HW.cpp:261
MLIR_CAPI_EXPORTED bool hwTypeIsATypeAliasType(MlirType)
If the type is an HW type alias.
Definition: HW.cpp:172
MLIR_CAPI_EXPORTED MlirType hwTypeAliasTypeGetCanonicalType(MlirType typeAlias)
Definition: HW.cpp:188
MLIR_CAPI_EXPORTED MlirStringRef hwTypeAliasTypeGetName(MlirType typeAlias)
Definition: HW.cpp:198
MLIR_CAPI_EXPORTED intptr_t hwModuleTypeGetNumInputs(MlirType type)
Get an HW module type's number of inputs.
Definition: HW.cpp:106
MLIR_CAPI_EXPORTED bool hwTypeIsAIntType(MlirType)
If the type is an HW int.
Definition: HW.cpp:54
MLIR_CAPI_EXPORTED MlirAttribute hwInnerRefAttrGetName(MlirAttribute)
Definition: HW.cpp:236
MLIR_CAPI_EXPORTED bool hwAttrIsAInnerRefAttr(MlirAttribute)
Definition: HW.cpp:225
MLIR_CAPI_EXPORTED MlirAttribute hwInnerSymAttrGet(MlirAttribute symName)
Definition: HW.cpp:216
MLIR_CAPI_EXPORTED MlirType hwStructTypeGetField(MlirType structType, MlirStringRef fieldName)
Definition: HW.cpp:145
MLIR_CAPI_EXPORTED MlirType hwTypeAliasTypeGetInnerType(MlirType typeAlias)
Definition: HW.cpp:193
MLIR_CAPI_EXPORTED MlirStringRef hwModuleTypeGetInputName(MlirType type, intptr_t index)
Get an HW module type's input name at a specific index.
Definition: HW.cpp:114
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclRefAttr(MlirAttribute)
Definition: HW.cpp:265
MLIR_CAPI_EXPORTED bool hwAttrIsAOutputFileAttr(MlirAttribute)
Definition: HW.cpp:293
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGet(MlirStringRef name, MlirType type, MlirAttribute value)
Definition: HW.cpp:247
MLIR_CAPI_EXPORTED MlirStringRef hwTypeAliasTypeGetScope(MlirType typeAlias)
Definition: HW.cpp:203
MLIR_CAPI_EXPORTED bool hwTypeIsAStructType(MlirType)
If the type is an HW struct.
Definition: HW.cpp:130
MLIR_CAPI_EXPORTED bool hwAttrIsAInnerSymAttr(MlirAttribute)
Definition: HW.cpp:212
MLIR_CAPI_EXPORTED bool hwTypeIsAInOut(MlirType type)
If the type is an HW inout.
Definition: HW.cpp:72
MLIR_CAPI_EXPORTED MlirType hwInOutTypeGetElementType(MlirType)
Returns the element type of an inout type.
Definition: HW.cpp:68
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclRefAttrGetName(MlirAttribute decl)
Definition: HW.cpp:276
MLIR_CAPI_EXPORTED MlirStringRef hwModuleTypeGetOutputName(MlirType type, intptr_t index)
Get an HW module type's output name at a specific index.
Definition: HW.cpp:126
MLIR_CAPI_EXPORTED MlirType hwInOutTypeGet(MlirType element)
Creates an HW inout type in the context associated with element.
Definition: HW.cpp:64
MLIR_CAPI_EXPORTED MlirType hwArrayTypeGetElementType(MlirType)
returns the element type of an array type
Definition: HW.cpp:46
MLIR_CAPI_EXPORTED bool hwAttrIsAParamVerbatimAttr(MlirAttribute)
Definition: HW.cpp:283
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclAttr(MlirAttribute)
Definition: HW.cpp:244
MLIR_CAPI_EXPORTED bool hwTypeIsAModuleType(MlirType type)
If the type is an HW module type.
Definition: HW.cpp:74
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclRefAttrGet(MlirContext ctx, MlirStringRef cName)
Definition: HW.cpp:269
MLIR_CAPI_EXPORTED intptr_t hwStructTypeGetNumFields(MlirType structType)
Definition: HW.cpp:158
MLIR_CAPI_EXPORTED MlirType hwModuleTypeGetOutputType(MlirType type, intptr_t index)
Get an HW module type's output type at a specific index.
Definition: HW.cpp:122
MLIR_CAPI_EXPORTED intptr_t hwArrayTypeGetSize(MlirType)
returns the size of an array type
Definition: HW.cpp:50
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclAttrGetName(MlirAttribute decl)
Definition: HW.cpp:255
MLIR_CAPI_EXPORTED MlirAttribute hwParamVerbatimAttrGet(MlirAttribute text)
Definition: HW.cpp:286
MLIR_CAPI_EXPORTED MlirType hwParamDeclRefAttrGetType(MlirAttribute decl)
Definition: HW.cpp:279
mlir::Type innerType(mlir::Type type)
Definition: ESITypes.cpp:184
void populateDialectHWSubmodule(pybind11::module &m)
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21