14 #include "mlir/Bindings/Python/PybindAdaptors.h"
15 #include "mlir/CAPI/IR.h"
16 #include "mlir/CAPI/Support.h"
19 #include <pybind11/functional.h>
20 #include <pybind11/pybind11.h>
21 #include <pybind11/stl.h>
26 using namespace mlir::python::adaptors;
29 return py::module::import(
"circt.dialects.msft")
30 .attr(
"PhysLocationAttr")(attr)
39 return mlirLogicalResultIsSuccess(
56 MlirOperation
place(MlirOperation instOp, MlirAttribute loc,
57 std::string subpath, MlirLocation srcLoc) {
58 auto cSubpath = mlirStringRefCreate(subpath.c_str(), subpath.size());
65 return mlirLogicalResultIsSuccess(
72 uint64_t column, uint64_t nearestToY) {
74 db, prim, column, nearestToY);
81 std::tuple<py::object, py::object, py::object, py::object> bounds,
82 py::object prim, py::object walkOrder) {
84 auto handleNone = [](py::object o) {
85 return o.is_none() ? -1 : o.cast<int64_t>();
87 int64_t cBounds[4] = {
88 handleNone(std::get<0>(bounds)), handleNone(std::get<1>(bounds)),
89 handleNone(std::get<2>(bounds)), handleNone(std::get<3>(bounds))};
97 if (!walkOrder.is_none())
105 [](MlirAttribute loc, MlirOperation locOp,
void *userData) {
106 py::gil_scoped_acquire gil;
107 py::function pycb = *((py::function *)(userData));
110 cBounds, cPrim, cWalkOrder, &pycb);
120 static py::handle
getItem(MlirAttribute locVec, intptr_t pos) {
122 if (loc.ptr ==
nullptr)
124 return py::detail::type_caster<MlirAttribute>().cast(
125 loc, py::return_value_policy::automatic, py::handle());
133 throw py::stop_iteration();
135 return getItem(attr, nextIndex++);
138 static void bind(py::module &m) {
139 py::class_<PyLocationVecIterator>(m,
"LocationVectorAttrIterator",
147 intptr_t nextIndex = 0;
154 m.doc() =
"MSFT dialect Python native extension";
158 py::enum_<PrimitiveType>(m,
"PrimitiveType")
159 .value(
"M20K", PrimitiveType::M20K)
160 .value(
"DSP", PrimitiveType::DSP)
161 .value(
"FF", PrimitiveType::FF)
164 py::enum_<CirctMSFTDirection>(m,
"Direction")
170 mlir_attribute_subclass(m,
"PhysLocationAttr",
174 [](py::object cls, PrimitiveType devType, uint64_t x, uint64_t y,
175 uint64_t num, MlirContext
ctxt) {
179 "Create a physical location attribute", py::arg(),
180 py::arg(
"dev_type"), py::arg(
"x"), py::arg(
"y"), py::arg(
"num"),
181 py::arg(
"ctxt") = py::none())
182 .def_property_readonly(
184 [](MlirAttribute
self) {
188 .def_property_readonly(
"x",
189 [](MlirAttribute
self) {
192 .def_property_readonly(
"y",
193 [](MlirAttribute
self) {
196 .def_property_readonly(
"num", [](MlirAttribute
self) {
200 mlir_attribute_subclass(m,
"PhysicalBoundsAttr",
204 [](py::object cls, uint64_t xMin, uint64_t xMax, uint64_t yMin,
205 uint64_t yMax, MlirContext
ctxt) {
206 auto physicalBounds =
208 return cls(physicalBounds);
210 "Create a PhysicalBounds attribute", py::arg(
"cls"), py::arg(
"xMin"),
211 py::arg(
"xMax"), py::arg(
"yMin"), py::arg(
"yMax"),
212 py::arg(
"context") = py::none());
214 mlir_attribute_subclass(m,
"LocationVectorAttr",
218 [](py::object cls, MlirType type, std::vector<py::handle> pylocs,
222 SmallVector<MlirAttribute> locs;
223 for (
auto attrHandle : pylocs)
224 if (attrHandle.is_none())
225 locs.push_back({nullptr});
227 locs.push_back(mlirPythonCapsuleToAttribute(
228 mlirApiObjectToCapsule(attrHandle).ptr()));
232 "Create a LocationVector attribute", py::arg(
"cls"), py::arg(
"type"),
233 py::arg(
"locs"), py::arg(
"context") = py::none())
237 "Get the location at the specified position", py::arg(
"pos"))
242 py::class_<PrimitiveDB>(m,
"PrimitiveDB")
243 .def(py::init<MlirContext>(), py::arg(
"ctxt") = py::none())
245 "Inform the DB about a new placement.", py::arg(
"loc_and_prim"))
247 "Query the DB as to whether or not a primitive exists.",
250 py::class_<PlacementDB>(m,
"PlacementDB")
251 .def(py::init<MlirModule, PrimitiveDB *>(), py::arg(
"top"),
252 py::arg(
"seed") =
nullptr)
254 py::arg(
"dyn_inst"), py::arg(
"location"), py::arg(
"subpath"),
255 py::arg(
"src_location") = py::none())
257 "Remove a placement.", py::arg(
"location"))
259 "Move a placement to another location.", py::arg(
"old_location"),
260 py::arg(
"new_location"))
262 "Find the nearest free primitive location in column.",
263 py::arg(
"prim_type"), py::arg(
"column"), py::arg(
"nearest_to_y"))
265 "Get the instance at location. Returns None if nothing exists "
266 "there. Otherwise, returns (path, subpath, op) of the instance "
269 "Walk the placements, with possible bounds. Bounds are (xmin, xmax, "
270 "ymin, ymax) with 'None' being unbounded.",
273 std::make_tuple(py::none(), py::none(), py::none(), py::none()),
274 py::arg(
"prim_type") = py::none(),
275 py::arg(
"walk_order") = py::none());
277 py::class_<CirctMSFTWalkOrder>(m,
"WalkOrder")
278 .def(py::init<CirctMSFTDirection, CirctMSFTDirection>(),
static py::handle getPhysLocationAttr(MlirAttribute attr)
MLIR_CAPI_EXPORTED uint64_t circtMSFTPhysLocationAttrGetNum(MlirAttribute)
MLIR_CAPI_EXPORTED MlirType circtMSFTLocationVectorAttrGetType(MlirAttribute)
MLIR_CAPI_EXPORTED CirctMSFTPlacementDB circtMSFTCreatePlacementDB(MlirModule top, CirctMSFTPrimitiveDB seed)
MLIR_CAPI_EXPORTED CirctMSFTPrimitiveType circtMSFTPhysLocationAttrGetPrimitiveType(MlirAttribute)
MLIR_CAPI_EXPORTED void circtMSFTDeletePlacementDB(CirctMSFTPlacementDB self)
MLIR_CAPI_EXPORTED bool circtMSFTAttributeIsAPhysicalBoundsAttr(MlirAttribute)
int32_t CirctMSFTPrimitiveType
MLIR_CAPI_EXPORTED bool circtMSFTAttributeIsALocationVectorAttribute(MlirAttribute)
MLIR_CAPI_EXPORTED MlirAttribute circtMSFTPlacementDBGetNearestFreeInColumn(CirctMSFTPlacementDB, CirctMSFTPrimitiveType prim, uint64_t column, uint64_t nearestToY)
MLIR_CAPI_EXPORTED MlirAttribute circtMSFTLocationVectorAttrGet(MlirContext, MlirType type, intptr_t numElements, MlirAttribute const *elements)
MLIR_CAPI_EXPORTED intptr_t circtMSFTLocationVectorAttrGetNumElements(MlirAttribute)
MLIR_CAPI_EXPORTED MlirLogicalResult circtMSFTPlacementDBMovePlacement(CirctMSFTPlacementDB, MlirOperation locOp, MlirAttribute newLoc)
MLIR_CAPI_EXPORTED MlirAttribute circtMSFTLocationVectorAttrGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED void circtMSFTPlacementDBRemovePlacement(CirctMSFTPlacementDB, MlirOperation locOp)
MLIR_CAPI_EXPORTED bool circtMSFTPrimitiveDBIsValidLocation(CirctMSFTPrimitiveDB, MlirAttribute locAndPrim)
MLIR_CAPI_EXPORTED MlirOperation circtMSFTPlacementDBGetInstanceAt(CirctMSFTPlacementDB, MlirAttribute loc)
MLIR_CAPI_EXPORTED CirctMSFTPrimitiveDB circtMSFTCreatePrimitiveDB(MlirContext)
MLIR_CAPI_EXPORTED void circtMSFTDeletePrimitiveDB(CirctMSFTPrimitiveDB self)
MLIR_CAPI_EXPORTED uint64_t circtMSFTPhysLocationAttrGetX(MlirAttribute)
MLIR_CAPI_EXPORTED void circtMSFTPlacementDBWalkPlacements(CirctMSFTPlacementDB, CirctMSFTPlacementCallback, int64_t bounds[4], CirctMSFTPrimitiveType primTypeFilter, CirctMSFTWalkOrder walkOrder, void *userData)
Walk all the placements within 'bounds' ([xmin, xmax, ymin, ymax], inclusive on all sides),...
MLIR_CAPI_EXPORTED MlirAttribute circtMSFTPhysicalBoundsAttrGet(MlirContext, uint64_t, uint64_t, uint64_t, uint64_t)
MLIR_CAPI_EXPORTED bool circtMSFTAttributeIsAPhysLocationAttribute(MlirAttribute)
MLIR_CAPI_EXPORTED MlirAttribute circtMSFTPhysLocationAttrGet(MlirContext, CirctMSFTPrimitiveType, uint64_t x, uint64_t y, uint64_t num)
MLIR_CAPI_EXPORTED MlirOperation circtMSFTPlacementDBPlace(CirctMSFTPlacementDB, MlirOperation inst, MlirAttribute loc, MlirStringRef subpath, MlirLocation srcLoc)
MLIR_CAPI_EXPORTED MlirLogicalResult circtMSFTPrimitiveDBAddPrimitive(CirctMSFTPrimitiveDB, MlirAttribute locAndPrim)
MLIR_CAPI_EXPORTED void mlirMSFTRegisterPasses(void)
MLIR_CAPI_EXPORTED uint64_t circtMSFTPhysLocationAttrGetY(MlirAttribute)
MLIR_CAPI_EXPORTED void circtMSFTReplaceAllUsesWith(MlirValue value, MlirValue newValue)
void walkPlacements(py::function pycb, std::tuple< py::object, py::object, py::object, py::object > bounds, py::object prim, py::object walkOrder)
MlirOperation getInstanceAt(MlirAttribute loc)
MlirOperation place(MlirOperation instOp, MlirAttribute loc, std::string subpath, MlirLocation srcLoc)
bool movePlacement(MlirOperation locOp, MlirAttribute newLoc)
PlacementDB(MlirModule top, PrimitiveDB *seed)
py::handle getNearestFreeInColumn(CirctMSFTPrimitiveType prim, uint64_t column, uint64_t nearestToY)
void removePlacement(MlirOperation locOp)
bool isValidLocation(MlirAttribute loc)
PrimitiveDB(MlirContext ctxt)
bool addPrimitive(MlirAttribute locAndPrim)
PyLocationVecIterator(MlirAttribute attr)
static void bind(py::module &m)
PyLocationVecIterator & dunderIter()
static py::handle getItem(MlirAttribute locVec, intptr_t pos)
Get item at the specified position, translating a nullptr to None.
void populateDialectMSFTSubmodule(pybind11::module &m)