================
@@ -2001,20 +2001,43 @@ void populateFlattenCFGPatterns(RewritePatternSet 
&patterns) {
 }
 
 void CIRFlattenCFGPass::runOnOperation() {
-  RewritePatternSet patterns(&getContext());
-  populateFlattenCFGPatterns(patterns);
-
-  // Collect operations to apply patterns.
-  llvm::SmallVector<Operation *, 16> ops;
-  getOperation()->walk<mlir::WalkOrder::PostOrder>([&](Operation *op) {
-    if (isa<IfOp, ScopeOp, SwitchOp, LoopOpInterface, TernaryOp, 
CleanupScopeOp,
-            TryOp>(op))
-      ops.push_back(op);
-  });
+  RewritePatternSet patternList(&getContext());
+  populateFlattenCFGPatterns(patternList);
+  FrozenRewritePatternSet patterns(std::move(patternList));
+
+  PatternApplicator applicator(patterns);
+  // We need _A_ cost model, and everything here is the same cost-model, so 
this
+  // is effectively a no-op, but necessary to use the PatternApplicator.
+  applicator.applyDefaultCostModel();
+
+  mlir::PatternRewriter rewriter(&getContext());
+
+
+  bool changed;
+  do {
+    changed = false;
+    // Collect flatten candidates post-order so an inner op is handled before
+    // its parent; op pointers stay valid across the block splits / region
+    // inlines the patterns perform (a pattern only erases the matched op and
+    // its descendants, which are visited first), so the list can be iterated
+    // directly.
+    llvm::SmallVector<Operation *, 16> ops;
+    getOperation()->walk<mlir::WalkOrder::PostOrder>([&](Operation *op) {
+      if (isa<IfOp, ScopeOp, SwitchOp, LoopOpInterface, TernaryOp,
+              CleanupScopeOp, TryOp>(op))
+        ops.push_back(op);
+    });
 
-  // Apply patterns.
-  if (applyOpPatternsGreedily(ops, std::move(patterns)).failed())
-    signalPassFailure();
+    for (mlir::Operation *op : ops) {
+      rewriter.setInsertionPoint(op);
+      if (mlir::succeeded(applicator.matchAndRewrite(op, rewriter))) {
----------------
erichkeane wrote:

Huh, I missed the `rewriteRegionExitToContinue`.  I looked thru all the things 
that seemed to return success and noticed they all seemed to (obviously 
incorrectly!) return 'success' only after modifying.

I spent a while looking, and am kinda shocked there isn't someone else who has 
had to do this one!  I debugged a while and found that `RewriterBase::Listener` 
exists, but can't really find anyone using it for something like this, so I'll 
write a simple state-change-listener.

https://github.com/llvm/llvm-project/pull/211368
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to