xref: /third_party/mesa3d/src/microsoft/clc/clc_nir.c (revision bf215546)
1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © Microsoft 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 "u_math.h"
25bf215546Sopenharmony_ci#include "nir.h"
26bf215546Sopenharmony_ci#include "glsl_types.h"
27bf215546Sopenharmony_ci#include "nir_types.h"
28bf215546Sopenharmony_ci#include "nir_builder.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "clc_nir.h"
31bf215546Sopenharmony_ci#include "clc_compiler.h"
32bf215546Sopenharmony_ci#include "../compiler/dxil_nir.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_cistatic bool
35bf215546Sopenharmony_cilower_load_base_global_invocation_id(nir_builder *b, nir_intrinsic_instr *intr,
36bf215546Sopenharmony_ci                                    nir_variable *var)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   b->cursor = nir_after_instr(&intr->instr);
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci   nir_ssa_def *offset =
41bf215546Sopenharmony_ci      build_load_ubo_dxil(b, nir_imm_int(b, var->data.binding),
42bf215546Sopenharmony_ci                          nir_imm_int(b,
43bf215546Sopenharmony_ci                                      offsetof(struct clc_work_properties_data,
44bf215546Sopenharmony_ci                                               global_offset_x)),
45bf215546Sopenharmony_ci                          nir_dest_num_components(intr->dest),
46bf215546Sopenharmony_ci                          nir_dest_bit_size(intr->dest));
47bf215546Sopenharmony_ci   nir_ssa_def_rewrite_uses(&intr->dest.ssa, offset);
48bf215546Sopenharmony_ci   nir_instr_remove(&intr->instr);
49bf215546Sopenharmony_ci   return true;
50bf215546Sopenharmony_ci}
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic bool
53bf215546Sopenharmony_cilower_load_work_dim(nir_builder *b, nir_intrinsic_instr *intr,
54bf215546Sopenharmony_ci                    nir_variable *var)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   b->cursor = nir_after_instr(&intr->instr);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   nir_ssa_def *dim =
59bf215546Sopenharmony_ci      build_load_ubo_dxil(b, nir_imm_int(b, var->data.binding),
60bf215546Sopenharmony_ci                          nir_imm_int(b,
61bf215546Sopenharmony_ci                                      offsetof(struct clc_work_properties_data,
62bf215546Sopenharmony_ci                                               work_dim)),
63bf215546Sopenharmony_ci                          nir_dest_num_components(intr->dest),
64bf215546Sopenharmony_ci                          nir_dest_bit_size(intr->dest));
65bf215546Sopenharmony_ci   nir_ssa_def_rewrite_uses(&intr->dest.ssa, dim);
66bf215546Sopenharmony_ci   nir_instr_remove(&intr->instr);
67bf215546Sopenharmony_ci   return true;
68bf215546Sopenharmony_ci}
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_cistatic bool
71bf215546Sopenharmony_cilower_load_num_workgroups(nir_builder *b, nir_intrinsic_instr *intr,
72bf215546Sopenharmony_ci                          nir_variable *var)
73bf215546Sopenharmony_ci{
74bf215546Sopenharmony_ci   b->cursor = nir_after_instr(&intr->instr);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   nir_ssa_def *count =
77bf215546Sopenharmony_ci      build_load_ubo_dxil(b, nir_imm_int(b, var->data.binding),
78bf215546Sopenharmony_ci                         nir_imm_int(b,
79bf215546Sopenharmony_ci                                     offsetof(struct clc_work_properties_data,
80bf215546Sopenharmony_ci                                              group_count_total_x)),
81bf215546Sopenharmony_ci                         nir_dest_num_components(intr->dest),
82bf215546Sopenharmony_ci                         nir_dest_bit_size(intr->dest));
83bf215546Sopenharmony_ci   nir_ssa_def_rewrite_uses(&intr->dest.ssa, count);
84bf215546Sopenharmony_ci   nir_instr_remove(&intr->instr);
85bf215546Sopenharmony_ci   return true;
86bf215546Sopenharmony_ci}
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_cistatic bool
89bf215546Sopenharmony_cilower_load_base_workgroup_id(nir_builder *b, nir_intrinsic_instr *intr,
90bf215546Sopenharmony_ci                             nir_variable *var)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci   b->cursor = nir_after_instr(&intr->instr);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   nir_ssa_def *offset =
95bf215546Sopenharmony_ci      build_load_ubo_dxil(b, nir_imm_int(b, var->data.binding),
96bf215546Sopenharmony_ci                         nir_imm_int(b,
97bf215546Sopenharmony_ci                                     offsetof(struct clc_work_properties_data,
98bf215546Sopenharmony_ci                                              group_id_offset_x)),
99bf215546Sopenharmony_ci                         nir_dest_num_components(intr->dest),
100bf215546Sopenharmony_ci                         nir_dest_bit_size(intr->dest));
101bf215546Sopenharmony_ci   nir_ssa_def_rewrite_uses(&intr->dest.ssa, offset);
102bf215546Sopenharmony_ci   nir_instr_remove(&intr->instr);
103bf215546Sopenharmony_ci   return true;
104bf215546Sopenharmony_ci}
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_cibool
107bf215546Sopenharmony_ciclc_nir_lower_system_values(nir_shader *nir, nir_variable *var)
108bf215546Sopenharmony_ci{
109bf215546Sopenharmony_ci   bool progress = false;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   foreach_list_typed(nir_function, func, node, &nir->functions) {
112bf215546Sopenharmony_ci      if (!func->is_entrypoint)
113bf215546Sopenharmony_ci         continue;
114bf215546Sopenharmony_ci      assert(func->impl);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci      nir_builder b;
117bf215546Sopenharmony_ci      nir_builder_init(&b, func->impl);
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci      nir_foreach_block(block, func->impl) {
120bf215546Sopenharmony_ci         nir_foreach_instr_safe(instr, block) {
121bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_intrinsic)
122bf215546Sopenharmony_ci               continue;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci            nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci            switch (intr->intrinsic) {
127bf215546Sopenharmony_ci            case nir_intrinsic_load_base_global_invocation_id:
128bf215546Sopenharmony_ci               progress |= lower_load_base_global_invocation_id(&b, intr, var);
129bf215546Sopenharmony_ci               break;
130bf215546Sopenharmony_ci            case nir_intrinsic_load_work_dim:
131bf215546Sopenharmony_ci               progress |= lower_load_work_dim(&b, intr, var);
132bf215546Sopenharmony_ci               break;
133bf215546Sopenharmony_ci            case nir_intrinsic_load_num_workgroups:
134bf215546Sopenharmony_ci               lower_load_num_workgroups(&b, intr, var);
135bf215546Sopenharmony_ci               break;
136bf215546Sopenharmony_ci            case nir_intrinsic_load_base_workgroup_id:
137bf215546Sopenharmony_ci               lower_load_base_workgroup_id(&b, intr, var);
138bf215546Sopenharmony_ci               break;
139bf215546Sopenharmony_ci            default: break;
140bf215546Sopenharmony_ci            }
141bf215546Sopenharmony_ci         }
142bf215546Sopenharmony_ci      }
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   return progress;
146bf215546Sopenharmony_ci}
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_cistatic bool
149bf215546Sopenharmony_cilower_load_kernel_input(nir_builder *b, nir_intrinsic_instr *intr,
150bf215546Sopenharmony_ci                        nir_variable *var)
151bf215546Sopenharmony_ci{
152bf215546Sopenharmony_ci   b->cursor = nir_before_instr(&intr->instr);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   unsigned bit_size = nir_dest_bit_size(intr->dest);
155bf215546Sopenharmony_ci   enum glsl_base_type base_type;
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci   switch (bit_size) {
158bf215546Sopenharmony_ci   case 64:
159bf215546Sopenharmony_ci      base_type = GLSL_TYPE_UINT64;
160bf215546Sopenharmony_ci      break;
161bf215546Sopenharmony_ci   case 32:
162bf215546Sopenharmony_ci      base_type = GLSL_TYPE_UINT;
163bf215546Sopenharmony_ci      break;
164bf215546Sopenharmony_ci    case 16:
165bf215546Sopenharmony_ci      base_type = GLSL_TYPE_UINT16;
166bf215546Sopenharmony_ci      break;
167bf215546Sopenharmony_ci    case 8:
168bf215546Sopenharmony_ci      base_type = GLSL_TYPE_UINT8;
169bf215546Sopenharmony_ci      break;
170bf215546Sopenharmony_ci   }
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   const struct glsl_type *type =
173bf215546Sopenharmony_ci      glsl_vector_type(base_type, nir_dest_num_components(intr->dest));
174bf215546Sopenharmony_ci   nir_ssa_def *ptr = nir_vec2(b, nir_imm_int(b, var->data.binding),
175bf215546Sopenharmony_ci                                  nir_u2u(b, intr->src[0].ssa, 32));
176bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_build_deref_cast(b, ptr, nir_var_mem_ubo, type,
177bf215546Sopenharmony_ci                                                    bit_size / 8);
178bf215546Sopenharmony_ci   deref->cast.align_mul = nir_intrinsic_align_mul(intr);
179bf215546Sopenharmony_ci   deref->cast.align_offset = nir_intrinsic_align_offset(intr);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   nir_ssa_def *result =
182bf215546Sopenharmony_ci      nir_load_deref(b, deref);
183bf215546Sopenharmony_ci   nir_ssa_def_rewrite_uses(&intr->dest.ssa, result);
184bf215546Sopenharmony_ci   nir_instr_remove(&intr->instr);
185bf215546Sopenharmony_ci   return true;
186bf215546Sopenharmony_ci}
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_cibool
189bf215546Sopenharmony_ciclc_nir_lower_kernel_input_loads(nir_shader *nir, nir_variable *var)
190bf215546Sopenharmony_ci{
191bf215546Sopenharmony_ci   bool progress = false;
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   foreach_list_typed(nir_function, func, node, &nir->functions) {
194bf215546Sopenharmony_ci      if (!func->is_entrypoint)
195bf215546Sopenharmony_ci         continue;
196bf215546Sopenharmony_ci      assert(func->impl);
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci      nir_builder b;
199bf215546Sopenharmony_ci      nir_builder_init(&b, func->impl);
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci      nir_foreach_block(block, func->impl) {
202bf215546Sopenharmony_ci         nir_foreach_instr_safe(instr, block) {
203bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_intrinsic)
204bf215546Sopenharmony_ci               continue;
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci            nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci            if (intr->intrinsic == nir_intrinsic_load_kernel_input)
209bf215546Sopenharmony_ci               progress |= lower_load_kernel_input(&b, intr, var);
210bf215546Sopenharmony_ci         }
211bf215546Sopenharmony_ci      }
212bf215546Sopenharmony_ci   }
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   return progress;
215bf215546Sopenharmony_ci}
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_cistatic nir_variable *
219bf215546Sopenharmony_ciadd_printf_var(struct nir_shader *nir, unsigned uav_id)
220bf215546Sopenharmony_ci{
221bf215546Sopenharmony_ci   /* This size is arbitrary. Minimum required per spec is 1MB */
222bf215546Sopenharmony_ci   const unsigned max_printf_size = 1 * 1024 * 1024;
223bf215546Sopenharmony_ci   const unsigned printf_array_size = max_printf_size / sizeof(unsigned);
224bf215546Sopenharmony_ci   nir_variable *var =
225bf215546Sopenharmony_ci      nir_variable_create(nir, nir_var_mem_ssbo,
226bf215546Sopenharmony_ci                          glsl_array_type(glsl_uint_type(), printf_array_size, sizeof(unsigned)),
227bf215546Sopenharmony_ci                          "printf");
228bf215546Sopenharmony_ci   var->data.binding = uav_id;
229bf215546Sopenharmony_ci   return var;
230bf215546Sopenharmony_ci}
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_cibool
233bf215546Sopenharmony_ciclc_lower_printf_base(nir_shader *nir, unsigned uav_id)
234bf215546Sopenharmony_ci{
235bf215546Sopenharmony_ci   nir_variable *printf_var = NULL;
236bf215546Sopenharmony_ci   nir_ssa_def *printf_deref = NULL;
237bf215546Sopenharmony_ci   nir_foreach_function(func, nir) {
238bf215546Sopenharmony_ci      nir_builder b;
239bf215546Sopenharmony_ci      nir_builder_init(&b, func->impl);
240bf215546Sopenharmony_ci      b.cursor = nir_before_instr(nir_block_first_instr(nir_start_block(func->impl)));
241bf215546Sopenharmony_ci      bool progress = false;
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci      nir_foreach_block(block, func->impl) {
244bf215546Sopenharmony_ci         nir_foreach_instr_safe(instr, block) {
245bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_intrinsic)
246bf215546Sopenharmony_ci               continue;
247bf215546Sopenharmony_ci            nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
248bf215546Sopenharmony_ci            if (intrin->intrinsic != nir_intrinsic_load_printf_buffer_address)
249bf215546Sopenharmony_ci               continue;
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_ci            if (!printf_var) {
252bf215546Sopenharmony_ci               printf_var = add_printf_var(nir, uav_id);
253bf215546Sopenharmony_ci               nir_deref_instr *deref = nir_build_deref_var(&b, printf_var);
254bf215546Sopenharmony_ci               printf_deref = &deref->dest.ssa;
255bf215546Sopenharmony_ci            }
256bf215546Sopenharmony_ci            nir_ssa_def_rewrite_uses(&intrin->dest.ssa, printf_deref);
257bf215546Sopenharmony_ci            progress = true;
258bf215546Sopenharmony_ci         }
259bf215546Sopenharmony_ci      }
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_ci      if (progress)
262bf215546Sopenharmony_ci         nir_metadata_preserve(func->impl, nir_metadata_loop_analysis |
263bf215546Sopenharmony_ci                                           nir_metadata_block_index |
264bf215546Sopenharmony_ci                                           nir_metadata_dominance);
265bf215546Sopenharmony_ci      else
266bf215546Sopenharmony_ci         nir_metadata_preserve(func->impl, nir_metadata_all);
267bf215546Sopenharmony_ci   }
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   return printf_var != NULL;
270bf215546Sopenharmony_ci}
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_cistatic nir_variable *
273bf215546Sopenharmony_cifind_identical_const_sampler(nir_shader *nir, nir_variable *sampler)
274bf215546Sopenharmony_ci{
275bf215546Sopenharmony_ci   nir_foreach_variable_with_modes(uniform, nir, nir_var_uniform) {
276bf215546Sopenharmony_ci      if (!glsl_type_is_sampler(uniform->type) || !uniform->data.sampler.is_inline_sampler)
277bf215546Sopenharmony_ci         continue;
278bf215546Sopenharmony_ci      if (uniform->data.sampler.addressing_mode == sampler->data.sampler.addressing_mode &&
279bf215546Sopenharmony_ci          uniform->data.sampler.normalized_coordinates == sampler->data.sampler.normalized_coordinates &&
280bf215546Sopenharmony_ci          uniform->data.sampler.filter_mode == sampler->data.sampler.filter_mode)
281bf215546Sopenharmony_ci         return uniform;
282bf215546Sopenharmony_ci   }
283bf215546Sopenharmony_ci   unreachable("Should have at least found the input sampler");
284bf215546Sopenharmony_ci}
285bf215546Sopenharmony_ci
286bf215546Sopenharmony_cistatic bool
287bf215546Sopenharmony_ciclc_nir_dedupe_const_samplers_instr(nir_builder *b,
288bf215546Sopenharmony_ci                                    nir_instr *instr,
289bf215546Sopenharmony_ci                                    void *cb_data)
290bf215546Sopenharmony_ci{
291bf215546Sopenharmony_ci   nir_shader *nir = cb_data;
292bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_tex)
293bf215546Sopenharmony_ci      return false;
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci   nir_tex_instr *tex = nir_instr_as_tex(instr);
296bf215546Sopenharmony_ci   int sampler_idx = nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref);
297bf215546Sopenharmony_ci   if (sampler_idx == -1)
298bf215546Sopenharmony_ci      return false;
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_src_as_deref(tex->src[sampler_idx].src);
301bf215546Sopenharmony_ci   nir_variable *sampler = nir_deref_instr_get_variable(deref);
302bf215546Sopenharmony_ci   if (!sampler)
303bf215546Sopenharmony_ci      return false;
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci   assert(sampler->data.mode == nir_var_uniform);
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci   if (!sampler->data.sampler.is_inline_sampler)
308bf215546Sopenharmony_ci      return false;
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci   nir_variable *replacement = find_identical_const_sampler(nir, sampler);
311bf215546Sopenharmony_ci   if (replacement == sampler)
312bf215546Sopenharmony_ci      return false;
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci   b->cursor = nir_before_instr(&tex->instr);
315bf215546Sopenharmony_ci   nir_deref_instr *replacement_deref = nir_build_deref_var(b, replacement);
316bf215546Sopenharmony_ci   nir_instr_rewrite_src(&tex->instr, &tex->src[sampler_idx].src,
317bf215546Sopenharmony_ci                         nir_src_for_ssa(&replacement_deref->dest.ssa));
318bf215546Sopenharmony_ci   nir_deref_instr_remove_if_unused(deref);
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci   return true;
321bf215546Sopenharmony_ci}
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_cibool
324bf215546Sopenharmony_ciclc_nir_dedupe_const_samplers(nir_shader *nir)
325bf215546Sopenharmony_ci{
326bf215546Sopenharmony_ci   return nir_shader_instructions_pass(nir,
327bf215546Sopenharmony_ci                                       clc_nir_dedupe_const_samplers_instr,
328bf215546Sopenharmony_ci                                       nir_metadata_block_index |
329bf215546Sopenharmony_ci                                       nir_metadata_dominance,
330bf215546Sopenharmony_ci                                       nir);
331bf215546Sopenharmony_ci}
332