DAGDeltaAlgorithm.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include "llvm/ADT/DAGDeltaAlgorithm.h"
  33. #include "llvm/ADT/DeltaAlgorithm.h"
  34. #include "llvm/Support/Debug.h"
  35. #include "llvm/Support/Format.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include <algorithm>
  38. #include <cassert>
  39. #include <map>
  40. using namespace llvm;
  41. #define DEBUG_TYPE "dag-delta"
  42. namespace {
  43. class DAGDeltaAlgorithmImpl {
  44. friend class DeltaActiveSetHelper;
  45. public:
  46. typedef DAGDeltaAlgorithm::change_ty change_ty;
  47. typedef DAGDeltaAlgorithm::changeset_ty changeset_ty;
  48. typedef DAGDeltaAlgorithm::changesetlist_ty changesetlist_ty;
  49. typedef DAGDeltaAlgorithm::edge_ty edge_ty;
  50. private:
  51. typedef std::vector<change_ty>::iterator pred_iterator_ty;
  52. typedef std::vector<change_ty>::iterator succ_iterator_ty;
  53. typedef std::set<change_ty>::iterator pred_closure_iterator_ty;
  54. typedef std::set<change_ty>::iterator succ_closure_iterator_ty;
  55. DAGDeltaAlgorithm &DDA;
  56. std::vector<change_ty> Roots;
  57. /// Cache of failed test results. Successful test results are never cached
  58. /// since we always reduce following a success. We maintain an independent
  59. /// cache from that used by the individual delta passes because we may get
  60. /// hits across multiple individual delta invocations.
  61. mutable std::set<changeset_ty> FailedTestsCache;
  62. // FIXME: Gross.
  63. std::map<change_ty, std::vector<change_ty> > Predecessors;
  64. std::map<change_ty, std::vector<change_ty> > Successors;
  65. std::map<change_ty, std::set<change_ty> > PredClosure;
  66. std::map<change_ty, std::set<change_ty> > SuccClosure;
  67. private:
  68. pred_iterator_ty pred_begin(change_ty Node) {
  69. assert(Predecessors.count(Node) && "Invalid node!");
  70. return Predecessors[Node].begin();
  71. }
  72. pred_iterator_ty pred_end(change_ty Node) {
  73. assert(Predecessors.count(Node) && "Invalid node!");
  74. return Predecessors[Node].end();
  75. }
  76. pred_closure_iterator_ty pred_closure_begin(change_ty Node) {
  77. assert(PredClosure.count(Node) && "Invalid node!");
  78. return PredClosure[Node].begin();
  79. }
  80. pred_closure_iterator_ty pred_closure_end(change_ty Node) {
  81. assert(PredClosure.count(Node) && "Invalid node!");
  82. return PredClosure[Node].end();
  83. }
  84. succ_iterator_ty succ_begin(change_ty Node) {
  85. assert(Successors.count(Node) && "Invalid node!");
  86. return Successors[Node].begin();
  87. }
  88. succ_iterator_ty succ_end(change_ty Node) {
  89. assert(Successors.count(Node) && "Invalid node!");
  90. return Successors[Node].end();
  91. }
  92. succ_closure_iterator_ty succ_closure_begin(change_ty Node) {
  93. assert(SuccClosure.count(Node) && "Invalid node!");
  94. return SuccClosure[Node].begin();
  95. }
  96. succ_closure_iterator_ty succ_closure_end(change_ty Node) {
  97. assert(SuccClosure.count(Node) && "Invalid node!");
  98. return SuccClosure[Node].end();
  99. }
  100. void UpdatedSearchState(const changeset_ty &Changes,
  101. const changesetlist_ty &Sets,
  102. const changeset_ty &Required) {
  103. DDA.UpdatedSearchState(Changes, Sets, Required);
  104. }
  105. /// ExecuteOneTest - Execute a single test predicate on the change set \p S.
  106. bool ExecuteOneTest(const changeset_ty &S) {
  107. // Check dependencies invariant.
  108. LLVM_DEBUG({
  109. for (changeset_ty::const_iterator it = S.begin(), ie = S.end(); it != ie;
  110. ++it)
  111. for (succ_iterator_ty it2 = succ_begin(*it), ie2 = succ_end(*it);
  112. it2 != ie2; ++it2)
  113. assert(S.count(*it2) && "Attempt to run invalid changeset!");
  114. });
  115. return DDA.ExecuteOneTest(S);
  116. }
  117. public:
  118. DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm &DDA, const changeset_ty &Changes,
  119. const std::vector<edge_ty> &Dependencies);
  120. changeset_ty Run();
  121. /// GetTestResult - Get the test result for the active set \p Changes with
  122. /// \p Required changes from the cache, executing the test if necessary.
  123. ///
  124. /// \param Changes - The set of active changes being minimized, which should
  125. /// have their pred closure included in the test.
  126. /// \param Required - The set of changes which have previously been
  127. /// established to be required.
  128. /// \return - The test result.
  129. bool GetTestResult(const changeset_ty &Changes, const changeset_ty &Required);
  130. };
  131. /// Helper object for minimizing an active set of changes.
  132. class DeltaActiveSetHelper : public DeltaAlgorithm {
  133. DAGDeltaAlgorithmImpl &DDAI;
  134. const changeset_ty &Required;
  135. protected:
  136. /// UpdatedSearchState - Callback used when the search state changes.
  137. void UpdatedSearchState(const changeset_ty &Changes,
  138. const changesetlist_ty &Sets) override {
  139. DDAI.UpdatedSearchState(Changes, Sets, Required);
  140. }
  141. bool ExecuteOneTest(const changeset_ty &S) override {
  142. return DDAI.GetTestResult(S, Required);
  143. }
  144. public:
  145. DeltaActiveSetHelper(DAGDeltaAlgorithmImpl &DDAI,
  146. const changeset_ty &Required)
  147. : DDAI(DDAI), Required(Required) {}
  148. };
  149. } // namespace
  150. DAGDeltaAlgorithmImpl::DAGDeltaAlgorithmImpl(
  151. DAGDeltaAlgorithm &DDA, const changeset_ty &Changes,
  152. const std::vector<edge_ty> &Dependencies)
  153. : DDA(DDA) {
  154. for (change_ty Change : Changes) {
  155. Predecessors.insert(std::make_pair(Change, std::vector<change_ty>()));
  156. Successors.insert(std::make_pair(Change, std::vector<change_ty>()));
  157. }
  158. for (const edge_ty &Dep : Dependencies) {
  159. Predecessors[Dep.second].push_back(Dep.first);
  160. Successors[Dep.first].push_back(Dep.second);
  161. }
  162. // Compute the roots.
  163. for (change_ty Change : Changes)
  164. if (succ_begin(Change) == succ_end(Change))
  165. Roots.push_back(Change);
  166. // Pre-compute the closure of the successor relation.
  167. std::vector<change_ty> Worklist(Roots.begin(), Roots.end());
  168. while (!Worklist.empty()) {
  169. change_ty Change = Worklist.back();
  170. Worklist.pop_back();
  171. std::set<change_ty> &ChangeSuccs = SuccClosure[Change];
  172. for (pred_iterator_ty it = pred_begin(Change),
  173. ie = pred_end(Change); it != ie; ++it) {
  174. SuccClosure[*it].insert(Change);
  175. SuccClosure[*it].insert(ChangeSuccs.begin(), ChangeSuccs.end());
  176. Worklist.push_back(*it);
  177. }
  178. }
  179. // Invert to form the predecessor closure map.
  180. for (change_ty Change : Changes)
  181. PredClosure.insert(std::make_pair(Change, std::set<change_ty>()));
  182. for (change_ty Change : Changes)
  183. for (succ_closure_iterator_ty it2 = succ_closure_begin(Change),
  184. ie2 = succ_closure_end(Change);
  185. it2 != ie2; ++it2)
  186. PredClosure[*it2].insert(Change);
  187. // Dump useful debug info.
  188. LLVM_DEBUG({
  189. llvm::errs() << "-- DAGDeltaAlgorithmImpl --\n";
  190. llvm::errs() << "Changes: [";
  191. for (changeset_ty::const_iterator it = Changes.begin(), ie = Changes.end();
  192. it != ie; ++it) {
  193. if (it != Changes.begin())
  194. llvm::errs() << ", ";
  195. llvm::errs() << *it;
  196. if (succ_begin(*it) != succ_end(*it)) {
  197. llvm::errs() << "(";
  198. for (succ_iterator_ty it2 = succ_begin(*it), ie2 = succ_end(*it);
  199. it2 != ie2; ++it2) {
  200. if (it2 != succ_begin(*it))
  201. llvm::errs() << ", ";
  202. llvm::errs() << "->" << *it2;
  203. }
  204. llvm::errs() << ")";
  205. }
  206. }
  207. llvm::errs() << "]\n";
  208. llvm::errs() << "Roots: [";
  209. for (std::vector<change_ty>::const_iterator it = Roots.begin(),
  210. ie = Roots.end();
  211. it != ie; ++it) {
  212. if (it != Roots.begin())
  213. llvm::errs() << ", ";
  214. llvm::errs() << *it;
  215. }
  216. llvm::errs() << "]\n";
  217. llvm::errs() << "Predecessor Closure:\n";
  218. for (change_ty Change : Changes) {
  219. llvm::errs() << format(" %-4d: [", Change);
  220. for (pred_closure_iterator_ty it2 = pred_closure_begin(Change),
  221. ie2 = pred_closure_end(Change);
  222. it2 != ie2; ++it2) {
  223. if (it2 != pred_closure_begin(Change))
  224. llvm::errs() << ", ";
  225. llvm::errs() << *it2;
  226. }
  227. llvm::errs() << "]\n";
  228. }
  229. llvm::errs() << "Successor Closure:\n";
  230. for (change_ty Change : Changes) {
  231. llvm::errs() << format(" %-4d: [", Change);
  232. for (succ_closure_iterator_ty it2 = succ_closure_begin(Change),
  233. ie2 = succ_closure_end(Change);
  234. it2 != ie2; ++it2) {
  235. if (it2 != succ_closure_begin(Change))
  236. llvm::errs() << ", ";
  237. llvm::errs() << *it2;
  238. }
  239. llvm::errs() << "]\n";
  240. }
  241. llvm::errs() << "\n\n";
  242. });
  243. }
  244. bool DAGDeltaAlgorithmImpl::GetTestResult(const changeset_ty &Changes,
  245. const changeset_ty &Required) {
  246. changeset_ty Extended(Required);
  247. Extended.insert(Changes.begin(), Changes.end());
  248. for (change_ty Change : Changes)
  249. Extended.insert(pred_closure_begin(Change), pred_closure_end(Change));
  250. if (FailedTestsCache.count(Extended))
  251. return false;
  252. bool Result = ExecuteOneTest(Extended);
  253. if (!Result)
  254. FailedTestsCache.insert(Extended);
  255. return Result;
  256. }
  257. DAGDeltaAlgorithm::changeset_ty
  258. DAGDeltaAlgorithmImpl::Run() {
  259. // The current set of changes we are minimizing, starting at the roots.
  260. changeset_ty CurrentSet(Roots.begin(), Roots.end());
  261. // The set of required changes.
  262. changeset_ty Required;
  263. // Iterate until the active set of changes is empty. Convergence is guaranteed
  264. // assuming input was a DAG.
  265. //
  266. // Invariant: CurrentSet intersect Required == {}
  267. // Invariant: Required == (Required union succ*(Required))
  268. while (!CurrentSet.empty()) {
  269. LLVM_DEBUG({
  270. llvm::errs() << "DAG_DD - " << CurrentSet.size() << " active changes, "
  271. << Required.size() << " required changes\n";
  272. });
  273. // Minimize the current set of changes.
  274. DeltaActiveSetHelper Helper(*this, Required);
  275. changeset_ty CurrentMinSet = Helper.Run(CurrentSet);
  276. // Update the set of required changes. Since
  277. // CurrentMinSet subset CurrentSet
  278. // and after the last iteration,
  279. // succ(CurrentSet) subset Required
  280. // then
  281. // succ(CurrentMinSet) subset Required
  282. // and our invariant on Required is maintained.
  283. Required.insert(CurrentMinSet.begin(), CurrentMinSet.end());
  284. // Replace the current set with the predecssors of the minimized set of
  285. // active changes.
  286. CurrentSet.clear();
  287. for (change_ty CT : CurrentMinSet)
  288. CurrentSet.insert(pred_begin(CT), pred_end(CT));
  289. // FIXME: We could enforce CurrentSet intersect Required == {} here if we
  290. // wanted to protect against cyclic graphs.
  291. }
  292. return Required;
  293. }
  294. void DAGDeltaAlgorithm::anchor() {
  295. }
  296. DAGDeltaAlgorithm::changeset_ty
  297. DAGDeltaAlgorithm::Run(const changeset_ty &Changes,
  298. const std::vector<edge_ty> &Dependencies) {
  299. return DAGDeltaAlgorithmImpl(*this, Changes, Dependencies).Run();
  300. }