/src/llvm-project/llvm/lib/CodeGen/LatencyPriorityQueue.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===---- LatencyPriorityQueue.cpp - A latency-oriented priority queue ----===// |
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 the LatencyPriorityQueue class, which is a |
10 | | // SchedulingPriorityQueue that schedules using latency information to |
11 | | // reduce the length of the critical path through the basic block. |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
16 | | #include "llvm/Config/llvm-config.h" |
17 | | #include "llvm/Support/Debug.h" |
18 | | #include "llvm/Support/raw_ostream.h" |
19 | | using namespace llvm; |
20 | | |
21 | | #define DEBUG_TYPE "scheduler" |
22 | | |
23 | 5.14M | bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const { |
24 | | // The isScheduleHigh flag allows nodes with wraparound dependencies that |
25 | | // cannot easily be modeled as edges with latencies to be scheduled as |
26 | | // soon as possible in a top-down schedule. |
27 | 5.14M | if (LHS->isScheduleHigh && !RHS->isScheduleHigh) |
28 | 0 | return false; |
29 | 5.14M | if (!LHS->isScheduleHigh && RHS->isScheduleHigh) |
30 | 0 | return true; |
31 | | |
32 | 5.14M | unsigned LHSNum = LHS->NodeNum; |
33 | 5.14M | unsigned RHSNum = RHS->NodeNum; |
34 | | |
35 | | // The most important heuristic is scheduling the critical path. |
36 | 5.14M | unsigned LHSLatency = PQ->getLatency(LHSNum); |
37 | 5.14M | unsigned RHSLatency = PQ->getLatency(RHSNum); |
38 | 5.14M | if (LHSLatency < RHSLatency) return true; |
39 | 4.28M | if (LHSLatency > RHSLatency) return false; |
40 | | |
41 | | // After that, if two nodes have identical latencies, look to see if one will |
42 | | // unblock more other nodes than the other. |
43 | 1.83M | unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum); |
44 | 1.83M | unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum); |
45 | 1.83M | if (LHSBlocked < RHSBlocked) return true; |
46 | 1.63M | if (LHSBlocked > RHSBlocked) return false; |
47 | | |
48 | | // Finally, just to provide a stable ordering, use the node number as a |
49 | | // deciding factor. |
50 | 1.19M | return RHSNum < LHSNum; |
51 | 1.63M | } |
52 | | |
53 | | |
54 | | /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor |
55 | | /// of SU, return it, otherwise return null. |
56 | 5.17M | SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) { |
57 | 5.17M | SUnit *OnlyAvailablePred = nullptr; |
58 | 12.6M | for (const SDep &P : SU->Preds) { |
59 | 12.6M | SUnit &Pred = *P.getSUnit(); |
60 | 12.6M | if (!Pred.isScheduled) { |
61 | | // We found an available, but not scheduled, predecessor. If it's the |
62 | | // only one we have found, keep track of it... otherwise give up. |
63 | 7.30M | if (OnlyAvailablePred && OnlyAvailablePred != &Pred) |
64 | 2.46M | return nullptr; |
65 | 4.84M | OnlyAvailablePred = &Pred; |
66 | 4.84M | } |
67 | 12.6M | } |
68 | | |
69 | 2.71M | return OnlyAvailablePred; |
70 | 5.17M | } |
71 | | |
72 | 1.91M | void LatencyPriorityQueue::push(SUnit *SU) { |
73 | | // Look at all of the successors of this node. Count the number of nodes that |
74 | | // this node is the sole unscheduled node for. |
75 | 1.91M | unsigned NumNodesBlocking = 0; |
76 | 1.91M | for (const SDep &Succ : SU->Succs) |
77 | 3.32M | if (getSingleUnscheduledPred(Succ.getSUnit()) == SU) |
78 | 1.29M | ++NumNodesBlocking; |
79 | 1.91M | NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking; |
80 | | |
81 | 1.91M | Queue.push_back(SU); |
82 | 1.91M | } |
83 | | |
84 | | |
85 | | // scheduledNode - As nodes are scheduled, we look to see if there are any |
86 | | // successor nodes that have a single unscheduled predecessor. If so, that |
87 | | // single predecessor has a higher priority, since scheduling it will make |
88 | | // the node available. |
89 | 896k | void LatencyPriorityQueue::scheduledNode(SUnit *SU) { |
90 | 896k | for (const SDep &Succ : SU->Succs) |
91 | 1.85M | AdjustPriorityOfUnscheduledPreds(Succ.getSUnit()); |
92 | 896k | } |
93 | | |
94 | | /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just |
95 | | /// scheduled. If SU is not itself available, then there is at least one |
96 | | /// predecessor node that has not been scheduled yet. If SU has exactly ONE |
97 | | /// unscheduled predecessor, we want to increase its priority: it getting |
98 | | /// scheduled will make this node available, so it is better than some other |
99 | | /// node of the same priority that will not make a node available. |
100 | 1.85M | void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) { |
101 | 1.85M | if (SU->isAvailable) return; // All preds scheduled. |
102 | | |
103 | 1.85M | SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU); |
104 | 1.85M | if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) return; |
105 | | |
106 | | // Okay, we found a single predecessor that is available, but not scheduled. |
107 | | // Since it is available, it must be in the priority queue. First remove it. |
108 | 110k | remove(OnlyAvailablePred); |
109 | | |
110 | | // Reinsert the node into the priority queue, which recomputes its |
111 | | // NumNodesSolelyBlocking value. |
112 | 110k | push(OnlyAvailablePred); |
113 | 110k | } |
114 | | |
115 | 1.80M | SUnit *LatencyPriorityQueue::pop() { |
116 | 1.80M | if (empty()) return nullptr; |
117 | 1.80M | std::vector<SUnit *>::iterator Best = Queue.begin(); |
118 | 1.80M | for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()), |
119 | 6.94M | E = Queue.end(); I != E; ++I) |
120 | 5.14M | if (Picker(*Best, *I)) |
121 | 1.54M | Best = I; |
122 | 1.80M | SUnit *V = *Best; |
123 | 1.80M | if (Best != std::prev(Queue.end())) |
124 | 835k | std::swap(*Best, Queue.back()); |
125 | 1.80M | Queue.pop_back(); |
126 | 1.80M | return V; |
127 | 1.80M | } |
128 | | |
129 | 110k | void LatencyPriorityQueue::remove(SUnit *SU) { |
130 | 110k | assert(!Queue.empty() && "Queue is empty!"); |
131 | 0 | std::vector<SUnit *>::iterator I = find(Queue, SU); |
132 | 110k | assert(I != Queue.end() && "Queue doesn't contain the SU being removed!"); |
133 | 110k | if (I != std::prev(Queue.end())) |
134 | 61.2k | std::swap(*I, Queue.back()); |
135 | 110k | Queue.pop_back(); |
136 | 110k | } |
137 | | |
138 | | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
139 | 0 | LLVM_DUMP_METHOD void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const { |
140 | 0 | dbgs() << "Latency Priority Queue\n"; |
141 | 0 | dbgs() << " Number of Queue Entries: " << Queue.size() << "\n"; |
142 | 0 | for (const SUnit *SU : Queue) { |
143 | 0 | dbgs() << " "; |
144 | 0 | DAG->dumpNode(*SU); |
145 | 0 | } |
146 | 0 | } |
147 | | #endif |