1 //===--- DAGDeltaAlgorithm.cpp - A DAG Minimization Algorithm --*- C++ -*--===// 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 // The algorithm we use attempts to exploit the dependency information by 9 // minimizing top-down. We start by constructing an initial root set R, and 10 // then iteratively: 11 // 12 // 1. Minimize the set R using the test predicate: 13 // P'(S) = P(S union pred*(S)) 14 // 15 // 2. Extend R to R' = R union pred(R). 16 // 17 // until a fixed point is reached. 18 // 19 // The idea is that we want to quickly prune entire portions of the graph, so we 20 // try to find high-level nodes that can be eliminated with all of their 21 // dependents. 22 // 23 // FIXME: The current algorithm doesn't actually provide a strong guarantee 24 // about the minimality of the result. The problem is that after adding nodes to 25 // the required set, we no longer consider them for elimination. For strictly 26 // well formed predicates, this doesn't happen, but it commonly occurs in 27 // practice when there are unmodelled dependencies. I believe we can resolve 28 // this by allowing the required set to be minimized as well, but need more test 29 // cases first. 30 // 31 //===----------------------------------------------------------------------===// 32 33 #include "llvm/ADT/DAGDeltaAlgorithm.h" 34 #include "llvm/ADT/DeltaAlgorithm.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/Format.h" 37 #include "llvm/Support/raw_ostream.h" 38 #include <algorithm> 39 #include <cassert> 40 #include <iterator> 41 #include <map> 42 using namespace llvm; 43 44 #define DEBUG_TYPE "dag-delta" 45 46 namespace { 47 48 class DAGDeltaAlgorithmImpl { 49 friend class DeltaActiveSetHelper; 50 51 public: 52 typedef DAGDeltaAlgorithm::change_ty change_ty; 53 typedef DAGDeltaAlgorithm::changeset_ty changeset_ty; 54 typedef DAGDeltaAlgorithm::changesetlist_ty changesetlist_ty; 55 typedef DAGDeltaAlgorithm::edge_ty edge_ty; 56 57 private: 58 typedef std::vector<change_ty>::iterator pred_iterator_ty; 59 typedef std::vector<change_ty>::iterator succ_iterator_ty; 60 typedef std::set<change_ty>::iterator pred_closure_iterator_ty; 61 typedef std::set<change_ty>::iterator succ_closure_iterator_ty; 62 63 DAGDeltaAlgorithm &DDA; 64 65 std::vector<change_ty> Roots; 66 67 /// Cache of failed test results. Successful test results are never cached 68 /// since we always reduce following a success. We maintain an independent 69 /// cache from that used by the individual delta passes because we may get 70 /// hits across multiple individual delta invocations. 71 mutable std::set<changeset_ty> FailedTestsCache; 72 73 // FIXME: Gross. 74 std::map<change_ty, std::vector<change_ty> > Predecessors; 75 std::map<change_ty, std::vector<change_ty> > Successors; 76 77 std::map<change_ty, std::set<change_ty> > PredClosure; 78 std::map<change_ty, std::set<change_ty> > SuccClosure; 79 80 private: 81 pred_iterator_ty pred_begin(change_ty Node) { 82 assert(Predecessors.count(Node) && "Invalid node!"); 83 return Predecessors[Node].begin(); 84 } 85 pred_iterator_ty pred_end(change_ty Node) { 86 assert(Predecessors.count(Node) && "Invalid node!"); 87 return Predecessors[Node].end(); 88 } 89 90 pred_closure_iterator_ty pred_closure_begin(change_ty Node) { 91 assert(PredClosure.count(Node) && "Invalid node!"); 92 return PredClosure[Node].begin(); 93 } 94 pred_closure_iterator_ty pred_closure_end(change_ty Node) { 95 assert(PredClosure.count(Node) && "Invalid node!"); 96 return PredClosure[Node].end(); 97 } 98 99 succ_iterator_ty succ_begin(change_ty Node) { 100 assert(Successors.count(Node) && "Invalid node!"); 101 return Successors[Node].begin(); 102 } 103 succ_iterator_ty succ_end(change_ty Node) { 104 assert(Successors.count(Node) && "Invalid node!"); 105 return Successors[Node].end(); 106 } 107 108 succ_closure_iterator_ty succ_closure_begin(change_ty Node) { 109 assert(SuccClosure.count(Node) && "Invalid node!"); 110 return SuccClosure[Node].begin(); 111 } 112 succ_closure_iterator_ty succ_closure_end(change_ty Node) { 113 assert(SuccClosure.count(Node) && "Invalid node!"); 114 return SuccClosure[Node].end(); 115 } 116 117 void UpdatedSearchState(const changeset_ty &Changes, 118 const changesetlist_ty &Sets, 119 const changeset_ty &Required) { 120 DDA.UpdatedSearchState(Changes, Sets, Required); 121 } 122 123 /// ExecuteOneTest - Execute a single test predicate on the change set \p S. 124 bool ExecuteOneTest(const changeset_ty &S) { 125 // Check dependencies invariant. 126 LLVM_DEBUG({ 127 for (changeset_ty::const_iterator it = S.begin(), ie = S.end(); it != ie; 128 ++it) 129 for (succ_iterator_ty it2 = succ_begin(*it), ie2 = succ_end(*it); 130 it2 != ie2; ++it2) 131 assert(S.count(*it2) && "Attempt to run invalid changeset!"); 132 }); 133 134 return DDA.ExecuteOneTest(S); 135 } 136 137 public: 138 DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm &DDA, const changeset_ty &Changes, 139 const std::vector<edge_ty> &Dependencies); 140 141 changeset_ty Run(); 142 143 /// GetTestResult - Get the test result for the active set \p Changes with 144 /// \p Required changes from the cache, executing the test if necessary. 145 /// 146 /// \param Changes - The set of active changes being minimized, which should 147 /// have their pred closure included in the test. 148 /// \param Required - The set of changes which have previously been 149 /// established to be required. 150 /// \return - The test result. 151 bool GetTestResult(const changeset_ty &Changes, const changeset_ty &Required); 152 }; 153 154 /// Helper object for minimizing an active set of changes. 155 class DeltaActiveSetHelper : public DeltaAlgorithm { 156 DAGDeltaAlgorithmImpl &DDAI; 157 158 const changeset_ty &Required; 159 160 protected: 161 /// UpdatedSearchState - Callback used when the search state changes. 162 void UpdatedSearchState(const changeset_ty &Changes, 163 const changesetlist_ty &Sets) override { 164 DDAI.UpdatedSearchState(Changes, Sets, Required); 165 } 166 167 bool ExecuteOneTest(const changeset_ty &S) override { 168 return DDAI.GetTestResult(S, Required); 169 } 170 171 public: 172 DeltaActiveSetHelper(DAGDeltaAlgorithmImpl &DDAI, 173 const changeset_ty &Required) 174 : DDAI(DDAI), Required(Required) {} 175 }; 176 177 } // namespace 178 179 DAGDeltaAlgorithmImpl::DAGDeltaAlgorithmImpl( 180 DAGDeltaAlgorithm &DDA, const changeset_ty &Changes, 181 const std::vector<edge_ty> &Dependencies) 182 : DDA(DDA) { 183 for (change_ty Change : Changes) { 184 Predecessors.insert(std::make_pair(Change, std::vector<change_ty>())); 185 Successors.insert(std::make_pair(Change, std::vector<change_ty>())); 186 } 187 for (const edge_ty &Dep : Dependencies) { 188 Predecessors[Dep.second].push_back(Dep.first); 189 Successors[Dep.first].push_back(Dep.second); 190 } 191 192 // Compute the roots. 193 for (change_ty Change : Changes) 194 if (succ_begin(Change) == succ_end(Change)) 195 Roots.push_back(Change); 196 197 // Pre-compute the closure of the successor relation. 198 std::vector<change_ty> Worklist(Roots.begin(), Roots.end()); 199 while (!Worklist.empty()) { 200 change_ty Change = Worklist.back(); 201 Worklist.pop_back(); 202 203 std::set<change_ty> &ChangeSuccs = SuccClosure[Change]; 204 for (pred_iterator_ty it = pred_begin(Change), 205 ie = pred_end(Change); it != ie; ++it) { 206 SuccClosure[*it].insert(Change); 207 SuccClosure[*it].insert(ChangeSuccs.begin(), ChangeSuccs.end()); 208 Worklist.push_back(*it); 209 } 210 } 211 212 // Invert to form the predecessor closure map. 213 for (change_ty Change : Changes) 214 PredClosure.insert(std::make_pair(Change, std::set<change_ty>())); 215 for (change_ty Change : Changes) 216 for (succ_closure_iterator_ty it2 = succ_closure_begin(Change), 217 ie2 = succ_closure_end(Change); 218 it2 != ie2; ++it2) 219 PredClosure[*it2].insert(Change); 220 221 // Dump useful debug info. 222 LLVM_DEBUG({ 223 llvm::errs() << "-- DAGDeltaAlgorithmImpl --\n"; 224 llvm::errs() << "Changes: ["; 225 for (changeset_ty::const_iterator it = Changes.begin(), ie = Changes.end(); 226 it != ie; ++it) { 227 if (it != Changes.begin()) 228 llvm::errs() << ", "; 229 llvm::errs() << *it; 230 231 if (succ_begin(*it) != succ_end(*it)) { 232 llvm::errs() << "("; 233 for (succ_iterator_ty it2 = succ_begin(*it), ie2 = succ_end(*it); 234 it2 != ie2; ++it2) { 235 if (it2 != succ_begin(*it)) 236 llvm::errs() << ", "; 237 llvm::errs() << "->" << *it2; 238 } 239 llvm::errs() << ")"; 240 } 241 } 242 llvm::errs() << "]\n"; 243 244 llvm::errs() << "Roots: ["; 245 for (std::vector<change_ty>::const_iterator it = Roots.begin(), 246 ie = Roots.end(); 247 it != ie; ++it) { 248 if (it != Roots.begin()) 249 llvm::errs() << ", "; 250 llvm::errs() << *it; 251 } 252 llvm::errs() << "]\n"; 253 254 llvm::errs() << "Predecessor Closure:\n"; 255 for (change_ty Change : Changes) { 256 llvm::errs() << format(" %-4d: [", Change); 257 for (pred_closure_iterator_ty it2 = pred_closure_begin(Change), 258 ie2 = pred_closure_end(Change); 259 it2 != ie2; ++it2) { 260 if (it2 != pred_closure_begin(Change)) 261 llvm::errs() << ", "; 262 llvm::errs() << *it2; 263 } 264 llvm::errs() << "]\n"; 265 } 266 267 llvm::errs() << "Successor Closure:\n"; 268 for (change_ty Change : Changes) { 269 llvm::errs() << format(" %-4d: [", Change); 270 for (succ_closure_iterator_ty it2 = succ_closure_begin(Change), 271 ie2 = succ_closure_end(Change); 272 it2 != ie2; ++it2) { 273 if (it2 != succ_closure_begin(Change)) 274 llvm::errs() << ", "; 275 llvm::errs() << *it2; 276 } 277 llvm::errs() << "]\n"; 278 } 279 280 llvm::errs() << "\n\n"; 281 }); 282 } 283 284 bool DAGDeltaAlgorithmImpl::GetTestResult(const changeset_ty &Changes, 285 const changeset_ty &Required) { 286 changeset_ty Extended(Required); 287 Extended.insert(Changes.begin(), Changes.end()); 288 for (change_ty Change : Changes) 289 Extended.insert(pred_closure_begin(Change), pred_closure_end(Change)); 290 291 if (FailedTestsCache.count(Extended)) 292 return false; 293 294 bool Result = ExecuteOneTest(Extended); 295 if (!Result) 296 FailedTestsCache.insert(Extended); 297 298 return Result; 299 } 300 301 DAGDeltaAlgorithm::changeset_ty 302 DAGDeltaAlgorithmImpl::Run() { 303 // The current set of changes we are minimizing, starting at the roots. 304 changeset_ty CurrentSet(Roots.begin(), Roots.end()); 305 306 // The set of required changes. 307 changeset_ty Required; 308 309 // Iterate until the active set of changes is empty. Convergence is guaranteed 310 // assuming input was a DAG. 311 // 312 // Invariant: CurrentSet intersect Required == {} 313 // Invariant: Required == (Required union succ*(Required)) 314 while (!CurrentSet.empty()) { 315 LLVM_DEBUG({ 316 llvm::errs() << "DAG_DD - " << CurrentSet.size() << " active changes, " 317 << Required.size() << " required changes\n"; 318 }); 319 320 // Minimize the current set of changes. 321 DeltaActiveSetHelper Helper(*this, Required); 322 changeset_ty CurrentMinSet = Helper.Run(CurrentSet); 323 324 // Update the set of required changes. Since 325 // CurrentMinSet subset CurrentSet 326 // and after the last iteration, 327 // succ(CurrentSet) subset Required 328 // then 329 // succ(CurrentMinSet) subset Required 330 // and our invariant on Required is maintained. 331 Required.insert(CurrentMinSet.begin(), CurrentMinSet.end()); 332 333 // Replace the current set with the predecssors of the minimized set of 334 // active changes. 335 CurrentSet.clear(); 336 for (change_ty CT : CurrentMinSet) 337 CurrentSet.insert(pred_begin(CT), pred_end(CT)); 338 339 // FIXME: We could enforce CurrentSet intersect Required == {} here if we 340 // wanted to protect against cyclic graphs. 341 } 342 343 return Required; 344 } 345 346 void DAGDeltaAlgorithm::anchor() { 347 } 348 349 DAGDeltaAlgorithm::changeset_ty 350 DAGDeltaAlgorithm::Run(const changeset_ty &Changes, 351 const std::vector<edge_ty> &Dependencies) { 352 return DAGDeltaAlgorithmImpl(*this, Changes, Dependencies).Run(); 353 } 354