intel/compiler: Add support for variable workgroup size
[mesa.git] / src / intel / compiler / brw_nir_lower_cs_intrinsics.c
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 unsigned dispatch_width;
30 nir_function_impl *impl;
31 bool progress;
32 nir_builder builder;
33 unsigned local_workgroup_size;
34 };
35
36 static bool
37 lower_cs_intrinsics_convert_block(struct lower_intrinsics_state *state,
38 nir_block *block)
39 {
40 bool progress = false;
41 nir_builder *b = &state->builder;
42 nir_shader *nir = state->nir;
43
44 /* Reuse calculated values inside the block. */
45 nir_ssa_def *local_index = NULL;
46 nir_ssa_def *local_id = NULL;
47
48 nir_foreach_instr_safe(instr, block) {
49 if (instr->type != nir_instr_type_intrinsic)
50 continue;
51
52 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
53
54 b->cursor = nir_after_instr(&intrinsic->instr);
55
56 nir_ssa_def *sysval;
57 switch (intrinsic->intrinsic) {
58 case nir_intrinsic_load_local_invocation_index:
59 case nir_intrinsic_load_local_invocation_id: {
60 /* First time we are using those, so let's calculate them. */
61 if (!local_index) {
62 assert(!local_id);
63
64 nir_ssa_def *subgroup_id;
65 if (state->local_workgroup_size <= state->dispatch_width)
66 subgroup_id = nir_imm_int(b, 0);
67 else
68 subgroup_id = nir_load_subgroup_id(b);
69
70 nir_ssa_def *thread_local_id =
71 nir_imul_imm(b, subgroup_id, state->dispatch_width);
72 nir_ssa_def *channel = nir_load_subgroup_invocation(b);
73 nir_ssa_def *linear = nir_iadd(b, channel, thread_local_id);
74
75 nir_ssa_def *size_x;
76 nir_ssa_def *size_y;
77 if (state->nir->info.cs.local_size_variable) {
78 nir_ssa_def *size_xyz = nir_load_local_group_size(b);
79 size_x = nir_channel(b, size_xyz, 0);
80 size_y = nir_channel(b, size_xyz, 1);
81 } else {
82 size_x = nir_imm_int(b, nir->info.cs.local_size[0]);
83 size_y = nir_imm_int(b, nir->info.cs.local_size[1]);
84 }
85
86 /* The local invocation index and ID must respect the following
87 *
88 * gl_LocalInvocationID.x =
89 * gl_LocalInvocationIndex % gl_WorkGroupSize.x;
90 * gl_LocalInvocationID.y =
91 * (gl_LocalInvocationIndex / gl_WorkGroupSize.x) %
92 * gl_WorkGroupSize.y;
93 * gl_LocalInvocationID.z =
94 * (gl_LocalInvocationIndex /
95 * (gl_WorkGroupSize.x * gl_WorkGroupSize.y)) %
96 * gl_WorkGroupSize.z;
97 *
98 * However, the final % gl_WorkGroupSize.z does nothing unless we
99 * accidentally end up with a gl_LocalInvocationIndex that is too
100 * large so it can safely be omitted.
101 */
102
103 if (state->nir->info.cs.derivative_group != DERIVATIVE_GROUP_QUADS) {
104 /* If we are not grouping in quads, just set the local invocatio
105 * index linearly, and calculate local invocation ID from that.
106 */
107 local_index = linear;
108
109 nir_ssa_def *id_x, *id_y, *id_z;
110 id_x = nir_umod(b, local_index, size_x);
111 id_y = nir_umod(b, nir_udiv(b, local_index, size_x), size_y);
112 id_z = nir_udiv(b, local_index, nir_imul(b, size_x, size_y));
113 local_id = nir_vec3(b, id_x, id_y, id_z);
114 } else {
115 /* For quads, first we figure out the 2x2 grid the invocation
116 * belongs to -- treating extra Z layers as just more rows.
117 * Then map that into local invocation ID (trivial) and local
118 * invocation index. Skipping Z simplify index calculation.
119 */
120
121 nir_ssa_def *one = nir_imm_int(b, 1);
122 nir_ssa_def *double_size_x = nir_ishl(b, size_x, one);
123
124 /* ID within a pair of rows, where each group of 4 is 2x2 quad. */
125 nir_ssa_def *row_pair_id = nir_umod(b, linear, double_size_x);
126 nir_ssa_def *y_row_pairs = nir_udiv(b, linear, double_size_x);
127
128 nir_ssa_def *x =
129 nir_ior(b,
130 nir_iand(b, row_pair_id, one),
131 nir_iand(b, nir_ishr(b, row_pair_id, one),
132 nir_imm_int(b, 0xfffffffe)));
133 nir_ssa_def *y =
134 nir_ior(b,
135 nir_ishl(b, y_row_pairs, one),
136 nir_iand(b, nir_ishr(b, row_pair_id, one), one));
137
138 local_id = nir_vec3(b, x,
139 nir_umod(b, y, size_y),
140 nir_udiv(b, y, size_y));
141 local_index = nir_iadd(b, x, nir_imul(b, y, size_x));
142 }
143 }
144
145 assert(local_id);
146 assert(local_index);
147 if (intrinsic->intrinsic == nir_intrinsic_load_local_invocation_id)
148 sysval = local_id;
149 else
150 sysval = local_index;
151 break;
152 }
153
154 case nir_intrinsic_load_subgroup_id:
155 if (state->local_workgroup_size > 8)
156 continue;
157
158 /* For small workgroup sizes, we know subgroup_id will be zero */
159 sysval = nir_imm_int(b, 0);
160 break;
161
162 case nir_intrinsic_load_num_subgroups: {
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 nir_ssa_def *size = nir_imul(b, nir_imul(b, size_x, size_y), size_z);
169
170 /* Calculate the equivalent of DIV_ROUND_UP. */
171 sysval = nir_idiv(b,
172 nir_iadd_imm(b,
173 nir_iadd_imm(b, size, state->dispatch_width), -1),
174 nir_imm_int(b, state->dispatch_width));
175 } else {
176 unsigned local_workgroup_size =
177 nir->info.cs.local_size[0] * nir->info.cs.local_size[1] *
178 nir->info.cs.local_size[2];
179 unsigned num_subgroups =
180 DIV_ROUND_UP(local_workgroup_size, state->dispatch_width);
181 sysval = nir_imm_int(b, num_subgroups);
182 }
183 break;
184 }
185
186 default:
187 continue;
188 }
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
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
213 brw_nir_lower_cs_intrinsics(nir_shader *nir,
214 unsigned dispatch_width)
215 {
216 assert(nir->info.stage == MESA_SHADER_COMPUTE);
217
218 struct lower_intrinsics_state state = {
219 .nir = nir,
220 .dispatch_width = dispatch_width,
221 };
222
223 if (!nir->info.cs.local_size_variable) {
224 state.local_workgroup_size = nir->info.cs.local_size[0] *
225 nir->info.cs.local_size[1] *
226 nir->info.cs.local_size[2];
227 } else {
228 state.local_workgroup_size = nir->info.cs.max_variable_local_size;
229 }
230
231 /* Constraints from NV_compute_shader_derivatives. */
232 if (nir->info.cs.derivative_group == DERIVATIVE_GROUP_QUADS &&
233 !nir->info.cs.local_size_variable) {
234 assert(nir->info.cs.local_size[0] % 2 == 0);
235 assert(nir->info.cs.local_size[1] % 2 == 0);
236 } else if (nir->info.cs.derivative_group == DERIVATIVE_GROUP_LINEAR &&
237 !nir->info.cs.local_size_variable) {
238 assert(state.local_workgroup_size % 4 == 0);
239 }
240
241 nir_foreach_function(function, nir) {
242 if (function->impl) {
243 state.impl = function->impl;
244 lower_cs_intrinsics_convert_impl(&state);
245 }
246 }
247
248 return state.progress;
249 }