• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2019 Red Hat.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  **************************************************************************/
25 
26 #ifndef LP_BLD_NIR_H
27 #define LP_BLD_NIR_H
28 
29 #include "gallivm/lp_bld.h"
30 #include "gallivm/lp_bld_limits.h"
31 #include "lp_bld_type.h"
32 
33 #include "gallivm/lp_bld_tgsi.h"
34 #include "nir.h"
35 
36 struct nir_shader;
37 
38 void lp_build_nir_soa(struct gallivm_state *gallivm,
39                       struct nir_shader *shader,
40                       const struct lp_build_tgsi_params *params,
41                       LLVMValueRef (*outputs)[4]);
42 
43 struct lp_build_nir_context
44 {
45    struct lp_build_context base;
46    struct lp_build_context uint_bld;
47    struct lp_build_context int_bld;
48    struct lp_build_context uint8_bld;
49    struct lp_build_context int8_bld;
50    struct lp_build_context uint16_bld;
51    struct lp_build_context int16_bld;
52    struct lp_build_context dbl_bld;
53    struct lp_build_context uint64_bld;
54    struct lp_build_context int64_bld;
55 
56    LLVMValueRef *ssa_defs;
57    struct hash_table *regs;
58    struct hash_table *vars;
59 
60    nir_shader *shader;
61 
62    void (*load_ubo)(struct lp_build_nir_context *bld_base,
63                     unsigned nc,
64                     unsigned bit_size,
65                     bool offset_is_uniform,
66                     LLVMValueRef index, LLVMValueRef offset, LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);
67 
68    void (*load_kernel_arg)(struct lp_build_nir_context *bld_base,
69                            unsigned nc,
70                            unsigned bit_size,
71                            unsigned offset_bit_size,
72                            bool offset_is_uniform,
73                            LLVMValueRef offset, LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);
74 
75    void (*load_global)(struct lp_build_nir_context *bld_base,
76                        unsigned nc, unsigned bit_size,
77                        unsigned offset_bit_size,
78                        LLVMValueRef offset, LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);
79 
80    void (*store_global)(struct lp_build_nir_context *bld_base,
81                         unsigned writemask,
82                         unsigned nc, unsigned bit_size,
83                         unsigned addr_bit_size,
84                         LLVMValueRef addr, LLVMValueRef dst);
85 
86    void (*atomic_global)(struct lp_build_nir_context *bld_base,
87                          nir_intrinsic_op op,
88                          unsigned addr_bit_size,
89                          LLVMValueRef addr,
90                          LLVMValueRef val, LLVMValueRef val2,
91                          LLVMValueRef *result);
92 
93    /* for SSBO and shared memory */
94    void (*load_mem)(struct lp_build_nir_context *bld_base,
95                     unsigned nc, unsigned bit_size,
96                     LLVMValueRef index, LLVMValueRef offset, LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);
97    void (*store_mem)(struct lp_build_nir_context *bld_base,
98                      unsigned writemask, unsigned nc, unsigned bit_size,
99                      LLVMValueRef index, LLVMValueRef offset, LLVMValueRef dst);
100 
101    void (*atomic_mem)(struct lp_build_nir_context *bld_base,
102                       nir_intrinsic_op op,
103                       LLVMValueRef index, LLVMValueRef offset,
104                       LLVMValueRef val, LLVMValueRef val2,
105                       LLVMValueRef *result);
106 
107    void (*barrier)(struct lp_build_nir_context *bld_base);
108 
109    void (*image_op)(struct lp_build_nir_context *bld_base,
110                     struct lp_img_params *params);
111    void (*image_size)(struct lp_build_nir_context *bld_base,
112                       struct lp_sampler_size_query_params *params);
113    LLVMValueRef (*get_ssbo_size)(struct lp_build_nir_context *bld_base,
114                                  LLVMValueRef index);
115 
116    void (*load_var)(struct lp_build_nir_context *bld_base,
117                     nir_variable_mode deref_mode,
118                     unsigned num_components,
119                     unsigned bit_size,
120                     nir_variable *var,
121                     unsigned vertex_index,
122                     LLVMValueRef indir_vertex_index,
123                     unsigned const_index,
124                     LLVMValueRef indir_index,
125                     LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);
126    void (*store_var)(struct lp_build_nir_context *bld_base,
127                      nir_variable_mode deref_mode,
128                      unsigned num_components,
129                      unsigned bit_size,
130                      nir_variable *var,
131                      unsigned writemask,
132                      LLVMValueRef indir_vertex_index,
133                      unsigned const_index,
134                      LLVMValueRef indir_index,
135                      LLVMValueRef dst);
136 
137    LLVMValueRef (*load_reg)(struct lp_build_nir_context *bld_base,
138                             struct lp_build_context *reg_bld,
139                             const nir_reg_src *reg,
140                             LLVMValueRef indir_src,
141                             LLVMValueRef reg_storage);
142    void (*store_reg)(struct lp_build_nir_context *bld_base,
143                      struct lp_build_context *reg_bld,
144                      const nir_reg_dest *reg,
145                      unsigned writemask,
146                      LLVMValueRef indir_src,
147                      LLVMValueRef reg_storage,
148                      LLVMValueRef dst[NIR_MAX_VEC_COMPONENTS]);
149 
150    void (*load_scratch)(struct lp_build_nir_context *bld_base,
151                         unsigned nc, unsigned bit_size,
152                         LLVMValueRef offset,
153                         LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);
154    void (*store_scratch)(struct lp_build_nir_context *bld_base,
155                          unsigned writemask, unsigned nc,
156                          unsigned bit_size, LLVMValueRef offset,
157                          LLVMValueRef val);
158 
159    void (*emit_var_decl)(struct lp_build_nir_context *bld_base,
160                          nir_variable *var);
161 
162    void (*tex)(struct lp_build_nir_context *bld_base,
163                struct lp_sampler_params *params);
164 
165    void (*tex_size)(struct lp_build_nir_context *bld_base,
166                     struct lp_sampler_size_query_params *params);
167 
168    void (*sysval_intrin)(struct lp_build_nir_context *bld_base,
169                          nir_intrinsic_instr *instr,
170                          LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);
171    void (*discard)(struct lp_build_nir_context *bld_base,
172                    LLVMValueRef cond);
173 
174    void (*bgnloop)(struct lp_build_nir_context *bld_base);
175    void (*endloop)(struct lp_build_nir_context *bld_base);
176    void (*if_cond)(struct lp_build_nir_context *bld_base, LLVMValueRef cond);
177    void (*else_stmt)(struct lp_build_nir_context *bld_base);
178    void (*endif_stmt)(struct lp_build_nir_context *bld_base);
179    void (*break_stmt)(struct lp_build_nir_context *bld_base);
180    void (*continue_stmt)(struct lp_build_nir_context *bld_base);
181 
182    void (*emit_vertex)(struct lp_build_nir_context *bld_base, uint32_t stream_id);
183    void (*end_primitive)(struct lp_build_nir_context *bld_base, uint32_t stream_id);
184 
185    void (*vote)(struct lp_build_nir_context *bld_base, LLVMValueRef src, nir_intrinsic_instr *instr, LLVMValueRef dst[4]);
186    void (*helper_invocation)(struct lp_build_nir_context *bld_base, LLVMValueRef *dst);
187 
188    void (*interp_at)(struct lp_build_nir_context *bld_base,
189                      unsigned num_components,
190                      nir_variable *var,
191                      bool centroid, bool sample,
192                      unsigned const_index,
193                      LLVMValueRef indir_index,
194                      LLVMValueRef offsets[2], LLVMValueRef dst[4]);
195 //   LLVMValueRef main_function
196 };
197 
198 struct lp_build_nir_soa_context
199 {
200    struct lp_build_nir_context bld_base;
201 
202    /* Builder for scalar elements of shader's data type (float) */
203    struct lp_build_context elem_bld;
204    struct lp_build_context uint_elem_bld;
205 
206    LLVMValueRef consts_ptr;
207    LLVMValueRef const_sizes_ptr;
208    LLVMValueRef consts[LP_MAX_TGSI_CONST_BUFFERS];
209    LLVMValueRef consts_sizes[LP_MAX_TGSI_CONST_BUFFERS];
210    const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS];
211    LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS];
212    LLVMValueRef context_ptr;
213    LLVMValueRef thread_data_ptr;
214 
215    LLVMValueRef ssbo_ptr;
216    LLVMValueRef ssbo_sizes_ptr;
217    LLVMValueRef ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
218    LLVMValueRef ssbo_sizes[LP_MAX_TGSI_SHADER_BUFFERS];
219 
220    LLVMValueRef shared_ptr;
221    LLVMValueRef scratch_ptr;
222    unsigned scratch_size;
223 
224    const struct lp_build_coro_suspend_info *coro;
225 
226    const struct lp_build_sampler_soa *sampler;
227    const struct lp_build_image_soa *image;
228 
229    const struct lp_build_gs_iface *gs_iface;
230    const struct lp_build_tcs_iface *tcs_iface;
231    const struct lp_build_tes_iface *tes_iface;
232    const struct lp_build_fs_iface *fs_iface;
233    LLVMValueRef emitted_prims_vec_ptr[PIPE_MAX_VERTEX_STREAMS];
234    LLVMValueRef total_emitted_vertices_vec_ptr[PIPE_MAX_VERTEX_STREAMS];
235    LLVMValueRef emitted_vertices_vec_ptr[PIPE_MAX_VERTEX_STREAMS];
236    LLVMValueRef max_output_vertices_vec;
237    struct lp_bld_tgsi_system_values system_values;
238 
239    nir_variable_mode indirects;
240    struct lp_build_mask_context *mask;
241    struct lp_exec_mask exec_mask;
242 
243    /* We allocate/use this array of inputs if (indirects & nir_var_shader_in) is
244     * set. The inputs[] array above is unused then.
245     */
246    LLVMValueRef inputs_array;
247 
248    LLVMValueRef kernel_args_ptr;
249    unsigned gs_vertex_streams;
250 };
251 
252 bool
253 lp_build_nir_llvm(struct lp_build_nir_context *bld_base,
254                   struct nir_shader *nir);
255 
256 void lp_build_opt_nir(struct nir_shader *nir);
257 
258 static inline LLVMValueRef
lp_nir_array_build_gather_values(LLVMBuilderRef builder,LLVMValueRef * values,unsigned value_count)259 lp_nir_array_build_gather_values(LLVMBuilderRef builder,
260                                  LLVMValueRef * values,
261                                  unsigned value_count)
262 {
263    LLVMTypeRef arr_type = LLVMArrayType(LLVMTypeOf(values[0]), value_count);
264    LLVMValueRef arr = LLVMGetUndef(arr_type);
265    unsigned i;
266 
267    for (i = 0; i < value_count; i++) {
268       arr = LLVMBuildInsertValue(builder, arr, values[i], i, "");
269    }
270    return arr;
271 }
272 
273 
get_int_bld(struct lp_build_nir_context * bld_base,bool is_unsigned,unsigned op_bit_size)274 static inline struct lp_build_context *get_int_bld(struct lp_build_nir_context *bld_base,
275                                                    bool is_unsigned,
276                                                    unsigned op_bit_size)
277 {
278    if (is_unsigned) {
279       switch (op_bit_size) {
280       case 64:
281          return &bld_base->uint64_bld;
282       case 32:
283       default:
284          return &bld_base->uint_bld;
285       case 16:
286          return &bld_base->uint16_bld;
287       case 8:
288          return &bld_base->uint8_bld;
289       }
290    } else {
291       switch (op_bit_size) {
292       case 64:
293          return &bld_base->int64_bld;
294       default:
295       case 32:
296          return &bld_base->int_bld;
297       case 16:
298          return &bld_base->int16_bld;
299       case 8:
300          return &bld_base->int8_bld;
301       }
302    }
303 }
304 
305 #endif
306