1 //===- UseDefAnalysis.cpp - Analysis for Transitive UseDef chains ---------===//
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 // This file implements Analysis functions specific to slicing in Function.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Analysis/SliceAnalysis.h"
14 #include "mlir/Dialect/Affine/IR/AffineOps.h"
15 #include "mlir/Dialect/SCF/SCF.h"
16 #include "mlir/IR/Function.h"
17 #include "mlir/IR/Operation.h"
18 #include "mlir/Support/LLVM.h"
19 #include "llvm/ADT/SetVector.h"
20 
21 ///
22 /// Implements Analysis functions specific to slicing in Function.
23 ///
24 
25 using namespace mlir;
26 
27 using llvm::SetVector;
28 
29 static void getForwardSliceImpl(Operation *op,
30                                 SetVector<Operation *> *forwardSlice,
31                                 TransitiveFilter filter) {
32   if (!op) {
33     return;
34   }
35 
36   // Evaluate whether we should keep this use.
37   // This is useful in particular to implement scoping; i.e. return the
38   // transitive forwardSlice in the current scope.
39   if (!filter(op)) {
40     return;
41   }
42 
43   if (auto forOp = dyn_cast<AffineForOp>(op)) {
44     for (auto *ownerOp : forOp.getInductionVar().getUsers())
45       if (forwardSlice->count(ownerOp) == 0)
46         getForwardSliceImpl(ownerOp, forwardSlice, filter);
47   } else if (auto forOp = dyn_cast<scf::ForOp>(op)) {
48     for (auto *ownerOp : forOp.getInductionVar().getUsers())
49       if (forwardSlice->count(ownerOp) == 0)
50         getForwardSliceImpl(ownerOp, forwardSlice, filter);
51     for (auto result : forOp.getResults())
52       for (auto *ownerOp : result.getUsers())
53         if (forwardSlice->count(ownerOp) == 0)
54           getForwardSliceImpl(ownerOp, forwardSlice, filter);
55   } else {
56     assert(op->getNumRegions() == 0 && "unexpected generic op with regions");
57     assert(op->getNumResults() <= 1 && "unexpected multiple results");
58     if (op->getNumResults() > 0) {
59       for (auto *ownerOp : op->getResult(0).getUsers())
60         if (forwardSlice->count(ownerOp) == 0)
61           getForwardSliceImpl(ownerOp, forwardSlice, filter);
62     }
63   }
64 
65   forwardSlice->insert(op);
66 }
67 
68 void mlir::getForwardSlice(Operation *op, SetVector<Operation *> *forwardSlice,
69                            TransitiveFilter filter) {
70   getForwardSliceImpl(op, forwardSlice, filter);
71   // Don't insert the top level operation, we just queried on it and don't
72   // want it in the results.
73   forwardSlice->remove(op);
74 
75   // Reverse to get back the actual topological order.
76   // std::reverse does not work out of the box on SetVector and I want an
77   // in-place swap based thing (the real std::reverse, not the LLVM adapter).
78   std::vector<Operation *> v(forwardSlice->takeVector());
79   forwardSlice->insert(v.rbegin(), v.rend());
80 }
81 
82 static void getBackwardSliceImpl(Operation *op,
83                                  SetVector<Operation *> *backwardSlice,
84                                  TransitiveFilter filter) {
85   if (!op)
86     return;
87 
88   assert((op->getNumRegions() == 0 || isa<AffineForOp>(op) ||
89           isa<scf::ForOp>(op)) &&
90          "unexpected generic op with regions");
91 
92   // Evaluate whether we should keep this def.
93   // This is useful in particular to implement scoping; i.e. return the
94   // transitive forwardSlice in the current scope.
95   if (!filter(op)) {
96     return;
97   }
98 
99   for (auto en : llvm::enumerate(op->getOperands())) {
100     auto operand = en.value();
101     if (auto blockArg = operand.dyn_cast<BlockArgument>()) {
102       if (auto affIv = getForInductionVarOwner(operand)) {
103         auto *affOp = affIv.getOperation();
104         if (backwardSlice->count(affOp) == 0)
105           getBackwardSliceImpl(affOp, backwardSlice, filter);
106       } else if (auto loopIv = scf::getForInductionVarOwner(operand)) {
107         auto *loopOp = loopIv.getOperation();
108         if (backwardSlice->count(loopOp) == 0)
109           getBackwardSliceImpl(loopOp, backwardSlice, filter);
110       } else if (blockArg.getOwner() !=
111                  &op->getParentOfType<FuncOp>().getBody().front()) {
112         op->emitError("unsupported CF for operand ") << en.index();
113         llvm_unreachable("Unsupported control flow");
114       }
115       continue;
116     }
117     auto *op = operand.getDefiningOp();
118     if (backwardSlice->count(op) == 0) {
119       getBackwardSliceImpl(op, backwardSlice, filter);
120     }
121   }
122 
123   backwardSlice->insert(op);
124 }
125 
126 void mlir::getBackwardSlice(Operation *op,
127                             SetVector<Operation *> *backwardSlice,
128                             TransitiveFilter filter) {
129   getBackwardSliceImpl(op, backwardSlice, filter);
130 
131   // Don't insert the top level operation, we just queried on it and don't
132   // want it in the results.
133   backwardSlice->remove(op);
134 }
135 
136 SetVector<Operation *> mlir::getSlice(Operation *op,
137                                       TransitiveFilter backwardFilter,
138                                       TransitiveFilter forwardFilter) {
139   SetVector<Operation *> slice;
140   slice.insert(op);
141 
142   unsigned currentIndex = 0;
143   SetVector<Operation *> backwardSlice;
144   SetVector<Operation *> forwardSlice;
145   while (currentIndex != slice.size()) {
146     auto *currentOp = (slice)[currentIndex];
147     // Compute and insert the backwardSlice starting from currentOp.
148     backwardSlice.clear();
149     getBackwardSlice(currentOp, &backwardSlice, backwardFilter);
150     slice.insert(backwardSlice.begin(), backwardSlice.end());
151 
152     // Compute and insert the forwardSlice starting from currentOp.
153     forwardSlice.clear();
154     getForwardSlice(currentOp, &forwardSlice, forwardFilter);
155     slice.insert(forwardSlice.begin(), forwardSlice.end());
156     ++currentIndex;
157   }
158   return topologicalSort(slice);
159 }
160 
161 namespace {
162 /// DFS post-order implementation that maintains a global count to work across
163 /// multiple invocations, to help implement topological sort on multi-root DAGs.
164 /// We traverse all operations but only record the ones that appear in
165 /// `toSort` for the final result.
166 struct DFSState {
167   DFSState(const SetVector<Operation *> &set)
168       : toSort(set), topologicalCounts(), seen() {}
169   const SetVector<Operation *> &toSort;
170   SmallVector<Operation *, 16> topologicalCounts;
171   DenseSet<Operation *> seen;
172 };
173 } // namespace
174 
175 static void DFSPostorder(Operation *current, DFSState *state) {
176   assert(current->getNumResults() <= 1 && "NYI: multi-result");
177   if (current->getNumResults() > 0) {
178     for (auto &u : current->getResult(0).getUses()) {
179       auto *op = u.getOwner();
180       DFSPostorder(op, state);
181     }
182   }
183   bool inserted;
184   using IterTy = decltype(state->seen.begin());
185   IterTy iter;
186   std::tie(iter, inserted) = state->seen.insert(current);
187   if (inserted) {
188     if (state->toSort.count(current) > 0) {
189       state->topologicalCounts.push_back(current);
190     }
191   }
192 }
193 
194 SetVector<Operation *>
195 mlir::topologicalSort(const SetVector<Operation *> &toSort) {
196   if (toSort.empty()) {
197     return toSort;
198   }
199 
200   // Run from each root with global count and `seen` set.
201   DFSState state(toSort);
202   for (auto *s : toSort) {
203     assert(toSort.count(s) == 1 && "NYI: multi-sets not supported");
204     DFSPostorder(s, &state);
205   }
206 
207   // Reorder and return.
208   SetVector<Operation *> res;
209   for (auto it = state.topologicalCounts.rbegin(),
210             eit = state.topologicalCounts.rend();
211        it != eit; ++it) {
212     res.insert(*it);
213   }
214   return res;
215 }
216