Skip to content

Commit f2b6302

Browse files
authored
[mlir][transforms] Process RegionBranchOp with empty region
This PR adds process for RegionBranchOp with empty region, such as `scf.if`.
1 parent 6dc356d commit f2b6302

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

mlir/lib/Transforms/RemoveDeadValues.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ static void processRegionBranchOp(RegionBranchOpInterface regionBranchOp,
375375
// Mark live arguments in the regions of `regionBranchOp` in `liveArgs`.
376376
auto markLiveArgs = [&](DenseMap<Region *, BitVector> &liveArgs) {
377377
for (Region &region : regionBranchOp->getRegions()) {
378+
if (region.empty())
379+
continue;
378380
SmallVector<Value> arguments(region.front().getArguments());
379381
BitVector regionLiveArgs = markLives(arguments, nonLiveSet, la);
380382
liveArgs[&region] = regionLiveArgs;
@@ -420,6 +422,8 @@ static void processRegionBranchOp(RegionBranchOpInterface regionBranchOp,
420422
auto markNonForwardedReturnValues =
421423
[&](DenseMap<Operation *, BitVector> &nonForwardedRets) {
422424
for (Region &region : regionBranchOp->getRegions()) {
425+
if (region.empty())
426+
continue;
423427
Operation *terminator = region.front().getTerminator();
424428
nonForwardedRets[terminator] =
425429
BitVector(terminator->getNumOperands(), true);
@@ -499,6 +503,8 @@ static void processRegionBranchOp(RegionBranchOpInterface regionBranchOp,
499503
// Recompute `resultsToKeep` and `argsToKeep` based on
500504
// `terminatorOperandsToKeep`.
501505
for (Region &region : regionBranchOp->getRegions()) {
506+
if (region.empty())
507+
continue;
502508
Operation *terminator = region.front().getTerminator();
503509
for (const RegionSuccessor &successor : getSuccessors(&region)) {
504510
Region *successorRegion = successor.getSuccessor();
@@ -547,6 +553,8 @@ static void processRegionBranchOp(RegionBranchOpInterface regionBranchOp,
547553

548554
// Update the terminator operands that need to be kept.
549555
for (Region &region : regionBranchOp->getRegions()) {
556+
if (region.empty())
557+
continue;
550558
updateOperandsOrTerminatorOperandsToKeep(
551559
terminatorOperandsToKeep[region.back().getTerminator()],
552560
resultsToKeep, argsToKeep, &region);
@@ -611,8 +619,8 @@ static void processRegionBranchOp(RegionBranchOpInterface regionBranchOp,
611619

612620
// Do (2.a) and (2.b).
613621
for (Region &region : regionBranchOp->getRegions()) {
614-
assert(!region.empty() && "expected a non-empty region in an op "
615-
"implementing `RegionBranchOpInterface`");
622+
if (region.empty())
623+
continue;
616624
BitVector argsToRemove = argsToKeep[&region].flip();
617625
cl.blocks.push_back({&region.front(), argsToRemove});
618626
collectNonLiveValues(nonLiveSet, region.front().getArguments(),
@@ -621,6 +629,8 @@ static void processRegionBranchOp(RegionBranchOpInterface regionBranchOp,
621629

622630
// Do (2.c).
623631
for (Region &region : regionBranchOp->getRegions()) {
632+
if (region.empty())
633+
continue;
624634
Operation *terminator = region.front().getTerminator();
625635
cl.operands.push_back(
626636
{terminator, terminatorOperandsToKeep[terminator].flip()});

0 commit comments

Comments
 (0)