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