3cefcdb66147e52bac53eed7a853d5ae8816ffe6
[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 params->type = bld_base->base.type;
1229 params->context_ptr = bld->context_ptr;
1230 params->thread_data_ptr = bld->thread_data_ptr;
1231 params->exec_mask = mask_vec(bld_base);
1232 bld->image->emit_op(bld->image,
1233 bld->bld_base.base.gallivm,
1234 params);
1235
1236 }
1237
1238 static void emit_image_size(struct lp_build_nir_context *bld_base,
1239 struct lp_sampler_size_query_params *params)
1240 {
1241 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1242
1243 params->int_type = bld_base->int_bld.type;
1244 params->context_ptr = bld->context_ptr;
1245
1246 bld->image->emit_size_query(bld->image,
1247 bld->bld_base.base.gallivm,
1248 params);
1249
1250 }
1251
1252 static void init_var_slots(struct lp_build_nir_context *bld_base,
1253 nir_variable *var, unsigned sc)
1254 {
1255 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1256 unsigned slots = glsl_count_attribute_slots(var->type, false) * 4;
1257
1258 if (!bld->outputs)
1259 return;
1260 for (unsigned comp = sc; comp < slots + sc; comp++) {
1261 unsigned this_loc = var->data.driver_location + (comp / 4);
1262 unsigned this_chan = comp % 4;
1263
1264 if (!bld->outputs[this_loc][this_chan])
1265 bld->outputs[this_loc][this_chan] = lp_build_alloca(bld_base->base.gallivm,
1266 bld_base->base.vec_type, "output");
1267 }
1268 }
1269
1270 static void emit_var_decl(struct lp_build_nir_context *bld_base,
1271 nir_variable *var)
1272 {
1273 unsigned sc = var->data.location_frac;
1274 switch (var->data.mode) {
1275 case nir_var_shader_out: {
1276 if (bld_base->shader->info.stage == MESA_SHADER_FRAGMENT) {
1277 if (var->data.location == FRAG_RESULT_STENCIL)
1278 sc = 1;
1279 else if (var->data.location == FRAG_RESULT_DEPTH)
1280 sc = 2;
1281 }
1282 init_var_slots(bld_base, var, sc);
1283 break;
1284 }
1285 default:
1286 break;
1287 }
1288 }
1289
1290 static void emit_tex(struct lp_build_nir_context *bld_base,
1291 struct lp_sampler_params *params)
1292 {
1293 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1294
1295 params->type = bld_base->base.type;
1296 params->context_ptr = bld->context_ptr;
1297 params->thread_data_ptr = bld->thread_data_ptr;
1298
1299 bld->sampler->emit_tex_sample(bld->sampler,
1300 bld->bld_base.base.gallivm,
1301 params);
1302 }
1303
1304 static void emit_tex_size(struct lp_build_nir_context *bld_base,
1305 struct lp_sampler_size_query_params *params)
1306 {
1307 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1308
1309 params->int_type = bld_base->int_bld.type;
1310 params->context_ptr = bld->context_ptr;
1311
1312 bld->sampler->emit_size_query(bld->sampler,
1313 bld->bld_base.base.gallivm,
1314 params);
1315 }
1316
1317 static void emit_sysval_intrin(struct lp_build_nir_context *bld_base,
1318 nir_intrinsic_instr *instr,
1319 LLVMValueRef result[NIR_MAX_VEC_COMPONENTS])
1320 {
1321 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1322 struct gallivm_state *gallivm = bld_base->base.gallivm;
1323 switch (instr->intrinsic) {
1324 case nir_intrinsic_load_instance_id:
1325 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.instance_id);
1326 break;
1327 case nir_intrinsic_load_base_instance:
1328 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.base_instance);
1329 break;
1330 case nir_intrinsic_load_base_vertex:
1331 result[0] = bld->system_values.basevertex;
1332 break;
1333 case nir_intrinsic_load_vertex_id:
1334 result[0] = bld->system_values.vertex_id;
1335 break;
1336 case nir_intrinsic_load_primitive_id:
1337 result[0] = bld->system_values.prim_id;
1338 break;
1339 case nir_intrinsic_load_work_group_id:
1340 for (unsigned i = 0; i < 3; i++)
1341 result[i] = lp_build_broadcast_scalar(&bld_base->uint_bld, LLVMBuildExtractElement(gallivm->builder, bld->system_values.block_id, lp_build_const_int32(gallivm, i), ""));
1342 break;
1343 case nir_intrinsic_load_local_invocation_id:
1344 for (unsigned i = 0; i < 3; i++)
1345 result[i] = LLVMBuildExtractValue(gallivm->builder, bld->system_values.thread_id, i, "");
1346 break;
1347 case nir_intrinsic_load_num_work_groups:
1348 for (unsigned i = 0; i < 3; i++)
1349 result[i] = lp_build_broadcast_scalar(&bld_base->uint_bld, LLVMBuildExtractElement(gallivm->builder, bld->system_values.grid_size, lp_build_const_int32(gallivm, i), ""));
1350 break;
1351 case nir_intrinsic_load_invocation_id:
1352 if (bld_base->shader->info.stage == MESA_SHADER_TESS_CTRL)
1353 result[0] = bld->system_values.invocation_id;
1354 else
1355 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.invocation_id);
1356 break;
1357 case nir_intrinsic_load_front_face:
1358 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.front_facing);
1359 break;
1360 case nir_intrinsic_load_draw_id:
1361 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.draw_id);
1362 break;
1363 default:
1364 break;
1365 case nir_intrinsic_load_local_group_size:
1366 for (unsigned i = 0; i < 3; i++)
1367 result[i] = lp_build_broadcast_scalar(&bld_base->uint_bld, LLVMBuildExtractElement(gallivm->builder, bld->system_values.block_size, lp_build_const_int32(gallivm, i), ""));
1368 break;
1369 case nir_intrinsic_load_work_dim:
1370 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.work_dim);
1371 break;
1372 case nir_intrinsic_load_tess_coord:
1373 for (unsigned i = 0; i < 3; i++) {
1374 result[i] = LLVMBuildExtractValue(gallivm->builder, bld->system_values.tess_coord, i, "");
1375 }
1376 break;
1377 case nir_intrinsic_load_tess_level_outer:
1378 for (unsigned i = 0; i < 4; i++)
1379 result[i] = lp_build_broadcast_scalar(&bld_base->base, LLVMBuildExtractValue(gallivm->builder, bld->system_values.tess_outer, i, ""));
1380 break;
1381 case nir_intrinsic_load_tess_level_inner:
1382 for (unsigned i = 0; i < 2; i++)
1383 result[i] = lp_build_broadcast_scalar(&bld_base->base, LLVMBuildExtractValue(gallivm->builder, bld->system_values.tess_inner, i, ""));
1384 break;
1385 case nir_intrinsic_load_patch_vertices_in:
1386 result[0] = bld->system_values.vertices_in;
1387 break;
1388 case nir_intrinsic_load_sample_id:
1389 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.sample_id);
1390 break;
1391 case nir_intrinsic_load_sample_pos:
1392 for (unsigned i = 0; i < 2; i++) {
1393 LLVMValueRef idx = LLVMBuildMul(gallivm->builder, bld->system_values.sample_id, lp_build_const_int32(gallivm, 2), "");
1394 idx = LLVMBuildAdd(gallivm->builder, idx, lp_build_const_int32(gallivm, i), "");
1395 LLVMValueRef val = lp_build_array_get(gallivm, bld->system_values.sample_pos, idx);
1396 result[i] = lp_build_broadcast_scalar(&bld_base->base, val);
1397 }
1398 break;
1399 case nir_intrinsic_load_sample_mask_in:
1400 result[0] = bld->system_values.sample_mask_in;
1401 break;
1402 }
1403 }
1404
1405 static void emit_helper_invocation(struct lp_build_nir_context *bld_base,
1406 LLVMValueRef *dst)
1407 {
1408 struct gallivm_state *gallivm = bld_base->base.gallivm;
1409 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1410 *dst = lp_build_cmp(uint_bld, PIPE_FUNC_NOTEQUAL, mask_vec(bld_base), lp_build_const_int_vec(gallivm, uint_bld->type, -1));
1411 }
1412
1413 static void bgnloop(struct lp_build_nir_context *bld_base)
1414 {
1415 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1416 lp_exec_bgnloop(&bld->exec_mask, true);
1417 }
1418
1419 static void endloop(struct lp_build_nir_context *bld_base)
1420 {
1421 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1422 lp_exec_endloop(bld_base->base.gallivm, &bld->exec_mask);
1423 }
1424
1425 static void if_cond(struct lp_build_nir_context *bld_base, LLVMValueRef cond)
1426 {
1427 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1428 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1429 lp_exec_mask_cond_push(&bld->exec_mask, LLVMBuildBitCast(builder, cond, bld_base->base.int_vec_type, ""));
1430 }
1431
1432 static void else_stmt(struct lp_build_nir_context *bld_base)
1433 {
1434 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1435 lp_exec_mask_cond_invert(&bld->exec_mask);
1436 }
1437
1438 static void endif_stmt(struct lp_build_nir_context *bld_base)
1439 {
1440 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1441 lp_exec_mask_cond_pop(&bld->exec_mask);
1442 }
1443
1444 static void break_stmt(struct lp_build_nir_context *bld_base)
1445 {
1446 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1447
1448 lp_exec_break(&bld->exec_mask, NULL, false);
1449 }
1450
1451 static void continue_stmt(struct lp_build_nir_context *bld_base)
1452 {
1453 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1454 lp_exec_continue(&bld->exec_mask);
1455 }
1456
1457 static void discard(struct lp_build_nir_context *bld_base, LLVMValueRef cond)
1458 {
1459 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1460 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1461 LLVMValueRef mask;
1462
1463 if (!cond) {
1464 if (bld->exec_mask.has_mask) {
1465 mask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp");
1466 } else {
1467 mask = LLVMConstNull(bld->bld_base.base.int_vec_type);
1468 }
1469 } else {
1470 mask = LLVMBuildNot(builder, cond, "");
1471 if (bld->exec_mask.has_mask) {
1472 LLVMValueRef invmask;
1473 invmask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp");
1474 mask = LLVMBuildOr(builder, mask, invmask, "");
1475 }
1476 }
1477 lp_build_mask_update(bld->mask, mask);
1478 }
1479
1480 static void
1481 increment_vec_ptr_by_mask(struct lp_build_nir_context * bld_base,
1482 LLVMValueRef ptr,
1483 LLVMValueRef mask)
1484 {
1485 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1486 LLVMValueRef current_vec = LLVMBuildLoad(builder, ptr, "");
1487
1488 current_vec = LLVMBuildSub(builder, current_vec, mask, "");
1489
1490 LLVMBuildStore(builder, current_vec, ptr);
1491 }
1492
1493 static void
1494 clear_uint_vec_ptr_from_mask(struct lp_build_nir_context * bld_base,
1495 LLVMValueRef ptr,
1496 LLVMValueRef mask)
1497 {
1498 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1499 LLVMValueRef current_vec = LLVMBuildLoad(builder, ptr, "");
1500
1501 current_vec = lp_build_select(&bld_base->uint_bld,
1502 mask,
1503 bld_base->uint_bld.zero,
1504 current_vec);
1505
1506 LLVMBuildStore(builder, current_vec, ptr);
1507 }
1508
1509 static LLVMValueRef
1510 clamp_mask_to_max_output_vertices(struct lp_build_nir_soa_context * bld,
1511 LLVMValueRef current_mask_vec,
1512 LLVMValueRef total_emitted_vertices_vec)
1513 {
1514 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1515 struct lp_build_context *int_bld = &bld->bld_base.int_bld;
1516 LLVMValueRef max_mask = lp_build_cmp(int_bld, PIPE_FUNC_LESS,
1517 total_emitted_vertices_vec,
1518 bld->max_output_vertices_vec);
1519
1520 return LLVMBuildAnd(builder, current_mask_vec, max_mask, "");
1521 }
1522
1523 static void emit_vertex(struct lp_build_nir_context *bld_base, uint32_t stream_id)
1524 {
1525 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1526 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1527
1528 assert(bld->gs_iface->emit_vertex);
1529 LLVMValueRef total_emitted_vertices_vec =
1530 LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr[stream_id], "");
1531 LLVMValueRef mask = mask_vec(bld_base);
1532 mask = clamp_mask_to_max_output_vertices(bld, mask,
1533 total_emitted_vertices_vec);
1534 bld->gs_iface->emit_vertex(bld->gs_iface, &bld->bld_base.base,
1535 bld->outputs,
1536 total_emitted_vertices_vec,
1537 lp_build_const_int_vec(bld->bld_base.base.gallivm, bld->bld_base.base.type, stream_id));
1538
1539 increment_vec_ptr_by_mask(bld_base, bld->emitted_vertices_vec_ptr[stream_id],
1540 mask);
1541 increment_vec_ptr_by_mask(bld_base, bld->total_emitted_vertices_vec_ptr[stream_id],
1542 mask);
1543 }
1544
1545 static void
1546 end_primitive_masked(struct lp_build_nir_context * bld_base,
1547 LLVMValueRef mask, uint32_t stream_id)
1548 {
1549 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1550 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1551
1552 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1553 LLVMValueRef emitted_vertices_vec =
1554 LLVMBuildLoad(builder, bld->emitted_vertices_vec_ptr[stream_id], "");
1555 LLVMValueRef emitted_prims_vec =
1556 LLVMBuildLoad(builder, bld->emitted_prims_vec_ptr[stream_id], "");
1557 LLVMValueRef total_emitted_vertices_vec =
1558 LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr[stream_id], "");
1559
1560 LLVMValueRef emitted_mask = lp_build_cmp(uint_bld,
1561 PIPE_FUNC_NOTEQUAL,
1562 emitted_vertices_vec,
1563 uint_bld->zero);
1564 mask = LLVMBuildAnd(builder, mask, emitted_mask, "");
1565 if (stream_id == 0)
1566 bld->gs_iface->end_primitive(bld->gs_iface, &bld->bld_base.base,
1567 total_emitted_vertices_vec,
1568 emitted_vertices_vec, emitted_prims_vec, mask);
1569 increment_vec_ptr_by_mask(bld_base, bld->emitted_prims_vec_ptr[stream_id],
1570 mask);
1571 clear_uint_vec_ptr_from_mask(bld_base, bld->emitted_vertices_vec_ptr[stream_id],
1572 mask);
1573 }
1574
1575 static void end_primitive(struct lp_build_nir_context *bld_base, uint32_t stream_id)
1576 {
1577 ASSERTED struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1578
1579 assert(bld->gs_iface->end_primitive);
1580
1581 LLVMValueRef mask = mask_vec(bld_base);
1582 end_primitive_masked(bld_base, mask, stream_id);
1583 }
1584
1585 static void
1586 emit_prologue(struct lp_build_nir_soa_context *bld)
1587 {
1588 struct gallivm_state * gallivm = bld->bld_base.base.gallivm;
1589 if (bld->indirects & nir_var_shader_in && !bld->gs_iface && !bld->tcs_iface && !bld->tes_iface) {
1590 uint32_t num_inputs = util_bitcount64(bld->bld_base.shader->info.inputs_read);
1591 unsigned index, chan;
1592 LLVMTypeRef vec_type = bld->bld_base.base.vec_type;
1593 LLVMValueRef array_size = lp_build_const_int32(gallivm, num_inputs * 4);
1594 bld->inputs_array = lp_build_array_alloca(gallivm,
1595 vec_type, array_size,
1596 "input_array");
1597
1598 for (index = 0; index < num_inputs; ++index) {
1599 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
1600 LLVMValueRef lindex =
1601 lp_build_const_int32(gallivm, index * 4 + chan);
1602 LLVMValueRef input_ptr =
1603 LLVMBuildGEP(gallivm->builder, bld->inputs_array,
1604 &lindex, 1, "");
1605 LLVMValueRef value = bld->inputs[index][chan];
1606 if (value)
1607 LLVMBuildStore(gallivm->builder, value, input_ptr);
1608 }
1609 }
1610 }
1611 }
1612
1613 static void emit_vote(struct lp_build_nir_context *bld_base, LLVMValueRef src, nir_intrinsic_instr *instr, LLVMValueRef result[4])
1614 {
1615 struct gallivm_state * gallivm = bld_base->base.gallivm;
1616 LLVMBuilderRef builder = gallivm->builder;
1617
1618 LLVMValueRef exec_mask = mask_vec(bld_base);
1619 struct lp_build_loop_state loop_state;
1620
1621 LLVMValueRef outer_cond = LLVMBuildICmp(builder, LLVMIntNE, exec_mask, bld_base->uint_bld.zero, "");
1622
1623 LLVMValueRef res_store = lp_build_alloca(gallivm, bld_base->int_bld.elem_type, "");
1624 LLVMValueRef init_val = NULL;
1625 if (instr->intrinsic == nir_intrinsic_vote_ieq) {
1626 /* for equal we unfortunately have to loop and find the first valid one. */
1627 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
1628 LLVMValueRef if_cond = LLVMBuildExtractElement(gallivm->builder, outer_cond, loop_state.counter, "");
1629
1630 struct lp_build_if_state ifthen;
1631 lp_build_if(&ifthen, gallivm, if_cond);
1632 LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, src,
1633 loop_state.counter, "");
1634 LLVMBuildStore(builder, value_ptr, res_store);
1635 lp_build_endif(&ifthen);
1636 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, bld_base->uint_bld.type.length),
1637 NULL, LLVMIntUGE);
1638 init_val = LLVMBuildLoad(builder, res_store, "");
1639 } else {
1640 LLVMBuildStore(builder, lp_build_const_int32(gallivm, instr->intrinsic == nir_intrinsic_vote_any ? 0 : -1), res_store);
1641 }
1642
1643 LLVMValueRef res;
1644 lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
1645 LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, src,
1646 loop_state.counter, "");
1647 struct lp_build_if_state ifthen;
1648 LLVMValueRef if_cond;
1649 if_cond = LLVMBuildExtractElement(gallivm->builder, outer_cond, loop_state.counter, "");
1650
1651 lp_build_if(&ifthen, gallivm, if_cond);
1652 res = LLVMBuildLoad(builder, res_store, "");
1653
1654 if (instr->intrinsic == nir_intrinsic_vote_ieq) {
1655 LLVMValueRef tmp = LLVMBuildICmp(builder, LLVMIntEQ, init_val, value_ptr, "");
1656 tmp = LLVMBuildSExt(builder, tmp, bld_base->uint_bld.elem_type, "");
1657 res = LLVMBuildOr(builder, res, tmp, "");
1658 } else if (instr->intrinsic == nir_intrinsic_vote_any)
1659 res = LLVMBuildOr(builder, res, value_ptr, "");
1660 else
1661 res = LLVMBuildAnd(builder, res, value_ptr, "");
1662 LLVMBuildStore(builder, res, res_store);
1663 lp_build_endif(&ifthen);
1664 lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, bld_base->uint_bld.type.length),
1665 NULL, LLVMIntUGE);
1666 result[0] = lp_build_broadcast_scalar(&bld_base->uint_bld, LLVMBuildLoad(builder, res_store, ""));
1667 }
1668
1669 static void
1670 emit_interp_at(struct lp_build_nir_context *bld_base,
1671 unsigned num_components,
1672 nir_variable *var,
1673 bool centroid,
1674 bool sample,
1675 unsigned const_index,
1676 LLVMValueRef indir_index,
1677 LLVMValueRef offsets[2],
1678 LLVMValueRef dst[4])
1679 {
1680 struct lp_build_nir_soa_context *bld = (struct lp_build_nir_soa_context *)bld_base;
1681
1682 for (unsigned i = 0; i < num_components; i++) {
1683 dst[i] = bld->fs_iface->interp_fn(bld->fs_iface, &bld_base->base,
1684 const_index + var->data.driver_location, i + var->data.location_frac,
1685 centroid, sample, indir_index, offsets);
1686 }
1687 }
1688
1689 void lp_build_nir_soa(struct gallivm_state *gallivm,
1690 struct nir_shader *shader,
1691 const struct lp_build_tgsi_params *params,
1692 LLVMValueRef (*outputs)[4])
1693 {
1694 struct lp_build_nir_soa_context bld;
1695 struct lp_type type = params->type;
1696 struct lp_type res_type;
1697
1698 assert(type.length <= LP_MAX_VECTOR_LENGTH);
1699 memset(&res_type, 0, sizeof res_type);
1700 res_type.width = type.width;
1701 res_type.length = type.length;
1702 res_type.sign = 1;
1703
1704 /* Setup build context */
1705 memset(&bld, 0, sizeof bld);
1706 lp_build_context_init(&bld.bld_base.base, gallivm, type);
1707 lp_build_context_init(&bld.bld_base.uint_bld, gallivm, lp_uint_type(type));
1708 lp_build_context_init(&bld.bld_base.int_bld, gallivm, lp_int_type(type));
1709 lp_build_context_init(&bld.elem_bld, gallivm, lp_elem_type(type));
1710 lp_build_context_init(&bld.uint_elem_bld, gallivm, lp_elem_type(lp_uint_type(type)));
1711 {
1712 struct lp_type dbl_type;
1713 dbl_type = type;
1714 dbl_type.width *= 2;
1715 lp_build_context_init(&bld.bld_base.dbl_bld, gallivm, dbl_type);
1716 }
1717 {
1718 struct lp_type uint64_type;
1719 uint64_type = lp_uint_type(type);
1720 uint64_type.width *= 2;
1721 lp_build_context_init(&bld.bld_base.uint64_bld, gallivm, uint64_type);
1722 }
1723 {
1724 struct lp_type int64_type;
1725 int64_type = lp_int_type(type);
1726 int64_type.width *= 2;
1727 lp_build_context_init(&bld.bld_base.int64_bld, gallivm, int64_type);
1728 }
1729 {
1730 struct lp_type uint16_type;
1731 uint16_type = lp_uint_type(type);
1732 uint16_type.width /= 2;
1733 lp_build_context_init(&bld.bld_base.uint16_bld, gallivm, uint16_type);
1734 }
1735 {
1736 struct lp_type int16_type;
1737 int16_type = lp_int_type(type);
1738 int16_type.width /= 2;
1739 lp_build_context_init(&bld.bld_base.int16_bld, gallivm, int16_type);
1740 }
1741 {
1742 struct lp_type uint8_type;
1743 uint8_type = lp_uint_type(type);
1744 uint8_type.width /= 4;
1745 lp_build_context_init(&bld.bld_base.uint8_bld, gallivm, uint8_type);
1746 }
1747 {
1748 struct lp_type int8_type;
1749 int8_type = lp_int_type(type);
1750 int8_type.width /= 4;
1751 lp_build_context_init(&bld.bld_base.int8_bld, gallivm, int8_type);
1752 }
1753 bld.bld_base.load_var = emit_load_var;
1754 bld.bld_base.store_var = emit_store_var;
1755 bld.bld_base.load_reg = emit_load_reg;
1756 bld.bld_base.store_reg = emit_store_reg;
1757 bld.bld_base.emit_var_decl = emit_var_decl;
1758 bld.bld_base.load_ubo = emit_load_ubo;
1759 bld.bld_base.load_kernel_arg = emit_load_kernel_arg;
1760 bld.bld_base.load_global = emit_load_global;
1761 bld.bld_base.store_global = emit_store_global;
1762 bld.bld_base.atomic_global = emit_atomic_global;
1763 bld.bld_base.tex = emit_tex;
1764 bld.bld_base.tex_size = emit_tex_size;
1765 bld.bld_base.bgnloop = bgnloop;
1766 bld.bld_base.endloop = endloop;
1767 bld.bld_base.if_cond = if_cond;
1768 bld.bld_base.else_stmt = else_stmt;
1769 bld.bld_base.endif_stmt = endif_stmt;
1770 bld.bld_base.break_stmt = break_stmt;
1771 bld.bld_base.continue_stmt = continue_stmt;
1772 bld.bld_base.sysval_intrin = emit_sysval_intrin;
1773 bld.bld_base.discard = discard;
1774 bld.bld_base.emit_vertex = emit_vertex;
1775 bld.bld_base.end_primitive = end_primitive;
1776 bld.bld_base.load_mem = emit_load_mem;
1777 bld.bld_base.store_mem = emit_store_mem;
1778 bld.bld_base.get_buffer_size = emit_get_buffer_size;
1779 bld.bld_base.atomic_mem = emit_atomic_mem;
1780 bld.bld_base.barrier = emit_barrier;
1781 bld.bld_base.image_op = emit_image_op;
1782 bld.bld_base.image_size = emit_image_size;
1783 bld.bld_base.vote = emit_vote;
1784 bld.bld_base.helper_invocation = emit_helper_invocation;
1785 bld.bld_base.interp_at = emit_interp_at;
1786
1787 bld.mask = params->mask;
1788 bld.inputs = params->inputs;
1789 bld.outputs = outputs;
1790 bld.consts_ptr = params->consts_ptr;
1791 bld.const_sizes_ptr = params->const_sizes_ptr;
1792 bld.ssbo_ptr = params->ssbo_ptr;
1793 bld.ssbo_sizes_ptr = params->ssbo_sizes_ptr;
1794 bld.sampler = params->sampler;
1795 // bld.bld_base.info = params->info;
1796
1797 bld.context_ptr = params->context_ptr;
1798 bld.thread_data_ptr = params->thread_data_ptr;
1799 bld.image = params->image;
1800 bld.shared_ptr = params->shared_ptr;
1801 bld.coro = params->coro;
1802 bld.kernel_args_ptr = params->kernel_args;
1803 bld.indirects = 0;
1804 if (params->info->indirect_files & (1 << TGSI_FILE_INPUT))
1805 bld.indirects |= nir_var_shader_in;
1806
1807 bld.gs_iface = params->gs_iface;
1808 bld.tcs_iface = params->tcs_iface;
1809 bld.tes_iface = params->tes_iface;
1810 bld.fs_iface = params->fs_iface;
1811 if (bld.gs_iface) {
1812 struct lp_build_context *uint_bld = &bld.bld_base.uint_bld;
1813
1814 bld.max_output_vertices_vec = lp_build_const_int_vec(gallivm, bld.bld_base.int_bld.type,
1815 shader->info.gs.vertices_out);
1816 for (int i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++) {
1817 bld.emitted_prims_vec_ptr[i] =
1818 lp_build_alloca(gallivm, uint_bld->vec_type, "emitted_prims_ptr");
1819 bld.emitted_vertices_vec_ptr[i] =
1820 lp_build_alloca(gallivm, uint_bld->vec_type, "emitted_vertices_ptr");
1821 bld.total_emitted_vertices_vec_ptr[i] =
1822 lp_build_alloca(gallivm, uint_bld->vec_type, "total_emitted_vertices_ptr");
1823 }
1824 }
1825 lp_exec_mask_init(&bld.exec_mask, &bld.bld_base.int_bld);
1826
1827 bld.system_values = *params->system_values;
1828
1829 bld.bld_base.shader = shader;
1830
1831 emit_prologue(&bld);
1832 lp_build_nir_llvm(&bld.bld_base, shader);
1833
1834 if (bld.gs_iface) {
1835 LLVMBuilderRef builder = bld.bld_base.base.gallivm->builder;
1836 LLVMValueRef total_emitted_vertices_vec;
1837 LLVMValueRef emitted_prims_vec;
1838
1839 end_primitive_masked(&bld.bld_base, lp_build_mask_value(bld.mask), 0);
1840 for (int i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++) {
1841 total_emitted_vertices_vec =
1842 LLVMBuildLoad(builder, bld.total_emitted_vertices_vec_ptr[i], "");
1843
1844 emitted_prims_vec =
1845 LLVMBuildLoad(builder, bld.emitted_prims_vec_ptr[i], "");
1846 bld.gs_iface->gs_epilogue(bld.gs_iface,
1847 total_emitted_vertices_vec,
1848 emitted_prims_vec, i);
1849 }
1850 }
1851 lp_exec_mask_fini(&bld.exec_mask);
1852 }