39 for (
auto op : device.getOps<TokenOp>()) {
41 op->getAttrOfType<StringAttr>(::mlir::SymbolTable::getSymbolAttrName())
43 int value = op.getTokenValue();
44 tokenSymbols[tokenName] = value;
48 std::map<StringRef, SmallVector<UseTokenOp, 4>> visitors;
49 device.getBodyRegion().walk([&](Operation *Op) {
50 if (
auto op = dyn_cast<UseTokenOp>(Op)) {
51 StringRef tokenName = op.getTokenName();
52 assert(tokenSymbols.find(tokenName) != tokenSymbols.end() &&
55 tokenMapPushBack(tokenAcqMap, tokenName, op.getOperation());
56 visitors[tokenName].push_back(op);
58 tokenMapPushBack(tokenRelMap, tokenName, op.getOperation());
59 if (!visitors[tokenName].empty()) {
60 Operation *Op = visitors[tokenName].pop_back_val();
61 tokenPairs.push_back({Op, op.getOperation()});
64 }
else if (
auto op = dyn_cast<MemcpyOp>(Op)) {
65 StringRef tokenName = op.getTokenName();
66 assert(tokenSymbols.find(tokenName) != tokenSymbols.end() &&
68 Operation *Op = op.getOperation();
69 tokenMapPushBack(tokenAcqMap, tokenName, op.getOperation());
70 tokenMapPushBack(tokenRelMap, tokenName, op.getOperation());
71 tokenPairs.push_back({Op, Op});
77 for (
const auto &map : tokenAcqMap) {
78 StringRef tokenName = map.first;
79 for (
auto Op : map.second) {
80 [[maybe_unused]]
bool isReleased =
false;
81 if (
auto op = dyn_cast<MemcpyOp>(Op))
84 for (
auto pair : tokenPairs) {
85 if (UseTokenOp aop = dyn_cast<UseTokenOp>(pair.first)) {
86 if (tokenName == aop.getTokenName() && Op == aop.getOperation()) {
93 assert(isReleased &&
"No release found for acquire!"
94 "This might potentially lead to deadlock");
104 for (
const auto &map : tokenRelMap) {
105 StringRef tokenName = map.first;
106 auto tokenRels = map.second;
107 auto tokenAcqs = [&]() {
108 for (
auto &pair : tokenAcqMap)
109 if (pair.first == tokenName)
111 return std::vector<Operation *>();
113 for (
auto ROp : tokenRels) {
115 if (
auto op = dyn_cast<UseTokenOp>(ROp))
116 releaseValue = op.getTokenValue();
117 else if (
auto op = dyn_cast<MemcpyOp>(ROp))
118 releaseValue = op.getReleaseTokenValue();
120 for (
auto AOp : tokenAcqs) {
122 if (
auto op = dyn_cast<UseTokenOp>(AOp))
123 acquireValue = op.getTokenValue();
124 else if (
auto op = dyn_cast<MemcpyOp>(AOp))
125 acquireValue = op.getAcquireTokenValue();
131 if (releaseValue != acquireValue)
134 tokenChains.push_back({ROp, AOp});
139 for (
auto tile : device.getOps<TileOp>()) {
140 int colIndex = tile.colIndex();
141 int rowIndex = tile.rowIndex();
142 tiles[{colIndex, rowIndex}] = tile;
163 if (CoreOp core = dyn_cast<CoreOp>(Op)) {
164 colIndex = core.colIndex();
165 rowIndex = core.rowIndex();
166 }
else if (MemOp mem = dyn_cast<MemOp>(Op)) {
167 colIndex = mem.colIndex();
168 rowIndex = mem.rowIndex();
169 }
else if (ShimDMAOp shimDma = dyn_cast<ShimDMAOp>(Op)) {
170 colIndex = shimDma.colIndex();
171 rowIndex = shimDma.rowIndex();
174 return {colIndex, rowIndex};
179 bool IsOp1Mem = isa<MemOp>(Op1) || isa<ShimDMAOp>(Op1);
180 bool IsOp2Mem = isa<MemOp>(Op2) || isa<ShimDMAOp>(Op2);
182 assert((!IsOp1Mem || !IsOp2Mem) &&
183 "Op1 and Op2 cannot be both Mem operation!");
185 TileID coord1 = getCoord(Op1);
186 TileID coord2 = getCoord(Op2);
188 int col1 = coord1.col;
189 int row1 = coord1.row;
190 int col2 = coord2.col;
191 int row2 = coord2.row;
195 bool IsOp1ShareableMem =
196 IsOp1Mem && targetModel.isLegalMemAffinity(col2, row2, col1, row1);
197 bool IsOp2ShareableMem =
198 IsOp2Mem && targetModel.isLegalMemAffinity(col1, row1, col2, row2);
200 if (IsOp1ShareableMem)
201 return tiles[coord1];
202 if (IsOp2ShareableMem)
203 return tiles[coord2];
206 if (!IsOp1Mem && !IsOp2Mem) {
207 bool IsS = targetModel.isSouth(col1, row1, col2, row2);
208 bool IsW = targetModel.isWest(col1, row1, col2, row2);
209 bool IsN = targetModel.isNorth(col1, row1, col2, row2);
210 bool IsE = targetModel.isEast(col1, row1, col2, row2);
211 bool IsInternal = targetModel.isInternal(col1, row1, col2, row2);
212 bool IsEvenRow = ((row1 % 2) == 0);
215 if (IsS || IsN || (IsW && !IsEvenRow) || (IsE && IsEvenRow))
216 return tiles[coord2];
217 if ((IsW && IsEvenRow) || (IsE && !IsEvenRow) || IsInternal)
218 return tiles[coord1];