137 DeviceOp device = getOperation();
138 OpBuilder builder = OpBuilder::atBlockTerminator(device.getBody());
142 LLVM_DEBUG(TA.
print(llvm::dbgs()));
143 SmallVector<std::pair<Operation *, Operation *>, 4> tokenChains(
145 SmallVector<std::pair<Operation *, Operation *>, 4> tokenPairs(
147 DenseMap<TileID, Operation *> tiles(TA.
getTiles());
149 DenseMap<std::pair<Operation *, int>,
int> locks;
150 DenseMap<std::pair<Operation *, Operation *>, std::pair<Value, int>>
152 DenseMap<Operation *, std::vector<std::pair<Value, int>>> acqLocks;
153 DenseMap<Operation *, std::vector<std::pair<Value, int>>> relLocks;
155 for (
auto map : tokenChains) {
156 Operation *release = map.first;
157 Operation *acquire = map.second;
161 bool IsRelUserCore = isa<CoreOp>(relUser);
162 bool IsAcqUserCore = isa<CoreOp>(acqUser);
166 LLVM_DEBUG(llvm::dbgs() <<
"\n=== CHECKING TOKEN CHAIN ===\n";
167 release->print(llvm::dbgs());
168 if (IsRelUserCore) llvm::dbgs() <<
" @Core";
169 else llvm::dbgs() <<
" @DMA";
170 llvm::dbgs() <<
" (" << TA.
getCoord(relUser).col <<
", "
171 << TA.
getCoord(relUser).row <<
")" <<
'\n';
172 acquire->print(llvm::dbgs());
173 if (IsAcqUserCore) llvm::dbgs() <<
" @Core";
174 else llvm::dbgs() <<
" @DMA";
175 llvm::dbgs() <<
" (" << TA.
getCoord(acqUser).col <<
", "
176 << TA.
getCoord(acqUser).row <<
")" <<
'\n';);
185 if (!tileOp && ((!IsRelUserCore && IsAcqUserCore) ||
186 (!IsAcqUserCore && IsRelUserCore)))
189 "Sorry, the lock users of this chain do not have a common lock");
191 TileOp tile = dyn_cast<TileOp>(tileOp);
192 int lockID = getLockID(locks, tileOp);
193 assert(lockID >= 0 &&
"No more locks to allocate!");
194 LLVM_DEBUG(llvm::dbgs() <<
"Shared tile \n"; tileOp->print(llvm::dbgs()));
195 LLVM_DEBUG(llvm::dbgs() <<
" LockID: " << lockID <<
'\n');
196 builder.setInsertionPointAfter(tileOp);
198 builder.create<LockOp>(builder.getUnknownLoc(), tile, lockID, 0);
200 lockChains[std::make_pair(release, acquire)] = std::make_pair(lock, 1);
202 for (
auto pair : tokenPairs) {
203 Operation *acqFromPair = pair.first;
204 Operation *relFromPair = pair.second;
206 if (relFromPair == release)
207 acqLocks[acqFromPair].push_back(std::make_pair(lock, 0));
209 if (acqFromPair == acquire)
210 relLocks[relFromPair].push_back(std::make_pair(lock, 0));
214 ConversionTarget target(getContext());
215 target.addLegalOp<UseLockOp>();
217 RewritePatternSet patterns(&getContext());
221 if (failed(applyPartialConversion(device, target, std::move(patterns))))