1 /* -*- mesa-c++ -*-
2 *
3 * Copyright (c) 2022 Collabora LTD
4 *
5 * Author: Gert Wollny <gert.wollny@collabora.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * on the rights to use, copy, modify, merge, publish, distribute, sub
11 * license, and/or sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "sfn_debug.h"
28 #include "sfn_ra.h"
29
30 #include <cassert>
31 #include <queue>
32
33 namespace r600 {
34
prepare_row(int row)35 void ComponentInterference::prepare_row(int row)
36 {
37 m_rows.resize(row + 1);
38
39 }
40
add(size_t idx1,size_t idx2)41 void ComponentInterference::add(size_t idx1, size_t idx2)
42 {
43 assert(idx1 > idx2);
44 assert(m_rows.size() > idx1);
45 m_rows[idx1].push_back(idx2);
46 m_rows[idx2].push_back(idx1);
47 }
48
49
Interference(LiveRangeMap & map)50 Interference::Interference(LiveRangeMap& map):
51 m_map(map)
52 {
53 initialize();
54 }
55
initialize()56 void Interference::initialize()
57 {
58 for(int i = 0; i < 4; ++i) {
59 initialize(m_components_maps[i], m_map.component(i));
60 }
61 }
62
initialize(ComponentInterference & comp_interference,LiveRangeMap::ChannelLiveRange & clr)63 void Interference::initialize(ComponentInterference& comp_interference,
64 LiveRangeMap::ChannelLiveRange& clr)
65 {
66 for (size_t row = 0; row < clr.size(); ++row) {
67 auto& row_entry = clr[row];
68 comp_interference.prepare_row(row);
69 for (size_t col = 0; col < row; ++col) {
70 auto& col_entry = clr[col];
71 if (row_entry.m_end >= col_entry.m_start &&
72 row_entry.m_start <= col_entry.m_end)
73 comp_interference.add(row, col);
74 }
75 }
76 }
77
78 struct Group {
79 int priority;
80 std::array<PRegister, 4> channels;
81 };
82
operator <(const Group & lhs,const Group & rhs)83 static inline bool operator < (const Group& lhs, const Group& rhs)
84 {
85 return lhs.priority < rhs.priority;
86 }
87
88 using GroupRegisters = std::priority_queue<Group>;
89
90 static bool
group_allocation(LiveRangeMap & lrm,const Interference & interference,GroupRegisters & groups)91 group_allocation (LiveRangeMap& lrm, const Interference& interference, GroupRegisters& groups)
92 {
93 int color = 0;
94 // allocate grouped registers
95 while (!groups.empty()) {
96 auto group = groups.top();
97 groups.pop();
98
99 int start_comp = 0;
100 while (!group.channels[start_comp])
101 ++start_comp;
102
103 sfn_log << SfnLog::merge << "Color group with " << *group.channels[start_comp] << "\n";
104
105 // don't restart registers for exports, we may be able tp merge the
106 // export calls, is fthe registers are consecutive
107 if (group.priority > 0)
108 color = 0;
109
110 while (color < 124) {
111 /* Find the coloring for the first channel */
112 bool color_in_use = false;
113 int comp = start_comp;
114
115 auto& adjecency = interference.row(start_comp, group.channels[comp]->index());
116 auto& regs = lrm.component(comp);
117
118 sfn_log << SfnLog::merge << "Try color "<< color;
119
120 for (auto adj : adjecency) {
121 if (regs[adj].m_color == color) {
122 color_in_use = true;
123 sfn_log << SfnLog::merge << " in use\n";
124 break;
125 }
126 }
127
128 if (color_in_use) {
129 ++color;
130 continue;
131 }
132
133 /* First channel color found, check whether it can be used for all channels */
134 while (comp < 4) {
135 sfn_log << SfnLog::merge << " interference: ";
136 if (group.channels[comp]) {
137 auto& component_life_ranges = lrm.component(comp);
138 auto& adjecencies = interference.row(comp, group.channels[comp]->index());
139
140 for (auto adj_index : adjecencies) {
141 sfn_log << SfnLog::merge << *component_life_ranges[adj_index].m_register << " ";
142 if (component_life_ranges[adj_index].m_color == color) {
143 color_in_use = true;
144 sfn_log << SfnLog::merge << "used";
145 break;
146 }
147 }
148
149 if (color_in_use)
150 break;
151 }
152 ++comp;
153 }
154
155 /* We couldn't allocate all channels with this color, so try next */
156 if (color_in_use) {
157 ++color;
158 sfn_log << SfnLog::merge << "\n";
159 continue;
160 }
161 sfn_log << SfnLog::merge << " success\n";
162
163 /* Coloring successful */
164 for (auto reg : group.channels) {
165 if (reg) {
166 auto& vregs = lrm.component(reg->chan());
167 auto& vreg_cmp = vregs[reg->index()];
168 assert(vreg_cmp.m_start != -1 || vreg_cmp.m_end != -1);
169 vreg_cmp.m_color = color;
170 }
171 }
172 break;
173 }
174
175 if (color == 124)
176 return false;
177 }
178
179 return true;
180 }
181
182 static bool
scalar_allocation(LiveRangeMap & lrm,const Interference & interference)183 scalar_allocation (LiveRangeMap& lrm, const Interference& interference)
184 {
185 for (int comp = 0; comp < 4; ++comp) {
186 auto& live_ranges = lrm.component(comp);
187 for (auto& r : live_ranges) {
188 if (r.m_color != -1)
189 continue;
190
191 if (r.m_start == -1 &&
192 r.m_end == -1)
193 continue;
194
195 sfn_log << SfnLog::merge << "Color " << *r.m_register << "\n";
196
197 auto& adjecency = interference.row(comp, r.m_register->index());
198
199 int color = 0;
200
201 while (color < 124) {
202 bool color_in_use = false;
203 for (auto adj : adjecency) {
204 if (live_ranges[adj].m_color == color) {
205 color_in_use = true;
206 break;
207 }
208 }
209
210 if (color_in_use) {
211 ++color;
212 continue;
213 }
214
215 r.m_color = color;
216 break;
217 }
218 if (color == 124)
219 return false;
220 }
221 }
222 return true;
223 }
224
register_allocation(LiveRangeMap & lrm)225 bool register_allocation(LiveRangeMap& lrm)
226 {
227 Interference interference(lrm);
228
229 std::map<int, Group> groups;
230
231 // setup fixed colors and group relationships
232 for (int i = 0; i < 4; ++i) {
233 auto& comp = lrm.component(i);
234 for (auto& entry : comp) {
235 sfn_log << SfnLog::merge << "Prepare RA for "
236 << *entry.m_register
237 << " [" << entry.m_start << ", " << entry.m_end << "]\n";
238 auto pin = entry.m_register->pin();
239 if (entry.m_start == -1 && entry.m_end == -1) {
240 if (pin == pin_group || pin == pin_chgr)
241 entry.m_register->set_chan(7);
242 continue;
243 }
244
245 auto sel = entry.m_register->sel();
246 /* fully pinned registers contain system values with the
247 * definite register index, and array values are allocated
248 * right after the system registers, so just reuse the IDs (for now) */
249 if (pin == pin_fully || pin == pin_array) {
250 /* Must set all array element entries */
251 sfn_log << SfnLog::merge << "Pin color " << sel << " to " << *entry.m_register << "\n";
252 entry.m_color = sel;
253 } else if (pin == pin_group || pin == pin_chgr) {
254 /* Groups must all have the same sel() value, because they are used
255 * as vec4 registers */
256 auto igroup = groups.find(sel);
257 if (igroup != groups.end()) {
258 igroup->second.channels[i] = entry.m_register;
259 assert(comp[entry.m_register->index()].m_register->index() == entry.m_register->index());
260 } else {
261 int priority = entry.m_use.test(LiveRangeEntry::use_export) ? - entry.m_end : entry.m_start;
262 Group group{priority, {nullptr, nullptr, nullptr, nullptr}};
263 group.channels[i] = entry.m_register;
264 assert(comp[group.channels[i]->index()].m_register->index() == entry.m_register->index());
265 groups[sel] = group;
266 }
267 }
268 }
269 }
270
271 GroupRegisters groups_sorted;
272 for (auto& [sel, group] : groups)
273 groups_sorted.push(group);
274
275 if (!group_allocation (lrm, interference, groups_sorted))
276 return false;
277
278 if (!scalar_allocation(lrm, interference))
279 return false;
280
281 for (int i = 0; i < 4; ++i) {
282 auto& comp = lrm.component(i);
283 for (auto& entry : comp) {
284 sfn_log << SfnLog::merge << "Set " << *entry.m_register << " to ";
285 entry.m_register->set_sel(entry.m_color);
286 entry.m_register->set_pin(pin_none);
287 sfn_log << SfnLog::merge << *entry.m_register << "\n";
288 }
289 }
290
291 return true;
292 }
293
294 }
295