1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
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
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Connor Abbott (cwabbott0@gmail.com)
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "nir.h"
29bf215546Sopenharmony_ci#include "nir_builder.h"
30bf215546Sopenharmony_ci#include "util/u_math.h"
31bf215546Sopenharmony_ci#include "util/set.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cistruct lower_sysval_state {
34bf215546Sopenharmony_ci   const nir_lower_compute_system_values_options *options;
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci   /* List of intrinsics that have already been lowered and shouldn't be
37bf215546Sopenharmony_ci    * lowered again.
38bf215546Sopenharmony_ci    */
39bf215546Sopenharmony_ci   struct set *lower_once_list;
40bf215546Sopenharmony_ci};
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistatic nir_ssa_def *
43bf215546Sopenharmony_cisanitize_32bit_sysval(nir_builder *b, nir_intrinsic_instr *intrin)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci   assert(intrin->dest.is_ssa);
46bf215546Sopenharmony_ci   const unsigned bit_size = intrin->dest.ssa.bit_size;
47bf215546Sopenharmony_ci   if (bit_size == 32)
48bf215546Sopenharmony_ci      return NULL;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   intrin->dest.ssa.bit_size = 32;
51bf215546Sopenharmony_ci   return nir_u2u(b, &intrin->dest.ssa, bit_size);
52bf215546Sopenharmony_ci}
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_cistatic nir_ssa_def*
55bf215546Sopenharmony_cibuild_global_group_size(nir_builder *b, unsigned bit_size)
56bf215546Sopenharmony_ci{
57bf215546Sopenharmony_ci   nir_ssa_def *group_size = nir_load_workgroup_size(b);
58bf215546Sopenharmony_ci   nir_ssa_def *num_workgroups = nir_load_num_workgroups(b, bit_size);
59bf215546Sopenharmony_ci   return nir_imul(b, nir_u2u(b, group_size, bit_size),
60bf215546Sopenharmony_ci                      num_workgroups);
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistatic bool
64bf215546Sopenharmony_cilower_system_value_filter(const nir_instr *instr, const void *_state)
65bf215546Sopenharmony_ci{
66bf215546Sopenharmony_ci   return instr->type == nir_instr_type_intrinsic;
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_cistatic nir_ssa_def *
70bf215546Sopenharmony_cilower_system_value_instr(nir_builder *b, nir_instr *instr, void *_state)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   /* All the intrinsics we care about are loads */
75bf215546Sopenharmony_ci   if (!nir_intrinsic_infos[intrin->intrinsic].has_dest)
76bf215546Sopenharmony_ci      return NULL;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   assert(intrin->dest.is_ssa);
79bf215546Sopenharmony_ci   const unsigned bit_size = intrin->dest.ssa.bit_size;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   switch (intrin->intrinsic) {
82bf215546Sopenharmony_ci   case nir_intrinsic_load_vertex_id:
83bf215546Sopenharmony_ci      if (b->shader->options->vertex_id_zero_based) {
84bf215546Sopenharmony_ci         return nir_iadd(b, nir_load_vertex_id_zero_base(b),
85bf215546Sopenharmony_ci                            nir_load_first_vertex(b));
86bf215546Sopenharmony_ci      } else {
87bf215546Sopenharmony_ci         return NULL;
88bf215546Sopenharmony_ci      }
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   case nir_intrinsic_load_base_vertex:
91bf215546Sopenharmony_ci      /**
92bf215546Sopenharmony_ci       * From the OpenGL 4.6 (11.1.3.9 Shader Inputs) specification:
93bf215546Sopenharmony_ci       *
94bf215546Sopenharmony_ci       * "gl_BaseVertex holds the integer value passed to the baseVertex
95bf215546Sopenharmony_ci       * parameter to the command that resulted in the current shader
96bf215546Sopenharmony_ci       * invocation. In the case where the command has no baseVertex
97bf215546Sopenharmony_ci       * parameter, the value of gl_BaseVertex is zero."
98bf215546Sopenharmony_ci       */
99bf215546Sopenharmony_ci      if (b->shader->options->lower_base_vertex) {
100bf215546Sopenharmony_ci         return nir_iand(b, nir_load_is_indexed_draw(b),
101bf215546Sopenharmony_ci                            nir_load_first_vertex(b));
102bf215546Sopenharmony_ci      } else {
103bf215546Sopenharmony_ci         return NULL;
104bf215546Sopenharmony_ci      }
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   case nir_intrinsic_load_helper_invocation:
107bf215546Sopenharmony_ci      if (b->shader->options->lower_helper_invocation) {
108bf215546Sopenharmony_ci         nir_ssa_def *tmp;
109bf215546Sopenharmony_ci         tmp = nir_ishl(b, nir_imm_int(b, 1),
110bf215546Sopenharmony_ci                           nir_load_sample_id_no_per_sample(b));
111bf215546Sopenharmony_ci         tmp = nir_iand(b, nir_load_sample_mask_in(b), tmp);
112bf215546Sopenharmony_ci         return nir_inot(b, nir_i2b(b, tmp));
113bf215546Sopenharmony_ci      } else {
114bf215546Sopenharmony_ci         return NULL;
115bf215546Sopenharmony_ci      }
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_id:
118bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_index:
119bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_size:
120bf215546Sopenharmony_ci      return sanitize_32bit_sysval(b, intrin);
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   case nir_intrinsic_load_deref: {
123bf215546Sopenharmony_ci      nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
124bf215546Sopenharmony_ci      if (!nir_deref_mode_is(deref, nir_var_system_value))
125bf215546Sopenharmony_ci         return NULL;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci      nir_ssa_def *column = NULL;
128bf215546Sopenharmony_ci      if (deref->deref_type != nir_deref_type_var) {
129bf215546Sopenharmony_ci         /* The only one system values that aren't plane variables are
130bf215546Sopenharmony_ci          * gl_SampleMask which is always an array of one element and a
131bf215546Sopenharmony_ci          * couple of ray-tracing intrinsics which are matrices.
132bf215546Sopenharmony_ci          */
133bf215546Sopenharmony_ci         assert(deref->deref_type == nir_deref_type_array);
134bf215546Sopenharmony_ci         assert(deref->arr.index.is_ssa);
135bf215546Sopenharmony_ci         column = deref->arr.index.ssa;
136bf215546Sopenharmony_ci         deref = nir_deref_instr_parent(deref);
137bf215546Sopenharmony_ci         assert(deref->deref_type == nir_deref_type_var);
138bf215546Sopenharmony_ci         assert(deref->var->data.location == SYSTEM_VALUE_SAMPLE_MASK_IN ||
139bf215546Sopenharmony_ci                deref->var->data.location == SYSTEM_VALUE_RAY_OBJECT_TO_WORLD ||
140bf215546Sopenharmony_ci                deref->var->data.location == SYSTEM_VALUE_RAY_WORLD_TO_OBJECT ||
141bf215546Sopenharmony_ci                deref->var->data.location == SYSTEM_VALUE_MESH_VIEW_INDICES);
142bf215546Sopenharmony_ci      }
143bf215546Sopenharmony_ci      nir_variable *var = deref->var;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci      switch (var->data.location) {
146bf215546Sopenharmony_ci      case SYSTEM_VALUE_INSTANCE_INDEX:
147bf215546Sopenharmony_ci         return nir_iadd(b, nir_load_instance_id(b),
148bf215546Sopenharmony_ci                            nir_load_base_instance(b));
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci      case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
151bf215546Sopenharmony_ci      case SYSTEM_VALUE_SUBGROUP_GE_MASK:
152bf215546Sopenharmony_ci      case SYSTEM_VALUE_SUBGROUP_GT_MASK:
153bf215546Sopenharmony_ci      case SYSTEM_VALUE_SUBGROUP_LE_MASK:
154bf215546Sopenharmony_ci      case SYSTEM_VALUE_SUBGROUP_LT_MASK: {
155bf215546Sopenharmony_ci         nir_intrinsic_op op =
156bf215546Sopenharmony_ci            nir_intrinsic_from_system_value(var->data.location);
157bf215546Sopenharmony_ci         nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
158bf215546Sopenharmony_ci         nir_ssa_dest_init_for_type(&load->instr, &load->dest,
159bf215546Sopenharmony_ci                                    var->type, NULL);
160bf215546Sopenharmony_ci         load->num_components = load->dest.ssa.num_components;
161bf215546Sopenharmony_ci         nir_builder_instr_insert(b, &load->instr);
162bf215546Sopenharmony_ci         return &load->dest.ssa;
163bf215546Sopenharmony_ci      }
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci      case SYSTEM_VALUE_DEVICE_INDEX:
166bf215546Sopenharmony_ci         if (b->shader->options->lower_device_index_to_zero)
167bf215546Sopenharmony_ci            return nir_imm_int(b, 0);
168bf215546Sopenharmony_ci         break;
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci      case SYSTEM_VALUE_GLOBAL_GROUP_SIZE:
171bf215546Sopenharmony_ci         return build_global_group_size(b, bit_size);
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci      case SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL:
174bf215546Sopenharmony_ci         return nir_load_barycentric(b, nir_intrinsic_load_barycentric_pixel,
175bf215546Sopenharmony_ci                                     INTERP_MODE_NOPERSPECTIVE);
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci      case SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID:
178bf215546Sopenharmony_ci         return nir_load_barycentric(b, nir_intrinsic_load_barycentric_centroid,
179bf215546Sopenharmony_ci                                     INTERP_MODE_NOPERSPECTIVE);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci      case SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE:
182bf215546Sopenharmony_ci         return nir_load_barycentric(b, nir_intrinsic_load_barycentric_sample,
183bf215546Sopenharmony_ci                                     INTERP_MODE_NOPERSPECTIVE);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci      case SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL:
186bf215546Sopenharmony_ci         return nir_load_barycentric(b, nir_intrinsic_load_barycentric_pixel,
187bf215546Sopenharmony_ci                                     INTERP_MODE_SMOOTH);
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci      case SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID:
190bf215546Sopenharmony_ci         return nir_load_barycentric(b, nir_intrinsic_load_barycentric_centroid,
191bf215546Sopenharmony_ci                                     INTERP_MODE_SMOOTH);
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci      case SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE:
194bf215546Sopenharmony_ci         return nir_load_barycentric(b, nir_intrinsic_load_barycentric_sample,
195bf215546Sopenharmony_ci                                     INTERP_MODE_SMOOTH);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci      case SYSTEM_VALUE_BARYCENTRIC_PULL_MODEL:
198bf215546Sopenharmony_ci         return nir_load_barycentric(b, nir_intrinsic_load_barycentric_model,
199bf215546Sopenharmony_ci                                     INTERP_MODE_NONE);
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci      case SYSTEM_VALUE_HELPER_INVOCATION: {
202bf215546Sopenharmony_ci         /* When demote operation is used, reading the HelperInvocation
203bf215546Sopenharmony_ci          * needs to use Volatile memory access semantics to provide the
204bf215546Sopenharmony_ci          * correct (dynamic) value.  See OpDemoteToHelperInvocation.
205bf215546Sopenharmony_ci          */
206bf215546Sopenharmony_ci         if (nir_intrinsic_access(intrin) & ACCESS_VOLATILE)
207bf215546Sopenharmony_ci            return nir_is_helper_invocation(b, 1);
208bf215546Sopenharmony_ci         break;
209bf215546Sopenharmony_ci      }
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci      case SYSTEM_VALUE_MESH_VIEW_INDICES:
212bf215546Sopenharmony_ci         return nir_load_mesh_view_indices(b, intrin->dest.ssa.num_components,
213bf215546Sopenharmony_ci               bit_size, column, .base = 0,
214bf215546Sopenharmony_ci               .range = intrin->dest.ssa.num_components * bit_size / 8);
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci      default:
217bf215546Sopenharmony_ci         break;
218bf215546Sopenharmony_ci      }
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci      nir_intrinsic_op sysval_op =
221bf215546Sopenharmony_ci         nir_intrinsic_from_system_value(var->data.location);
222bf215546Sopenharmony_ci      if (glsl_type_is_matrix(var->type)) {
223bf215546Sopenharmony_ci         assert(nir_intrinsic_infos[sysval_op].index_map[NIR_INTRINSIC_COLUMN] > 0);
224bf215546Sopenharmony_ci         unsigned num_cols = glsl_get_matrix_columns(var->type);
225bf215546Sopenharmony_ci         ASSERTED unsigned num_rows = glsl_get_vector_elements(var->type);
226bf215546Sopenharmony_ci         assert(num_rows == intrin->dest.ssa.num_components);
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci         nir_ssa_def *cols[4];
229bf215546Sopenharmony_ci         for (unsigned i = 0; i < num_cols; i++) {
230bf215546Sopenharmony_ci            cols[i] = nir_load_system_value(b, sysval_op, i,
231bf215546Sopenharmony_ci                                            intrin->dest.ssa.num_components,
232bf215546Sopenharmony_ci                                            intrin->dest.ssa.bit_size);
233bf215546Sopenharmony_ci            assert(cols[i]->num_components == num_rows);
234bf215546Sopenharmony_ci         }
235bf215546Sopenharmony_ci         return nir_select_from_ssa_def_array(b, cols, num_cols, column);
236bf215546Sopenharmony_ci      } else {
237bf215546Sopenharmony_ci         return nir_load_system_value(b, sysval_op, 0,
238bf215546Sopenharmony_ci                                      intrin->dest.ssa.num_components,
239bf215546Sopenharmony_ci                                      intrin->dest.ssa.bit_size);
240bf215546Sopenharmony_ci      }
241bf215546Sopenharmony_ci   }
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   default:
244bf215546Sopenharmony_ci      return NULL;
245bf215546Sopenharmony_ci   }
246bf215546Sopenharmony_ci}
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_cibool
249bf215546Sopenharmony_cinir_lower_system_values(nir_shader *shader)
250bf215546Sopenharmony_ci{
251bf215546Sopenharmony_ci   bool progress = nir_shader_lower_instructions(shader,
252bf215546Sopenharmony_ci                                                 lower_system_value_filter,
253bf215546Sopenharmony_ci                                                 lower_system_value_instr,
254bf215546Sopenharmony_ci                                                 NULL);
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci   /* We're going to delete the variables so we need to clean up all those
257bf215546Sopenharmony_ci    * derefs we left lying around.
258bf215546Sopenharmony_ci    */
259bf215546Sopenharmony_ci   if (progress)
260bf215546Sopenharmony_ci      nir_remove_dead_derefs(shader);
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci   nir_foreach_variable_with_modes_safe(var, shader, nir_var_system_value)
263bf215546Sopenharmony_ci      exec_node_remove(&var->node);
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   return progress;
266bf215546Sopenharmony_ci}
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_cistatic nir_ssa_def *
269bf215546Sopenharmony_cilower_id_to_index_no_umod(nir_builder *b, nir_ssa_def *index,
270bf215546Sopenharmony_ci                          nir_ssa_def *size, unsigned bit_size)
271bf215546Sopenharmony_ci{
272bf215546Sopenharmony_ci   /* We lower ID to Index with the following formula:
273bf215546Sopenharmony_ci    *
274bf215546Sopenharmony_ci    *    id.z = index / (size.x * size.y)
275bf215546Sopenharmony_ci    *    id.y = (index - (id.z * (size.x * size.y))) / size.x
276bf215546Sopenharmony_ci    *    id.x = index - ((id.z * (size.x * size.y)) + (id.y * size.x))
277bf215546Sopenharmony_ci    *
278bf215546Sopenharmony_ci    * This is more efficient on HW that doesn't have a
279bf215546Sopenharmony_ci    * modulo division instruction and when the size is either
280bf215546Sopenharmony_ci    * not compile time known or not a power of two.
281bf215546Sopenharmony_ci    */
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci   nir_ssa_def *size_x = nir_channel(b, size, 0);
284bf215546Sopenharmony_ci   nir_ssa_def *size_y = nir_channel(b, size, 1);
285bf215546Sopenharmony_ci   nir_ssa_def *size_x_y = nir_imul(b, size_x, size_y);
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci   nir_ssa_def *id_z = nir_udiv(b, index, size_x_y);
288bf215546Sopenharmony_ci   nir_ssa_def *z_portion = nir_imul(b, id_z, size_x_y);
289bf215546Sopenharmony_ci   nir_ssa_def *id_y = nir_udiv(b, nir_isub(b, index, z_portion), size_x);
290bf215546Sopenharmony_ci   nir_ssa_def *y_portion = nir_imul(b, id_y, size_x);
291bf215546Sopenharmony_ci   nir_ssa_def *id_x = nir_isub(b, index, nir_iadd(b, z_portion, y_portion));
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_ci   return nir_u2u(b, nir_vec3(b, id_x, id_y, id_z), bit_size);
294bf215546Sopenharmony_ci}
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_cistatic nir_ssa_def *
298bf215546Sopenharmony_cilower_id_to_index(nir_builder *b, nir_ssa_def *index, nir_ssa_def *size,
299bf215546Sopenharmony_ci                  unsigned bit_size)
300bf215546Sopenharmony_ci{
301bf215546Sopenharmony_ci   /* We lower gl_LocalInvocationID to gl_LocalInvocationIndex based
302bf215546Sopenharmony_ci    * on this formula:
303bf215546Sopenharmony_ci    *
304bf215546Sopenharmony_ci    *    id.x = index % size.x;
305bf215546Sopenharmony_ci    *    id.y = (index / size.x) % gl_WorkGroupSize.y;
306bf215546Sopenharmony_ci    *    id.z = (index / (size.x * size.y)) % size.z;
307bf215546Sopenharmony_ci    *
308bf215546Sopenharmony_ci    * However, the final % size.z does nothing unless we
309bf215546Sopenharmony_ci    * accidentally end up with an index that is too
310bf215546Sopenharmony_ci    * large so it can safely be omitted.
311bf215546Sopenharmony_ci    *
312bf215546Sopenharmony_ci    * Because no hardware supports a local workgroup size greater than
313bf215546Sopenharmony_ci    * about 1K, this calculation can be done in 32-bit and can save some
314bf215546Sopenharmony_ci    * 64-bit arithmetic.
315bf215546Sopenharmony_ci    */
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci   nir_ssa_def *size_x = nir_channel(b, size, 0);
318bf215546Sopenharmony_ci   nir_ssa_def *size_y = nir_channel(b, size, 1);
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci   nir_ssa_def *id_x = nir_umod(b, index, size_x);
321bf215546Sopenharmony_ci   nir_ssa_def *id_y = nir_umod(b, nir_udiv(b, index, size_x), size_y);
322bf215546Sopenharmony_ci   nir_ssa_def *id_z = nir_udiv(b, index, nir_imul(b, size_x, size_y));
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci   return nir_u2u(b, nir_vec3(b, id_x, id_y, id_z), bit_size);
325bf215546Sopenharmony_ci}
326bf215546Sopenharmony_ci
327bf215546Sopenharmony_cistatic bool
328bf215546Sopenharmony_cilower_compute_system_value_filter(const nir_instr *instr, const void *_state)
329bf215546Sopenharmony_ci{
330bf215546Sopenharmony_ci   return instr->type == nir_instr_type_intrinsic;
331bf215546Sopenharmony_ci}
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_cistatic nir_ssa_def *
334bf215546Sopenharmony_cilower_compute_system_value_instr(nir_builder *b,
335bf215546Sopenharmony_ci                                 nir_instr *instr, void *_state)
336bf215546Sopenharmony_ci{
337bf215546Sopenharmony_ci   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
338bf215546Sopenharmony_ci   struct lower_sysval_state *state = (struct lower_sysval_state *)_state;
339bf215546Sopenharmony_ci   const nir_lower_compute_system_values_options *options = state->options;
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci   /* All the intrinsics we care about are loads */
342bf215546Sopenharmony_ci   if (!nir_intrinsic_infos[intrin->intrinsic].has_dest)
343bf215546Sopenharmony_ci      return NULL;
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ci   assert(intrin->dest.is_ssa);
346bf215546Sopenharmony_ci   const unsigned bit_size = intrin->dest.ssa.bit_size;
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_ci   switch (intrin->intrinsic) {
349bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_id:
350bf215546Sopenharmony_ci      /* If lower_cs_local_id_to_index is true, then we replace
351bf215546Sopenharmony_ci       * local_invocation_id with a formula based on local_invocation_index.
352bf215546Sopenharmony_ci       */
353bf215546Sopenharmony_ci      if (b->shader->options->lower_cs_local_id_to_index ||
354bf215546Sopenharmony_ci          (options && options->lower_cs_local_id_to_index)) {
355bf215546Sopenharmony_ci         nir_ssa_def *local_index = nir_load_local_invocation_index(b);
356bf215546Sopenharmony_ci         nir_ssa_def *local_size = nir_load_workgroup_size(b);
357bf215546Sopenharmony_ci         return lower_id_to_index(b, local_index, local_size, bit_size);
358bf215546Sopenharmony_ci      }
359bf215546Sopenharmony_ci      if (options && options->shuffle_local_ids_for_quad_derivatives &&
360bf215546Sopenharmony_ci          b->shader->info.cs.derivative_group == DERIVATIVE_GROUP_QUADS &&
361bf215546Sopenharmony_ci          _mesa_set_search(state->lower_once_list, instr) == NULL) {
362bf215546Sopenharmony_ci         nir_ssa_def *ids = nir_load_local_invocation_id(b);
363bf215546Sopenharmony_ci         _mesa_set_add(state->lower_once_list, ids->parent_instr);
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci         nir_ssa_def *x = nir_channel(b, ids, 0);
366bf215546Sopenharmony_ci         nir_ssa_def *y = nir_channel(b, ids, 1);
367bf215546Sopenharmony_ci         nir_ssa_def *z = nir_channel(b, ids, 2);
368bf215546Sopenharmony_ci         unsigned size_x = b->shader->info.workgroup_size[0];
369bf215546Sopenharmony_ci         nir_ssa_def *size_x_imm;
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci         if (b->shader->info.workgroup_size_variable)
372bf215546Sopenharmony_ci            size_x_imm = nir_channel(b, nir_load_workgroup_size(b), 0);
373bf215546Sopenharmony_ci         else
374bf215546Sopenharmony_ci            size_x_imm = nir_imm_int(b, size_x);
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci         /* Remap indices from:
377bf215546Sopenharmony_ci          *    | 0| 1| 2| 3|
378bf215546Sopenharmony_ci          *    | 4| 5| 6| 7|
379bf215546Sopenharmony_ci          *    | 8| 9|10|11|
380bf215546Sopenharmony_ci          *    |12|13|14|15|
381bf215546Sopenharmony_ci          * to:
382bf215546Sopenharmony_ci          *    | 0| 1| 4| 5|
383bf215546Sopenharmony_ci          *    | 2| 3| 6| 7|
384bf215546Sopenharmony_ci          *    | 8| 9|12|13|
385bf215546Sopenharmony_ci          *    |10|11|14|15|
386bf215546Sopenharmony_ci          *
387bf215546Sopenharmony_ci          * That's the layout required by AMD hardware for derivatives to
388bf215546Sopenharmony_ci          * work. Other hardware may work differently.
389bf215546Sopenharmony_ci          *
390bf215546Sopenharmony_ci          * It's a classic tiling pattern that can be implemented by inserting
391bf215546Sopenharmony_ci          * bit y[0] between bits x[0] and x[1] like this:
392bf215546Sopenharmony_ci          *
393bf215546Sopenharmony_ci          *    x[0],y[0],x[1],...x[last],y[1],...,y[last]
394bf215546Sopenharmony_ci          *
395bf215546Sopenharmony_ci          * If the width is a power of two, use:
396bf215546Sopenharmony_ci          *    i = ((x & 1) | ((y & 1) << 1) | ((x & ~1) << 1)) | ((y & ~1) << logbase2(size_x))
397bf215546Sopenharmony_ci          *
398bf215546Sopenharmony_ci          * If the width is not a power of two or the local size is variable, use:
399bf215546Sopenharmony_ci          *    i = ((x & 1) | ((y & 1) << 1) | ((x & ~1) << 1)) + ((y & ~1) * size_x)
400bf215546Sopenharmony_ci          *
401bf215546Sopenharmony_ci          * GL_NV_compute_shader_derivatives requires that the width and height
402bf215546Sopenharmony_ci          * are a multiple of two, which is also a requirement for the second
403bf215546Sopenharmony_ci          * expression to work.
404bf215546Sopenharmony_ci          *
405bf215546Sopenharmony_ci          * The 2D result is: (x,y) = (i % w, i / w)
406bf215546Sopenharmony_ci          */
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci         nir_ssa_def *one = nir_imm_int(b, 1);
409bf215546Sopenharmony_ci         nir_ssa_def *inv_one = nir_imm_int(b, ~1);
410bf215546Sopenharmony_ci         nir_ssa_def *x_bit0 = nir_iand(b, x, one);
411bf215546Sopenharmony_ci         nir_ssa_def *y_bit0 = nir_iand(b, y, one);
412bf215546Sopenharmony_ci         nir_ssa_def *x_bits_1n = nir_iand(b, x, inv_one);
413bf215546Sopenharmony_ci         nir_ssa_def *y_bits_1n = nir_iand(b, y, inv_one);
414bf215546Sopenharmony_ci         nir_ssa_def *bits_01 = nir_ior(b, x_bit0, nir_ishl(b, y_bit0, one));
415bf215546Sopenharmony_ci         nir_ssa_def *bits_01x = nir_ior(b, bits_01,
416bf215546Sopenharmony_ci                                         nir_ishl(b, x_bits_1n, one));
417bf215546Sopenharmony_ci         nir_ssa_def *i;
418bf215546Sopenharmony_ci
419bf215546Sopenharmony_ci         if (!b->shader->info.workgroup_size_variable &&
420bf215546Sopenharmony_ci             util_is_power_of_two_nonzero(size_x)) {
421bf215546Sopenharmony_ci            nir_ssa_def *log2_size_x = nir_imm_int(b, util_logbase2(size_x));
422bf215546Sopenharmony_ci            i = nir_ior(b, bits_01x, nir_ishl(b, y_bits_1n, log2_size_x));
423bf215546Sopenharmony_ci         } else {
424bf215546Sopenharmony_ci            i = nir_iadd(b, bits_01x, nir_imul(b, y_bits_1n, size_x_imm));
425bf215546Sopenharmony_ci         }
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_ci         /* This should be fast if size_x is an immediate or even a power
428bf215546Sopenharmony_ci          * of two.
429bf215546Sopenharmony_ci          */
430bf215546Sopenharmony_ci         x = nir_umod(b, i, size_x_imm);
431bf215546Sopenharmony_ci         y = nir_udiv(b, i, size_x_imm);
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci         return nir_vec3(b, x, y, z);
434bf215546Sopenharmony_ci      }
435bf215546Sopenharmony_ci
436bf215546Sopenharmony_ci      /* If a workgroup size dimension is 1, then the local invocation id must be zero. */
437bf215546Sopenharmony_ci      nir_component_mask_t is_zero = 0;
438bf215546Sopenharmony_ci      is_zero |= b->shader->info.workgroup_size[0] == 1 ? 0x1 : 0x0;
439bf215546Sopenharmony_ci      is_zero |= b->shader->info.workgroup_size[1] == 1 ? 0x2 : 0x0;
440bf215546Sopenharmony_ci      is_zero |= b->shader->info.workgroup_size[2] == 1 ? 0x4 : 0x0;
441bf215546Sopenharmony_ci      if (!b->shader->info.workgroup_size_variable && is_zero) {
442bf215546Sopenharmony_ci         nir_ssa_scalar defs[3];
443bf215546Sopenharmony_ci         for (unsigned i = 0; i < 3; i++) {
444bf215546Sopenharmony_ci            defs[i] = is_zero & (1 << i) ? nir_get_ssa_scalar(nir_imm_zero(b, 1, 32), 0) :
445bf215546Sopenharmony_ci                                           nir_get_ssa_scalar(&intrin->dest.ssa, i);
446bf215546Sopenharmony_ci         }
447bf215546Sopenharmony_ci         return nir_vec_scalars(b, defs, 3);
448bf215546Sopenharmony_ci      }
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci      return NULL;
451bf215546Sopenharmony_ci
452bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_index:
453bf215546Sopenharmony_ci      /* If lower_cs_local_index_to_id is true, then we replace
454bf215546Sopenharmony_ci       * local_invocation_index with a formula based on local_invocation_id.
455bf215546Sopenharmony_ci       */
456bf215546Sopenharmony_ci      if (b->shader->options->lower_cs_local_index_to_id ||
457bf215546Sopenharmony_ci          (options && options->lower_local_invocation_index)) {
458bf215546Sopenharmony_ci         /* From the GLSL man page for gl_LocalInvocationIndex:
459bf215546Sopenharmony_ci          *
460bf215546Sopenharmony_ci          *    "The value of gl_LocalInvocationIndex is equal to
461bf215546Sopenharmony_ci          *    gl_LocalInvocationID.z * gl_WorkGroupSize.x *
462bf215546Sopenharmony_ci          *    gl_WorkGroupSize.y + gl_LocalInvocationID.y *
463bf215546Sopenharmony_ci          *    gl_WorkGroupSize.x + gl_LocalInvocationID.x"
464bf215546Sopenharmony_ci          */
465bf215546Sopenharmony_ci         nir_ssa_def *local_id = nir_load_local_invocation_id(b);
466bf215546Sopenharmony_ci         nir_ssa_def *local_size = nir_load_workgroup_size(b);
467bf215546Sopenharmony_ci         nir_ssa_def *size_x = nir_channel(b, local_size, 0);
468bf215546Sopenharmony_ci         nir_ssa_def *size_y = nir_channel(b, local_size, 1);
469bf215546Sopenharmony_ci
470bf215546Sopenharmony_ci         /* Because no hardware supports a local workgroup size greater than
471bf215546Sopenharmony_ci          * about 1K, this calculation can be done in 32-bit and can save some
472bf215546Sopenharmony_ci          * 64-bit arithmetic.
473bf215546Sopenharmony_ci          */
474bf215546Sopenharmony_ci         nir_ssa_def *index;
475bf215546Sopenharmony_ci         index = nir_imul(b, nir_channel(b, local_id, 2),
476bf215546Sopenharmony_ci                             nir_imul(b, size_x, size_y));
477bf215546Sopenharmony_ci         index = nir_iadd(b, index,
478bf215546Sopenharmony_ci                             nir_imul(b, nir_channel(b, local_id, 1), size_x));
479bf215546Sopenharmony_ci         index = nir_iadd(b, index, nir_channel(b, local_id, 0));
480bf215546Sopenharmony_ci         return nir_u2u(b, index, bit_size);
481bf215546Sopenharmony_ci      } else {
482bf215546Sopenharmony_ci         return NULL;
483bf215546Sopenharmony_ci      }
484bf215546Sopenharmony_ci
485bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_size:
486bf215546Sopenharmony_ci      if (b->shader->info.workgroup_size_variable) {
487bf215546Sopenharmony_ci         /* If the local work group size is variable it can't be lowered at
488bf215546Sopenharmony_ci          * this point.  We do, however, have to make sure that the intrinsic
489bf215546Sopenharmony_ci          * is only 32-bit.
490bf215546Sopenharmony_ci          */
491bf215546Sopenharmony_ci         return NULL;
492bf215546Sopenharmony_ci      } else {
493bf215546Sopenharmony_ci         /* using a 32 bit constant is safe here as no device/driver needs more
494bf215546Sopenharmony_ci          * than 32 bits for the local size */
495bf215546Sopenharmony_ci         nir_const_value workgroup_size_const[3];
496bf215546Sopenharmony_ci         memset(workgroup_size_const, 0, sizeof(workgroup_size_const));
497bf215546Sopenharmony_ci         workgroup_size_const[0].u32 = b->shader->info.workgroup_size[0];
498bf215546Sopenharmony_ci         workgroup_size_const[1].u32 = b->shader->info.workgroup_size[1];
499bf215546Sopenharmony_ci         workgroup_size_const[2].u32 = b->shader->info.workgroup_size[2];
500bf215546Sopenharmony_ci         return nir_u2u(b, nir_build_imm(b, 3, 32, workgroup_size_const), bit_size);
501bf215546Sopenharmony_ci      }
502bf215546Sopenharmony_ci
503bf215546Sopenharmony_ci   case nir_intrinsic_load_global_invocation_id_zero_base: {
504bf215546Sopenharmony_ci      if ((options && options->has_base_workgroup_id) ||
505bf215546Sopenharmony_ci          !b->shader->options->has_cs_global_id) {
506bf215546Sopenharmony_ci         nir_ssa_def *group_size = nir_load_workgroup_size(b);
507bf215546Sopenharmony_ci         nir_ssa_def *group_id = nir_load_workgroup_id(b, bit_size);
508bf215546Sopenharmony_ci         nir_ssa_def *local_id = nir_load_local_invocation_id(b);
509bf215546Sopenharmony_ci
510bf215546Sopenharmony_ci         return nir_iadd(b, nir_imul(b, group_id,
511bf215546Sopenharmony_ci                                        nir_u2u(b, group_size, bit_size)),
512bf215546Sopenharmony_ci                            nir_u2u(b, local_id, bit_size));
513bf215546Sopenharmony_ci      } else {
514bf215546Sopenharmony_ci         return NULL;
515bf215546Sopenharmony_ci      }
516bf215546Sopenharmony_ci   }
517bf215546Sopenharmony_ci
518bf215546Sopenharmony_ci   case nir_intrinsic_load_global_invocation_id: {
519bf215546Sopenharmony_ci      if (options && options->has_base_global_invocation_id)
520bf215546Sopenharmony_ci         return nir_iadd(b, nir_load_global_invocation_id_zero_base(b, bit_size),
521bf215546Sopenharmony_ci                            nir_load_base_global_invocation_id(b, bit_size));
522bf215546Sopenharmony_ci      else if ((options && options->has_base_workgroup_id) ||
523bf215546Sopenharmony_ci               !b->shader->options->has_cs_global_id)
524bf215546Sopenharmony_ci         return nir_load_global_invocation_id_zero_base(b, bit_size);
525bf215546Sopenharmony_ci      else
526bf215546Sopenharmony_ci         return NULL;
527bf215546Sopenharmony_ci   }
528bf215546Sopenharmony_ci
529bf215546Sopenharmony_ci   case nir_intrinsic_load_global_invocation_index: {
530bf215546Sopenharmony_ci      /* OpenCL's global_linear_id explicitly removes the global offset before computing this */
531bf215546Sopenharmony_ci      assert(b->shader->info.stage == MESA_SHADER_KERNEL);
532bf215546Sopenharmony_ci      nir_ssa_def *global_base_id = nir_load_base_global_invocation_id(b, bit_size);
533bf215546Sopenharmony_ci      nir_ssa_def *global_id = nir_isub(b, nir_load_global_invocation_id(b, bit_size), global_base_id);
534bf215546Sopenharmony_ci      nir_ssa_def *global_size = build_global_group_size(b, bit_size);
535bf215546Sopenharmony_ci
536bf215546Sopenharmony_ci      /* index = id.x + ((id.y + (id.z * size.y)) * size.x) */
537bf215546Sopenharmony_ci      nir_ssa_def *index;
538bf215546Sopenharmony_ci      index = nir_imul(b, nir_channel(b, global_id, 2),
539bf215546Sopenharmony_ci                          nir_channel(b, global_size, 1));
540bf215546Sopenharmony_ci      index = nir_iadd(b, nir_channel(b, global_id, 1), index);
541bf215546Sopenharmony_ci      index = nir_imul(b, nir_channel(b, global_size, 0), index);
542bf215546Sopenharmony_ci      index = nir_iadd(b, nir_channel(b, global_id, 0), index);
543bf215546Sopenharmony_ci      return index;
544bf215546Sopenharmony_ci   }
545bf215546Sopenharmony_ci
546bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_id: {
547bf215546Sopenharmony_ci      if (options && options->has_base_workgroup_id)
548bf215546Sopenharmony_ci         return nir_iadd(b, nir_u2u(b, nir_load_workgroup_id_zero_base(b), bit_size),
549bf215546Sopenharmony_ci                            nir_load_base_workgroup_id(b, bit_size));
550bf215546Sopenharmony_ci      else if (options && options->lower_workgroup_id_to_index)
551bf215546Sopenharmony_ci         return lower_id_to_index_no_umod(b, nir_load_workgroup_index(b),
552bf215546Sopenharmony_ci                                          nir_load_num_workgroups(b, bit_size),
553bf215546Sopenharmony_ci                                          bit_size);
554bf215546Sopenharmony_ci
555bf215546Sopenharmony_ci      return NULL;
556bf215546Sopenharmony_ci
557bf215546Sopenharmony_ci   }
558bf215546Sopenharmony_ci
559bf215546Sopenharmony_ci   default:
560bf215546Sopenharmony_ci      return NULL;
561bf215546Sopenharmony_ci   }
562bf215546Sopenharmony_ci}
563bf215546Sopenharmony_ci
564bf215546Sopenharmony_cibool
565bf215546Sopenharmony_cinir_lower_compute_system_values(nir_shader *shader,
566bf215546Sopenharmony_ci                                const nir_lower_compute_system_values_options *options)
567bf215546Sopenharmony_ci{
568bf215546Sopenharmony_ci   if (!gl_shader_stage_uses_workgroup(shader->info.stage))
569bf215546Sopenharmony_ci      return false;
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci   struct lower_sysval_state state;
572bf215546Sopenharmony_ci   state.options = options;
573bf215546Sopenharmony_ci   state.lower_once_list = _mesa_pointer_set_create(NULL);
574bf215546Sopenharmony_ci
575bf215546Sopenharmony_ci   bool progress =
576bf215546Sopenharmony_ci      nir_shader_lower_instructions(shader,
577bf215546Sopenharmony_ci                                    lower_compute_system_value_filter,
578bf215546Sopenharmony_ci                                    lower_compute_system_value_instr,
579bf215546Sopenharmony_ci                                    (void*)&state);
580bf215546Sopenharmony_ci   ralloc_free(state.lower_once_list);
581bf215546Sopenharmony_ci
582bf215546Sopenharmony_ci   /* Update this so as not to lower it again. */
583bf215546Sopenharmony_ci   if (options && options->shuffle_local_ids_for_quad_derivatives &&
584bf215546Sopenharmony_ci       shader->info.cs.derivative_group == DERIVATIVE_GROUP_QUADS)
585bf215546Sopenharmony_ci      shader->info.cs.derivative_group = DERIVATIVE_GROUP_LINEAR;
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_ci   return progress;
588bf215546Sopenharmony_ci}
589