Skip to content

Commit 8c605bd

Browse files
authored
[MLIR] Add logging to eraseUnreachableBlocks (NFC) (#153968)
1 parent 906c9e9 commit 8c605bd

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

mlir/lib/Transforms/Utils/RegionUtils.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
#include "llvm/ADT/DepthFirstIterator.h"
2424
#include "llvm/ADT/PostOrderIterator.h"
2525
#include "llvm/ADT/STLExtras.h"
26+
#include "llvm/Support/DebugLog.h"
2627

2728
#include <deque>
2829
#include <iterator>
2930

3031
using namespace mlir;
3132

33+
#define DEBUG_TYPE "region-utils"
34+
3235
void mlir::replaceAllUsesInRegionWith(Value orig, Value replacement,
3336
Region &region) {
3437
for (auto &use : llvm::make_early_inc_range(orig.getUses())) {
@@ -182,19 +185,34 @@ SmallVector<Value> mlir::makeRegionIsolatedFromAbove(
182185
// TODO: We could likely merge this with the DCE algorithm below.
183186
LogicalResult mlir::eraseUnreachableBlocks(RewriterBase &rewriter,
184187
MutableArrayRef<Region> regions) {
188+
LDBG() << "Starting eraseUnreachableBlocks with " << regions.size()
189+
<< " regions";
190+
185191
// Set of blocks found to be reachable within a given region.
186192
llvm::df_iterator_default_set<Block *, 16> reachable;
187193
// If any blocks were found to be dead.
188-
bool erasedDeadBlocks = false;
194+
int erasedDeadBlocks = 0;
189195

190196
SmallVector<Region *, 1> worklist;
191197
worklist.reserve(regions.size());
192198
for (Region &region : regions)
193199
worklist.push_back(&region);
200+
201+
LDBG(2) << "Initial worklist size: " << worklist.size();
202+
194203
while (!worklist.empty()) {
195204
Region *region = worklist.pop_back_val();
196-
if (region->empty())
205+
if (region->empty()) {
206+
LDBG(2) << "Skipping empty region";
197207
continue;
208+
}
209+
210+
LDBG(2) << "Processing region with " << region->getBlocks().size()
211+
<< " blocks";
212+
if (region->getParentOp())
213+
LDBG(2) << " -> for operation: "
214+
<< OpWithFlags(region->getParentOp(),
215+
OpPrintingFlags().skipRegions());
198216

199217
// If this is a single block region, just collect the nested regions.
200218
if (region->hasOneBlock()) {
@@ -209,13 +227,17 @@ LogicalResult mlir::eraseUnreachableBlocks(RewriterBase &rewriter,
209227
for (Block *block : depth_first_ext(&region->front(), reachable))
210228
(void)block /* Mark all reachable blocks */;
211229

230+
LDBG(2) << "Found " << reachable.size() << " reachable blocks out of "
231+
<< region->getBlocks().size() << " total blocks";
232+
212233
// Collect all of the dead blocks and push the live regions onto the
213234
// worklist.
214235
for (Block &block : llvm::make_early_inc_range(*region)) {
215236
if (!reachable.count(&block)) {
237+
LDBG() << "Erasing unreachable block: " << &block;
216238
block.dropAllDefinedValueUses();
217239
rewriter.eraseBlock(&block);
218-
erasedDeadBlocks = true;
240+
++erasedDeadBlocks;
219241
continue;
220242
}
221243

@@ -226,7 +248,10 @@ LogicalResult mlir::eraseUnreachableBlocks(RewriterBase &rewriter,
226248
}
227249
}
228250

229-
return success(erasedDeadBlocks);
251+
LDBG() << "Finished eraseUnreachableBlocks, erased " << erasedDeadBlocks
252+
<< " dead blocks";
253+
254+
return success(erasedDeadBlocks > 0);
230255
}
231256

232257
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)