MLIR  20.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).
55 /// It is also to be noted that a value could be of multiple types (1/2/3) at
56 /// the same time.
57 ///
58 /// A value "has memory effects" iff it:
59 /// (1.a) is an operand of an op with memory effects OR
60 /// (1.b) is a non-forwarded branch operand and its branch op could take the
61 /// control to a block that has an op with memory effects OR
62 /// (1.c) is a non-forwarded call operand.
63 ///
64 /// A value `A` is said to be "used to compute" value `B` iff `B` cannot be
65 /// computed in the absence of `A`. Thus, in this implementation, we say that
66 /// value `A` is used to compute value `B` iff:
67 /// (3.a) `B` is a result of an op with operand `A` OR
68 /// (3.b) `A` is used to compute some value `C` and `C` is used to compute
69 /// `B`.
70 
71 LogicalResult
74  // This marks values of type (1.a) liveness as "live".
75  if (!isMemoryEffectFree(op)) {
76  for (auto *operand : operands)
77  propagateIfChanged(operand, operand->markLive());
78  }
79 
80  // This marks values of type (3) liveness as "live".
81  bool foundLiveResult = false;
82  for (const Liveness *r : results) {
83  if (r->isLive && !foundLiveResult) {
84  // It is assumed that each operand is used to compute each result of an
85  // op. Thus, if at least one result is live, each operand is live.
86  for (Liveness *operand : operands)
87  meet(operand, *r);
88  foundLiveResult = true;
89  }
90  addDependency(const_cast<Liveness *>(r), getProgramPointAfter(op));
91  }
92  return success();
93 }
94 
96  // We know (at the moment) and assume (for the future) that `operand` is a
97  // non-forwarded branch operand of a `RegionBranchOpInterface`,
98  // `BranchOpInterface`, `RegionBranchTerminatorOpInterface` or return-like op.
99  Operation *op = operand.getOwner();
100  assert((isa<RegionBranchOpInterface>(op) || isa<BranchOpInterface>(op) ||
101  isa<RegionBranchTerminatorOpInterface>(op)) &&
102  "expected the op to be `RegionBranchOpInterface`, "
103  "`BranchOpInterface` or `RegionBranchTerminatorOpInterface`");
104 
105  // The lattices of the non-forwarded branch operands don't get updated like
106  // the forwarded branch operands or the non-branch operands. Thus they need
107  // to be handled separately. This is where we handle them.
108 
109  // This marks values of type (1.b) liveness as "live". A non-forwarded
110  // branch operand will be live if a block where its op could take the control
111  // has an op with memory effects.
112  // Populating such blocks in `blocks`.
114  if (isa<RegionBranchOpInterface>(op)) {
115  // When the op is a `RegionBranchOpInterface`, like an `scf.for` or an
116  // `scf.index_switch` op, its branch operand controls the flow into this
117  // op's regions.
118  for (Region &region : op->getRegions()) {
119  for (Block &block : region)
120  blocks.push_back(&block);
121  }
122  } else if (isa<BranchOpInterface>(op)) {
123  // When the op is a `BranchOpInterface`, like a `cf.cond_br` or a
124  // `cf.switch` op, its branch operand controls the flow into this op's
125  // successors.
126  blocks = op->getSuccessors();
127  } else {
128  // When the op is a `RegionBranchTerminatorOpInterface`, like an
129  // `scf.condition` op or return-like, like an `scf.yield` op, its branch
130  // operand controls the flow into this op's parent's (which is a
131  // `RegionBranchOpInterface`'s) regions.
132  Operation *parentOp = op->getParentOp();
133  assert(isa<RegionBranchOpInterface>(parentOp) &&
134  "expected parent op to implement `RegionBranchOpInterface`");
135  for (Region &region : parentOp->getRegions()) {
136  for (Block &block : region)
137  blocks.push_back(&block);
138  }
139  }
140  bool foundMemoryEffectingOp = false;
141  for (Block *block : blocks) {
142  if (foundMemoryEffectingOp)
143  break;
144  for (Operation &nestedOp : *block) {
145  if (!isMemoryEffectFree(&nestedOp)) {
146  Liveness *operandLiveness = getLatticeElement(operand.get());
147  propagateIfChanged(operandLiveness, operandLiveness->markLive());
148  foundMemoryEffectingOp = true;
149  break;
150  }
151  }
152  }
153 
154  // Now that we have checked for memory-effecting ops in the blocks of concern,
155  // we will simply visit the op with this non-forwarded operand to potentially
156  // mark it "live" due to type (1.a/3) liveness.
157  SmallVector<Liveness *, 4> operandLiveness;
158  operandLiveness.push_back(getLatticeElement(operand.get()));
159  SmallVector<const Liveness *, 4> resultsLiveness;
160  for (const Value result : op->getResults())
161  resultsLiveness.push_back(getLatticeElement(result));
162  (void)visitOperation(op, operandLiveness, resultsLiveness);
163 
164  // We also visit the parent op with the parent's results and this operand if
165  // `op` is a `RegionBranchTerminatorOpInterface` because its non-forwarded
166  // operand depends on not only its memory effects/results but also on those of
167  // its parent's.
168  if (!isa<RegionBranchTerminatorOpInterface>(op))
169  return;
170  Operation *parentOp = op->getParentOp();
171  SmallVector<const Liveness *, 4> parentResultsLiveness;
172  for (const Value parentResult : parentOp->getResults())
173  parentResultsLiveness.push_back(getLatticeElement(parentResult));
174  (void)visitOperation(parentOp, operandLiveness, parentResultsLiveness);
175 }
176 
178  // We know (at the moment) and assume (for the future) that `operand` is a
179  // non-forwarded call operand of an op implementing `CallOpInterface`.
180  assert(isa<CallOpInterface>(operand.getOwner()) &&
181  "expected the op to implement `CallOpInterface`");
182 
183  // The lattices of the non-forwarded call operands don't get updated like the
184  // forwarded call operands or the non-call operands. Thus they need to be
185  // handled separately. This is where we handle them.
186 
187  // This marks values of type (1.c) liveness as "live". A non-forwarded
188  // call operand is live.
189  Liveness *operandLiveness = getLatticeElement(operand.get());
190  propagateIfChanged(operandLiveness, operandLiveness->markLive());
191 }
192 
194  // This marks values of type (2) liveness as "live".
195  (void)lattice->markLive();
196 }
197 
198 //===----------------------------------------------------------------------===//
199 // RunLivenessAnalysis
200 //===----------------------------------------------------------------------===//
201 
203  SymbolTableCollection symbolTable;
204 
205  solver.load<DeadCodeAnalysis>();
207  solver.load<LivenessAnalysis>(symbolTable);
208  (void)solver.initializeAndRun(op);
209 }
210 
212  return solver.lookupState<Liveness>(val);
213 }
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:267
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
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:672
SuccessorRange getSuccessors()
Definition: Operation.h:699
result_range getResults()
Definition: Operation.h:410
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 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)