1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2021 Collabora, Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "compiler.h" 25bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci/* Divergent attribute access is undefined behaviour. To avoid divergence, 28bf215546Sopenharmony_ci * lower to an if-chain like: 29bf215546Sopenharmony_ci * 30bf215546Sopenharmony_ci * value = 0; 31bf215546Sopenharmony_ci * if (lane == 0) 32bf215546Sopenharmony_ci * value = ld() 33bf215546Sopenharmony_ci * else if (lane == 1) 34bf215546Sopenharmony_ci * value = ld() 35bf215546Sopenharmony_ci * ... 36bf215546Sopenharmony_ci * else if (lane == MAX_LANE) 37bf215546Sopenharmony_ci * value = ld() 38bf215546Sopenharmony_ci */ 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_cistatic bool 41bf215546Sopenharmony_cibi_lower_divergent_indirects_impl(nir_builder *b, nir_instr *instr, void *data) 42bf215546Sopenharmony_ci{ 43bf215546Sopenharmony_ci if (instr->type != nir_instr_type_intrinsic) 44bf215546Sopenharmony_ci return false; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); 47bf215546Sopenharmony_ci gl_shader_stage stage = b->shader->info.stage; 48bf215546Sopenharmony_ci nir_src *offset; 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci /* Not all indirect access needs this workaround */ 51bf215546Sopenharmony_ci switch (intr->intrinsic) { 52bf215546Sopenharmony_ci case nir_intrinsic_load_input: 53bf215546Sopenharmony_ci case nir_intrinsic_load_interpolated_input: 54bf215546Sopenharmony_ci /* Attributes and varyings */ 55bf215546Sopenharmony_ci offset = nir_get_io_offset_src(intr); 56bf215546Sopenharmony_ci break; 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci case nir_intrinsic_store_output: 59bf215546Sopenharmony_ci /* Varyings only */ 60bf215546Sopenharmony_ci if (stage == MESA_SHADER_FRAGMENT) 61bf215546Sopenharmony_ci return false; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci offset = nir_get_io_offset_src(intr); 64bf215546Sopenharmony_ci break; 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci case nir_intrinsic_image_atomic_add: 67bf215546Sopenharmony_ci case nir_intrinsic_image_atomic_imin: 68bf215546Sopenharmony_ci case nir_intrinsic_image_atomic_umin: 69bf215546Sopenharmony_ci case nir_intrinsic_image_atomic_imax: 70bf215546Sopenharmony_ci case nir_intrinsic_image_atomic_umax: 71bf215546Sopenharmony_ci case nir_intrinsic_image_atomic_and: 72bf215546Sopenharmony_ci case nir_intrinsic_image_atomic_or: 73bf215546Sopenharmony_ci case nir_intrinsic_image_atomic_xor: 74bf215546Sopenharmony_ci case nir_intrinsic_image_load: 75bf215546Sopenharmony_ci case nir_intrinsic_image_store: 76bf215546Sopenharmony_ci /* Any image access */ 77bf215546Sopenharmony_ci offset = &intr->src[0]; 78bf215546Sopenharmony_ci break; 79bf215546Sopenharmony_ci default: 80bf215546Sopenharmony_ci return false; 81bf215546Sopenharmony_ci } 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci if (!nir_src_is_divergent(*offset)) 84bf215546Sopenharmony_ci return false; 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci /* This indirect does need it */ 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci b->cursor = nir_before_instr(instr); 89bf215546Sopenharmony_ci nir_ssa_def *lane = nir_load_subgroup_invocation(b); 90bf215546Sopenharmony_ci unsigned *lanes = data; 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci /* Write zero in a funny way to bypass lower_load_const_to_scalar */ 93bf215546Sopenharmony_ci bool has_dest = nir_intrinsic_infos[intr->intrinsic].has_dest; 94bf215546Sopenharmony_ci unsigned size = has_dest ? nir_dest_bit_size(intr->dest) : 32; 95bf215546Sopenharmony_ci nir_ssa_def *zero = has_dest ? nir_imm_zero(b, 1, size) : NULL; 96bf215546Sopenharmony_ci nir_ssa_def *zeroes[4] = { zero, zero, zero, zero }; 97bf215546Sopenharmony_ci nir_ssa_def *res = has_dest ? 98bf215546Sopenharmony_ci nir_vec(b, zeroes, nir_dest_num_components(intr->dest)) : NULL; 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci for (unsigned i = 0; i < (*lanes); ++i) { 101bf215546Sopenharmony_ci nir_push_if(b, nir_ieq_imm(b, lane, i)); 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci nir_instr *c = nir_instr_clone(b->shader, instr); 104bf215546Sopenharmony_ci nir_intrinsic_instr *c_intr = nir_instr_as_intrinsic(c); 105bf215546Sopenharmony_ci nir_builder_instr_insert(b, c); 106bf215546Sopenharmony_ci nir_pop_if(b, NULL); 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci if (has_dest) { 109bf215546Sopenharmony_ci assert(c_intr->dest.is_ssa); 110bf215546Sopenharmony_ci nir_ssa_def *c_ssa = &c_intr->dest.ssa; 111bf215546Sopenharmony_ci res = nir_if_phi(b, c_ssa, res); 112bf215546Sopenharmony_ci } 113bf215546Sopenharmony_ci } 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci if (has_dest) 116bf215546Sopenharmony_ci nir_ssa_def_rewrite_uses(&intr->dest.ssa, res); 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci nir_instr_remove(instr); 119bf215546Sopenharmony_ci return true; 120bf215546Sopenharmony_ci} 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_cibool 123bf215546Sopenharmony_cibi_lower_divergent_indirects(nir_shader *shader, unsigned lanes) 124bf215546Sopenharmony_ci{ 125bf215546Sopenharmony_ci return nir_shader_instructions_pass(shader, 126bf215546Sopenharmony_ci bi_lower_divergent_indirects_impl, 127bf215546Sopenharmony_ci nir_metadata_none, &lanes); 128bf215546Sopenharmony_ci} 129