| 1 |
/* |
| 2 |
* Copyright (C) 2021 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler/nir/nir_builder.h" |
| 7 |
#include "compiler.h" |
| 8 |
|
| 9 |
/* Divergent attribute access is undefined behaviour. To avoid divergence, |
| 10 |
* lower to an if-chain like: |
| 11 |
* |
| 12 |
* value = 0; |
| 13 |
* if (lane == 0) |
| 14 |
* value = ld() |
| 15 |
* else if (lane == 1) |
| 16 |
* value = ld() |
| 17 |
* ... |
| 18 |
* else if (lane == MAX_LANE) |
| 19 |
* value = ld() |
| 20 |
*/ |
| 21 |
|
| 22 |
static bool |
| 23 |
bi_lower_divergent_indirects_impl(nir_builder *b, nir_intrinsic_instr *intr, |
| 24 |
void *data) |
| 25 |
{ |
| 26 |
mesa_shader_stage stage = b->shader->info.stage; |
| 27 |
nir_src *offset; |
| 28 |
|
| 29 |
/* Not all indirect access needs this workaround */ |
| 30 |
switch (intr->intrinsic) { |
| 31 |
case nir_intrinsic_load_input: |
| 32 |
case nir_intrinsic_load_interpolated_input: |
| 33 |
case nir_intrinsic_load_attribute_pan: |
| 34 |
/* Attributes and varyings */ |
| 35 |
offset = nir_get_io_offset_src(intr); |
| 36 |
break; |
| 37 |
|
| 38 |
case nir_intrinsic_store_output: |
| 39 |
/* Varyings only */ |
| 40 |
if (stage == MESA_SHADER_FRAGMENT) |
| 41 |
return false; |
| 42 |
|
| 43 |
offset = nir_get_io_offset_src(intr); |
| 44 |
break; |
| 45 |
|
| 46 |
case nir_intrinsic_store_per_view_output: |
| 47 |
assert(stage == MESA_SHADER_VERTEX); |
| 48 |
assert(!nir_src_is_divergent(&intr->src[1])); |
| 49 |
offset = nir_get_io_offset_src(intr); |
| 50 |
break; |
| 51 |
|
| 52 |
case nir_intrinsic_image_texel_address: |
| 53 |
case nir_intrinsic_image_load: |
| 54 |
case nir_intrinsic_image_store: |
| 55 |
/* Any image access */ |
| 56 |
offset = &intr->src[0]; |
| 57 |
break; |
| 58 |
default: |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
if (!nir_src_is_divergent(offset)) |
| 63 |
return false; |
| 64 |
|
| 65 |
/* This indirect does need it */ |
| 66 |
|
| 67 |
b->cursor = nir_before_instr(&intr->instr); |
| 68 |
nir_def *lane = nir_load_subgroup_invocation(b); |
| 69 |
unsigned *lanes = data; |
| 70 |
|
| 71 |
/* Write zero in a funny way to bypass lower_load_const_to_scalar */ |
| 72 |
bool has_dest = nir_intrinsic_infos[intr->intrinsic].has_dest; |
| 73 |
unsigned size = has_dest ? intr->def.bit_size : 32; |
| 74 |
nir_def *zero = has_dest ? nir_imm_zero(b, 1, size) : NULL; |
| 75 |
nir_def *zeroes[4] = {zero, zero, zero, zero}; |
| 76 |
nir_def *res = |
| 77 |
has_dest ? nir_vec(b, zeroes, intr->def.num_components) : NULL; |
| 78 |
|
| 79 |
for (unsigned i = 0; i < (*lanes); ++i) { |
| 80 |
nir_push_if(b, nir_ieq_imm(b, lane, i)); |
| 81 |
|
| 82 |
nir_instr *c = nir_instr_clone(b->shader, &intr->instr); |
| 83 |
nir_intrinsic_instr *c_intr = nir_instr_as_intrinsic(c); |
| 84 |
nir_builder_instr_insert(b, c); |
| 85 |
nir_pop_if(b, NULL); |
| 86 |
|
| 87 |
if (has_dest) { |
| 88 |
nir_def *c_ssa = &c_intr->def; |
| 89 |
res = nir_if_phi(b, c_ssa, res); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (has_dest) |
| 94 |
nir_def_rewrite_uses(&intr->def, res); |
| 95 |
|
| 96 |
nir_instr_remove(&intr->instr); |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
bool |
| 101 |
bi_lower_divergent_indirects(nir_shader *shader, unsigned lanes) |
| 102 |
{ |
| 103 |
return nir_shader_intrinsics_pass(shader, bi_lower_divergent_indirects_impl, |
| 104 |
nir_metadata_none, &lanes); |
| 105 |
} |