Skip to content

Commit 17de7ed

Browse files
committed
[MLIR][NFC] Minor cleanup in liveness.
- Rename isLastUse to isDeadAfter to reflect what the function does. - Avoid a second walk over all operations in BlockInfoBuilder constructor. - use std::move() to save the new in set. Differential Revision: https://reviews.llvm.org/D106702
1 parent 3c2c985 commit 17de7ed

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

mlir/include/mlir/Analysis/Liveness.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Value;
4343
/// auto &allInValues = liveness.getLiveIn(block);
4444
/// auto &allOutValues = liveness.getLiveOut(block);
4545
/// auto allOperationsInWhichValueIsLive = liveness.resolveLiveness(value);
46-
/// bool lastUse = liveness.isLastUse(value, operation);
46+
/// bool isDeafAfter = liveness.isDeadAfter(value, operation);
4747
class Liveness {
4848
public:
4949
using OperationListT = std::vector<Operation *>;
@@ -74,9 +74,8 @@ class Liveness {
7474
/// Returns a reference to a set containing live-out values (unordered).
7575
const ValueSetT &getLiveOut(Block *block) const;
7676

77-
/// Returns true if the given operation represent the last use of the
78-
/// given value.
79-
bool isLastUse(Value value, Operation *operation) const;
77+
/// Returns true if `value` is not live after `operation`.
78+
bool isDeadAfter(Value value, Operation *operation) const;
8079

8180
/// Dumps the liveness information in a human readable format.
8281
void dump() const;

mlir/lib/Analysis/Liveness.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,16 @@ struct BlockInfoBuilder {
6363
for (Value result : operation.getResults())
6464
gatherOutValues(result);
6565

66-
// Mark all nested operation results as defined.
66+
// Mark all nested operation results as defined, and nested operation
67+
// operands as used. All defined value will be removed from the used set
68+
// at the end.
6769
block->walk([&](Operation *op) {
6870
for (Value result : op->getResults())
6971
defValues.insert(result);
72+
for (Value operand : op->getOperands())
73+
useValues.insert(operand);
7074
});
71-
72-
// Check all operations for used operands.
73-
block->walk([&](Operation *op) {
74-
for (Value operand : op->getOperands()) {
75-
// If the operand is already defined in the scope of this
76-
// block, we can skip the value in the use set.
77-
if (!defValues.count(operand))
78-
useValues.insert(operand);
79-
}
80-
});
75+
llvm::set_subtract(useValues, defValues);
8176
}
8277

8378
/// Updates live-in information of the current block. To do so it uses the
@@ -94,16 +89,16 @@ struct BlockInfoBuilder {
9489
if (newIn.size() == inValues.size())
9590
return false;
9691

97-
inValues = newIn;
92+
inValues = std::move(newIn);
9893
return true;
9994
}
10095

10196
/// Updates live-out information of the current block. It iterates over all
10297
/// successors and unifies their live-in values with the current live-out
10398
/// values.
104-
template <typename SourceT> void updateLiveOut(SourceT &source) {
99+
void updateLiveOut(const DenseMap<Block *, BlockInfoBuilder> &builders) {
105100
for (Block *succ : block->getSuccessors()) {
106-
BlockInfoBuilder &builder = source[succ];
101+
const BlockInfoBuilder &builder = builders.find(succ)->second;
107102
llvm::set_union(outValues, builder.inValues);
108103
}
109104
}
@@ -138,7 +133,7 @@ static void buildBlockMapping(Operation *operation,
138133
toProcess.insert(block->pred_begin(), block->pred_end());
139134
});
140135

141-
// Propagate the in and out-value sets (fixpoint iteration)
136+
// Propagate the in and out-value sets (fixpoint iteration).
142137
while (!toProcess.empty()) {
143138
Block *current = toProcess.pop_back_val();
144139
BlockInfoBuilder &builder = builders[current];
@@ -162,7 +157,6 @@ Liveness::Liveness(Operation *op) : operation(op) { build(); }
162157

163158
/// Initializes the internal mappings.
164159
void Liveness::build() {
165-
166160
// Build internal block mapping.
167161
DenseMap<Block *, BlockInfoBuilder> builders;
168162
buildBlockMapping(operation, builders);
@@ -242,9 +236,8 @@ const Liveness::ValueSetT &Liveness::getLiveOut(Block *block) const {
242236
return getLiveness(block)->out();
243237
}
244238

245-
/// Returns true if the given operation represent the last use of the given
246-
/// value.
247-
bool Liveness::isLastUse(Value value, Operation *operation) const {
239+
/// Returns true if `value` is not live after `operation`.
240+
bool Liveness::isDeadAfter(Value value, Operation *operation) const {
248241
Block *block = operation->getBlock();
249242
const LivenessBlockInfo *blockInfo = getLiveness(block);
250243

0 commit comments

Comments
 (0)