|
16 | 16 | #include "circt/Dialect/Sim/SimOps.h" |
17 | 17 | #include "circt/Dialect/Sim/SimTypes.h" |
18 | 18 | #include "circt/Support/Debug.h" |
| 19 | +#include "circt/Support/SparseOpSCC.h" |
19 | 20 | #include "mlir/Dialect/SCF/IR/SCF.h" |
20 | 21 | #include "mlir/Pass/Pass.h" |
21 | 22 | #include "llvm/ADT/IndexedMap.h" |
@@ -232,7 +233,6 @@ LogicalResult ProceduralizeSimPass::proceduralizePrintOps( |
232 | 233 | mapping.map(getFileOp.getResult(), clonedGetFile.getResult()); |
233 | 234 |
|
234 | 235 | cleanupList.push_back(getFileOp); |
235 | | - cleanupList.push_back(getFileOp.getFileName().getDefiningOp()); |
236 | 236 | } |
237 | 237 |
|
238 | 238 | // Materialize print inputs before creating any conditional blocks. |
@@ -269,45 +269,29 @@ LogicalResult ProceduralizeSimPass::proceduralizePrintOps( |
269 | 269 |
|
270 | 270 | PrintFormattedProcOp::create(builder, printOp.getLoc(), procPrintInput, |
271 | 271 | procPrintStream); |
272 | | - cleanupList.push_back(printOp.getInput().getDefiningOp()); |
273 | | - printOp.erase(); |
| 272 | + cleanupList.push_back(printOp); |
274 | 273 | } |
275 | 274 | return success(); |
276 | 275 | } |
277 | 276 |
|
278 | 277 | // Prune the DAGs of formatting fragments left outside of the newly created |
279 | 278 | // TriggeredOps. |
280 | 279 | void ProceduralizeSimPass::cleanup() { |
281 | | - SmallVector<Operation *> cleanupNextList; |
282 | | - SmallDenseSet<Operation *> erasedOps; |
283 | | - |
284 | | - bool noChange = true; |
285 | | - while (!cleanupList.empty() || !cleanupNextList.empty()) { |
286 | | - |
287 | | - if (cleanupList.empty()) { |
288 | | - if (noChange) |
289 | | - break; |
290 | | - cleanupList = std::move(cleanupNextList); |
291 | | - cleanupNextList = {}; |
292 | | - noChange = true; |
293 | | - } |
294 | | - |
295 | | - auto *opToErase = cleanupList.pop_back_val(); |
296 | | - if (erasedOps.contains(opToErase)) |
297 | | - continue; |
298 | | - |
299 | | - if (opToErase->getUses().empty()) { |
300 | | - // Remove a dead op. If it is a concat remove its operands, too. |
301 | | - if (auto concat = dyn_cast<FormatStringConcatOp>(opToErase)) |
302 | | - for (auto operand : concat.getInputs()) |
303 | | - cleanupNextList.push_back(operand.getDefiningOp()); |
304 | | - opToErase->erase(); |
305 | | - erasedOps.insert(opToErase); |
306 | | - noChange = false; |
307 | | - } else { |
308 | | - // Op still has uses, revisit later. |
309 | | - cleanupNextList.push_back(opToErase); |
310 | | - } |
| 280 | + SparseOpSCC<OpSCCDirection::Backward> sccs([](Operation *op, |
| 281 | + OpOperand &operand) { |
| 282 | + return isa<FormatStringType, OutputStreamType>(operand.get().getType()) && |
| 283 | + isa_and_present<SimDialect>(op->getDialect()); |
| 284 | + }); |
| 285 | + sccs.visit(cleanupList); |
| 286 | + assert(sccs.getNumCyclicSCCs() == 0 && |
| 287 | + "Cyclic graph should have been rejected"); |
| 288 | + for (auto scc : sccs.reverseTopological()) { |
| 289 | + auto *op = cast<Operation *>(scc); |
| 290 | + if (op->use_empty()) |
| 291 | + op->erase(); |
| 292 | + else |
| 293 | + op->emitWarning("Operation has remaining (transitive) users outside of " |
| 294 | + "procedural regions."); |
311 | 295 | } |
312 | 296 | } |
313 | 297 |
|
|
0 commit comments