MLIR  21.0.0git
LivenessAnalysis.cpp
Go to the documentation of this file.
1 //===- LivenessAnalysis.cpp - Liveness analysis ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/IR/SymbolTable.h"
10 #include <cassert>
12 
17 #include <mlir/IR/Operation.h>
18 #include <mlir/IR/Value.h>
21 #include <mlir/Support/LLVM.h>
22 
23 using namespace mlir;
24 using namespace mlir::dataflow;
25 
26 //===----------------------------------------------------------------------===//
27 // Liveness
28 //===----------------------------------------------------------------------===//
29 
30 void Liveness::print(raw_ostream &os) const {
31  os << (isLive ? "live" : "not live");
32 }
33 
35  bool wasLive = isLive;
36  isLive = true;
38 }
39 
41  const auto *otherLiveness = reinterpret_cast<const Liveness *>(&other);
42  return otherLiveness->isLive ? markLive() : ChangeResult::NoChange;
43 }
44 
45 //===----------------------------------------------------------------------===//
46 // LivenessAnalysis
47 //===----------------------------------------------------------------------===//
48 
49 /// For every value, liveness analysis determines whether or not it is "live".
50 ///
51 /// A value is considered "live" iff it:
52 /// (1) has memory effects OR
53 /// (2) is returned by a public function OR
54 /// (3) is used to compute a value of type (1) or (2) OR
55 /// (4) is returned by a return-like op whose parent isn't a callable
56 /// nor a RegionBranchOpInterface (e.g.: linalg.yield, gpu.yield,...)
57 /// These ops have their own semantics, so we conservatively mark the
58 /// the yield value as live.
59 /// It is also to be noted that a value could be of multiple types (1/2/3) at
60 /// the same time.
61 ///
62 /// A value "has memory effects" iff it:
63 /// (1.a) is an operand of an op with memory effects OR
64 /// (1.b) is a non-forwarded branch operand and its branch op could take the
65 /// control to a block that has an op with memory effects OR
66 /// (1.c) is a non-forwarded branch operand and its branch op could result
67 /// in different live result OR
68 /// (1.d) is a non-forwarded call operand.
69 ///
70 /// A value `A` is said to be "used to compute" value `B` iff `B` cannot be
71 /// computed in the absence of `A`. Thus, in this implementation, we say that
72 /// value `A` is used to compute value `B` iff:
73 /// (3.a) `B` is a result of an op with operand `A` OR
74 /// (3.b) `A` is used to compute some value `C` and `C` is used to compute
75 /// `B`.
76 
77 LogicalResult
80  // This marks values of type (1.a) and (4) liveness as "live".
81  if (!isMemoryEffectFree(op) || op->hasTrait<OpTrait::ReturnLike>()) {
82  for (auto *operand : operands)
83  propagateIfChanged(operand, operand->markLive());
84  }
85 
86  // This marks values of type (3) liveness as "live".
87  bool foundLiveResult = false;
88  for (const Liveness *r : results) {
89  if (r->isLive && !foundLiveResult) {
90  // It is assumed that each operand is used to compute each result of an
91  // op. Thus, if at least one result is live, each operand is live.
92  for (Liveness *operand : operands)
93  meet(operand, *r);
94  foundLiveResult = true;
95  }
96  addDependency(const_cast<Liveness *>(r), getProgramPointAfter(op));
97  }
98  return success();
99 }
100 
102  // We know (at the moment) and assume (for the future) that `operand` is a
103  // non-forwarded branch operand of a `RegionBranchOpInterface`,
104  // `BranchOpInterface`, `RegionBranchTerminatorOpInterface` or return-like op.
105  Operation *op = operand.getOwner();
106  assert((isa<RegionBranchOpInterface>(op) || isa<BranchOpInterface>(op) ||
107  isa<RegionBranchTerminatorOpInterface>(op)) &&
108  "expected the op to be `RegionBranchOpInterface`, "
109  "`BranchOpInterface` or `RegionBranchTerminatorOpInterface`");
110 
111  // The lattices of the non-forwarded branch operands don't get updated like
112  // the forwarded branch operands or the non-branch operands. Thus they need
113  // to be handled separately. This is where we handle them.
114 
115  // This marks values of type (1.b/1.c) liveness as "live". A non-forwarded
116  // branch operand will be live if a block where its op could take the control
117  // has an op with memory effects or could result in different results.
118  // Populating such blocks in `blocks`.
119  bool mayLive = false;
121  if (isa<RegionBranchOpInterface>(op)) {
122  if (op->getNumResults() != 0) {
123  // This mark value of type 1.c liveness as may live, because the region
124  // branch operation has a return value, and the non-forwarded operand can
125  // determine the region to jump to, it can thereby control the result of
126  // the region branch operation.
127  // Therefore, if the result value is live, we conservatively consider the
128  // non-forwarded operand of the region branch operation with result may
129  // live and record all result.
130  for (Value result : op->getResults()) {
131  if (getLatticeElement(result)->isLive) {
132  mayLive = true;
133  break;
134  }
135  }
136  } else {
137  // When the op is a `RegionBranchOpInterface`, like an `scf.for` or an
138  // `scf.index_switch` op, its branch operand controls the flow into this
139  // op's regions.
140  for (Region &region : op->getRegions()) {
141  for (Block &block : region)
142  blocks.push_back(&block);
143  }
144  }
145  } else if (isa<BranchOpInterface>(op)) {
146  // We cannot track all successor blocks of the branch operation(More
147  // specifically, it's the successor's successor). Additionally, different
148  // blocks might also lead to the different block argument described in 1.c.
149  // Therefore, we conservatively consider the non-forwarded operand of the
150  // branch operation may live.
151  mayLive = true;
152  } else {
153  Operation *parentOp = op->getParentOp();
154  assert(isa<RegionBranchOpInterface>(parentOp) &&
155  "expected parent op to implement `RegionBranchOpInterface`");
156  if (parentOp->getNumResults() != 0) {
157  // This mark value of type 1.c liveness as may live, because the region
158  // branch operation has a return value, and the non-forwarded operand can
159  // determine the region to jump to, it can thereby control the result of
160  // the region branch operation.
161  // Therefore, if the result value is live, we conservatively consider the
162  // non-forwarded operand of the region branch operation with result may
163  // live and record all result.
164  for (Value result : parentOp->getResults()) {
165  if (getLatticeElement(result)->isLive) {
166  mayLive = true;
167  break;
168  }
169  }
170  } else {
171  // When the op is a `RegionBranchTerminatorOpInterface`, like an
172  // `scf.condition` op or return-like, like an `scf.yield` op, its branch
173  // operand controls the flow into this op's parent's (which is a
174  // `RegionBranchOpInterface`'s) regions.
175  for (Region &region : parentOp->getRegions()) {
176  for (Block &block : region)
177  blocks.push_back(&block);
178  }
179  }
180  }
181  for (Block *block : blocks) {
182  if (mayLive)
183  break;
184  for (Operation &nestedOp : *block) {
185  if (!isMemoryEffectFree(&nestedOp)) {
186  mayLive = true;
187  break;
188  }
189  }
190  }
191 
192  if (mayLive) {
193  Liveness *operandLiveness = getLatticeElement(operand.get());
194  propagateIfChanged(operandLiveness, operandLiveness->markLive());
195  }
196 
197  // Now that we have checked for memory-effecting ops in the blocks of concern,
198  // we will simply visit the op with this non-forwarded operand to potentially
199  // mark it "live" due to type (1.a/3) liveness.
200  SmallVector<Liveness *, 4> operandLiveness;
201  operandLiveness.push_back(getLatticeElement(operand.get()));
202  SmallVector<const Liveness *, 4> resultsLiveness;
203  for (const Value result : op->getResults())
204  resultsLiveness.push_back(getLatticeElement(result));
205  (void)visitOperation(op, operandLiveness, resultsLiveness);
206 
207  // We also visit the parent op with the parent's results and this operand if
208  // `op` is a `RegionBranchTerminatorOpInterface` because its non-forwarded
209  // operand depends on not only its memory effects/results but also on those of
210  // its parent's.
211  if (!isa<RegionBranchTerminatorOpInterface>(op))
212  return;
213  Operation *parentOp = op->getParentOp();
214  SmallVector<const Liveness *, 4> parentResultsLiveness;
215  for (const Value parentResult : parentOp->getResults())
216  parentResultsLiveness.push_back(getLatticeElement(parentResult));
217  (void)visitOperation(parentOp, operandLiveness, parentResultsLiveness);
218 }
219 
221  // We know (at the moment) and assume (for the future) that `operand` is a
222  // non-forwarded call operand of an op implementing `CallOpInterface`.
223  assert(isa<CallOpInterface>(operand.getOwner()) &&
224  "expected the op to implement `CallOpInterface`");
225 
226  // The lattices of the non-forwarded call operands don't get updated like the
227  // forwarded call operands or the non-call operands. Thus they need to be
228  // handled separately. This is where we handle them.
229 
230  // This marks values of type (1.c) liveness as "live". A non-forwarded
231  // call operand is live.
232  Liveness *operandLiveness = getLatticeElement(operand.get());
233  propagateIfChanged(operandLiveness, operandLiveness->markLive());
234 }
235 
237  if (lattice->isLive) {
238  return;
239  }
240  // This marks values of type (2) liveness as "live".
241  (void)lattice->markLive();
243 }
244 
245 //===----------------------------------------------------------------------===//
246 // RunLivenessAnalysis
247 //===----------------------------------------------------------------------===//
248 
250  SymbolTableCollection symbolTable;
251 
252  solver.load<DeadCodeAnalysis>();
254  solver.load<LivenessAnalysis>(symbolTable);
255  (void)solver.initializeAndRun(op);
256 }
257 
259  return solver.lookupState<Liveness>(val);
260 }
Block represents an ordered list of Operations.
Definition: Block.h:33
void addDependency(AnalysisState *state, ProgramPoint *point)
Create a dependency between the given analysis state and lattice anchor on this analysis.
void propagateIfChanged(AnalysisState *state, ChangeResult changed)
Propagate an update to a state if it changed.
ProgramPoint * getProgramPointAfter(Operation *op)
const StateT * lookupState(AnchorT anchor) const
Lookup an analysis state for the given lattice anchor.
AnalysisT * load(Args &&...args)
Load an analysis into the solver. Return the analysis instance.
LogicalResult initializeAndRun(Operation *top)
Initialize the children analyses starting from the provided top-level operation and run the analysis ...
IRValueT get() const
Return the current value being used by this operand.
Definition: UseDefLists.h:160
This class represents an operand of an operation.
Definition: Value.h:257
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition: Operation.h:749
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition: Operation.h:234
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Definition: Operation.h:677
result_range getResults()
Definition: Operation.h:415
unsigned getNumResults()
Return the number of results held by this operation.
Definition: Operation.h:404
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
This class represents a collection of SymbolTables.
Definition: SymbolTable.h:283
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
void meet(AbstractSparseLattice *lhs, const AbstractSparseLattice &rhs)
Join the lattice element and propagate and update if it changed.
This class represents an abstract lattice.
Dead code analysis analyzes control-flow, as understood by RegionBranchOpInterface and BranchOpInterf...
An analysis that, by going backwards along the dataflow graph, annotates each value with a boolean st...
void setToExitState(Liveness *lattice) override
Set the given lattice element(s) at control flow exit point(s).
void visitBranchOperand(OpOperand &operand) override
void visitCallOperand(OpOperand &operand) override
LogicalResult visitOperation(Operation *op, ArrayRef< Liveness * > operands, ArrayRef< const Liveness * > results) override
For every value, liveness analysis determines whether or not it is "live".
Liveness * getLatticeElement(Value value) override
Get the lattice element for a value.
This analysis implements sparse constant propagation, which attempts to determine constant-valued res...
Operation * getOwner() const
Return the owner of this operand.
Definition: UseDefLists.h:38
Include the generated interface declarations.
ChangeResult
A result type used to indicate if a change happened.
bool isMemoryEffectFree(Operation *op)
Returns true if the given operation is free of memory effects.
This trait indicates that a terminator operation is "return-like".
This lattice represents, for a given value, whether or not it is "live".
void print(raw_ostream &os) const override
Print the contents of the analysis state.
ChangeResult meet(const AbstractSparseLattice &other) override
Meet (intersect) the information in this lattice with 'rhs'.
const Liveness * getLiveness(Value val)