1 /**************************************************************************
3 * Copyright 2019 Red Hat.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **************************************************************************/
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"
39 * combine the execution mask if there is one with the current mask.
42 mask_vec(struct lp_build_nir_context
*bld_base
)
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
) {
52 return exec_mask
->exec_mask
;
53 return LLVMBuildAnd(builder
, lp_build_mask_value(bld
->mask
),
54 exec_mask
->exec_mask
, "");
59 struct lp_build_nir_context
* bld_base
,
63 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
64 LLVMBuilderRef builder
= gallivm
->builder
;
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)));
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
);
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);
80 res
= LLVMBuildShuffleVector(builder
, input
, input2
, LLVMConstVector(shuffles
, len
), "");
82 return LLVMBuildBitCast(builder
, res
, bld_base
->dbl_bld
.vec_type
, "");
86 emit_store_64bit_split(struct lp_build_nir_context
*bld_base
,
88 LLVMValueRef split_values
[2])
90 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
91 LLVMBuilderRef builder
= gallivm
->builder
;
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;
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);
103 shuffles
[i
] = lp_build_const_int32(gallivm
, i
* 2 + 1);
104 shuffles2
[i
] = lp_build_const_int32(gallivm
, i
* 2);
108 split_values
[0] = LLVMBuildShuffleVector(builder
, value
,
109 LLVMGetUndef(LLVMTypeOf(value
)),
110 LLVMConstVector(shuffles
,
111 bld_base
->base
.type
.length
),
113 split_values
[1] = LLVMBuildShuffleVector(builder
, value
,
114 LLVMGetUndef(LLVMTypeOf(value
)),
115 LLVMConstVector(shuffles2
,
116 bld_base
->base
.type
.length
),
121 emit_store_64bit_chan(struct lp_build_nir_context
*bld_base
,
122 LLVMValueRef chan_ptr
,
123 LLVMValueRef chan_ptr2
,
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];
130 emit_store_64bit_split(bld_base
, value
, split_vals
);
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
);
137 get_soa_array_offsets(struct lp_build_context
*uint_bld
,
138 LLVMValueRef indirect_index
,
141 bool need_perelement_offset
)
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
;
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
);
155 if (need_perelement_offset
) {
156 LLVMValueRef pixel_offsets
;
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
,
165 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
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
)
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
;
185 res
= LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), bld_base
->base
.type
.length
* 2));
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.
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)
207 indexes
= lp_build_select(uint_bld
, overflow_mask
, uint_bld
->zero
, indexes
);
209 indexes2
= lp_build_select(uint_bld
, overflow_mask
, uint_bld
->zero
, indexes2
);
213 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
215 for (i
= 0; i
< bld
->type
.length
* (indexes2
? 2 : 1); i
++) {
218 LLVMValueRef scalar_ptr
, scalar
;
220 di
= lp_build_const_int32(gallivm
, i
);
222 si
= lp_build_const_int32(gallivm
, i
>> 1);
226 if (indexes2
&& (i
& 1)) {
227 index
= LLVMBuildExtractElement(builder
,
230 index
= LLVMBuildExtractElement(builder
,
233 scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
234 &index
, 1, "gather_ptr");
235 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
237 res
= LLVMBuildInsertElement(builder
, res
, scalar
, di
, "");
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
);
248 res
= lp_build_select(bld
, overflow_mask
, bld
->zero
, res
);
255 * Scatter/store vector.
258 emit_mask_scatter(struct lp_build_nir_soa_context
*bld
,
259 LLVMValueRef base_ptr
,
260 LLVMValueRef indexes
,
262 struct lp_exec_mask
*mask
)
264 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
265 LLVMBuilderRef builder
= gallivm
->builder
;
267 LLVMValueRef pred
= mask
->has_mask
? mask
->exec_mask
: NULL
;
270 * Loop over elements of index_vec, store scalar value.
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
;
281 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
282 ii
, val
, index
, scalar_ptr
);
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
);
291 LLVMBuildStore(builder
, val
, scalar_ptr
);
296 static void emit_load_var(struct lp_build_nir_context
*bld_base
,
297 nir_variable_mode deref_mode
,
298 unsigned num_components
,
301 unsigned vertex_index
,
302 LLVMValueRef indir_vertex_index
,
303 unsigned const_index
,
304 LLVMValueRef indir_index
,
305 LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
])
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
;
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
);
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
;
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
));
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
);
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
);
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
);
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
));
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
);
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
,
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
, "");
393 index_vec2
= get_soa_array_offsets(&bld_base
->uint_bld
,
394 indir_index
, 4, idx
+ 1, TRUE
);
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
);
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
);
411 result
[i
] = input_ptr
;
414 if (bit_size
== 64) {
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]);
420 result
[i
] = bld
->inputs
[var
->data
.driver_location
+ const_index
][idx
];
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
);
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
));
438 attrib_index_val
= lp_build_const_int32(gallivm
, const_index
+ var
->data
.driver_location
);
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
);
458 static void emit_store_chan(struct lp_build_nir_context
*bld_base
,
459 nir_variable_mode deref_mode
,
461 unsigned location
, unsigned comp
,
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
;
469 if (bit_size
== 64) {
476 emit_store_64bit_chan(bld_base
, bld
->outputs
[location
][chan
],
477 bld
->outputs
[location
][chan
+ 1], dst
);
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
]);
485 static void emit_store_tcs_chan(struct lp_build_nir_context
*bld_base
,
488 unsigned const_index
,
489 LLVMValueRef indir_vertex_index
,
490 LLVMValueRef indir_index
,
493 LLVMValueRef chan_val
)
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) {
508 LLVMValueRef attrib_index_val
;
509 LLVMValueRef swizzle_index_val
= lp_build_const_int32(gallivm
, swizzle
);
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
));
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,
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,
528 indir_index
? true : false,
529 attrib_index_val
, swizzle_index_val2
,
530 split_vals
[1], mask_vec(bld_base
));
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,
536 indir_index
? true : false,
537 attrib_index_val
, swizzle_index_val
,
538 chan_val
, mask_vec(bld_base
));
542 static void emit_store_var(struct lp_build_nir_context
*bld_base
,
543 nir_variable_mode deref_mode
,
544 unsigned num_components
,
548 LLVMValueRef indir_vertex_index
,
549 unsigned const_index
,
550 LLVMValueRef indir_index
,
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
)
562 else if (var
->data
.location
== FRAG_RESULT_DEPTH
)
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
);
572 emit_store_chan(bld_base
, deref_mode
, bit_size
, location
+ const_index
, comp
, chan
, chan_val
);
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
)
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
);
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
);
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
);
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
, "");
612 return nc
== 1 ? vals
[0] : lp_nir_array_build_gather_values(builder
, vals
, nc
);
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
,
619 LLVMValueRef indir_src
,
620 LLVMValueRef reg_storage
,
621 LLVMValueRef dst
[NIR_MAX_VEC_COMPONENTS
])
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
);
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
);
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
)))
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
);
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
);
654 static void emit_load_kernel_arg(struct lp_build_nir_context
*bld_base
,
657 unsigned offset_bit_size
,
658 bool offset_is_uniform
,
660 LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
])
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
);
671 else if (bit_size
== 32)
673 else if (bit_size
== 64)
676 offset
= lp_build_shr(bld_offset
, offset
, lp_build_const_int_vec(gallivm
, bld_offset
->type
, size_shift
));
678 LLVMTypeRef ptr_type
= LLVMPointerType(bld_broad
->elem_type
, 0);
679 kernel_args_ptr
= LLVMBuildBitCast(builder
, kernel_args_ptr
, ptr_type
, "");
681 if (offset_is_uniform
) {
682 offset
= LLVMBuildExtractElement(builder
, offset
, lp_build_const_int32(gallivm
, 0), "");
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
), "");
687 LLVMValueRef scalar
= lp_build_pointer_get(builder
, kernel_args_ptr
, this_offset
);
688 result
[c
] = lp_build_broadcast_scalar(bld_broad
, scalar
);
693 static LLVMValueRef
global_addr_to_ptr(struct gallivm_state
*gallivm
, LLVMValueRef addr_ptr
, unsigned bit_size
)
695 LLVMBuilderRef builder
= gallivm
->builder
;
698 addr_ptr
= LLVMBuildIntToPtr(builder
, addr_ptr
, LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0), "");
701 addr_ptr
= LLVMBuildIntToPtr(builder
, addr_ptr
, LLVMPointerType(LLVMInt16TypeInContext(gallivm
->context
), 0), "");
705 addr_ptr
= LLVMBuildIntToPtr(builder
, addr_ptr
, LLVMPointerType(LLVMInt32TypeInContext(gallivm
->context
), 0), "");
708 addr_ptr
= LLVMBuildIntToPtr(builder
, addr_ptr
, LLVMPointerType(LLVMInt64TypeInContext(gallivm
->context
), 0), "");
714 static void emit_load_global(struct lp_build_nir_context
*bld_base
,
717 unsigned addr_bit_size
,
719 LLVMValueRef outval
[NIR_MAX_VEC_COMPONENTS
])
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
;
726 res_bld
= get_int_bld(bld_base
, true, bit_size
);
728 for (unsigned c
= 0; c
< nc
; c
++) {
729 LLVMValueRef result
= lp_build_alloca(gallivm
, res_bld
->vec_type
, "");
731 struct lp_build_loop_state loop_state
;
732 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
734 LLVMValueRef addr_ptr
= LLVMBuildExtractElement(gallivm
->builder
, addr
,
735 loop_state
.counter
, "");
736 addr_ptr
= global_addr_to_ptr(gallivm
, addr_ptr
, bit_size
);
738 LLVMValueRef value_ptr
= lp_build_pointer_get(builder
, addr_ptr
, lp_build_const_int32(gallivm
, c
));
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
),
746 outval
[c
] = LLVMBuildLoad(builder
, result
, "");
750 static void emit_store_global(struct lp_build_nir_context
*bld_base
,
752 unsigned nc
, unsigned bit_size
,
753 unsigned addr_bit_size
,
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
;
761 for (unsigned c
= 0; c
< nc
; c
++) {
762 if (!(writemask
& (1u << c
)))
764 LLVMValueRef val
= (nc
== 1) ? dst
: LLVMBuildExtractValue(builder
, dst
, c
, "");
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
, "");
772 LLVMValueRef addr_ptr
= LLVMBuildExtractElement(gallivm
->builder
, addr
,
773 loop_state
.counter
, "");
774 addr_ptr
= global_addr_to_ptr(gallivm
, addr_ptr
, bit_size
);
777 value_ptr
= LLVMBuildBitCast(builder
, value_ptr
, LLVMInt32TypeInContext(gallivm
->context
), "");
780 value_ptr
= LLVMBuildBitCast(builder
, value_ptr
, LLVMInt64TypeInContext(gallivm
->context
), "");
785 struct lp_build_if_state ifthen
;
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
),
797 static void emit_atomic_global(struct lp_build_nir_context
*bld_base
,
798 nir_intrinsic_op nir_op
,
799 unsigned addr_bit_size
,
801 LLVMValueRef val
, LLVMValueRef val2
,
802 LLVMValueRef
*result
)
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
;
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));
814 LLVMValueRef value_ptr
= LLVMBuildExtractElement(gallivm
->builder
, val
,
815 loop_state
.counter
, "");
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
;
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
);
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
,
833 LLVMAtomicOrderingSequentiallyConsistent
,
834 LLVMAtomicOrderingSequentiallyConsistent
,
836 scalar
= LLVMBuildExtractValue(gallivm
->builder
, scalar
, 0, "");
838 LLVMAtomicRMWBinOp op
;
840 case nir_intrinsic_global_atomic_add
:
841 op
= LLVMAtomicRMWBinOpAdd
;
843 case nir_intrinsic_global_atomic_exchange
:
844 op
= LLVMAtomicRMWBinOpXchg
;
846 case nir_intrinsic_global_atomic_and
:
847 op
= LLVMAtomicRMWBinOpAnd
;
849 case nir_intrinsic_global_atomic_or
:
850 op
= LLVMAtomicRMWBinOpOr
;
852 case nir_intrinsic_global_atomic_xor
:
853 op
= LLVMAtomicRMWBinOpXor
;
855 case nir_intrinsic_global_atomic_umin
:
856 op
= LLVMAtomicRMWBinOpUMin
;
858 case nir_intrinsic_global_atomic_umax
:
859 op
= LLVMAtomicRMWBinOpUMax
;
861 case nir_intrinsic_global_atomic_imin
:
862 op
= LLVMAtomicRMWBinOpMin
;
864 case nir_intrinsic_global_atomic_imax
:
865 op
= LLVMAtomicRMWBinOpMax
;
868 unreachable("unknown atomic op");
871 scalar
= LLVMBuildAtomicRMW(builder
, op
,
873 LLVMAtomicOrderingSequentiallyConsistent
,
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
),
886 *result
= LLVMBuildLoad(builder
, atom_res
, "");
889 static void emit_load_ubo(struct lp_build_nir_context
*bld_base
,
892 bool offset_is_uniform
,
895 LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
])
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;
906 else if (bit_size
== 64)
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
, "");
915 if (offset_is_uniform
) {
916 offset
= LLVMBuildExtractElement(builder
, offset
, lp_build_const_int32(gallivm
, 0), "");
918 for (unsigned c
= 0; c
< nc
; c
++) {
919 LLVMValueRef this_offset
= LLVMBuildAdd(builder
, offset
, lp_build_const_int32(gallivm
, c
), "");
921 LLVMValueRef scalar
= lp_build_pointer_get(builder
, consts_ptr
, this_offset
);
922 result
[c
] = lp_build_broadcast_scalar(bld_broad
, scalar
);
925 LLVMValueRef overflow_mask
;
926 LLVMValueRef num_consts
= lp_build_array_get(gallivm
, bld
->const_sizes_ptr
, index
);
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
);
935 result
[c
] = build_gather(bld_base
, bld_broad
, consts_ptr
, this_offset
, overflow_mask
, NULL
);
941 static void emit_load_mem(struct lp_build_nir_context
*bld_base
,
946 LLVMValueRef outval
[NIR_MAX_VEC_COMPONENTS
])
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
;
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
);
961 ssbo_ptr
= lp_build_array_get(gallivm
, bld
->ssbo_ptr
, LLVMBuildExtractElement(builder
, index
, lp_build_const_int32(gallivm
, 0), ""));
963 ssbo_ptr
= bld
->shared_ptr
;
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
);
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
, "");
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));
979 struct lp_build_if_state ifthen
;
980 LLVMValueRef cond
, temp_res
;
982 loop_index
= LLVMBuildExtractElement(gallivm
->builder
, loop_index
,
983 loop_state
.counter
, "");
985 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, exec_mask
, uint_bld
->zero
, "");
986 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, loop_state
.counter
, "");
988 lp_build_if(&ifthen
, gallivm
, cond
);
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
);
994 scalar
= lp_build_pointer_get(builder
, ssbo_ptr
, loop_index
);
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
, "");
1003 zero
= LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
), 0, 0);
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
),
1011 outval
[c
] = LLVMBuildLoad(gallivm
->builder
, result
, "");
1015 static void emit_store_mem(struct lp_build_nir_context
*bld_base
,
1020 LLVMValueRef offset
,
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
;
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), ""));
1036 ssbo_ptr
= bld
->shared_ptr
;
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
)))
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
, "");
1045 LLVMValueRef exec_mask
= mask_vec(bld_base
);
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
, "");
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
, "");
1056 value_ptr
= LLVMBuildBitCast(gallivm
->builder
, value_ptr
, bld_base
->uint64_bld
.elem_type
, "");
1058 value_ptr
= LLVMBuildBitCast(gallivm
->builder
, value_ptr
, uint_bld
->elem_type
, "");
1059 struct lp_build_if_state ifthen
;
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
);
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
),
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
)
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
;
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), ""));
1097 ssbo_ptr
= bld
->shared_ptr
;
1099 offset
= lp_build_shr_imm(uint_bld
, offset
, 2);
1100 LLVMValueRef atom_res
= lp_build_alloca(gallivm
,
1101 uint_bld
->vec_type
, "");
1103 LLVMValueRef exec_mask
= mask_vec(bld_base
);
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
, "");
1109 struct lp_build_loop_state loop_state
;
1110 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
1112 LLVMValueRef value_ptr
= LLVMBuildExtractElement(gallivm
->builder
, val
,
1113 loop_state
.counter
, "");
1114 value_ptr
= LLVMBuildBitCast(gallivm
->builder
, value_ptr
, uint_bld
->elem_type
, "");
1116 offset
= LLVMBuildExtractElement(gallivm
->builder
, offset
,
1117 loop_state
.counter
, "");
1119 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, ssbo_ptr
,
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
);
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
,
1135 LLVMAtomicOrderingSequentiallyConsistent
,
1136 LLVMAtomicOrderingSequentiallyConsistent
,
1138 scalar
= LLVMBuildExtractValue(gallivm
->builder
, scalar
, 0, "");
1140 LLVMAtomicRMWBinOp op
;
1143 case nir_intrinsic_shared_atomic_add
:
1144 case nir_intrinsic_ssbo_atomic_add
:
1145 op
= LLVMAtomicRMWBinOpAdd
;
1147 case nir_intrinsic_shared_atomic_exchange
:
1148 case nir_intrinsic_ssbo_atomic_exchange
:
1149 op
= LLVMAtomicRMWBinOpXchg
;
1151 case nir_intrinsic_shared_atomic_and
:
1152 case nir_intrinsic_ssbo_atomic_and
:
1153 op
= LLVMAtomicRMWBinOpAnd
;
1155 case nir_intrinsic_shared_atomic_or
:
1156 case nir_intrinsic_ssbo_atomic_or
:
1157 op
= LLVMAtomicRMWBinOpOr
;
1159 case nir_intrinsic_shared_atomic_xor
:
1160 case nir_intrinsic_ssbo_atomic_xor
:
1161 op
= LLVMAtomicRMWBinOpXor
;
1163 case nir_intrinsic_shared_atomic_umin
:
1164 case nir_intrinsic_ssbo_atomic_umin
:
1165 op
= LLVMAtomicRMWBinOpUMin
;
1167 case nir_intrinsic_shared_atomic_umax
:
1168 case nir_intrinsic_ssbo_atomic_umax
:
1169 op
= LLVMAtomicRMWBinOpUMax
;
1171 case nir_intrinsic_ssbo_atomic_imin
:
1172 case nir_intrinsic_shared_atomic_imin
:
1173 op
= LLVMAtomicRMWBinOpMin
;
1175 case nir_intrinsic_ssbo_atomic_imax
:
1176 case nir_intrinsic_shared_atomic_imax
:
1177 op
= LLVMAtomicRMWBinOpMax
;
1180 unreachable("unknown atomic op");
1182 scalar
= LLVMBuildAtomicRMW(builder
, op
,
1183 scalar_ptr
, value_ptr
,
1184 LLVMAtomicOrderingSequentiallyConsistent
,
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
);
1196 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, uint_bld
->type
.length
),
1198 *result
= LLVMBuildLoad(builder
, atom_res
, "");
1201 static void emit_barrier(struct lp_build_nir_context
*bld_base
)
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
;
1206 LLVMBasicBlockRef resume
= lp_build_insert_new_block(gallivm
, "resume");
1208 lp_build_coro_suspend_switch(gallivm
, bld
->coro
, resume
, false);
1209 LLVMPositionBuilderAtEnd(gallivm
->builder
, resume
);
1212 static LLVMValueRef
emit_get_buffer_size(struct lp_build_nir_context
*bld_base
,
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
);
1224 static void emit_image_op(struct lp_build_nir_context
*bld_base
,
1225 struct lp_img_params
*params
)
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
;
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
);
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), "");
1239 bld
->image
->emit_op(bld
->image
,
1240 bld
->bld_base
.base
.gallivm
,
1245 static void emit_image_size(struct lp_build_nir_context
*bld_base
,
1246 struct lp_sampler_size_query_params
*params
)
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
;
1251 params
->int_type
= bld_base
->int_bld
.type
;
1252 params
->context_ptr
= bld
->context_ptr
;
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
,
1263 static void init_var_slots(struct lp_build_nir_context
*bld_base
,
1264 nir_variable
*var
, unsigned sc
)
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;
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;
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");
1281 static void emit_var_decl(struct lp_build_nir_context
*bld_base
,
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
)
1290 else if (var
->data
.location
== FRAG_RESULT_DEPTH
)
1293 init_var_slots(bld_base
, var
, sc
);
1301 static void emit_tex(struct lp_build_nir_context
*bld_base
,
1302 struct lp_sampler_params
*params
)
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
;
1307 params
->type
= bld_base
->base
.type
;
1308 params
->context_ptr
= bld
->context_ptr
;
1309 params
->thread_data_ptr
= bld
->thread_data_ptr
;
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
;
1322 orig_texel_ptr
= params
->texel
;
1324 for (i
= 0; i
< 5; i
++) {
1325 coords
[i
] = params
->coords
[i
];
1327 orig_offset
= params
->texture_index_offset
;
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
, "");
1336 params
->coords
= new_coords
;
1337 params
->texture_index_offset
= LLVMBuildExtractElement(gallivm
->builder
,
1340 params
->type
= lp_elem_type(bld_base
->base
.type
);
1342 params
->texel
= texel
;
1343 bld
->sampler
->emit_tex_sample(bld
->sampler
,
1347 for (i
= 0; i
< 4; i
++) {
1348 result
[i
] = LLVMBuildInsertElement(gallivm
->builder
, result
[i
], texel
[i
], idx
, "");
1351 for (i
= 0; i
< 4; i
++) {
1352 orig_texel_ptr
[i
] = result
[i
];
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), "");
1362 params
->type
= bld_base
->base
.type
;
1363 bld
->sampler
->emit_tex_sample(bld
->sampler
,
1364 bld
->bld_base
.base
.gallivm
,
1368 static void emit_tex_size(struct lp_build_nir_context
*bld_base
,
1369 struct lp_sampler_size_query_params
*params
)
1371 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1373 params
->int_type
= bld_base
->int_bld
.type
;
1374 params
->context_ptr
= bld
->context_ptr
;
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
,
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
])
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
);
1395 case nir_intrinsic_load_base_instance
:
1396 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.base_instance
);
1398 case nir_intrinsic_load_base_vertex
:
1399 result
[0] = bld
->system_values
.basevertex
;
1401 case nir_intrinsic_load_vertex_id
:
1402 result
[0] = bld
->system_values
.vertex_id
;
1404 case nir_intrinsic_load_primitive_id
:
1405 result
[0] = bld
->system_values
.prim_id
;
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
), ""));
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
, "");
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
), ""));
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
;
1423 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.invocation_id
);
1425 case nir_intrinsic_load_front_face
:
1426 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.front_facing
);
1428 case nir_intrinsic_load_draw_id
:
1429 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.draw_id
);
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
), ""));
1437 case nir_intrinsic_load_work_dim
:
1438 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.work_dim
);
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
, "");
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
, ""));
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
, ""));
1453 case nir_intrinsic_load_patch_vertices_in
:
1454 result
[0] = bld
->system_values
.vertices_in
;
1456 case nir_intrinsic_load_sample_id
:
1457 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.sample_id
);
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
);
1467 case nir_intrinsic_load_sample_mask_in
:
1468 result
[0] = bld
->system_values
.sample_mask_in
;
1473 static void emit_helper_invocation(struct lp_build_nir_context
*bld_base
,
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));
1481 static void bgnloop(struct lp_build_nir_context
*bld_base
)
1483 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1484 lp_exec_bgnloop(&bld
->exec_mask
, true);
1487 static void endloop(struct lp_build_nir_context
*bld_base
)
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
);
1493 static void if_cond(struct lp_build_nir_context
*bld_base
, LLVMValueRef cond
)
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
, ""));
1500 static void else_stmt(struct lp_build_nir_context
*bld_base
)
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
);
1506 static void endif_stmt(struct lp_build_nir_context
*bld_base
)
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
);
1512 static void break_stmt(struct lp_build_nir_context
*bld_base
)
1514 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1516 lp_exec_break(&bld
->exec_mask
, NULL
, false);
1519 static void continue_stmt(struct lp_build_nir_context
*bld_base
)
1521 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1522 lp_exec_continue(&bld
->exec_mask
);
1525 static void discard(struct lp_build_nir_context
*bld_base
, LLVMValueRef cond
)
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
;
1532 if (bld
->exec_mask
.has_mask
) {
1533 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1535 mask
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
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
, "");
1545 lp_build_mask_update(bld
->mask
, mask
);
1549 increment_vec_ptr_by_mask(struct lp_build_nir_context
* bld_base
,
1553 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1554 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
1556 current_vec
= LLVMBuildSub(builder
, current_vec
, mask
, "");
1558 LLVMBuildStore(builder
, current_vec
, ptr
);
1562 clear_uint_vec_ptr_from_mask(struct lp_build_nir_context
* bld_base
,
1566 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1567 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
1569 current_vec
= lp_build_select(&bld_base
->uint_bld
,
1571 bld_base
->uint_bld
.zero
,
1574 LLVMBuildStore(builder
, current_vec
, ptr
);
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
)
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
);
1588 return LLVMBuildAnd(builder
, current_mask_vec
, max_mask
, "");
1591 static void emit_vertex(struct lp_build_nir_context
*bld_base
, uint32_t stream_id
)
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
;
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
,
1604 total_emitted_vertices_vec
,
1605 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, bld
->bld_base
.base
.type
, stream_id
));
1607 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_vertices_vec_ptr
[stream_id
],
1609 increment_vec_ptr_by_mask(bld_base
, bld
->total_emitted_vertices_vec_ptr
[stream_id
],
1614 end_primitive_masked(struct lp_build_nir_context
* bld_base
,
1615 LLVMValueRef mask
, uint32_t stream_id
)
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
;
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
], "");
1628 LLVMValueRef emitted_mask
= lp_build_cmp(uint_bld
,
1630 emitted_vertices_vec
,
1632 mask
= LLVMBuildAnd(builder
, mask
, emitted_mask
, "");
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
],
1639 clear_uint_vec_ptr_from_mask(bld_base
, bld
->emitted_vertices_vec_ptr
[stream_id
],
1643 static void end_primitive(struct lp_build_nir_context
*bld_base
, uint32_t stream_id
)
1645 ASSERTED
struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1647 assert(bld
->gs_iface
->end_primitive
);
1649 LLVMValueRef mask
= mask_vec(bld_base
);
1650 end_primitive_masked(bld_base
, mask
, stream_id
);
1654 emit_prologue(struct lp_build_nir_soa_context
*bld
)
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
,
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
,
1673 LLVMValueRef value
= bld
->inputs
[index
][chan
];
1675 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
1681 static void emit_vote(struct lp_build_nir_context
*bld_base
, LLVMValueRef src
, nir_intrinsic_instr
*instr
, LLVMValueRef result
[4])
1683 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1684 LLVMBuilderRef builder
= gallivm
->builder
;
1686 LLVMValueRef exec_mask
= mask_vec(bld_base
);
1687 struct lp_build_loop_state loop_state
;
1689 LLVMValueRef outer_cond
= LLVMBuildICmp(builder
, LLVMIntNE
, exec_mask
, bld_base
->uint_bld
.zero
, "");
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
, "");
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
),
1706 init_val
= LLVMBuildLoad(builder
, res_store
, "");
1708 LLVMBuildStore(builder
, lp_build_const_int32(gallivm
, instr
->intrinsic
== nir_intrinsic_vote_any
? 0 : -1), res_store
);
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
, "");
1719 lp_build_if(&ifthen
, gallivm
, if_cond
);
1720 res
= LLVMBuildLoad(builder
, res_store
, "");
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
, "");
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
),
1734 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, LLVMBuildLoad(builder
, res_store
, ""));
1738 emit_interp_at(struct lp_build_nir_context
*bld_base
,
1739 unsigned num_components
,
1743 unsigned const_index
,
1744 LLVMValueRef indir_index
,
1745 LLVMValueRef offsets
[2],
1746 LLVMValueRef dst
[4])
1748 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
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
);
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])
1762 struct lp_build_nir_soa_context bld
;
1763 struct lp_type type
= params
->type
;
1764 struct lp_type res_type
;
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
;
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
)));
1780 struct lp_type dbl_type
;
1782 dbl_type
.width
*= 2;
1783 lp_build_context_init(&bld
.bld_base
.dbl_bld
, gallivm
, dbl_type
);
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
);
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
);
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
);
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
);
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
);
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
);
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
;
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;
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
;
1872 if (params
->info
->indirect_files
& (1 << TGSI_FILE_INPUT
))
1873 bld
.indirects
|= nir_var_shader_in
;
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
;
1880 struct lp_build_context
*uint_bld
= &bld
.bld_base
.uint_bld
;
1882 bld
.gs_vertex_streams
= params
->gs_vertex_streams
;
1883 bld
.max_output_vertices_vec
= lp_build_const_int_vec(gallivm
, bld
.bld_base
.int_bld
.type
,
1884 shader
->info
.gs
.vertices_out
);
1885 for (int i
= 0; i
< params
->gs_vertex_streams
; i
++) {
1886 bld
.emitted_prims_vec_ptr
[i
] =
1887 lp_build_alloca(gallivm
, uint_bld
->vec_type
, "emitted_prims_ptr");
1888 bld
.emitted_vertices_vec_ptr
[i
] =
1889 lp_build_alloca(gallivm
, uint_bld
->vec_type
, "emitted_vertices_ptr");
1890 bld
.total_emitted_vertices_vec_ptr
[i
] =
1891 lp_build_alloca(gallivm
, uint_bld
->vec_type
, "total_emitted_vertices_ptr");
1894 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.int_bld
);
1896 bld
.system_values
= *params
->system_values
;
1898 bld
.bld_base
.shader
= shader
;
1900 emit_prologue(&bld
);
1901 lp_build_nir_llvm(&bld
.bld_base
, shader
);
1904 LLVMBuilderRef builder
= bld
.bld_base
.base
.gallivm
->builder
;
1905 LLVMValueRef total_emitted_vertices_vec
;
1906 LLVMValueRef emitted_prims_vec
;
1908 for (int i
= 0; i
< params
->gs_vertex_streams
; i
++) {
1909 end_primitive_masked(&bld
.bld_base
, lp_build_mask_value(bld
.mask
), i
);
1911 total_emitted_vertices_vec
=
1912 LLVMBuildLoad(builder
, bld
.total_emitted_vertices_vec_ptr
[i
], "");
1915 LLVMBuildLoad(builder
, bld
.emitted_prims_vec_ptr
[i
], "");
1916 bld
.gs_iface
->gs_epilogue(bld
.gs_iface
,
1917 total_emitted_vertices_vec
,
1918 emitted_prims_vec
, i
);
1921 lp_exec_mask_fini(&bld
.exec_mask
);