Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/freedreno/ir3/ir3_nir.c
4565 views
1
/*
2
* Copyright (C) 2015 Rob Clark <[email protected]>
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 FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
* SOFTWARE.
22
*
23
* Authors:
24
* Rob Clark <[email protected]>
25
*/
26
27
#include "util/debug.h"
28
#include "util/u_math.h"
29
30
#include "ir3_compiler.h"
31
#include "ir3_nir.h"
32
#include "ir3_shader.h"
33
34
static const nir_shader_compiler_options options = {
35
.lower_fpow = true,
36
.lower_scmp = true,
37
.lower_flrp16 = true,
38
.lower_flrp32 = true,
39
.lower_flrp64 = true,
40
.lower_ffract = true,
41
.lower_fmod = true,
42
.lower_fdiv = true,
43
.lower_isign = true,
44
.lower_ldexp = true,
45
.lower_uadd_carry = true,
46
.lower_usub_borrow = true,
47
.lower_mul_high = true,
48
.lower_mul_2x32_64 = true,
49
.fuse_ffma16 = true,
50
.fuse_ffma32 = true,
51
.fuse_ffma64 = true,
52
.vertex_id_zero_based = true,
53
.lower_extract_byte = true,
54
.lower_extract_word = true,
55
.lower_insert_byte = true,
56
.lower_insert_word = true,
57
.lower_helper_invocation = true,
58
.lower_bitfield_insert_to_shifts = true,
59
.lower_bitfield_extract_to_shifts = true,
60
.lower_pack_half_2x16 = true,
61
.lower_pack_snorm_4x8 = true,
62
.lower_pack_snorm_2x16 = true,
63
.lower_pack_unorm_4x8 = true,
64
.lower_pack_unorm_2x16 = true,
65
.lower_unpack_half_2x16 = true,
66
.lower_unpack_snorm_4x8 = true,
67
.lower_unpack_snorm_2x16 = true,
68
.lower_unpack_unorm_4x8 = true,
69
.lower_unpack_unorm_2x16 = true,
70
.lower_pack_split = true,
71
.use_interpolated_input_intrinsics = true,
72
.lower_rotate = true,
73
.lower_to_scalar = true,
74
.has_imul24 = true,
75
.has_fsub = true,
76
.has_isub = true,
77
.lower_wpos_pntc = true,
78
.lower_cs_local_index_from_id = true,
79
80
/* Only needed for the spirv_to_nir() pass done in ir3_cmdline.c
81
* but that should be harmless for GL since 64b is not
82
* supported there.
83
*/
84
.lower_int64_options = (nir_lower_int64_options)~0,
85
.lower_uniforms_to_ubo = true,
86
.use_scoped_barrier = true,
87
};
88
89
/* we don't want to lower vertex_id to _zero_based on newer gpus: */
90
static const nir_shader_compiler_options options_a6xx = {
91
.lower_fpow = true,
92
.lower_scmp = true,
93
.lower_flrp16 = true,
94
.lower_flrp32 = true,
95
.lower_flrp64 = true,
96
.lower_ffract = true,
97
.lower_fmod = true,
98
.lower_fdiv = true,
99
.lower_isign = true,
100
.lower_ldexp = true,
101
.lower_uadd_carry = true,
102
.lower_usub_borrow = true,
103
.lower_mul_high = true,
104
.lower_mul_2x32_64 = true,
105
.fuse_ffma16 = true,
106
.fuse_ffma32 = true,
107
.fuse_ffma64 = true,
108
.vertex_id_zero_based = false,
109
.lower_extract_byte = true,
110
.lower_extract_word = true,
111
.lower_insert_byte = true,
112
.lower_insert_word = true,
113
.lower_helper_invocation = true,
114
.lower_bitfield_insert_to_shifts = true,
115
.lower_bitfield_extract_to_shifts = true,
116
.lower_pack_half_2x16 = true,
117
.lower_pack_snorm_4x8 = true,
118
.lower_pack_snorm_2x16 = true,
119
.lower_pack_unorm_4x8 = true,
120
.lower_pack_unorm_2x16 = true,
121
.lower_unpack_half_2x16 = true,
122
.lower_unpack_snorm_4x8 = true,
123
.lower_unpack_snorm_2x16 = true,
124
.lower_unpack_unorm_4x8 = true,
125
.lower_unpack_unorm_2x16 = true,
126
.lower_pack_split = true,
127
.use_interpolated_input_intrinsics = true,
128
.lower_rotate = true,
129
.vectorize_io = true,
130
.lower_to_scalar = true,
131
.has_imul24 = true,
132
.has_fsub = true,
133
.has_isub = true,
134
.max_unroll_iterations = 32,
135
.lower_wpos_pntc = true,
136
.lower_cs_local_index_from_id = true,
137
138
/* Only needed for the spirv_to_nir() pass done in ir3_cmdline.c
139
* but that should be harmless for GL since 64b is not
140
* supported there.
141
*/
142
.lower_int64_options = (nir_lower_int64_options)~0,
143
.lower_uniforms_to_ubo = true,
144
.lower_device_index_to_zero = true,
145
.use_scoped_barrier = true,
146
};
147
148
const nir_shader_compiler_options *
149
ir3_get_compiler_options(struct ir3_compiler *compiler)
150
{
151
if (compiler->gpu_id >= 600)
152
return &options_a6xx;
153
return &options;
154
}
155
156
static bool
157
ir3_nir_should_vectorize_mem(unsigned align_mul, unsigned align_offset,
158
unsigned bit_size, unsigned num_components,
159
nir_intrinsic_instr *low,
160
nir_intrinsic_instr *high, void *data)
161
{
162
assert(bit_size >= 8);
163
if (bit_size != 32)
164
return false;
165
unsigned byte_size = bit_size / 8;
166
167
int size = num_components * byte_size;
168
169
/* Don't care about alignment past vec4. */
170
assert(util_is_power_of_two_nonzero(align_mul));
171
align_mul = MIN2(align_mul, 16);
172
align_offset &= 15;
173
174
/* Our offset alignment should aways be at least 4 bytes */
175
if (align_mul < 4)
176
return false;
177
178
unsigned worst_start_offset = 16 - align_mul + align_offset;
179
if (worst_start_offset + size > 16)
180
return false;
181
182
return true;
183
}
184
185
#define OPT(nir, pass, ...) \
186
({ \
187
bool this_progress = false; \
188
NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
189
this_progress; \
190
})
191
192
#define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
193
194
void
195
ir3_optimize_loop(struct ir3_compiler *compiler, nir_shader *s)
196
{
197
bool progress;
198
unsigned lower_flrp = (s->options->lower_flrp16 ? 16 : 0) |
199
(s->options->lower_flrp32 ? 32 : 0) |
200
(s->options->lower_flrp64 ? 64 : 0);
201
202
do {
203
progress = false;
204
205
OPT_V(s, nir_lower_vars_to_ssa);
206
progress |= OPT(s, nir_opt_copy_prop_vars);
207
progress |= OPT(s, nir_opt_dead_write_vars);
208
progress |= OPT(s, nir_lower_alu_to_scalar, NULL, NULL);
209
progress |= OPT(s, nir_lower_phis_to_scalar, false);
210
211
progress |= OPT(s, nir_copy_prop);
212
progress |= OPT(s, nir_opt_dce);
213
progress |= OPT(s, nir_opt_cse);
214
static int gcm = -1;
215
if (gcm == -1)
216
gcm = env_var_as_unsigned("GCM", 0);
217
if (gcm == 1)
218
progress |= OPT(s, nir_opt_gcm, true);
219
else if (gcm == 2)
220
progress |= OPT(s, nir_opt_gcm, false);
221
progress |= OPT(s, nir_opt_peephole_select, 16, true, true);
222
progress |= OPT(s, nir_opt_intrinsics);
223
/* NOTE: GS lowering inserts an output var with varying slot that
224
* is larger than VARYING_SLOT_MAX (ie. GS_VERTEX_FLAGS_IR3),
225
* which triggers asserts in nir_shader_gather_info(). To work
226
* around that skip lowering phi precision for GS.
227
*
228
* Calling nir_shader_gather_info() late also seems to cause
229
* problems for tess lowering, for now since we only enable
230
* fp16/int16 for frag and compute, skip phi precision lowering
231
* for other stages.
232
*/
233
if ((s->info.stage == MESA_SHADER_FRAGMENT) ||
234
(s->info.stage == MESA_SHADER_COMPUTE)) {
235
progress |= OPT(s, nir_opt_phi_precision);
236
}
237
progress |= OPT(s, nir_opt_algebraic);
238
progress |= OPT(s, nir_lower_alu);
239
progress |= OPT(s, nir_lower_pack);
240
progress |= OPT(s, nir_opt_constant_folding);
241
242
nir_load_store_vectorize_options vectorize_opts = {
243
.modes = nir_var_mem_ubo,
244
.callback = ir3_nir_should_vectorize_mem,
245
.robust_modes = compiler->robust_ubo_access ? nir_var_mem_ubo : 0,
246
};
247
progress |= OPT(s, nir_opt_load_store_vectorize, &vectorize_opts);
248
249
if (lower_flrp != 0) {
250
if (OPT(s, nir_lower_flrp, lower_flrp, false /* always_precise */)) {
251
OPT(s, nir_opt_constant_folding);
252
progress = true;
253
}
254
255
/* Nothing should rematerialize any flrps, so we only
256
* need to do this lowering once.
257
*/
258
lower_flrp = 0;
259
}
260
261
progress |= OPT(s, nir_opt_dead_cf);
262
if (OPT(s, nir_opt_trivial_continues)) {
263
progress |= true;
264
/* If nir_opt_trivial_continues makes progress, then we need to clean
265
* things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
266
* to make progress.
267
*/
268
OPT(s, nir_copy_prop);
269
OPT(s, nir_opt_dce);
270
}
271
progress |= OPT(s, nir_opt_if, false);
272
progress |= OPT(s, nir_opt_loop_unroll, nir_var_all);
273
progress |= OPT(s, nir_opt_remove_phis);
274
progress |= OPT(s, nir_opt_undef);
275
} while (progress);
276
}
277
278
static bool
279
should_split_wrmask(const nir_instr *instr, const void *data)
280
{
281
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
282
283
switch (intr->intrinsic) {
284
case nir_intrinsic_store_ssbo:
285
case nir_intrinsic_store_shared:
286
case nir_intrinsic_store_global:
287
case nir_intrinsic_store_scratch:
288
return true;
289
default:
290
return false;
291
}
292
}
293
294
void
295
ir3_nir_lower_io_to_temporaries(nir_shader *s)
296
{
297
/* Outputs consumed by the VPC, VS inputs, and FS outputs are all handled
298
* by the hardware pre-loading registers at the beginning and then reading
299
* them at the end, so we can't access them indirectly except through
300
* normal register-indirect accesses, and therefore ir3 doesn't support
301
* indirect accesses on those. Other i/o is lowered in ir3_nir_lower_tess,
302
* and indirects work just fine for those. GS outputs may be consumed by
303
* VPC, but have their own lowering in ir3_nir_lower_gs() which does
304
* something similar to nir_lower_io_to_temporaries so we shouldn't need
305
* to lower them.
306
*
307
* Note: this might be a little inefficient for VS or TES outputs which are
308
* when the next stage isn't an FS, but it probably don't make sense to
309
* depend on the next stage before variant creation.
310
*
311
* TODO: for gallium, mesa/st also does some redundant lowering, including
312
* running this pass for GS inputs/outputs which we don't want but not
313
* including TES outputs or FS inputs which we do need. We should probably
314
* stop doing that once we're sure all drivers are doing their own
315
* indirect i/o lowering.
316
*/
317
bool lower_input = s->info.stage == MESA_SHADER_VERTEX ||
318
s->info.stage == MESA_SHADER_FRAGMENT;
319
bool lower_output = s->info.stage != MESA_SHADER_TESS_CTRL &&
320
s->info.stage != MESA_SHADER_GEOMETRY;
321
if (lower_input || lower_output) {
322
NIR_PASS_V(s, nir_lower_io_to_temporaries, nir_shader_get_entrypoint(s),
323
lower_output, lower_input);
324
325
/* nir_lower_io_to_temporaries() creates global variables and copy
326
* instructions which need to be cleaned up.
327
*/
328
NIR_PASS_V(s, nir_split_var_copies);
329
NIR_PASS_V(s, nir_lower_var_copies);
330
NIR_PASS_V(s, nir_lower_global_vars_to_local);
331
}
332
333
/* Regardless of the above, we need to lower indirect references to
334
* compact variables such as clip/cull distances because due to how
335
* TCS<->TES IO works we cannot handle indirect accesses that "straddle"
336
* vec4 components. nir_lower_indirect_derefs has a special case for
337
* compact variables, so it will actually lower them even though we pass
338
* in 0 modes.
339
*
340
* Using temporaries would be slightly better but
341
* nir_lower_io_to_temporaries currently doesn't support TCS i/o.
342
*/
343
NIR_PASS_V(s, nir_lower_indirect_derefs, 0, UINT32_MAX);
344
}
345
346
void
347
ir3_finalize_nir(struct ir3_compiler *compiler, nir_shader *s)
348
{
349
struct nir_lower_tex_options tex_options = {
350
.lower_rect = 0,
351
.lower_tg4_offsets = true,
352
};
353
354
if (compiler->gpu_id >= 400) {
355
/* a4xx seems to have *no* sam.p */
356
tex_options.lower_txp = ~0; /* lower all txp */
357
} else {
358
/* a3xx just needs to avoid sam.p for 3d tex */
359
tex_options.lower_txp = (1 << GLSL_SAMPLER_DIM_3D);
360
}
361
362
if (ir3_shader_debug & IR3_DBG_DISASM) {
363
mesa_logi("----------------------");
364
nir_log_shaderi(s);
365
mesa_logi("----------------------");
366
}
367
368
if (s->info.stage == MESA_SHADER_GEOMETRY)
369
NIR_PASS_V(s, ir3_nir_lower_gs);
370
371
NIR_PASS_V(s, nir_lower_amul, ir3_glsl_type_size);
372
373
OPT_V(s, nir_lower_regs_to_ssa);
374
OPT_V(s, nir_lower_wrmasks, should_split_wrmask, s);
375
376
OPT_V(s, nir_lower_tex, &tex_options);
377
OPT_V(s, nir_lower_load_const_to_scalar);
378
if (compiler->gpu_id < 500)
379
OPT_V(s, ir3_nir_lower_tg4_to_tex);
380
381
ir3_optimize_loop(compiler, s);
382
383
/* do idiv lowering after first opt loop to get a chance to propagate
384
* constants for divide by immed power-of-two:
385
*/
386
nir_lower_idiv_options idiv_options = {
387
.imprecise_32bit_lowering = true,
388
.allow_fp16 = true,
389
};
390
const bool idiv_progress = OPT(s, nir_lower_idiv, &idiv_options);
391
392
if (idiv_progress)
393
ir3_optimize_loop(compiler, s);
394
395
OPT_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
396
397
if (ir3_shader_debug & IR3_DBG_DISASM) {
398
mesa_logi("----------------------");
399
nir_log_shaderi(s);
400
mesa_logi("----------------------");
401
}
402
403
/* st_program.c's parameter list optimization requires that future nir
404
* variants don't reallocate the uniform storage, so we have to remove
405
* uniforms that occupy storage. But we don't want to remove samplers,
406
* because they're needed for YUV variant lowering.
407
*/
408
nir_foreach_uniform_variable_safe (var, s) {
409
if (var->data.mode == nir_var_uniform &&
410
(glsl_type_get_image_count(var->type) ||
411
glsl_type_get_sampler_count(var->type)))
412
continue;
413
414
exec_node_remove(&var->node);
415
}
416
nir_validate_shader(s, "after uniform var removal");
417
418
nir_sweep(s);
419
}
420
421
static bool
422
lower_subgroup_id_filter(const nir_instr *instr, const void *unused)
423
{
424
(void)unused;
425
426
if (instr->type != nir_instr_type_intrinsic)
427
return false;
428
429
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
430
return intr->intrinsic == nir_intrinsic_load_subgroup_invocation ||
431
intr->intrinsic == nir_intrinsic_load_subgroup_id ||
432
intr->intrinsic == nir_intrinsic_load_num_subgroups;
433
}
434
435
static nir_ssa_def *
436
lower_subgroup_id(nir_builder *b, nir_instr *instr, void *unused)
437
{
438
(void)instr;
439
(void)unused;
440
441
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
442
if (intr->intrinsic == nir_intrinsic_load_subgroup_invocation) {
443
return nir_iand(
444
b, nir_load_local_invocation_index(b),
445
nir_isub(b, nir_load_subgroup_size(b), nir_imm_int(b, 1)));
446
} else if (intr->intrinsic == nir_intrinsic_load_subgroup_id) {
447
return nir_ishr(b, nir_load_local_invocation_index(b),
448
nir_load_subgroup_id_shift_ir3(b));
449
} else {
450
assert(intr->intrinsic == nir_intrinsic_load_num_subgroups);
451
/* If the workgroup size is constant,
452
* nir_lower_compute_system_values() will replace local_size with a
453
* constant so this can mostly be constant folded away.
454
*/
455
nir_ssa_def *local_size = nir_load_workgroup_size(b);
456
nir_ssa_def *size =
457
nir_imul24(b, nir_channel(b, local_size, 0),
458
nir_imul24(b, nir_channel(b, local_size, 1),
459
nir_channel(b, local_size, 2)));
460
nir_ssa_def *one = nir_imm_int(b, 1);
461
return nir_iadd(b, one,
462
nir_ishr(b, nir_isub(b, size, one),
463
nir_load_subgroup_id_shift_ir3(b)));
464
}
465
}
466
467
static bool
468
ir3_nir_lower_subgroup_id_cs(nir_shader *shader)
469
{
470
return nir_shader_lower_instructions(shader, lower_subgroup_id_filter,
471
lower_subgroup_id, NULL);
472
}
473
474
/**
475
* Late passes that need to be done after pscreen->finalize_nir()
476
*/
477
void
478
ir3_nir_post_finalize(struct ir3_compiler *compiler, nir_shader *s)
479
{
480
NIR_PASS_V(s, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
481
ir3_glsl_type_size, (nir_lower_io_options)0);
482
483
if (s->info.stage == MESA_SHADER_FRAGMENT) {
484
/* NOTE: lower load_barycentric_at_sample first, since it
485
* produces load_barycentric_at_offset:
486
*/
487
NIR_PASS_V(s, ir3_nir_lower_load_barycentric_at_sample);
488
NIR_PASS_V(s, ir3_nir_lower_load_barycentric_at_offset);
489
NIR_PASS_V(s, ir3_nir_move_varying_inputs);
490
NIR_PASS_V(s, nir_lower_fb_read);
491
}
492
493
if (compiler->gpu_id >= 600 && s->info.stage == MESA_SHADER_FRAGMENT &&
494
!(ir3_shader_debug & IR3_DBG_NOFP16)) {
495
NIR_PASS_V(s, nir_lower_mediump_io, nir_var_shader_out, 0, false);
496
}
497
498
if (s->info.stage == MESA_SHADER_COMPUTE) {
499
bool progress = false;
500
NIR_PASS(progress, s, nir_lower_subgroups,
501
&(nir_lower_subgroups_options){
502
.subgroup_size = 128,
503
.ballot_bit_size = 32,
504
.ballot_components = 4,
505
.lower_to_scalar = true,
506
.lower_vote_eq = true,
507
.lower_subgroup_masks = true,
508
.lower_read_invocation_to_cond = true,
509
});
510
511
progress = false;
512
NIR_PASS(progress, s, ir3_nir_lower_subgroup_id_cs);
513
514
/* ir3_nir_lower_subgroup_id_cs creates extra compute intrinsics which
515
* we need to lower again.
516
*/
517
if (progress)
518
NIR_PASS_V(s, nir_lower_compute_system_values, NULL);
519
}
520
521
/* we cannot ensure that ir3_finalize_nir() is only called once, so
522
* we also need to do trig workarounds here:
523
*/
524
OPT_V(s, ir3_nir_apply_trig_workarounds);
525
526
ir3_optimize_loop(compiler, s);
527
}
528
529
static bool
530
ir3_nir_lower_view_layer_id(nir_shader *nir, bool layer_zero, bool view_zero)
531
{
532
unsigned layer_id_loc = ~0, view_id_loc = ~0;
533
nir_foreach_shader_in_variable (var, nir) {
534
if (var->data.location == VARYING_SLOT_LAYER)
535
layer_id_loc = var->data.driver_location;
536
if (var->data.location == VARYING_SLOT_VIEWPORT)
537
view_id_loc = var->data.driver_location;
538
}
539
540
assert(!layer_zero || layer_id_loc != ~0);
541
assert(!view_zero || view_id_loc != ~0);
542
543
bool progress = false;
544
nir_builder b;
545
546
nir_foreach_function (func, nir) {
547
nir_builder_init(&b, func->impl);
548
549
nir_foreach_block (block, func->impl) {
550
nir_foreach_instr_safe (instr, block) {
551
if (instr->type != nir_instr_type_intrinsic)
552
continue;
553
554
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
555
556
if (intrin->intrinsic != nir_intrinsic_load_input)
557
continue;
558
559
unsigned base = nir_intrinsic_base(intrin);
560
if (base != layer_id_loc && base != view_id_loc)
561
continue;
562
563
b.cursor = nir_before_instr(&intrin->instr);
564
nir_ssa_def *zero = nir_imm_int(&b, 0);
565
nir_ssa_def_rewrite_uses(&intrin->dest.ssa, zero);
566
nir_instr_remove(&intrin->instr);
567
progress = true;
568
}
569
}
570
571
if (progress) {
572
nir_metadata_preserve(
573
func->impl, nir_metadata_block_index | nir_metadata_dominance);
574
} else {
575
nir_metadata_preserve(func->impl, nir_metadata_all);
576
}
577
}
578
579
return progress;
580
}
581
582
void
583
ir3_nir_lower_variant(struct ir3_shader_variant *so, nir_shader *s)
584
{
585
if (ir3_shader_debug & IR3_DBG_DISASM) {
586
mesa_logi("----------------------");
587
nir_log_shaderi(s);
588
mesa_logi("----------------------");
589
}
590
591
bool progress = false;
592
593
if (so->key.has_gs || so->key.tessellation) {
594
switch (so->shader->type) {
595
case MESA_SHADER_VERTEX:
596
NIR_PASS_V(s, ir3_nir_lower_to_explicit_output, so,
597
so->key.tessellation);
598
progress = true;
599
break;
600
case MESA_SHADER_TESS_CTRL:
601
NIR_PASS_V(s, ir3_nir_lower_tess_ctrl, so, so->key.tessellation);
602
NIR_PASS_V(s, ir3_nir_lower_to_explicit_input, so);
603
progress = true;
604
break;
605
case MESA_SHADER_TESS_EVAL:
606
NIR_PASS_V(s, ir3_nir_lower_tess_eval, so, so->key.tessellation);
607
if (so->key.has_gs)
608
NIR_PASS_V(s, ir3_nir_lower_to_explicit_output, so,
609
so->key.tessellation);
610
progress = true;
611
break;
612
case MESA_SHADER_GEOMETRY:
613
NIR_PASS_V(s, ir3_nir_lower_to_explicit_input, so);
614
progress = true;
615
break;
616
default:
617
break;
618
}
619
}
620
621
if (s->info.stage == MESA_SHADER_VERTEX) {
622
if (so->key.ucp_enables)
623
progress |=
624
OPT(s, nir_lower_clip_vs, so->key.ucp_enables, false, false, NULL);
625
} else if (s->info.stage == MESA_SHADER_FRAGMENT) {
626
bool layer_zero =
627
so->key.layer_zero && (s->info.inputs_read & VARYING_BIT_LAYER);
628
bool view_zero =
629
so->key.view_zero && (s->info.inputs_read & VARYING_BIT_VIEWPORT);
630
631
if (so->key.ucp_enables && !so->shader->compiler->has_clip_cull)
632
progress |= OPT(s, nir_lower_clip_fs, so->key.ucp_enables, false);
633
if (layer_zero || view_zero)
634
progress |= OPT(s, ir3_nir_lower_view_layer_id, layer_zero, view_zero);
635
}
636
637
/* Move large constant variables to the constants attached to the NIR
638
* shader, which we will upload in the immediates range. This generates
639
* amuls, so we need to clean those up after.
640
*
641
* Passing no size_align, we would get packed values, which if we end up
642
* having to load with LDC would result in extra reads to unpack from
643
* straddling loads. Align everything to vec4 to avoid that, though we
644
* could theoretically do better.
645
*/
646
OPT_V(s, nir_opt_large_constants, glsl_get_vec4_size_align_bytes,
647
32 /* bytes */);
648
OPT_V(s, ir3_nir_lower_load_constant, so);
649
650
if (!so->binning_pass)
651
OPT_V(s, ir3_nir_analyze_ubo_ranges, so);
652
653
progress |= OPT(s, ir3_nir_lower_ubo_loads, so);
654
655
/* Lower large temporaries to scratch, which in Qualcomm terms is private
656
* memory, to avoid excess register pressure. This should happen after
657
* nir_opt_large_constants, because loading from a UBO is much, much less
658
* expensive.
659
*/
660
if (so->shader->compiler->has_pvtmem) {
661
progress |= OPT(s, nir_lower_vars_to_scratch, nir_var_function_temp,
662
16 * 16 /* bytes */, glsl_get_natural_size_align_bytes);
663
}
664
665
/* Lower scratch writemasks */
666
progress |= OPT(s, nir_lower_wrmasks, should_split_wrmask, s);
667
668
OPT_V(s, nir_lower_amul, ir3_glsl_type_size);
669
670
/* UBO offset lowering has to come after we've decided what will
671
* be left as load_ubo
672
*/
673
if (so->shader->compiler->gpu_id >= 600)
674
progress |= OPT(s, nir_lower_ubo_vec4);
675
676
OPT_V(s, ir3_nir_lower_io_offsets, so->shader->compiler->gpu_id);
677
678
if (progress)
679
ir3_optimize_loop(so->shader->compiler, s);
680
681
/* Fixup indirect load_uniform's which end up with a const base offset
682
* which is too large to encode. Do this late(ish) so we actually
683
* can differentiate indirect vs non-indirect.
684
*/
685
if (OPT(s, ir3_nir_fixup_load_uniform))
686
ir3_optimize_loop(so->shader->compiler, s);
687
688
/* Do late algebraic optimization to turn add(a, neg(b)) back into
689
* subs, then the mandatory cleanup after algebraic. Note that it may
690
* produce fnegs, and if so then we need to keep running to squash
691
* fneg(fneg(a)).
692
*/
693
bool more_late_algebraic = true;
694
while (more_late_algebraic) {
695
more_late_algebraic = OPT(s, nir_opt_algebraic_late);
696
OPT_V(s, nir_opt_constant_folding);
697
OPT_V(s, nir_copy_prop);
698
OPT_V(s, nir_opt_dce);
699
OPT_V(s, nir_opt_cse);
700
}
701
702
OPT_V(s, nir_opt_sink, nir_move_const_undef);
703
704
if (ir3_shader_debug & IR3_DBG_DISASM) {
705
mesa_logi("----------------------");
706
nir_log_shaderi(s);
707
mesa_logi("----------------------");
708
}
709
710
nir_sweep(s);
711
712
/* Binning pass variants re-use the const_state of the corresponding
713
* draw pass shader, so that same const emit can be re-used for both
714
* passes:
715
*/
716
if (!so->binning_pass)
717
ir3_setup_const_state(s, so, ir3_const_state(so));
718
}
719
720
static void
721
ir3_nir_scan_driver_consts(nir_shader *shader, struct ir3_const_state *layout)
722
{
723
nir_foreach_function (function, shader) {
724
if (!function->impl)
725
continue;
726
727
nir_foreach_block (block, function->impl) {
728
nir_foreach_instr (instr, block) {
729
if (instr->type != nir_instr_type_intrinsic)
730
continue;
731
732
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
733
unsigned idx;
734
735
switch (intr->intrinsic) {
736
case nir_intrinsic_get_ssbo_size:
737
if (ir3_bindless_resource(intr->src[0]))
738
break;
739
idx = nir_src_as_uint(intr->src[0]);
740
if (layout->ssbo_size.mask & (1 << idx))
741
break;
742
layout->ssbo_size.mask |= (1 << idx);
743
layout->ssbo_size.off[idx] = layout->ssbo_size.count;
744
layout->ssbo_size.count += 1; /* one const per */
745
break;
746
case nir_intrinsic_image_atomic_add:
747
case nir_intrinsic_image_atomic_imin:
748
case nir_intrinsic_image_atomic_umin:
749
case nir_intrinsic_image_atomic_imax:
750
case nir_intrinsic_image_atomic_umax:
751
case nir_intrinsic_image_atomic_and:
752
case nir_intrinsic_image_atomic_or:
753
case nir_intrinsic_image_atomic_xor:
754
case nir_intrinsic_image_atomic_exchange:
755
case nir_intrinsic_image_atomic_comp_swap:
756
case nir_intrinsic_image_store:
757
case nir_intrinsic_image_size:
758
idx = nir_src_as_uint(intr->src[0]);
759
if (layout->image_dims.mask & (1 << idx))
760
break;
761
layout->image_dims.mask |= (1 << idx);
762
layout->image_dims.off[idx] = layout->image_dims.count;
763
layout->image_dims.count += 3; /* three const per */
764
break;
765
case nir_intrinsic_load_base_vertex:
766
case nir_intrinsic_load_first_vertex:
767
layout->num_driver_params =
768
MAX2(layout->num_driver_params, IR3_DP_VTXID_BASE + 1);
769
break;
770
case nir_intrinsic_load_base_instance:
771
layout->num_driver_params =
772
MAX2(layout->num_driver_params, IR3_DP_INSTID_BASE + 1);
773
break;
774
case nir_intrinsic_load_user_clip_plane:
775
idx = nir_intrinsic_ucp_id(intr);
776
layout->num_driver_params = MAX2(layout->num_driver_params,
777
IR3_DP_UCP0_X + (idx + 1) * 4);
778
break;
779
case nir_intrinsic_load_num_workgroups:
780
layout->num_driver_params =
781
MAX2(layout->num_driver_params, IR3_DP_NUM_WORK_GROUPS_Z + 1);
782
break;
783
case nir_intrinsic_load_workgroup_size:
784
layout->num_driver_params = MAX2(layout->num_driver_params,
785
IR3_DP_LOCAL_GROUP_SIZE_Z + 1);
786
break;
787
case nir_intrinsic_load_base_workgroup_id:
788
layout->num_driver_params =
789
MAX2(layout->num_driver_params, IR3_DP_BASE_GROUP_Z + 1);
790
break;
791
case nir_intrinsic_load_subgroup_size:
792
layout->num_driver_params =
793
MAX2(layout->num_driver_params, IR3_DP_SUBGROUP_SIZE + 1);
794
break;
795
case nir_intrinsic_load_subgroup_id_shift_ir3:
796
layout->num_driver_params =
797
MAX2(layout->num_driver_params, IR3_DP_SUBGROUP_ID_SHIFT + 1);
798
break;
799
default:
800
break;
801
}
802
}
803
}
804
}
805
}
806
807
/* Sets up the variant-dependent constant state for the ir3_shader. Note
808
* that it is also used from ir3_nir_analyze_ubo_ranges() to figure out the
809
* maximum number of driver params that would eventually be used, to leave
810
* space for this function to allocate the driver params.
811
*/
812
void
813
ir3_setup_const_state(nir_shader *nir, struct ir3_shader_variant *v,
814
struct ir3_const_state *const_state)
815
{
816
struct ir3_compiler *compiler = v->shader->compiler;
817
818
memset(&const_state->offsets, ~0, sizeof(const_state->offsets));
819
820
ir3_nir_scan_driver_consts(nir, const_state);
821
822
if ((compiler->gpu_id < 500) && (v->shader->stream_output.num_outputs > 0)) {
823
const_state->num_driver_params =
824
MAX2(const_state->num_driver_params, IR3_DP_VTXCNT_MAX + 1);
825
}
826
827
const_state->num_ubos = nir->info.num_ubos;
828
829
/* num_driver_params is scalar, align to vec4: */
830
const_state->num_driver_params = align(const_state->num_driver_params, 4);
831
832
debug_assert((const_state->ubo_state.size % 16) == 0);
833
unsigned constoff = const_state->ubo_state.size / 16;
834
unsigned ptrsz = ir3_pointer_size(compiler);
835
836
if (const_state->num_ubos > 0) {
837
const_state->offsets.ubo = constoff;
838
constoff += align(const_state->num_ubos * ptrsz, 4) / 4;
839
}
840
841
if (const_state->ssbo_size.count > 0) {
842
unsigned cnt = const_state->ssbo_size.count;
843
const_state->offsets.ssbo_sizes = constoff;
844
constoff += align(cnt, 4) / 4;
845
}
846
847
if (const_state->image_dims.count > 0) {
848
unsigned cnt = const_state->image_dims.count;
849
const_state->offsets.image_dims = constoff;
850
constoff += align(cnt, 4) / 4;
851
}
852
853
if (const_state->num_driver_params > 0) {
854
/* offset cannot be 0 for vs params loaded by CP_DRAW_INDIRECT_MULTI */
855
if (v->type == MESA_SHADER_VERTEX && compiler->gpu_id >= 600)
856
constoff = MAX2(constoff, 1);
857
const_state->offsets.driver_param = constoff;
858
}
859
constoff += const_state->num_driver_params / 4;
860
861
if ((v->type == MESA_SHADER_VERTEX) && (compiler->gpu_id < 500) &&
862
v->shader->stream_output.num_outputs > 0) {
863
const_state->offsets.tfbo = constoff;
864
constoff += align(IR3_MAX_SO_BUFFERS * ptrsz, 4) / 4;
865
}
866
867
switch (v->type) {
868
case MESA_SHADER_VERTEX:
869
const_state->offsets.primitive_param = constoff;
870
constoff += 1;
871
break;
872
case MESA_SHADER_TESS_CTRL:
873
case MESA_SHADER_TESS_EVAL:
874
constoff = align(constoff - 1, 4) + 3;
875
const_state->offsets.primitive_param = constoff;
876
const_state->offsets.primitive_map = constoff + 5;
877
constoff += 5 + DIV_ROUND_UP(v->input_size, 4);
878
break;
879
case MESA_SHADER_GEOMETRY:
880
const_state->offsets.primitive_param = constoff;
881
const_state->offsets.primitive_map = constoff + 1;
882
constoff += 1 + DIV_ROUND_UP(v->input_size, 4);
883
break;
884
default:
885
break;
886
}
887
888
const_state->offsets.immediate = constoff;
889
890
assert(constoff <= ir3_max_const(v));
891
}
892
893