| 1 |
/* |
| 2 |
* Copyright (C) 2020,2026 Collabora Ltd. |
| 3 |
* Copyright (C) 2022 Alyssa Rosenzweig |
| 4 |
* Copyright (C) 2025 Arm Ltd. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "compiler/glsl_types.h" |
| 9 |
#include "compiler/nir/nir_builder.h" |
| 10 |
#include "panfrost/compiler/pan_compiler.h" |
| 11 |
#include "panfrost/compiler/pan_nir.h" |
| 12 |
#include "util/perf/cpu_trace.h" |
| 13 |
|
| 14 |
#include "panfrost/model/pan_model.h" |
| 15 |
#include "valhall/valhall.h" |
| 16 |
#include "bi_debug.h" |
| 17 |
#include "bifrost_compile.h" |
| 18 |
#include "bifrost_nir.h" |
| 19 |
#include "compiler.h" |
| 20 |
|
| 21 |
/* |
| 22 |
* Some operations are only available as 32-bit instructions. 64-bit floats are |
| 23 |
* unsupported and ints are lowered with nir_lower_int64. Certain 8-bit and |
| 24 |
* 16-bit instructions, however, are lowered here. |
| 25 |
*/ |
| 26 |
static unsigned |
| 27 |
bi_lower_bit_size(const nir_instr *instr, void *data) |
| 28 |
{ |
| 29 |
switch (instr->type) { |
| 30 |
case nir_instr_type_alu: { |
| 31 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 32 |
uint64_t gpu_id = *((uint64_t *)data); |
| 33 |
|
| 34 |
switch (alu->op) { |
| 35 |
case nir_op_fexp2: |
| 36 |
case nir_op_flog2: |
| 37 |
case nir_op_fpow: |
| 38 |
case nir_op_fsin: |
| 39 |
case nir_op_fcos: |
| 40 |
case nir_op_bit_count: |
| 41 |
case nir_op_bitfield_reverse: |
| 42 |
return (nir_src_bit_size(alu->src[0].src) == 32) ? 0 : 32; |
| 43 |
case nir_op_fround_even: |
| 44 |
case nir_op_fceil: |
| 45 |
case nir_op_ffloor: |
| 46 |
case nir_op_ffract: |
| 47 |
case nir_op_ftrunc: |
| 48 |
case nir_op_frexp_sig: |
| 49 |
case nir_op_frexp_exp: |
| 50 |
/* On v11+, FROUND.v2s16 is gone */ |
| 51 |
if (pan_arch(gpu_id) < 11) |
| 52 |
return 0; |
| 53 |
return (nir_src_bit_size(alu->src[0].src) == 32) ? 0 : 32; |
| 54 |
case nir_op_iadd: |
| 55 |
case nir_op_isub: |
| 56 |
case nir_op_iadd_sat: |
| 57 |
case nir_op_uadd_sat: |
| 58 |
case nir_op_isub_sat: |
| 59 |
case nir_op_usub_sat: |
| 60 |
case nir_op_ineg: |
| 61 |
case nir_op_iabs: |
| 62 |
/* On v11+, IABS.v4s8, IADD.v4s8 and ISUB.v4s8 are gone */ |
| 63 |
if (pan_arch(gpu_id) < 11) |
| 64 |
return 0; |
| 65 |
|
| 66 |
return (nir_src_bit_size(alu->src[0].src) == 8) ? 16 : 0; |
| 67 |
default: |
| 68 |
return 0; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
case nir_instr_type_intrinsic: { |
| 73 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 74 |
|
| 75 |
/* We only support ballot on 32-bit types. */ |
| 76 |
switch (intr->intrinsic) { |
| 77 |
case nir_intrinsic_ballot: |
| 78 |
case nir_intrinsic_ballot_relaxed: |
| 79 |
return (nir_src_bit_size(intr->src[0]) == 32) ? 0 : 32; |
| 80 |
default: |
| 81 |
return 0; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
default: |
| 86 |
return 0; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/* Although Bifrost generally supports packed 16-bit vec2 and 8-bit vec4, |
| 91 |
* transcendentals are an exception. Also shifts because of lane size mismatch |
| 92 |
* (8-bit in Bifrost, 32-bit in NIR TODO - workaround!). Some conversions need |
| 93 |
* to be scalarized due to type size. */ |
| 94 |
|
| 95 |
static uint8_t |
| 96 |
bi_vectorize_filter(const nir_instr *instr, const void *data) |
| 97 |
{ |
| 98 |
uint64_t gpu_id = *((uint64_t *)data); |
| 99 |
|
| 100 |
if (instr->type == nir_instr_type_phi) { |
| 101 |
unsigned bit_size = nir_instr_as_phi(instr)->def.bit_size; |
| 102 |
if (bit_size == 8) |
| 103 |
return 4; |
| 104 |
if (bit_size == 16) |
| 105 |
return 2; |
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
/* Do not vectorize all non-ALU instruction */ |
| 110 |
if (instr->type != nir_instr_type_alu) |
| 111 |
return 0; |
| 112 |
|
| 113 |
const nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 114 |
|
| 115 |
switch (alu->op) { |
| 116 |
case nir_op_ball_fequal2: |
| 117 |
case nir_op_ball_fequal3: |
| 118 |
case nir_op_ball_fequal4: |
| 119 |
case nir_op_bany_fnequal2: |
| 120 |
case nir_op_bany_fnequal3: |
| 121 |
case nir_op_bany_fnequal4: |
| 122 |
case nir_op_ball_iequal2: |
| 123 |
case nir_op_ball_iequal3: |
| 124 |
case nir_op_ball_iequal4: |
| 125 |
case nir_op_bany_inequal2: |
| 126 |
case nir_op_bany_inequal3: |
| 127 |
case nir_op_bany_inequal4: return 1; |
| 128 |
case nir_op_pack_uvec2_to_uint: |
| 129 |
case nir_op_pack_uvec4_to_uint: |
| 130 |
return 0; |
| 131 |
case nir_op_frcp: |
| 132 |
case nir_op_frsq: |
| 133 |
case nir_op_ishl: |
| 134 |
case nir_op_ishr: |
| 135 |
case nir_op_ushr: |
| 136 |
case nir_op_extract_u16: |
| 137 |
case nir_op_extract_i16: |
| 138 |
case nir_op_insert_u16: |
| 139 |
return 1; |
| 140 |
/* On v11+, we lost all packed F16 conversions */ |
| 141 |
case nir_op_f2f16: |
| 142 |
case nir_op_f2f16_rtz: |
| 143 |
case nir_op_f2f16_rtne: |
| 144 |
case nir_op_u2f16: |
| 145 |
case nir_op_i2f16: |
| 146 |
case nir_op_frexp_sig: |
| 147 |
case nir_op_frexp_exp: |
| 148 |
if (pan_arch(gpu_id) >= 11) |
| 149 |
return 1; |
| 150 |
|
| 151 |
break; |
| 152 |
default: |
| 153 |
break; |
| 154 |
} |
| 155 |
|
| 156 |
const uint8_t bit_size = |
| 157 |
MAX2(alu->def.bit_size, nir_src_bit_size(alu->src[0].src)); |
| 158 |
|
| 159 |
if (bit_size == 1) |
| 160 |
return 0; |
| 161 |
else |
| 162 |
return MAX2(1, 32 / bit_size); |
| 163 |
} |
| 164 |
|
| 165 |
static bool |
| 166 |
mem_vectorize_cb(unsigned align_mul, unsigned align_offset, unsigned bit_size, |
| 167 |
unsigned num_components, int64_t hole_size, |
| 168 |
nir_intrinsic_instr *low, nir_intrinsic_instr *high, |
| 169 |
void *data) |
| 170 |
{ |
| 171 |
if (hole_size > 0) |
| 172 |
return false; |
| 173 |
|
| 174 |
/* We have a hard limit of at most 4 components */ |
| 175 |
if (num_components > 4) |
| 176 |
return false; |
| 177 |
|
| 178 |
const unsigned bytes = num_components * (bit_size / 8); |
| 179 |
const unsigned max_bytes = 128u / 8u; /* LOAD.i128 */ |
| 180 |
|
| 181 |
const unsigned combined_align = nir_combined_align(align_mul, align_offset); |
| 182 |
return bytes <= combined_align && bytes <= max_bytes; |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
bi_optimize_loop_nir(nir_shader *nir, uint64_t gpu_id, bool allow_copies) |
| 187 |
{ |
| 188 |
bool progress; |
| 189 |
|
| 190 |
do { |
| 191 |
progress = false; |
| 192 |
|
| 193 |
NIR_PASS(progress, nir, nir_split_array_vars, nir_var_function_temp); |
| 194 |
NIR_PASS(progress, nir, nir_shrink_vec_array_vars, nir_var_function_temp); |
| 195 |
NIR_PASS(progress, nir, nir_opt_deref); |
| 196 |
|
| 197 |
NIR_PASS(progress, nir, nir_lower_vars_to_ssa); |
| 198 |
NIR_PASS(progress, nir, nir_lower_wrmasks); |
| 199 |
|
| 200 |
if (allow_copies) { |
| 201 |
/* Only run this pass in the first call to bi_optimize_nir. Later |
| 202 |
* calls assume that we've lowered away any copy_deref instructions |
| 203 |
* and we don't want to introduce any more. |
| 204 |
*/ |
| 205 |
NIR_PASS(progress, nir, nir_opt_find_array_copies); |
| 206 |
} |
| 207 |
|
| 208 |
NIR_PASS(progress, nir, nir_opt_copy_prop_vars); |
| 209 |
NIR_PASS(progress, nir, nir_opt_dead_write_vars); |
| 210 |
NIR_PASS(progress, nir, nir_opt_combine_stores, nir_var_all); |
| 211 |
|
| 212 |
NIR_PASS(progress, nir, nir_lower_alu_width, bi_vectorize_filter, &gpu_id); |
| 213 |
NIR_PASS(progress, nir, nir_opt_vectorize, bi_vectorize_filter, &gpu_id); |
| 214 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 215 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 216 |
NIR_PASS(progress, nir, nir_opt_cse); |
| 217 |
|
| 218 |
nir_opt_peephole_select_options peephole_select_options = { |
| 219 |
.limit = 64, |
| 220 |
.expensive_alu_ok = true, |
| 221 |
}; |
| 222 |
NIR_PASS(progress, nir, nir_opt_peephole_select, |
| 223 |
&peephole_select_options); |
| 224 |
NIR_PASS(progress, nir, nir_opt_idiv_const, 8); |
| 225 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 226 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 227 |
|
| 228 |
NIR_PASS(progress, nir, nir_opt_dead_cf); |
| 229 |
|
| 230 |
bool loop_progress = false; |
| 231 |
NIR_PASS(loop_progress, nir, nir_opt_loop); |
| 232 |
progress |= loop_progress; |
| 233 |
|
| 234 |
if (loop_progress) { |
| 235 |
/* If nir_opt_loop makes progress, then we need to clean things up |
| 236 |
* if we want any hope of nir_opt_if or nir_opt_loop_unroll to make |
| 237 |
* progress. |
| 238 |
*/ |
| 239 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 240 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 241 |
} |
| 242 |
|
| 243 |
/* XXX: On Bifrost (G52), this cause a failure on |
| 244 |
* "dEQP-VK.graphicsfuzz.spv-composite-phi" and is related to an unknown |
| 245 |
* scheduling issue */ |
| 246 |
if (pan_arch(gpu_id) >= 9) |
| 247 |
NIR_PASS( |
| 248 |
progress, nir, nir_opt_if, |
| 249 |
nir_opt_if_optimize_phi_true_false | nir_opt_if_avoid_64bit_phis); |
| 250 |
|
| 251 |
NIR_PASS(progress, nir, nir_opt_phi_to_bool); |
| 252 |
NIR_PASS(progress, nir, nir_opt_loop_unroll); |
| 253 |
NIR_PASS(progress, nir, nir_opt_remove_phis); |
| 254 |
NIR_PASS(progress, nir, nir_opt_undef); |
| 255 |
} while (progress); |
| 256 |
|
| 257 |
NIR_PASS(_, nir, nir_lower_undef_to_zero); |
| 258 |
|
| 259 |
NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_function_temp, NULL); |
| 260 |
} |
| 261 |
|
| 262 |
void |
| 263 |
bifrost_optimize_nir(nir_shader *nir, uint64_t gpu_id) |
| 264 |
{ |
| 265 |
bi_optimize_loop_nir(nir, gpu_id, true); |
| 266 |
} |
| 267 |
|
| 268 |
static void |
| 269 |
bi_optimize_nir(nir_shader *nir, uint64_t gpu_id, |
| 270 |
nir_variable_mode robust_modes) |
| 271 |
{ |
| 272 |
NIR_PASS(_, nir, nir_opt_shrink_stores, true); |
| 273 |
bi_optimize_loop_nir(nir, gpu_id, false); |
| 274 |
|
| 275 |
NIR_PASS(_, nir, nir_opt_shrink_vectors, false); |
| 276 |
|
| 277 |
nir_load_store_vectorize_options vectorize_opts = { |
| 278 |
.modes = nir_var_mem_global | |
| 279 |
nir_var_mem_shared | |
| 280 |
nir_var_mem_ubo | |
| 281 |
nir_var_shader_temp, |
| 282 |
.callback = mem_vectorize_cb, |
| 283 |
.robust_modes = robust_modes, |
| 284 |
}; |
| 285 |
|
| 286 |
/* Only allow vectorization of SSBOs when no robustness2 is configured */ |
| 287 |
if (!(robust_modes & nir_var_mem_ssbo)) |
| 288 |
vectorize_opts.modes |= nir_var_mem_ssbo; |
| 289 |
|
| 290 |
NIR_PASS(_, nir, nir_opt_load_store_vectorize, &vectorize_opts); |
| 291 |
|
| 292 |
/* nir_lower_pack can generate split operations, execute algebraic again to |
| 293 |
* handle them */ |
| 294 |
NIR_PASS(_, nir, nir_opt_algebraic); |
| 295 |
|
| 296 |
/* TODO: Why is 64-bit getting rematerialized? |
| 297 |
* KHR-GLES31.core.shader_image_load_store.basic-allTargets-atomicFS */ |
| 298 |
NIR_PASS(_, nir, nir_lower_int64); |
| 299 |
|
| 300 |
/* Algebraic can materialize instructions with a bit_size that we need to lower */ |
| 301 |
NIR_PASS(_, nir, nir_lower_bit_size, bi_lower_bit_size, &gpu_id); |
| 302 |
|
| 303 |
/* We need to cleanup after each iteration of late algebraic |
| 304 |
* optimizations, since otherwise NIR can produce weird edge cases |
| 305 |
* (like fneg of a constant) which we don't handle */ |
| 306 |
bool late_algebraic = true; |
| 307 |
while (late_algebraic) { |
| 308 |
late_algebraic = false; |
| 309 |
NIR_PASS(late_algebraic, nir, nir_opt_algebraic_late); |
| 310 |
NIR_PASS(_, nir, nir_lower_alu_width, bi_vectorize_filter, &gpu_id); |
| 311 |
NIR_PASS(_, nir, nir_opt_constant_folding); |
| 312 |
NIR_PASS(_, nir, nir_opt_copy_prop); |
| 313 |
NIR_PASS(_, nir, nir_opt_dce); |
| 314 |
NIR_PASS(_, nir, nir_opt_cse); |
| 315 |
} |
| 316 |
|
| 317 |
/* This opt currently helps on Bifrost but not Valhall */ |
| 318 |
if (pan_arch(gpu_id) < 9) |
| 319 |
NIR_PASS(_, nir, bifrost_nir_opt_boolean_bitwise); |
| 320 |
|
| 321 |
NIR_PASS(_, nir, pan_nir_lower_bool_to_bitsize); |
| 322 |
NIR_PASS(_, nir, nir_lower_alu_width, bi_vectorize_filter, &gpu_id); |
| 323 |
NIR_PASS(_, nir, nir_opt_vectorize, bi_vectorize_filter, &gpu_id); |
| 324 |
|
| 325 |
/* Prepass to simplify instruction selection */ |
| 326 |
bool late_algebraic_progress = true; |
| 327 |
while (late_algebraic_progress) { |
| 328 |
late_algebraic_progress = false; |
| 329 |
NIR_PASS(late_algebraic_progress, nir, bifrost_nir_lower_algebraic_late, |
| 330 |
pan_arch(gpu_id)); |
| 331 |
late_algebraic |= late_algebraic_progress; |
| 332 |
} |
| 333 |
|
| 334 |
while (late_algebraic) { |
| 335 |
late_algebraic = false; |
| 336 |
NIR_PASS(late_algebraic, nir, nir_opt_algebraic_late); |
| 337 |
NIR_PASS(_, nir, nir_lower_alu_width, bi_vectorize_filter, &gpu_id); |
| 338 |
NIR_PASS(_, nir, nir_opt_constant_folding); |
| 339 |
NIR_PASS(_, nir, nir_opt_copy_prop); |
| 340 |
NIR_PASS(_, nir, nir_opt_dce); |
| 341 |
NIR_PASS(_, nir, nir_opt_cse); |
| 342 |
} |
| 343 |
|
| 344 |
/* Backend scheduler is purely local, so do some global optimizations |
| 345 |
* to reduce register pressure. */ |
| 346 |
nir_move_options move_all = nir_move_const_undef | nir_move_load_ubo | |
| 347 |
nir_move_load_input | nir_move_load_frag_coord | |
| 348 |
nir_move_comparisons | nir_move_copies | |
| 349 |
nir_move_load_ssbo; |
| 350 |
|
| 351 |
NIR_PASS(_, nir, nir_opt_sink, move_all); |
| 352 |
NIR_PASS(_, nir, nir_opt_move, move_all); |
| 353 |
|
| 354 |
/* We might lower attribute, varying, and image indirects. Use the |
| 355 |
* gathered info to skip the extra analysis in the happy path. */ |
| 356 |
bool any_indirects = |
| 357 |
nir->info.inputs_read_indirectly || nir->info.outputs_read_indirectly || |
| 358 |
nir->info.outputs_written_indirectly || |
| 359 |
nir->info.patch_inputs_read_indirectly || |
| 360 |
nir->info.patch_outputs_read_indirectly || |
| 361 |
nir->info.patch_outputs_written_indirectly || nir->info.images_used[0]; |
| 362 |
|
| 363 |
if (any_indirects) { |
| 364 |
nir_divergence_analysis(nir); |
| 365 |
NIR_PASS(_, nir, bi_lower_divergent_indirects, |
| 366 |
pan_subgroup_size(pan_arch(gpu_id))); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
void |
| 371 |
bifrost_preprocess_nir(nir_shader *nir, uint64_t gpu_id) |
| 372 |
{ |
| 373 |
MESA_TRACE_FUNC(); |
| 374 |
|
| 375 |
/* The DISCARD instruction just flags the thread as discarded, but the |
| 376 |
* actual termination only happens when all threads in the quad are |
| 377 |
* discarded, or when an instruction with a .discard flow is |
| 378 |
* encountered (Valhall) or when a clause with a .terminate_discarded_thread |
| 379 |
* is reached (Bifrost). |
| 380 |
* We could do without nir_lower_terminate_to_demote(), but this allows |
| 381 |
* for extra dead-code elimination when code sections are detected as |
| 382 |
* being unused after a termination is crossed. |
| 383 |
*/ |
| 384 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) |
| 385 |
NIR_PASS(_, nir, nir_lower_terminate_to_demote); |
| 386 |
|
| 387 |
/* Ensure that halt are translated to returns and get ride of them */ |
| 388 |
NIR_PASS(_, nir, nir_lower_halt_to_return); |
| 389 |
NIR_PASS(_, nir, nir_lower_returns); |
| 390 |
|
| 391 |
/* Lower gl_Position pre-optimisation, but after lowering vars to ssa |
| 392 |
* (so we don't accidentally duplicate the epilogue since mesa/st has |
| 393 |
* messed with our I/O quite a bit already) */ |
| 394 |
|
| 395 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 396 |
|
| 397 |
if (nir->info.stage == MESA_SHADER_VERTEX) { |
| 398 |
if (pan_arch(gpu_id) <= 7) |
| 399 |
NIR_PASS(_, nir, pan_nir_lower_vertex_id); |
| 400 |
} |
| 401 |
|
| 402 |
/* Get rid of any global vars before we lower to scratch. */ |
| 403 |
NIR_PASS(_, nir, nir_lower_global_vars_to_local); |
| 404 |
|
| 405 |
/* Valhall introduces packed thread local storage, which improves cache |
| 406 |
* locality of TLS access. However, access to packed TLS cannot |
| 407 |
* straddle 16-byte boundaries. As such, when packed TLS is in use |
| 408 |
* (currently unconditional for Valhall), we force vec4 alignment for |
| 409 |
* scratch access. |
| 410 |
*/ |
| 411 |
glsl_type_size_align_func vars_to_scratch_size_align_func = |
| 412 |
(pan_arch(gpu_id) >= 9) ? glsl_get_vec4_size_align_bytes |
| 413 |
: glsl_get_natural_size_align_bytes; |
| 414 |
/* Lower large arrays to scratch and small arrays to bcsel */ |
| 415 |
NIR_PASS(_, nir, nir_lower_scratch_to_var); |
| 416 |
NIR_PASS(_, nir, nir_lower_vars_to_scratch, 256, |
| 417 |
vars_to_scratch_size_align_func, vars_to_scratch_size_align_func); |
| 418 |
NIR_PASS(_, nir, nir_lower_indirect_derefs_to_if_else_trees, |
| 419 |
nir_var_function_temp, ~0); |
| 420 |
|
| 421 |
NIR_PASS(_, nir, nir_split_var_copies); |
| 422 |
NIR_PASS(_, nir, nir_lower_var_copies); |
| 423 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 424 |
|
| 425 |
bi_optimize_loop_nir(nir, gpu_id, true); |
| 426 |
|
| 427 |
/* Lower away all variables for smaller shaders */ |
| 428 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 429 |
NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_function_temp, NULL); |
| 430 |
} |
| 431 |
|
| 432 |
/* |
| 433 |
* Build a bit mask of varyings (by location) that are flatshaded. This |
| 434 |
* information is needed by lower_mediump_io, as we don't yet support 16-bit |
| 435 |
* flat varyings. |
| 436 |
* |
| 437 |
* Also varyings that are used as texture coordinates should be kept at fp32 so |
| 438 |
* the texture instruction may be promoted to VAR_TEX. In general this is a good |
| 439 |
* idea, as fp16 texture coordinates are not supported by the hardware and are |
| 440 |
* usually inappropriate. (There are both relevant CTS bugs here, even.) |
| 441 |
* |
| 442 |
* TODO: If we compacted the varyings with some fixup code in the vertex shader, |
| 443 |
* we could implement 16-bit flat varyings. Consider if this case matters. |
| 444 |
* |
| 445 |
* TODO: The texture coordinate handling could be less heavyhanded. |
| 446 |
*/ |
| 447 |
static bool |
| 448 |
bi_gather_texcoords(nir_builder *b, nir_instr *instr, void *data) |
| 449 |
{ |
| 450 |
uint64_t *mask = data; |
| 451 |
|
| 452 |
if (instr->type != nir_instr_type_tex) |
| 453 |
return false; |
| 454 |
|
| 455 |
nir_tex_instr *tex = nir_instr_as_tex(instr); |
| 456 |
|
| 457 |
int coord_idx = nir_tex_instr_src_index(tex, nir_tex_src_coord); |
| 458 |
if (coord_idx < 0) |
| 459 |
return false; |
| 460 |
|
| 461 |
nir_src src = tex->src[coord_idx].src; |
| 462 |
nir_scalar x = nir_scalar_resolved(src.ssa, 0); |
| 463 |
nir_scalar y = nir_scalar_resolved(src.ssa, 1); |
| 464 |
|
| 465 |
if (x.def != y.def) |
| 466 |
return false; |
| 467 |
|
| 468 |
nir_instr *parent = nir_def_instr(x.def); |
| 469 |
|
| 470 |
if (parent->type != nir_instr_type_intrinsic) |
| 471 |
return false; |
| 472 |
|
| 473 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(parent); |
| 474 |
|
| 475 |
if (intr->intrinsic != nir_intrinsic_load_interpolated_input) |
| 476 |
return false; |
| 477 |
|
| 478 |
nir_io_semantics sem = nir_intrinsic_io_semantics(intr); |
| 479 |
*mask |= BITFIELD64_BIT(sem.location); |
| 480 |
return false; |
| 481 |
} |
| 482 |
|
| 483 |
static uint64_t |
| 484 |
bi_fp32_varying_mask(nir_shader *nir) |
| 485 |
{ |
| 486 |
uint64_t mask = 0; |
| 487 |
|
| 488 |
assert(nir->info.stage == MESA_SHADER_FRAGMENT); |
| 489 |
|
| 490 |
nir_foreach_shader_in_variable(var, nir) { |
| 491 |
if (var->data.interpolation == INTERP_MODE_FLAT) { |
| 492 |
unsigned slots = glsl_count_attribute_slots(var->type, false); |
| 493 |
mask |= BITFIELD64_RANGE(var->data.location, slots); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
nir_shader_instructions_pass(nir, bi_gather_texcoords, nir_metadata_all, |
| 498 |
&mask); |
| 499 |
|
| 500 |
return mask; |
| 501 |
} |
| 502 |
|
| 503 |
static bool |
| 504 |
bi_lower_subgroups(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 505 |
{ |
| 506 |
uint64_t gpu_id = *(uint64_t *)data; |
| 507 |
unsigned int arch = pan_arch(gpu_id); |
| 508 |
|
| 509 |
b->cursor = nir_before_instr(&intr->instr); |
| 510 |
|
| 511 |
nir_def *val = NULL; |
| 512 |
switch (intr->intrinsic) { |
| 513 |
case nir_intrinsic_vote_any: |
| 514 |
val = nir_ine_imm(b, nir_ballot(b, 1, 32, intr->src[0].ssa), 0); |
| 515 |
break; |
| 516 |
|
| 517 |
case nir_intrinsic_vote_all: |
| 518 |
val = nir_ieq_imm(b, nir_ballot(b, 1, 32, nir_inot(b, intr->src[0].ssa)), 0); |
| 519 |
break; |
| 520 |
|
| 521 |
case nir_intrinsic_load_subgroup_id: { |
| 522 |
nir_def *local_id = nir_load_local_invocation_id(b); |
| 523 |
nir_def *local_size = nir_load_workgroup_size(b); |
| 524 |
/* local_id.x + local_size.x * (local_id.y + local_size.y * local_id.z) */ |
| 525 |
nir_def *flat_local_id = |
| 526 |
nir_iadd(b, |
| 527 |
nir_channel(b, local_id, 0), |
| 528 |
nir_imul(b, |
| 529 |
nir_channel(b, local_size, 0), |
| 530 |
nir_iadd(b, |
| 531 |
nir_channel(b, local_id, 1), |
| 532 |
nir_imul(b, |
| 533 |
nir_channel(b, local_size, 1), |
| 534 |
nir_channel(b, local_id, 2))))); |
| 535 |
/* |
| 536 |
* nir_udiv_imm with a power of two divisor, which pan_subgroup_size is, |
| 537 |
* will construct a right shift instead of an udiv. |
| 538 |
*/ |
| 539 |
val = nir_udiv_imm(b, flat_local_id, pan_subgroup_size(arch)); |
| 540 |
break; |
| 541 |
} |
| 542 |
|
| 543 |
case nir_intrinsic_load_subgroup_size: |
| 544 |
val = nir_imm_int(b, pan_subgroup_size(arch)); |
| 545 |
break; |
| 546 |
|
| 547 |
case nir_intrinsic_load_num_subgroups: { |
| 548 |
uint32_t subgroup_size = pan_subgroup_size(arch); |
| 549 |
assert(!b->shader->info.workgroup_size_variable); |
| 550 |
uint32_t workgroup_size = |
| 551 |
b->shader->info.workgroup_size[0] * |
| 552 |
b->shader->info.workgroup_size[1] * |
| 553 |
b->shader->info.workgroup_size[2]; |
| 554 |
uint32_t num_subgroups = DIV_ROUND_UP(workgroup_size, subgroup_size); |
| 555 |
val = nir_imm_int(b, num_subgroups); |
| 556 |
break; |
| 557 |
} |
| 558 |
|
| 559 |
default: |
| 560 |
return false; |
| 561 |
} |
| 562 |
|
| 563 |
nir_def_rewrite_uses(&intr->def, val); |
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
/* Workgroups may be merged if the structure of the workgroup is not software |
| 568 |
* visible. This is true if neither shared memory nor BARRIER instructions nor |
| 569 |
* subgroups are used. The hardware may be able to optimize compute shaders |
| 570 |
* that set this flag. |
| 571 |
* |
| 572 |
* From the vulkan spec version 1.4.317 section 9.25.8: |
| 573 |
* |
| 574 |
* "For shaders that have defined workgroups, each invocation in a subgroup |
| 575 |
* must be in the same local workgroup." |
| 576 |
*/ |
| 577 |
bool |
| 578 |
valhall_can_merge_workgroups(nir_shader *nir) |
| 579 |
{ |
| 580 |
if (nir->info.shared_size != 0) |
| 581 |
return false; |
| 582 |
|
| 583 |
nir_foreach_function_impl(impl, nir) { |
| 584 |
nir_foreach_block(block, impl) { |
| 585 |
nir_foreach_instr(instr, block) { |
| 586 |
if (instr->type != nir_instr_type_intrinsic) |
| 587 |
continue; |
| 588 |
|
| 589 |
/* We only emit BARRIER instructions for workgroup execution |
| 590 |
* barriers. For subgroup execution barriers, the only consequence |
| 591 |
* of merging workgroups is that the scope may be larger, which is |
| 592 |
* allowed. */ |
| 593 |
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); |
| 594 |
if (intrin->intrinsic == nir_intrinsic_barrier && |
| 595 |
nir_intrinsic_execution_scope(intrin) == SCOPE_WORKGROUP) |
| 596 |
return false; |
| 597 |
|
| 598 |
/* This is in nir->info.uses_wide_subgroups, but we don't want to |
| 599 |
* force an extra nir_gather_shader_info call. */ |
| 600 |
if (nir_intrinsic_has_semantic(intrin, NIR_INTRINSIC_SUBGROUP)) |
| 601 |
return false; |
| 602 |
|
| 603 |
/* load_subgroup_invocation allows observing merged workgroups |
| 604 |
* because the first thread in the workgroup may have a nonzero |
| 605 |
* subgroup invocation and so on. We don't have to care about |
| 606 |
* load_subgroup_id, because we implement it by dividing the local |
| 607 |
* invocation id, so it doesn't care what the actual subgroup |
| 608 |
* layout is in hw. |
| 609 |
* |
| 610 |
* Note that these intrinsics do not have NIR_INTRINSIC_SUBGROUP |
| 611 |
* because they do not perform any communication with other |
| 612 |
* subgroup threads. */ |
| 613 |
if (intrin->intrinsic == nir_intrinsic_load_subgroup_invocation) |
| 614 |
return false; |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
return true; |
| 620 |
} |
| 621 |
|
| 622 |
static bool |
| 623 |
bi_lower_load_output(nir_builder *b, nir_intrinsic_instr *intr, |
| 624 |
UNUSED void *data) |
| 625 |
{ |
| 626 |
if (intr->intrinsic != nir_intrinsic_load_output) |
| 627 |
return false; |
| 628 |
|
| 629 |
unsigned loc = nir_intrinsic_io_semantics(intr).location; |
| 630 |
assert(loc >= FRAG_RESULT_DATA0); |
| 631 |
unsigned rt = loc - FRAG_RESULT_DATA0; |
| 632 |
|
| 633 |
b->cursor = nir_before_instr(&intr->instr); |
| 634 |
|
| 635 |
nir_def *conversion = nir_load_rt_conversion_pan( |
| 636 |
b, .base = rt, .src_type = nir_intrinsic_dest_type(intr)); |
| 637 |
|
| 638 |
nir_def *lowered = nir_load_tile_pan( |
| 639 |
b, intr->def.num_components, intr->def.bit_size, |
| 640 |
pan_nir_tile_location_sample(b, loc, nir_imm_int(b, 0)), |
| 641 |
pan_nir_tile_default_coverage(b), |
| 642 |
conversion, .dest_type = nir_intrinsic_dest_type(intr), |
| 643 |
.io_semantics = nir_intrinsic_io_semantics(intr)); |
| 644 |
|
| 645 |
nir_def_rewrite_uses(&intr->def, lowered); |
| 646 |
return true; |
| 647 |
} |
| 648 |
|
| 649 |
static bool |
| 650 |
bifrost_nir_lower_load_output(nir_shader *nir) |
| 651 |
{ |
| 652 |
assert(nir->info.stage == MESA_SHADER_FRAGMENT); |
| 653 |
|
| 654 |
return nir_shader_intrinsics_pass( |
| 655 |
nir, bi_lower_load_output, |
| 656 |
nir_metadata_control_flow, NULL); |
| 657 |
} |
| 658 |
|
| 659 |
static nir_mem_access_size_align |
| 660 |
mem_access_size_align_cb(nir_intrinsic_op intrin, uint8_t bytes, |
| 661 |
uint8_t bit_size, uint32_t align_mul, |
| 662 |
uint32_t align_offset, bool offset_is_const, |
| 663 |
enum gl_access_qualifier access, const void *cb_data) |
| 664 |
{ |
| 665 |
uint32_t align = nir_combined_align(align_mul, align_offset); |
| 666 |
assert(util_is_power_of_two_nonzero(align)); |
| 667 |
|
| 668 |
/* No more than 16 bytes at a time. */ |
| 669 |
bytes = MIN2(bytes, 16); |
| 670 |
|
| 671 |
/* All loads must be aligned up to the next power of two of their byte |
| 672 |
* size. If we have insufficient alignment, split into smaller loads. */ |
| 673 |
unsigned required_align = util_next_power_of_two(bytes); |
| 674 |
if (align < required_align) { |
| 675 |
bytes = align; |
| 676 |
required_align = bytes; |
| 677 |
} |
| 678 |
|
| 679 |
/* If the number of bytes is a multiple of 4, use 32-bit loads. Else if it's |
| 680 |
* a multiple of 2, use 16-bit loads. Else use 8-bit loads. |
| 681 |
* |
| 682 |
* But if we're only aligned to 1 byte, use 8-bit loads. If we're only |
| 683 |
* aligned to 2 bytes, use 16-bit loads, unless we needed 8-bit loads due to |
| 684 |
* the size. |
| 685 |
*/ |
| 686 |
if ((bytes & 1) || (align == 1)) |
| 687 |
bit_size = 8; |
| 688 |
else if ((bytes & 2) || (align == 2)) |
| 689 |
bit_size = 16; |
| 690 |
else if (bit_size >= 32) |
| 691 |
bit_size = 32; |
| 692 |
|
| 693 |
unsigned num_comps = MIN2(bytes / (bit_size / 8), 4); |
| 694 |
|
| 695 |
/* Push constants require 32-bit loads. */ |
| 696 |
if (intrin == nir_intrinsic_load_push_constant) { |
| 697 |
if (align_mul >= 4) { |
| 698 |
/* If align_mul is bigger than 4 we can use align_offset to find |
| 699 |
* the exact number of words we need to read. |
| 700 |
*/ |
| 701 |
num_comps = DIV_ROUND_UP((align_offset % 4) + bytes, 4); |
| 702 |
} else { |
| 703 |
/* If bytes is aligned on 32-bit, the access might still cross one |
| 704 |
* word at the beginning, and one word at the end. If bytes is not |
| 705 |
* aligned on 32-bit, the extra two words should cover for both the |
| 706 |
* size and offset mis-alignment. |
| 707 |
*/ |
| 708 |
num_comps = (bytes / 4) + 2; |
| 709 |
} |
| 710 |
|
| 711 |
bit_size = MAX2(bit_size, 32); |
| 712 |
required_align = 4; |
| 713 |
} |
| 714 |
|
| 715 |
return (nir_mem_access_size_align){ |
| 716 |
.num_components = num_comps, |
| 717 |
.bit_size = bit_size, |
| 718 |
.align = required_align, |
| 719 |
.shift = nir_mem_access_shift_method_scalar, |
| 720 |
}; |
| 721 |
} |
| 722 |
|
| 723 |
static void bi_lower_texture_nir(nir_shader *nir, uint64_t gpu_id); |
| 724 |
static void bi_lower_texture_late_nir(nir_shader *nir, uint64_t gpu_id); |
| 725 |
|
| 726 |
void |
| 727 |
bifrost_postprocess_nir(nir_shader *nir, uint64_t gpu_id) |
| 728 |
{ |
| 729 |
MESA_TRACE_FUNC(); |
| 730 |
|
| 731 |
/* We assume that UBO and SSBO were lowered, let's move things around. */ |
| 732 |
nir_move_options move_all = nir_move_const_undef | nir_move_load_ubo | |
| 733 |
nir_move_comparisons | nir_move_copies | |
| 734 |
nir_move_load_ssbo; |
| 735 |
|
| 736 |
NIR_PASS(_, nir, nir_opt_sink, move_all); |
| 737 |
NIR_PASS(_, nir, nir_opt_move, move_all); |
| 738 |
|
| 739 |
bi_lower_texture_nir(nir, gpu_id); |
| 740 |
|
| 741 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 742 |
NIR_PASS(_, nir, pan_nir_lower_noperspective_fs); |
| 743 |
|
| 744 |
NIR_PASS(_, nir, nir_lower_mediump_io, |
| 745 |
nir_var_shader_in | nir_var_shader_out, |
| 746 |
~bi_fp32_varying_mask(nir), false); |
| 747 |
|
| 748 |
NIR_PASS(_, nir, bifrost_nir_lower_load_output); |
| 749 |
} else if (nir->info.stage == MESA_SHADER_VERTEX) { |
| 750 |
NIR_PASS(_, nir, nir_lower_viewport_transform); |
| 751 |
NIR_PASS(_, nir, nir_lower_point_size, 1.0, 0.0); |
| 752 |
NIR_PASS(_, nir, pan_nir_lower_noperspective_vs); |
| 753 |
} |
| 754 |
|
| 755 |
nir_lower_mem_access_bit_sizes_options mem_size_options = { |
| 756 |
.modes = nir_var_mem_ubo | nir_var_mem_push_const | nir_var_mem_ssbo | |
| 757 |
nir_var_mem_constant | nir_var_mem_task_payload | |
| 758 |
nir_var_shader_temp | nir_var_function_temp | |
| 759 |
nir_var_mem_global | nir_var_mem_shared, |
| 760 |
.callback = mem_access_size_align_cb, |
| 761 |
}; |
| 762 |
NIR_PASS(_, nir, nir_lower_mem_access_bit_sizes, &mem_size_options); |
| 763 |
|
| 764 |
nir_lower_ssbo_options ssbo_opts = { |
| 765 |
.native_loads = pan_arch(gpu_id) >= 9, |
| 766 |
.native_offset = pan_arch(gpu_id) >= 9, |
| 767 |
}; |
| 768 |
NIR_PASS(_, nir, nir_lower_ssbo, &ssbo_opts); |
| 769 |
|
| 770 |
/* |
| 771 |
* Lower subgroups ops before lowering int64: nir_lower_int64 doesn't know |
| 772 |
* how to lower imul reductions and scans. |
| 773 |
* |
| 774 |
* TODO: we can implement certain operations (notably reductions, scans, |
| 775 |
* certain shuffles, etc) more efficiently than nir_lower_subgroups. Moreover |
| 776 |
* we can implement reductions and scans on f16vec2 values without splitting |
| 777 |
* to scalar first. |
| 778 |
*/ |
| 779 |
bool lower_subgroups_progress = false; |
| 780 |
NIR_PASS(lower_subgroups_progress, nir, nir_lower_subgroups, |
| 781 |
&(nir_lower_subgroups_options) { |
| 782 |
.subgroup_size = pan_subgroup_size(pan_arch(gpu_id)), |
| 783 |
.ballot_bit_size = 32, |
| 784 |
.ballot_components = 1, |
| 785 |
.lower_to_scalar = true, |
| 786 |
.lower_vote_feq = true, |
| 787 |
.lower_vote_ieq = true, |
| 788 |
.lower_vote_bool_eq = true, |
| 789 |
.lower_first_invocation_to_ballot = true, |
| 790 |
.lower_read_first_invocation = true, |
| 791 |
.lower_subgroup_masks = true, |
| 792 |
.lower_relative_shuffle = true, |
| 793 |
.lower_shuffle = true, |
| 794 |
.lower_quad = true, |
| 795 |
.lower_quad_broadcast_dynamic = true, |
| 796 |
.lower_quad_vote = true, |
| 797 |
.lower_elect = true, |
| 798 |
.lower_rotate_to_shuffle = true, |
| 799 |
.lower_rotate_clustered_to_shuffle = true, |
| 800 |
.lower_inverse_ballot = true, |
| 801 |
.lower_reduce = true, |
| 802 |
.lower_boolean_reduce = true, |
| 803 |
.lower_boolean_shuffle = true, |
| 804 |
}); |
| 805 |
/* nir_lower_subgroups creates new vars, clean them up. */ |
| 806 |
if (lower_subgroups_progress) |
| 807 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 808 |
|
| 809 |
NIR_PASS(_, nir, nir_shader_intrinsics_pass, bi_lower_subgroups, |
| 810 |
nir_metadata_control_flow, &gpu_id); |
| 811 |
|
| 812 |
NIR_PASS(_, nir, nir_lower_64bit_phis); |
| 813 |
NIR_PASS(_, nir, nir_lower_int64); |
| 814 |
NIR_PASS(_, nir, nir_lower_bit_size, bi_lower_bit_size, &gpu_id); |
| 815 |
|
| 816 |
NIR_PASS(_, nir, nir_opt_idiv_const, 8); |
| 817 |
NIR_PASS(_, nir, nir_lower_idiv, |
| 818 |
&(nir_lower_idiv_options){.allow_fp16 = true}); |
| 819 |
|
| 820 |
NIR_PASS(_, nir, nir_lower_alu_width, bi_vectorize_filter, &gpu_id); |
| 821 |
NIR_PASS(_, nir, nir_lower_load_const_to_scalar); |
| 822 |
NIR_PASS(_, nir, nir_lower_phis_to_scalar, bi_vectorize_filter, &gpu_id); |
| 823 |
NIR_PASS(_, nir, nir_lower_flrp, 16 | 32 | 64, false /* always_precise */); |
| 824 |
NIR_PASS(_, nir, nir_lower_var_copies); |
| 825 |
NIR_PASS(_, nir, nir_lower_alu); |
| 826 |
NIR_PASS(_, nir, nir_lower_frag_coord_to_pixel_coord); |
| 827 |
NIR_PASS(_, nir, pan_nir_lower_var_special_pan); |
| 828 |
|
| 829 |
bi_lower_texture_late_nir(nir, gpu_id); |
| 830 |
} |
| 831 |
|
| 832 |
static void |
| 833 |
bi_lower_texture_nir(nir_shader *nir, uint64_t gpu_id) |
| 834 |
{ |
| 835 |
NIR_PASS(_, nir, nir_lower_image_atomics_to_global, NULL, NULL); |
| 836 |
|
| 837 |
/* on Bifrost, lower MSAA load/stores to 3D load/stores */ |
| 838 |
if (pan_arch(gpu_id) < 9) |
| 839 |
NIR_PASS(_, nir, pan_nir_lower_image_ms); |
| 840 |
|
| 841 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 842 |
NIR_PASS(_, nir, nir_lower_is_helper_invocation); |
| 843 |
NIR_PASS(_, nir, pan_nir_lower_helper_invocation); |
| 844 |
NIR_PASS(_, nir, pan_nir_lower_sample_pos); |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
static bool |
| 849 |
lower_texel_buffer_fetch(nir_builder *b, nir_tex_instr *tex, void *data) |
| 850 |
{ |
| 851 |
if (tex->op != nir_texop_txf || tex->sampler_dim != GLSL_SAMPLER_DIM_BUF) |
| 852 |
return false; |
| 853 |
|
| 854 |
unsigned *arch = data; |
| 855 |
b->cursor = nir_before_instr(&tex->instr); |
| 856 |
|
| 857 |
nir_def *res_handle = nir_imm_int(b, tex->texture_index); |
| 858 |
nir_def *buf_index = NULL; |
| 859 |
for (unsigned i = 0; i < tex->num_srcs; ++i) { |
| 860 |
switch (tex->src[i].src_type) { |
| 861 |
case nir_tex_src_coord: |
| 862 |
buf_index = tex->src[i].src.ssa; |
| 863 |
break; |
| 864 |
case nir_tex_src_texture_offset: |
| 865 |
/* This should always be 0 as lower_index_to_offset is expected to be |
| 866 |
* set */ |
| 867 |
assert(tex->texture_index == 0); |
| 868 |
res_handle = tex->src[i].src.ssa; |
| 869 |
break; |
| 870 |
default: |
| 871 |
continue; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
nir_def *loaded_texel_addr = |
| 876 |
nir_load_texel_buf_index_address_pan(b, res_handle, buf_index); |
| 877 |
nir_def *texel_addr = |
| 878 |
nir_pack_64_2x32(b, nir_channels(b, loaded_texel_addr, BITFIELD_MASK(2))); |
| 879 |
|
| 880 |
nir_def *loaded_mem; |
| 881 |
if (*arch >= 9) { |
| 882 |
nir_def *icd = nir_load_texel_buf_conv_pan(b, res_handle); |
| 883 |
loaded_mem = nir_load_global_cvt_pan(b, tex->def.num_components, |
| 884 |
tex->def.bit_size, texel_addr, |
| 885 |
icd, tex->dest_type); |
| 886 |
} else { |
| 887 |
nir_def *icd = nir_channel(b, loaded_texel_addr, 2); |
| 888 |
loaded_mem = nir_load_global_cvt_pan(b, tex->def.num_components, |
| 889 |
tex->def.bit_size, texel_addr, |
| 890 |
icd, tex->dest_type); |
| 891 |
} |
| 892 |
nir_def_replace(&tex->def, loaded_mem); |
| 893 |
return true; |
| 894 |
} |
| 895 |
|
| 896 |
static bool |
| 897 |
pan_nir_lower_texel_buffer_fetch(nir_shader *shader, unsigned arch) |
| 898 |
{ |
| 899 |
return nir_shader_tex_pass(shader, lower_texel_buffer_fetch, |
| 900 |
nir_metadata_control_flow, &arch); |
| 901 |
} |
| 902 |
|
| 903 |
static bool |
| 904 |
lower_buf_image_access(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 905 |
{ |
| 906 |
switch (intr->intrinsic) { |
| 907 |
case nir_intrinsic_image_texel_address: |
| 908 |
case nir_intrinsic_image_load: |
| 909 |
case nir_intrinsic_image_store: |
| 910 |
break; |
| 911 |
default: |
| 912 |
return false; |
| 913 |
} |
| 914 |
enum glsl_sampler_dim dim = nir_intrinsic_image_dim(intr); |
| 915 |
if (dim != GLSL_SAMPLER_DIM_BUF) |
| 916 |
return false; |
| 917 |
|
| 918 |
unsigned *arch = data; |
| 919 |
b->cursor = nir_before_instr(&intr->instr); |
| 920 |
|
| 921 |
nir_def *res_handle = intr->src[0].ssa; |
| 922 |
nir_def *buf_index = nir_channel(b, intr->src[1].ssa, 0); |
| 923 |
nir_def *loaded_texel_addr = |
| 924 |
nir_load_texel_buf_index_address_pan(b, res_handle, buf_index); |
| 925 |
nir_def *texel_addr = |
| 926 |
nir_pack_64_2x32(b, nir_channels(b, loaded_texel_addr, BITFIELD_MASK(2))); |
| 927 |
|
| 928 |
switch (intr->intrinsic) { |
| 929 |
case nir_intrinsic_image_texel_address: |
| 930 |
nir_def_replace(&intr->def, texel_addr); |
| 931 |
break; |
| 932 |
case nir_intrinsic_image_load: { |
| 933 |
nir_def *icd; |
| 934 |
if (*arch >= 9) |
| 935 |
icd = nir_load_texel_buf_conv_pan(b, res_handle); |
| 936 |
else |
| 937 |
icd = nir_channel(b, loaded_texel_addr, 2); |
| 938 |
nir_def *loaded_mem = nir_load_global_cvt_pan( |
| 939 |
b, intr->def.num_components, intr->def.bit_size, texel_addr, icd, |
| 940 |
.dest_type = nir_intrinsic_dest_type(intr)); |
| 941 |
nir_def_replace(&intr->def, loaded_mem); |
| 942 |
break; |
| 943 |
} |
| 944 |
case nir_intrinsic_image_store: { |
| 945 |
/* Due to SPIR-V limitations, the source type is not fully reliable: it |
| 946 |
* reports uint32 even for write_imagei. This causes an incorrect |
| 947 |
* u32->s32->u32 roundtrip which incurs an unwanted clamping. Use auto32 |
| 948 |
* instead, which will match per the OpenCL spec. Of course this does |
| 949 |
* not work for 16-bit stores, but those are not available in OpenCL. |
| 950 |
*/ |
| 951 |
ASSERTED nir_alu_type T = nir_intrinsic_src_type(intr); |
| 952 |
assert(nir_alu_type_get_type_size(T) == 32); |
| 953 |
|
| 954 |
nir_def *value = intr->src[3].ssa; |
| 955 |
nir_def *icd; |
| 956 |
if (*arch >= 9) |
| 957 |
icd = nir_load_texel_buf_conv_pan(b, res_handle); |
| 958 |
else |
| 959 |
icd = nir_channel(b, loaded_texel_addr, 2); |
| 960 |
nir_store_global_cvt_pan(b, value, texel_addr, icd, .src_type = 32); |
| 961 |
nir_instr_remove(&intr->instr); |
| 962 |
break; |
| 963 |
} |
| 964 |
default: |
| 965 |
UNREACHABLE("Unexpected intrinsic"); |
| 966 |
} |
| 967 |
|
| 968 |
return true; |
| 969 |
} |
| 970 |
|
| 971 |
static bool |
| 972 |
pan_nir_lower_buf_image_access(nir_shader *shader, unsigned arch) |
| 973 |
{ |
| 974 |
return nir_shader_intrinsics_pass(shader, lower_buf_image_access, |
| 975 |
nir_metadata_control_flow, &arch); |
| 976 |
} |
| 977 |
|
| 978 |
/* This must be called after any lowering of resource indices |
| 979 |
* (panfrost_nir_lower_res_indices / panvk_per_arch(nir_lower_descriptors)) |
| 980 |
* and lowering of attribute indices (pan_nir_lower_image_index / |
| 981 |
* pan_nir_lower_texel_buffer_fetch_index) |
| 982 |
*/ |
| 983 |
static void |
| 984 |
bi_lower_texture_late_nir(nir_shader *nir, uint64_t gpu_id) |
| 985 |
{ |
| 986 |
NIR_PASS(_, nir, pan_nir_lower_texel_buffer_fetch, pan_arch(gpu_id)); |
| 987 |
NIR_PASS(_, nir, pan_nir_lower_buf_image_access, pan_arch(gpu_id)); |
| 988 |
} |
| 989 |
|
| 990 |
/* Decide if Index-Driven Vertex Shading should be used for a given shader */ |
| 991 |
static bool |
| 992 |
bi_should_idvs(nir_shader *nir, const struct pan_compile_inputs *inputs) |
| 993 |
{ |
| 994 |
/* Opt-out */ |
| 995 |
if (inputs->no_idvs || bifrost_debug & BIFROST_DBG_NOIDVS) |
| 996 |
return false; |
| 997 |
|
| 998 |
/* IDVS splits up vertex shaders, not defined on other shader stages */ |
| 999 |
if (nir->info.stage != MESA_SHADER_VERTEX) |
| 1000 |
return false; |
| 1001 |
|
| 1002 |
/* Bifrost cannot write gl_PointSize during IDVS */ |
| 1003 |
if ((pan_arch(inputs->gpu_id) < 9) && |
| 1004 |
nir->info.outputs_written & VARYING_BIT_PSIZ) |
| 1005 |
return false; |
| 1006 |
|
| 1007 |
/* Otherwise, IDVS is usually better */ |
| 1008 |
return true; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/* Atomics and memory write on the vertex stage have implementation-defined |
| 1012 |
* behaviors on how many invocations will happen. However for some reasons, |
| 1013 |
* atomic counters on GL/GLES specs are quite ambigous here and even have tests |
| 1014 |
* counting how many invocations have been made on VS.... This pass detects |
| 1015 |
* atomics that result in a direct store output of one specific IDVS stage |
| 1016 |
* and ensure it's only executed for said stage. |
| 1017 |
* |
| 1018 |
* This allows |
| 1019 |
* "dEQP-GLES31.functional.shaders.opaque_type_indexing.atomic_counter.*" to |
| 1020 |
* pass under ANGLE. |
| 1021 |
*/ |
| 1022 |
|
| 1023 |
static bool |
| 1024 |
bifrost_nir_lower_vs_atomics_impl(nir_builder *b, nir_intrinsic_instr *intr, |
| 1025 |
UNUSED void *data) |
| 1026 |
{ |
| 1027 |
if (intr->intrinsic != nir_intrinsic_global_atomic) |
| 1028 |
return false; |
| 1029 |
|
| 1030 |
unsigned output_mask = 0; |
| 1031 |
nir_foreach_use(use, &intr->def) { |
| 1032 |
nir_instr *parent = nir_src_parent_instr(use); |
| 1033 |
if (parent->type != nir_instr_type_intrinsic) |
| 1034 |
continue; |
| 1035 |
|
| 1036 |
nir_intrinsic_instr *parent_intr = nir_instr_as_intrinsic(parent); |
| 1037 |
if (parent_intr->intrinsic != nir_intrinsic_store_output && |
| 1038 |
parent_intr->intrinsic != nir_intrinsic_store_per_view_output) |
| 1039 |
continue; |
| 1040 |
|
| 1041 |
nir_io_semantics sem = nir_intrinsic_io_semantics(parent_intr); |
| 1042 |
output_mask |= BITFIELD_BIT(va_shader_output_from_loc(sem.location)); |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* In case they are not written to any outputs, we default to only output in |
| 1046 |
* the position stage */ |
| 1047 |
if (output_mask == 0) |
| 1048 |
output_mask |= VA_SHADER_OUTPUT_POSITION_BIT; |
| 1049 |
|
| 1050 |
/* In case they are not written to both IDVS stages, we just do not try |
| 1051 |
* lowering it */ |
| 1052 |
if (((output_mask & VA_SHADER_OUTPUT_VARY_BIT) && |
| 1053 |
(output_mask & (VA_SHADER_OUTPUT_POSITION_BIT | |
| 1054 |
VA_SHADER_OUTPUT_ATTRIB_BIT)))) |
| 1055 |
return false; |
| 1056 |
|
| 1057 |
/* In case we know we have only outputs to a certain type, we can make the |
| 1058 |
* atomic exclusive to this */ |
| 1059 |
b->cursor = nir_before_instr(&intr->instr); |
| 1060 |
nir_def *res = nir_undef(b, intr->def.num_components, intr->def.bit_size); |
| 1061 |
|
| 1062 |
nir_def *shader_output = nir_load_shader_output_pan(b); |
| 1063 |
nir_push_if(b, nir_i2b(b, nir_iand_imm(b, shader_output, output_mask))); |
| 1064 |
nir_instr *new_instr = nir_instr_clone(b->shader, &intr->instr); |
| 1065 |
nir_intrinsic_instr *new_intr = nir_instr_as_intrinsic(new_instr); |
| 1066 |
nir_builder_instr_insert(b, new_instr); |
| 1067 |
nir_pop_if(b, NULL); |
| 1068 |
|
| 1069 |
res = nir_if_phi(b, &new_intr->def, res); |
| 1070 |
nir_def_replace(&intr->def, res); |
| 1071 |
|
| 1072 |
return true; |
| 1073 |
} |
| 1074 |
|
| 1075 |
static bool |
| 1076 |
bifrost_nir_lower_vs_atomics(nir_shader *shader) |
| 1077 |
{ |
| 1078 |
assert(shader->info.stage == MESA_SHADER_VERTEX); |
| 1079 |
return nir_shader_intrinsics_pass(shader, bifrost_nir_lower_vs_atomics_impl, |
| 1080 |
nir_metadata_none, NULL); |
| 1081 |
} |
| 1082 |
|
| 1083 |
void |
| 1084 |
bifrost_compile_shader_nir(nir_shader *nir, |
| 1085 |
const struct pan_compile_inputs *inputs, |
| 1086 |
struct util_dynarray *binary, |
| 1087 |
struct pan_shader_info *info) |
| 1088 |
{ |
| 1089 |
MESA_TRACE_FUNC(); |
| 1090 |
|
| 1091 |
bifrost_init_debug_options(); |
| 1092 |
|
| 1093 |
/* The varying layout (if any) may have different bit sizes for some |
| 1094 |
* varyings than we have in the shader. For descriptors, this isn't a |
| 1095 |
* problem as it's handled by the descriptor layout. However, for direct |
| 1096 |
* loads and stores on Valhall+, we need the right bit sizes in the shader. |
| 1097 |
* We could do this in the back-end as we emit but it's easier for now to |
| 1098 |
* lower in NIR. This also handles the case where we do a load from the |
| 1099 |
* fragment shader of something that isn't written by the vertex shader. |
| 1100 |
* In that case, we just return zero. |
| 1101 |
*/ |
| 1102 |
if (pan_arch(inputs->gpu_id) >= 9 && inputs->varying_layout) |
| 1103 |
NIR_PASS(_, nir, pan_nir_resize_varying_io, inputs->varying_layout); |
| 1104 |
|
| 1105 |
if (nir->info.stage == MESA_SHADER_VERTEX) { |
| 1106 |
info->vs.idvs = bi_should_idvs(nir, inputs); |
| 1107 |
|
| 1108 |
if (info->vs.idvs && nir->info.writes_memory) |
| 1109 |
NIR_PASS(_, nir, bifrost_nir_lower_vs_atomics); |
| 1110 |
|
| 1111 |
NIR_PASS(_, nir, pan_nir_lower_vs_outputs, inputs->gpu_id, |
| 1112 |
inputs->varying_layout, info->vs.idvs, |
| 1113 |
&info->vs.needs_extended_fifo); |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 1117 |
/* Blit shaders may not need to run ATEST, since ATEST is not needed if |
| 1118 |
* early-z is forced, alpha-to-coverage is disabled, and there are no |
| 1119 |
* writes to the coverage mask. The latter two are satisfied for all |
| 1120 |
* blit shaders, so we just care about early-z, which blit shaders force |
| 1121 |
* iff they do not write depth or stencil |
| 1122 |
*/ |
| 1123 |
const bool emit_zs = |
| 1124 |
nir->info.outputs_written & (BITFIELD_BIT(FRAG_RESULT_DEPTH) | |
| 1125 |
BITFIELD_BIT(FRAG_RESULT_STENCIL)); |
| 1126 |
const bool skip_atest = inputs->is_blit && !emit_zs; |
| 1127 |
NIR_PASS(_, nir, pan_nir_lower_fs_outputs, skip_atest); |
| 1128 |
} |
| 1129 |
|
| 1130 |
bi_optimize_nir(nir, inputs->gpu_id, inputs->robust_modes); |
| 1131 |
|
| 1132 |
/* Lower constants to scalar but then immediately fold so we get minimum- |
| 1133 |
* width vectors instead of scalars |
| 1134 |
*/ |
| 1135 |
NIR_PASS(_, nir, nir_lower_load_const_to_scalar); |
| 1136 |
NIR_PASS(_, nir, nir_opt_constant_folding); |
| 1137 |
uint64_t gpu_id = inputs->gpu_id; |
| 1138 |
NIR_PASS(_, nir, nir_lower_phis_to_scalar, bi_vectorize_filter, &gpu_id); |
| 1139 |
NIR_PASS(_, nir, nir_opt_copy_prop); |
| 1140 |
NIR_PASS(_, nir, nir_opt_dce); |
| 1141 |
|
| 1142 |
info->tls_size = nir->scratch_size; |
| 1143 |
info->stage = nir->info.stage; |
| 1144 |
|
| 1145 |
if (nir->info.stage == MESA_SHADER_VERTEX) { |
| 1146 |
assert(inputs->varying_layout); |
| 1147 |
memcpy(&info->varyings.formats, inputs->varying_layout, |
| 1148 |
sizeof(*inputs->varying_layout)); |
| 1149 |
} else if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 1150 |
pan_varying_collect_formats(&info->varyings.formats, |
| 1151 |
nir, inputs->gpu_id, |
| 1152 |
inputs->trust_varying_flat_highp_types, false); |
| 1153 |
info->varyings.noperspective = |
| 1154 |
pan_nir_collect_noperspective_varyings_fs(nir); |
| 1155 |
|
| 1156 |
if (!inputs->is_blend) |
| 1157 |
NIR_PASS(_, nir, pan_nir_lower_fs_inputs, inputs->gpu_id, |
| 1158 |
inputs->varying_layout, info); |
| 1159 |
} |
| 1160 |
|
| 1161 |
if (nir->info.stage == MESA_SHADER_VERTEX && info->vs.idvs) { |
| 1162 |
/* On 5th Gen, IDVS is only in one binary */ |
| 1163 |
if (pan_arch(inputs->gpu_id) >= 12) |
| 1164 |
bi_compile_variant(nir, inputs, binary, info, BI_IDVS_ALL); |
| 1165 |
else { |
| 1166 |
bi_compile_variant(nir, inputs, binary, info, BI_IDVS_POSITION); |
| 1167 |
bi_compile_variant(nir, inputs, binary, info, BI_IDVS_VARYING); |
| 1168 |
} |
| 1169 |
} else { |
| 1170 |
bi_compile_variant(nir, inputs, binary, info, BI_IDVS_NONE); |
| 1171 |
} |
| 1172 |
|
| 1173 |
info->ubo_mask &= (1 << nir->info.num_ubos) - 1; |
| 1174 |
} |