[Mlir-commits] [mlir] 17de7ed - [MLIR][NFC] Minor cleanup in liveness.

Rahul Joshi llvmlistbot at llvm.org
Fri Jul 23 13:30:40 PDT 2021


Author: Rahul Joshi
Date: 2021-07-23T13:30:02-07:00
New Revision: 17de7ed556ad8df3d2dcaa9322a792fc70243194

URL: https://github.com/llvm/llvm-project/commit/17de7ed556ad8df3d2dcaa9322a792fc70243194
DIFF: https://github.com/llvm/llvm-project/commit/17de7ed556ad8df3d2dcaa9322a792fc70243194.diff

LOG: [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

Added: 
    

Modified: 
    mlir/include/mlir/Analysis/Liveness.h
    mlir/lib/Analysis/Liveness.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Analysis/Liveness.h b/mlir/include/mlir/Analysis/Liveness.h
index 3bd298a0fbe74..8c32a0ffda7e2 100644
--- a/mlir/include/mlir/Analysis/Liveness.h
+++ b/mlir/include/mlir/Analysis/Liveness.h
@@ -43,7 +43,7 @@ class Value;
 ///   auto &allInValues = liveness.getLiveIn(block);
 ///   auto &allOutValues = liveness.getLiveOut(block);
 ///   auto allOperationsInWhichValueIsLive = liveness.resolveLiveness(value);
-///   bool lastUse = liveness.isLastUse(value, operation);
+///   bool isDeafAfter = liveness.isDeadAfter(value, operation);
 class Liveness {
 public:
   using OperationListT = std::vector<Operation *>;
@@ -74,9 +74,8 @@ class Liveness {
   /// Returns a reference to a set containing live-out values (unordered).
   const ValueSetT &getLiveOut(Block *block) const;
 
-  /// Returns true if the given operation represent the last use of the
-  /// given value.
-  bool isLastUse(Value value, Operation *operation) const;
+  /// Returns true if `value` is not live after `operation`.
+  bool isDeadAfter(Value value, Operation *operation) const;
 
   /// Dumps the liveness information in a human readable format.
   void dump() const;

diff  --git a/mlir/lib/Analysis/Liveness.cpp b/mlir/lib/Analysis/Liveness.cpp
index 1c26db1df8dfb..39061679e295d 100644
--- a/mlir/lib/Analysis/Liveness.cpp
+++ b/mlir/lib/Analysis/Liveness.cpp
@@ -63,21 +63,16 @@ struct BlockInfoBuilder {
       for (Value result : operation.getResults())
         gatherOutValues(result);
 
-    // Mark all nested operation results as defined.
+    // Mark all nested operation results as defined, and nested operation
+    // operands as used. All defined value will be removed from the used set
+    // at the end.
     block->walk([&](Operation *op) {
       for (Value result : op->getResults())
         defValues.insert(result);
+      for (Value operand : op->getOperands())
+        useValues.insert(operand);
     });
-
-    // Check all operations for used operands.
-    block->walk([&](Operation *op) {
-      for (Value operand : op->getOperands()) {
-        // If the operand is already defined in the scope of this
-        // block, we can skip the value in the use set.
-        if (!defValues.count(operand))
-          useValues.insert(operand);
-      }
-    });
+    llvm::set_subtract(useValues, defValues);
   }
 
   /// Updates live-in information of the current block. To do so it uses the
@@ -94,16 +89,16 @@ struct BlockInfoBuilder {
     if (newIn.size() == inValues.size())
       return false;
 
-    inValues = newIn;
+    inValues = std::move(newIn);
     return true;
   }
 
   /// Updates live-out information of the current block. It iterates over all
   /// successors and unifies their live-in values with the current live-out
   /// values.
-  template <typename SourceT> void updateLiveOut(SourceT &source) {
+  void updateLiveOut(const DenseMap<Block *, BlockInfoBuilder> &builders) {
     for (Block *succ : block->getSuccessors()) {
-      BlockInfoBuilder &builder = source[succ];
+      const BlockInfoBuilder &builder = builders.find(succ)->second;
       llvm::set_union(outValues, builder.inValues);
     }
   }
@@ -138,7 +133,7 @@ static void buildBlockMapping(Operation *operation,
       toProcess.insert(block->pred_begin(), block->pred_end());
   });
 
-  // Propagate the in and out-value sets (fixpoint iteration)
+  // Propagate the in and out-value sets (fixpoint iteration).
   while (!toProcess.empty()) {
     Block *current = toProcess.pop_back_val();
     BlockInfoBuilder &builder = builders[current];
@@ -162,7 +157,6 @@ Liveness::Liveness(Operation *op) : operation(op) { build(); }
 
 /// Initializes the internal mappings.
 void Liveness::build() {
-
   // Build internal block mapping.
   DenseMap<Block *, BlockInfoBuilder> builders;
   buildBlockMapping(operation, builders);
@@ -242,9 +236,8 @@ const Liveness::ValueSetT &Liveness::getLiveOut(Block *block) const {
   return getLiveness(block)->out();
 }
 
-/// Returns true if the given operation represent the last use of the given
-/// value.
-bool Liveness::isLastUse(Value value, Operation *operation) const {
+/// Returns true if `value` is not live after `operation`.
+bool Liveness::isDeadAfter(Value value, Operation *operation) const {
   Block *block = operation->getBlock();
   const LivenessBlockInfo *blockInfo = getLiveness(block);
 


        


More information about the Mlir-commits mailing list