Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/amd/compiler/aco_optimizer_postRA.cpp
4550 views
1
/*
2
* Copyright © 2021 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_ir.h"
26
27
#include <algorithm>
28
#include <array>
29
#include <bitset>
30
#include <vector>
31
32
namespace aco {
33
namespace {
34
35
constexpr const size_t max_reg_cnt = 512;
36
37
enum {
38
not_written_in_block = -1,
39
clobbered = -2,
40
const_or_undef = -3,
41
written_by_multiple_instrs = -4,
42
};
43
44
struct pr_opt_ctx {
45
Program* program;
46
Block* current_block;
47
int current_instr_idx;
48
std::vector<uint16_t> uses;
49
std::array<int, max_reg_cnt * 4u> instr_idx_by_regs;
50
51
void reset_block(Block* block)
52
{
53
current_block = block;
54
current_instr_idx = -1;
55
std::fill(instr_idx_by_regs.begin(), instr_idx_by_regs.end(), not_written_in_block);
56
}
57
};
58
59
void
60
save_reg_writes(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
61
{
62
for (const Definition& def : instr->definitions) {
63
assert(def.regClass().type() != RegType::sgpr || def.physReg().reg() <= 255);
64
assert(def.regClass().type() != RegType::vgpr || def.physReg().reg() >= 256);
65
66
unsigned dw_size = DIV_ROUND_UP(def.bytes(), 4u);
67
unsigned r = def.physReg().reg();
68
int idx = ctx.current_instr_idx;
69
70
if (def.regClass().is_subdword())
71
idx = clobbered;
72
73
assert(def.size() == dw_size || def.regClass().is_subdword());
74
std::fill(&ctx.instr_idx_by_regs[r], &ctx.instr_idx_by_regs[r + dw_size], idx);
75
}
76
}
77
78
int
79
last_writer_idx(pr_opt_ctx& ctx, PhysReg physReg, RegClass rc)
80
{
81
/* Verify that all of the operand's registers are written by the same instruction. */
82
int instr_idx = ctx.instr_idx_by_regs[physReg.reg()];
83
unsigned dw_size = DIV_ROUND_UP(rc.bytes(), 4u);
84
unsigned r = physReg.reg();
85
bool all_same = std::all_of(&ctx.instr_idx_by_regs[r], &ctx.instr_idx_by_regs[r + dw_size],
86
[instr_idx](int i) { return i == instr_idx; });
87
88
return all_same ? instr_idx : written_by_multiple_instrs;
89
}
90
91
int
92
last_writer_idx(pr_opt_ctx& ctx, const Operand& op)
93
{
94
if (op.isConstant() || op.isUndefined())
95
return const_or_undef;
96
97
int instr_idx = ctx.instr_idx_by_regs[op.physReg().reg()];
98
99
#ifndef NDEBUG
100
/* Debug mode: */
101
instr_idx = last_writer_idx(ctx, op.physReg(), op.regClass());
102
assert(instr_idx != written_by_multiple_instrs);
103
#endif
104
105
return instr_idx;
106
}
107
108
void
109
try_apply_branch_vcc(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
110
{
111
/* We are looking for the following pattern:
112
*
113
* vcc = ... ; last_vcc_wr
114
* sX, scc = s_and_bXX vcc, exec ; op0_instr
115
* (...vcc and exec must not be clobbered inbetween...)
116
* s_cbranch_XX scc ; instr
117
*
118
* If possible, the above is optimized into:
119
*
120
* vcc = ... ; last_vcc_wr
121
* s_cbranch_XX vcc ; instr modified to use vcc
122
*/
123
124
/* Don't try to optimize this on GFX6-7 because SMEM may corrupt the vccz bit. */
125
if (ctx.program->chip_class < GFX8)
126
return;
127
128
if (instr->format != Format::PSEUDO_BRANCH || instr->operands.size() == 0 ||
129
instr->operands[0].physReg() != scc)
130
return;
131
132
int op0_instr_idx = last_writer_idx(ctx, instr->operands[0]);
133
int last_vcc_wr_idx = last_writer_idx(ctx, vcc, ctx.program->lane_mask);
134
int last_exec_wr_idx = last_writer_idx(ctx, exec, ctx.program->lane_mask);
135
136
/* We need to make sure:
137
* - the operand register used by the branch, and VCC were both written in the current block
138
* - VCC was NOT written after the operand register
139
* - EXEC is sane and was NOT written after the operand register
140
*/
141
if (op0_instr_idx < 0 || last_vcc_wr_idx < 0 || last_vcc_wr_idx > op0_instr_idx ||
142
last_exec_wr_idx > last_vcc_wr_idx || last_exec_wr_idx < not_written_in_block)
143
return;
144
145
aco_ptr<Instruction>& op0_instr = ctx.current_block->instructions[op0_instr_idx];
146
aco_ptr<Instruction>& last_vcc_wr = ctx.current_block->instructions[last_vcc_wr_idx];
147
148
if ((op0_instr->opcode != aco_opcode::s_and_b64 /* wave64 */ &&
149
op0_instr->opcode != aco_opcode::s_and_b32 /* wave32 */) ||
150
op0_instr->operands[0].physReg() != vcc || op0_instr->operands[1].physReg() != exec ||
151
!last_vcc_wr->isVOPC())
152
return;
153
154
assert(last_vcc_wr->definitions[0].tempId() == op0_instr->operands[0].tempId());
155
156
/* Reduce the uses of the SCC def */
157
ctx.uses[instr->operands[0].tempId()]--;
158
/* Use VCC instead of SCC in the branch */
159
instr->operands[0] = op0_instr->operands[0];
160
}
161
162
void
163
try_optimize_scc_nocompare(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
164
{
165
/* We are looking for the following pattern:
166
*
167
* s_bfe_u32 s0, s3, 0x40018 ; outputs SGPR and SCC if the SGPR != 0
168
* s_cmp_eq_i32 s0, 0 ; comparison between the SGPR and 0
169
* s_cbranch_scc0 BB3 ; use the result of the comparison, eg. branch or cselect
170
*
171
* If possible, the above is optimized into:
172
*
173
* s_bfe_u32 s0, s3, 0x40018 ; original instruction
174
* s_cbranch_scc1 BB3 ; modified to use SCC directly rather than the SGPR with comparison
175
*
176
*/
177
178
if (!instr->isSALU() && !instr->isBranch())
179
return;
180
181
if (instr->isSOPC() &&
182
(instr->opcode == aco_opcode::s_cmp_eq_u32 || instr->opcode == aco_opcode::s_cmp_eq_i32 ||
183
instr->opcode == aco_opcode::s_cmp_lg_u32 || instr->opcode == aco_opcode::s_cmp_lg_i32 ||
184
instr->opcode == aco_opcode::s_cmp_eq_u64 || instr->opcode == aco_opcode::s_cmp_lg_u64) &&
185
(instr->operands[0].constantEquals(0) || instr->operands[1].constantEquals(0)) &&
186
(instr->operands[0].isTemp() || instr->operands[1].isTemp())) {
187
/* Make sure the constant is always in operand 1 */
188
if (instr->operands[0].isConstant())
189
std::swap(instr->operands[0], instr->operands[1]);
190
191
if (ctx.uses[instr->operands[0].tempId()] > 1)
192
return;
193
194
/* Make sure both SCC and Operand 0 are written by the same instruction. */
195
int wr_idx = last_writer_idx(ctx, instr->operands[0]);
196
int sccwr_idx = last_writer_idx(ctx, scc, s1);
197
if (wr_idx < 0 || wr_idx != sccwr_idx)
198
return;
199
200
aco_ptr<Instruction>& wr_instr = ctx.current_block->instructions[wr_idx];
201
if (!wr_instr->isSALU() || wr_instr->definitions.size() < 2 ||
202
wr_instr->definitions[1].physReg() != scc)
203
return;
204
205
/* Look for instructions which set SCC := (D != 0) */
206
switch (wr_instr->opcode) {
207
case aco_opcode::s_bfe_i32:
208
case aco_opcode::s_bfe_i64:
209
case aco_opcode::s_bfe_u32:
210
case aco_opcode::s_bfe_u64:
211
case aco_opcode::s_and_b32:
212
case aco_opcode::s_and_b64:
213
case aco_opcode::s_andn2_b32:
214
case aco_opcode::s_andn2_b64:
215
case aco_opcode::s_or_b32:
216
case aco_opcode::s_or_b64:
217
case aco_opcode::s_orn2_b32:
218
case aco_opcode::s_orn2_b64:
219
case aco_opcode::s_xor_b32:
220
case aco_opcode::s_xor_b64:
221
case aco_opcode::s_not_b32:
222
case aco_opcode::s_not_b64:
223
case aco_opcode::s_nor_b32:
224
case aco_opcode::s_nor_b64:
225
case aco_opcode::s_xnor_b32:
226
case aco_opcode::s_xnor_b64:
227
case aco_opcode::s_nand_b32:
228
case aco_opcode::s_nand_b64:
229
case aco_opcode::s_lshl_b32:
230
case aco_opcode::s_lshl_b64:
231
case aco_opcode::s_lshr_b32:
232
case aco_opcode::s_lshr_b64:
233
case aco_opcode::s_ashr_i32:
234
case aco_opcode::s_ashr_i64:
235
case aco_opcode::s_abs_i32:
236
case aco_opcode::s_absdiff_i32: break;
237
default: return;
238
}
239
240
/* Use the SCC def from wr_instr */
241
ctx.uses[instr->operands[0].tempId()]--;
242
instr->operands[0] = Operand(wr_instr->definitions[1].getTemp(), scc);
243
ctx.uses[instr->operands[0].tempId()]++;
244
245
/* Set the opcode and operand to 32-bit */
246
instr->operands[1] = Operand::zero();
247
instr->opcode =
248
(instr->opcode == aco_opcode::s_cmp_eq_u32 || instr->opcode == aco_opcode::s_cmp_eq_i32 ||
249
instr->opcode == aco_opcode::s_cmp_eq_u64)
250
? aco_opcode::s_cmp_eq_u32
251
: aco_opcode::s_cmp_lg_u32;
252
} else if ((instr->format == Format::PSEUDO_BRANCH && instr->operands.size() == 1 &&
253
instr->operands[0].physReg() == scc) ||
254
instr->opcode == aco_opcode::s_cselect_b32) {
255
256
/* For cselect, operand 2 is the SCC condition */
257
unsigned scc_op_idx = 0;
258
if (instr->opcode == aco_opcode::s_cselect_b32) {
259
scc_op_idx = 2;
260
}
261
262
int wr_idx = last_writer_idx(ctx, instr->operands[scc_op_idx]);
263
if (wr_idx < 0)
264
return;
265
266
aco_ptr<Instruction>& wr_instr = ctx.current_block->instructions[wr_idx];
267
268
/* Check if we found the pattern above. */
269
if (wr_instr->opcode != aco_opcode::s_cmp_eq_u32 &&
270
wr_instr->opcode != aco_opcode::s_cmp_lg_u32)
271
return;
272
if (wr_instr->operands[0].physReg() != scc)
273
return;
274
if (!wr_instr->operands[1].constantEquals(0))
275
return;
276
277
/* The optimization can be unsafe when there are other users. */
278
if (ctx.uses[instr->operands[scc_op_idx].tempId()] > 1)
279
return;
280
281
if (wr_instr->opcode == aco_opcode::s_cmp_eq_u32) {
282
/* Flip the meaning of the instruction to correctly use the SCC. */
283
if (instr->format == Format::PSEUDO_BRANCH)
284
instr->opcode = instr->opcode == aco_opcode::p_cbranch_z ? aco_opcode::p_cbranch_nz
285
: aco_opcode::p_cbranch_z;
286
else if (instr->opcode == aco_opcode::s_cselect_b32)
287
std::swap(instr->operands[0], instr->operands[1]);
288
else
289
unreachable(
290
"scc_nocompare optimization is only implemented for p_cbranch and s_cselect");
291
}
292
293
/* Use the SCC def from the original instruction, not the comparison */
294
ctx.uses[instr->operands[scc_op_idx].tempId()]--;
295
instr->operands[scc_op_idx] = wr_instr->operands[0];
296
}
297
}
298
299
void
300
process_instruction(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
301
{
302
ctx.current_instr_idx++;
303
304
try_apply_branch_vcc(ctx, instr);
305
306
try_optimize_scc_nocompare(ctx, instr);
307
308
if (instr)
309
save_reg_writes(ctx, instr);
310
}
311
312
} // namespace
313
314
void
315
optimize_postRA(Program* program)
316
{
317
pr_opt_ctx ctx;
318
ctx.program = program;
319
ctx.uses = dead_code_analysis(program);
320
321
/* Forward pass
322
* Goes through each instruction exactly once, and can transform
323
* instructions or adjust the use counts of temps.
324
*/
325
for (auto& block : program->blocks) {
326
ctx.reset_block(&block);
327
328
for (aco_ptr<Instruction>& instr : block.instructions)
329
process_instruction(ctx, instr);
330
}
331
332
/* Cleanup pass
333
* Gets rid of instructions which are manually deleted or
334
* no longer have any uses.
335
*/
336
for (auto& block : program->blocks) {
337
auto new_end = std::remove_if(block.instructions.begin(), block.instructions.end(),
338
[&ctx](const aco_ptr<Instruction>& instr)
339
{ return !instr || is_dead(ctx.uses, instr.get()); });
340
block.instructions.resize(new_end - block.instructions.begin());
341
}
342
}
343
344
} // namespace aco
345
346