Skip to content

Commit 55321b8

Browse files
fix liveness-analysis in the loop ivs case.
1 parent b4b7aae commit 55321b8

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

mlir/lib/Analysis/DataFlow/LivenessAnalysis.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <mlir/IR/Operation.h>
1818
#include <mlir/IR/Value.h>
1919
#include <mlir/Interfaces/CallInterfaces.h>
20+
#include <mlir/Interfaces/LoopLikeInterface.h>
2021
#include <mlir/Interfaces/SideEffectInterfaces.h>
2122
#include <mlir/Support/LLVM.h>
2223

@@ -309,15 +310,29 @@ RunLivenessAnalysis::RunLivenessAnalysis(Operation *op) {
309310
<< " has no liveness info (unreachable), mark dead";
310311
solver.getOrCreateState<Liveness>(result.value());
311312
}
313+
SmallVector<Value> mustLiveValues;
314+
if (auto loopOp = dyn_cast<LoopLikeOpInterface>(op)) {
315+
std::optional<SmallVector<Value>> ivs = loopOp.getLoopInductionVars();
316+
if (ivs.has_value())
317+
mustLiveValues.append(*ivs);
318+
}
312319
for (auto &region : op->getRegions()) {
313320
for (auto &block : region) {
314321
for (auto blockArg : llvm::enumerate(block.getArguments())) {
315322
if (getLiveness(blockArg.value()))
316323
continue;
317-
LDBG() << "Block argument: " << blockArg.index() << " of "
318-
<< OpWithFlags(op, OpPrintingFlags().skipRegions())
319-
<< " has no liveness info, mark dead";
320-
solver.getOrCreateState<Liveness>(blockArg.value());
324+
if (llvm::find(mustLiveValues, blockArg.value())) {
325+
LDBG() << "Block argument: " << blockArg.index() << " of "
326+
<< OpWithFlags(op, OpPrintingFlags().skipRegions())
327+
<< " is must value, mark live";
328+
(void)solver.getOrCreateState<Liveness>(blockArg.value())
329+
->markLive();
330+
} else {
331+
LDBG() << "Block argument: " << blockArg.index() << " of "
332+
<< OpWithFlags(op, OpPrintingFlags().skipRegions())
333+
<< " has no liveness info, mark dead";
334+
solver.getOrCreateState<Liveness>(blockArg.value());
335+
}
321336
}
322337
}
323338
}

mlir/test/Transforms/remove-dead-values.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,16 @@ func.func @callee(%arg0: index, %arg1: index, %arg2: index) -> index {
649649
%res = call @mutl_parameter(%arg0, %arg1, %arg2) : (index, index, index) -> (index)
650650
return %res : index
651651
}
652+
653+
// -----
654+
655+
// This test verifies that the induction variables in loops are not deleted.
656+
657+
// CHECK-LABEL: func @dead_value_loop_ivs
658+
func.func @dead_value_loop_ivs(%lb: index, %ub: index, %step: index, %b : i1) -> i1 {
659+
%loop_ret = scf.for %iv = %lb to %ub step %step iter_args(%iter = %b) -> (i1) {
660+
cf.assert %b, "loop not dead"
661+
scf.yield %b : i1
662+
}
663+
return %loop_ret : i1
664+
}

0 commit comments

Comments
 (0)