2
0

DAGDeltaAlgorithm.cpp 12 KB

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