nir/lower_system_values: Drop the context-aware builder functions
[mesa.git] / src / compiler / nir / nir_lower_system_values.c
1 /*
2 * Copyright © 2014 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 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_builder.h"
30
31 static nir_ssa_def *
32 sanitize_32bit_sysval(nir_builder *b, nir_intrinsic_instr *intrin)
33 {
34 assert(intrin->dest.is_ssa);
35 const unsigned bit_size = intrin->dest.ssa.bit_size;
36 if (bit_size == 32)
37 return NULL;
38
39 intrin->dest.ssa.bit_size = 32;
40 return nir_u2u(b, &intrin->dest.ssa, bit_size);
41 }
42
43 static nir_ssa_def*
44 build_global_group_size(nir_builder *b, unsigned bit_size)
45 {
46 nir_ssa_def *group_size = nir_load_local_group_size(b);
47 nir_ssa_def *num_work_groups = nir_load_num_work_groups(b);
48 return nir_imul(b, nir_u2u(b, group_size, bit_size),
49 nir_u2u(b, num_work_groups, bit_size));
50 }
51
52 static bool
53 lower_system_value_filter(const nir_instr *instr, const void *_state)
54 {
55 return instr->type == nir_instr_type_intrinsic;
56 }
57
58 static nir_ssa_def *
59 lower_system_value_instr(nir_builder *b, nir_instr *instr, void *_state)
60 {
61 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
62
63 /* All the intrinsics we care about are loads */
64 if (!nir_intrinsic_infos[intrin->intrinsic].has_dest)
65 return NULL;
66
67 assert(intrin->dest.is_ssa);
68 const unsigned bit_size = intrin->dest.ssa.bit_size;
69
70 switch (intrin->intrinsic) {
71 case nir_intrinsic_load_local_invocation_id:
72 /* If lower_cs_local_id_from_index is true, then we derive the local
73 * index from the local id.
74 */
75 if (b->shader->options->lower_cs_local_id_from_index) {
76 /* We lower gl_LocalInvocationID from gl_LocalInvocationIndex based
77 * on this formula:
78 *
79 * gl_LocalInvocationID.x =
80 * gl_LocalInvocationIndex % gl_WorkGroupSize.x;
81 * gl_LocalInvocationID.y =
82 * (gl_LocalInvocationIndex / gl_WorkGroupSize.x) %
83 * gl_WorkGroupSize.y;
84 * gl_LocalInvocationID.z =
85 * (gl_LocalInvocationIndex /
86 * (gl_WorkGroupSize.x * gl_WorkGroupSize.y)) %
87 * gl_WorkGroupSize.z;
88 *
89 * However, the final % gl_WorkGroupSize.z does nothing unless we
90 * accidentally end up with a gl_LocalInvocationIndex that is too
91 * large so it can safely be omitted.
92 */
93 nir_ssa_def *local_index = nir_load_local_invocation_index(b);
94 nir_ssa_def *local_size = nir_load_local_group_size(b);
95
96 /* Because no hardware supports a local workgroup size greater than
97 * about 1K, this calculation can be done in 32-bit and can save some
98 * 64-bit arithmetic.
99 */
100 nir_ssa_def *id_x, *id_y, *id_z;
101 id_x = nir_umod(b, local_index,
102 nir_channel(b, local_size, 0));
103 id_y = nir_umod(b, nir_udiv(b, local_index,
104 nir_channel(b, local_size, 0)),
105 nir_channel(b, local_size, 1));
106 id_z = nir_udiv(b, local_index,
107 nir_imul(b, nir_channel(b, local_size, 0),
108 nir_channel(b, local_size, 1)));
109 return nir_u2u(b, nir_vec3(b, id_x, id_y, id_z), bit_size);
110 } else {
111 return sanitize_32bit_sysval(b, intrin);
112 }
113
114 case nir_intrinsic_load_local_group_size:
115 if (b->shader->info.cs.local_size_variable) {
116 /* If the local work group size is variable it can't be lowered at
117 * this point. We do, however, have to make sure that the intrinsic
118 * is only 32-bit.
119 */
120 return sanitize_32bit_sysval(b, intrin);
121 } else {
122 /* using a 32 bit constant is safe here as no device/driver needs more
123 * than 32 bits for the local size */
124 nir_const_value local_size_const[3];
125 memset(local_size_const, 0, sizeof(local_size_const));
126 local_size_const[0].u32 = b->shader->info.cs.local_size[0];
127 local_size_const[1].u32 = b->shader->info.cs.local_size[1];
128 local_size_const[2].u32 = b->shader->info.cs.local_size[2];
129 return nir_u2u(b, nir_build_imm(b, 3, 32, local_size_const), bit_size);
130 }
131
132 case nir_intrinsic_load_global_invocation_id: {
133 nir_ssa_def *group_size = nir_load_local_group_size(b);
134 nir_ssa_def *group_id = nir_load_work_group_id(b);
135 nir_ssa_def *local_id = nir_load_local_invocation_id(b);
136
137 return nir_iadd(b, nir_imul(b, nir_u2u(b, group_id, bit_size),
138 nir_u2u(b, group_size, bit_size)),
139 nir_u2u(b, local_id, bit_size));
140 }
141
142 case nir_intrinsic_load_deref: {
143 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
144 if (deref->mode != nir_var_system_value)
145 return NULL;
146
147 if (deref->deref_type != nir_deref_type_var) {
148 /* The only one system value that is an array and that is
149 * gl_SampleMask which is always an array of one element.
150 */
151 assert(deref->deref_type == nir_deref_type_array);
152 deref = nir_deref_instr_parent(deref);
153 assert(deref->deref_type == nir_deref_type_var);
154 assert(deref->var->data.location == SYSTEM_VALUE_SAMPLE_MASK_IN);
155 }
156 nir_variable *var = deref->var;
157
158 switch (var->data.location) {
159 case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX: {
160 nir_ssa_def *global_id = nir_load_global_invocation_id(b, bit_size);
161 nir_ssa_def *global_size = build_global_group_size(b, bit_size);
162
163 /* index = id.x + ((id.y + (id.z * size.y)) * size.x) */
164 nir_ssa_def *index;
165 index = nir_imul(b, nir_channel(b, global_id, 2),
166 nir_channel(b, global_size, 1));
167 index = nir_iadd(b, nir_channel(b, global_id, 1), index);
168 index = nir_imul(b, nir_channel(b, global_size, 0), index);
169 index = nir_iadd(b, nir_channel(b, global_id, 0), index);
170 return index;
171 }
172
173 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX: {
174 /* If lower_cs_local_index_from_id is true, then we derive the local
175 * index from the local id.
176 */
177 if (!b->shader->options->lower_cs_local_index_from_id)
178 break;
179
180 /* From the GLSL man page for gl_LocalInvocationIndex:
181 *
182 * "The value of gl_LocalInvocationIndex is equal to
183 * gl_LocalInvocationID.z * gl_WorkGroupSize.x *
184 * gl_WorkGroupSize.y + gl_LocalInvocationID.y *
185 * gl_WorkGroupSize.x + gl_LocalInvocationID.x"
186 */
187 nir_ssa_def *local_id = nir_load_local_invocation_id(b);
188
189 nir_ssa_def *size_x =
190 nir_imm_int(b, b->shader->info.cs.local_size[0]);
191 nir_ssa_def *size_y =
192 nir_imm_int(b, b->shader->info.cs.local_size[1]);
193
194 /* Because no hardware supports a local workgroup size greater than
195 * about 1K, this calculation can be done in 32-bit and can save some
196 * 64-bit arithmetic.
197 */
198 nir_ssa_def *index;
199 index = nir_imul(b, nir_channel(b, local_id, 2),
200 nir_imul(b, size_x, size_y));
201 index = nir_iadd(b, index,
202 nir_imul(b, nir_channel(b, local_id, 1), size_x));
203 index = nir_iadd(b, index, nir_channel(b, local_id, 0));
204 index = nir_u2u(b, index, bit_size);
205 return index;
206 }
207
208 case SYSTEM_VALUE_VERTEX_ID:
209 if (b->shader->options->vertex_id_zero_based) {
210 return nir_iadd(b, nir_load_vertex_id_zero_base(b),
211 nir_load_first_vertex(b));
212 }
213 break;
214
215 case SYSTEM_VALUE_BASE_VERTEX:
216 /**
217 * From the OpenGL 4.6 (11.1.3.9 Shader Inputs) specification:
218 *
219 * "gl_BaseVertex holds the integer value passed to the baseVertex
220 * parameter to the command that resulted in the current shader
221 * invocation. In the case where the command has no baseVertex
222 * parameter, the value of gl_BaseVertex is zero."
223 */
224 if (b->shader->options->lower_base_vertex) {
225 return nir_iand(b, nir_load_is_indexed_draw(b),
226 nir_load_first_vertex(b));
227 }
228 break;
229
230 case SYSTEM_VALUE_HELPER_INVOCATION:
231 if (b->shader->options->lower_helper_invocation) {
232 nir_ssa_def *tmp;
233
234 tmp = nir_ishl(b,
235 nir_imm_int(b, 1),
236 nir_load_sample_id_no_per_sample(b));
237
238 tmp = nir_iand(b,
239 nir_load_sample_mask_in(b),
240 tmp);
241
242 return nir_inot(b, nir_i2b(b, tmp));
243 }
244 break;
245
246 case SYSTEM_VALUE_INSTANCE_INDEX:
247 return nir_iadd(b, nir_load_instance_id(b),
248 nir_load_base_instance(b));
249
250 case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
251 case SYSTEM_VALUE_SUBGROUP_GE_MASK:
252 case SYSTEM_VALUE_SUBGROUP_GT_MASK:
253 case SYSTEM_VALUE_SUBGROUP_LE_MASK:
254 case SYSTEM_VALUE_SUBGROUP_LT_MASK: {
255 nir_intrinsic_op op =
256 nir_intrinsic_from_system_value(var->data.location);
257 nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
258 nir_ssa_dest_init_for_type(&load->instr, &load->dest,
259 var->type, NULL);
260 load->num_components = load->dest.ssa.num_components;
261 nir_builder_instr_insert(b, &load->instr);
262 return &load->dest.ssa;
263 }
264
265 case SYSTEM_VALUE_DEVICE_INDEX:
266 if (b->shader->options->lower_device_index_to_zero)
267 return nir_imm_int(b, 0);
268 break;
269
270 case SYSTEM_VALUE_GLOBAL_GROUP_SIZE:
271 return build_global_group_size(b, bit_size);
272
273 default:
274 break;
275 }
276
277 nir_intrinsic_op sysval_op =
278 nir_intrinsic_from_system_value(var->data.location);
279 return nir_load_system_value(b, sysval_op, 0,
280 intrin->dest.ssa.bit_size);
281 }
282
283 default:
284 return NULL;
285 }
286 }
287
288 bool
289 nir_lower_system_values(nir_shader *shader)
290 {
291 bool progress = nir_shader_lower_instructions(shader,
292 lower_system_value_filter,
293 lower_system_value_instr,
294 NULL);
295
296 /* We're going to delete the variables so we need to clean up all those
297 * derefs we left lying around.
298 */
299 if (progress)
300 nir_remove_dead_derefs(shader);
301
302 exec_list_make_empty(&shader->system_values);
303
304 return progress;
305 }