gallivm/nir: support passing image index into image code.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_nir_soa.c
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 #include "lp_bld_nir.h"
27 #include "lp_bld_init.h"
28 #include "lp_bld_flow.h"
29 #include "lp_bld_logic.h"
30 #include "lp_bld_gather.h"
31 #include "lp_bld_const.h"
32 #include "lp_bld_struct.h"
33 #include "lp_bld_arit.h"
34 #include "lp_bld_bitarit.h"
35 #include "lp_bld_coro.h"
36 #include "lp_bld_printf.h"
37 #include "util/u_math.h"
38 /*
39 * combine the execution mask if there is one with the current mask.
40 */
41 static LLVMValueRef
42 mask_vec(struct lp_build_nir_context *bld_base)
43 {
44 struct lp_build_nir_soa_context * bld = (struct lp_build_nir_soa_context *)bld_base;
45 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
46 struct lp_exec_mask *exec_mask = &bld->exec_mask;
47 LLVMValueRef bld_mask = bld->mask ? lp_build_mask_value(bld->mask) : NULL;
48 if (!exec_mask->has_mask) {
49 return bld_mask;
50 }
51 if (!bld_mask)
52 return exec_mask->exec_mask;
53 return LLVMBuildAnd(builder, lp_build_mask_value(bld->mask),
54 exec_mask->exec_mask, "");
55 }
56
57 static LLVMValueRef
58 emit_fetch_64bit(
59 struct lp_build_nir_context * bld_base,
60 LLVMValueRef input,
61 LLVMValueRef input2)
62 {
63 struct gallivm_state *gallivm = bld_base->base.gallivm;
64 LLVMBuilderRef builder = gallivm->builder;
65 LLVMValueRef res;
66 int i;
67 LLVMValueRef shuffles[2 * (LP_MAX_VECTOR_WIDTH/32)];
68 int len = bld_base->base.type.length * 2;
69 assert(len <= (2 * (LP_MAX_VECTOR_WIDTH/32)));
70
71 for (i = 0; i < bld_base->base.type.length * 2; i+=2) {
72 #if UTIL_ARCH_LITTLE_ENDIAN
73 shuffles[i] = lp_build_const_int32(gallivm, i / 2);
74 shuffles[i + 1] = lp_build_const_int32(gallivm, i / 2 + bld_base->base.type.length);
75 #else
76 shuffles[i] = lp_build_const_int32(gallivm, i / 2 + bld_base->base.type.length);
77 shuffles[i + 1] = lp_build_const_int32(gallivm, i / 2);
78 #endif
79 }
80 res = LLVMBuildShuffleVector(builder, input, input2, LLVMConstVector(shuffles, len), "");
81
82 return LLVMBuildBitCast(builder, res, bld_base->dbl_bld.vec_type, "");
83 }
84
85 static void
86 emit_store_64bit_split(struct lp_build_nir_context *bld_base,
87 LLVMValueRef value,
88 LLVMValueRef split_values[2])
89 {
90 struct gallivm_state *gallivm = bld_base->base.gallivm;
91 LLVMBuilderRef builder = gallivm->builder;
92 unsigned i;
93 LLVMValueRef shuffles[LP_MAX_VECTOR_WIDTH/32];
94 LLVMValueRef shuffles2[LP_MAX_VECTOR_WIDTH/32];
95 int len = bld_base->base.type.length * 2;
96
97 value = LLVMBuildBitCast(gallivm->builder, value, LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), len), "");
98 for (i = 0; i < bld_base->base.type.length; i++) {
99 #if UTIL_ARCH_LITTLE_ENDIAN
100 shuffles[i] = lp_build_const_int32(gallivm, i * 2);
101 shuffles2[i] = lp_build_const_int32(gallivm, (i * 2) + 1);
102 #else
103 shuffles[i] = lp_build_const_int32(gallivm, i * 2 + 1);
104 shuffles2[i] = lp_build_const_int32(gallivm, i * 2);
105 #endif
106 }
107
108 split_values[0] = LLVMBuildShuffleVector(builder, value,
109 LLVMGetUndef(LLVMTypeOf(value)),
110 LLVMConstVector(shuffles,
111 bld_base->base.type.length),
112 "");
113 split_values[1] = LLVMBuildShuffleVector(builder, value,
114 LLVMGetUndef(LLVMTypeOf(value)),
115 LLVMConstVector(shuffles2,
116 bld_base->base.type.length),
117 "");
118 }
119
120 static void
121 emit_store_64bit_chan(struct lp_build_nir_context *bld_base,
122 LLVMValueRef chan_ptr,
123 LLVMValueRef chan_ptr2,
124 LLVMValueRef value)
125 {
126 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
127 struct lp_build_context *float_bld = &bld_base->base;
128 LLVMValueRef split_vals[2];
129
130 emit_store_64bit_split(bld_base, value, split_vals);
131
132 lp_exec_mask_store(&bld->exec_mask, float_bld, split_vals[0], chan_ptr);
133 lp_exec_mask_store(&bld->exec_mask, float_bld, split_vals[1], chan_ptr2);
134 }
135
136 static LLVMValueRef
137 get_soa_array_offsets(struct lp_build_context *uint_bld,
138 LLVMValueRef indirect_index,
139 int num_components,
140 unsigned chan_index,
141 bool need_perelement_offset)
142 {
143 struct gallivm_state *gallivm = uint_bld->gallivm;
144 LLVMValueRef chan_vec =
145 lp_build_const_int_vec(uint_bld->gallivm, uint_bld->type, chan_index);
146 LLVMValueRef length_vec =
147 lp_build_const_int_vec(gallivm, uint_bld->type, uint_bld->type.length);
148 LLVMValueRef index_vec;
149
150 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
151 index_vec = lp_build_mul(uint_bld, indirect_index, lp_build_const_int_vec(uint_bld->gallivm, uint_bld->type, num_components));
152 index_vec = lp_build_add(uint_bld, index_vec, chan_vec);
153 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
154
155 if (need_perelement_offset) {
156 LLVMValueRef pixel_offsets;
157 unsigned i;
158 /* build pixel offset vector: {0, 1, 2, 3, ...} */
159 pixel_offsets = uint_bld->undef;
160 for (i = 0; i < uint_bld->type.length; i++) {
161 LLVMValueRef ii = lp_build_const_int32(gallivm, i);
162 pixel_offsets = LLVMBuildInsertElement(gallivm->builder, pixel_offsets,
163 ii, ii, "");
164 }
165 index_vec = lp_build_add(uint_bld, index_vec, pixel_offsets);
166 }
167 return index_vec;
168 }
169
170 static LLVMValueRef
171 build_gather(struct lp_build_nir_context *bld_base,
172 struct lp_build_context *bld,
173 LLVMValueRef base_ptr,
174 LLVMValueRef indexes,
175 LLVMValueRef overflow_mask,
176 LLVMValueRef indexes2)
177 {
178 struct gallivm_state *gallivm = bld_base->base.gallivm;
179 LLVMBuilderRef builder = gallivm->builder;
180 struct lp_build_context *uint_bld = &bld_base->uint_bld;
181 LLVMValueRef res;
182 unsigned i;
183
184 if (indexes2)
185 res = LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), bld_base->base.type.length * 2));
186 else
187 res = bld->undef;
188 /*
189 * overflow_mask is a vector telling us which channels
190 * in the vector overflowed. We use the overflow behavior for
191 * constant buffers which is defined as:
192 * Out of bounds access to constant buffer returns 0 in all
193 * components. Out of bounds behavior is always with respect
194 * to the size of the buffer bound at that slot.
195 */
196
197 if (overflow_mask) {
198 /*
199 * We avoid per-element control flow here (also due to llvm going crazy,
200 * though I suspect it's better anyway since overflow is likely rare).
201 * Note that since we still fetch from buffers even if num_elements was
202 * zero (in this case we'll fetch from index zero) the jit func callers
203 * MUST provide valid fake constant buffers of size 4x32 (the values do
204 * not matter), otherwise we'd still need (not per element though)
205 * control flow.
206 */
207 indexes = lp_build_select(uint_bld, overflow_mask, uint_bld->zero, indexes);
208 if (indexes2)
209 indexes2 = lp_build_select(uint_bld, overflow_mask, uint_bld->zero, indexes2);
210 }
211
212 /*
213 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
214 */
215 for (i = 0; i < bld->type.length * (indexes2 ? 2 : 1); i++) {
216 LLVMValueRef si, di;
217 LLVMValueRef index;
218 LLVMValueRef scalar_ptr, scalar;
219
220 di = lp_build_const_int32(gallivm, i);
221 if (indexes2)
222 si = lp_build_const_int32(gallivm, i >> 1);
223 else
224 si = di;
225
226 if (indexes2 && (i & 1)) {
227 index = LLVMBuildExtractElement(builder,
228 indexes2, si, "");
229 } else {
230 index = LLVMBuildExtractElement(builder,
231 indexes, si, "");
232 }
233 scalar_ptr = LLVMBuildGEP(builder, base_ptr,
234 &index, 1, "gather_ptr");
235 scalar = LLVMBuildLoad(builder, scalar_ptr, "");
236
237 res = LLVMBuildInsertElement(builder, res, scalar, di, "");
238 }
239
240 if (overflow_mask) {
241 if (indexes2) {
242 res = LLVMBuildBitCast(builder, res, bld_base->dbl_bld.vec_type, "");
243 overflow_mask = LLVMBuildSExt(builder, overflow_mask,
244 bld_base->dbl_bld.int_vec_type, "");
245 res = lp_build_select(&bld_base->dbl_bld, overflow_mask,
246 bld_base->dbl_bld.zero, res);
247 } else
248 res = lp_build_select(bld, overflow_mask, bld->zero, res);
249 }
250
251 return res;
252 }
253
254 /**
255 * Scatter/store vector.
256 */
257 static void
258 emit_mask_scatter(struct lp_build_nir_soa_context *bld,
259 LLVMValueRef base_ptr,
260 LLVMValueRef indexes,
261 LLVMValueRef values,
262 struct lp_exec_mask *mask)
263 {
264 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
265 LLVMBuilderRef builder = gallivm->builder;
266 unsigned i;
267 LLVMValueRef pred = mask->has_mask ? mask->exec_mask : NULL;
268
269 /*
270 * Loop over elements of index_vec, store scalar value.
271 */
272 for (i = 0; i < bld->bld_base.base.type.length; i++) {
273 LLVMValueRef ii = lp_build_const_int32(gallivm, i);
274 LLVMValueRef index = LLVMBuildExtractElement(builder, indexes, ii, "");
275 LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, base_ptr, &index, 1, "scatter_ptr");
276 LLVMValueRef val = LLVMBuildExtractElement(builder, values, ii, "scatter_val");
277 LLVMValueRef scalar_pred = pred ?
278 LLVMBuildExtractElement(builder, pred, ii, "scatter_pred") : NULL;
279
280 if (0)
281 lp_build_printf(gallivm, "scatter %d: val %f at %d %p\n",
282 ii, val, index, scalar_ptr);
283
284 if (scalar_pred) {
285 LLVMValueRef real_val, dst_val;
286 dst_val = LLVMBuildLoad(builder, scalar_ptr, "");
287 real_val = lp_build_select(&bld->uint_elem_bld, scalar_pred, val, dst_val);
288 LLVMBuildStore(builder, real_val, scalar_ptr);
289 }
290 else {
291 LLVMBuildStore(builder, val, scalar_ptr);
292 }
293 }
294 }
295
296 static void emit_load_var(struct lp_build_nir_context *bld_base,
297 nir_variable_mode deref_mode,
298 unsigned num_components,
299 unsigned bit_size,
300 nir_variable *var,
301 unsigned vertex_index,
302 LLVMValueRef indir_vertex_index,
303 unsigned const_index,
304 LLVMValueRef indir_index,
305 LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
306 {
307 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
308 struct gallivm_state *gallivm = bld_base->base.gallivm;
309 int dmul = bit_size == 64 ? 2 : 1;
310 switch (deref_mode) {
311 case nir_var_shader_in:
312 for (unsigned i = 0; i < num_components; i++) {
313 int idx = (i * dmul) + var->data.location_frac;
314 if (bld->gs_iface) {
315 LLVMValueRef vertex_index_val = lp_build_const_int32(gallivm, vertex_index);
316 LLVMValueRef attrib_index_val = lp_build_const_int32(gallivm, const_index + var->data.driver_location);
317 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx);
318 LLVMValueRef result2;
319 result[i] = bld->gs_iface->fetch_input(bld->gs_iface, &bld_base->base,
320 false, vertex_index_val, 0, attrib_index_val, swizzle_index_val);
321 if (bit_size == 64) {
322 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx + 1);
323 result2 = bld->gs_iface->fetch_input(bld->gs_iface, &bld_base->base,
324 false, vertex_index_val, 0, attrib_index_val, swizzle_index_val);
325 result[i] = emit_fetch_64bit(bld_base, result[i], result2);
326 }
327 } else if (bld->tes_iface) {
328 LLVMValueRef vertex_index_val = lp_build_const_int32(gallivm, vertex_index);
329 LLVMValueRef attrib_index_val;
330 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx);
331 LLVMValueRef result2;
332
333 if (indir_index)
334 attrib_index_val = lp_build_add(&bld_base->uint_bld, indir_index, lp_build_const_int_vec(gallivm, bld_base->uint_bld.type, var->data.driver_location));
335 else
336 attrib_index_val = lp_build_const_int32(gallivm, const_index + var->data.driver_location);
337 if (var->data.patch) {
338 result[i] = bld->tes_iface->fetch_patch_input(bld->tes_iface, &bld_base->base,
339 indir_index ? true : false, attrib_index_val, swizzle_index_val);
340 if (bit_size == 64) {
341 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx + 1);
342 result2 = bld->tes_iface->fetch_patch_input(bld->tes_iface, &bld_base->base,
343 indir_index ? true : false, attrib_index_val, swizzle_index_val);
344 result[i] = emit_fetch_64bit(bld_base, result[i], result2);
345 }
346 }
347 else {
348 result[i] = bld->tes_iface->fetch_vertex_input(bld->tes_iface, &bld_base->base,
349 indir_vertex_index ? true : false,
350 indir_vertex_index ? indir_vertex_index : vertex_index_val,
351 indir_index ? true : false, attrib_index_val, swizzle_index_val);
352 if (bit_size == 64) {
353 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx + 1);
354 result2 = bld->tes_iface->fetch_vertex_input(bld->tes_iface, &bld_base->base,
355 indir_vertex_index ? true : false,
356 indir_vertex_index ? indir_vertex_index : vertex_index_val,
357 indir_index ? true : false, attrib_index_val, swizzle_index_val);
358 result[i] = emit_fetch_64bit(bld_base, result[i], result2);
359 }
360 }
361 } else if (bld->tcs_iface) {
362 LLVMValueRef vertex_index_val = lp_build_const_int32(gallivm, vertex_index);
363 LLVMValueRef attrib_index_val;
364 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx);
365
366 if (indir_index)
367 attrib_index_val = lp_build_add(&bld_base->uint_bld, indir_index, lp_build_const_int_vec(gallivm, bld_base->uint_bld.type, var->data.driver_location));
368 else
369 attrib_index_val = lp_build_const_int32(gallivm, const_index + var->data.driver_location);
370 result[i] = bld->tcs_iface->emit_fetch_input(bld->tcs_iface, &bld_base->base,
371 indir_vertex_index ? true : false, indir_vertex_index ? indir_vertex_index : vertex_index_val,
372 indir_index ? true : false, attrib_index_val, swizzle_index_val);
373 if (bit_size == 64) {
374 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx + 1);
375 LLVMValueRef result2 = bld->tcs_iface->emit_fetch_input(bld->tcs_iface, &bld_base->base,
376 indir_vertex_index ? true : false, indir_vertex_index ? indir_vertex_index : vertex_index_val,
377 indir_index ? true : false, attrib_index_val, swizzle_index_val);
378 result[i] = emit_fetch_64bit(bld_base, result[i], result2);
379 }
380 } else {
381 if (indir_index) {
382 LLVMValueRef attrib_index_val = lp_build_add(&bld_base->uint_bld, indir_index, lp_build_const_int_vec(gallivm, bld_base->uint_bld.type, var->data.driver_location));
383 LLVMValueRef index_vec = get_soa_array_offsets(&bld_base->uint_bld,
384 attrib_index_val, 4, idx,
385 TRUE);
386 LLVMValueRef index_vec2 = NULL;
387 LLVMTypeRef fptr_type;
388 LLVMValueRef inputs_array;
389 fptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
390 inputs_array = LLVMBuildBitCast(gallivm->builder, bld->inputs_array, fptr_type, "");
391
392 if (bit_size == 64)
393 index_vec2 = get_soa_array_offsets(&bld_base->uint_bld,
394 indir_index, 4, idx + 1, TRUE);
395
396 /* Gather values from the input register array */
397 result[i] = build_gather(bld_base, &bld_base->base, inputs_array, index_vec, NULL, index_vec2);
398 } else {
399 if (bld->indirects & nir_var_shader_in) {
400 LLVMValueRef lindex = lp_build_const_int32(gallivm,
401 var->data.driver_location * 4 + idx);
402 LLVMValueRef input_ptr = lp_build_pointer_get(gallivm->builder,
403 bld->inputs_array, lindex);
404 if (bit_size == 64) {
405 LLVMValueRef lindex2 = lp_build_const_int32(gallivm,
406 var->data.driver_location * 4 + (idx + 1));
407 LLVMValueRef input_ptr2 = lp_build_pointer_get(gallivm->builder,
408 bld->inputs_array, lindex2);
409 result[i] = emit_fetch_64bit(bld_base, input_ptr, input_ptr2);
410 } else {
411 result[i] = input_ptr;
412 }
413 } else {
414 if (bit_size == 64) {
415 LLVMValueRef tmp[2];
416 tmp[0] = bld->inputs[var->data.driver_location + const_index][idx];
417 tmp[1] = bld->inputs[var->data.driver_location + const_index][idx + 1];
418 result[i] = emit_fetch_64bit(bld_base, tmp[0], tmp[1]);
419 } else {
420 result[i] = bld->inputs[var->data.driver_location + const_index][idx];
421 }
422 }
423 }
424 }
425 }
426 break;
427 case nir_var_shader_out:
428 for (unsigned i = 0; i < num_components; i++) {
429 int idx = (i * dmul) + var->data.location_frac;
430 if (bld->tcs_iface) {
431 LLVMValueRef vertex_index_val = lp_build_const_int32(gallivm, vertex_index);
432 LLVMValueRef attrib_index_val;
433 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx);
434
435 if (indir_index)
436 attrib_index_val = lp_build_add(&bld_base->uint_bld, indir_index, lp_build_const_int_vec(gallivm, bld_base->uint_bld.type, var->data.driver_location));
437 else
438 attrib_index_val = lp_build_const_int32(gallivm, const_index + var->data.driver_location);
439
440 result[i] = bld->tcs_iface->emit_fetch_output(bld->tcs_iface, &bld_base->base,
441 indir_vertex_index ? true : false, indir_vertex_index ? indir_vertex_index : vertex_index_val,
442 indir_index ? true : false, attrib_index_val, swizzle_index_val, 0);
443 if (bit_size == 64) {
444 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, idx + 1);
445 LLVMValueRef result2 = bld->tcs_iface->emit_fetch_output(bld->tcs_iface, &bld_base->base,
446 indir_vertex_index ? true : false, indir_vertex_index ? indir_vertex_index : vertex_index_val,
447 indir_index ? true : false, attrib_index_val, swizzle_index_val, 0);
448 result[i] = emit_fetch_64bit(bld_base, result[i], result2);
449 }
450 }
451 }
452 break;
453 default:
454 break;
455 }
456 }
457
458 static void emit_store_chan(struct lp_build_nir_context *bld_base,
459 nir_variable_mode deref_mode,
460 unsigned bit_size,
461 unsigned location, unsigned comp,
462 unsigned chan,
463 LLVMValueRef dst)
464 {
465 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
466 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
467 struct lp_build_context *float_bld = &bld_base->base;
468
469 if (bit_size == 64) {
470 chan *= 2;
471 chan += comp;
472 if (chan >= 4) {
473 chan -= 4;
474 location++;
475 }
476 emit_store_64bit_chan(bld_base, bld->outputs[location][chan],
477 bld->outputs[location][chan + 1], dst);
478 } else {
479 dst = LLVMBuildBitCast(builder, dst, float_bld->vec_type, "");
480 lp_exec_mask_store(&bld->exec_mask, float_bld, dst,
481 bld->outputs[location][chan + comp]);
482 }
483 }
484
485 static void emit_store_tcs_chan(struct lp_build_nir_context *bld_base,
486 unsigned bit_size,
487 unsigned location,
488 unsigned const_index,
489 LLVMValueRef indir_vertex_index,
490 LLVMValueRef indir_index,
491 unsigned comp,
492 unsigned chan,
493 LLVMValueRef chan_val)
494 {
495 struct gallivm_state *gallivm = bld_base->base.gallivm;
496 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
497 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
498 unsigned swizzle = chan;
499 if (bit_size == 64) {
500 swizzle *= 2;
501 swizzle += comp;
502 if (swizzle >= 4) {
503 swizzle -= 4;
504 location++;
505 }
506 } else
507 swizzle += comp;
508 LLVMValueRef attrib_index_val;
509 LLVMValueRef swizzle_index_val = lp_build_const_int32(gallivm, swizzle);
510
511 if (indir_index)
512 attrib_index_val = lp_build_add(&bld_base->uint_bld, indir_index, lp_build_const_int_vec(gallivm, bld_base->uint_bld.type, location));
513 else
514 attrib_index_val = lp_build_const_int32(gallivm, const_index + location);
515 if (bit_size == 64) {
516 LLVMValueRef split_vals[2];
517 LLVMValueRef swizzle_index_val2 = lp_build_const_int32(gallivm, swizzle + 1);
518 emit_store_64bit_split(bld_base, chan_val, split_vals);
519 bld->tcs_iface->emit_store_output(bld->tcs_iface, &bld_base->base, 0,
520 indir_vertex_index ? true : false,
521 indir_vertex_index,
522 indir_index ? true : false,
523 attrib_index_val, swizzle_index_val,
524 split_vals[0], mask_vec(bld_base));
525 bld->tcs_iface->emit_store_output(bld->tcs_iface, &bld_base->base, 0,
526 indir_vertex_index ? true : false,
527 indir_vertex_index,
528 indir_index ? true : false,
529 attrib_index_val, swizzle_index_val2,
530 split_vals[1], mask_vec(bld_base));
531 } else {
532 chan_val = LLVMBuildBitCast(builder, chan_val, bld_base->base.vec_type, "");
533 bld->tcs_iface->emit_store_output(bld->tcs_iface, &bld_base->base, 0,
534 indir_vertex_index ? true : false,
535 indir_vertex_index,
536 indir_index ? true : false,
537 attrib_index_val, swizzle_index_val,
538 chan_val, mask_vec(bld_base));
539 }
540 }
541
542 static void emit_store_var(struct lp_build_nir_context *bld_base,
543 nir_variable_mode deref_mode,
544 unsigned num_components,
545 unsigned bit_size,
546 nir_variable *var,
547 unsigned writemask,
548 LLVMValueRef indir_vertex_index,
549 unsigned const_index,
550 LLVMValueRef indir_index,
551 LLVMValueRef dst)
552 {
553 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
554 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
555 switch (deref_mode) {
556 case nir_var_shader_out: {
557 unsigned location = var->data.driver_location;
558 unsigned comp = var->data.location_frac;
559 if (bld_base->shader->info.stage == MESA_SHADER_FRAGMENT) {
560 if (var->data.location == FRAG_RESULT_STENCIL)
561 comp = 1;
562 else if (var->data.location == FRAG_RESULT_DEPTH)
563 comp = 2;
564 }
565
566 for (unsigned chan = 0; chan < num_components; chan++) {
567 if (writemask & (1u << chan)) {
568 LLVMValueRef chan_val = (num_components == 1) ? dst : LLVMBuildExtractValue(builder, dst, chan, "");
569 if (bld->tcs_iface) {
570 emit_store_tcs_chan(bld_base, bit_size, location, const_index, indir_vertex_index, indir_index, comp, chan, chan_val);
571 } else
572 emit_store_chan(bld_base, deref_mode, bit_size, location + const_index, comp, chan, chan_val);
573 }
574 }
575 break;
576 }
577 default:
578 break;
579 }
580 }
581
582 static LLVMValueRef emit_load_reg(struct lp_build_nir_context *bld_base,
583 struct lp_build_context *reg_bld,
584 const nir_reg_src *reg,
585 LLVMValueRef indir_src,
586 LLVMValueRef reg_storage)
587 {
588 struct gallivm_state *gallivm = bld_base->base.gallivm;
589 LLVMBuilderRef builder = gallivm->builder;
590 int nc = reg->reg->num_components;
591 LLVMValueRef vals[NIR_MAX_VEC_COMPONENTS] = { NULL };
592 struct lp_build_context *uint_bld = &bld_base->uint_bld;
593 if (reg->reg->num_array_elems) {
594 LLVMValueRef indirect_val = lp_build_const_int_vec(gallivm, uint_bld->type, reg->base_offset);
595 if (reg->indirect) {
596 LLVMValueRef max_index = lp_build_const_int_vec(gallivm, uint_bld->type, reg->reg->num_array_elems - 1);
597 indirect_val = LLVMBuildAdd(builder, indirect_val, indir_src, "");
598 indirect_val = lp_build_min(uint_bld, indirect_val, max_index);
599 }
600 reg_storage = LLVMBuildBitCast(builder, reg_storage, LLVMPointerType(reg_bld->elem_type, 0), "");
601 for (unsigned i = 0; i < nc; i++) {
602 LLVMValueRef indirect_offset = get_soa_array_offsets(uint_bld, indirect_val, nc, i, TRUE);
603 vals[i] = build_gather(bld_base, reg_bld, reg_storage, indirect_offset, NULL, NULL);
604 }
605 } else {
606 for (unsigned i = 0; i < nc; i++) {
607 LLVMValueRef this_storage = nc == 1 ? reg_storage : lp_build_array_get_ptr(gallivm, reg_storage,
608 lp_build_const_int32(gallivm, i));
609 vals[i] = LLVMBuildLoad(builder, this_storage, "");
610 }
611 }
612 return nc == 1 ? vals[0] : lp_nir_array_build_gather_values(builder, vals, nc);
613 }
614
615 static void emit_store_reg(struct lp_build_nir_context *bld_base,
616 struct lp_build_context *reg_bld,
617 const nir_reg_dest *reg,
618 unsigned writemask,
619 LLVMValueRef indir_src,
620 LLVMValueRef reg_storage,
621 LLVMValueRef dst[NIR_MAX_VEC_COMPONENTS])
622 {
623 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
624 struct gallivm_state *gallivm = bld_base->base.gallivm;
625 LLVMBuilderRef builder = gallivm->builder;
626 struct lp_build_context *uint_bld = &bld_base->uint_bld;
627 int nc = reg->reg->num_components;
628 if (reg->reg->num_array_elems > 0) {
629 LLVMValueRef indirect_val = lp_build_const_int_vec(gallivm, uint_bld->type, reg->base_offset);
630 if (reg->indirect) {
631 LLVMValueRef max_index = lp_build_const_int_vec(gallivm, uint_bld->type, reg->reg->num_array_elems - 1);
632 indirect_val = LLVMBuildAdd(builder, indirect_val, indir_src, "");
633 indirect_val = lp_build_min(uint_bld, indirect_val, max_index);
634 }
635 reg_storage = LLVMBuildBitCast(builder, reg_storage, LLVMPointerType(reg_bld->elem_type, 0), "");
636 for (unsigned i = 0; i < nc; i++) {
637 if (!(writemask & (1 << i)))
638 continue;
639 LLVMValueRef indirect_offset = get_soa_array_offsets(uint_bld, indirect_val, nc, i, TRUE);
640 dst[i] = LLVMBuildBitCast(builder, dst[i], reg_bld->vec_type, "");
641 emit_mask_scatter(bld, reg_storage, indirect_offset, dst[i], &bld->exec_mask);
642 }
643 return;
644 }
645
646 for (unsigned i = 0; i < nc; i++) {
647 LLVMValueRef this_storage = nc == 1 ? reg_storage : lp_build_array_get_ptr(gallivm, reg_storage,
648 lp_build_const_int32(gallivm, i));
649 dst[i] = LLVMBuildBitCast(builder, dst[i], reg_bld->vec_type, "");
650 lp_exec_mask_store(&bld->exec_mask, reg_bld, dst[i], this_storage);
651 }
652 }
653
654 static void emit_load_kernel_arg(struct lp_build_nir_context *bld_base,
655 unsigned nc,
656 unsigned bit_size,
657 unsigned offset_bit_size,
658 bool offset_is_uniform,
659 LLVMValueRef offset,
660 LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
661 {
662 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
663 struct gallivm_state *gallivm = bld_base->base.gallivm;
664 LLVMBuilderRef builder = gallivm->builder;
665 struct lp_build_context *bld_broad = get_int_bld(bld_base, true, bit_size);
666 LLVMValueRef kernel_args_ptr = bld->kernel_args_ptr;
667 unsigned size_shift = 0;
668 struct lp_build_context *bld_offset = get_int_bld(bld_base, true, offset_bit_size);
669 if (bit_size == 16)
670 size_shift = 1;
671 else if (bit_size == 32)
672 size_shift = 2;
673 else if (bit_size == 64)
674 size_shift = 3;
675 if (size_shift)
676 offset = lp_build_shr(bld_offset, offset, lp_build_const_int_vec(gallivm, bld_offset->type, size_shift));
677
678 LLVMTypeRef ptr_type = LLVMPointerType(bld_broad->elem_type, 0);
679 kernel_args_ptr = LLVMBuildBitCast(builder, kernel_args_ptr, ptr_type, "");
680
681 if (offset_is_uniform) {
682 offset = LLVMBuildExtractElement(builder, offset, lp_build_const_int32(gallivm, 0), "");
683
684 for (unsigned c = 0; c < nc; c++) {
685 LLVMValueRef this_offset = LLVMBuildAdd(builder, offset, offset_bit_size == 64 ? lp_build_const_int64(gallivm, c) : lp_build_const_int32(gallivm, c), "");
686
687 LLVMValueRef scalar = lp_build_pointer_get(builder, kernel_args_ptr, this_offset);
688 result[c] = lp_build_broadcast_scalar(bld_broad, scalar);
689 }
690 }
691 }
692
693 static LLVMValueRef global_addr_to_ptr(struct gallivm_state *gallivm, LLVMValueRef addr_ptr, unsigned bit_size)
694 {
695 LLVMBuilderRef builder = gallivm->builder;
696 switch (bit_size) {
697 case 8:
698 addr_ptr = LLVMBuildIntToPtr(builder, addr_ptr, LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0), "");
699 break;
700 case 16:
701 addr_ptr = LLVMBuildIntToPtr(builder, addr_ptr, LLVMPointerType(LLVMInt16TypeInContext(gallivm->context), 0), "");
702 break;
703 case 32:
704 default:
705 addr_ptr = LLVMBuildIntToPtr(builder, addr_ptr, LLVMPointerType(LLVMInt32TypeInContext(gallivm->context), 0), "");
706 break;
707 case 64:
708 addr_ptr = LLVMBuildIntToPtr(builder, addr_ptr, LLVMPointerType(LLVMInt64TypeInContext(gallivm->context), 0), "");
709 break;
710 }
711 return addr_ptr;
712 }
713
714 static void emit_load_global(struct lp_build_nir_context *bld_base,
715 unsigned nc,
716 unsigned bit_size,
717 unsigned addr_bit_size,
718 LLVMValueRef addr,
719 LLVMValueRef outval[NIR_MAX_VEC_COMPONENTS])
720 {
721 struct gallivm_state *gallivm = bld_base->base.gallivm;
722 LLVMBuilderRef builder = gallivm->builder;
723 struct lp_build_context *uint_bld = &bld_base->uint_bld;
724 struct lp_build_context *res_bld;
725
726 res_bld = get_int_bld(bld_base, true, bit_size);
727
728 for (unsigned c = 0; c < nc; c++) {
729 LLVMValueRef result = lp_build_alloca(gallivm, res_bld->vec_type, "");
730
731 struct lp_build_loop_state loop_state;
732 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
733
734 LLVMValueRef addr_ptr = LLVMBuildExtractElement(gallivm->builder, addr,
735 loop_state.counter, "");
736 addr_ptr = global_addr_to_ptr(gallivm, addr_ptr, bit_size);
737
738 LLVMValueRef value_ptr = lp_build_pointer_get(builder, addr_ptr, lp_build_const_int32(gallivm, c));
739
740 LLVMValueRef temp_res;
741 temp_res = LLVMBuildLoad(builder, result, "");
742 temp_res = LLVMBuildInsertElement(builder, temp_res, value_ptr, loop_state.counter, "");
743 LLVMBuildStore(builder, temp_res, result);
744 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
745 NULL, LLVMIntUGE);
746 outval[c] = LLVMBuildLoad(builder, result, "");
747 }
748 }
749
750 static void emit_store_global(struct lp_build_nir_context *bld_base,
751 unsigned writemask,
752 unsigned nc, unsigned bit_size,
753 unsigned addr_bit_size,
754 LLVMValueRef addr,
755 LLVMValueRef dst)
756 {
757 struct gallivm_state *gallivm = bld_base->base.gallivm;
758 LLVMBuilderRef builder = gallivm->builder;
759 struct lp_build_context *uint_bld = &bld_base->uint_bld;
760
761 for (unsigned c = 0; c < nc; c++) {
762 if (!(writemask & (1u << c)))
763 continue;
764 LLVMValueRef val = (nc == 1) ? dst : LLVMBuildExtractValue(builder, dst, c, "");
765
766 LLVMValueRef exec_mask = mask_vec(bld_base);
767 struct lp_build_loop_state loop_state;
768 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
769 LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, val,
770 loop_state.counter, "");
771
772 LLVMValueRef addr_ptr = LLVMBuildExtractElement(gallivm->builder, addr,
773 loop_state.counter, "");
774 addr_ptr = global_addr_to_ptr(gallivm, addr_ptr, bit_size);
775 switch (bit_size) {
776 case 32:
777 value_ptr = LLVMBuildBitCast(builder, value_ptr, LLVMInt32TypeInContext(gallivm->context), "");
778 break;
779 case 64:
780 value_ptr = LLVMBuildBitCast(builder, value_ptr, LLVMInt64TypeInContext(gallivm->context), "");
781 break;
782 default:
783 break;
784 }
785 struct lp_build_if_state ifthen;
786
787 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, "");
788 cond = LLVMBuildExtractElement(gallivm->builder, cond, loop_state.counter, "");
789 lp_build_if(&ifthen, gallivm, cond);
790 lp_build_pointer_set(builder, addr_ptr, lp_build_const_int32(gallivm, c), value_ptr);
791 lp_build_endif(&ifthen);
792 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
793 NULL, LLVMIntUGE);
794 }
795 }
796
797 static void emit_atomic_global(struct lp_build_nir_context *bld_base,
798 nir_intrinsic_op nir_op,
799 unsigned addr_bit_size,
800 LLVMValueRef addr,
801 LLVMValueRef val, LLVMValueRef val2,
802 LLVMValueRef *result)
803 {
804 struct gallivm_state *gallivm = bld_base->base.gallivm;
805 LLVMBuilderRef builder = gallivm->builder;
806 struct lp_build_context *uint_bld = &bld_base->uint_bld;
807
808 LLVMValueRef atom_res = lp_build_alloca(gallivm,
809 uint_bld->vec_type, "");
810 LLVMValueRef exec_mask = mask_vec(bld_base);
811 struct lp_build_loop_state loop_state;
812 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
813
814 LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, val,
815 loop_state.counter, "");
816
817 LLVMValueRef addr_ptr = LLVMBuildExtractElement(gallivm->builder, addr,
818 loop_state.counter, "");
819 addr_ptr = global_addr_to_ptr(gallivm, addr_ptr, 32);
820 struct lp_build_if_state ifthen;
821 LLVMValueRef cond, temp_res;
822 LLVMValueRef scalar;
823 cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, "");
824 cond = LLVMBuildExtractElement(gallivm->builder, cond, loop_state.counter, "");
825 lp_build_if(&ifthen, gallivm, cond);
826
827 if (nir_op == nir_intrinsic_global_atomic_comp_swap) {
828 LLVMValueRef cas_src_ptr = LLVMBuildExtractElement(gallivm->builder, val2,
829 loop_state.counter, "");
830 cas_src_ptr = LLVMBuildBitCast(gallivm->builder, cas_src_ptr, uint_bld->elem_type, "");
831 scalar = LLVMBuildAtomicCmpXchg(builder, addr_ptr, value_ptr,
832 cas_src_ptr,
833 LLVMAtomicOrderingSequentiallyConsistent,
834 LLVMAtomicOrderingSequentiallyConsistent,
835 false);
836 scalar = LLVMBuildExtractValue(gallivm->builder, scalar, 0, "");
837 } else {
838 LLVMAtomicRMWBinOp op;
839 switch (nir_op) {
840 case nir_intrinsic_global_atomic_add:
841 op = LLVMAtomicRMWBinOpAdd;
842 break;
843 case nir_intrinsic_global_atomic_exchange:
844 op = LLVMAtomicRMWBinOpXchg;
845 break;
846 case nir_intrinsic_global_atomic_and:
847 op = LLVMAtomicRMWBinOpAnd;
848 break;
849 case nir_intrinsic_global_atomic_or:
850 op = LLVMAtomicRMWBinOpOr;
851 break;
852 case nir_intrinsic_global_atomic_xor:
853 op = LLVMAtomicRMWBinOpXor;
854 break;
855 case nir_intrinsic_global_atomic_umin:
856 op = LLVMAtomicRMWBinOpUMin;
857 break;
858 case nir_intrinsic_global_atomic_umax:
859 op = LLVMAtomicRMWBinOpUMax;
860 break;
861 case nir_intrinsic_global_atomic_imin:
862 op = LLVMAtomicRMWBinOpMin;
863 break;
864 case nir_intrinsic_global_atomic_imax:
865 op = LLVMAtomicRMWBinOpMax;
866 break;
867 default:
868 unreachable("unknown atomic op");
869 }
870
871 scalar = LLVMBuildAtomicRMW(builder, op,
872 addr_ptr, value_ptr,
873 LLVMAtomicOrderingSequentiallyConsistent,
874 false);
875 }
876 temp_res = LLVMBuildLoad(builder, atom_res, "");
877 temp_res = LLVMBuildInsertElement(builder, temp_res, scalar, loop_state.counter, "");
878 LLVMBuildStore(builder, temp_res, atom_res);
879 lp_build_else(&ifthen);
880 temp_res = LLVMBuildLoad(builder, atom_res, "");
881 temp_res = LLVMBuildInsertElement(builder, temp_res, lp_build_const_int32(gallivm, 0), loop_state.counter, "");
882 LLVMBuildStore(builder, temp_res, atom_res);
883 lp_build_endif(&ifthen);
884 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
885 NULL, LLVMIntUGE);
886 *result = LLVMBuildLoad(builder, atom_res, "");
887 }
888
889 static void emit_load_ubo(struct lp_build_nir_context *bld_base,
890 unsigned nc,
891 unsigned bit_size,
892 bool offset_is_uniform,
893 LLVMValueRef index,
894 LLVMValueRef offset,
895 LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
896 {
897 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
898 struct gallivm_state *gallivm = bld_base->base.gallivm;
899 LLVMBuilderRef builder = gallivm->builder;
900 struct lp_build_context *uint_bld = &bld_base->uint_bld;
901 struct lp_build_context *bld_broad = bit_size == 64 ? &bld_base->dbl_bld : &bld_base->base;
902 LLVMValueRef consts_ptr = lp_build_array_get(gallivm, bld->consts_ptr, index);
903 unsigned size_shift = 0;
904 if (bit_size == 32)
905 size_shift = 2;
906 else if (bit_size == 64)
907 size_shift = 3;
908 if (size_shift)
909 offset = lp_build_shr(uint_bld, offset, lp_build_const_int_vec(gallivm, uint_bld->type, size_shift));
910 if (bit_size == 64) {
911 LLVMTypeRef dptr_type = LLVMPointerType(bld_base->dbl_bld.elem_type, 0);
912 consts_ptr = LLVMBuildBitCast(builder, consts_ptr, dptr_type, "");
913 }
914
915 if (offset_is_uniform) {
916 offset = LLVMBuildExtractElement(builder, offset, lp_build_const_int32(gallivm, 0), "");
917
918 for (unsigned c = 0; c < nc; c++) {
919 LLVMValueRef this_offset = LLVMBuildAdd(builder, offset, lp_build_const_int32(gallivm, c), "");
920
921 LLVMValueRef scalar = lp_build_pointer_get(builder, consts_ptr, this_offset);
922 result[c] = lp_build_broadcast_scalar(bld_broad, scalar);
923 }
924 } else {
925 LLVMValueRef overflow_mask;
926 LLVMValueRef num_consts = lp_build_array_get(gallivm, bld->const_sizes_ptr, index);
927
928 num_consts = LLVMBuildShl(gallivm->builder, num_consts, lp_build_const_int32(gallivm, 4), "");
929 num_consts = lp_build_broadcast_scalar(uint_bld, num_consts);
930 for (unsigned c = 0; c < nc; c++) {
931 LLVMValueRef this_offset = lp_build_add(uint_bld, offset, lp_build_const_int_vec(gallivm, uint_bld->type, c));
932 overflow_mask = lp_build_compare(gallivm, uint_bld->type, PIPE_FUNC_GEQUAL,
933 this_offset, num_consts);
934
935 result[c] = build_gather(bld_base, bld_broad, consts_ptr, this_offset, overflow_mask, NULL);
936 }
937 }
938 }
939
940
941 static void emit_load_mem(struct lp_build_nir_context *bld_base,
942 unsigned nc,
943 unsigned bit_size,
944 LLVMValueRef index,
945 LLVMValueRef offset,
946 LLVMValueRef outval[NIR_MAX_VEC_COMPONENTS])
947 {
948 struct gallivm_state *gallivm = bld_base->base.gallivm;
949 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
950 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
951 LLVMValueRef ssbo_ptr = NULL;
952 struct lp_build_context *uint_bld = &bld_base->uint_bld;
953 struct lp_build_context *uint64_bld = &bld_base->uint64_bld;
954 LLVMValueRef ssbo_limit = NULL;
955
956 if (index) {
957 LLVMValueRef ssbo_size_ptr = lp_build_array_get(gallivm, bld->ssbo_sizes_ptr, LLVMBuildExtractElement(builder, index, lp_build_const_int32(gallivm, 0), ""));
958 ssbo_limit = LLVMBuildAShr(gallivm->builder, ssbo_size_ptr, lp_build_const_int32(gallivm, bit_size == 64 ? 3 : 2), "");
959 ssbo_limit = lp_build_broadcast_scalar(uint_bld, ssbo_limit);
960
961 ssbo_ptr = lp_build_array_get(gallivm, bld->ssbo_ptr, LLVMBuildExtractElement(builder, index, lp_build_const_int32(gallivm, 0), ""));
962 } else
963 ssbo_ptr = bld->shared_ptr;
964
965 offset = LLVMBuildAShr(gallivm->builder, offset, lp_build_const_int_vec(gallivm, uint_bld->type, bit_size == 64 ? 3 : 2), "");
966 for (unsigned c = 0; c < nc; c++) {
967 LLVMValueRef loop_index = lp_build_add(uint_bld, offset, lp_build_const_int_vec(gallivm, uint_bld->type, c));
968 LLVMValueRef exec_mask = mask_vec(bld_base);
969
970 if (ssbo_limit) {
971 LLVMValueRef ssbo_oob_cmp = lp_build_cmp(uint_bld, PIPE_FUNC_LESS, loop_index, ssbo_limit);
972 exec_mask = LLVMBuildAnd(builder, exec_mask, ssbo_oob_cmp, "");
973 }
974
975 LLVMValueRef result = lp_build_alloca(gallivm, bit_size == 64 ? uint64_bld->vec_type : uint_bld->vec_type, "");
976 struct lp_build_loop_state loop_state;
977 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
978
979 struct lp_build_if_state ifthen;
980 LLVMValueRef cond, temp_res;
981
982 loop_index = LLVMBuildExtractElement(gallivm->builder, loop_index,
983 loop_state.counter, "");
984
985 cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, "");
986 cond = LLVMBuildExtractElement(gallivm->builder, cond, loop_state.counter, "");
987
988 lp_build_if(&ifthen, gallivm, cond);
989 LLVMValueRef scalar;
990 if (bit_size == 64) {
991 LLVMValueRef ssbo_ptr2 = LLVMBuildBitCast(builder, ssbo_ptr, LLVMPointerType(uint64_bld->elem_type, 0), "");
992 scalar = lp_build_pointer_get(builder, ssbo_ptr2, loop_index);
993 } else
994 scalar = lp_build_pointer_get(builder, ssbo_ptr, loop_index);
995
996 temp_res = LLVMBuildLoad(builder, result, "");
997 temp_res = LLVMBuildInsertElement(builder, temp_res, scalar, loop_state.counter, "");
998 LLVMBuildStore(builder, temp_res, result);
999 lp_build_else(&ifthen);
1000 temp_res = LLVMBuildLoad(builder, result, "");
1001 LLVMValueRef zero;
1002 if (bit_size == 64)
1003 zero = LLVMConstInt(LLVMInt64TypeInContext(gallivm->context), 0, 0);
1004 else
1005 zero = lp_build_const_int32(gallivm, 0);
1006 temp_res = LLVMBuildInsertElement(builder, temp_res, zero, loop_state.counter, "");
1007 LLVMBuildStore(builder, temp_res, result);
1008 lp_build_endif(&ifthen);
1009 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
1010 NULL, LLVMIntUGE);
1011 outval[c] = LLVMBuildLoad(gallivm->builder, result, "");
1012 }
1013 }
1014
1015 static void emit_store_mem(struct lp_build_nir_context *bld_base,
1016 unsigned writemask,
1017 unsigned nc,
1018 unsigned bit_size,
1019 LLVMValueRef index,
1020 LLVMValueRef offset,
1021 LLVMValueRef dst)
1022 {
1023 struct gallivm_state *gallivm = bld_base->base.gallivm;
1024 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1025 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1026 LLVMValueRef ssbo_ptr;
1027 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1028 LLVMValueRef ssbo_limit = NULL;
1029
1030 if (index) {
1031 LLVMValueRef ssbo_size_ptr = lp_build_array_get(gallivm, bld->ssbo_sizes_ptr, LLVMBuildExtractElement(builder, index, lp_build_const_int32(gallivm, 0), ""));
1032 ssbo_limit = LLVMBuildAShr(gallivm->builder, ssbo_size_ptr, lp_build_const_int32(gallivm, bit_size == 64 ? 3 : 2), "");
1033 ssbo_limit = lp_build_broadcast_scalar(uint_bld, ssbo_limit);
1034 ssbo_ptr = lp_build_array_get(gallivm, bld->ssbo_ptr, LLVMBuildExtractElement(builder, index, lp_build_const_int32(gallivm, 0), ""));
1035 } else
1036 ssbo_ptr = bld->shared_ptr;
1037
1038 offset = lp_build_shr_imm(uint_bld, offset, bit_size == 64 ? 3 : 2);
1039 for (unsigned c = 0; c < nc; c++) {
1040 if (!(writemask & (1u << c)))
1041 continue;
1042 LLVMValueRef loop_index = lp_build_add(uint_bld, offset, lp_build_const_int_vec(gallivm, uint_bld->type, c));
1043 LLVMValueRef val = (nc == 1) ? dst : LLVMBuildExtractValue(builder, dst, c, "");
1044
1045 LLVMValueRef exec_mask = mask_vec(bld_base);
1046 if (ssbo_limit) {
1047 LLVMValueRef ssbo_oob_cmp = lp_build_cmp(uint_bld, PIPE_FUNC_LESS, loop_index, ssbo_limit);
1048 exec_mask = LLVMBuildAnd(builder, exec_mask, ssbo_oob_cmp, "");
1049 }
1050
1051 struct lp_build_loop_state loop_state;
1052 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
1053 LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, val,
1054 loop_state.counter, "");
1055 if (bit_size == 64)
1056 value_ptr = LLVMBuildBitCast(gallivm->builder, value_ptr, bld_base->uint64_bld.elem_type, "");
1057 else
1058 value_ptr = LLVMBuildBitCast(gallivm->builder, value_ptr, uint_bld->elem_type, "");
1059 struct lp_build_if_state ifthen;
1060 LLVMValueRef cond;
1061
1062 loop_index = LLVMBuildExtractElement(gallivm->builder, loop_index,
1063 loop_state.counter, "");
1064 cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, "");
1065 cond = LLVMBuildExtractElement(gallivm->builder, cond, loop_state.counter, "");
1066 lp_build_if(&ifthen, gallivm, cond);
1067 if (bit_size == 64) {
1068 LLVMValueRef ssbo_ptr2 = LLVMBuildBitCast(builder, ssbo_ptr, LLVMPointerType(bld_base->uint64_bld.elem_type, 0), "");
1069 lp_build_pointer_set(builder, ssbo_ptr2, loop_index, value_ptr);
1070 } else
1071 lp_build_pointer_set(builder, ssbo_ptr, loop_index, value_ptr);
1072 lp_build_endif(&ifthen);
1073 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
1074 NULL, LLVMIntUGE);
1075 }
1076 }
1077
1078 static void emit_atomic_mem(struct lp_build_nir_context *bld_base,
1079 nir_intrinsic_op nir_op,
1080 LLVMValueRef index, LLVMValueRef offset,
1081 LLVMValueRef val, LLVMValueRef val2,
1082 LLVMValueRef *result)
1083 {
1084 struct gallivm_state *gallivm = bld_base->base.gallivm;
1085 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1086 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1087 LLVMValueRef ssbo_ptr;
1088 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1089 LLVMValueRef ssbo_limit = NULL;
1090
1091 if (index) {
1092 LLVMValueRef ssbo_size_ptr = lp_build_array_get(gallivm, bld->ssbo_sizes_ptr, LLVMBuildExtractElement(builder, index, lp_build_const_int32(gallivm, 0), ""));
1093 ssbo_limit = LLVMBuildAShr(gallivm->builder, ssbo_size_ptr, lp_build_const_int32(gallivm, 2), "");
1094 ssbo_limit = lp_build_broadcast_scalar(uint_bld, ssbo_limit);
1095 ssbo_ptr = lp_build_array_get(gallivm, bld->ssbo_ptr, LLVMBuildExtractElement(builder, index, lp_build_const_int32(gallivm, 0), ""));
1096 } else
1097 ssbo_ptr = bld->shared_ptr;
1098
1099 offset = lp_build_shr_imm(uint_bld, offset, 2);
1100 LLVMValueRef atom_res = lp_build_alloca(gallivm,
1101 uint_bld->vec_type, "");
1102
1103 LLVMValueRef exec_mask = mask_vec(bld_base);
1104 if (ssbo_limit) {
1105 LLVMValueRef ssbo_oob_cmp = lp_build_cmp(uint_bld, PIPE_FUNC_LESS, offset, ssbo_limit);
1106 exec_mask = LLVMBuildAnd(builder, exec_mask, ssbo_oob_cmp, "");
1107 }
1108
1109 struct lp_build_loop_state loop_state;
1110 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
1111
1112 LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, val,
1113 loop_state.counter, "");
1114 value_ptr = LLVMBuildBitCast(gallivm->builder, value_ptr, uint_bld->elem_type, "");
1115
1116 offset = LLVMBuildExtractElement(gallivm->builder, offset,
1117 loop_state.counter, "");
1118
1119 LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, ssbo_ptr,
1120 &offset, 1, "");
1121
1122 struct lp_build_if_state ifthen;
1123 LLVMValueRef cond, temp_res;
1124 LLVMValueRef scalar;
1125 cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, "");
1126 cond = LLVMBuildExtractElement(gallivm->builder, cond, loop_state.counter, "");
1127 lp_build_if(&ifthen, gallivm, cond);
1128
1129 if (nir_op == nir_intrinsic_ssbo_atomic_comp_swap || nir_op == nir_intrinsic_shared_atomic_comp_swap) {
1130 LLVMValueRef cas_src_ptr = LLVMBuildExtractElement(gallivm->builder, val2,
1131 loop_state.counter, "");
1132 cas_src_ptr = LLVMBuildBitCast(gallivm->builder, cas_src_ptr, uint_bld->elem_type, "");
1133 scalar = LLVMBuildAtomicCmpXchg(builder, scalar_ptr, value_ptr,
1134 cas_src_ptr,
1135 LLVMAtomicOrderingSequentiallyConsistent,
1136 LLVMAtomicOrderingSequentiallyConsistent,
1137 false);
1138 scalar = LLVMBuildExtractValue(gallivm->builder, scalar, 0, "");
1139 } else {
1140 LLVMAtomicRMWBinOp op;
1141
1142 switch (nir_op) {
1143 case nir_intrinsic_shared_atomic_add:
1144 case nir_intrinsic_ssbo_atomic_add:
1145 op = LLVMAtomicRMWBinOpAdd;
1146 break;
1147 case nir_intrinsic_shared_atomic_exchange:
1148 case nir_intrinsic_ssbo_atomic_exchange:
1149 op = LLVMAtomicRMWBinOpXchg;
1150 break;
1151 case nir_intrinsic_shared_atomic_and:
1152 case nir_intrinsic_ssbo_atomic_and:
1153 op = LLVMAtomicRMWBinOpAnd;
1154 break;
1155 case nir_intrinsic_shared_atomic_or:
1156 case nir_intrinsic_ssbo_atomic_or:
1157 op = LLVMAtomicRMWBinOpOr;
1158 break;
1159 case nir_intrinsic_shared_atomic_xor:
1160 case nir_intrinsic_ssbo_atomic_xor:
1161 op = LLVMAtomicRMWBinOpXor;
1162 break;
1163 case nir_intrinsic_shared_atomic_umin:
1164 case nir_intrinsic_ssbo_atomic_umin:
1165 op = LLVMAtomicRMWBinOpUMin;
1166 break;
1167 case nir_intrinsic_shared_atomic_umax:
1168 case nir_intrinsic_ssbo_atomic_umax:
1169 op = LLVMAtomicRMWBinOpUMax;
1170 break;
1171 case nir_intrinsic_ssbo_atomic_imin:
1172 case nir_intrinsic_shared_atomic_imin:
1173 op = LLVMAtomicRMWBinOpMin;
1174 break;
1175 case nir_intrinsic_ssbo_atomic_imax:
1176 case nir_intrinsic_shared_atomic_imax:
1177 op = LLVMAtomicRMWBinOpMax;
1178 break;
1179 default:
1180 unreachable("unknown atomic op");
1181 }
1182 scalar = LLVMBuildAtomicRMW(builder, op,
1183 scalar_ptr, value_ptr,
1184 LLVMAtomicOrderingSequentiallyConsistent,
1185 false);
1186 }
1187 temp_res = LLVMBuildLoad(builder, atom_res, "");
1188 temp_res = LLVMBuildInsertElement(builder, temp_res, scalar, loop_state.counter, "");
1189 LLVMBuildStore(builder, temp_res, atom_res);
1190 lp_build_else(&ifthen);
1191 temp_res = LLVMBuildLoad(builder, atom_res, "");
1192 temp_res = LLVMBuildInsertElement(builder, temp_res, lp_build_const_int32(gallivm, 0), loop_state.counter, "");
1193 LLVMBuildStore(builder, temp_res, atom_res);
1194 lp_build_endif(&ifthen);
1195
1196 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
1197 NULL, LLVMIntUGE);
1198 *result = LLVMBuildLoad(builder, atom_res, "");
1199 }
1200
1201 static void emit_barrier(struct lp_build_nir_context *bld_base)
1202 {
1203 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1204 struct gallivm_state * gallivm = bld_base->base.gallivm;
1205
1206 LLVMBasicBlockRef resume = lp_build_insert_new_block(gallivm, "resume");
1207
1208 lp_build_coro_suspend_switch(gallivm, bld->coro, resume, false);
1209 LLVMPositionBuilderAtEnd(gallivm->builder, resume);
1210 }
1211
1212 static LLVMValueRef emit_get_buffer_size(struct lp_build_nir_context *bld_base,
1213 LLVMValueRef index)
1214 {
1215 struct gallivm_state *gallivm = bld_base->base.gallivm;
1216 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1217 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1218 struct lp_build_context *bld_broad = &bld_base->uint_bld;
1219 LLVMValueRef size_ptr = lp_build_array_get(bld_base->base.gallivm, bld->ssbo_sizes_ptr,
1220 LLVMBuildExtractElement(builder, index, lp_build_const_int32(gallivm, 0), ""));
1221 return lp_build_broadcast_scalar(bld_broad, size_ptr);
1222 }
1223
1224 static void emit_image_op(struct lp_build_nir_context *bld_base,
1225 struct lp_img_params *params)
1226 {
1227 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1228 struct gallivm_state *gallivm = bld_base->base.gallivm;
1229
1230 params->type = bld_base->base.type;
1231 params->context_ptr = bld->context_ptr;
1232 params->thread_data_ptr = bld->thread_data_ptr;
1233 params->exec_mask = mask_vec(bld_base);
1234
1235 if (params->image_index_offset)
1236 params->image_index_offset = LLVMBuildExtractElement(gallivm->builder, params->image_index_offset,
1237 lp_build_const_int32(gallivm, 0), "");
1238
1239 bld->image->emit_op(bld->image,
1240 bld->bld_base.base.gallivm,
1241 params);
1242
1243 }
1244
1245 static void emit_image_size(struct lp_build_nir_context *bld_base,
1246 struct lp_sampler_size_query_params *params)
1247 {
1248 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1249 struct gallivm_state *gallivm = bld_base->base.gallivm;
1250
1251 params->int_type = bld_base->int_bld.type;
1252 params->context_ptr = bld->context_ptr;
1253
1254 if (params->texture_unit_offset)
1255 params->texture_unit_offset = LLVMBuildExtractElement(gallivm->builder, params->texture_unit_offset,
1256 lp_build_const_int32(gallivm, 0), "");
1257 bld->image->emit_size_query(bld->image,
1258 bld->bld_base.base.gallivm,
1259 params);
1260
1261 }
1262
1263 static void init_var_slots(struct lp_build_nir_context *bld_base,
1264 nir_variable *var, unsigned sc)
1265 {
1266 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1267 unsigned slots = glsl_count_attribute_slots(var->type, false) * 4;
1268
1269 if (!bld->outputs)
1270 return;
1271 for (unsigned comp = sc; comp < slots + sc; comp++) {
1272 unsigned this_loc = var->data.driver_location + (comp / 4);
1273 unsigned this_chan = comp % 4;
1274
1275 if (!bld->outputs[this_loc][this_chan])
1276 bld->outputs[this_loc][this_chan] = lp_build_alloca(bld_base->base.gallivm,
1277 bld_base->base.vec_type, "output");
1278 }
1279 }
1280
1281 static void emit_var_decl(struct lp_build_nir_context *bld_base,
1282 nir_variable *var)
1283 {
1284 unsigned sc = var->data.location_frac;
1285 switch (var->data.mode) {
1286 case nir_var_shader_out: {
1287 if (bld_base->shader->info.stage == MESA_SHADER_FRAGMENT) {
1288 if (var->data.location == FRAG_RESULT_STENCIL)
1289 sc = 1;
1290 else if (var->data.location == FRAG_RESULT_DEPTH)
1291 sc = 2;
1292 }
1293 init_var_slots(bld_base, var, sc);
1294 break;
1295 }
1296 default:
1297 break;
1298 }
1299 }
1300
1301 static void emit_tex(struct lp_build_nir_context *bld_base,
1302 struct lp_sampler_params *params)
1303 {
1304 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1305 struct gallivm_state *gallivm = bld_base->base.gallivm;
1306
1307 params->type = bld_base->base.type;
1308 params->context_ptr = bld->context_ptr;
1309 params->thread_data_ptr = bld->thread_data_ptr;
1310
1311 if (params->texture_index_offset && bld_base->shader->info.stage != MESA_SHADER_FRAGMENT) {
1312 /* this is horrible but this can be dynamic */
1313 LLVMValueRef coords[5];
1314 LLVMValueRef *orig_texel_ptr;
1315 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1316 LLVMValueRef result[4] = { LLVMGetUndef(bld_base->base.vec_type),
1317 LLVMGetUndef(bld_base->base.vec_type),
1318 LLVMGetUndef(bld_base->base.vec_type),
1319 LLVMGetUndef(bld_base->base.vec_type) };
1320 LLVMValueRef texel[4], orig_offset;
1321 unsigned i;
1322 orig_texel_ptr = params->texel;
1323
1324 for (i = 0; i < 5; i++) {
1325 coords[i] = params->coords[i];
1326 }
1327 orig_offset = params->texture_index_offset;
1328
1329 for (unsigned v = 0; v < uint_bld->type.length; v++) {
1330 LLVMValueRef idx = lp_build_const_int32(gallivm, v);
1331 LLVMValueRef new_coords[5];
1332 for (i = 0; i < 5; i++) {
1333 new_coords[i] = LLVMBuildExtractElement(gallivm->builder,
1334 coords[i], idx, "");
1335 }
1336 params->coords = new_coords;
1337 params->texture_index_offset = LLVMBuildExtractElement(gallivm->builder,
1338 orig_offset,
1339 idx, "");
1340 params->type = lp_elem_type(bld_base->base.type);
1341
1342 params->texel = texel;
1343 bld->sampler->emit_tex_sample(bld->sampler,
1344 gallivm,
1345 params);
1346
1347 for (i = 0; i < 4; i++) {
1348 result[i] = LLVMBuildInsertElement(gallivm->builder, result[i], texel[i], idx, "");
1349 }
1350 }
1351 for (i = 0; i < 4; i++) {
1352 orig_texel_ptr[i] = result[i];
1353 }
1354 return;
1355 }
1356
1357 if (params->texture_index_offset)
1358 params->texture_index_offset = LLVMBuildExtractElement(bld_base->base.gallivm->builder,
1359 params->texture_index_offset,
1360 lp_build_const_int32(bld_base->base.gallivm, 0), "");
1361
1362 params->type = bld_base->base.type;
1363 bld->sampler->emit_tex_sample(bld->sampler,
1364 bld->bld_base.base.gallivm,
1365 params);
1366 }
1367
1368 static void emit_tex_size(struct lp_build_nir_context *bld_base,
1369 struct lp_sampler_size_query_params *params)
1370 {
1371 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1372
1373 params->int_type = bld_base->int_bld.type;
1374 params->context_ptr = bld->context_ptr;
1375
1376 if (params->texture_unit_offset)
1377 params->texture_unit_offset = LLVMBuildExtractElement(bld_base->base.gallivm->builder,
1378 params->texture_unit_offset,
1379 lp_build_const_int32(bld_base->base.gallivm, 0), "");
1380 bld->sampler->emit_size_query(bld->sampler,
1381 bld->bld_base.base.gallivm,
1382 params);
1383 }
1384
1385 static void emit_sysval_intrin(struct lp_build_nir_context *bld_base,
1386 nir_intrinsic_instr *instr,
1387 LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1388 {
1389 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1390 struct gallivm_state *gallivm = bld_base->base.gallivm;
1391 switch (instr->intrinsic) {
1392 case nir_intrinsic_load_instance_id:
1393 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.instance_id);
1394 break;
1395 case nir_intrinsic_load_base_instance:
1396 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.base_instance);
1397 break;
1398 case nir_intrinsic_load_base_vertex:
1399 result[0] = bld->system_values.basevertex;
1400 break;
1401 case nir_intrinsic_load_vertex_id:
1402 result[0] = bld->system_values.vertex_id;
1403 break;
1404 case nir_intrinsic_load_primitive_id:
1405 result[0] = bld->system_values.prim_id;
1406 break;
1407 case nir_intrinsic_load_work_group_id:
1408 for (unsigned i = 0; i < 3; i++)
1409 result[i] = lp_build_broadcast_scalar(&bld_base->uint_bld, LLVMBuildExtractElement(gallivm->builder, bld->system_values.block_id, lp_build_const_int32(gallivm, i), ""));
1410 break;
1411 case nir_intrinsic_load_local_invocation_id:
1412 for (unsigned i = 0; i < 3; i++)
1413 result[i] = LLVMBuildExtractValue(gallivm->builder, bld->system_values.thread_id, i, "");
1414 break;
1415 case nir_intrinsic_load_num_work_groups:
1416 for (unsigned i = 0; i < 3; i++)
1417 result[i] = lp_build_broadcast_scalar(&bld_base->uint_bld, LLVMBuildExtractElement(gallivm->builder, bld->system_values.grid_size, lp_build_const_int32(gallivm, i), ""));
1418 break;
1419 case nir_intrinsic_load_invocation_id:
1420 if (bld_base->shader->info.stage == MESA_SHADER_TESS_CTRL)
1421 result[0] = bld->system_values.invocation_id;
1422 else
1423 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.invocation_id);
1424 break;
1425 case nir_intrinsic_load_front_face:
1426 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.front_facing);
1427 break;
1428 case nir_intrinsic_load_draw_id:
1429 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.draw_id);
1430 break;
1431 default:
1432 break;
1433 case nir_intrinsic_load_local_group_size:
1434 for (unsigned i = 0; i < 3; i++)
1435 result[i] = lp_build_broadcast_scalar(&bld_base->uint_bld, LLVMBuildExtractElement(gallivm->builder, bld->system_values.block_size, lp_build_const_int32(gallivm, i), ""));
1436 break;
1437 case nir_intrinsic_load_work_dim:
1438 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.work_dim);
1439 break;
1440 case nir_intrinsic_load_tess_coord:
1441 for (unsigned i = 0; i < 3; i++) {
1442 result[i] = LLVMBuildExtractValue(gallivm->builder, bld->system_values.tess_coord, i, "");
1443 }
1444 break;
1445 case nir_intrinsic_load_tess_level_outer:
1446 for (unsigned i = 0; i < 4; i++)
1447 result[i] = lp_build_broadcast_scalar(&bld_base->base, LLVMBuildExtractValue(gallivm->builder, bld->system_values.tess_outer, i, ""));
1448 break;
1449 case nir_intrinsic_load_tess_level_inner:
1450 for (unsigned i = 0; i < 2; i++)
1451 result[i] = lp_build_broadcast_scalar(&bld_base->base, LLVMBuildExtractValue(gallivm->builder, bld->system_values.tess_inner, i, ""));
1452 break;
1453 case nir_intrinsic_load_patch_vertices_in:
1454 result[0] = bld->system_values.vertices_in;
1455 break;
1456 case nir_intrinsic_load_sample_id:
1457 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.sample_id);
1458 break;
1459 case nir_intrinsic_load_sample_pos:
1460 for (unsigned i = 0; i < 2; i++) {
1461 LLVMValueRef idx = LLVMBuildMul(gallivm->builder, bld->system_values.sample_id, lp_build_const_int32(gallivm, 2), "");
1462 idx = LLVMBuildAdd(gallivm->builder, idx, lp_build_const_int32(gallivm, i), "");
1463 LLVMValueRef val = lp_build_array_get(gallivm, bld->system_values.sample_pos, idx);
1464 result[i] = lp_build_broadcast_scalar(&bld_base->base, val);
1465 }
1466 break;
1467 case nir_intrinsic_load_sample_mask_in:
1468 result[0] = bld->system_values.sample_mask_in;
1469 break;
1470 }
1471 }
1472
1473 static void emit_helper_invocation(struct lp_build_nir_context *bld_base,
1474 LLVMValueRef *dst)
1475 {
1476 struct gallivm_state *gallivm = bld_base->base.gallivm;
1477 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1478 *dst = lp_build_cmp(uint_bld, PIPE_FUNC_NOTEQUAL, mask_vec(bld_base), lp_build_const_int_vec(gallivm, uint_bld->type, -1));
1479 }
1480
1481 static void bgnloop(struct lp_build_nir_context *bld_base)
1482 {
1483 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1484 lp_exec_bgnloop(&bld->exec_mask, true);
1485 }
1486
1487 static void endloop(struct lp_build_nir_context *bld_base)
1488 {
1489 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1490 lp_exec_endloop(bld_base->base.gallivm, &bld->exec_mask);
1491 }
1492
1493 static void if_cond(struct lp_build_nir_context *bld_base, LLVMValueRef cond)
1494 {
1495 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1496 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1497 lp_exec_mask_cond_push(&bld->exec_mask, LLVMBuildBitCast(builder, cond, bld_base->base.int_vec_type, ""));
1498 }
1499
1500 static void else_stmt(struct lp_build_nir_context *bld_base)
1501 {
1502 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1503 lp_exec_mask_cond_invert(&bld->exec_mask);
1504 }
1505
1506 static void endif_stmt(struct lp_build_nir_context *bld_base)
1507 {
1508 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1509 lp_exec_mask_cond_pop(&bld->exec_mask);
1510 }
1511
1512 static void break_stmt(struct lp_build_nir_context *bld_base)
1513 {
1514 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1515
1516 lp_exec_break(&bld->exec_mask, NULL, false);
1517 }
1518
1519 static void continue_stmt(struct lp_build_nir_context *bld_base)
1520 {
1521 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1522 lp_exec_continue(&bld->exec_mask);
1523 }
1524
1525 static void discard(struct lp_build_nir_context *bld_base, LLVMValueRef cond)
1526 {
1527 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1528 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1529 LLVMValueRef mask;
1530
1531 if (!cond) {
1532 if (bld->exec_mask.has_mask) {
1533 mask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp");
1534 } else {
1535 mask = LLVMConstNull(bld->bld_base.base.int_vec_type);
1536 }
1537 } else {
1538 mask = LLVMBuildNot(builder, cond, "");
1539 if (bld->exec_mask.has_mask) {
1540 LLVMValueRef invmask;
1541 invmask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp");
1542 mask = LLVMBuildOr(builder, mask, invmask, "");
1543 }
1544 }
1545 lp_build_mask_update(bld->mask, mask);
1546 }
1547
1548 static void
1549 increment_vec_ptr_by_mask(struct lp_build_nir_context * bld_base,
1550 LLVMValueRef ptr,
1551 LLVMValueRef mask)
1552 {
1553 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1554 LLVMValueRef current_vec = LLVMBuildLoad(builder, ptr, "");
1555
1556 current_vec = LLVMBuildSub(builder, current_vec, mask, "");
1557
1558 LLVMBuildStore(builder, current_vec, ptr);
1559 }
1560
1561 static void
1562 clear_uint_vec_ptr_from_mask(struct lp_build_nir_context * bld_base,
1563 LLVMValueRef ptr,
1564 LLVMValueRef mask)
1565 {
1566 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1567 LLVMValueRef current_vec = LLVMBuildLoad(builder, ptr, "");
1568
1569 current_vec = lp_build_select(&bld_base->uint_bld,
1570 mask,
1571 bld_base->uint_bld.zero,
1572 current_vec);
1573
1574 LLVMBuildStore(builder, current_vec, ptr);
1575 }
1576
1577 static LLVMValueRef
1578 clamp_mask_to_max_output_vertices(struct lp_build_nir_soa_context * bld,
1579 LLVMValueRef current_mask_vec,
1580 LLVMValueRef total_emitted_vertices_vec)
1581 {
1582 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1583 struct lp_build_context *int_bld = &bld->bld_base.int_bld;
1584 LLVMValueRef max_mask = lp_build_cmp(int_bld, PIPE_FUNC_LESS,
1585 total_emitted_vertices_vec,
1586 bld->max_output_vertices_vec);
1587
1588 return LLVMBuildAnd(builder, current_mask_vec, max_mask, "");
1589 }
1590
1591 static void emit_vertex(struct lp_build_nir_context *bld_base, uint32_t stream_id)
1592 {
1593 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1594 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1595
1596 assert(bld->gs_iface->emit_vertex);
1597 LLVMValueRef total_emitted_vertices_vec =
1598 LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr[stream_id], "");
1599 LLVMValueRef mask = mask_vec(bld_base);
1600 mask = clamp_mask_to_max_output_vertices(bld, mask,
1601 total_emitted_vertices_vec);
1602 bld->gs_iface->emit_vertex(bld->gs_iface, &bld->bld_base.base,
1603 bld->outputs,
1604 total_emitted_vertices_vec,
1605 lp_build_const_int_vec(bld->bld_base.base.gallivm, bld->bld_base.base.type, stream_id));
1606
1607 increment_vec_ptr_by_mask(bld_base, bld->emitted_vertices_vec_ptr[stream_id],
1608 mask);
1609 increment_vec_ptr_by_mask(bld_base, bld->total_emitted_vertices_vec_ptr[stream_id],
1610 mask);
1611 }
1612
1613 static void
1614 end_primitive_masked(struct lp_build_nir_context * bld_base,
1615 LLVMValueRef mask, uint32_t stream_id)
1616 {
1617 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1618 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1619
1620 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1621 LLVMValueRef emitted_vertices_vec =
1622 LLVMBuildLoad(builder, bld->emitted_vertices_vec_ptr[stream_id], "");
1623 LLVMValueRef emitted_prims_vec =
1624 LLVMBuildLoad(builder, bld->emitted_prims_vec_ptr[stream_id], "");
1625 LLVMValueRef total_emitted_vertices_vec =
1626 LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr[stream_id], "");
1627
1628 LLVMValueRef emitted_mask = lp_build_cmp(uint_bld,
1629 PIPE_FUNC_NOTEQUAL,
1630 emitted_vertices_vec,
1631 uint_bld->zero);
1632 mask = LLVMBuildAnd(builder, mask, emitted_mask, "");
1633 if (stream_id == 0)
1634 bld->gs_iface->end_primitive(bld->gs_iface, &bld->bld_base.base,
1635 total_emitted_vertices_vec,
1636 emitted_vertices_vec, emitted_prims_vec, mask);
1637 increment_vec_ptr_by_mask(bld_base, bld->emitted_prims_vec_ptr[stream_id],
1638 mask);
1639 clear_uint_vec_ptr_from_mask(bld_base, bld->emitted_vertices_vec_ptr[stream_id],
1640 mask);
1641 }
1642
1643 static void end_primitive(struct lp_build_nir_context *bld_base, uint32_t stream_id)
1644 {
1645 ASSERTED struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1646
1647 assert(bld->gs_iface->end_primitive);
1648
1649 LLVMValueRef mask = mask_vec(bld_base);
1650 end_primitive_masked(bld_base, mask, stream_id);
1651 }
1652
1653 static void
1654 emit_prologue(struct lp_build_nir_soa_context *bld)
1655 {
1656 struct gallivm_state * gallivm = bld->bld_base.base.gallivm;
1657 if (bld->indirects & nir_var_shader_in && !bld->gs_iface && !bld->tcs_iface && !bld->tes_iface) {
1658 uint32_t num_inputs = util_bitcount64(bld->bld_base.shader->info.inputs_read);
1659 unsigned index, chan;
1660 LLVMTypeRef vec_type = bld->bld_base.base.vec_type;
1661 LLVMValueRef array_size = lp_build_const_int32(gallivm, num_inputs * 4);
1662 bld->inputs_array = lp_build_array_alloca(gallivm,
1663 vec_type, array_size,
1664 "input_array");
1665
1666 for (index = 0; index < num_inputs; ++index) {
1667 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
1668 LLVMValueRef lindex =
1669 lp_build_const_int32(gallivm, index * 4 + chan);
1670 LLVMValueRef input_ptr =
1671 LLVMBuildGEP(gallivm->builder, bld->inputs_array,
1672 &lindex, 1, "");
1673 LLVMValueRef value = bld->inputs[index][chan];
1674 if (value)
1675 LLVMBuildStore(gallivm->builder, value, input_ptr);
1676 }
1677 }
1678 }
1679 }
1680
1681 static void emit_vote(struct lp_build_nir_context *bld_base, LLVMValueRef src, nir_intrinsic_instr *instr, LLVMValueRef result[4])
1682 {
1683 struct gallivm_state * gallivm = bld_base->base.gallivm;
1684 LLVMBuilderRef builder = gallivm->builder;
1685
1686 LLVMValueRef exec_mask = mask_vec(bld_base);
1687 struct lp_build_loop_state loop_state;
1688
1689 LLVMValueRef outer_cond = LLVMBuildICmp(builder, LLVMIntNE, exec_mask, bld_base->uint_bld.zero, "");
1690
1691 LLVMValueRef res_store = lp_build_alloca(gallivm, bld_base->int_bld.elem_type, "");
1692 LLVMValueRef init_val = NULL;
1693 if (instr->intrinsic == nir_intrinsic_vote_ieq) {
1694 /* for equal we unfortunately have to loop and find the first valid one. */
1695 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
1696 LLVMValueRef if_cond = LLVMBuildExtractElement(gallivm->builder, outer_cond, loop_state.counter, "");
1697
1698 struct lp_build_if_state ifthen;
1699 lp_build_if(&ifthen, gallivm, if_cond);
1700 LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, src,
1701 loop_state.counter, "");
1702 LLVMBuildStore(builder, value_ptr, res_store);
1703 lp_build_endif(&ifthen);
1704 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, bld_base->uint_bld.type.length),
1705 NULL, LLVMIntUGE);
1706 init_val = LLVMBuildLoad(builder, res_store, "");
1707 } else {
1708 LLVMBuildStore(builder, lp_build_const_int32(gallivm, instr->intrinsic == nir_intrinsic_vote_any ? 0 : -1), res_store);
1709 }
1710
1711 LLVMValueRef res;
1712 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
1713 LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, src,
1714 loop_state.counter, "");
1715 struct lp_build_if_state ifthen;
1716 LLVMValueRef if_cond;
1717 if_cond = LLVMBuildExtractElement(gallivm->builder, outer_cond, loop_state.counter, "");
1718
1719 lp_build_if(&ifthen, gallivm, if_cond);
1720 res = LLVMBuildLoad(builder, res_store, "");
1721
1722 if (instr->intrinsic == nir_intrinsic_vote_ieq) {
1723 LLVMValueRef tmp = LLVMBuildICmp(builder, LLVMIntEQ, init_val, value_ptr, "");
1724 tmp = LLVMBuildSExt(builder, tmp, bld_base->uint_bld.elem_type, "");
1725 res = LLVMBuildOr(builder, res, tmp, "");
1726 } else if (instr->intrinsic == nir_intrinsic_vote_any)
1727 res = LLVMBuildOr(builder, res, value_ptr, "");
1728 else
1729 res = LLVMBuildAnd(builder, res, value_ptr, "");
1730 LLVMBuildStore(builder, res, res_store);
1731 lp_build_endif(&ifthen);
1732 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, bld_base->uint_bld.type.length),
1733 NULL, LLVMIntUGE);
1734 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, LLVMBuildLoad(builder, res_store, ""));
1735 }
1736
1737 static void
1738 emit_interp_at(struct lp_build_nir_context *bld_base,
1739 unsigned num_components,
1740 nir_variable *var,
1741 bool centroid,
1742 bool sample,
1743 unsigned const_index,
1744 LLVMValueRef indir_index,
1745 LLVMValueRef offsets[2],
1746 LLVMValueRef dst[4])
1747 {
1748 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1749
1750 for (unsigned i = 0; i < num_components; i++) {
1751 dst[i] = bld->fs_iface->interp_fn(bld->fs_iface, &bld_base->base,
1752 const_index + var->data.driver_location, i + var->data.location_frac,
1753 centroid, sample, indir_index, offsets);
1754 }
1755 }
1756
1757 void lp_build_nir_soa(struct gallivm_state *gallivm,
1758 struct nir_shader *shader,
1759 const struct lp_build_tgsi_params *params,
1760 LLVMValueRef (*outputs)[4])
1761 {
1762 struct lp_build_nir_soa_context bld;
1763 struct lp_type type = params->type;
1764 struct lp_type res_type;
1765
1766 assert(type.length <= LP_MAX_VECTOR_LENGTH);
1767 memset(&res_type, 0, sizeof res_type);
1768 res_type.width = type.width;
1769 res_type.length = type.length;
1770 res_type.sign = 1;
1771
1772 /* Setup build context */
1773 memset(&bld, 0, sizeof bld);
1774 lp_build_context_init(&bld.bld_base.base, gallivm, type);
1775 lp_build_context_init(&bld.bld_base.uint_bld, gallivm, lp_uint_type(type));
1776 lp_build_context_init(&bld.bld_base.int_bld, gallivm, lp_int_type(type));
1777 lp_build_context_init(&bld.elem_bld, gallivm, lp_elem_type(type));
1778 lp_build_context_init(&bld.uint_elem_bld, gallivm, lp_elem_type(lp_uint_type(type)));
1779 {
1780 struct lp_type dbl_type;
1781 dbl_type = type;
1782 dbl_type.width *= 2;
1783 lp_build_context_init(&bld.bld_base.dbl_bld, gallivm, dbl_type);
1784 }
1785 {
1786 struct lp_type uint64_type;
1787 uint64_type = lp_uint_type(type);
1788 uint64_type.width *= 2;
1789 lp_build_context_init(&bld.bld_base.uint64_bld, gallivm, uint64_type);
1790 }
1791 {
1792 struct lp_type int64_type;
1793 int64_type = lp_int_type(type);
1794 int64_type.width *= 2;
1795 lp_build_context_init(&bld.bld_base.int64_bld, gallivm, int64_type);
1796 }
1797 {
1798 struct lp_type uint16_type;
1799 uint16_type = lp_uint_type(type);
1800 uint16_type.width /= 2;
1801 lp_build_context_init(&bld.bld_base.uint16_bld, gallivm, uint16_type);
1802 }
1803 {
1804 struct lp_type int16_type;
1805 int16_type = lp_int_type(type);
1806 int16_type.width /= 2;
1807 lp_build_context_init(&bld.bld_base.int16_bld, gallivm, int16_type);
1808 }
1809 {
1810 struct lp_type uint8_type;
1811 uint8_type = lp_uint_type(type);
1812 uint8_type.width /= 4;
1813 lp_build_context_init(&bld.bld_base.uint8_bld, gallivm, uint8_type);
1814 }
1815 {
1816 struct lp_type int8_type;
1817 int8_type = lp_int_type(type);
1818 int8_type.width /= 4;
1819 lp_build_context_init(&bld.bld_base.int8_bld, gallivm, int8_type);
1820 }
1821 bld.bld_base.load_var = emit_load_var;
1822 bld.bld_base.store_var = emit_store_var;
1823 bld.bld_base.load_reg = emit_load_reg;
1824 bld.bld_base.store_reg = emit_store_reg;
1825 bld.bld_base.emit_var_decl = emit_var_decl;
1826 bld.bld_base.load_ubo = emit_load_ubo;
1827 bld.bld_base.load_kernel_arg = emit_load_kernel_arg;
1828 bld.bld_base.load_global = emit_load_global;
1829 bld.bld_base.store_global = emit_store_global;
1830 bld.bld_base.atomic_global = emit_atomic_global;
1831 bld.bld_base.tex = emit_tex;
1832 bld.bld_base.tex_size = emit_tex_size;
1833 bld.bld_base.bgnloop = bgnloop;
1834 bld.bld_base.endloop = endloop;
1835 bld.bld_base.if_cond = if_cond;
1836 bld.bld_base.else_stmt = else_stmt;
1837 bld.bld_base.endif_stmt = endif_stmt;
1838 bld.bld_base.break_stmt = break_stmt;
1839 bld.bld_base.continue_stmt = continue_stmt;
1840 bld.bld_base.sysval_intrin = emit_sysval_intrin;
1841 bld.bld_base.discard = discard;
1842 bld.bld_base.emit_vertex = emit_vertex;
1843 bld.bld_base.end_primitive = end_primitive;
1844 bld.bld_base.load_mem = emit_load_mem;
1845 bld.bld_base.store_mem = emit_store_mem;
1846 bld.bld_base.get_buffer_size = emit_get_buffer_size;
1847 bld.bld_base.atomic_mem = emit_atomic_mem;
1848 bld.bld_base.barrier = emit_barrier;
1849 bld.bld_base.image_op = emit_image_op;
1850 bld.bld_base.image_size = emit_image_size;
1851 bld.bld_base.vote = emit_vote;
1852 bld.bld_base.helper_invocation = emit_helper_invocation;
1853 bld.bld_base.interp_at = emit_interp_at;
1854
1855 bld.mask = params->mask;
1856 bld.inputs = params->inputs;
1857 bld.outputs = outputs;
1858 bld.consts_ptr = params->consts_ptr;
1859 bld.const_sizes_ptr = params->const_sizes_ptr;
1860 bld.ssbo_ptr = params->ssbo_ptr;
1861 bld.ssbo_sizes_ptr = params->ssbo_sizes_ptr;
1862 bld.sampler = params->sampler;
1863 // bld.bld_base.info = params->info;
1864
1865 bld.context_ptr = params->context_ptr;
1866 bld.thread_data_ptr = params->thread_data_ptr;
1867 bld.image = params->image;
1868 bld.shared_ptr = params->shared_ptr;
1869 bld.coro = params->coro;
1870 bld.kernel_args_ptr = params->kernel_args;
1871 bld.indirects = 0;
1872 if (params->info->indirect_files & (1 << TGSI_FILE_INPUT))
1873 bld.indirects |= nir_var_shader_in;
1874
1875 bld.gs_iface = params->gs_iface;
1876 bld.tcs_iface = params->tcs_iface;
1877 bld.tes_iface = params->tes_iface;
1878 bld.fs_iface = params->fs_iface;
1879 if (bld.gs_iface) {
1880 struct lp_build_context *uint_bld = &bld.bld_base.uint_bld;
1881
1882 bld.max_output_vertices_vec = lp_build_const_int_vec(gallivm, bld.bld_base.int_bld.type,
1883 shader->info.gs.vertices_out);
1884 for (int i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++) {
1885 bld.emitted_prims_vec_ptr[i] =
1886 lp_build_alloca(gallivm, uint_bld->vec_type, "emitted_prims_ptr");
1887 bld.emitted_vertices_vec_ptr[i] =
1888 lp_build_alloca(gallivm, uint_bld->vec_type, "emitted_vertices_ptr");
1889 bld.total_emitted_vertices_vec_ptr[i] =
1890 lp_build_alloca(gallivm, uint_bld->vec_type, "total_emitted_vertices_ptr");
1891 }
1892 }
1893 lp_exec_mask_init(&bld.exec_mask, &bld.bld_base.int_bld);
1894
1895 bld.system_values = *params->system_values;
1896
1897 bld.bld_base.shader = shader;
1898
1899 emit_prologue(&bld);
1900 lp_build_nir_llvm(&bld.bld_base, shader);
1901
1902 if (bld.gs_iface) {
1903 LLVMBuilderRef builder = bld.bld_base.base.gallivm->builder;
1904 LLVMValueRef total_emitted_vertices_vec;
1905 LLVMValueRef emitted_prims_vec;
1906
1907 end_primitive_masked(&bld.bld_base, lp_build_mask_value(bld.mask), 0);
1908 for (int i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++) {
1909 total_emitted_vertices_vec =
1910 LLVMBuildLoad(builder, bld.total_emitted_vertices_vec_ptr[i], "");
1911
1912 emitted_prims_vec =
1913 LLVMBuildLoad(builder, bld.emitted_prims_vec_ptr[i], "");
1914 bld.gs_iface->gs_epilogue(bld.gs_iface,
1915 total_emitted_vertices_vec,
1916 emitted_prims_vec, i);
1917 }
1918 }
1919 lp_exec_mask_fini(&bld.exec_mask);
1920 }