18#include "mlir/CAPI/IR.h"
19#include "mlir/CAPI/Registration.h"
20#include "mlir/CAPI/Support.h"
21#include "mlir/CAPI/Utils.h"
22#include "mlir/IR/Builders.h"
23#include "mlir/Transforms/Passes.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Support/raw_ostream.h"
33 mlir::registerCanonicalizerPass();
55 PhysLocationAttr loc = cast<PhysLocationAttr>(
unwrap(cLoc));
60 PhysLocationAttr loc = cast<PhysLocationAttr>(
unwrap(cLoc));
61 return unwrap(self)->isValidLocation(loc);
72 if (seed.ptr ==
nullptr)
81 MlirOperation cinst, MlirAttribute cloc,
82 MlirStringRef subpath,
83 MlirLocation csrcLoc) {
84 auto inst = cast<DynamicInstanceOp>(
unwrap(cinst));
85 Location srcLoc =
unwrap(csrcLoc);
86 Attribute locAttr =
unwrap(cloc);
88 if (
auto pla = dyn_cast<PhysLocationAttr>(locAttr))
90 if (
auto locVec = dyn_cast<LocationVectorAttr>(locAttr))
91 return wrap(
unwrap(db)->place(inst, locVec, srcLoc));
92 llvm_unreachable(
"Can only place PDPhysLocationOp and PDRegPhysLocationOp");
95 MlirOperation clocOp) {
96 Operation *locOp =
unwrap(clocOp);
97 if (
auto physLocOp = dyn_cast<PDPhysLocationOp>(locOp))
98 unwrap(db)->removePlacement(physLocOp);
99 else if (
auto regPhysLocOp = dyn_cast<PDRegPhysLocationOp>(locOp))
100 unwrap(db)->removePlacement(regPhysLocOp);
102 assert(
false &&
"Can only remove PDPhysLocationOp and PDRegPhysLocationOp");
105 MlirOperation clocOp,
106 MlirAttribute cnewLoc) {
107 Operation *locOp =
unwrap(clocOp);
108 Attribute newLoc =
unwrap(cnewLoc);
109 if (
auto physLocOp = dyn_cast<PDPhysLocationOp>(locOp))
111 unwrap(db)->movePlacement(physLocOp, cast<PhysLocationAttr>(newLoc)));
112 if (
auto regPhysLocOp = dyn_cast<PDRegPhysLocationOp>(locOp))
113 return wrap(
unwrap(db)->movePlacement(regPhysLocOp,
114 cast<LocationVectorAttr>(newLoc)));
115 llvm_unreachable(
"Can only move PDPhysLocationOp and PDRegPhysLocationOp");
119 return wrap(
unwrap(db)->getInstanceAt(cast<PhysLocationAttr>(
unwrap(loc))));
123 uint64_t nearestToY) {
125 return wrap(
unwrap(db)->getNearestFreeInColumn((PrimitiveType)prim, column,
137 auto cb = [ccb, userData](PhysLocationAttr loc,
138 DynInstDataOpInterface locOp) {
139 ccb(
wrap(loc),
wrap(locOp), userData);
141 std::optional<PrimitiveType> primTypeFilter;
142 if (cPrimTypeFilter >= 0)
143 primTypeFilter =
static_cast<PrimitiveType
>(cPrimTypeFilter);
145 std::optional<PlacementDB::WalkOrder> walkOrder;
153 cb, std::make_tuple(bounds[0], bounds[1], bounds[2], bounds[3]),
154 primTypeFilter, walkOrder);
162 PrimitiveType type,
long x,
long y,
long num) {
163 Operation *op =
unwrap(cOp);
164 MLIRContext *ctxt = op->getContext();
165 PhysLocationAttr loc = PhysLocationAttr::get(
166 ctxt, PrimitiveTypeAttr::get(ctxt, type), x, y, num);
167 StringAttr entity = StringAttr::get(ctxt, entityName);
168 OpBuilder(op).create<PDPhysLocationOp>(op->getLoc(), loc, entity,
169 FlatSymbolRefAttr::get(op));
170 op->setAttr(entity, loc);
174 return llvm::isa<PhysLocationAttr>(
unwrap(attr));
178 uint64_t x, uint64_t y,
180 auto *ctxt =
unwrap(cCtxt);
181 return wrap(PhysLocationAttr::get(
182 ctxt, PrimitiveTypeAttr::get(ctxt, (PrimitiveType)devType), x, y, num));
202 return llvm::isa<PhysicalBoundsAttr>(
unwrap(attr));
206 uint64_t xMin, uint64_t xMax,
207 uint64_t yMin, uint64_t yMax) {
208 auto *context =
unwrap(cContext);
209 return wrap(PhysicalBoundsAttr::get(context, xMin, xMax, yMin, yMax));
213 return llvm::isa<LocationVectorAttr>(
unwrap(attr));
217 MlirAttribute
const *elements) {
218 SmallVector<PhysLocationAttr, 32> physLocs;
220 if (elements[i].ptr !=
nullptr)
221 physLocs.push_back(cast<PhysLocationAttr>(
unwrap(elements[i])));
223 physLocs.push_back({});
224 return wrap(LocationVectorAttr::get(
unwrap(ctxt), TypeAttr::get(
unwrap(type)),
228 return wrap(cast<LocationVectorAttr>(
unwrap(attr)).getType().getValue());
231 return cast<LocationVectorAttr>(
unwrap(attr)).getLocs().size();
235 return wrap(cast<LocationVectorAttr>(
unwrap(attr)).getLocs()[pos]);
assert(baseType &&"element must be base type")
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
MlirType uint64_t numElements
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
static void registerPasses()
MlirType circtMSFTLocationVectorAttrGetType(MlirAttribute attr)
MLIR_CAPI_EXPORTED void circtMSFTPlacementDBWalkPlacements(CirctMSFTPlacementDB cdb, CirctMSFTPlacementCallback ccb, int64_t bounds[4], CirctMSFTPrimitiveType cPrimTypeFilter, CirctMSFTWalkOrder cWalkOrder, void *userData)
Walk all the placements within 'bounds' ([xmin, xmax, ymin, ymax], inclusive on all sides),...
MlirOperation circtMSFTPlacementDBGetInstanceAt(CirctMSFTPlacementDB db, MlirAttribute loc)
uint64_t circtMSFTPhysLocationAttrGetY(MlirAttribute attr)
bool circtMSFTAttributeIsAPhysicalBoundsAttr(MlirAttribute attr)
uint64_t circtMSFTPhysLocationAttrGetNum(MlirAttribute attr)
CirctMSFTPrimitiveType circtMSFTPhysLocationAttrGetPrimitiveType(MlirAttribute attr)
void circtMSFTDeletePlacementDB(CirctMSFTPlacementDB self)
MlirLogicalResult circtMSFTPrimitiveDBAddPrimitive(CirctMSFTPrimitiveDB self, MlirAttribute cLoc)
bool circtMSFTPrimitiveDBIsValidLocation(CirctMSFTPrimitiveDB self, MlirAttribute cLoc)
void circtMSFTDeletePrimitiveDB(CirctMSFTPrimitiveDB self)
bool circtMSFTAttributeIsAPhysLocationAttribute(MlirAttribute attr)
void circtMSFTPlacementDBRemovePlacement(CirctMSFTPlacementDB db, MlirOperation clocOp)
intptr_t circtMSFTLocationVectorAttrGetNumElements(MlirAttribute attr)
CirctMSFTPlacementDB circtMSFTCreatePlacementDB(MlirModule top, CirctMSFTPrimitiveDB seed)
void mlirMSFTAddPhysLocationAttr(MlirOperation cOp, const char *entityName, PrimitiveType type, long x, long y, long num)
MlirAttribute circtMSFTLocationVectorAttrGet(MlirContext ctxt, MlirType type, intptr_t numElements, MlirAttribute const *elements)
MlirAttribute circtMSFTLocationVectorAttrGetElement(MlirAttribute attr, intptr_t pos)
MlirOperation circtMSFTPlacementDBPlace(CirctMSFTPlacementDB db, MlirOperation cinst, MlirAttribute cloc, MlirStringRef subpath, MlirLocation csrcLoc)
MlirAttribute circtMSFTPhysLocationAttrGet(MlirContext cCtxt, CirctMSFTPrimitiveType devType, uint64_t x, uint64_t y, uint64_t num)
MlirAttribute circtMSFTPhysicalBoundsAttrGet(MlirContext cContext, uint64_t xMin, uint64_t xMax, uint64_t yMin, uint64_t yMax)
void circtMSFTReplaceAllUsesWith(MlirValue value, MlirValue newValue)
void mlirMSFTRegisterPasses()
MlirAttribute circtMSFTPlacementDBGetNearestFreeInColumn(CirctMSFTPlacementDB db, CirctMSFTPrimitiveType prim, uint64_t column, uint64_t nearestToY)
CirctMSFTPrimitiveDB circtMSFTCreatePrimitiveDB(MlirContext ctxt)
uint64_t circtMSFTPhysLocationAttrGetX(MlirAttribute attr)
bool circtMSFTAttributeIsALocationVectorAttribute(MlirAttribute attr)
MlirLogicalResult circtMSFTPlacementDBMovePlacement(CirctMSFTPlacementDB db, MlirOperation clocOp, MlirAttribute cnewLoc)
int32_t CirctMSFTPrimitiveType
void(* CirctMSFTPlacementCallback)(MlirAttribute loc, MlirOperation locOp, void *userData)
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
A data structure to contain both the locations of the primitives on the device and instance assignmen...
Direction
Contains the order to iterate in each dimension for walkPlacements.
void walkPlacements(function_ref< void(PhysLocationAttr, DynInstDataOpInterface)>, std::tuple< int64_t, int64_t, int64_t, int64_t > bounds=std::make_tuple(-1, -1, -1, -1), std::optional< PrimitiveType > primType={}, std::optional< WalkOrder >={})
Walk the placement information in some sort of reasonable order.
A data structure to contain locations of the primitives on the device.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
enum CirctMSFTDirection columns
enum CirctMSFTDirection rows