1 /**************************************************************************
3 * Copyright 2019 Red Hat.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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
24 **************************************************************************/
29 #include "gallivm/lp_bld.h"
30 #include "gallivm/lp_bld_limits.h"
31 #include "lp_bld_type.h"
33 #include "gallivm/lp_bld_tgsi.h"
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]);
43 struct lp_build_nir_context
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
;
56 LLVMValueRef
*ssa_defs
;
57 struct hash_table
*regs
;
58 struct hash_table
*vars
;
62 void (*load_ubo
)(struct lp_build_nir_context
*bld_base
,
65 bool offset_is_uniform
,
66 LLVMValueRef index
, LLVMValueRef offset
, LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
]);
68 void (*load_kernel_arg
)(struct lp_build_nir_context
*bld_base
,
71 unsigned offset_bit_size
,
72 bool offset_is_uniform
,
73 LLVMValueRef offset
, LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
]);
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
]);
80 void (*store_global
)(struct lp_build_nir_context
*bld_base
,
82 unsigned nc
, unsigned bit_size
,
83 unsigned addr_bit_size
,
84 LLVMValueRef addr
, LLVMValueRef dst
);
86 void (*atomic_global
)(struct lp_build_nir_context
*bld_base
,
88 unsigned addr_bit_size
,
90 LLVMValueRef val
, LLVMValueRef val2
,
91 LLVMValueRef
*result
);
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
);
101 void (*atomic_mem
)(struct lp_build_nir_context
*bld_base
,
103 LLVMValueRef index
, LLVMValueRef offset
,
104 LLVMValueRef val
, LLVMValueRef val2
,
105 LLVMValueRef
*result
);
107 void (*barrier
)(struct lp_build_nir_context
*bld_base
);
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_buffer_size
)(struct lp_build_nir_context
*bld_base
,
116 void (*load_var
)(struct lp_build_nir_context
*bld_base
,
117 nir_variable_mode deref_mode
,
118 unsigned num_components
,
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
,
132 LLVMValueRef indir_vertex_index
,
133 unsigned const_index
,
134 LLVMValueRef indir_index
,
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
,
146 LLVMValueRef indir_src
,
147 LLVMValueRef reg_storage
,
148 LLVMValueRef dst
[NIR_MAX_VEC_COMPONENTS
]);
150 void (*emit_var_decl
)(struct lp_build_nir_context
*bld_base
,
153 void (*tex
)(struct lp_build_nir_context
*bld_base
,
154 struct lp_sampler_params
*params
);
156 void (*tex_size
)(struct lp_build_nir_context
*bld_base
,
157 struct lp_sampler_size_query_params
*params
);
159 void (*sysval_intrin
)(struct lp_build_nir_context
*bld_base
,
160 nir_intrinsic_instr
*instr
,
161 LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
]);
162 void (*discard
)(struct lp_build_nir_context
*bld_base
,
165 void (*bgnloop
)(struct lp_build_nir_context
*bld_base
);
166 void (*endloop
)(struct lp_build_nir_context
*bld_base
);
167 void (*if_cond
)(struct lp_build_nir_context
*bld_base
, LLVMValueRef cond
);
168 void (*else_stmt
)(struct lp_build_nir_context
*bld_base
);
169 void (*endif_stmt
)(struct lp_build_nir_context
*bld_base
);
170 void (*break_stmt
)(struct lp_build_nir_context
*bld_base
);
171 void (*continue_stmt
)(struct lp_build_nir_context
*bld_base
);
173 void (*emit_vertex
)(struct lp_build_nir_context
*bld_base
, uint32_t stream_id
);
174 void (*end_primitive
)(struct lp_build_nir_context
*bld_base
, uint32_t stream_id
);
176 void (*vote
)(struct lp_build_nir_context
*bld_base
, LLVMValueRef src
, nir_intrinsic_instr
*instr
, LLVMValueRef dst
[4]);
177 void (*helper_invocation
)(struct lp_build_nir_context
*bld_base
, LLVMValueRef
*dst
);
179 void (*interp_at
)(struct lp_build_nir_context
*bld_base
,
180 unsigned num_components
,
182 bool centroid
, bool sample
,
183 unsigned const_index
,
184 LLVMValueRef indir_index
,
185 LLVMValueRef offsets
[2], LLVMValueRef dst
[4]);
186 // LLVMValueRef main_function
189 struct lp_build_nir_soa_context
191 struct lp_build_nir_context bld_base
;
193 /* Builder for scalar elements of shader's data type (float) */
194 struct lp_build_context elem_bld
;
195 struct lp_build_context uint_elem_bld
;
197 LLVMValueRef consts_ptr
;
198 LLVMValueRef const_sizes_ptr
;
199 LLVMValueRef consts
[LP_MAX_TGSI_CONST_BUFFERS
];
200 LLVMValueRef consts_sizes
[LP_MAX_TGSI_CONST_BUFFERS
];
201 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
202 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
203 LLVMValueRef context_ptr
;
204 LLVMValueRef thread_data_ptr
;
206 LLVMValueRef ssbo_ptr
;
207 LLVMValueRef ssbo_sizes_ptr
;
208 LLVMValueRef ssbos
[LP_MAX_TGSI_SHADER_BUFFERS
];
209 LLVMValueRef ssbo_sizes
[LP_MAX_TGSI_SHADER_BUFFERS
];
211 LLVMValueRef shared_ptr
;
213 const struct lp_build_coro_suspend_info
*coro
;
215 const struct lp_build_sampler_soa
*sampler
;
216 const struct lp_build_image_soa
*image
;
218 const struct lp_build_gs_iface
*gs_iface
;
219 const struct lp_build_tcs_iface
*tcs_iface
;
220 const struct lp_build_tes_iface
*tes_iface
;
221 const struct lp_build_fs_iface
*fs_iface
;
222 LLVMValueRef emitted_prims_vec_ptr
[PIPE_MAX_VERTEX_STREAMS
];
223 LLVMValueRef total_emitted_vertices_vec_ptr
[PIPE_MAX_VERTEX_STREAMS
];
224 LLVMValueRef emitted_vertices_vec_ptr
[PIPE_MAX_VERTEX_STREAMS
];
225 LLVMValueRef max_output_vertices_vec
;
226 struct lp_bld_tgsi_system_values system_values
;
228 nir_variable_mode indirects
;
229 struct lp_build_mask_context
*mask
;
230 struct lp_exec_mask exec_mask
;
232 /* We allocate/use this array of inputs if (indirects & nir_var_shader_in) is
233 * set. The inputs[] array above is unused then.
235 LLVMValueRef inputs_array
;
237 LLVMValueRef kernel_args_ptr
;
241 lp_build_nir_llvm(struct lp_build_nir_context
*bld_base
,
242 struct nir_shader
*nir
);
244 void lp_build_opt_nir(struct nir_shader
*nir
);
246 static inline LLVMValueRef
247 lp_nir_array_build_gather_values(LLVMBuilderRef builder
,
248 LLVMValueRef
* values
,
249 unsigned value_count
)
251 LLVMTypeRef arr_type
= LLVMArrayType(LLVMTypeOf(values
[0]), value_count
);
252 LLVMValueRef arr
= LLVMGetUndef(arr_type
);
255 for (i
= 0; i
< value_count
; i
++) {
256 arr
= LLVMBuildInsertValue(builder
, arr
, values
[i
], i
, "");
262 static inline struct lp_build_context
*get_int_bld(struct lp_build_nir_context
*bld_base
,
264 unsigned op_bit_size
)
267 switch (op_bit_size
) {
269 return &bld_base
->uint64_bld
;
272 return &bld_base
->uint_bld
;
274 return &bld_base
->uint16_bld
;
276 return &bld_base
->uint8_bld
;
279 switch (op_bit_size
) {
281 return &bld_base
->int64_bld
;
284 return &bld_base
->int_bld
;
286 return &bld_base
->int16_bld
;
288 return &bld_base
->int8_bld
;