CIRCT  20.0.0git
MSFT.cpp
Go to the documentation of this file.
1 //===- MSFT.cpp - C interface for the MSFT dialect ------------------------===//
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 "circt-c/Dialect/MSFT.h"
17 #include "circt/Support/LLVM.h"
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"
26 
27 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(MSFT, msft, circt::msft::MSFTDialect)
28 
29 using namespace circt;
30 using namespace circt::msft;
31 
33  mlir::registerCanonicalizerPass();
35 }
36 
37 void circtMSFTReplaceAllUsesWith(MlirValue value, MlirValue newValue) {
38  unwrap(value).replaceAllUsesWith(unwrap(newValue));
39 }
40 
41 //===----------------------------------------------------------------------===//
42 // PrimitiveDB.
43 //===----------------------------------------------------------------------===//
44 
46 
48  return wrap(new PrimitiveDB(unwrap(ctxt)));
49 }
51  delete unwrap(self);
52 }
54  MlirAttribute cLoc) {
55  PhysLocationAttr loc = cast<PhysLocationAttr>(unwrap(cLoc));
56  return wrap(unwrap(self)->addPrimitive(loc));
57 }
59  MlirAttribute cLoc) {
60  PhysLocationAttr loc = cast<PhysLocationAttr>(unwrap(cLoc));
61  return unwrap(self)->isValidLocation(loc);
62 }
63 
64 //===----------------------------------------------------------------------===//
65 // PlacementDB.
66 //===----------------------------------------------------------------------===//
67 
69 
71  CirctMSFTPrimitiveDB seed) {
72  if (seed.ptr == nullptr)
73  return wrap(new PlacementDB(unwrap(top)));
74  return wrap(new PlacementDB(unwrap(top), *unwrap(seed)));
75 }
77  delete unwrap(self);
78 }
79 
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);
87 
88  if (auto pla = dyn_cast<PhysLocationAttr>(locAttr))
89  return wrap(unwrap(db)->place(inst, pla, unwrap(subpath), srcLoc));
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");
93 }
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);
101  else
102  assert(false && "Can only remove PDPhysLocationOp and PDRegPhysLocationOp");
103 }
105  MlirOperation clocOp,
106  MlirAttribute cnewLoc) {
107  Operation *locOp = unwrap(clocOp);
108  Attribute newLoc = unwrap(cnewLoc);
109  if (auto physLocOp = dyn_cast<PDPhysLocationOp>(locOp))
110  return wrap(
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");
116 }
118  MlirAttribute loc) {
119  return wrap(unwrap(db)->getInstanceAt(cast<PhysLocationAttr>(unwrap(loc))));
120 }
122  CirctMSFTPlacementDB db, CirctMSFTPrimitiveType prim, uint64_t column,
123  uint64_t nearestToY) {
124 
125  return wrap(unwrap(db)->getNearestFreeInColumn((PrimitiveType)prim, column,
126  nearestToY));
127 }
128 
129 /// Walk all the placements within 'bounds' ([xmin, xmax, ymin, ymax], inclusive
130 /// on all sides), with -1 meaning unbounded.
131 MLIR_CAPI_EXPORTED void circtMSFTPlacementDBWalkPlacements(
132  CirctMSFTPlacementDB cdb, CirctMSFTPlacementCallback ccb, int64_t bounds[4],
133  CirctMSFTPrimitiveType cPrimTypeFilter, CirctMSFTWalkOrder cWalkOrder,
134  void *userData) {
135 
136  PlacementDB *db = unwrap(cdb);
137  auto cb = [ccb, userData](PhysLocationAttr loc,
138  DynInstDataOpInterface locOp) {
139  ccb(wrap(loc), wrap(locOp), userData);
140  };
141  std::optional<PrimitiveType> primTypeFilter;
142  if (cPrimTypeFilter >= 0)
143  primTypeFilter = static_cast<PrimitiveType>(cPrimTypeFilter);
144 
145  std::optional<PlacementDB::WalkOrder> walkOrder;
146  if (cWalkOrder.columns != CirctMSFTDirection::NONE ||
147  cWalkOrder.rows != CirctMSFTDirection::NONE)
148  walkOrder = PlacementDB::WalkOrder{
149  static_cast<PlacementDB::Direction>(cWalkOrder.columns),
150  static_cast<PlacementDB::Direction>(cWalkOrder.rows)};
151 
152  db->walkPlacements(
153  cb, std::make_tuple(bounds[0], bounds[1], bounds[2], bounds[3]),
154  primTypeFilter, walkOrder);
155 }
156 
157 //===----------------------------------------------------------------------===//
158 // MSFT Attributes.
159 //===----------------------------------------------------------------------===//
160 
161 void mlirMSFTAddPhysLocationAttr(MlirOperation cOp, const char *entityName,
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,
170  op->setAttr(entity, loc);
171 }
172 
174  return llvm::isa<PhysLocationAttr>(unwrap(attr));
175 }
176 MlirAttribute circtMSFTPhysLocationAttrGet(MlirContext cCtxt,
177  CirctMSFTPrimitiveType devType,
178  uint64_t x, uint64_t y,
179  uint64_t num) {
180  auto *ctxt = unwrap(cCtxt);
182  ctxt, PrimitiveTypeAttr::get(ctxt, (PrimitiveType)devType), x, y, num));
183 }
184 
187  return (CirctMSFTPrimitiveType)cast<PhysLocationAttr>(unwrap(attr))
188  .getPrimitiveType()
189  .getValue();
190 }
191 uint64_t circtMSFTPhysLocationAttrGetX(MlirAttribute attr) {
192  return (CirctMSFTPrimitiveType)cast<PhysLocationAttr>(unwrap(attr)).getX();
193 }
194 uint64_t circtMSFTPhysLocationAttrGetY(MlirAttribute attr) {
195  return (CirctMSFTPrimitiveType)cast<PhysLocationAttr>(unwrap(attr)).getY();
196 }
197 uint64_t circtMSFTPhysLocationAttrGetNum(MlirAttribute attr) {
198  return (CirctMSFTPrimitiveType)cast<PhysLocationAttr>(unwrap(attr)).getNum();
199 }
200 
201 bool circtMSFTAttributeIsAPhysicalBoundsAttr(MlirAttribute attr) {
202  return llvm::isa<PhysicalBoundsAttr>(unwrap(attr));
203 }
204 
205 MlirAttribute circtMSFTPhysicalBoundsAttrGet(MlirContext cContext,
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));
210 }
211 
213  return llvm::isa<LocationVectorAttr>(unwrap(attr));
214 }
215 MlirAttribute circtMSFTLocationVectorAttrGet(MlirContext ctxt, MlirType type,
216  intptr_t numElements,
217  MlirAttribute const *elements) {
218  SmallVector<PhysLocationAttr, 32> physLocs;
219  for (intptr_t i = 0; i < numElements; ++i)
220  if (elements[i].ptr != nullptr)
221  physLocs.push_back(cast<PhysLocationAttr>(unwrap(elements[i])));
222  else
223  physLocs.push_back({});
225  physLocs));
226 }
227 MlirType circtMSFTLocationVectorAttrGetType(MlirAttribute attr) {
228  return wrap(cast<LocationVectorAttr>(unwrap(attr)).getType().getValue());
229 }
230 intptr_t circtMSFTLocationVectorAttrGetNumElements(MlirAttribute attr) {
231  return cast<LocationVectorAttr>(unwrap(attr)).getLocs().size();
232 }
233 MlirAttribute circtMSFTLocationVectorAttrGetElement(MlirAttribute attr,
234  intptr_t pos) {
235  return wrap(cast<LocationVectorAttr>(unwrap(attr)).getLocs()[pos]);
236 }
DEFINE_C_API_PTR_METHODS(CirctFirtoolFirtoolOptions, circt::firtool::FirtoolOptions) CirctFirtoolFirtoolOptions circtFirtoolOptionsCreateDefault()
Definition: Firtool.cpp:18
assert(baseType &&"element must be base type")
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
MlirType uint64_t numElements
Definition: CHIRRTL.cpp:30
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
static void registerPasses()
Definition: CIRCTModule.cpp:45
MlirType circtMSFTLocationVectorAttrGetType(MlirAttribute attr)
Definition: MSFT.cpp:227
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),...
Definition: MSFT.cpp:131
MlirOperation circtMSFTPlacementDBGetInstanceAt(CirctMSFTPlacementDB db, MlirAttribute loc)
Definition: MSFT.cpp:117
uint64_t circtMSFTPhysLocationAttrGetY(MlirAttribute attr)
Definition: MSFT.cpp:194
bool circtMSFTAttributeIsAPhysicalBoundsAttr(MlirAttribute attr)
Definition: MSFT.cpp:201
uint64_t circtMSFTPhysLocationAttrGetNum(MlirAttribute attr)
Definition: MSFT.cpp:197
CirctMSFTPrimitiveType circtMSFTPhysLocationAttrGetPrimitiveType(MlirAttribute attr)
Definition: MSFT.cpp:186
void circtMSFTDeletePlacementDB(CirctMSFTPlacementDB self)
Definition: MSFT.cpp:76
MlirLogicalResult circtMSFTPrimitiveDBAddPrimitive(CirctMSFTPrimitiveDB self, MlirAttribute cLoc)
Definition: MSFT.cpp:53
bool circtMSFTPrimitiveDBIsValidLocation(CirctMSFTPrimitiveDB self, MlirAttribute cLoc)
Definition: MSFT.cpp:58
void circtMSFTDeletePrimitiveDB(CirctMSFTPrimitiveDB self)
Definition: MSFT.cpp:50
bool circtMSFTAttributeIsAPhysLocationAttribute(MlirAttribute attr)
Definition: MSFT.cpp:173
void circtMSFTPlacementDBRemovePlacement(CirctMSFTPlacementDB db, MlirOperation clocOp)
Definition: MSFT.cpp:94
intptr_t circtMSFTLocationVectorAttrGetNumElements(MlirAttribute attr)
Definition: MSFT.cpp:230
CirctMSFTPlacementDB circtMSFTCreatePlacementDB(MlirModule top, CirctMSFTPrimitiveDB seed)
Definition: MSFT.cpp:70
void mlirMSFTAddPhysLocationAttr(MlirOperation cOp, const char *entityName, PrimitiveType type, long x, long y, long num)
Definition: MSFT.cpp:161
MlirAttribute circtMSFTLocationVectorAttrGet(MlirContext ctxt, MlirType type, intptr_t numElements, MlirAttribute const *elements)
Definition: MSFT.cpp:215
MlirAttribute circtMSFTLocationVectorAttrGetElement(MlirAttribute attr, intptr_t pos)
Definition: MSFT.cpp:233
MlirOperation circtMSFTPlacementDBPlace(CirctMSFTPlacementDB db, MlirOperation cinst, MlirAttribute cloc, MlirStringRef subpath, MlirLocation csrcLoc)
Definition: MSFT.cpp:80
MlirAttribute circtMSFTPhysLocationAttrGet(MlirContext cCtxt, CirctMSFTPrimitiveType devType, uint64_t x, uint64_t y, uint64_t num)
Definition: MSFT.cpp:176
MlirAttribute circtMSFTPhysicalBoundsAttrGet(MlirContext cContext, uint64_t xMin, uint64_t xMax, uint64_t yMin, uint64_t yMax)
Definition: MSFT.cpp:205
void circtMSFTReplaceAllUsesWith(MlirValue value, MlirValue newValue)
Definition: MSFT.cpp:37
void mlirMSFTRegisterPasses()
Definition: MSFT.cpp:32
MlirAttribute circtMSFTPlacementDBGetNearestFreeInColumn(CirctMSFTPlacementDB db, CirctMSFTPrimitiveType prim, uint64_t column, uint64_t nearestToY)
Definition: MSFT.cpp:121
CirctMSFTPrimitiveDB circtMSFTCreatePrimitiveDB(MlirContext ctxt)
Definition: MSFT.cpp:47
uint64_t circtMSFTPhysLocationAttrGetX(MlirAttribute attr)
Definition: MSFT.cpp:191
bool circtMSFTAttributeIsALocationVectorAttribute(MlirAttribute attr)
Definition: MSFT.cpp:212
MlirLogicalResult circtMSFTPlacementDBMovePlacement(CirctMSFTPlacementDB db, MlirOperation clocOp, MlirAttribute cnewLoc)
Definition: MSFT.cpp:104
int32_t CirctMSFTPrimitiveType
Definition: MSFT.h:24
void(* CirctMSFTPlacementCallback)(MlirAttribute loc, MlirOperation locOp, void *userData)
Definition: MSFT.h:109
@ NONE
Definition: MSFT.h:87
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition: OM.cpp:113
A data structure to contain both the locations of the primitives on the device and instance assignmen...
Definition: DeviceDB.h:64
Direction
Contains the order to iterate in each dimension for walkPlacements.
Definition: DeviceDB.h:73
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.
Definition: DeviceDB.h:27
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:55
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: msft.py:1
enum CirctMSFTDirection columns
Definition: MSFT.h:89
enum CirctMSFTDirection rows
Definition: MSFT.h:90