• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "util/u_math.h"
25 #include "nir.h"
26 #include "glsl_types.h"
27 #include "nir_builder.h"
28 
29 #include "clc_nir.h"
30 #include "clc_compiler.h"
31 #include "../compiler/dxil_nir.h"
32 
33 static nir_def *
load_ubo(nir_builder * b,nir_intrinsic_instr * intr,nir_variable * var,unsigned offset)34 load_ubo(nir_builder *b, nir_intrinsic_instr *intr, nir_variable *var, unsigned offset)
35 {
36    return nir_load_ubo(b,
37                        intr->def.num_components,
38                        intr->def.bit_size,
39                        nir_imm_int(b, var->data.binding),
40                        nir_imm_int(b, offset),
41                        .align_mul = 256,
42                        .align_offset = offset,
43                        .range_base = offset,
44                        .range = intr->def.bit_size * intr->def.num_components / 8);
45 }
46 
47 static bool
lower_load_base_global_invocation_id(nir_builder * b,nir_intrinsic_instr * intr,nir_variable * var)48 lower_load_base_global_invocation_id(nir_builder *b, nir_intrinsic_instr *intr,
49                                     nir_variable *var)
50 {
51    b->cursor = nir_after_instr(&intr->instr);
52 
53    nir_def *offset = load_ubo(b, intr, var, offsetof(struct clc_work_properties_data,
54                                                          global_offset_x));
55    nir_def_rewrite_uses(&intr->def, offset);
56    nir_instr_remove(&intr->instr);
57    return true;
58 }
59 
60 static bool
lower_load_work_dim(nir_builder * b,nir_intrinsic_instr * intr,nir_variable * var)61 lower_load_work_dim(nir_builder *b, nir_intrinsic_instr *intr,
62                     nir_variable *var)
63 {
64    b->cursor = nir_after_instr(&intr->instr);
65 
66    nir_def *dim = load_ubo(b, intr, var, offsetof(struct clc_work_properties_data,
67                                                       work_dim));
68    nir_def_rewrite_uses(&intr->def, dim);
69    nir_instr_remove(&intr->instr);
70    return true;
71 }
72 
73 static bool
lower_load_num_workgroups(nir_builder * b,nir_intrinsic_instr * intr,nir_variable * var)74 lower_load_num_workgroups(nir_builder *b, nir_intrinsic_instr *intr,
75                           nir_variable *var)
76 {
77    b->cursor = nir_after_instr(&intr->instr);
78 
79    nir_def *count =
80       load_ubo(b, intr, var, offsetof(struct clc_work_properties_data,
81                                       group_count_total_x));
82    nir_def_rewrite_uses(&intr->def, count);
83    nir_instr_remove(&intr->instr);
84    return true;
85 }
86 
87 static bool
lower_load_base_workgroup_id(nir_builder * b,nir_intrinsic_instr * intr,nir_variable * var)88 lower_load_base_workgroup_id(nir_builder *b, nir_intrinsic_instr *intr,
89                              nir_variable *var)
90 {
91    b->cursor = nir_after_instr(&intr->instr);
92 
93    nir_def *offset =
94       load_ubo(b, intr, var, offsetof(struct clc_work_properties_data,
95                                       group_id_offset_x));
96    nir_def_rewrite_uses(&intr->def, offset);
97    nir_instr_remove(&intr->instr);
98    return true;
99 }
100 
101 bool
clc_nir_lower_system_values(nir_shader * nir,nir_variable * var)102 clc_nir_lower_system_values(nir_shader *nir, nir_variable *var)
103 {
104    bool progress = false;
105 
106    foreach_list_typed(nir_function, func, node, &nir->functions) {
107       if (!func->is_entrypoint)
108          continue;
109       assert(func->impl);
110 
111       nir_builder b = nir_builder_create(func->impl);
112 
113       nir_foreach_block(block, func->impl) {
114          nir_foreach_instr_safe(instr, block) {
115             if (instr->type != nir_instr_type_intrinsic)
116                continue;
117 
118             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
119 
120             switch (intr->intrinsic) {
121             case nir_intrinsic_load_base_global_invocation_id:
122                progress |= lower_load_base_global_invocation_id(&b, intr, var);
123                break;
124             case nir_intrinsic_load_work_dim:
125                progress |= lower_load_work_dim(&b, intr, var);
126                break;
127             case nir_intrinsic_load_num_workgroups:
128                progress |= lower_load_num_workgroups(&b, intr, var);
129                break;
130             case nir_intrinsic_load_base_workgroup_id:
131                progress |= lower_load_base_workgroup_id(&b, intr, var);
132                break;
133             default: break;
134             }
135          }
136       }
137    }
138 
139    return progress;
140 }
141 
142 static bool
lower_load_kernel_input(nir_builder * b,nir_intrinsic_instr * intr,nir_variable * var)143 lower_load_kernel_input(nir_builder *b, nir_intrinsic_instr *intr,
144                         nir_variable *var)
145 {
146    b->cursor = nir_before_instr(&intr->instr);
147 
148    unsigned bit_size = intr->def.bit_size;
149    enum glsl_base_type base_type;
150 
151    switch (bit_size) {
152    case 64:
153       base_type = GLSL_TYPE_UINT64;
154       break;
155    case 32:
156       base_type = GLSL_TYPE_UINT;
157       break;
158    case 16:
159       base_type = GLSL_TYPE_UINT16;
160       break;
161    case 8:
162       base_type = GLSL_TYPE_UINT8;
163       break;
164    default:
165       unreachable("invalid bit size");
166    }
167 
168    const struct glsl_type *type =
169       glsl_vector_type(base_type, intr->def.num_components);
170    nir_def *ptr = nir_vec2(b, nir_imm_int(b, var->data.binding),
171                                   nir_u2uN(b, intr->src[0].ssa, 32));
172    nir_deref_instr *deref = nir_build_deref_cast(b, ptr, nir_var_mem_ubo, type,
173                                                     bit_size / 8);
174    deref->cast.align_mul = nir_intrinsic_align_mul(intr);
175    deref->cast.align_offset = nir_intrinsic_align_offset(intr);
176 
177    nir_def *result =
178       nir_load_deref(b, deref);
179    nir_def_rewrite_uses(&intr->def, result);
180    nir_instr_remove(&intr->instr);
181    return true;
182 }
183 
184 bool
clc_nir_lower_kernel_input_loads(nir_shader * nir,nir_variable * var)185 clc_nir_lower_kernel_input_loads(nir_shader *nir, nir_variable *var)
186 {
187    bool progress = false;
188 
189    foreach_list_typed(nir_function, func, node, &nir->functions) {
190       if (!func->is_entrypoint)
191          continue;
192       assert(func->impl);
193 
194       nir_builder b = nir_builder_create(func->impl);
195 
196       nir_foreach_block(block, func->impl) {
197          nir_foreach_instr_safe(instr, block) {
198             if (instr->type != nir_instr_type_intrinsic)
199                continue;
200 
201             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
202 
203             if (intr->intrinsic == nir_intrinsic_load_kernel_input)
204                progress |= lower_load_kernel_input(&b, intr, var);
205          }
206       }
207    }
208 
209    return progress;
210 }
211 
212 
213 static nir_variable *
add_printf_var(struct nir_shader * nir,unsigned uav_id)214 add_printf_var(struct nir_shader *nir, unsigned uav_id)
215 {
216    /* This size is arbitrary. Minimum required per spec is 1MB */
217    const unsigned max_printf_size = 1 * 1024 * 1024;
218    const unsigned printf_array_size = max_printf_size / sizeof(unsigned);
219    nir_variable *var =
220       nir_variable_create(nir, nir_var_mem_ssbo,
221                           glsl_array_type(glsl_uint_type(), printf_array_size, sizeof(unsigned)),
222                           "printf");
223    var->data.binding = uav_id;
224    return var;
225 }
226 
227 bool
clc_lower_printf_base(nir_shader * nir,unsigned uav_id)228 clc_lower_printf_base(nir_shader *nir, unsigned uav_id)
229 {
230    nir_variable *printf_var = NULL;
231    nir_def *printf_deref = NULL;
232    nir_foreach_function_impl(impl, nir) {
233       nir_builder b = nir_builder_at(nir_before_impl(impl));
234       bool progress = false;
235 
236       nir_foreach_block(block, impl) {
237          nir_foreach_instr_safe(instr, block) {
238             if (instr->type != nir_instr_type_intrinsic)
239                continue;
240             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
241             if (intrin->intrinsic != nir_intrinsic_load_printf_buffer_address)
242                continue;
243 
244             if (!printf_var) {
245                printf_var = add_printf_var(nir, uav_id);
246                nir_deref_instr *deref = nir_build_deref_var(&b, printf_var);
247                printf_deref = &deref->def;
248             }
249             nir_def_rewrite_uses(&intrin->def, printf_deref);
250             progress = true;
251          }
252       }
253 
254       if (progress)
255          nir_metadata_preserve(impl, nir_metadata_loop_analysis |
256                                      nir_metadata_block_index |
257                                      nir_metadata_dominance);
258       else
259          nir_metadata_preserve(impl, nir_metadata_all);
260    }
261 
262    return printf_var != NULL;
263 }
264