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
24bf215546Sopenharmony_ci#include "brw_nir.h"
25bf215546Sopenharmony_ci#include "brw_nir_rt.h"
26bf215546Sopenharmony_ci#include "brw_shader.h"
27bf215546Sopenharmony_ci#include "dev/intel_debug.h"
28bf215546Sopenharmony_ci#include "compiler/glsl_types.h"
29bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h"
30bf215546Sopenharmony_ci#include "util/u_math.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistatic bool
33bf215546Sopenharmony_ciremap_tess_levels(nir_builder *b, nir_intrinsic_instr *intr,
34bf215546Sopenharmony_ci                  enum tess_primitive_mode _primitive_mode)
35bf215546Sopenharmony_ci{
36bf215546Sopenharmony_ci   const int location = nir_intrinsic_base(intr);
37bf215546Sopenharmony_ci   const unsigned component = nir_intrinsic_component(intr);
38bf215546Sopenharmony_ci   bool out_of_bounds;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci   if (location == VARYING_SLOT_TESS_LEVEL_INNER) {
41bf215546Sopenharmony_ci      switch (_primitive_mode) {
42bf215546Sopenharmony_ci      case TESS_PRIMITIVE_QUADS:
43bf215546Sopenharmony_ci         /* gl_TessLevelInner[0..1] lives at DWords 3-2 (reversed). */
44bf215546Sopenharmony_ci         nir_intrinsic_set_base(intr, 0);
45bf215546Sopenharmony_ci         nir_intrinsic_set_component(intr, 3 - component);
46bf215546Sopenharmony_ci         out_of_bounds = false;
47bf215546Sopenharmony_ci         break;
48bf215546Sopenharmony_ci      case TESS_PRIMITIVE_TRIANGLES:
49bf215546Sopenharmony_ci         /* gl_TessLevelInner[0] lives at DWord 4. */
50bf215546Sopenharmony_ci         nir_intrinsic_set_base(intr, 1);
51bf215546Sopenharmony_ci         out_of_bounds = component > 0;
52bf215546Sopenharmony_ci         break;
53bf215546Sopenharmony_ci      case TESS_PRIMITIVE_ISOLINES:
54bf215546Sopenharmony_ci         out_of_bounds = true;
55bf215546Sopenharmony_ci         break;
56bf215546Sopenharmony_ci      default:
57bf215546Sopenharmony_ci         unreachable("Bogus tessellation domain");
58bf215546Sopenharmony_ci      }
59bf215546Sopenharmony_ci   } else if (location == VARYING_SLOT_TESS_LEVEL_OUTER) {
60bf215546Sopenharmony_ci      if (_primitive_mode == TESS_PRIMITIVE_ISOLINES) {
61bf215546Sopenharmony_ci         /* gl_TessLevelOuter[0..1] lives at DWords 6-7 (in order). */
62bf215546Sopenharmony_ci         nir_intrinsic_set_base(intr, 1);
63bf215546Sopenharmony_ci         nir_intrinsic_set_component(intr, 2 + nir_intrinsic_component(intr));
64bf215546Sopenharmony_ci         out_of_bounds = component > 1;
65bf215546Sopenharmony_ci      } else {
66bf215546Sopenharmony_ci         /* Triangles use DWords 7-5 (reversed); Quads use 7-4 (reversed) */
67bf215546Sopenharmony_ci         nir_intrinsic_set_base(intr, 1);
68bf215546Sopenharmony_ci         nir_intrinsic_set_component(intr, 3 - nir_intrinsic_component(intr));
69bf215546Sopenharmony_ci         out_of_bounds = component == 3 && _primitive_mode == TESS_PRIMITIVE_TRIANGLES;
70bf215546Sopenharmony_ci      }
71bf215546Sopenharmony_ci   } else {
72bf215546Sopenharmony_ci      return false;
73bf215546Sopenharmony_ci   }
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   if (out_of_bounds) {
76bf215546Sopenharmony_ci      if (nir_intrinsic_infos[intr->intrinsic].has_dest) {
77bf215546Sopenharmony_ci         b->cursor = nir_before_instr(&intr->instr);
78bf215546Sopenharmony_ci         nir_ssa_def *undef = nir_ssa_undef(b, 1, 32);
79bf215546Sopenharmony_ci         nir_ssa_def_rewrite_uses(&intr->dest.ssa, undef);
80bf215546Sopenharmony_ci      }
81bf215546Sopenharmony_ci      nir_instr_remove(&intr->instr);
82bf215546Sopenharmony_ci   }
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   return true;
85bf215546Sopenharmony_ci}
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_cistatic bool
88bf215546Sopenharmony_ciis_input(nir_intrinsic_instr *intrin)
89bf215546Sopenharmony_ci{
90bf215546Sopenharmony_ci   return intrin->intrinsic == nir_intrinsic_load_input ||
91bf215546Sopenharmony_ci          intrin->intrinsic == nir_intrinsic_load_per_vertex_input ||
92bf215546Sopenharmony_ci          intrin->intrinsic == nir_intrinsic_load_interpolated_input;
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistatic bool
96bf215546Sopenharmony_ciis_output(nir_intrinsic_instr *intrin)
97bf215546Sopenharmony_ci{
98bf215546Sopenharmony_ci   return intrin->intrinsic == nir_intrinsic_load_output ||
99bf215546Sopenharmony_ci          intrin->intrinsic == nir_intrinsic_load_per_vertex_output ||
100bf215546Sopenharmony_ci          intrin->intrinsic == nir_intrinsic_store_output ||
101bf215546Sopenharmony_ci          intrin->intrinsic == nir_intrinsic_store_per_vertex_output;
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_cistatic bool
106bf215546Sopenharmony_ciremap_patch_urb_offsets(nir_block *block, nir_builder *b,
107bf215546Sopenharmony_ci                        const struct brw_vue_map *vue_map,
108bf215546Sopenharmony_ci                        enum tess_primitive_mode tes_primitive_mode)
109bf215546Sopenharmony_ci{
110bf215546Sopenharmony_ci   const bool is_passthrough_tcs = b->shader->info.name &&
111bf215546Sopenharmony_ci      strcmp(b->shader->info.name, "passthrough TCS") == 0;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   nir_foreach_instr_safe(instr, block) {
114bf215546Sopenharmony_ci      if (instr->type != nir_instr_type_intrinsic)
115bf215546Sopenharmony_ci         continue;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci      gl_shader_stage stage = b->shader->info.stage;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci      if ((stage == MESA_SHADER_TESS_CTRL && is_output(intrin)) ||
122bf215546Sopenharmony_ci          (stage == MESA_SHADER_TESS_EVAL && is_input(intrin))) {
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci         if (!is_passthrough_tcs &&
125bf215546Sopenharmony_ci             remap_tess_levels(b, intrin, tes_primitive_mode))
126bf215546Sopenharmony_ci            continue;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci         int vue_slot = vue_map->varying_to_slot[intrin->const_index[0]];
129bf215546Sopenharmony_ci         assert(vue_slot != -1);
130bf215546Sopenharmony_ci         intrin->const_index[0] = vue_slot;
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci         nir_src *vertex = nir_get_io_arrayed_index_src(intrin);
133bf215546Sopenharmony_ci         if (vertex) {
134bf215546Sopenharmony_ci            if (nir_src_is_const(*vertex)) {
135bf215546Sopenharmony_ci               intrin->const_index[0] += nir_src_as_uint(*vertex) *
136bf215546Sopenharmony_ci                                         vue_map->num_per_vertex_slots;
137bf215546Sopenharmony_ci            } else {
138bf215546Sopenharmony_ci               b->cursor = nir_before_instr(&intrin->instr);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci               /* Multiply by the number of per-vertex slots. */
141bf215546Sopenharmony_ci               nir_ssa_def *vertex_offset =
142bf215546Sopenharmony_ci                  nir_imul(b,
143bf215546Sopenharmony_ci                           nir_ssa_for_src(b, *vertex, 1),
144bf215546Sopenharmony_ci                           nir_imm_int(b,
145bf215546Sopenharmony_ci                                       vue_map->num_per_vertex_slots));
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci               /* Add it to the existing offset */
148bf215546Sopenharmony_ci               nir_src *offset = nir_get_io_offset_src(intrin);
149bf215546Sopenharmony_ci               nir_ssa_def *total_offset =
150bf215546Sopenharmony_ci                  nir_iadd(b, vertex_offset,
151bf215546Sopenharmony_ci                           nir_ssa_for_src(b, *offset, 1));
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci               nir_instr_rewrite_src(&intrin->instr, offset,
154bf215546Sopenharmony_ci                                     nir_src_for_ssa(total_offset));
155bf215546Sopenharmony_ci            }
156bf215546Sopenharmony_ci         }
157bf215546Sopenharmony_ci      }
158bf215546Sopenharmony_ci   }
159bf215546Sopenharmony_ci   return true;
160bf215546Sopenharmony_ci}
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_civoid
163bf215546Sopenharmony_cibrw_nir_lower_vs_inputs(nir_shader *nir,
164bf215546Sopenharmony_ci                        bool edgeflag_is_last,
165bf215546Sopenharmony_ci                        const uint8_t *vs_attrib_wa_flags)
166bf215546Sopenharmony_ci{
167bf215546Sopenharmony_ci   /* Start with the location of the variable's base. */
168bf215546Sopenharmony_ci   nir_foreach_shader_in_variable(var, nir)
169bf215546Sopenharmony_ci      var->data.driver_location = var->data.location;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   /* Now use nir_lower_io to walk dereference chains.  Attribute arrays are
172bf215546Sopenharmony_ci    * loaded as one vec4 or dvec4 per element (or matrix column), depending on
173bf215546Sopenharmony_ci    * whether it is a double-precision type or not.
174bf215546Sopenharmony_ci    */
175bf215546Sopenharmony_ci   nir_lower_io(nir, nir_var_shader_in, type_size_vec4,
176bf215546Sopenharmony_ci                nir_lower_io_lower_64bit_to_32);
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   /* This pass needs actual constants */
179bf215546Sopenharmony_ci   nir_opt_constant_folding(nir);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   brw_nir_apply_attribute_workarounds(nir, vs_attrib_wa_flags);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   /* The last step is to remap VERT_ATTRIB_* to actual registers */
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   /* Whether or not we have any system generated values.  gl_DrawID is not
188bf215546Sopenharmony_ci    * included here as it lives in its own vec4.
189bf215546Sopenharmony_ci    */
190bf215546Sopenharmony_ci   const bool has_sgvs =
191bf215546Sopenharmony_ci      BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_FIRST_VERTEX) ||
192bf215546Sopenharmony_ci      BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BASE_INSTANCE) ||
193bf215546Sopenharmony_ci      BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) ||
194bf215546Sopenharmony_ci      BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID);
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   const unsigned num_inputs = util_bitcount64(nir->info.inputs_read);
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci   nir_foreach_function(function, nir) {
199bf215546Sopenharmony_ci      if (!function->impl)
200bf215546Sopenharmony_ci         continue;
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci      nir_builder b;
203bf215546Sopenharmony_ci      nir_builder_init(&b, function->impl);
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci      nir_foreach_block(block, function->impl) {
206bf215546Sopenharmony_ci         nir_foreach_instr_safe(instr, block) {
207bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_intrinsic)
208bf215546Sopenharmony_ci               continue;
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci            nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci            switch (intrin->intrinsic) {
213bf215546Sopenharmony_ci            case nir_intrinsic_load_first_vertex:
214bf215546Sopenharmony_ci            case nir_intrinsic_load_base_instance:
215bf215546Sopenharmony_ci            case nir_intrinsic_load_vertex_id_zero_base:
216bf215546Sopenharmony_ci            case nir_intrinsic_load_instance_id:
217bf215546Sopenharmony_ci            case nir_intrinsic_load_is_indexed_draw:
218bf215546Sopenharmony_ci            case nir_intrinsic_load_draw_id: {
219bf215546Sopenharmony_ci               b.cursor = nir_after_instr(&intrin->instr);
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci               /* gl_VertexID and friends are stored by the VF as the last
222bf215546Sopenharmony_ci                * vertex element.  We convert them to load_input intrinsics at
223bf215546Sopenharmony_ci                * the right location.
224bf215546Sopenharmony_ci                */
225bf215546Sopenharmony_ci               nir_intrinsic_instr *load =
226bf215546Sopenharmony_ci                  nir_intrinsic_instr_create(nir, nir_intrinsic_load_input);
227bf215546Sopenharmony_ci               load->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci               nir_intrinsic_set_base(load, num_inputs);
230bf215546Sopenharmony_ci               switch (intrin->intrinsic) {
231bf215546Sopenharmony_ci               case nir_intrinsic_load_first_vertex:
232bf215546Sopenharmony_ci                  nir_intrinsic_set_component(load, 0);
233bf215546Sopenharmony_ci                  break;
234bf215546Sopenharmony_ci               case nir_intrinsic_load_base_instance:
235bf215546Sopenharmony_ci                  nir_intrinsic_set_component(load, 1);
236bf215546Sopenharmony_ci                  break;
237bf215546Sopenharmony_ci               case nir_intrinsic_load_vertex_id_zero_base:
238bf215546Sopenharmony_ci                  nir_intrinsic_set_component(load, 2);
239bf215546Sopenharmony_ci                  break;
240bf215546Sopenharmony_ci               case nir_intrinsic_load_instance_id:
241bf215546Sopenharmony_ci                  nir_intrinsic_set_component(load, 3);
242bf215546Sopenharmony_ci                  break;
243bf215546Sopenharmony_ci               case nir_intrinsic_load_draw_id:
244bf215546Sopenharmony_ci               case nir_intrinsic_load_is_indexed_draw:
245bf215546Sopenharmony_ci                  /* gl_DrawID and IsIndexedDraw are stored right after
246bf215546Sopenharmony_ci                   * gl_VertexID and friends if any of them exist.
247bf215546Sopenharmony_ci                   */
248bf215546Sopenharmony_ci                  nir_intrinsic_set_base(load, num_inputs + has_sgvs);
249bf215546Sopenharmony_ci                  if (intrin->intrinsic == nir_intrinsic_load_draw_id)
250bf215546Sopenharmony_ci                     nir_intrinsic_set_component(load, 0);
251bf215546Sopenharmony_ci                  else
252bf215546Sopenharmony_ci                     nir_intrinsic_set_component(load, 1);
253bf215546Sopenharmony_ci                  break;
254bf215546Sopenharmony_ci               default:
255bf215546Sopenharmony_ci                  unreachable("Invalid system value intrinsic");
256bf215546Sopenharmony_ci               }
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci               load->num_components = 1;
259bf215546Sopenharmony_ci               nir_ssa_dest_init(&load->instr, &load->dest, 1, 32, NULL);
260bf215546Sopenharmony_ci               nir_builder_instr_insert(&b, &load->instr);
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci               nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
263bf215546Sopenharmony_ci                                        &load->dest.ssa);
264bf215546Sopenharmony_ci               nir_instr_remove(&intrin->instr);
265bf215546Sopenharmony_ci               break;
266bf215546Sopenharmony_ci            }
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci            case nir_intrinsic_load_input: {
269bf215546Sopenharmony_ci               /* Attributes come in a contiguous block, ordered by their
270bf215546Sopenharmony_ci                * gl_vert_attrib value.  That means we can compute the slot
271bf215546Sopenharmony_ci                * number for an attribute by masking out the enabled attributes
272bf215546Sopenharmony_ci                * before it and counting the bits.
273bf215546Sopenharmony_ci                */
274bf215546Sopenharmony_ci               int attr = nir_intrinsic_base(intrin);
275bf215546Sopenharmony_ci               uint64_t inputs_read = nir->info.inputs_read;
276bf215546Sopenharmony_ci               int slot = -1;
277bf215546Sopenharmony_ci               if (edgeflag_is_last) {
278bf215546Sopenharmony_ci                  inputs_read &= ~BITFIELD64_BIT(VERT_ATTRIB_EDGEFLAG);
279bf215546Sopenharmony_ci                  if (attr == VERT_ATTRIB_EDGEFLAG)
280bf215546Sopenharmony_ci                     slot = num_inputs - 1;
281bf215546Sopenharmony_ci               }
282bf215546Sopenharmony_ci               if (slot == -1)
283bf215546Sopenharmony_ci                  slot = util_bitcount64(inputs_read &
284bf215546Sopenharmony_ci                                         BITFIELD64_MASK(attr));
285bf215546Sopenharmony_ci               nir_intrinsic_set_base(intrin, slot);
286bf215546Sopenharmony_ci               break;
287bf215546Sopenharmony_ci            }
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci            default:
290bf215546Sopenharmony_ci               break; /* Nothing to do */
291bf215546Sopenharmony_ci            }
292bf215546Sopenharmony_ci         }
293bf215546Sopenharmony_ci      }
294bf215546Sopenharmony_ci   }
295bf215546Sopenharmony_ci}
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_civoid
298bf215546Sopenharmony_cibrw_nir_lower_vue_inputs(nir_shader *nir,
299bf215546Sopenharmony_ci                         const struct brw_vue_map *vue_map)
300bf215546Sopenharmony_ci{
301bf215546Sopenharmony_ci   nir_foreach_shader_in_variable(var, nir)
302bf215546Sopenharmony_ci      var->data.driver_location = var->data.location;
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci   /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
305bf215546Sopenharmony_ci   nir_lower_io(nir, nir_var_shader_in, type_size_vec4,
306bf215546Sopenharmony_ci                nir_lower_io_lower_64bit_to_32);
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci   /* This pass needs actual constants */
309bf215546Sopenharmony_ci   nir_opt_constant_folding(nir);
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci   nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci   nir_foreach_function(function, nir) {
314bf215546Sopenharmony_ci      if (!function->impl)
315bf215546Sopenharmony_ci         continue;
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci      nir_foreach_block(block, function->impl) {
318bf215546Sopenharmony_ci         nir_foreach_instr(instr, block) {
319bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_intrinsic)
320bf215546Sopenharmony_ci               continue;
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci            nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci            if (intrin->intrinsic == nir_intrinsic_load_input ||
325bf215546Sopenharmony_ci                intrin->intrinsic == nir_intrinsic_load_per_vertex_input) {
326bf215546Sopenharmony_ci               /* Offset 0 is the VUE header, which contains
327bf215546Sopenharmony_ci                * VARYING_SLOT_LAYER [.y], VARYING_SLOT_VIEWPORT [.z], and
328bf215546Sopenharmony_ci                * VARYING_SLOT_PSIZ [.w].
329bf215546Sopenharmony_ci                */
330bf215546Sopenharmony_ci               int varying = nir_intrinsic_base(intrin);
331bf215546Sopenharmony_ci               int vue_slot;
332bf215546Sopenharmony_ci               switch (varying) {
333bf215546Sopenharmony_ci               case VARYING_SLOT_PSIZ:
334bf215546Sopenharmony_ci                  nir_intrinsic_set_base(intrin, 0);
335bf215546Sopenharmony_ci                  nir_intrinsic_set_component(intrin, 3);
336bf215546Sopenharmony_ci                  break;
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_ci               default:
339bf215546Sopenharmony_ci                  vue_slot = vue_map->varying_to_slot[varying];
340bf215546Sopenharmony_ci                  assert(vue_slot != -1);
341bf215546Sopenharmony_ci                  nir_intrinsic_set_base(intrin, vue_slot);
342bf215546Sopenharmony_ci                  break;
343bf215546Sopenharmony_ci               }
344bf215546Sopenharmony_ci            }
345bf215546Sopenharmony_ci         }
346bf215546Sopenharmony_ci      }
347bf215546Sopenharmony_ci   }
348bf215546Sopenharmony_ci}
349bf215546Sopenharmony_ci
350bf215546Sopenharmony_civoid
351bf215546Sopenharmony_cibrw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue_map)
352bf215546Sopenharmony_ci{
353bf215546Sopenharmony_ci   nir_foreach_shader_in_variable(var, nir)
354bf215546Sopenharmony_ci      var->data.driver_location = var->data.location;
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci   nir_lower_io(nir, nir_var_shader_in, type_size_vec4,
357bf215546Sopenharmony_ci                nir_lower_io_lower_64bit_to_32);
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci   /* This pass needs actual constants */
360bf215546Sopenharmony_ci   nir_opt_constant_folding(nir);
361bf215546Sopenharmony_ci
362bf215546Sopenharmony_ci   nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci   nir_foreach_function(function, nir) {
365bf215546Sopenharmony_ci      if (function->impl) {
366bf215546Sopenharmony_ci         nir_builder b;
367bf215546Sopenharmony_ci         nir_builder_init(&b, function->impl);
368bf215546Sopenharmony_ci         nir_foreach_block(block, function->impl) {
369bf215546Sopenharmony_ci            remap_patch_urb_offsets(block, &b, vue_map,
370bf215546Sopenharmony_ci                                    nir->info.tess._primitive_mode);
371bf215546Sopenharmony_ci         }
372bf215546Sopenharmony_ci      }
373bf215546Sopenharmony_ci   }
374bf215546Sopenharmony_ci}
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci/**
377bf215546Sopenharmony_ci * Convert interpolateAtOffset() offsets from [-0.5, +0.5] floating point
378bf215546Sopenharmony_ci * offsets to integer [-8, +7] offsets (in units of 1/16th of a pixel).
379bf215546Sopenharmony_ci *
380bf215546Sopenharmony_ci * We clamp to +7/16 on the upper end of the range, since +0.5 isn't
381bf215546Sopenharmony_ci * representable in a S0.4 value; a naive conversion would give us -8/16,
382bf215546Sopenharmony_ci * which is the opposite of what was intended.
383bf215546Sopenharmony_ci *
384bf215546Sopenharmony_ci * This is allowed by GL_ARB_gpu_shader5's quantization rules:
385bf215546Sopenharmony_ci *
386bf215546Sopenharmony_ci *    "Not all values of <offset> may be supported; x and y offsets may
387bf215546Sopenharmony_ci *     be rounded to fixed-point values with the number of fraction bits
388bf215546Sopenharmony_ci *     given by the implementation-dependent constant
389bf215546Sopenharmony_ci *     FRAGMENT_INTERPOLATION_OFFSET_BITS."
390bf215546Sopenharmony_ci */
391bf215546Sopenharmony_cistatic bool
392bf215546Sopenharmony_cilower_barycentric_at_offset(nir_builder *b, nir_instr *instr, void *data)
393bf215546Sopenharmony_ci{
394bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_intrinsic)
395bf215546Sopenharmony_ci      return false;
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci   if (intrin->intrinsic != nir_intrinsic_load_barycentric_at_offset)
400bf215546Sopenharmony_ci      return false;
401bf215546Sopenharmony_ci
402bf215546Sopenharmony_ci   b->cursor = nir_before_instr(instr);
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci   assert(intrin->src[0].ssa);
405bf215546Sopenharmony_ci   nir_ssa_def *offset =
406bf215546Sopenharmony_ci      nir_imin(b, nir_imm_int(b, 7),
407bf215546Sopenharmony_ci               nir_f2i32(b, nir_fmul(b, nir_imm_float(b, 16),
408bf215546Sopenharmony_ci                                     intrin->src[0].ssa)));
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci   nir_instr_rewrite_src(instr, &intrin->src[0], nir_src_for_ssa(offset));
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci   return true;
413bf215546Sopenharmony_ci}
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_civoid
416bf215546Sopenharmony_cibrw_nir_lower_fs_inputs(nir_shader *nir,
417bf215546Sopenharmony_ci                        const struct intel_device_info *devinfo,
418bf215546Sopenharmony_ci                        const struct brw_wm_prog_key *key)
419bf215546Sopenharmony_ci{
420bf215546Sopenharmony_ci   nir_foreach_shader_in_variable(var, nir) {
421bf215546Sopenharmony_ci      var->data.driver_location = var->data.location;
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci      /* Apply default interpolation mode.
424bf215546Sopenharmony_ci       *
425bf215546Sopenharmony_ci       * Everything defaults to smooth except for the legacy GL color
426bf215546Sopenharmony_ci       * built-in variables, which might be flat depending on API state.
427bf215546Sopenharmony_ci       */
428bf215546Sopenharmony_ci      if (var->data.interpolation == INTERP_MODE_NONE) {
429bf215546Sopenharmony_ci         const bool flat = key->flat_shade &&
430bf215546Sopenharmony_ci            (var->data.location == VARYING_SLOT_COL0 ||
431bf215546Sopenharmony_ci             var->data.location == VARYING_SLOT_COL1);
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci         var->data.interpolation = flat ? INTERP_MODE_FLAT
434bf215546Sopenharmony_ci                                        : INTERP_MODE_SMOOTH;
435bf215546Sopenharmony_ci      }
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci      /* On Ironlake and below, there is only one interpolation mode.
438bf215546Sopenharmony_ci       * Centroid interpolation doesn't mean anything on this hardware --
439bf215546Sopenharmony_ci       * there is no multisampling.
440bf215546Sopenharmony_ci       */
441bf215546Sopenharmony_ci      if (devinfo->ver < 6) {
442bf215546Sopenharmony_ci         var->data.centroid = false;
443bf215546Sopenharmony_ci         var->data.sample = false;
444bf215546Sopenharmony_ci      }
445bf215546Sopenharmony_ci   }
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci   nir_lower_io_options lower_io_options = nir_lower_io_lower_64bit_to_32;
448bf215546Sopenharmony_ci   if (key->persample_interp)
449bf215546Sopenharmony_ci      lower_io_options |= nir_lower_io_force_sample_interpolation;
450bf215546Sopenharmony_ci
451bf215546Sopenharmony_ci   nir_lower_io(nir, nir_var_shader_in, type_size_vec4, lower_io_options);
452bf215546Sopenharmony_ci   if (devinfo->ver >= 11)
453bf215546Sopenharmony_ci      nir_lower_interpolation(nir, ~0);
454bf215546Sopenharmony_ci
455bf215546Sopenharmony_ci   if (!key->multisample_fbo)
456bf215546Sopenharmony_ci      nir_lower_single_sampled(nir);
457bf215546Sopenharmony_ci
458bf215546Sopenharmony_ci   nir_shader_instructions_pass(nir, lower_barycentric_at_offset,
459bf215546Sopenharmony_ci                                nir_metadata_block_index |
460bf215546Sopenharmony_ci                                nir_metadata_dominance,
461bf215546Sopenharmony_ci                                NULL);
462bf215546Sopenharmony_ci
463bf215546Sopenharmony_ci   /* This pass needs actual constants */
464bf215546Sopenharmony_ci   nir_opt_constant_folding(nir);
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci   nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
467bf215546Sopenharmony_ci}
468bf215546Sopenharmony_ci
469bf215546Sopenharmony_civoid
470bf215546Sopenharmony_cibrw_nir_lower_vue_outputs(nir_shader *nir)
471bf215546Sopenharmony_ci{
472bf215546Sopenharmony_ci   nir_foreach_shader_out_variable(var, nir) {
473bf215546Sopenharmony_ci      var->data.driver_location = var->data.location;
474bf215546Sopenharmony_ci   }
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci   nir_lower_io(nir, nir_var_shader_out, type_size_vec4,
477bf215546Sopenharmony_ci                nir_lower_io_lower_64bit_to_32);
478bf215546Sopenharmony_ci}
479bf215546Sopenharmony_ci
480bf215546Sopenharmony_civoid
481bf215546Sopenharmony_cibrw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue_map,
482bf215546Sopenharmony_ci                          enum tess_primitive_mode tes_primitive_mode)
483bf215546Sopenharmony_ci{
484bf215546Sopenharmony_ci   nir_foreach_shader_out_variable(var, nir) {
485bf215546Sopenharmony_ci      var->data.driver_location = var->data.location;
486bf215546Sopenharmony_ci   }
487bf215546Sopenharmony_ci
488bf215546Sopenharmony_ci   nir_lower_io(nir, nir_var_shader_out, type_size_vec4,
489bf215546Sopenharmony_ci                nir_lower_io_lower_64bit_to_32);
490bf215546Sopenharmony_ci
491bf215546Sopenharmony_ci   /* This pass needs actual constants */
492bf215546Sopenharmony_ci   nir_opt_constant_folding(nir);
493bf215546Sopenharmony_ci
494bf215546Sopenharmony_ci   nir_io_add_const_offset_to_base(nir, nir_var_shader_out);
495bf215546Sopenharmony_ci
496bf215546Sopenharmony_ci   nir_foreach_function(function, nir) {
497bf215546Sopenharmony_ci      if (function->impl) {
498bf215546Sopenharmony_ci         nir_builder b;
499bf215546Sopenharmony_ci         nir_builder_init(&b, function->impl);
500bf215546Sopenharmony_ci         nir_foreach_block(block, function->impl) {
501bf215546Sopenharmony_ci            remap_patch_urb_offsets(block, &b, vue_map, tes_primitive_mode);
502bf215546Sopenharmony_ci         }
503bf215546Sopenharmony_ci      }
504bf215546Sopenharmony_ci   }
505bf215546Sopenharmony_ci}
506bf215546Sopenharmony_ci
507bf215546Sopenharmony_civoid
508bf215546Sopenharmony_cibrw_nir_lower_fs_outputs(nir_shader *nir)
509bf215546Sopenharmony_ci{
510bf215546Sopenharmony_ci   nir_foreach_shader_out_variable(var, nir) {
511bf215546Sopenharmony_ci      var->data.driver_location =
512bf215546Sopenharmony_ci         SET_FIELD(var->data.index, BRW_NIR_FRAG_OUTPUT_INDEX) |
513bf215546Sopenharmony_ci         SET_FIELD(var->data.location, BRW_NIR_FRAG_OUTPUT_LOCATION);
514bf215546Sopenharmony_ci   }
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci   nir_lower_io(nir, nir_var_shader_out, type_size_dvec4, 0);
517bf215546Sopenharmony_ci}
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci#define OPT(pass, ...) ({                                  \
520bf215546Sopenharmony_ci   bool this_progress = false;                             \
521bf215546Sopenharmony_ci   NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__);      \
522bf215546Sopenharmony_ci   if (this_progress)                                      \
523bf215546Sopenharmony_ci      progress = true;                                     \
524bf215546Sopenharmony_ci   this_progress;                                          \
525bf215546Sopenharmony_ci})
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_civoid
528bf215546Sopenharmony_cibrw_nir_optimize(nir_shader *nir, const struct brw_compiler *compiler,
529bf215546Sopenharmony_ci                 bool is_scalar, bool allow_copies)
530bf215546Sopenharmony_ci{
531bf215546Sopenharmony_ci   bool progress;
532bf215546Sopenharmony_ci   unsigned lower_flrp =
533bf215546Sopenharmony_ci      (nir->options->lower_flrp16 ? 16 : 0) |
534bf215546Sopenharmony_ci      (nir->options->lower_flrp32 ? 32 : 0) |
535bf215546Sopenharmony_ci      (nir->options->lower_flrp64 ? 64 : 0);
536bf215546Sopenharmony_ci
537bf215546Sopenharmony_ci   do {
538bf215546Sopenharmony_ci      progress = false;
539bf215546Sopenharmony_ci      OPT(nir_split_array_vars, nir_var_function_temp);
540bf215546Sopenharmony_ci      OPT(nir_shrink_vec_array_vars, nir_var_function_temp);
541bf215546Sopenharmony_ci      OPT(nir_opt_deref);
542bf215546Sopenharmony_ci      if (OPT(nir_opt_memcpy))
543bf215546Sopenharmony_ci         OPT(nir_split_var_copies);
544bf215546Sopenharmony_ci      OPT(nir_lower_vars_to_ssa);
545bf215546Sopenharmony_ci      if (allow_copies) {
546bf215546Sopenharmony_ci         /* Only run this pass in the first call to brw_nir_optimize.  Later
547bf215546Sopenharmony_ci          * calls assume that we've lowered away any copy_deref instructions
548bf215546Sopenharmony_ci          * and we don't want to introduce any more.
549bf215546Sopenharmony_ci          */
550bf215546Sopenharmony_ci         OPT(nir_opt_find_array_copies);
551bf215546Sopenharmony_ci      }
552bf215546Sopenharmony_ci      OPT(nir_opt_copy_prop_vars);
553bf215546Sopenharmony_ci      OPT(nir_opt_dead_write_vars);
554bf215546Sopenharmony_ci      OPT(nir_opt_combine_stores, nir_var_all);
555bf215546Sopenharmony_ci
556bf215546Sopenharmony_ci      OPT(nir_opt_ray_queries);
557bf215546Sopenharmony_ci
558bf215546Sopenharmony_ci      if (is_scalar) {
559bf215546Sopenharmony_ci         OPT(nir_lower_alu_to_scalar, NULL, NULL);
560bf215546Sopenharmony_ci      } else {
561bf215546Sopenharmony_ci         OPT(nir_opt_shrink_stores, true);
562bf215546Sopenharmony_ci         OPT(nir_opt_shrink_vectors);
563bf215546Sopenharmony_ci      }
564bf215546Sopenharmony_ci
565bf215546Sopenharmony_ci      OPT(nir_copy_prop);
566bf215546Sopenharmony_ci
567bf215546Sopenharmony_ci      if (is_scalar) {
568bf215546Sopenharmony_ci         OPT(nir_lower_phis_to_scalar, false);
569bf215546Sopenharmony_ci      }
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci      OPT(nir_copy_prop);
572bf215546Sopenharmony_ci      OPT(nir_opt_dce);
573bf215546Sopenharmony_ci      OPT(nir_opt_cse);
574bf215546Sopenharmony_ci      OPT(nir_opt_combine_stores, nir_var_all);
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci      /* Passing 0 to the peephole select pass causes it to convert
577bf215546Sopenharmony_ci       * if-statements that contain only move instructions in the branches
578bf215546Sopenharmony_ci       * regardless of the count.
579bf215546Sopenharmony_ci       *
580bf215546Sopenharmony_ci       * Passing 1 to the peephole select pass causes it to convert
581bf215546Sopenharmony_ci       * if-statements that contain at most a single ALU instruction (total)
582bf215546Sopenharmony_ci       * in both branches.  Before Gfx6, some math instructions were
583bf215546Sopenharmony_ci       * prohibitively expensive and the results of compare operations need an
584bf215546Sopenharmony_ci       * extra resolve step.  For these reasons, this pass is more harmful
585bf215546Sopenharmony_ci       * than good on those platforms.
586bf215546Sopenharmony_ci       *
587bf215546Sopenharmony_ci       * For indirect loads of uniforms (push constants), we assume that array
588bf215546Sopenharmony_ci       * indices will nearly always be in bounds and the cost of the load is
589bf215546Sopenharmony_ci       * low.  Therefore there shouldn't be a performance benefit to avoid it.
590bf215546Sopenharmony_ci       * However, in vec4 tessellation shaders, these loads operate by
591bf215546Sopenharmony_ci       * actually pulling from memory.
592bf215546Sopenharmony_ci       */
593bf215546Sopenharmony_ci      const bool is_vec4_tessellation = !is_scalar &&
594bf215546Sopenharmony_ci         (nir->info.stage == MESA_SHADER_TESS_CTRL ||
595bf215546Sopenharmony_ci          nir->info.stage == MESA_SHADER_TESS_EVAL);
596bf215546Sopenharmony_ci      OPT(nir_opt_peephole_select, 0, !is_vec4_tessellation, false);
597bf215546Sopenharmony_ci      OPT(nir_opt_peephole_select, 8, !is_vec4_tessellation,
598bf215546Sopenharmony_ci          compiler->devinfo->ver >= 6);
599bf215546Sopenharmony_ci
600bf215546Sopenharmony_ci      OPT(nir_opt_intrinsics);
601bf215546Sopenharmony_ci      OPT(nir_opt_idiv_const, 32);
602bf215546Sopenharmony_ci      OPT(nir_opt_algebraic);
603bf215546Sopenharmony_ci      OPT(nir_lower_constant_convert_alu_types);
604bf215546Sopenharmony_ci      OPT(nir_opt_constant_folding);
605bf215546Sopenharmony_ci
606bf215546Sopenharmony_ci      if (lower_flrp != 0) {
607bf215546Sopenharmony_ci         if (OPT(nir_lower_flrp,
608bf215546Sopenharmony_ci                 lower_flrp,
609bf215546Sopenharmony_ci                 false /* always_precise */)) {
610bf215546Sopenharmony_ci            OPT(nir_opt_constant_folding);
611bf215546Sopenharmony_ci         }
612bf215546Sopenharmony_ci
613bf215546Sopenharmony_ci         /* Nothing should rematerialize any flrps, so we only need to do this
614bf215546Sopenharmony_ci          * lowering once.
615bf215546Sopenharmony_ci          */
616bf215546Sopenharmony_ci         lower_flrp = 0;
617bf215546Sopenharmony_ci      }
618bf215546Sopenharmony_ci
619bf215546Sopenharmony_ci      OPT(nir_opt_dead_cf);
620bf215546Sopenharmony_ci      if (OPT(nir_opt_trivial_continues)) {
621bf215546Sopenharmony_ci         /* If nir_opt_trivial_continues makes progress, then we need to clean
622bf215546Sopenharmony_ci          * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
623bf215546Sopenharmony_ci          * to make progress.
624bf215546Sopenharmony_ci          */
625bf215546Sopenharmony_ci         OPT(nir_copy_prop);
626bf215546Sopenharmony_ci         OPT(nir_opt_dce);
627bf215546Sopenharmony_ci      }
628bf215546Sopenharmony_ci      OPT(nir_opt_if, nir_opt_if_optimize_phi_true_false);
629bf215546Sopenharmony_ci      OPT(nir_opt_conditional_discard);
630bf215546Sopenharmony_ci      if (nir->options->max_unroll_iterations != 0) {
631bf215546Sopenharmony_ci         OPT(nir_opt_loop_unroll);
632bf215546Sopenharmony_ci      }
633bf215546Sopenharmony_ci      OPT(nir_opt_remove_phis);
634bf215546Sopenharmony_ci      OPT(nir_opt_gcm, false);
635bf215546Sopenharmony_ci      OPT(nir_opt_undef);
636bf215546Sopenharmony_ci      OPT(nir_lower_pack);
637bf215546Sopenharmony_ci   } while (progress);
638bf215546Sopenharmony_ci
639bf215546Sopenharmony_ci   /* Workaround Gfxbench unused local sampler variable which will trigger an
640bf215546Sopenharmony_ci    * assert in the opt_large_constants pass.
641bf215546Sopenharmony_ci    */
642bf215546Sopenharmony_ci   OPT(nir_remove_dead_variables, nir_var_function_temp, NULL);
643bf215546Sopenharmony_ci}
644bf215546Sopenharmony_ci
645bf215546Sopenharmony_cistatic unsigned
646bf215546Sopenharmony_cilower_bit_size_callback(const nir_instr *instr, UNUSED void *data)
647bf215546Sopenharmony_ci{
648bf215546Sopenharmony_ci   const struct brw_compiler *compiler = (const struct brw_compiler *) data;
649bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = compiler->devinfo;
650bf215546Sopenharmony_ci
651bf215546Sopenharmony_ci   switch (instr->type) {
652bf215546Sopenharmony_ci   case nir_instr_type_alu: {
653bf215546Sopenharmony_ci      nir_alu_instr *alu = nir_instr_as_alu(instr);
654bf215546Sopenharmony_ci      switch (alu->op) {
655bf215546Sopenharmony_ci      case nir_op_bit_count:
656bf215546Sopenharmony_ci      case nir_op_ufind_msb:
657bf215546Sopenharmony_ci      case nir_op_ifind_msb:
658bf215546Sopenharmony_ci      case nir_op_find_lsb:
659bf215546Sopenharmony_ci         /* These are handled specially because the destination is always
660bf215546Sopenharmony_ci          * 32-bit and so the bit size of the instruction is given by the
661bf215546Sopenharmony_ci          * source.
662bf215546Sopenharmony_ci          */
663bf215546Sopenharmony_ci         assert(alu->src[0].src.is_ssa);
664bf215546Sopenharmony_ci         return alu->src[0].src.ssa->bit_size == 32 ? 0 : 32;
665bf215546Sopenharmony_ci      default:
666bf215546Sopenharmony_ci         break;
667bf215546Sopenharmony_ci      }
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci      assert(alu->dest.dest.is_ssa);
670bf215546Sopenharmony_ci      if (alu->dest.dest.ssa.bit_size >= 32)
671bf215546Sopenharmony_ci         return 0;
672bf215546Sopenharmony_ci
673bf215546Sopenharmony_ci      /* Note: nir_op_iabs and nir_op_ineg are not lowered here because the
674bf215546Sopenharmony_ci       * 8-bit ABS or NEG instruction should eventually get copy propagated
675bf215546Sopenharmony_ci       * into the MOV that does the type conversion.  This results in far
676bf215546Sopenharmony_ci       * fewer MOV instructions.
677bf215546Sopenharmony_ci       */
678bf215546Sopenharmony_ci      switch (alu->op) {
679bf215546Sopenharmony_ci      case nir_op_idiv:
680bf215546Sopenharmony_ci      case nir_op_imod:
681bf215546Sopenharmony_ci      case nir_op_irem:
682bf215546Sopenharmony_ci      case nir_op_udiv:
683bf215546Sopenharmony_ci      case nir_op_umod:
684bf215546Sopenharmony_ci      case nir_op_fceil:
685bf215546Sopenharmony_ci      case nir_op_ffloor:
686bf215546Sopenharmony_ci      case nir_op_ffract:
687bf215546Sopenharmony_ci      case nir_op_fround_even:
688bf215546Sopenharmony_ci      case nir_op_ftrunc:
689bf215546Sopenharmony_ci         return 32;
690bf215546Sopenharmony_ci      case nir_op_frcp:
691bf215546Sopenharmony_ci      case nir_op_frsq:
692bf215546Sopenharmony_ci      case nir_op_fsqrt:
693bf215546Sopenharmony_ci      case nir_op_fpow:
694bf215546Sopenharmony_ci      case nir_op_fexp2:
695bf215546Sopenharmony_ci      case nir_op_flog2:
696bf215546Sopenharmony_ci      case nir_op_fsin:
697bf215546Sopenharmony_ci      case nir_op_fcos:
698bf215546Sopenharmony_ci         return devinfo->ver < 9 ? 32 : 0;
699bf215546Sopenharmony_ci      case nir_op_isign:
700bf215546Sopenharmony_ci         assert(!"Should have been lowered by nir_opt_algebraic.");
701bf215546Sopenharmony_ci         return 0;
702bf215546Sopenharmony_ci      default:
703bf215546Sopenharmony_ci         if (nir_op_infos[alu->op].num_inputs >= 2 &&
704bf215546Sopenharmony_ci             alu->dest.dest.ssa.bit_size == 8)
705bf215546Sopenharmony_ci            return 16;
706bf215546Sopenharmony_ci
707bf215546Sopenharmony_ci         if (nir_alu_instr_is_comparison(alu) &&
708bf215546Sopenharmony_ci             alu->src[0].src.ssa->bit_size == 8)
709bf215546Sopenharmony_ci            return 16;
710bf215546Sopenharmony_ci
711bf215546Sopenharmony_ci         return 0;
712bf215546Sopenharmony_ci      }
713bf215546Sopenharmony_ci      break;
714bf215546Sopenharmony_ci   }
715bf215546Sopenharmony_ci
716bf215546Sopenharmony_ci   case nir_instr_type_intrinsic: {
717bf215546Sopenharmony_ci      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
718bf215546Sopenharmony_ci      switch (intrin->intrinsic) {
719bf215546Sopenharmony_ci      case nir_intrinsic_read_invocation:
720bf215546Sopenharmony_ci      case nir_intrinsic_read_first_invocation:
721bf215546Sopenharmony_ci      case nir_intrinsic_vote_feq:
722bf215546Sopenharmony_ci      case nir_intrinsic_vote_ieq:
723bf215546Sopenharmony_ci      case nir_intrinsic_shuffle:
724bf215546Sopenharmony_ci      case nir_intrinsic_shuffle_xor:
725bf215546Sopenharmony_ci      case nir_intrinsic_shuffle_up:
726bf215546Sopenharmony_ci      case nir_intrinsic_shuffle_down:
727bf215546Sopenharmony_ci      case nir_intrinsic_quad_broadcast:
728bf215546Sopenharmony_ci      case nir_intrinsic_quad_swap_horizontal:
729bf215546Sopenharmony_ci      case nir_intrinsic_quad_swap_vertical:
730bf215546Sopenharmony_ci      case nir_intrinsic_quad_swap_diagonal:
731bf215546Sopenharmony_ci         if (intrin->src[0].ssa->bit_size == 8)
732bf215546Sopenharmony_ci            return 16;
733bf215546Sopenharmony_ci         return 0;
734bf215546Sopenharmony_ci
735bf215546Sopenharmony_ci      case nir_intrinsic_reduce:
736bf215546Sopenharmony_ci      case nir_intrinsic_inclusive_scan:
737bf215546Sopenharmony_ci      case nir_intrinsic_exclusive_scan:
738bf215546Sopenharmony_ci         /* There are a couple of register region issues that make things
739bf215546Sopenharmony_ci          * complicated for 8-bit types:
740bf215546Sopenharmony_ci          *
741bf215546Sopenharmony_ci          *    1. Only raw moves are allowed to write to a packed 8-bit
742bf215546Sopenharmony_ci          *       destination.
743bf215546Sopenharmony_ci          *    2. If we use a strided destination, the efficient way to do
744bf215546Sopenharmony_ci          *       scan operations ends up using strides that are too big to
745bf215546Sopenharmony_ci          *       encode in an instruction.
746bf215546Sopenharmony_ci          *
747bf215546Sopenharmony_ci          * To get around these issues, we just do all 8-bit scan operations
748bf215546Sopenharmony_ci          * in 16 bits.  It's actually fewer instructions than what we'd have
749bf215546Sopenharmony_ci          * to do if we were trying to do it in native 8-bit types and the
750bf215546Sopenharmony_ci          * results are the same once we truncate to 8 bits at the end.
751bf215546Sopenharmony_ci          */
752bf215546Sopenharmony_ci         if (intrin->dest.ssa.bit_size == 8)
753bf215546Sopenharmony_ci            return 16;
754bf215546Sopenharmony_ci         return 0;
755bf215546Sopenharmony_ci
756bf215546Sopenharmony_ci      default:
757bf215546Sopenharmony_ci         return 0;
758bf215546Sopenharmony_ci      }
759bf215546Sopenharmony_ci      break;
760bf215546Sopenharmony_ci   }
761bf215546Sopenharmony_ci
762bf215546Sopenharmony_ci   case nir_instr_type_phi: {
763bf215546Sopenharmony_ci      nir_phi_instr *phi = nir_instr_as_phi(instr);
764bf215546Sopenharmony_ci      if (phi->dest.ssa.bit_size == 8)
765bf215546Sopenharmony_ci         return 16;
766bf215546Sopenharmony_ci      return 0;
767bf215546Sopenharmony_ci   }
768bf215546Sopenharmony_ci
769bf215546Sopenharmony_ci   default:
770bf215546Sopenharmony_ci      return 0;
771bf215546Sopenharmony_ci   }
772bf215546Sopenharmony_ci}
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_ci/* On gfx12.5+, if the offsets are not both constant and in the {-8,7} range,
775bf215546Sopenharmony_ci * we will have nir_lower_tex() lower the source offset by returning true from
776bf215546Sopenharmony_ci * this filter function.
777bf215546Sopenharmony_ci */
778bf215546Sopenharmony_cistatic bool
779bf215546Sopenharmony_cilower_xehp_tg4_offset_filter(const nir_instr *instr, UNUSED const void *data)
780bf215546Sopenharmony_ci{
781bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_tex)
782bf215546Sopenharmony_ci      return false;
783bf215546Sopenharmony_ci
784bf215546Sopenharmony_ci   nir_tex_instr *tex = nir_instr_as_tex(instr);
785bf215546Sopenharmony_ci
786bf215546Sopenharmony_ci   if (tex->op != nir_texop_tg4)
787bf215546Sopenharmony_ci      return false;
788bf215546Sopenharmony_ci
789bf215546Sopenharmony_ci   int offset_index = nir_tex_instr_src_index(tex, nir_tex_src_offset);
790bf215546Sopenharmony_ci   if (offset_index < 0)
791bf215546Sopenharmony_ci      return false;
792bf215546Sopenharmony_ci
793bf215546Sopenharmony_ci   if (!nir_src_is_const(tex->src[offset_index].src))
794bf215546Sopenharmony_ci      return true;
795bf215546Sopenharmony_ci
796bf215546Sopenharmony_ci   int64_t offset_x = nir_src_comp_as_int(tex->src[offset_index].src, 0);
797bf215546Sopenharmony_ci   int64_t offset_y = nir_src_comp_as_int(tex->src[offset_index].src, 1);
798bf215546Sopenharmony_ci
799bf215546Sopenharmony_ci   return offset_x < -8 || offset_x > 7 || offset_y < -8 || offset_y > 7;
800bf215546Sopenharmony_ci}
801bf215546Sopenharmony_ci
802bf215546Sopenharmony_ci/* Does some simple lowering and runs the standard suite of optimizations
803bf215546Sopenharmony_ci *
804bf215546Sopenharmony_ci * This is intended to be called more-or-less directly after you get the
805bf215546Sopenharmony_ci * shader out of GLSL or some other source.  While it is geared towards i965,
806bf215546Sopenharmony_ci * it is not at all generator-specific except for the is_scalar flag.  Even
807bf215546Sopenharmony_ci * there, it is safe to call with is_scalar = false for a shader that is
808bf215546Sopenharmony_ci * intended for the FS backend as long as nir_optimize is called again with
809bf215546Sopenharmony_ci * is_scalar = true to scalarize everything prior to code gen.
810bf215546Sopenharmony_ci */
811bf215546Sopenharmony_civoid
812bf215546Sopenharmony_cibrw_preprocess_nir(const struct brw_compiler *compiler, nir_shader *nir,
813bf215546Sopenharmony_ci                   const nir_shader *softfp64)
814bf215546Sopenharmony_ci{
815bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = compiler->devinfo;
816bf215546Sopenharmony_ci   UNUSED bool progress; /* Written by OPT */
817bf215546Sopenharmony_ci
818bf215546Sopenharmony_ci   const bool is_scalar = compiler->scalar_stage[nir->info.stage];
819bf215546Sopenharmony_ci
820bf215546Sopenharmony_ci   nir_validate_ssa_dominance(nir, "before brw_preprocess_nir");
821bf215546Sopenharmony_ci
822bf215546Sopenharmony_ci   if (is_scalar) {
823bf215546Sopenharmony_ci      OPT(nir_lower_alu_to_scalar, NULL, NULL);
824bf215546Sopenharmony_ci   }
825bf215546Sopenharmony_ci
826bf215546Sopenharmony_ci   if (nir->info.stage == MESA_SHADER_GEOMETRY)
827bf215546Sopenharmony_ci      OPT(nir_lower_gs_intrinsics, 0);
828bf215546Sopenharmony_ci
829bf215546Sopenharmony_ci   /* See also brw_nir_trig_workarounds.py */
830bf215546Sopenharmony_ci   if (compiler->precise_trig &&
831bf215546Sopenharmony_ci       !(devinfo->ver >= 10 || devinfo->platform == INTEL_PLATFORM_KBL))
832bf215546Sopenharmony_ci      OPT(brw_nir_apply_trig_workarounds);
833bf215546Sopenharmony_ci
834bf215546Sopenharmony_ci   if (devinfo->ver >= 12)
835bf215546Sopenharmony_ci      OPT(brw_nir_clamp_image_1d_2d_array_sizes);
836bf215546Sopenharmony_ci
837bf215546Sopenharmony_ci   const nir_lower_tex_options tex_options = {
838bf215546Sopenharmony_ci      .lower_txp = ~0,
839bf215546Sopenharmony_ci      .lower_txf_offset = true,
840bf215546Sopenharmony_ci      .lower_rect_offset = true,
841bf215546Sopenharmony_ci      .lower_txd_cube_map = true,
842bf215546Sopenharmony_ci      .lower_txd_3d = devinfo->verx10 >= 125,    /* Wa_1209978020 */
843bf215546Sopenharmony_ci      .lower_txd_array = devinfo->verx10 >= 125, /* Wa_1209978020 */
844bf215546Sopenharmony_ci      .lower_txb_shadow_clamp = true,
845bf215546Sopenharmony_ci      .lower_txd_shadow_clamp = true,
846bf215546Sopenharmony_ci      .lower_txd_offset_clamp = true,
847bf215546Sopenharmony_ci      .lower_tg4_offsets = true,
848bf215546Sopenharmony_ci      .lower_txs_lod = true, /* Wa_14012320009 */
849bf215546Sopenharmony_ci      .lower_offset_filter =
850bf215546Sopenharmony_ci         devinfo->verx10 >= 125 ? lower_xehp_tg4_offset_filter : NULL,
851bf215546Sopenharmony_ci      .lower_invalid_implicit_lod = true,
852bf215546Sopenharmony_ci   };
853bf215546Sopenharmony_ci
854bf215546Sopenharmony_ci   OPT(nir_lower_tex, &tex_options);
855bf215546Sopenharmony_ci   OPT(nir_normalize_cubemap_coords);
856bf215546Sopenharmony_ci
857bf215546Sopenharmony_ci   OPT(nir_lower_global_vars_to_local);
858bf215546Sopenharmony_ci
859bf215546Sopenharmony_ci   OPT(nir_split_var_copies);
860bf215546Sopenharmony_ci   OPT(nir_split_struct_vars, nir_var_function_temp);
861bf215546Sopenharmony_ci
862bf215546Sopenharmony_ci   brw_nir_optimize(nir, compiler, is_scalar, true);
863bf215546Sopenharmony_ci
864bf215546Sopenharmony_ci   OPT(nir_lower_doubles, softfp64, nir->options->lower_doubles_options);
865bf215546Sopenharmony_ci   OPT(nir_lower_int64);
866bf215546Sopenharmony_ci
867bf215546Sopenharmony_ci   OPT(nir_lower_bit_size, lower_bit_size_callback, (void *)compiler);
868bf215546Sopenharmony_ci
869bf215546Sopenharmony_ci   if (is_scalar) {
870bf215546Sopenharmony_ci      OPT(nir_lower_load_const_to_scalar);
871bf215546Sopenharmony_ci   }
872bf215546Sopenharmony_ci
873bf215546Sopenharmony_ci   /* Lower a bunch of stuff */
874bf215546Sopenharmony_ci   OPT(nir_lower_var_copies);
875bf215546Sopenharmony_ci
876bf215546Sopenharmony_ci   /* This needs to be run after the first optimization pass but before we
877bf215546Sopenharmony_ci    * lower indirect derefs away
878bf215546Sopenharmony_ci    */
879bf215546Sopenharmony_ci   if (compiler->supports_shader_constants) {
880bf215546Sopenharmony_ci      OPT(nir_opt_large_constants, NULL, 32);
881bf215546Sopenharmony_ci   }
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci   OPT(nir_lower_system_values);
884bf215546Sopenharmony_ci   OPT(nir_lower_compute_system_values, NULL);
885bf215546Sopenharmony_ci
886bf215546Sopenharmony_ci   const nir_lower_subgroups_options subgroups_options = {
887bf215546Sopenharmony_ci      .ballot_bit_size = 32,
888bf215546Sopenharmony_ci      .ballot_components = 1,
889bf215546Sopenharmony_ci      .lower_to_scalar = true,
890bf215546Sopenharmony_ci      .lower_vote_trivial = !is_scalar,
891bf215546Sopenharmony_ci      .lower_relative_shuffle = true,
892bf215546Sopenharmony_ci      .lower_quad_broadcast_dynamic = true,
893bf215546Sopenharmony_ci      .lower_elect = true,
894bf215546Sopenharmony_ci   };
895bf215546Sopenharmony_ci   OPT(nir_lower_subgroups, &subgroups_options);
896bf215546Sopenharmony_ci
897bf215546Sopenharmony_ci   nir_variable_mode indirect_mask =
898bf215546Sopenharmony_ci      brw_nir_no_indirect_mask(compiler, nir->info.stage);
899bf215546Sopenharmony_ci   OPT(nir_lower_indirect_derefs, indirect_mask, UINT32_MAX);
900bf215546Sopenharmony_ci
901bf215546Sopenharmony_ci   /* Even in cases where we can handle indirect temporaries via scratch, we
902bf215546Sopenharmony_ci    * it can still be expensive.  Lower indirects on small arrays to
903bf215546Sopenharmony_ci    * conditional load/stores.
904bf215546Sopenharmony_ci    *
905bf215546Sopenharmony_ci    * The threshold of 16 was chosen semi-arbitrarily.  The idea is that an
906bf215546Sopenharmony_ci    * indirect on an array of 16 elements is about 30 instructions at which
907bf215546Sopenharmony_ci    * point, you may be better off doing a send.  With a SIMD8 program, 16
908bf215546Sopenharmony_ci    * floats is 1/8 of the entire register file.  Any array larger than that
909bf215546Sopenharmony_ci    * is likely to cause pressure issues.  Also, this value is sufficiently
910bf215546Sopenharmony_ci    * high that the benchmarks known to suffer from large temporary array
911bf215546Sopenharmony_ci    * issues are helped but nothing else in shader-db is hurt except for maybe
912bf215546Sopenharmony_ci    * that one kerbal space program shader.
913bf215546Sopenharmony_ci    */
914bf215546Sopenharmony_ci   if (is_scalar && !(indirect_mask & nir_var_function_temp))
915bf215546Sopenharmony_ci      OPT(nir_lower_indirect_derefs, nir_var_function_temp, 16);
916bf215546Sopenharmony_ci
917bf215546Sopenharmony_ci   /* Lower array derefs of vectors for SSBO and UBO loads.  For both UBOs and
918bf215546Sopenharmony_ci    * SSBOs, our back-end is capable of loading an entire vec4 at a time and
919bf215546Sopenharmony_ci    * we would like to take advantage of that whenever possible regardless of
920bf215546Sopenharmony_ci    * whether or not the app gives us full loads.  This should allow the
921bf215546Sopenharmony_ci    * optimizer to combine UBO and SSBO load operations and save us some send
922bf215546Sopenharmony_ci    * messages.
923bf215546Sopenharmony_ci    */
924bf215546Sopenharmony_ci   OPT(nir_lower_array_deref_of_vec,
925bf215546Sopenharmony_ci       nir_var_mem_ubo | nir_var_mem_ssbo,
926bf215546Sopenharmony_ci       nir_lower_direct_array_deref_of_vec_load);
927bf215546Sopenharmony_ci
928bf215546Sopenharmony_ci   /* Get rid of split copies */
929bf215546Sopenharmony_ci   brw_nir_optimize(nir, compiler, is_scalar, false);
930bf215546Sopenharmony_ci}
931bf215546Sopenharmony_ci
932bf215546Sopenharmony_civoid
933bf215546Sopenharmony_cibrw_nir_link_shaders(const struct brw_compiler *compiler,
934bf215546Sopenharmony_ci                     nir_shader *producer, nir_shader *consumer)
935bf215546Sopenharmony_ci{
936bf215546Sopenharmony_ci   if (producer->info.stage == MESA_SHADER_MESH &&
937bf215546Sopenharmony_ci       consumer->info.stage == MESA_SHADER_FRAGMENT) {
938bf215546Sopenharmony_ci      /* gl_MeshPerPrimitiveNV[].gl_ViewportIndex, gl_PrimitiveID and gl_Layer
939bf215546Sopenharmony_ci       * are per primitive, but fragment shader does not have them marked as
940bf215546Sopenharmony_ci       * such. Add the annotation here.
941bf215546Sopenharmony_ci       */
942bf215546Sopenharmony_ci      nir_foreach_shader_in_variable(var, consumer) {
943bf215546Sopenharmony_ci         switch (var->data.location) {
944bf215546Sopenharmony_ci            case VARYING_SLOT_LAYER:
945bf215546Sopenharmony_ci            case VARYING_SLOT_PRIMITIVE_ID:
946bf215546Sopenharmony_ci            case VARYING_SLOT_VIEWPORT:
947bf215546Sopenharmony_ci               var->data.per_primitive = 1;
948bf215546Sopenharmony_ci               break;
949bf215546Sopenharmony_ci            default:
950bf215546Sopenharmony_ci               continue;
951bf215546Sopenharmony_ci         }
952bf215546Sopenharmony_ci      }
953bf215546Sopenharmony_ci   }
954bf215546Sopenharmony_ci
955bf215546Sopenharmony_ci   nir_lower_io_arrays_to_elements(producer, consumer);
956bf215546Sopenharmony_ci   nir_validate_shader(producer, "after nir_lower_io_arrays_to_elements");
957bf215546Sopenharmony_ci   nir_validate_shader(consumer, "after nir_lower_io_arrays_to_elements");
958bf215546Sopenharmony_ci
959bf215546Sopenharmony_ci   const bool p_is_scalar = compiler->scalar_stage[producer->info.stage];
960bf215546Sopenharmony_ci   const bool c_is_scalar = compiler->scalar_stage[consumer->info.stage];
961bf215546Sopenharmony_ci
962bf215546Sopenharmony_ci   if (p_is_scalar && c_is_scalar) {
963bf215546Sopenharmony_ci      NIR_PASS(_, producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
964bf215546Sopenharmony_ci      NIR_PASS(_, consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
965bf215546Sopenharmony_ci      brw_nir_optimize(producer, compiler, p_is_scalar, false);
966bf215546Sopenharmony_ci      brw_nir_optimize(consumer, compiler, c_is_scalar, false);
967bf215546Sopenharmony_ci   }
968bf215546Sopenharmony_ci
969bf215546Sopenharmony_ci   if (nir_link_opt_varyings(producer, consumer))
970bf215546Sopenharmony_ci      brw_nir_optimize(consumer, compiler, c_is_scalar, false);
971bf215546Sopenharmony_ci
972bf215546Sopenharmony_ci   NIR_PASS(_, producer, nir_remove_dead_variables, nir_var_shader_out, NULL);
973bf215546Sopenharmony_ci   NIR_PASS(_, consumer, nir_remove_dead_variables, nir_var_shader_in, NULL);
974bf215546Sopenharmony_ci
975bf215546Sopenharmony_ci   if (nir_remove_unused_varyings(producer, consumer)) {
976bf215546Sopenharmony_ci      if (should_print_nir(producer)) {
977bf215546Sopenharmony_ci         printf("nir_remove_unused_varyings\n");
978bf215546Sopenharmony_ci         nir_print_shader(producer, stdout);
979bf215546Sopenharmony_ci      }
980bf215546Sopenharmony_ci      if (should_print_nir(consumer)) {
981bf215546Sopenharmony_ci         printf("nir_remove_unused_varyings\n");
982bf215546Sopenharmony_ci         nir_print_shader(consumer, stdout);
983bf215546Sopenharmony_ci      }
984bf215546Sopenharmony_ci
985bf215546Sopenharmony_ci      NIR_PASS(_, producer, nir_lower_global_vars_to_local);
986bf215546Sopenharmony_ci      NIR_PASS(_, consumer, nir_lower_global_vars_to_local);
987bf215546Sopenharmony_ci
988bf215546Sopenharmony_ci      /* The backend might not be able to handle indirects on
989bf215546Sopenharmony_ci       * temporaries so we need to lower indirects on any of the
990bf215546Sopenharmony_ci       * varyings we have demoted here.
991bf215546Sopenharmony_ci       */
992bf215546Sopenharmony_ci      NIR_PASS(_, producer, nir_lower_indirect_derefs,
993bf215546Sopenharmony_ci                  brw_nir_no_indirect_mask(compiler, producer->info.stage),
994bf215546Sopenharmony_ci                  UINT32_MAX);
995bf215546Sopenharmony_ci      NIR_PASS(_, consumer, nir_lower_indirect_derefs,
996bf215546Sopenharmony_ci                  brw_nir_no_indirect_mask(compiler, consumer->info.stage),
997bf215546Sopenharmony_ci                  UINT32_MAX);
998bf215546Sopenharmony_ci
999bf215546Sopenharmony_ci      brw_nir_optimize(producer, compiler, p_is_scalar, false);
1000bf215546Sopenharmony_ci      brw_nir_optimize(consumer, compiler, c_is_scalar, false);
1001bf215546Sopenharmony_ci   }
1002bf215546Sopenharmony_ci
1003bf215546Sopenharmony_ci   NIR_PASS(_, producer, nir_lower_io_to_vector, nir_var_shader_out);
1004bf215546Sopenharmony_ci   NIR_PASS(_, producer, nir_opt_combine_stores, nir_var_shader_out);
1005bf215546Sopenharmony_ci   NIR_PASS(_, consumer, nir_lower_io_to_vector, nir_var_shader_in);
1006bf215546Sopenharmony_ci
1007bf215546Sopenharmony_ci   if (producer->info.stage != MESA_SHADER_TESS_CTRL &&
1008bf215546Sopenharmony_ci       producer->info.stage != MESA_SHADER_MESH &&
1009bf215546Sopenharmony_ci       producer->info.stage != MESA_SHADER_TASK) {
1010bf215546Sopenharmony_ci      /* Calling lower_io_to_vector creates output variable writes with
1011bf215546Sopenharmony_ci       * write-masks.  On non-TCS outputs, the back-end can't handle it and we
1012bf215546Sopenharmony_ci       * need to call nir_lower_io_to_temporaries to get rid of them.  This,
1013bf215546Sopenharmony_ci       * in turn, creates temporary variables and extra copy_deref intrinsics
1014bf215546Sopenharmony_ci       * that we need to clean up.
1015bf215546Sopenharmony_ci       *
1016bf215546Sopenharmony_ci       * Note Mesh/Task don't support I/O as temporaries (I/O is shared
1017bf215546Sopenharmony_ci       * between whole workgroup, possibly using multiple HW threads). For
1018bf215546Sopenharmony_ci       * those write-mask in output is handled by I/O lowering.
1019bf215546Sopenharmony_ci       */
1020bf215546Sopenharmony_ci      NIR_PASS_V(producer, nir_lower_io_to_temporaries,
1021bf215546Sopenharmony_ci                 nir_shader_get_entrypoint(producer), true, false);
1022bf215546Sopenharmony_ci      NIR_PASS(_, producer, nir_lower_global_vars_to_local);
1023bf215546Sopenharmony_ci      NIR_PASS(_, producer, nir_split_var_copies);
1024bf215546Sopenharmony_ci      NIR_PASS(_, producer, nir_lower_var_copies);
1025bf215546Sopenharmony_ci   }
1026bf215546Sopenharmony_ci}
1027bf215546Sopenharmony_ci
1028bf215546Sopenharmony_cistatic bool
1029bf215546Sopenharmony_cibrw_nir_should_vectorize_mem(unsigned align_mul, unsigned align_offset,
1030bf215546Sopenharmony_ci                             unsigned bit_size,
1031bf215546Sopenharmony_ci                             unsigned num_components,
1032bf215546Sopenharmony_ci                             nir_intrinsic_instr *low,
1033bf215546Sopenharmony_ci                             nir_intrinsic_instr *high,
1034bf215546Sopenharmony_ci                             void *data)
1035bf215546Sopenharmony_ci{
1036bf215546Sopenharmony_ci   /* Don't combine things to generate 64-bit loads/stores.  We have to split
1037bf215546Sopenharmony_ci    * those back into 32-bit ones anyway and UBO loads aren't split in NIR so
1038bf215546Sopenharmony_ci    * we don't want to make a mess for the back-end.
1039bf215546Sopenharmony_ci    */
1040bf215546Sopenharmony_ci   if (bit_size > 32)
1041bf215546Sopenharmony_ci      return false;
1042bf215546Sopenharmony_ci
1043bf215546Sopenharmony_ci   /* We can handle at most a vec4 right now.  Anything bigger would get
1044bf215546Sopenharmony_ci    * immediately split by brw_nir_lower_mem_access_bit_sizes anyway.
1045bf215546Sopenharmony_ci    */
1046bf215546Sopenharmony_ci   if (num_components > 4)
1047bf215546Sopenharmony_ci      return false;
1048bf215546Sopenharmony_ci
1049bf215546Sopenharmony_ci
1050bf215546Sopenharmony_ci   uint32_t align;
1051bf215546Sopenharmony_ci   if (align_offset)
1052bf215546Sopenharmony_ci      align = 1 << (ffs(align_offset) - 1);
1053bf215546Sopenharmony_ci   else
1054bf215546Sopenharmony_ci      align = align_mul;
1055bf215546Sopenharmony_ci
1056bf215546Sopenharmony_ci   if (align < bit_size / 8)
1057bf215546Sopenharmony_ci      return false;
1058bf215546Sopenharmony_ci
1059bf215546Sopenharmony_ci   return true;
1060bf215546Sopenharmony_ci}
1061bf215546Sopenharmony_ci
1062bf215546Sopenharmony_cistatic
1063bf215546Sopenharmony_cibool combine_all_barriers(nir_intrinsic_instr *a,
1064bf215546Sopenharmony_ci                          nir_intrinsic_instr *b,
1065bf215546Sopenharmony_ci                          void *data)
1066bf215546Sopenharmony_ci{
1067bf215546Sopenharmony_ci   /* Translation to backend IR will get rid of modes we don't care about, so
1068bf215546Sopenharmony_ci    * no harm in always combining them.
1069bf215546Sopenharmony_ci    *
1070bf215546Sopenharmony_ci    * TODO: While HW has only ACQUIRE|RELEASE fences, we could improve the
1071bf215546Sopenharmony_ci    * scheduling so that it can take advantage of the different semantics.
1072bf215546Sopenharmony_ci    */
1073bf215546Sopenharmony_ci   nir_intrinsic_set_memory_modes(a, nir_intrinsic_memory_modes(a) |
1074bf215546Sopenharmony_ci                                     nir_intrinsic_memory_modes(b));
1075bf215546Sopenharmony_ci   nir_intrinsic_set_memory_semantics(a, nir_intrinsic_memory_semantics(a) |
1076bf215546Sopenharmony_ci                                         nir_intrinsic_memory_semantics(b));
1077bf215546Sopenharmony_ci   nir_intrinsic_set_memory_scope(a, MAX2(nir_intrinsic_memory_scope(a),
1078bf215546Sopenharmony_ci                                          nir_intrinsic_memory_scope(b)));
1079bf215546Sopenharmony_ci   return true;
1080bf215546Sopenharmony_ci}
1081bf215546Sopenharmony_ci
1082bf215546Sopenharmony_cistatic void
1083bf215546Sopenharmony_cibrw_vectorize_lower_mem_access(nir_shader *nir,
1084bf215546Sopenharmony_ci                               const struct brw_compiler *compiler,
1085bf215546Sopenharmony_ci                               bool is_scalar,
1086bf215546Sopenharmony_ci                               bool robust_buffer_access)
1087bf215546Sopenharmony_ci{
1088bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = compiler->devinfo;
1089bf215546Sopenharmony_ci   bool progress = false;
1090bf215546Sopenharmony_ci
1091bf215546Sopenharmony_ci   if (is_scalar) {
1092bf215546Sopenharmony_ci      nir_load_store_vectorize_options options = {
1093bf215546Sopenharmony_ci         .modes = nir_var_mem_ubo | nir_var_mem_ssbo |
1094bf215546Sopenharmony_ci                  nir_var_mem_global | nir_var_mem_shared |
1095bf215546Sopenharmony_ci                  nir_var_mem_task_payload,
1096bf215546Sopenharmony_ci         .callback = brw_nir_should_vectorize_mem,
1097bf215546Sopenharmony_ci         .robust_modes = (nir_variable_mode)0,
1098bf215546Sopenharmony_ci      };
1099bf215546Sopenharmony_ci
1100bf215546Sopenharmony_ci      if (robust_buffer_access) {
1101bf215546Sopenharmony_ci         options.robust_modes = nir_var_mem_ubo | nir_var_mem_ssbo |
1102bf215546Sopenharmony_ci                                nir_var_mem_global;
1103bf215546Sopenharmony_ci      }
1104bf215546Sopenharmony_ci
1105bf215546Sopenharmony_ci      OPT(nir_opt_load_store_vectorize, &options);
1106bf215546Sopenharmony_ci   }
1107bf215546Sopenharmony_ci
1108bf215546Sopenharmony_ci   OPT(brw_nir_lower_mem_access_bit_sizes, devinfo);
1109bf215546Sopenharmony_ci
1110bf215546Sopenharmony_ci   while (progress) {
1111bf215546Sopenharmony_ci      progress = false;
1112bf215546Sopenharmony_ci
1113bf215546Sopenharmony_ci      OPT(nir_lower_pack);
1114bf215546Sopenharmony_ci      OPT(nir_copy_prop);
1115bf215546Sopenharmony_ci      OPT(nir_opt_dce);
1116bf215546Sopenharmony_ci      OPT(nir_opt_cse);
1117bf215546Sopenharmony_ci      OPT(nir_opt_algebraic);
1118bf215546Sopenharmony_ci      OPT(nir_opt_constant_folding);
1119bf215546Sopenharmony_ci   }
1120bf215546Sopenharmony_ci}
1121bf215546Sopenharmony_ci
1122bf215546Sopenharmony_cistatic bool
1123bf215546Sopenharmony_cinir_shader_has_local_variables(const nir_shader *nir)
1124bf215546Sopenharmony_ci{
1125bf215546Sopenharmony_ci   nir_foreach_function(func, nir) {
1126bf215546Sopenharmony_ci      if (func->impl && !exec_list_is_empty(&func->impl->locals))
1127bf215546Sopenharmony_ci         return true;
1128bf215546Sopenharmony_ci   }
1129bf215546Sopenharmony_ci
1130bf215546Sopenharmony_ci   return false;
1131bf215546Sopenharmony_ci}
1132bf215546Sopenharmony_ci
1133bf215546Sopenharmony_ci/* Prepare the given shader for codegen
1134bf215546Sopenharmony_ci *
1135bf215546Sopenharmony_ci * This function is intended to be called right before going into the actual
1136bf215546Sopenharmony_ci * backend and is highly backend-specific.  Also, once this function has been
1137bf215546Sopenharmony_ci * called on a shader, it will no longer be in SSA form so most optimizations
1138bf215546Sopenharmony_ci * will not work.
1139bf215546Sopenharmony_ci */
1140bf215546Sopenharmony_civoid
1141bf215546Sopenharmony_cibrw_postprocess_nir(nir_shader *nir, const struct brw_compiler *compiler,
1142bf215546Sopenharmony_ci                    bool is_scalar, bool debug_enabled,
1143bf215546Sopenharmony_ci                    bool robust_buffer_access)
1144bf215546Sopenharmony_ci{
1145bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = compiler->devinfo;
1146bf215546Sopenharmony_ci
1147bf215546Sopenharmony_ci   UNUSED bool progress; /* Written by OPT */
1148bf215546Sopenharmony_ci
1149bf215546Sopenharmony_ci   OPT(nir_lower_bit_size, lower_bit_size_callback, (void *)compiler);
1150bf215546Sopenharmony_ci
1151bf215546Sopenharmony_ci   OPT(brw_nir_lower_scoped_barriers);
1152bf215546Sopenharmony_ci   OPT(nir_opt_combine_memory_barriers, combine_all_barriers, NULL);
1153bf215546Sopenharmony_ci
1154bf215546Sopenharmony_ci   do {
1155bf215546Sopenharmony_ci      progress = false;
1156bf215546Sopenharmony_ci      OPT(nir_opt_algebraic_before_ffma);
1157bf215546Sopenharmony_ci   } while (progress);
1158bf215546Sopenharmony_ci
1159bf215546Sopenharmony_ci   if (devinfo->verx10 >= 125) {
1160bf215546Sopenharmony_ci      const nir_lower_idiv_options options = {
1161bf215546Sopenharmony_ci         .imprecise_32bit_lowering = false,
1162bf215546Sopenharmony_ci         .allow_fp16 = false
1163bf215546Sopenharmony_ci      };
1164bf215546Sopenharmony_ci      OPT(nir_lower_idiv, &options);
1165bf215546Sopenharmony_ci   }
1166bf215546Sopenharmony_ci
1167bf215546Sopenharmony_ci   if (gl_shader_stage_can_set_fragment_shading_rate(nir->info.stage))
1168bf215546Sopenharmony_ci      brw_nir_lower_shading_rate_output(nir);
1169bf215546Sopenharmony_ci
1170bf215546Sopenharmony_ci   brw_nir_optimize(nir, compiler, is_scalar, false);
1171bf215546Sopenharmony_ci
1172bf215546Sopenharmony_ci   if (is_scalar && nir_shader_has_local_variables(nir)) {
1173bf215546Sopenharmony_ci      OPT(nir_lower_vars_to_explicit_types, nir_var_function_temp,
1174bf215546Sopenharmony_ci          glsl_get_natural_size_align_bytes);
1175bf215546Sopenharmony_ci      OPT(nir_lower_explicit_io, nir_var_function_temp,
1176bf215546Sopenharmony_ci          nir_address_format_32bit_offset);
1177bf215546Sopenharmony_ci      brw_nir_optimize(nir, compiler, is_scalar, false);
1178bf215546Sopenharmony_ci   }
1179bf215546Sopenharmony_ci
1180bf215546Sopenharmony_ci   brw_vectorize_lower_mem_access(nir, compiler, is_scalar,
1181bf215546Sopenharmony_ci                                  robust_buffer_access);
1182bf215546Sopenharmony_ci
1183bf215546Sopenharmony_ci   if (OPT(nir_lower_int64))
1184bf215546Sopenharmony_ci      brw_nir_optimize(nir, compiler, is_scalar, false);
1185bf215546Sopenharmony_ci
1186bf215546Sopenharmony_ci   if (devinfo->ver >= 6) {
1187bf215546Sopenharmony_ci      /* Try and fuse multiply-adds */
1188bf215546Sopenharmony_ci      OPT(brw_nir_opt_peephole_ffma);
1189bf215546Sopenharmony_ci   }
1190bf215546Sopenharmony_ci
1191bf215546Sopenharmony_ci   if (OPT(nir_opt_comparison_pre)) {
1192bf215546Sopenharmony_ci      OPT(nir_copy_prop);
1193bf215546Sopenharmony_ci      OPT(nir_opt_dce);
1194bf215546Sopenharmony_ci      OPT(nir_opt_cse);
1195bf215546Sopenharmony_ci
1196bf215546Sopenharmony_ci      /* Do the select peepehole again.  nir_opt_comparison_pre (combined with
1197bf215546Sopenharmony_ci       * the other optimization passes) will have removed at least one
1198bf215546Sopenharmony_ci       * instruction from one of the branches of the if-statement, so now it
1199bf215546Sopenharmony_ci       * might be under the threshold of conversion to bcsel.
1200bf215546Sopenharmony_ci       *
1201bf215546Sopenharmony_ci       * See brw_nir_optimize for the explanation of is_vec4_tessellation.
1202bf215546Sopenharmony_ci       */
1203bf215546Sopenharmony_ci      const bool is_vec4_tessellation = !is_scalar &&
1204bf215546Sopenharmony_ci         (nir->info.stage == MESA_SHADER_TESS_CTRL ||
1205bf215546Sopenharmony_ci          nir->info.stage == MESA_SHADER_TESS_EVAL);
1206bf215546Sopenharmony_ci      OPT(nir_opt_peephole_select, 0, is_vec4_tessellation, false);
1207bf215546Sopenharmony_ci      OPT(nir_opt_peephole_select, 1, is_vec4_tessellation,
1208bf215546Sopenharmony_ci          compiler->devinfo->ver >= 6);
1209bf215546Sopenharmony_ci   }
1210bf215546Sopenharmony_ci
1211bf215546Sopenharmony_ci   do {
1212bf215546Sopenharmony_ci      progress = false;
1213bf215546Sopenharmony_ci      if (OPT(nir_opt_algebraic_late)) {
1214bf215546Sopenharmony_ci         /* At this late stage, anything that makes more constants will wreak
1215bf215546Sopenharmony_ci          * havok on the vec4 backend.  The handling of constants in the vec4
1216bf215546Sopenharmony_ci          * backend is not good.
1217bf215546Sopenharmony_ci          */
1218bf215546Sopenharmony_ci         if (is_scalar)
1219bf215546Sopenharmony_ci            OPT(nir_opt_constant_folding);
1220bf215546Sopenharmony_ci
1221bf215546Sopenharmony_ci         OPT(nir_copy_prop);
1222bf215546Sopenharmony_ci         OPT(nir_opt_dce);
1223bf215546Sopenharmony_ci         OPT(nir_opt_cse);
1224bf215546Sopenharmony_ci      }
1225bf215546Sopenharmony_ci   } while (progress);
1226bf215546Sopenharmony_ci
1227bf215546Sopenharmony_ci
1228bf215546Sopenharmony_ci   OPT(brw_nir_lower_conversions);
1229bf215546Sopenharmony_ci
1230bf215546Sopenharmony_ci   if (is_scalar)
1231bf215546Sopenharmony_ci      OPT(nir_lower_alu_to_scalar, NULL, NULL);
1232bf215546Sopenharmony_ci
1233bf215546Sopenharmony_ci   while (OPT(nir_opt_algebraic_distribute_src_mods)) {
1234bf215546Sopenharmony_ci      OPT(nir_copy_prop);
1235bf215546Sopenharmony_ci      OPT(nir_opt_dce);
1236bf215546Sopenharmony_ci      OPT(nir_opt_cse);
1237bf215546Sopenharmony_ci   }
1238bf215546Sopenharmony_ci
1239bf215546Sopenharmony_ci   OPT(nir_copy_prop);
1240bf215546Sopenharmony_ci   OPT(nir_opt_dce);
1241bf215546Sopenharmony_ci   OPT(nir_opt_move, nir_move_comparisons);
1242bf215546Sopenharmony_ci   OPT(nir_opt_dead_cf);
1243bf215546Sopenharmony_ci
1244bf215546Sopenharmony_ci   NIR_PASS_V(nir, nir_convert_to_lcssa, true, true);
1245bf215546Sopenharmony_ci   NIR_PASS_V(nir, nir_divergence_analysis);
1246bf215546Sopenharmony_ci
1247bf215546Sopenharmony_ci   /* TODO: Enable nir_opt_uniform_atomics on Gfx7.x too.
1248bf215546Sopenharmony_ci    * It currently fails Vulkan tests on Haswell for an unknown reason.
1249bf215546Sopenharmony_ci    *
1250bf215546Sopenharmony_ci    * TODO: Using this optimization on RT/OpenCL kernels also seems to cause
1251bf215546Sopenharmony_ci    *       issues. Until we can understand those issues, disable it.
1252bf215546Sopenharmony_ci    */
1253bf215546Sopenharmony_ci   bool opt_uniform_atomic_stage_allowed =
1254bf215546Sopenharmony_ci      devinfo->ver >= 8 &&
1255bf215546Sopenharmony_ci      nir->info.stage != MESA_SHADER_KERNEL &&
1256bf215546Sopenharmony_ci      nir->info.stage != MESA_SHADER_RAYGEN &&
1257bf215546Sopenharmony_ci      !gl_shader_stage_is_callable(nir->info.stage);
1258bf215546Sopenharmony_ci
1259bf215546Sopenharmony_ci   if (opt_uniform_atomic_stage_allowed && OPT(nir_opt_uniform_atomics)) {
1260bf215546Sopenharmony_ci      const nir_lower_subgroups_options subgroups_options = {
1261bf215546Sopenharmony_ci         .ballot_bit_size = 32,
1262bf215546Sopenharmony_ci         .ballot_components = 1,
1263bf215546Sopenharmony_ci         .lower_elect = true,
1264bf215546Sopenharmony_ci      };
1265bf215546Sopenharmony_ci      OPT(nir_lower_subgroups, &subgroups_options);
1266bf215546Sopenharmony_ci
1267bf215546Sopenharmony_ci      if (OPT(nir_lower_int64))
1268bf215546Sopenharmony_ci         brw_nir_optimize(nir, compiler, is_scalar, false);
1269bf215546Sopenharmony_ci   }
1270bf215546Sopenharmony_ci
1271bf215546Sopenharmony_ci   /* Clean up LCSSA phis */
1272bf215546Sopenharmony_ci   OPT(nir_opt_remove_phis);
1273bf215546Sopenharmony_ci
1274bf215546Sopenharmony_ci   OPT(nir_lower_bool_to_int32);
1275bf215546Sopenharmony_ci   OPT(nir_copy_prop);
1276bf215546Sopenharmony_ci   OPT(nir_opt_dce);
1277bf215546Sopenharmony_ci
1278bf215546Sopenharmony_ci   OPT(nir_lower_locals_to_regs);
1279bf215546Sopenharmony_ci
1280bf215546Sopenharmony_ci   if (unlikely(debug_enabled)) {
1281bf215546Sopenharmony_ci      /* Re-index SSA defs so we print more sensible numbers. */
1282bf215546Sopenharmony_ci      nir_foreach_function(function, nir) {
1283bf215546Sopenharmony_ci         if (function->impl)
1284bf215546Sopenharmony_ci            nir_index_ssa_defs(function->impl);
1285bf215546Sopenharmony_ci      }
1286bf215546Sopenharmony_ci
1287bf215546Sopenharmony_ci      fprintf(stderr, "NIR (SSA form) for %s shader:\n",
1288bf215546Sopenharmony_ci              _mesa_shader_stage_to_string(nir->info.stage));
1289bf215546Sopenharmony_ci      nir_print_shader(nir, stderr);
1290bf215546Sopenharmony_ci   }
1291bf215546Sopenharmony_ci
1292bf215546Sopenharmony_ci   nir_validate_ssa_dominance(nir, "before nir_convert_from_ssa");
1293bf215546Sopenharmony_ci
1294bf215546Sopenharmony_ci   OPT(nir_convert_from_ssa, true);
1295bf215546Sopenharmony_ci
1296bf215546Sopenharmony_ci   if (!is_scalar) {
1297bf215546Sopenharmony_ci      OPT(nir_move_vec_src_uses_to_dest);
1298bf215546Sopenharmony_ci      OPT(nir_lower_vec_to_movs, NULL, NULL);
1299bf215546Sopenharmony_ci   }
1300bf215546Sopenharmony_ci
1301bf215546Sopenharmony_ci   OPT(nir_opt_dce);
1302bf215546Sopenharmony_ci
1303bf215546Sopenharmony_ci   if (OPT(nir_opt_rematerialize_compares))
1304bf215546Sopenharmony_ci      OPT(nir_opt_dce);
1305bf215546Sopenharmony_ci
1306bf215546Sopenharmony_ci   /* This is the last pass we run before we start emitting stuff.  It
1307bf215546Sopenharmony_ci    * determines when we need to insert boolean resolves on Gen <= 5.  We
1308bf215546Sopenharmony_ci    * run it last because it stashes data in instr->pass_flags and we don't
1309bf215546Sopenharmony_ci    * want that to be squashed by other NIR passes.
1310bf215546Sopenharmony_ci    */
1311bf215546Sopenharmony_ci   if (devinfo->ver <= 5)
1312bf215546Sopenharmony_ci      brw_nir_analyze_boolean_resolves(nir);
1313bf215546Sopenharmony_ci
1314bf215546Sopenharmony_ci   nir_sweep(nir);
1315bf215546Sopenharmony_ci
1316bf215546Sopenharmony_ci   if (unlikely(debug_enabled)) {
1317bf215546Sopenharmony_ci      fprintf(stderr, "NIR (final form) for %s shader:\n",
1318bf215546Sopenharmony_ci              _mesa_shader_stage_to_string(nir->info.stage));
1319bf215546Sopenharmony_ci      nir_print_shader(nir, stderr);
1320bf215546Sopenharmony_ci   }
1321bf215546Sopenharmony_ci}
1322bf215546Sopenharmony_ci
1323bf215546Sopenharmony_cistatic bool
1324bf215546Sopenharmony_cibrw_nir_apply_sampler_key(nir_shader *nir,
1325bf215546Sopenharmony_ci                          const struct brw_compiler *compiler,
1326bf215546Sopenharmony_ci                          const struct brw_sampler_prog_key_data *key_tex)
1327bf215546Sopenharmony_ci{
1328bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = compiler->devinfo;
1329bf215546Sopenharmony_ci   nir_lower_tex_options tex_options = {
1330bf215546Sopenharmony_ci      .lower_txd_clamp_bindless_sampler = true,
1331bf215546Sopenharmony_ci      .lower_txd_clamp_if_sampler_index_not_lt_16 = true,
1332bf215546Sopenharmony_ci      .lower_invalid_implicit_lod = true,
1333bf215546Sopenharmony_ci   };
1334bf215546Sopenharmony_ci
1335bf215546Sopenharmony_ci   /* Iron Lake and prior require lowering of all rectangle textures */
1336bf215546Sopenharmony_ci   if (devinfo->ver < 6)
1337bf215546Sopenharmony_ci      tex_options.lower_rect = true;
1338bf215546Sopenharmony_ci
1339bf215546Sopenharmony_ci   /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
1340bf215546Sopenharmony_ci   if (devinfo->ver < 8) {
1341bf215546Sopenharmony_ci      tex_options.saturate_s = key_tex->gl_clamp_mask[0];
1342bf215546Sopenharmony_ci      tex_options.saturate_t = key_tex->gl_clamp_mask[1];
1343bf215546Sopenharmony_ci      tex_options.saturate_r = key_tex->gl_clamp_mask[2];
1344bf215546Sopenharmony_ci   }
1345bf215546Sopenharmony_ci
1346bf215546Sopenharmony_ci   /* Prior to Haswell, we have to lower gradients on shadow samplers */
1347bf215546Sopenharmony_ci   tex_options.lower_txd_shadow = devinfo->verx10 <= 70;
1348bf215546Sopenharmony_ci
1349bf215546Sopenharmony_ci   tex_options.lower_y_uv_external = key_tex->y_uv_image_mask;
1350bf215546Sopenharmony_ci   tex_options.lower_y_u_v_external = key_tex->y_u_v_image_mask;
1351bf215546Sopenharmony_ci   tex_options.lower_yx_xuxv_external = key_tex->yx_xuxv_image_mask;
1352bf215546Sopenharmony_ci   tex_options.lower_xy_uxvx_external = key_tex->xy_uxvx_image_mask;
1353bf215546Sopenharmony_ci   tex_options.lower_ayuv_external = key_tex->ayuv_image_mask;
1354bf215546Sopenharmony_ci   tex_options.lower_xyuv_external = key_tex->xyuv_image_mask;
1355bf215546Sopenharmony_ci   tex_options.bt709_external = key_tex->bt709_mask;
1356bf215546Sopenharmony_ci   tex_options.bt2020_external = key_tex->bt2020_mask;
1357bf215546Sopenharmony_ci
1358bf215546Sopenharmony_ci   /* Setup array of scaling factors for each texture. */
1359bf215546Sopenharmony_ci   memcpy(&tex_options.scale_factors, &key_tex->scale_factors,
1360bf215546Sopenharmony_ci          sizeof(tex_options.scale_factors));
1361bf215546Sopenharmony_ci
1362bf215546Sopenharmony_ci   return nir_lower_tex(nir, &tex_options);
1363bf215546Sopenharmony_ci}
1364bf215546Sopenharmony_ci
1365bf215546Sopenharmony_cistatic unsigned
1366bf215546Sopenharmony_ciget_subgroup_size(const struct shader_info *info, unsigned max_subgroup_size)
1367bf215546Sopenharmony_ci{
1368bf215546Sopenharmony_ci   switch (info->subgroup_size) {
1369bf215546Sopenharmony_ci   case SUBGROUP_SIZE_API_CONSTANT:
1370bf215546Sopenharmony_ci      /* We have to use the global constant size. */
1371bf215546Sopenharmony_ci      return BRW_SUBGROUP_SIZE;
1372bf215546Sopenharmony_ci
1373bf215546Sopenharmony_ci   case SUBGROUP_SIZE_UNIFORM:
1374bf215546Sopenharmony_ci      /* It has to be uniform across all invocations but can vary per stage
1375bf215546Sopenharmony_ci       * if we want.  This gives us a bit more freedom.
1376bf215546Sopenharmony_ci       *
1377bf215546Sopenharmony_ci       * For compute, brw_nir_apply_key is called per-dispatch-width so this
1378bf215546Sopenharmony_ci       * is the actual subgroup size and not a maximum.  However, we only
1379bf215546Sopenharmony_ci       * invoke one size of any given compute shader so it's still guaranteed
1380bf215546Sopenharmony_ci       * to be uniform across invocations.
1381bf215546Sopenharmony_ci       */
1382bf215546Sopenharmony_ci      return max_subgroup_size;
1383bf215546Sopenharmony_ci
1384bf215546Sopenharmony_ci   case SUBGROUP_SIZE_VARYING:
1385bf215546Sopenharmony_ci      /* The subgroup size is allowed to be fully varying.  For geometry
1386bf215546Sopenharmony_ci       * stages, we know it's always 8 which is max_subgroup_size so we can
1387bf215546Sopenharmony_ci       * return that.  For compute, brw_nir_apply_key is called once per
1388bf215546Sopenharmony_ci       * dispatch-width so max_subgroup_size is the real subgroup size.
1389bf215546Sopenharmony_ci       *
1390bf215546Sopenharmony_ci       * For fragment, we return 0 and let it fall through to the back-end
1391bf215546Sopenharmony_ci       * compiler.  This means we can't optimize based on subgroup size but
1392bf215546Sopenharmony_ci       * that's a risk the client took when it asked for a varying subgroup
1393bf215546Sopenharmony_ci       * size.
1394bf215546Sopenharmony_ci       */
1395bf215546Sopenharmony_ci      return info->stage == MESA_SHADER_FRAGMENT ? 0 : max_subgroup_size;
1396bf215546Sopenharmony_ci
1397bf215546Sopenharmony_ci   case SUBGROUP_SIZE_REQUIRE_8:
1398bf215546Sopenharmony_ci   case SUBGROUP_SIZE_REQUIRE_16:
1399bf215546Sopenharmony_ci   case SUBGROUP_SIZE_REQUIRE_32:
1400bf215546Sopenharmony_ci      assert(gl_shader_stage_uses_workgroup(info->stage));
1401bf215546Sopenharmony_ci      /* These enum values are expressly chosen to be equal to the subgroup
1402bf215546Sopenharmony_ci       * size that they require.
1403bf215546Sopenharmony_ci       */
1404bf215546Sopenharmony_ci      return info->subgroup_size;
1405bf215546Sopenharmony_ci
1406bf215546Sopenharmony_ci   case SUBGROUP_SIZE_FULL_SUBGROUPS:
1407bf215546Sopenharmony_ci   case SUBGROUP_SIZE_REQUIRE_64:
1408bf215546Sopenharmony_ci   case SUBGROUP_SIZE_REQUIRE_128:
1409bf215546Sopenharmony_ci      break;
1410bf215546Sopenharmony_ci   }
1411bf215546Sopenharmony_ci
1412bf215546Sopenharmony_ci   unreachable("Invalid subgroup size type");
1413bf215546Sopenharmony_ci}
1414bf215546Sopenharmony_ci
1415bf215546Sopenharmony_civoid
1416bf215546Sopenharmony_cibrw_nir_apply_key(nir_shader *nir,
1417bf215546Sopenharmony_ci                  const struct brw_compiler *compiler,
1418bf215546Sopenharmony_ci                  const struct brw_base_prog_key *key,
1419bf215546Sopenharmony_ci                  unsigned max_subgroup_size,
1420bf215546Sopenharmony_ci                  bool is_scalar)
1421bf215546Sopenharmony_ci{
1422bf215546Sopenharmony_ci   bool progress = false;
1423bf215546Sopenharmony_ci
1424bf215546Sopenharmony_ci   OPT(brw_nir_apply_sampler_key, compiler, &key->tex);
1425bf215546Sopenharmony_ci
1426bf215546Sopenharmony_ci   const nir_lower_subgroups_options subgroups_options = {
1427bf215546Sopenharmony_ci      .subgroup_size = get_subgroup_size(&nir->info, max_subgroup_size),
1428bf215546Sopenharmony_ci      .ballot_bit_size = 32,
1429bf215546Sopenharmony_ci      .ballot_components = 1,
1430bf215546Sopenharmony_ci      .lower_subgroup_masks = true,
1431bf215546Sopenharmony_ci   };
1432bf215546Sopenharmony_ci   OPT(nir_lower_subgroups, &subgroups_options);
1433bf215546Sopenharmony_ci
1434bf215546Sopenharmony_ci   if (key->limit_trig_input_range)
1435bf215546Sopenharmony_ci      OPT(brw_nir_limit_trig_input_range_workaround);
1436bf215546Sopenharmony_ci
1437bf215546Sopenharmony_ci   if (progress)
1438bf215546Sopenharmony_ci      brw_nir_optimize(nir, compiler, is_scalar, false);
1439bf215546Sopenharmony_ci}
1440bf215546Sopenharmony_ci
1441bf215546Sopenharmony_cienum brw_conditional_mod
1442bf215546Sopenharmony_cibrw_cmod_for_nir_comparison(nir_op op)
1443bf215546Sopenharmony_ci{
1444bf215546Sopenharmony_ci   switch (op) {
1445bf215546Sopenharmony_ci   case nir_op_flt:
1446bf215546Sopenharmony_ci   case nir_op_flt32:
1447bf215546Sopenharmony_ci   case nir_op_ilt:
1448bf215546Sopenharmony_ci   case nir_op_ilt32:
1449bf215546Sopenharmony_ci   case nir_op_ult:
1450bf215546Sopenharmony_ci   case nir_op_ult32:
1451bf215546Sopenharmony_ci      return BRW_CONDITIONAL_L;
1452bf215546Sopenharmony_ci
1453bf215546Sopenharmony_ci   case nir_op_fge:
1454bf215546Sopenharmony_ci   case nir_op_fge32:
1455bf215546Sopenharmony_ci   case nir_op_ige:
1456bf215546Sopenharmony_ci   case nir_op_ige32:
1457bf215546Sopenharmony_ci   case nir_op_uge:
1458bf215546Sopenharmony_ci   case nir_op_uge32:
1459bf215546Sopenharmony_ci      return BRW_CONDITIONAL_GE;
1460bf215546Sopenharmony_ci
1461bf215546Sopenharmony_ci   case nir_op_feq:
1462bf215546Sopenharmony_ci   case nir_op_feq32:
1463bf215546Sopenharmony_ci   case nir_op_ieq:
1464bf215546Sopenharmony_ci   case nir_op_ieq32:
1465bf215546Sopenharmony_ci   case nir_op_b32all_fequal2:
1466bf215546Sopenharmony_ci   case nir_op_b32all_iequal2:
1467bf215546Sopenharmony_ci   case nir_op_b32all_fequal3:
1468bf215546Sopenharmony_ci   case nir_op_b32all_iequal3:
1469bf215546Sopenharmony_ci   case nir_op_b32all_fequal4:
1470bf215546Sopenharmony_ci   case nir_op_b32all_iequal4:
1471bf215546Sopenharmony_ci      return BRW_CONDITIONAL_Z;
1472bf215546Sopenharmony_ci
1473bf215546Sopenharmony_ci   case nir_op_fneu:
1474bf215546Sopenharmony_ci   case nir_op_fneu32:
1475bf215546Sopenharmony_ci   case nir_op_ine:
1476bf215546Sopenharmony_ci   case nir_op_ine32:
1477bf215546Sopenharmony_ci   case nir_op_b32any_fnequal2:
1478bf215546Sopenharmony_ci   case nir_op_b32any_inequal2:
1479bf215546Sopenharmony_ci   case nir_op_b32any_fnequal3:
1480bf215546Sopenharmony_ci   case nir_op_b32any_inequal3:
1481bf215546Sopenharmony_ci   case nir_op_b32any_fnequal4:
1482bf215546Sopenharmony_ci   case nir_op_b32any_inequal4:
1483bf215546Sopenharmony_ci      return BRW_CONDITIONAL_NZ;
1484bf215546Sopenharmony_ci
1485bf215546Sopenharmony_ci   default:
1486bf215546Sopenharmony_ci      unreachable("Unsupported NIR comparison op");
1487bf215546Sopenharmony_ci   }
1488bf215546Sopenharmony_ci}
1489bf215546Sopenharmony_ci
1490bf215546Sopenharmony_ciuint32_t
1491bf215546Sopenharmony_cibrw_aop_for_nir_intrinsic(const nir_intrinsic_instr *atomic)
1492bf215546Sopenharmony_ci{
1493bf215546Sopenharmony_ci   switch (atomic->intrinsic) {
1494bf215546Sopenharmony_ci#define AOP_CASE(atom) \
1495bf215546Sopenharmony_ci   case nir_intrinsic_image_atomic_##atom:            \
1496bf215546Sopenharmony_ci   case nir_intrinsic_bindless_image_atomic_##atom:   \
1497bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_##atom:             \
1498bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_##atom:           \
1499bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_##atom
1500bf215546Sopenharmony_ci
1501bf215546Sopenharmony_ci   AOP_CASE(add): {
1502bf215546Sopenharmony_ci      unsigned src_idx;
1503bf215546Sopenharmony_ci      switch (atomic->intrinsic) {
1504bf215546Sopenharmony_ci      case nir_intrinsic_image_atomic_add:
1505bf215546Sopenharmony_ci      case nir_intrinsic_bindless_image_atomic_add:
1506bf215546Sopenharmony_ci         src_idx = 3;
1507bf215546Sopenharmony_ci         break;
1508bf215546Sopenharmony_ci      case nir_intrinsic_ssbo_atomic_add:
1509bf215546Sopenharmony_ci         src_idx = 2;
1510bf215546Sopenharmony_ci         break;
1511bf215546Sopenharmony_ci      case nir_intrinsic_shared_atomic_add:
1512bf215546Sopenharmony_ci      case nir_intrinsic_global_atomic_add:
1513bf215546Sopenharmony_ci         src_idx = 1;
1514bf215546Sopenharmony_ci         break;
1515bf215546Sopenharmony_ci      default:
1516bf215546Sopenharmony_ci         unreachable("Invalid add atomic opcode");
1517bf215546Sopenharmony_ci      }
1518bf215546Sopenharmony_ci
1519bf215546Sopenharmony_ci      if (nir_src_is_const(atomic->src[src_idx])) {
1520bf215546Sopenharmony_ci         int64_t add_val = nir_src_as_int(atomic->src[src_idx]);
1521bf215546Sopenharmony_ci         if (add_val == 1)
1522bf215546Sopenharmony_ci            return BRW_AOP_INC;
1523bf215546Sopenharmony_ci         else if (add_val == -1)
1524bf215546Sopenharmony_ci            return BRW_AOP_DEC;
1525bf215546Sopenharmony_ci      }
1526bf215546Sopenharmony_ci      return BRW_AOP_ADD;
1527bf215546Sopenharmony_ci   }
1528bf215546Sopenharmony_ci
1529bf215546Sopenharmony_ci   AOP_CASE(imin):         return BRW_AOP_IMIN;
1530bf215546Sopenharmony_ci   AOP_CASE(umin):         return BRW_AOP_UMIN;
1531bf215546Sopenharmony_ci   AOP_CASE(imax):         return BRW_AOP_IMAX;
1532bf215546Sopenharmony_ci   AOP_CASE(umax):         return BRW_AOP_UMAX;
1533bf215546Sopenharmony_ci   AOP_CASE(and):          return BRW_AOP_AND;
1534bf215546Sopenharmony_ci   AOP_CASE(or):           return BRW_AOP_OR;
1535bf215546Sopenharmony_ci   AOP_CASE(xor):          return BRW_AOP_XOR;
1536bf215546Sopenharmony_ci   AOP_CASE(exchange):     return BRW_AOP_MOV;
1537bf215546Sopenharmony_ci   AOP_CASE(comp_swap):    return BRW_AOP_CMPWR;
1538bf215546Sopenharmony_ci
1539bf215546Sopenharmony_ci#undef AOP_CASE
1540bf215546Sopenharmony_ci#define AOP_CASE(atom) \
1541bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_##atom:          \
1542bf215546Sopenharmony_ci   case nir_intrinsic_shared_atomic_##atom:        \
1543bf215546Sopenharmony_ci   case nir_intrinsic_global_atomic_##atom
1544bf215546Sopenharmony_ci
1545bf215546Sopenharmony_ci   AOP_CASE(fmin):         return BRW_AOP_FMIN;
1546bf215546Sopenharmony_ci   AOP_CASE(fmax):         return BRW_AOP_FMAX;
1547bf215546Sopenharmony_ci   AOP_CASE(fcomp_swap):   return BRW_AOP_FCMPWR;
1548bf215546Sopenharmony_ci   AOP_CASE(fadd):         return BRW_AOP_FADD;
1549bf215546Sopenharmony_ci
1550bf215546Sopenharmony_ci#undef AOP_CASE
1551bf215546Sopenharmony_ci
1552bf215546Sopenharmony_ci   default:
1553bf215546Sopenharmony_ci      unreachable("Unsupported NIR atomic intrinsic");
1554bf215546Sopenharmony_ci   }
1555bf215546Sopenharmony_ci}
1556bf215546Sopenharmony_ci
1557bf215546Sopenharmony_cienum brw_reg_type
1558bf215546Sopenharmony_cibrw_type_for_nir_type(const struct intel_device_info *devinfo,
1559bf215546Sopenharmony_ci                      nir_alu_type type)
1560bf215546Sopenharmony_ci{
1561bf215546Sopenharmony_ci   switch (type) {
1562bf215546Sopenharmony_ci   case nir_type_uint:
1563bf215546Sopenharmony_ci   case nir_type_uint32:
1564bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_UD;
1565bf215546Sopenharmony_ci   case nir_type_bool:
1566bf215546Sopenharmony_ci   case nir_type_int:
1567bf215546Sopenharmony_ci   case nir_type_bool32:
1568bf215546Sopenharmony_ci   case nir_type_int32:
1569bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_D;
1570bf215546Sopenharmony_ci   case nir_type_float:
1571bf215546Sopenharmony_ci   case nir_type_float32:
1572bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_F;
1573bf215546Sopenharmony_ci   case nir_type_float16:
1574bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_HF;
1575bf215546Sopenharmony_ci   case nir_type_float64:
1576bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_DF;
1577bf215546Sopenharmony_ci   case nir_type_int64:
1578bf215546Sopenharmony_ci      return devinfo->ver < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_Q;
1579bf215546Sopenharmony_ci   case nir_type_uint64:
1580bf215546Sopenharmony_ci      return devinfo->ver < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_UQ;
1581bf215546Sopenharmony_ci   case nir_type_int16:
1582bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_W;
1583bf215546Sopenharmony_ci   case nir_type_uint16:
1584bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_UW;
1585bf215546Sopenharmony_ci   case nir_type_int8:
1586bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_B;
1587bf215546Sopenharmony_ci   case nir_type_uint8:
1588bf215546Sopenharmony_ci      return BRW_REGISTER_TYPE_UB;
1589bf215546Sopenharmony_ci   default:
1590bf215546Sopenharmony_ci      unreachable("unknown type");
1591bf215546Sopenharmony_ci   }
1592bf215546Sopenharmony_ci
1593bf215546Sopenharmony_ci   return BRW_REGISTER_TYPE_F;
1594bf215546Sopenharmony_ci}
1595bf215546Sopenharmony_ci
1596bf215546Sopenharmony_cinir_shader *
1597bf215546Sopenharmony_cibrw_nir_create_passthrough_tcs(void *mem_ctx, const struct brw_compiler *compiler,
1598bf215546Sopenharmony_ci                               const nir_shader_compiler_options *options,
1599bf215546Sopenharmony_ci                               const struct brw_tcs_prog_key *key)
1600bf215546Sopenharmony_ci{
1601bf215546Sopenharmony_ci   nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_TESS_CTRL,
1602bf215546Sopenharmony_ci                                                  options, "passthrough TCS");
1603bf215546Sopenharmony_ci   ralloc_steal(mem_ctx, b.shader);
1604bf215546Sopenharmony_ci   nir_shader *nir = b.shader;
1605bf215546Sopenharmony_ci   nir_variable *var;
1606bf215546Sopenharmony_ci   nir_ssa_def *load;
1607bf215546Sopenharmony_ci   nir_ssa_def *zero = nir_imm_int(&b, 0);
1608bf215546Sopenharmony_ci   nir_ssa_def *invoc_id = nir_load_invocation_id(&b);
1609bf215546Sopenharmony_ci
1610bf215546Sopenharmony_ci   nir->info.inputs_read = key->outputs_written &
1611bf215546Sopenharmony_ci      ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
1612bf215546Sopenharmony_ci   nir->info.outputs_written = key->outputs_written;
1613bf215546Sopenharmony_ci   nir->info.tess.tcs_vertices_out = key->input_vertices;
1614bf215546Sopenharmony_ci   nir->num_uniforms = 8 * sizeof(uint32_t);
1615bf215546Sopenharmony_ci
1616bf215546Sopenharmony_ci   var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_0");
1617bf215546Sopenharmony_ci   var->data.location = 0;
1618bf215546Sopenharmony_ci   var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_1");
1619bf215546Sopenharmony_ci   var->data.location = 1;
1620bf215546Sopenharmony_ci
1621bf215546Sopenharmony_ci   /* Write the patch URB header. */
1622bf215546Sopenharmony_ci   for (int i = 0; i <= 1; i++) {
1623bf215546Sopenharmony_ci      load = nir_load_uniform(&b, 4, 32, zero, .base = i * 4 * sizeof(uint32_t));
1624bf215546Sopenharmony_ci
1625bf215546Sopenharmony_ci      nir_store_output(&b, load, zero,
1626bf215546Sopenharmony_ci                       .base = VARYING_SLOT_TESS_LEVEL_INNER - i,
1627bf215546Sopenharmony_ci                       .write_mask = WRITEMASK_XYZW);
1628bf215546Sopenharmony_ci   }
1629bf215546Sopenharmony_ci
1630bf215546Sopenharmony_ci   /* Copy inputs to outputs. */
1631bf215546Sopenharmony_ci   uint64_t varyings = nir->info.inputs_read;
1632bf215546Sopenharmony_ci
1633bf215546Sopenharmony_ci   while (varyings != 0) {
1634bf215546Sopenharmony_ci      const int varying = ffsll(varyings) - 1;
1635bf215546Sopenharmony_ci
1636bf215546Sopenharmony_ci      load = nir_load_per_vertex_input(&b, 4, 32, invoc_id, zero, .base = varying);
1637bf215546Sopenharmony_ci
1638bf215546Sopenharmony_ci      nir_store_per_vertex_output(&b, load, invoc_id, zero,
1639bf215546Sopenharmony_ci                                  .base = varying,
1640bf215546Sopenharmony_ci                                  .write_mask = WRITEMASK_XYZW);
1641bf215546Sopenharmony_ci
1642bf215546Sopenharmony_ci      varyings &= ~BITFIELD64_BIT(varying);
1643bf215546Sopenharmony_ci   }
1644bf215546Sopenharmony_ci
1645bf215546Sopenharmony_ci   nir_validate_shader(nir, "in brw_nir_create_passthrough_tcs");
1646bf215546Sopenharmony_ci
1647bf215546Sopenharmony_ci   brw_preprocess_nir(compiler, nir, NULL);
1648bf215546Sopenharmony_ci
1649bf215546Sopenharmony_ci   return nir;
1650bf215546Sopenharmony_ci}
1651bf215546Sopenharmony_ci
1652bf215546Sopenharmony_cinir_ssa_def *
1653bf215546Sopenharmony_cibrw_nir_load_global_const(nir_builder *b, nir_intrinsic_instr *load_uniform,
1654bf215546Sopenharmony_ci      nir_ssa_def *base_addr, unsigned off)
1655bf215546Sopenharmony_ci{
1656bf215546Sopenharmony_ci   assert(load_uniform->intrinsic == nir_intrinsic_load_uniform);
1657bf215546Sopenharmony_ci   assert(load_uniform->dest.is_ssa);
1658bf215546Sopenharmony_ci   assert(load_uniform->src[0].is_ssa);
1659bf215546Sopenharmony_ci
1660bf215546Sopenharmony_ci   unsigned bit_size = load_uniform->dest.ssa.bit_size;
1661bf215546Sopenharmony_ci   assert(bit_size >= 8 && bit_size % 8 == 0);
1662bf215546Sopenharmony_ci   unsigned byte_size = bit_size / 8;
1663bf215546Sopenharmony_ci   nir_ssa_def *sysval;
1664bf215546Sopenharmony_ci
1665bf215546Sopenharmony_ci   if (nir_src_is_const(load_uniform->src[0])) {
1666bf215546Sopenharmony_ci      uint64_t offset = off +
1667bf215546Sopenharmony_ci                        nir_intrinsic_base(load_uniform) +
1668bf215546Sopenharmony_ci                        nir_src_as_uint(load_uniform->src[0]);
1669bf215546Sopenharmony_ci
1670bf215546Sopenharmony_ci      /* Things should be component-aligned. */
1671bf215546Sopenharmony_ci      assert(offset % byte_size == 0);
1672bf215546Sopenharmony_ci
1673bf215546Sopenharmony_ci      unsigned suboffset = offset % 64;
1674bf215546Sopenharmony_ci      uint64_t aligned_offset = offset - suboffset;
1675bf215546Sopenharmony_ci
1676bf215546Sopenharmony_ci      /* Load two just in case we go over a 64B boundary */
1677bf215546Sopenharmony_ci      nir_ssa_def *data[2];
1678bf215546Sopenharmony_ci      for (unsigned i = 0; i < 2; i++) {
1679bf215546Sopenharmony_ci         nir_ssa_def *addr = nir_iadd_imm(b, base_addr, aligned_offset + i * 64);
1680bf215546Sopenharmony_ci         data[i] = nir_load_global_const_block_intel(b, 16, addr,
1681bf215546Sopenharmony_ci                                                     nir_imm_true(b));
1682bf215546Sopenharmony_ci      }
1683bf215546Sopenharmony_ci
1684bf215546Sopenharmony_ci      sysval = nir_extract_bits(b, data, 2, suboffset * 8,
1685bf215546Sopenharmony_ci                                load_uniform->num_components, bit_size);
1686bf215546Sopenharmony_ci   } else {
1687bf215546Sopenharmony_ci      nir_ssa_def *offset32 =
1688bf215546Sopenharmony_ci         nir_iadd_imm(b, load_uniform->src[0].ssa,
1689bf215546Sopenharmony_ci                         off + nir_intrinsic_base(load_uniform));
1690bf215546Sopenharmony_ci      nir_ssa_def *addr = nir_iadd(b, base_addr, nir_u2u64(b, offset32));
1691bf215546Sopenharmony_ci      sysval = nir_load_global_constant(b, addr, byte_size,
1692bf215546Sopenharmony_ci                                        load_uniform->num_components, bit_size);
1693bf215546Sopenharmony_ci   }
1694bf215546Sopenharmony_ci
1695bf215546Sopenharmony_ci   return sysval;
1696bf215546Sopenharmony_ci}
1697