1 /*
2 * Copyright (c) 2016 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 struct lower_intrinsics_state {
28 nir_shader *nir;
29 nir_function_impl *impl;
30 bool progress;
31 nir_builder builder;
32 };
33
34 static bool
lower_cs_intrinsics_convert_block(struct lower_intrinsics_state * state,nir_block * block)35 lower_cs_intrinsics_convert_block(struct lower_intrinsics_state *state,
36 nir_block *block)
37 {
38 bool progress = false;
39 nir_builder *b = &state->builder;
40 nir_shader *nir = state->nir;
41
42 /* Reuse calculated values inside the block. */
43 nir_ssa_def *local_index = NULL;
44 nir_ssa_def *local_id = NULL;
45
46 nir_foreach_instr_safe(instr, block) {
47 if (instr->type != nir_instr_type_intrinsic)
48 continue;
49
50 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
51
52 b->cursor = nir_after_instr(&intrinsic->instr);
53
54 nir_ssa_def *sysval;
55 switch (intrinsic->intrinsic) {
56 case nir_intrinsic_load_local_group_size:
57 case nir_intrinsic_load_work_group_id:
58 case nir_intrinsic_load_num_work_groups:
59 /* Convert this to 32-bit if it's not */
60 if (intrinsic->dest.ssa.bit_size == 64) {
61 intrinsic->dest.ssa.bit_size = 32;
62 sysval = nir_u2u64(b, &intrinsic->dest.ssa);
63 nir_ssa_def_rewrite_uses_after(&intrinsic->dest.ssa,
64 nir_src_for_ssa(sysval),
65 sysval->parent_instr);
66 }
67 continue;
68
69 case nir_intrinsic_load_local_invocation_index:
70 case nir_intrinsic_load_local_invocation_id: {
71 /* First time we are using those, so let's calculate them. */
72 if (!local_index) {
73 assert(!local_id);
74
75 nir_ssa_def *subgroup_id = nir_load_subgroup_id(b);
76
77 nir_ssa_def *thread_local_id =
78 nir_imul(b, subgroup_id, nir_load_simd_width_intel(b));
79 nir_ssa_def *channel = nir_load_subgroup_invocation(b);
80 nir_ssa_def *linear = nir_iadd(b, channel, thread_local_id);
81
82 nir_ssa_def *size_x;
83 nir_ssa_def *size_y;
84 if (state->nir->info.cs.local_size_variable) {
85 nir_ssa_def *size_xyz = nir_load_local_group_size(b);
86 size_x = nir_channel(b, size_xyz, 0);
87 size_y = nir_channel(b, size_xyz, 1);
88 } else {
89 size_x = nir_imm_int(b, nir->info.cs.local_size[0]);
90 size_y = nir_imm_int(b, nir->info.cs.local_size[1]);
91 }
92
93 /* The local invocation index and ID must respect the following
94 *
95 * gl_LocalInvocationID.x =
96 * gl_LocalInvocationIndex % gl_WorkGroupSize.x;
97 * gl_LocalInvocationID.y =
98 * (gl_LocalInvocationIndex / gl_WorkGroupSize.x) %
99 * gl_WorkGroupSize.y;
100 * gl_LocalInvocationID.z =
101 * (gl_LocalInvocationIndex /
102 * (gl_WorkGroupSize.x * gl_WorkGroupSize.y)) %
103 * gl_WorkGroupSize.z;
104 *
105 * However, the final % gl_WorkGroupSize.z does nothing unless we
106 * accidentally end up with a gl_LocalInvocationIndex that is too
107 * large so it can safely be omitted.
108 */
109
110 if (state->nir->info.cs.derivative_group != DERIVATIVE_GROUP_QUADS) {
111 /* If we are not grouping in quads, just set the local invocatio
112 * index linearly, and calculate local invocation ID from that.
113 */
114 local_index = linear;
115
116 nir_ssa_def *id_x, *id_y, *id_z;
117 id_x = nir_umod(b, local_index, size_x);
118 id_y = nir_umod(b, nir_udiv(b, local_index, size_x), size_y);
119 id_z = nir_udiv(b, local_index, nir_imul(b, size_x, size_y));
120 local_id = nir_vec3(b, id_x, id_y, id_z);
121 } else {
122 /* For quads, first we figure out the 2x2 grid the invocation
123 * belongs to -- treating extra Z layers as just more rows.
124 * Then map that into local invocation ID (trivial) and local
125 * invocation index. Skipping Z simplify index calculation.
126 */
127
128 nir_ssa_def *one = nir_imm_int(b, 1);
129 nir_ssa_def *double_size_x = nir_ishl(b, size_x, one);
130
131 /* ID within a pair of rows, where each group of 4 is 2x2 quad. */
132 nir_ssa_def *row_pair_id = nir_umod(b, linear, double_size_x);
133 nir_ssa_def *y_row_pairs = nir_udiv(b, linear, double_size_x);
134
135 nir_ssa_def *x =
136 nir_ior(b,
137 nir_iand(b, row_pair_id, one),
138 nir_iand(b, nir_ishr(b, row_pair_id, one),
139 nir_imm_int(b, 0xfffffffe)));
140 nir_ssa_def *y =
141 nir_ior(b,
142 nir_ishl(b, y_row_pairs, one),
143 nir_iand(b, nir_ishr(b, row_pair_id, one), one));
144
145 local_id = nir_vec3(b, x,
146 nir_umod(b, y, size_y),
147 nir_udiv(b, y, size_y));
148 local_index = nir_iadd(b, x, nir_imul(b, y, size_x));
149 }
150 }
151
152 assert(local_id);
153 assert(local_index);
154 if (intrinsic->intrinsic == nir_intrinsic_load_local_invocation_id)
155 sysval = local_id;
156 else
157 sysval = local_index;
158 break;
159 }
160
161 case nir_intrinsic_load_num_subgroups: {
162 nir_ssa_def *size;
163 if (state->nir->info.cs.local_size_variable) {
164 nir_ssa_def *size_xyz = nir_load_local_group_size(b);
165 nir_ssa_def *size_x = nir_channel(b, size_xyz, 0);
166 nir_ssa_def *size_y = nir_channel(b, size_xyz, 1);
167 nir_ssa_def *size_z = nir_channel(b, size_xyz, 2);
168 size = nir_imul(b, nir_imul(b, size_x, size_y), size_z);
169 } else {
170 size = nir_imm_int(b, nir->info.cs.local_size[0] *
171 nir->info.cs.local_size[1] *
172 nir->info.cs.local_size[2]);
173 }
174
175 /* Calculate the equivalent of DIV_ROUND_UP. */
176 nir_ssa_def *simd_width = nir_load_simd_width_intel(b);
177 sysval =
178 nir_udiv(b, nir_iadd_imm(b, nir_iadd(b, size, simd_width), -1),
179 simd_width);
180 break;
181 }
182
183 default:
184 continue;
185 }
186
187 if (intrinsic->dest.ssa.bit_size == 64)
188 sysval = nir_u2u64(b, sysval);
189
190 nir_ssa_def_rewrite_uses(&intrinsic->dest.ssa, nir_src_for_ssa(sysval));
191 nir_instr_remove(&intrinsic->instr);
192
193 state->progress = true;
194 }
195
196 return progress;
197 }
198
199 static void
lower_cs_intrinsics_convert_impl(struct lower_intrinsics_state * state)200 lower_cs_intrinsics_convert_impl(struct lower_intrinsics_state *state)
201 {
202 nir_builder_init(&state->builder, state->impl);
203
204 nir_foreach_block(block, state->impl) {
205 lower_cs_intrinsics_convert_block(state, block);
206 }
207
208 nir_metadata_preserve(state->impl,
209 nir_metadata_block_index | nir_metadata_dominance);
210 }
211
212 bool
brw_nir_lower_cs_intrinsics(nir_shader * nir)213 brw_nir_lower_cs_intrinsics(nir_shader *nir)
214 {
215 assert(nir->info.stage == MESA_SHADER_COMPUTE ||
216 nir->info.stage == MESA_SHADER_KERNEL);
217
218 struct lower_intrinsics_state state = {
219 .nir = nir,
220 };
221
222 /* Constraints from NV_compute_shader_derivatives. */
223 if (!nir->info.cs.local_size_variable) {
224 if (nir->info.cs.derivative_group == DERIVATIVE_GROUP_QUADS) {
225 assert(nir->info.cs.local_size[0] % 2 == 0);
226 assert(nir->info.cs.local_size[1] % 2 == 0);
227 } else if (nir->info.cs.derivative_group == DERIVATIVE_GROUP_LINEAR) {
228 ASSERTED unsigned local_workgroup_size =
229 nir->info.cs.local_size[0] *
230 nir->info.cs.local_size[1] *
231 nir->info.cs.local_size[2];
232 assert(local_workgroup_size % 4 == 0);
233 }
234 }
235
236 nir_foreach_function(function, nir) {
237 if (function->impl) {
238 state.impl = function->impl;
239 lower_cs_intrinsics_convert_impl(&state);
240 }
241 }
242
243 return state.progress;
244 }
245