1 //===--- DAGDeltaAlgorithm.cpp - A DAG Minimization Algorithm --*- C++ -*--===//
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 //===----------------------------------------------------------------------===//
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
12 // 1. Minimize the set R using the test predicate:
13 // P'(S) = P(S union pred*(S))
15 // 2. Extend R to R' = R union pred(R).
17 // until a fixed point is reached.
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
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
31 //===----------------------------------------------------------------------===//
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"
42 #define DEBUG_TYPE "dag-delta"
46 class DAGDeltaAlgorithmImpl
{
47 friend class DeltaActiveSetHelper
;
50 typedef DAGDeltaAlgorithm::change_ty change_ty
;
51 typedef DAGDeltaAlgorithm::changeset_ty changeset_ty
;
52 typedef DAGDeltaAlgorithm::changesetlist_ty changesetlist_ty
;
53 typedef DAGDeltaAlgorithm::edge_ty edge_ty
;
56 typedef std::vector
<change_ty
>::iterator pred_iterator_ty
;
57 typedef std::vector
<change_ty
>::iterator succ_iterator_ty
;
58 typedef std::set
<change_ty
>::iterator pred_closure_iterator_ty
;
59 typedef std::set
<change_ty
>::iterator succ_closure_iterator_ty
;
61 DAGDeltaAlgorithm
&DDA
;
63 std::vector
<change_ty
> Roots
;
65 /// Cache of failed test results. Successful test results are never cached
66 /// since we always reduce following a success. We maintain an independent
67 /// cache from that used by the individual delta passes because we may get
68 /// hits across multiple individual delta invocations.
69 mutable std::set
<changeset_ty
> FailedTestsCache
;
72 std::map
<change_ty
, std::vector
<change_ty
> > Predecessors
;
73 std::map
<change_ty
, std::vector
<change_ty
> > Successors
;
75 std::map
<change_ty
, std::set
<change_ty
> > PredClosure
;
76 std::map
<change_ty
, std::set
<change_ty
> > SuccClosure
;
79 pred_iterator_ty
pred_begin(change_ty Node
) {
80 assert(Predecessors
.count(Node
) && "Invalid node!");
81 return Predecessors
[Node
].begin();
83 pred_iterator_ty
pred_end(change_ty Node
) {
84 assert(Predecessors
.count(Node
) && "Invalid node!");
85 return Predecessors
[Node
].end();
88 pred_closure_iterator_ty
pred_closure_begin(change_ty Node
) {
89 assert(PredClosure
.count(Node
) && "Invalid node!");
90 return PredClosure
[Node
].begin();
92 pred_closure_iterator_ty
pred_closure_end(change_ty Node
) {
93 assert(PredClosure
.count(Node
) && "Invalid node!");
94 return PredClosure
[Node
].end();
97 succ_iterator_ty
succ_begin(change_ty Node
) {
98 assert(Successors
.count(Node
) && "Invalid node!");
99 return Successors
[Node
].begin();
101 succ_iterator_ty
succ_end(change_ty Node
) {
102 assert(Successors
.count(Node
) && "Invalid node!");
103 return Successors
[Node
].end();
106 succ_closure_iterator_ty
succ_closure_begin(change_ty Node
) {
107 assert(SuccClosure
.count(Node
) && "Invalid node!");
108 return SuccClosure
[Node
].begin();
110 succ_closure_iterator_ty
succ_closure_end(change_ty Node
) {
111 assert(SuccClosure
.count(Node
) && "Invalid node!");
112 return SuccClosure
[Node
].end();
115 void UpdatedSearchState(const changeset_ty
&Changes
,
116 const changesetlist_ty
&Sets
,
117 const changeset_ty
&Required
) {
118 DDA
.UpdatedSearchState(Changes
, Sets
, Required
);
121 /// ExecuteOneTest - Execute a single test predicate on the change set \p S.
122 bool ExecuteOneTest(const changeset_ty
&S
) {
123 // Check dependencies invariant.
125 for (changeset_ty::const_iterator it
= S
.begin(), ie
= S
.end(); it
!= ie
;
127 for (succ_iterator_ty it2
= succ_begin(*it
), ie2
= succ_end(*it
);
129 assert(S
.count(*it2
) && "Attempt to run invalid changeset!");
132 return DDA
.ExecuteOneTest(S
);
136 DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm
&DDA
, const changeset_ty
&Changes
,
137 const std::vector
<edge_ty
> &Dependencies
);
141 /// GetTestResult - Get the test result for the active set \p Changes with
142 /// \p Required changes from the cache, executing the test if necessary.
144 /// \param Changes - The set of active changes being minimized, which should
145 /// have their pred closure included in the test.
146 /// \param Required - The set of changes which have previously been
147 /// established to be required.
148 /// \return - The test result.
149 bool GetTestResult(const changeset_ty
&Changes
, const changeset_ty
&Required
);
152 /// Helper object for minimizing an active set of changes.
153 class DeltaActiveSetHelper
: public DeltaAlgorithm
{
154 DAGDeltaAlgorithmImpl
&DDAI
;
156 const changeset_ty
&Required
;
159 /// UpdatedSearchState - Callback used when the search state changes.
160 void UpdatedSearchState(const changeset_ty
&Changes
,
161 const changesetlist_ty
&Sets
) override
{
162 DDAI
.UpdatedSearchState(Changes
, Sets
, Required
);
165 bool ExecuteOneTest(const changeset_ty
&S
) override
{
166 return DDAI
.GetTestResult(S
, Required
);
170 DeltaActiveSetHelper(DAGDeltaAlgorithmImpl
&DDAI
,
171 const changeset_ty
&Required
)
172 : DDAI(DDAI
), Required(Required
) {}
177 DAGDeltaAlgorithmImpl::DAGDeltaAlgorithmImpl(
178 DAGDeltaAlgorithm
&DDA
, const changeset_ty
&Changes
,
179 const std::vector
<edge_ty
> &Dependencies
)
181 for (change_ty Change
: Changes
) {
182 Predecessors
.insert(std::make_pair(Change
, std::vector
<change_ty
>()));
183 Successors
.insert(std::make_pair(Change
, std::vector
<change_ty
>()));
185 for (const edge_ty
&Dep
: Dependencies
) {
186 Predecessors
[Dep
.second
].push_back(Dep
.first
);
187 Successors
[Dep
.first
].push_back(Dep
.second
);
190 // Compute the roots.
191 for (change_ty Change
: Changes
)
192 if (succ_begin(Change
) == succ_end(Change
))
193 Roots
.push_back(Change
);
195 // Pre-compute the closure of the successor relation.
196 std::vector
<change_ty
> Worklist(Roots
.begin(), Roots
.end());
197 while (!Worklist
.empty()) {
198 change_ty Change
= Worklist
.back();
201 std::set
<change_ty
> &ChangeSuccs
= SuccClosure
[Change
];
202 for (pred_iterator_ty it
= pred_begin(Change
),
203 ie
= pred_end(Change
); it
!= ie
; ++it
) {
204 SuccClosure
[*it
].insert(Change
);
205 SuccClosure
[*it
].insert(ChangeSuccs
.begin(), ChangeSuccs
.end());
206 Worklist
.push_back(*it
);
210 // Invert to form the predecessor closure map.
211 for (change_ty Change
: Changes
)
212 PredClosure
.insert(std::make_pair(Change
, std::set
<change_ty
>()));
213 for (change_ty Change
: Changes
)
214 for (succ_closure_iterator_ty it2
= succ_closure_begin(Change
),
215 ie2
= succ_closure_end(Change
);
217 PredClosure
[*it2
].insert(Change
);
219 // Dump useful debug info.
221 llvm::errs() << "-- DAGDeltaAlgorithmImpl --\n";
222 llvm::errs() << "Changes: [";
223 for (changeset_ty::const_iterator it
= Changes
.begin(), ie
= Changes
.end();
225 if (it
!= Changes
.begin())
226 llvm::errs() << ", ";
229 if (succ_begin(*it
) != succ_end(*it
)) {
231 for (succ_iterator_ty it2
= succ_begin(*it
), ie2
= succ_end(*it
);
233 if (it2
!= succ_begin(*it
))
234 llvm::errs() << ", ";
235 llvm::errs() << "->" << *it2
;
240 llvm::errs() << "]\n";
242 llvm::errs() << "Roots: [";
243 for (std::vector
<change_ty
>::const_iterator it
= Roots
.begin(),
246 if (it
!= Roots
.begin())
247 llvm::errs() << ", ";
250 llvm::errs() << "]\n";
252 llvm::errs() << "Predecessor Closure:\n";
253 for (change_ty Change
: Changes
) {
254 llvm::errs() << format(" %-4d: [", Change
);
255 for (pred_closure_iterator_ty it2
= pred_closure_begin(Change
),
256 ie2
= pred_closure_end(Change
);
258 if (it2
!= pred_closure_begin(Change
))
259 llvm::errs() << ", ";
260 llvm::errs() << *it2
;
262 llvm::errs() << "]\n";
265 llvm::errs() << "Successor Closure:\n";
266 for (change_ty Change
: Changes
) {
267 llvm::errs() << format(" %-4d: [", Change
);
268 for (succ_closure_iterator_ty it2
= succ_closure_begin(Change
),
269 ie2
= succ_closure_end(Change
);
271 if (it2
!= succ_closure_begin(Change
))
272 llvm::errs() << ", ";
273 llvm::errs() << *it2
;
275 llvm::errs() << "]\n";
278 llvm::errs() << "\n\n";
282 bool DAGDeltaAlgorithmImpl::GetTestResult(const changeset_ty
&Changes
,
283 const changeset_ty
&Required
) {
284 changeset_ty
Extended(Required
);
285 Extended
.insert(Changes
.begin(), Changes
.end());
286 for (change_ty Change
: Changes
)
287 Extended
.insert(pred_closure_begin(Change
), pred_closure_end(Change
));
289 if (FailedTestsCache
.count(Extended
))
292 bool Result
= ExecuteOneTest(Extended
);
294 FailedTestsCache
.insert(Extended
);
299 DAGDeltaAlgorithm::changeset_ty
300 DAGDeltaAlgorithmImpl::Run() {
301 // The current set of changes we are minimizing, starting at the roots.
302 changeset_ty
CurrentSet(Roots
.begin(), Roots
.end());
304 // The set of required changes.
305 changeset_ty Required
;
307 // Iterate until the active set of changes is empty. Convergence is guaranteed
308 // assuming input was a DAG.
310 // Invariant: CurrentSet intersect Required == {}
311 // Invariant: Required == (Required union succ*(Required))
312 while (!CurrentSet
.empty()) {
314 llvm::errs() << "DAG_DD - " << CurrentSet
.size() << " active changes, "
315 << Required
.size() << " required changes\n";
318 // Minimize the current set of changes.
319 DeltaActiveSetHelper
Helper(*this, Required
);
320 changeset_ty CurrentMinSet
= Helper
.Run(CurrentSet
);
322 // Update the set of required changes. Since
323 // CurrentMinSet subset CurrentSet
324 // and after the last iteration,
325 // succ(CurrentSet) subset Required
327 // succ(CurrentMinSet) subset Required
328 // and our invariant on Required is maintained.
329 Required
.insert(CurrentMinSet
.begin(), CurrentMinSet
.end());
331 // Replace the current set with the predecssors of the minimized set of
334 for (change_ty CT
: CurrentMinSet
)
335 CurrentSet
.insert(pred_begin(CT
), pred_end(CT
));
337 // FIXME: We could enforce CurrentSet intersect Required == {} here if we
338 // wanted to protect against cyclic graphs.
344 void DAGDeltaAlgorithm::anchor() {
347 DAGDeltaAlgorithm::changeset_ty
348 DAGDeltaAlgorithm::Run(const changeset_ty
&Changes
,
349 const std::vector
<edge_ty
> &Dependencies
) {
350 return DAGDeltaAlgorithmImpl(*this, Changes
, Dependencies
).Run();