Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/amd/compiler/aco_ir.cpp
4550 views
1
/*
2
* Copyright © 2020 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 "util/debug.h"
28
29
#include "c11/threads.h"
30
31
namespace aco {
32
33
uint64_t debug_flags = 0;
34
35
static const struct debug_control aco_debug_options[] = {{"validateir", DEBUG_VALIDATE_IR},
36
{"validatera", DEBUG_VALIDATE_RA},
37
{"perfwarn", DEBUG_PERFWARN},
38
{"force-waitcnt", DEBUG_FORCE_WAITCNT},
39
{"novn", DEBUG_NO_VN},
40
{"noopt", DEBUG_NO_OPT},
41
{"nosched", DEBUG_NO_SCHED},
42
{"perfinfo", DEBUG_PERF_INFO},
43
{"liveinfo", DEBUG_LIVE_INFO},
44
{NULL, 0}};
45
46
static once_flag init_once_flag = ONCE_FLAG_INIT;
47
48
static void
49
init_once()
50
{
51
debug_flags = parse_debug_string(getenv("ACO_DEBUG"), aco_debug_options);
52
53
#ifndef NDEBUG
54
/* enable some flags by default on debug builds */
55
debug_flags |= aco::DEBUG_VALIDATE_IR;
56
#endif
57
}
58
59
void
60
init()
61
{
62
call_once(&init_once_flag, init_once);
63
}
64
65
void
66
init_program(Program* program, Stage stage, struct radv_shader_info* info,
67
enum chip_class chip_class, enum radeon_family family, bool wgp_mode,
68
ac_shader_config* config)
69
{
70
program->stage = stage;
71
program->config = config;
72
program->info = info;
73
program->chip_class = chip_class;
74
if (family == CHIP_UNKNOWN) {
75
switch (chip_class) {
76
case GFX6: program->family = CHIP_TAHITI; break;
77
case GFX7: program->family = CHIP_BONAIRE; break;
78
case GFX8: program->family = CHIP_POLARIS10; break;
79
case GFX9: program->family = CHIP_VEGA10; break;
80
case GFX10: program->family = CHIP_NAVI10; break;
81
default: program->family = CHIP_UNKNOWN; break;
82
}
83
} else {
84
program->family = family;
85
}
86
program->wave_size = info->wave_size;
87
program->lane_mask = program->wave_size == 32 ? s1 : s2;
88
89
program->dev.lds_encoding_granule = chip_class >= GFX7 ? 512 : 256;
90
program->dev.lds_alloc_granule =
91
chip_class >= GFX10_3 ? 1024 : program->dev.lds_encoding_granule;
92
program->dev.lds_limit = chip_class >= GFX7 ? 65536 : 32768;
93
/* apparently gfx702 also has 16-bank LDS but I can't find a family for that */
94
program->dev.has_16bank_lds = family == CHIP_KABINI || family == CHIP_STONEY;
95
96
program->dev.vgpr_limit = 256;
97
program->dev.physical_vgprs = 256;
98
program->dev.vgpr_alloc_granule = 4;
99
100
if (chip_class >= GFX10) {
101
program->dev.physical_sgprs = 5120; /* doesn't matter as long as it's at least 128 * 40 */
102
program->dev.physical_vgprs = program->wave_size == 32 ? 1024 : 512;
103
program->dev.sgpr_alloc_granule = 128;
104
program->dev.sgpr_limit =
105
108; /* includes VCC, which can be treated as s[106-107] on GFX10+ */
106
if (chip_class >= GFX10_3)
107
program->dev.vgpr_alloc_granule = program->wave_size == 32 ? 16 : 8;
108
else
109
program->dev.vgpr_alloc_granule = program->wave_size == 32 ? 8 : 4;
110
} else if (program->chip_class >= GFX8) {
111
program->dev.physical_sgprs = 800;
112
program->dev.sgpr_alloc_granule = 16;
113
program->dev.sgpr_limit = 102;
114
if (family == CHIP_TONGA || family == CHIP_ICELAND)
115
program->dev.sgpr_alloc_granule = 96; /* workaround hardware bug */
116
} else {
117
program->dev.physical_sgprs = 512;
118
program->dev.sgpr_alloc_granule = 8;
119
program->dev.sgpr_limit = 104;
120
}
121
122
program->dev.max_wave64_per_simd = 10;
123
if (program->chip_class >= GFX10_3)
124
program->dev.max_wave64_per_simd = 16;
125
else if (program->chip_class == GFX10)
126
program->dev.max_wave64_per_simd = 20;
127
else if (program->family >= CHIP_POLARIS10 && program->family <= CHIP_VEGAM)
128
program->dev.max_wave64_per_simd = 8;
129
130
program->dev.simd_per_cu = program->chip_class >= GFX10 ? 2 : 4;
131
132
switch (program->family) {
133
/* GFX8 APUs */
134
case CHIP_CARRIZO:
135
case CHIP_STONEY:
136
/* GFX9 APUS */
137
case CHIP_RAVEN:
138
case CHIP_RAVEN2:
139
case CHIP_RENOIR: program->dev.xnack_enabled = true; break;
140
default: break;
141
}
142
143
program->dev.sram_ecc_enabled = program->family == CHIP_ARCTURUS;
144
/* apparently gfx702 also has fast v_fma_f32 but I can't find a family for that */
145
program->dev.has_fast_fma32 = program->chip_class >= GFX9;
146
if (program->family == CHIP_TAHITI || program->family == CHIP_CARRIZO ||
147
program->family == CHIP_HAWAII)
148
program->dev.has_fast_fma32 = true;
149
150
program->wgp_mode = wgp_mode;
151
152
program->progress = CompilationProgress::after_isel;
153
154
program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;
155
program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;
156
program->next_fp_mode.must_flush_denorms32 = false;
157
program->next_fp_mode.must_flush_denorms16_64 = false;
158
program->next_fp_mode.care_about_round32 = false;
159
program->next_fp_mode.care_about_round16_64 = false;
160
program->next_fp_mode.denorm16_64 = fp_denorm_keep;
161
program->next_fp_mode.denorm32 = 0;
162
program->next_fp_mode.round16_64 = fp_round_ne;
163
program->next_fp_mode.round32 = fp_round_ne;
164
}
165
166
memory_sync_info
167
get_sync_info(const Instruction* instr)
168
{
169
switch (instr->format) {
170
case Format::SMEM: return instr->smem().sync;
171
case Format::MUBUF: return instr->mubuf().sync;
172
case Format::MIMG: return instr->mimg().sync;
173
case Format::MTBUF: return instr->mtbuf().sync;
174
case Format::FLAT:
175
case Format::GLOBAL:
176
case Format::SCRATCH: return instr->flatlike().sync;
177
case Format::DS: return instr->ds().sync;
178
default: return memory_sync_info();
179
}
180
}
181
182
bool
183
can_use_SDWA(chip_class chip, const aco_ptr<Instruction>& instr, bool pre_ra)
184
{
185
if (!instr->isVALU())
186
return false;
187
188
if (chip < GFX8 || instr->isDPP())
189
return false;
190
191
if (instr->isSDWA())
192
return true;
193
194
if (instr->isVOP3()) {
195
VOP3_instruction& vop3 = instr->vop3();
196
if (instr->format == Format::VOP3)
197
return false;
198
if (vop3.clamp && instr->format == asVOP3(Format::VOPC) && chip != GFX8)
199
return false;
200
if (vop3.omod && chip < GFX9)
201
return false;
202
203
// TODO: return true if we know we will use vcc
204
if (!pre_ra && instr->definitions.size() >= 2)
205
return false;
206
207
for (unsigned i = 1; i < instr->operands.size(); i++) {
208
if (instr->operands[i].isLiteral())
209
return false;
210
if (chip < GFX9 && !instr->operands[i].isOfType(RegType::vgpr))
211
return false;
212
}
213
}
214
215
if (!instr->definitions.empty() && instr->definitions[0].bytes() > 4)
216
return false;
217
218
if (!instr->operands.empty()) {
219
if (instr->operands[0].isLiteral())
220
return false;
221
if (chip < GFX9 && !instr->operands[0].isOfType(RegType::vgpr))
222
return false;
223
if (instr->operands[0].bytes() > 4)
224
return false;
225
if (instr->operands.size() > 1 && instr->operands[1].bytes() > 4)
226
return false;
227
}
228
229
bool is_mac = instr->opcode == aco_opcode::v_mac_f32 || instr->opcode == aco_opcode::v_mac_f16 ||
230
instr->opcode == aco_opcode::v_fmac_f32 || instr->opcode == aco_opcode::v_fmac_f16;
231
232
if (chip != GFX8 && is_mac)
233
return false;
234
235
// TODO: return true if we know we will use vcc
236
if (!pre_ra && instr->isVOPC())
237
return false;
238
if (!pre_ra && instr->operands.size() >= 3 && !is_mac)
239
return false;
240
241
return instr->opcode != aco_opcode::v_madmk_f32 && instr->opcode != aco_opcode::v_madak_f32 &&
242
instr->opcode != aco_opcode::v_madmk_f16 && instr->opcode != aco_opcode::v_madak_f16 &&
243
instr->opcode != aco_opcode::v_readfirstlane_b32 &&
244
instr->opcode != aco_opcode::v_clrexcp && instr->opcode != aco_opcode::v_swap_b32;
245
}
246
247
/* updates "instr" and returns the old instruction (or NULL if no update was needed) */
248
aco_ptr<Instruction>
249
convert_to_SDWA(chip_class chip, aco_ptr<Instruction>& instr)
250
{
251
if (instr->isSDWA())
252
return NULL;
253
254
aco_ptr<Instruction> tmp = std::move(instr);
255
Format format =
256
(Format)(((uint16_t)tmp->format & ~(uint16_t)Format::VOP3) | (uint16_t)Format::SDWA);
257
instr.reset(create_instruction<SDWA_instruction>(tmp->opcode, format, tmp->operands.size(),
258
tmp->definitions.size()));
259
std::copy(tmp->operands.cbegin(), tmp->operands.cend(), instr->operands.begin());
260
std::copy(tmp->definitions.cbegin(), tmp->definitions.cend(), instr->definitions.begin());
261
262
SDWA_instruction& sdwa = instr->sdwa();
263
264
if (tmp->isVOP3()) {
265
VOP3_instruction& vop3 = tmp->vop3();
266
memcpy(sdwa.neg, vop3.neg, sizeof(sdwa.neg));
267
memcpy(sdwa.abs, vop3.abs, sizeof(sdwa.abs));
268
sdwa.omod = vop3.omod;
269
sdwa.clamp = vop3.clamp;
270
}
271
272
for (unsigned i = 0; i < instr->operands.size(); i++) {
273
/* SDWA only uses operands 0 and 1. */
274
if (i >= 2)
275
break;
276
277
switch (instr->operands[i].bytes()) {
278
case 1: sdwa.sel[i] = sdwa_ubyte; break;
279
case 2: sdwa.sel[i] = sdwa_uword; break;
280
case 4: sdwa.sel[i] = sdwa_udword; break;
281
}
282
}
283
switch (instr->definitions[0].bytes()) {
284
case 1:
285
sdwa.dst_sel = sdwa_ubyte;
286
sdwa.dst_preserve = true;
287
break;
288
case 2:
289
sdwa.dst_sel = sdwa_uword;
290
sdwa.dst_preserve = true;
291
break;
292
case 4: sdwa.dst_sel = sdwa_udword; break;
293
}
294
295
if (instr->definitions[0].getTemp().type() == RegType::sgpr && chip == GFX8)
296
instr->definitions[0].setFixed(vcc);
297
if (instr->definitions.size() >= 2)
298
instr->definitions[1].setFixed(vcc);
299
if (instr->operands.size() >= 3)
300
instr->operands[2].setFixed(vcc);
301
302
return tmp;
303
}
304
305
bool
306
can_use_opsel(chip_class chip, aco_opcode op, int idx, bool high)
307
{
308
/* opsel is only GFX9+ */
309
if ((high || idx == -1) && chip < GFX9)
310
return false;
311
312
switch (op) {
313
case aco_opcode::v_div_fixup_f16:
314
case aco_opcode::v_fma_f16:
315
case aco_opcode::v_mad_f16:
316
case aco_opcode::v_mad_u16:
317
case aco_opcode::v_mad_i16:
318
case aco_opcode::v_med3_f16:
319
case aco_opcode::v_med3_i16:
320
case aco_opcode::v_med3_u16:
321
case aco_opcode::v_min3_f16:
322
case aco_opcode::v_min3_i16:
323
case aco_opcode::v_min3_u16:
324
case aco_opcode::v_max3_f16:
325
case aco_opcode::v_max3_i16:
326
case aco_opcode::v_max3_u16:
327
case aco_opcode::v_max_u16_e64:
328
case aco_opcode::v_max_i16_e64:
329
case aco_opcode::v_min_u16_e64:
330
case aco_opcode::v_min_i16_e64:
331
case aco_opcode::v_add_i16:
332
case aco_opcode::v_sub_i16:
333
case aco_opcode::v_add_u16_e64:
334
case aco_opcode::v_sub_u16_e64:
335
case aco_opcode::v_lshlrev_b16_e64:
336
case aco_opcode::v_lshrrev_b16_e64:
337
case aco_opcode::v_ashrrev_i16_e64:
338
case aco_opcode::v_mul_lo_u16_e64: return true;
339
case aco_opcode::v_pack_b32_f16:
340
case aco_opcode::v_cvt_pknorm_i16_f16:
341
case aco_opcode::v_cvt_pknorm_u16_f16: return idx != -1;
342
case aco_opcode::v_mad_u32_u16:
343
case aco_opcode::v_mad_i32_i16: return idx >= 0 && idx < 2;
344
default: return false;
345
}
346
}
347
348
uint32_t
349
get_reduction_identity(ReduceOp op, unsigned idx)
350
{
351
switch (op) {
352
case iadd8:
353
case iadd16:
354
case iadd32:
355
case iadd64:
356
case fadd16:
357
case fadd32:
358
case fadd64:
359
case ior8:
360
case ior16:
361
case ior32:
362
case ior64:
363
case ixor8:
364
case ixor16:
365
case ixor32:
366
case ixor64:
367
case umax8:
368
case umax16:
369
case umax32:
370
case umax64: return 0;
371
case imul8:
372
case imul16:
373
case imul32:
374
case imul64: return idx ? 0 : 1;
375
case fmul16: return 0x3c00u; /* 1.0 */
376
case fmul32: return 0x3f800000u; /* 1.0 */
377
case fmul64: return idx ? 0x3ff00000u : 0u; /* 1.0 */
378
case imin8: return INT8_MAX;
379
case imin16: return INT16_MAX;
380
case imin32: return INT32_MAX;
381
case imin64: return idx ? 0x7fffffffu : 0xffffffffu;
382
case imax8: return INT8_MIN;
383
case imax16: return INT16_MIN;
384
case imax32: return INT32_MIN;
385
case imax64: return idx ? 0x80000000u : 0;
386
case umin8:
387
case umin16:
388
case iand8:
389
case iand16: return 0xffffffffu;
390
case umin32:
391
case umin64:
392
case iand32:
393
case iand64: return 0xffffffffu;
394
case fmin16: return 0x7c00u; /* infinity */
395
case fmin32: return 0x7f800000u; /* infinity */
396
case fmin64: return idx ? 0x7ff00000u : 0u; /* infinity */
397
case fmax16: return 0xfc00u; /* negative infinity */
398
case fmax32: return 0xff800000u; /* negative infinity */
399
case fmax64: return idx ? 0xfff00000u : 0u; /* negative infinity */
400
default: unreachable("Invalid reduction operation"); break;
401
}
402
return 0;
403
}
404
405
bool
406
needs_exec_mask(const Instruction* instr)
407
{
408
if (instr->isSALU() || instr->isBranch())
409
return instr->reads_exec();
410
if (instr->isSMEM())
411
return false;
412
if (instr->isBarrier())
413
return false;
414
415
if (instr->isPseudo()) {
416
switch (instr->opcode) {
417
case aco_opcode::p_create_vector:
418
case aco_opcode::p_extract_vector:
419
case aco_opcode::p_split_vector:
420
case aco_opcode::p_phi:
421
case aco_opcode::p_parallelcopy:
422
for (Definition def : instr->definitions) {
423
if (def.getTemp().type() == RegType::vgpr)
424
return true;
425
}
426
return false;
427
case aco_opcode::p_spill:
428
case aco_opcode::p_reload:
429
case aco_opcode::p_logical_start:
430
case aco_opcode::p_logical_end:
431
case aco_opcode::p_startpgm: return false;
432
default: break;
433
}
434
}
435
436
if (instr->opcode == aco_opcode::v_readlane_b32 ||
437
instr->opcode == aco_opcode::v_readlane_b32_e64 ||
438
instr->opcode == aco_opcode::v_writelane_b32 ||
439
instr->opcode == aco_opcode::v_writelane_b32_e64)
440
return false;
441
442
return true;
443
}
444
445
wait_imm::wait_imm() : vm(unset_counter), exp(unset_counter), lgkm(unset_counter), vs(unset_counter)
446
{}
447
wait_imm::wait_imm(uint16_t vm_, uint16_t exp_, uint16_t lgkm_, uint16_t vs_)
448
: vm(vm_), exp(exp_), lgkm(lgkm_), vs(vs_)
449
{}
450
451
wait_imm::wait_imm(enum chip_class chip, uint16_t packed) : vs(unset_counter)
452
{
453
vm = packed & 0xf;
454
if (chip >= GFX9)
455
vm |= (packed >> 10) & 0x30;
456
457
exp = (packed >> 4) & 0x7;
458
459
lgkm = (packed >> 8) & 0xf;
460
if (chip >= GFX10)
461
lgkm |= (packed >> 8) & 0x30;
462
}
463
464
uint16_t
465
wait_imm::pack(enum chip_class chip) const
466
{
467
uint16_t imm = 0;
468
assert(exp == unset_counter || exp <= 0x7);
469
switch (chip) {
470
case GFX10:
471
case GFX10_3:
472
assert(lgkm == unset_counter || lgkm <= 0x3f);
473
assert(vm == unset_counter || vm <= 0x3f);
474
imm = ((vm & 0x30) << 10) | ((lgkm & 0x3f) << 8) | ((exp & 0x7) << 4) | (vm & 0xf);
475
break;
476
case GFX9:
477
assert(lgkm == unset_counter || lgkm <= 0xf);
478
assert(vm == unset_counter || vm <= 0x3f);
479
imm = ((vm & 0x30) << 10) | ((lgkm & 0xf) << 8) | ((exp & 0x7) << 4) | (vm & 0xf);
480
break;
481
default:
482
assert(lgkm == unset_counter || lgkm <= 0xf);
483
assert(vm == unset_counter || vm <= 0xf);
484
imm = ((lgkm & 0xf) << 8) | ((exp & 0x7) << 4) | (vm & 0xf);
485
break;
486
}
487
if (chip < GFX9 && vm == wait_imm::unset_counter)
488
imm |= 0xc000; /* should have no effect on pre-GFX9 and now we won't have to worry about the
489
architecture when interpreting the immediate */
490
if (chip < GFX10 && lgkm == wait_imm::unset_counter)
491
imm |= 0x3000; /* should have no effect on pre-GFX10 and now we won't have to worry about the
492
architecture when interpreting the immediate */
493
return imm;
494
}
495
496
bool
497
wait_imm::combine(const wait_imm& other)
498
{
499
bool changed = other.vm < vm || other.exp < exp || other.lgkm < lgkm || other.vs < vs;
500
vm = std::min(vm, other.vm);
501
exp = std::min(exp, other.exp);
502
lgkm = std::min(lgkm, other.lgkm);
503
vs = std::min(vs, other.vs);
504
return changed;
505
}
506
507
bool
508
wait_imm::empty() const
509
{
510
return vm == unset_counter && exp == unset_counter && lgkm == unset_counter &&
511
vs == unset_counter;
512
}
513
514
bool
515
should_form_clause(const Instruction* a, const Instruction* b)
516
{
517
/* Vertex attribute loads from the same binding likely load from similar addresses */
518
unsigned a_vtx_binding =
519
a->isMUBUF() ? a->mubuf().vtx_binding : (a->isMTBUF() ? a->mtbuf().vtx_binding : 0);
520
unsigned b_vtx_binding =
521
b->isMUBUF() ? b->mubuf().vtx_binding : (b->isMTBUF() ? b->mtbuf().vtx_binding : 0);
522
if (a_vtx_binding && a_vtx_binding == b_vtx_binding)
523
return true;
524
525
if (a->format != b->format)
526
return false;
527
528
/* Assume loads which don't use descriptors might load from similar addresses. */
529
if (a->isFlatLike())
530
return true;
531
if (a->isSMEM() && a->operands[0].bytes() == 8 && b->operands[0].bytes() == 8)
532
return true;
533
534
/* If they load from the same descriptor, assume they might load from similar
535
* addresses.
536
*/
537
if (a->isVMEM() || a->isSMEM())
538
return a->operands[0].tempId() == b->operands[0].tempId();
539
540
return false;
541
}
542
543
} // namespace aco
544
545