Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/amd/compiler/aco_lower_phis.cpp
4550 views
1
/*
2
* Copyright © 2019 Valve Corporation
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*
23
*/
24
25
#include "aco_builder.h"
26
#include "aco_ir.h"
27
28
#include <algorithm>
29
#include <map>
30
#include <vector>
31
32
namespace aco {
33
34
struct ssa_state {
35
bool checked_preds_for_uniform;
36
bool all_preds_uniform;
37
38
bool needs_init;
39
uint64_t cur_undef_operands;
40
41
unsigned phi_block_idx;
42
unsigned loop_nest_depth;
43
std::map<unsigned, unsigned> writes;
44
/* Whether there's a write in any of a block's predecessors. Indexed by the block index. */
45
std::vector<bool> any_pred_defined;
46
std::vector<Operand> latest;
47
std::vector<bool> visited;
48
};
49
50
Operand
51
get_ssa(Program* program, unsigned block_idx, ssa_state* state, bool before_write)
52
{
53
if (!before_write) {
54
auto it = state->writes.find(block_idx);
55
if (it != state->writes.end())
56
return Operand(Temp(it->second, program->lane_mask));
57
if (state->visited[block_idx])
58
return state->latest[block_idx];
59
}
60
61
state->visited[block_idx] = true;
62
63
Block& block = program->blocks[block_idx];
64
size_t pred = block.linear_preds.size();
65
if (pred == 0 || block.loop_nest_depth < state->loop_nest_depth ||
66
!state->any_pred_defined[block_idx]) {
67
return Operand(program->lane_mask);
68
} else if (block.loop_nest_depth > state->loop_nest_depth) {
69
Operand op = get_ssa(program, block_idx - 1, state, false);
70
state->latest[block_idx] = op;
71
return op;
72
} else if (pred == 1 || block.kind & block_kind_loop_exit) {
73
Operand op = get_ssa(program, block.linear_preds[0], state, false);
74
state->latest[block_idx] = op;
75
return op;
76
} else if (block.kind & block_kind_loop_header &&
77
!(program->blocks[state->phi_block_idx].kind & block_kind_loop_exit)) {
78
return Operand(program->lane_mask);
79
} else {
80
Temp res = Temp(program->allocateTmp(program->lane_mask));
81
state->latest[block_idx] = Operand(res);
82
83
aco_ptr<Pseudo_instruction> phi{
84
create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, pred, 1)};
85
for (unsigned i = 0; i < pred; i++)
86
phi->operands[i] = get_ssa(program, block.linear_preds[i], state, false);
87
phi->definitions[0] = Definition(res);
88
block.instructions.emplace(block.instructions.begin(), std::move(phi));
89
90
return Operand(res);
91
}
92
}
93
94
void
95
insert_before_logical_end(Block* block, aco_ptr<Instruction> instr)
96
{
97
auto IsLogicalEnd = [](const aco_ptr<Instruction>& inst) -> bool
98
{ return inst->opcode == aco_opcode::p_logical_end; };
99
auto it = std::find_if(block->instructions.crbegin(), block->instructions.crend(), IsLogicalEnd);
100
101
if (it == block->instructions.crend()) {
102
assert(block->instructions.back()->isBranch());
103
block->instructions.insert(std::prev(block->instructions.end()), std::move(instr));
104
} else {
105
block->instructions.insert(std::prev(it.base()), std::move(instr));
106
}
107
}
108
109
void
110
build_merge_code(Program* program, Block* block, Definition dst, Operand prev, Operand cur)
111
{
112
Builder bld(program);
113
114
auto IsLogicalEnd = [](const aco_ptr<Instruction>& instr) -> bool
115
{ return instr->opcode == aco_opcode::p_logical_end; };
116
auto it = std::find_if(block->instructions.rbegin(), block->instructions.rend(), IsLogicalEnd);
117
assert(it != block->instructions.rend());
118
bld.reset(&block->instructions, std::prev(it.base()));
119
120
if (prev.isUndefined()) {
121
bld.copy(dst, cur);
122
return;
123
}
124
125
bool prev_is_constant = prev.isConstant() && prev.constantValue() + 1u < 2u;
126
bool cur_is_constant = cur.isConstant() && cur.constantValue() + 1u < 2u;
127
128
if (!prev_is_constant) {
129
if (!cur_is_constant) {
130
Temp tmp1 = bld.tmp(bld.lm), tmp2 = bld.tmp(bld.lm);
131
bld.sop2(Builder::s_andn2, Definition(tmp1), bld.def(s1, scc), prev,
132
Operand(exec, bld.lm));
133
bld.sop2(Builder::s_and, Definition(tmp2), bld.def(s1, scc), cur, Operand(exec, bld.lm));
134
bld.sop2(Builder::s_or, dst, bld.def(s1, scc), tmp1, tmp2);
135
} else if (cur.constantValue()) {
136
bld.sop2(Builder::s_or, dst, bld.def(s1, scc), prev, Operand(exec, bld.lm));
137
} else {
138
bld.sop2(Builder::s_andn2, dst, bld.def(s1, scc), prev, Operand(exec, bld.lm));
139
}
140
} else if (prev.constantValue()) {
141
if (!cur_is_constant)
142
bld.sop2(Builder::s_orn2, dst, bld.def(s1, scc), cur, Operand(exec, bld.lm));
143
else if (cur.constantValue())
144
bld.copy(dst, Operand::c32_or_c64(UINT32_MAX, bld.lm == s2));
145
else
146
bld.sop1(Builder::s_not, dst, bld.def(s1, scc), Operand(exec, bld.lm));
147
} else {
148
if (!cur_is_constant)
149
bld.sop2(Builder::s_and, dst, bld.def(s1, scc), cur, Operand(exec, bld.lm));
150
else if (cur.constantValue())
151
bld.copy(dst, Operand(exec, bld.lm));
152
else
153
bld.copy(dst, Operand::zero(bld.lm.bytes()));
154
}
155
}
156
157
void
158
init_any_pred_defined(Program* program, ssa_state* state, Block* block, aco_ptr<Instruction>& phi)
159
{
160
std::fill(state->any_pred_defined.begin(), state->any_pred_defined.end(), false);
161
for (unsigned i = 0; i < block->logical_preds.size(); i++) {
162
if (phi->operands[i].isUndefined())
163
continue;
164
for (unsigned succ : program->blocks[block->logical_preds[i]].linear_succs)
165
state->any_pred_defined[succ] = true;
166
}
167
168
unsigned start = block->logical_preds[0];
169
170
/* for loop exit phis, start at the loop header */
171
const bool loop_exit = block->kind & block_kind_loop_exit;
172
while (loop_exit && program->blocks[start - 1].loop_nest_depth >= state->loop_nest_depth)
173
start--;
174
175
for (unsigned i = 0; i < 1u + loop_exit; i++) {
176
for (unsigned j = start; j < block->index; j++) {
177
if (!state->any_pred_defined[j])
178
continue;
179
for (unsigned succ : program->blocks[j].linear_succs)
180
state->any_pred_defined[succ] = true;
181
}
182
}
183
}
184
185
void
186
lower_divergent_bool_phi(Program* program, ssa_state* state, Block* block,
187
aco_ptr<Instruction>& phi)
188
{
189
Builder bld(program);
190
191
if (!state->checked_preds_for_uniform) {
192
state->all_preds_uniform = !(block->kind & block_kind_merge) &&
193
block->linear_preds.size() == block->logical_preds.size();
194
for (unsigned pred : block->logical_preds)
195
state->all_preds_uniform =
196
state->all_preds_uniform && (program->blocks[pred].kind & block_kind_uniform);
197
state->checked_preds_for_uniform = true;
198
}
199
200
if (state->all_preds_uniform) {
201
phi->opcode = aco_opcode::p_linear_phi;
202
return;
203
}
204
205
state->latest.resize(program->blocks.size());
206
state->visited.resize(program->blocks.size());
207
state->any_pred_defined.resize(program->blocks.size());
208
209
uint64_t undef_operands = 0;
210
for (unsigned i = 0; i < phi->operands.size(); i++)
211
undef_operands |= (uint64_t)phi->operands[i].isUndefined() << i;
212
213
if (state->needs_init || undef_operands != state->cur_undef_operands ||
214
block->logical_preds.size() > 64) {
215
/* this only has to be done once per block unless the set of predecessors
216
* which are undefined changes */
217
state->cur_undef_operands = undef_operands;
218
state->phi_block_idx = block->index;
219
state->loop_nest_depth = block->loop_nest_depth;
220
if (block->kind & block_kind_loop_exit) {
221
state->loop_nest_depth += 1;
222
}
223
state->writes.clear();
224
init_any_pred_defined(program, state, block, phi);
225
state->needs_init = false;
226
}
227
std::fill(state->latest.begin(), state->latest.end(), Operand(program->lane_mask));
228
std::fill(state->visited.begin(), state->visited.end(), false);
229
230
for (unsigned i = 0; i < phi->operands.size(); i++) {
231
if (phi->operands[i].isUndefined())
232
continue;
233
234
state->writes[block->logical_preds[i]] = program->allocateId(program->lane_mask);
235
}
236
237
bool uniform_merge = block->kind & block_kind_loop_header;
238
239
for (unsigned i = 0; i < phi->operands.size(); i++) {
240
Block* pred = &program->blocks[block->logical_preds[i]];
241
242
bool need_get_ssa = !uniform_merge;
243
if (block->kind & block_kind_loop_header && !(pred->kind & block_kind_uniform))
244
uniform_merge = false;
245
246
if (phi->operands[i].isUndefined())
247
continue;
248
249
Operand cur(bld.lm);
250
if (need_get_ssa)
251
cur = get_ssa(program, pred->index, state, true);
252
assert(cur.regClass() == bld.lm);
253
254
Temp new_cur = {state->writes.at(pred->index), program->lane_mask};
255
assert(new_cur.regClass() == bld.lm);
256
257
if (i == 1 && (block->kind & block_kind_merge) && phi->operands[0].isConstant())
258
cur = phi->operands[0];
259
build_merge_code(program, pred, Definition(new_cur), cur, phi->operands[i]);
260
}
261
262
unsigned num_preds = block->linear_preds.size();
263
if (phi->operands.size() != num_preds) {
264
Pseudo_instruction* new_phi{create_instruction<Pseudo_instruction>(
265
aco_opcode::p_linear_phi, Format::PSEUDO, num_preds, 1)};
266
new_phi->definitions[0] = phi->definitions[0];
267
phi.reset(new_phi);
268
} else {
269
phi->opcode = aco_opcode::p_linear_phi;
270
}
271
assert(phi->operands.size() == num_preds);
272
273
for (unsigned i = 0; i < num_preds; i++)
274
phi->operands[i] = get_ssa(program, block->linear_preds[i], state, false);
275
276
return;
277
}
278
279
void
280
lower_subdword_phis(Program* program, Block* block, aco_ptr<Instruction>& phi)
281
{
282
Builder bld(program);
283
for (unsigned i = 0; i < phi->operands.size(); i++) {
284
if (phi->operands[i].isUndefined())
285
continue;
286
if (phi->operands[i].regClass() == phi->definitions[0].regClass())
287
continue;
288
289
assert(phi->operands[i].isTemp());
290
Block* pred = &program->blocks[block->logical_preds[i]];
291
Temp phi_src = phi->operands[i].getTemp();
292
293
assert(phi_src.regClass().type() == RegType::sgpr);
294
Temp tmp = bld.tmp(RegClass(RegType::vgpr, phi_src.size()));
295
insert_before_logical_end(pred, bld.copy(Definition(tmp), phi_src).get_ptr());
296
Temp new_phi_src = bld.tmp(phi->definitions[0].regClass());
297
insert_before_logical_end(pred, bld.pseudo(aco_opcode::p_extract_vector,
298
Definition(new_phi_src), tmp, Operand::zero())
299
.get_ptr());
300
301
phi->operands[i].setTemp(new_phi_src);
302
}
303
return;
304
}
305
306
void
307
lower_phis(Program* program)
308
{
309
ssa_state state;
310
311
for (Block& block : program->blocks) {
312
state.checked_preds_for_uniform = false;
313
state.needs_init = true;
314
for (aco_ptr<Instruction>& phi : block.instructions) {
315
if (phi->opcode == aco_opcode::p_phi) {
316
assert(program->wave_size == 64 ? phi->definitions[0].regClass() != s1
317
: phi->definitions[0].regClass() != s2);
318
if (phi->definitions[0].regClass() == program->lane_mask)
319
lower_divergent_bool_phi(program, &state, &block, phi);
320
else if (phi->definitions[0].regClass().is_subdword())
321
lower_subdword_phis(program, &block, phi);
322
} else if (!is_phi(phi)) {
323
break;
324
}
325
}
326
}
327
}
328
329
} // namespace aco
330
331