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 shuffles
[i
] = lp_build_const_int32(gallivm
, i
/ 2);
73 shuffles
[i
+ 1] = lp_build_const_int32(gallivm
, i
/ 2 + bld_base
->base
.type
.length
);
75 res
= LLVMBuildShuffleVector(builder
, input
, input2
, LLVMConstVector(shuffles
, len
), "");
77 return LLVMBuildBitCast(builder
, res
, bld_base
->dbl_bld
.vec_type
, "");
81 emit_store_64bit_chan(struct lp_build_nir_context
*bld_base
,
82 LLVMValueRef chan_ptr
,
83 LLVMValueRef chan_ptr2
,
86 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
87 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
88 LLVMBuilderRef builder
= gallivm
->builder
;
89 struct lp_build_context
*float_bld
= &bld_base
->base
;
91 LLVMValueRef temp
, temp2
;
92 LLVMValueRef shuffles
[LP_MAX_VECTOR_WIDTH
/32];
93 LLVMValueRef shuffles2
[LP_MAX_VECTOR_WIDTH
/32];
94 int len
= bld_base
->base
.type
.length
* 2;
96 value
= LLVMBuildBitCast(gallivm
->builder
, value
, LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), len
), "");
97 for (i
= 0; i
< bld_base
->base
.type
.length
; i
++) {
98 shuffles
[i
] = lp_build_const_int32(gallivm
, i
* 2);
99 shuffles2
[i
] = lp_build_const_int32(gallivm
, (i
* 2) + 1);
102 temp
= LLVMBuildShuffleVector(builder
, value
,
103 LLVMGetUndef(LLVMTypeOf(value
)),
104 LLVMConstVector(shuffles
,
105 bld_base
->base
.type
.length
),
107 temp2
= LLVMBuildShuffleVector(builder
, value
,
108 LLVMGetUndef(LLVMTypeOf(value
)),
109 LLVMConstVector(shuffles2
,
110 bld_base
->base
.type
.length
),
113 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, temp
, chan_ptr
);
114 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, temp2
, chan_ptr2
);
118 get_soa_array_offsets(struct lp_build_context
*uint_bld
,
119 LLVMValueRef indirect_index
,
122 bool need_perelement_offset
)
124 struct gallivm_state
*gallivm
= uint_bld
->gallivm
;
125 LLVMValueRef chan_vec
=
126 lp_build_const_int_vec(uint_bld
->gallivm
, uint_bld
->type
, chan_index
);
127 LLVMValueRef length_vec
=
128 lp_build_const_int_vec(gallivm
, uint_bld
->type
, uint_bld
->type
.length
);
129 LLVMValueRef index_vec
;
131 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
132 index_vec
= lp_build_mul(uint_bld
, indirect_index
, lp_build_const_int_vec(uint_bld
->gallivm
, uint_bld
->type
, num_components
));
133 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
134 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
136 if (need_perelement_offset
) {
137 LLVMValueRef pixel_offsets
;
139 /* build pixel offset vector: {0, 1, 2, 3, ...} */
140 pixel_offsets
= uint_bld
->undef
;
141 for (i
= 0; i
< uint_bld
->type
.length
; i
++) {
142 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
143 pixel_offsets
= LLVMBuildInsertElement(gallivm
->builder
, pixel_offsets
,
146 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
152 build_gather(struct lp_build_nir_context
*bld_base
,
153 struct lp_build_context
*bld
,
154 LLVMValueRef base_ptr
,
155 LLVMValueRef indexes
,
156 LLVMValueRef overflow_mask
,
157 LLVMValueRef indexes2
)
159 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
160 LLVMBuilderRef builder
= gallivm
->builder
;
161 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
166 res
= LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), bld_base
->base
.type
.length
* 2));
170 * overflow_mask is a vector telling us which channels
171 * in the vector overflowed. We use the overflow behavior for
172 * constant buffers which is defined as:
173 * Out of bounds access to constant buffer returns 0 in all
174 * components. Out of bounds behavior is always with respect
175 * to the size of the buffer bound at that slot.
180 * We avoid per-element control flow here (also due to llvm going crazy,
181 * though I suspect it's better anyway since overflow is likely rare).
182 * Note that since we still fetch from buffers even if num_elements was
183 * zero (in this case we'll fetch from index zero) the jit func callers
184 * MUST provide valid fake constant buffers of size 4x32 (the values do
185 * not matter), otherwise we'd still need (not per element though)
188 indexes
= lp_build_select(uint_bld
, overflow_mask
, uint_bld
->zero
, indexes
);
190 indexes2
= lp_build_select(uint_bld
, overflow_mask
, uint_bld
->zero
, indexes2
);
194 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
196 for (i
= 0; i
< bld
->type
.length
* (indexes2
? 2 : 1); i
++) {
199 LLVMValueRef scalar_ptr
, scalar
;
201 di
= lp_build_const_int32(gallivm
, i
);
203 si
= lp_build_const_int32(gallivm
, i
>> 1);
207 if (indexes2
&& (i
& 1)) {
208 index
= LLVMBuildExtractElement(builder
,
211 index
= LLVMBuildExtractElement(builder
,
214 scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
215 &index
, 1, "gather_ptr");
216 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
218 res
= LLVMBuildInsertElement(builder
, res
, scalar
, di
, "");
223 res
= LLVMBuildBitCast(builder
, res
, bld_base
->dbl_bld
.vec_type
, "");
224 overflow_mask
= LLVMBuildSExt(builder
, overflow_mask
,
225 bld_base
->dbl_bld
.int_vec_type
, "");
226 res
= lp_build_select(&bld_base
->dbl_bld
, overflow_mask
,
227 bld_base
->dbl_bld
.zero
, res
);
229 res
= lp_build_select(bld
, overflow_mask
, bld
->zero
, res
);
236 * Scatter/store vector.
239 emit_mask_scatter(struct lp_build_nir_soa_context
*bld
,
240 LLVMValueRef base_ptr
,
241 LLVMValueRef indexes
,
243 struct lp_exec_mask
*mask
)
245 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
246 LLVMBuilderRef builder
= gallivm
->builder
;
248 LLVMValueRef pred
= mask
->has_mask
? mask
->exec_mask
: NULL
;
251 * Loop over elements of index_vec, store scalar value.
253 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
254 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
255 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
256 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
257 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
258 LLVMValueRef scalar_pred
= pred
?
259 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
262 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
263 ii
, val
, index
, scalar_ptr
);
266 LLVMValueRef real_val
, dst_val
;
267 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
268 real_val
= lp_build_select(&bld
->uint_elem_bld
, scalar_pred
, val
, dst_val
);
269 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
272 LLVMBuildStore(builder
, val
, scalar_ptr
);
277 static void emit_load_var(struct lp_build_nir_context
*bld_base
,
278 nir_variable_mode deref_mode
,
279 unsigned num_components
,
282 unsigned vertex_index
,
283 unsigned const_index
,
284 LLVMValueRef indir_index
,
285 LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
])
287 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
288 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
289 int dmul
= bit_size
== 64 ? 2 : 1;
290 switch (deref_mode
) {
291 case nir_var_shader_in
: {
292 for (unsigned i
= 0; i
< num_components
; i
++) {
293 int idx
= (i
* dmul
) + var
->data
.location_frac
;
295 LLVMValueRef vertex_index_val
= lp_build_const_int32(gallivm
, vertex_index
);
296 LLVMValueRef attrib_index_val
= lp_build_const_int32(gallivm
, const_index
+ var
->data
.driver_location
);
297 LLVMValueRef swizzle_index_val
= lp_build_const_int32(gallivm
, idx
);
298 LLVMValueRef result2
;
299 result
[i
] = bld
->gs_iface
->fetch_input(bld
->gs_iface
, &bld_base
->base
,
300 false, vertex_index_val
, 0, attrib_index_val
, swizzle_index_val
);
301 if (bit_size
== 64) {
302 LLVMValueRef swizzle_index_val
= lp_build_const_int32(gallivm
, idx
+ 1);
303 result2
= bld
->gs_iface
->fetch_input(bld
->gs_iface
, &bld_base
->base
,
304 false, vertex_index_val
, 0, attrib_index_val
, swizzle_index_val
);
305 result
[i
] = emit_fetch_64bit(bld_base
, result
[i
], result2
);
309 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
));
310 LLVMValueRef index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
311 attrib_index_val
, 4, idx
,
313 LLVMValueRef index_vec2
= NULL
;
314 LLVMTypeRef fptr_type
;
315 LLVMValueRef inputs_array
;
316 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
317 inputs_array
= LLVMBuildBitCast(gallivm
->builder
, bld
->inputs_array
, fptr_type
, "");
320 index_vec2
= get_soa_array_offsets(&bld_base
->uint_bld
,
321 indir_index
, 4, idx
+ 1, TRUE
);
323 /* Gather values from the input register array */
324 result
[i
] = build_gather(bld_base
, &bld_base
->base
, inputs_array
, index_vec
, NULL
, index_vec2
);
326 if (bld
->indirects
& nir_var_shader_in
) {
327 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
328 var
->data
.driver_location
* 4 + idx
);
329 LLVMValueRef input_ptr
= lp_build_pointer_get(gallivm
->builder
,
330 bld
->inputs_array
, lindex
);
331 if (bit_size
== 64) {
332 LLVMValueRef lindex2
= lp_build_const_int32(gallivm
,
333 var
->data
.driver_location
* 4 + (idx
+ 1));
334 LLVMValueRef input_ptr2
= lp_build_pointer_get(gallivm
->builder
,
335 bld
->inputs_array
, lindex2
);
336 result
[i
] = emit_fetch_64bit(bld_base
, input_ptr
, input_ptr2
);
338 result
[i
] = input_ptr
;
341 if (bit_size
== 64) {
343 tmp
[0] = bld
->inputs
[var
->data
.driver_location
+ const_index
][idx
];
344 tmp
[1] = bld
->inputs
[var
->data
.driver_location
+ const_index
][idx
+ 1];
345 result
[i
] = emit_fetch_64bit(bld_base
, tmp
[0], tmp
[1]);
347 result
[i
] = bld
->inputs
[var
->data
.driver_location
+ const_index
][idx
];
359 static void emit_store_chan(struct lp_build_nir_context
*bld_base
,
360 nir_variable_mode deref_mode
,
362 unsigned location
, unsigned comp
,
366 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
367 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
368 struct lp_build_context
*float_bld
= &bld_base
->base
;
370 if (bit_size
== 64) {
377 emit_store_64bit_chan(bld_base
, bld
->outputs
[location
][chan
],
378 bld
->outputs
[location
][chan
+ 1], dst
);
380 dst
= LLVMBuildBitCast(builder
, dst
, float_bld
->vec_type
, "");
381 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, dst
,
382 bld
->outputs
[location
][chan
+ comp
]);
386 static void emit_store_var(struct lp_build_nir_context
*bld_base
,
387 nir_variable_mode deref_mode
,
389 unsigned num_components
,
391 unsigned const_index
,
392 nir_variable
*var
, LLVMValueRef dst
)
394 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
395 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
396 switch (deref_mode
) {
397 case nir_var_shader_out
: {
398 unsigned location
= var
->data
.driver_location
+ const_index
;
399 unsigned comp
= var
->data
.location_frac
;
400 if (bld_base
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
401 if (var
->data
.location
== FRAG_RESULT_STENCIL
)
403 else if (var
->data
.location
== FRAG_RESULT_DEPTH
)
406 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
407 if (writemask
& (1u << chan
)) {
408 LLVMValueRef chan_val
= (num_components
== 1) ? dst
: LLVMBuildExtractValue(builder
, dst
, chan
, "");
409 emit_store_chan(bld_base
, deref_mode
, bit_size
, location
, comp
, chan
, chan_val
);
419 static LLVMValueRef
emit_load_reg(struct lp_build_nir_context
*bld_base
,
420 struct lp_build_context
*reg_bld
,
421 const nir_reg_src
*reg
,
422 LLVMValueRef indir_src
,
423 LLVMValueRef reg_storage
)
425 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
426 LLVMBuilderRef builder
= gallivm
->builder
;
427 int nc
= reg
->reg
->num_components
;
428 LLVMValueRef vals
[NIR_MAX_VEC_COMPONENTS
];
429 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
430 if (reg
->reg
->num_array_elems
) {
431 LLVMValueRef indirect_val
= lp_build_const_int_vec(gallivm
, uint_bld
->type
, reg
->base_offset
);
433 LLVMValueRef max_index
= lp_build_const_int_vec(gallivm
, uint_bld
->type
, reg
->reg
->num_array_elems
- 1);
434 indirect_val
= LLVMBuildAdd(builder
, indirect_val
, indir_src
, "");
435 indirect_val
= lp_build_min(uint_bld
, indirect_val
, max_index
);
437 reg_storage
= LLVMBuildBitCast(builder
, reg_storage
, LLVMPointerType(reg_bld
->elem_type
, 0), "");
438 for (unsigned i
= 0; i
< nc
; i
++) {
439 LLVMValueRef indirect_offset
= get_soa_array_offsets(uint_bld
, indirect_val
, nc
, i
, TRUE
);
440 vals
[i
] = build_gather(bld_base
, reg_bld
, reg_storage
, indirect_offset
, NULL
, NULL
);
443 for (unsigned i
= 0; i
< nc
; i
++) {
444 LLVMValueRef this_storage
= nc
== 1 ? reg_storage
: lp_build_array_get_ptr(gallivm
, reg_storage
,
445 lp_build_const_int32(gallivm
, i
));
446 vals
[i
] = LLVMBuildLoad(builder
, this_storage
, "");
449 return nc
== 1 ? vals
[0] : lp_nir_array_build_gather_values(builder
, vals
, nc
);
452 static void emit_store_reg(struct lp_build_nir_context
*bld_base
,
453 struct lp_build_context
*reg_bld
,
454 const nir_reg_dest
*reg
,
456 LLVMValueRef indir_src
,
457 LLVMValueRef reg_storage
,
458 LLVMValueRef dst
[NIR_MAX_VEC_COMPONENTS
])
460 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
461 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
462 LLVMBuilderRef builder
= gallivm
->builder
;
463 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
464 int nc
= reg
->reg
->num_components
;
465 if (reg
->reg
->num_array_elems
> 0) {
466 LLVMValueRef indirect_val
= lp_build_const_int_vec(gallivm
, uint_bld
->type
, reg
->base_offset
);
468 LLVMValueRef max_index
= lp_build_const_int_vec(gallivm
, uint_bld
->type
, reg
->reg
->num_array_elems
- 1);
469 indirect_val
= LLVMBuildAdd(builder
, indirect_val
, indir_src
, "");
470 indirect_val
= lp_build_min(uint_bld
, indirect_val
, max_index
);
472 reg_storage
= LLVMBuildBitCast(builder
, reg_storage
, LLVMPointerType(reg_bld
->elem_type
, 0), "");
473 for (unsigned i
= 0; i
< nc
; i
++) {
474 if (!(writemask
& (1 << i
)))
476 LLVMValueRef indirect_offset
= get_soa_array_offsets(uint_bld
, indirect_val
, nc
, i
, TRUE
);
477 dst
[i
] = LLVMBuildBitCast(builder
, dst
[i
], reg_bld
->vec_type
, "");
478 emit_mask_scatter(bld
, reg_storage
, indirect_offset
, dst
[i
], &bld
->exec_mask
);
483 for (unsigned i
= 0; i
< nc
; i
++) {
484 LLVMValueRef this_storage
= nc
== 1 ? reg_storage
: lp_build_array_get_ptr(gallivm
, reg_storage
,
485 lp_build_const_int32(gallivm
, i
));
486 dst
[i
] = LLVMBuildBitCast(builder
, dst
[i
], reg_bld
->vec_type
, "");
487 lp_exec_mask_store(&bld
->exec_mask
, reg_bld
, dst
[i
], this_storage
);
491 static void emit_load_kernel_arg(struct lp_build_nir_context
*bld_base
,
494 unsigned offset_bit_size
,
495 bool offset_is_uniform
,
497 LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
])
499 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
500 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
501 LLVMBuilderRef builder
= gallivm
->builder
;
502 struct lp_build_context
*bld_broad
= get_int_bld(bld_base
, true, bit_size
);
503 LLVMValueRef kernel_args_ptr
= bld
->kernel_args_ptr
;
504 unsigned size_shift
= 0;
505 struct lp_build_context
*bld_offset
= get_int_bld(bld_base
, true, offset_bit_size
);
508 else if (bit_size
== 32)
510 else if (bit_size
== 64)
513 offset
= lp_build_shr(bld_offset
, offset
, lp_build_const_int_vec(gallivm
, bld_offset
->type
, size_shift
));
515 LLVMTypeRef ptr_type
= LLVMPointerType(bld_broad
->elem_type
, 0);
516 kernel_args_ptr
= LLVMBuildBitCast(builder
, kernel_args_ptr
, ptr_type
, "");
518 if (offset_is_uniform
) {
519 offset
= LLVMBuildExtractElement(builder
, offset
, lp_build_const_int32(gallivm
, 0), "");
521 for (unsigned c
= 0; c
< nc
; c
++) {
522 LLVMValueRef this_offset
= LLVMBuildAdd(builder
, offset
, offset_bit_size
== 64 ? lp_build_const_int64(gallivm
, c
) : lp_build_const_int32(gallivm
, c
), "");
524 LLVMValueRef scalar
= lp_build_pointer_get(builder
, kernel_args_ptr
, this_offset
);
525 result
[c
] = lp_build_broadcast_scalar(bld_broad
, scalar
);
530 static LLVMValueRef
global_addr_to_ptr(struct gallivm_state
*gallivm
, LLVMValueRef addr_ptr
, unsigned bit_size
)
532 LLVMBuilderRef builder
= gallivm
->builder
;
535 addr_ptr
= LLVMBuildIntToPtr(builder
, addr_ptr
, LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0), "");
538 addr_ptr
= LLVMBuildIntToPtr(builder
, addr_ptr
, LLVMPointerType(LLVMInt16TypeInContext(gallivm
->context
), 0), "");
542 addr_ptr
= LLVMBuildIntToPtr(builder
, addr_ptr
, LLVMPointerType(LLVMInt32TypeInContext(gallivm
->context
), 0), "");
545 addr_ptr
= LLVMBuildIntToPtr(builder
, addr_ptr
, LLVMPointerType(LLVMInt64TypeInContext(gallivm
->context
), 0), "");
551 static void emit_load_global(struct lp_build_nir_context
*bld_base
,
554 unsigned addr_bit_size
,
556 LLVMValueRef outval
[NIR_MAX_VEC_COMPONENTS
])
558 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
559 LLVMBuilderRef builder
= gallivm
->builder
;
560 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
561 struct lp_build_context
*res_bld
;
563 res_bld
= get_int_bld(bld_base
, true, bit_size
);
565 for (unsigned c
= 0; c
< nc
; c
++) {
566 LLVMValueRef result
= lp_build_alloca(gallivm
, res_bld
->vec_type
, "");
568 struct lp_build_loop_state loop_state
;
569 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
571 LLVMValueRef addr_ptr
= LLVMBuildExtractElement(gallivm
->builder
, addr
,
572 loop_state
.counter
, "");
573 addr_ptr
= global_addr_to_ptr(gallivm
, addr_ptr
, bit_size
);
575 LLVMValueRef value_ptr
= lp_build_pointer_get(builder
, addr_ptr
, lp_build_const_int32(gallivm
, c
));
577 LLVMValueRef temp_res
;
578 temp_res
= LLVMBuildLoad(builder
, result
, "");
579 temp_res
= LLVMBuildInsertElement(builder
, temp_res
, value_ptr
, loop_state
.counter
, "");
580 LLVMBuildStore(builder
, temp_res
, result
);
581 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, uint_bld
->type
.length
),
583 outval
[c
] = LLVMBuildLoad(builder
, result
, "");
587 static void emit_store_global(struct lp_build_nir_context
*bld_base
,
589 unsigned nc
, unsigned bit_size
,
590 unsigned addr_bit_size
,
594 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
595 LLVMBuilderRef builder
= gallivm
->builder
;
596 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
598 for (unsigned c
= 0; c
< nc
; c
++) {
599 if (!(writemask
& (1u << c
)))
601 LLVMValueRef val
= (nc
== 1) ? dst
: LLVMBuildExtractValue(builder
, dst
, c
, "");
603 LLVMValueRef exec_mask
= mask_vec(bld_base
);
604 struct lp_build_loop_state loop_state
;
605 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
606 LLVMValueRef value_ptr
= LLVMBuildExtractElement(gallivm
->builder
, val
,
607 loop_state
.counter
, "");
609 LLVMValueRef addr_ptr
= LLVMBuildExtractElement(gallivm
->builder
, addr
,
610 loop_state
.counter
, "");
611 addr_ptr
= global_addr_to_ptr(gallivm
, addr_ptr
, bit_size
);
614 value_ptr
= LLVMBuildBitCast(builder
, value_ptr
, LLVMInt32TypeInContext(gallivm
->context
), "");
617 value_ptr
= LLVMBuildBitCast(builder
, value_ptr
, LLVMInt64TypeInContext(gallivm
->context
), "");
622 struct lp_build_if_state ifthen
;
624 LLVMValueRef cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, exec_mask
, uint_bld
->zero
, "");
625 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, loop_state
.counter
, "");
626 lp_build_if(&ifthen
, gallivm
, cond
);
627 lp_build_pointer_set(builder
, addr_ptr
, lp_build_const_int32(gallivm
, c
), value_ptr
);
628 lp_build_endif(&ifthen
);
629 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, uint_bld
->type
.length
),
634 static void emit_atomic_global(struct lp_build_nir_context
*bld_base
,
635 nir_intrinsic_op nir_op
,
636 unsigned addr_bit_size
,
638 LLVMValueRef val
, LLVMValueRef val2
,
639 LLVMValueRef
*result
)
641 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
642 LLVMBuilderRef builder
= gallivm
->builder
;
643 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
644 LLVMAtomicRMWBinOp op
;
646 case nir_intrinsic_global_atomic_add
:
647 op
= LLVMAtomicRMWBinOpAdd
;
649 case nir_intrinsic_global_atomic_exchange
:
650 op
= LLVMAtomicRMWBinOpXchg
;
652 case nir_intrinsic_global_atomic_and
:
653 op
= LLVMAtomicRMWBinOpAnd
;
655 case nir_intrinsic_global_atomic_or
:
656 op
= LLVMAtomicRMWBinOpOr
;
658 case nir_intrinsic_global_atomic_xor
:
659 op
= LLVMAtomicRMWBinOpXor
;
661 case nir_intrinsic_global_atomic_umin
:
662 op
= LLVMAtomicRMWBinOpUMin
;
664 case nir_intrinsic_global_atomic_umax
:
665 op
= LLVMAtomicRMWBinOpUMax
;
667 case nir_intrinsic_global_atomic_imin
:
668 op
= LLVMAtomicRMWBinOpMin
;
670 case nir_intrinsic_global_atomic_imax
:
671 op
= LLVMAtomicRMWBinOpMax
;
677 LLVMValueRef atom_res
= lp_build_alloca(gallivm
,
678 uint_bld
->vec_type
, "");
679 LLVMValueRef exec_mask
= mask_vec(bld_base
);
680 struct lp_build_loop_state loop_state
;
681 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
683 LLVMValueRef value_ptr
= LLVMBuildExtractElement(gallivm
->builder
, val
,
684 loop_state
.counter
, "");
686 LLVMValueRef addr_ptr
= LLVMBuildExtractElement(gallivm
->builder
, addr
,
687 loop_state
.counter
, "");
688 addr_ptr
= global_addr_to_ptr(gallivm
, addr_ptr
, 32);
689 struct lp_build_if_state ifthen
;
690 LLVMValueRef cond
, temp_res
;
692 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, exec_mask
, uint_bld
->zero
, "");
693 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, loop_state
.counter
, "");
694 lp_build_if(&ifthen
, gallivm
, cond
);
696 if (nir_op
== nir_intrinsic_global_atomic_comp_swap
) {
697 LLVMValueRef cas_src_ptr
= LLVMBuildExtractElement(gallivm
->builder
, val2
,
698 loop_state
.counter
, "");
699 cas_src_ptr
= LLVMBuildBitCast(gallivm
->builder
, cas_src_ptr
, uint_bld
->elem_type
, "");
700 scalar
= LLVMBuildAtomicCmpXchg(builder
, addr_ptr
, value_ptr
,
702 LLVMAtomicOrderingSequentiallyConsistent
,
703 LLVMAtomicOrderingSequentiallyConsistent
,
705 scalar
= LLVMBuildExtractValue(gallivm
->builder
, scalar
, 0, "");
707 scalar
= LLVMBuildAtomicRMW(builder
, op
,
709 LLVMAtomicOrderingSequentiallyConsistent
,
712 temp_res
= LLVMBuildLoad(builder
, atom_res
, "");
713 temp_res
= LLVMBuildInsertElement(builder
, temp_res
, scalar
, loop_state
.counter
, "");
714 LLVMBuildStore(builder
, temp_res
, atom_res
);
715 lp_build_else(&ifthen
);
716 temp_res
= LLVMBuildLoad(builder
, atom_res
, "");
717 temp_res
= LLVMBuildInsertElement(builder
, temp_res
, lp_build_const_int32(gallivm
, 0), loop_state
.counter
, "");
718 LLVMBuildStore(builder
, temp_res
, atom_res
);
719 lp_build_endif(&ifthen
);
720 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, uint_bld
->type
.length
),
722 *result
= LLVMBuildLoad(builder
, atom_res
, "");
725 static void emit_load_ubo(struct lp_build_nir_context
*bld_base
,
728 bool offset_is_uniform
,
731 LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
])
733 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
734 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
735 LLVMBuilderRef builder
= gallivm
->builder
;
736 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
737 struct lp_build_context
*bld_broad
= bit_size
== 64 ? &bld_base
->dbl_bld
: &bld_base
->base
;
738 LLVMValueRef consts_ptr
= lp_build_array_get(gallivm
, bld
->consts_ptr
, index
);
739 unsigned size_shift
= 0;
742 else if (bit_size
== 64)
745 offset
= lp_build_shr(uint_bld
, offset
, lp_build_const_int_vec(gallivm
, uint_bld
->type
, size_shift
));
746 if (bit_size
== 64) {
747 LLVMTypeRef dptr_type
= LLVMPointerType(bld_base
->dbl_bld
.elem_type
, 0);
748 consts_ptr
= LLVMBuildBitCast(builder
, consts_ptr
, dptr_type
, "");
751 if (offset_is_uniform
) {
752 offset
= LLVMBuildExtractElement(builder
, offset
, lp_build_const_int32(gallivm
, 0), "");
754 for (unsigned c
= 0; c
< nc
; c
++) {
755 LLVMValueRef this_offset
= LLVMBuildAdd(builder
, offset
, lp_build_const_int32(gallivm
, c
), "");
757 LLVMValueRef scalar
= lp_build_pointer_get(builder
, consts_ptr
, this_offset
);
758 result
[c
] = lp_build_broadcast_scalar(bld_broad
, scalar
);
761 LLVMValueRef overflow_mask
;
762 LLVMValueRef num_consts
= lp_build_array_get(gallivm
, bld
->const_sizes_ptr
, index
);
764 num_consts
= LLVMBuildShl(gallivm
->builder
, num_consts
, lp_build_const_int32(gallivm
, 4), "");
765 num_consts
= lp_build_broadcast_scalar(uint_bld
, num_consts
);
766 for (unsigned c
= 0; c
< nc
; c
++) {
767 LLVMValueRef this_offset
= lp_build_add(uint_bld
, offset
, lp_build_const_int_vec(gallivm
, uint_bld
->type
, c
));
768 overflow_mask
= lp_build_compare(gallivm
, uint_bld
->type
, PIPE_FUNC_GEQUAL
,
769 this_offset
, num_consts
);
771 result
[c
] = build_gather(bld_base
, bld_broad
, consts_ptr
, this_offset
, overflow_mask
, NULL
);
777 static void emit_load_mem(struct lp_build_nir_context
*bld_base
,
782 LLVMValueRef outval
[NIR_MAX_VEC_COMPONENTS
])
784 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
785 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
786 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
787 LLVMValueRef ssbo_ptr
= NULL
;
788 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
789 struct lp_build_context
*uint64_bld
= &bld_base
->uint64_bld
;
790 LLVMValueRef ssbo_limit
= NULL
;
793 LLVMValueRef ssbo_size_ptr
= lp_build_array_get(gallivm
, bld
->ssbo_sizes_ptr
, LLVMBuildExtractElement(builder
, index
, lp_build_const_int32(gallivm
, 0), ""));
794 ssbo_limit
= LLVMBuildAShr(gallivm
->builder
, ssbo_size_ptr
, lp_build_const_int32(gallivm
, bit_size
== 64 ? 3 : 2), "");
795 ssbo_limit
= lp_build_broadcast_scalar(uint_bld
, ssbo_limit
);
797 ssbo_ptr
= lp_build_array_get(gallivm
, bld
->ssbo_ptr
, LLVMBuildExtractElement(builder
, index
, lp_build_const_int32(gallivm
, 0), ""));
799 ssbo_ptr
= bld
->shared_ptr
;
801 offset
= LLVMBuildAShr(gallivm
->builder
, offset
, lp_build_const_int_vec(gallivm
, uint_bld
->type
, bit_size
== 64 ? 3 : 2), "");
802 for (unsigned c
= 0; c
< nc
; c
++) {
803 LLVMValueRef loop_index
= lp_build_add(uint_bld
, offset
, lp_build_const_int_vec(gallivm
, uint_bld
->type
, c
));
804 LLVMValueRef exec_mask
= mask_vec(bld_base
);
807 LLVMValueRef ssbo_oob_cmp
= lp_build_cmp(uint_bld
, PIPE_FUNC_LESS
, loop_index
, ssbo_limit
);
808 exec_mask
= LLVMBuildAnd(builder
, exec_mask
, ssbo_oob_cmp
, "");
811 LLVMValueRef result
= lp_build_alloca(gallivm
, bit_size
== 64 ? uint64_bld
->vec_type
: uint_bld
->vec_type
, "");
812 struct lp_build_loop_state loop_state
;
813 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
815 struct lp_build_if_state ifthen
;
816 LLVMValueRef cond
, temp_res
;
818 loop_index
= LLVMBuildExtractElement(gallivm
->builder
, loop_index
,
819 loop_state
.counter
, "");
821 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, exec_mask
, uint_bld
->zero
, "");
822 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, loop_state
.counter
, "");
824 lp_build_if(&ifthen
, gallivm
, cond
);
826 if (bit_size
== 64) {
827 LLVMValueRef ssbo_ptr2
= LLVMBuildBitCast(builder
, ssbo_ptr
, LLVMPointerType(uint64_bld
->elem_type
, 0), "");
828 scalar
= lp_build_pointer_get(builder
, ssbo_ptr2
, loop_index
);
830 scalar
= lp_build_pointer_get(builder
, ssbo_ptr
, loop_index
);
832 temp_res
= LLVMBuildLoad(builder
, result
, "");
833 temp_res
= LLVMBuildInsertElement(builder
, temp_res
, scalar
, loop_state
.counter
, "");
834 LLVMBuildStore(builder
, temp_res
, result
);
835 lp_build_else(&ifthen
);
836 temp_res
= LLVMBuildLoad(builder
, result
, "");
839 zero
= LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
), 0, 0);
841 zero
= lp_build_const_int32(gallivm
, 0);
842 temp_res
= LLVMBuildInsertElement(builder
, temp_res
, zero
, loop_state
.counter
, "");
843 LLVMBuildStore(builder
, temp_res
, result
);
844 lp_build_endif(&ifthen
);
845 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, uint_bld
->type
.length
),
847 outval
[c
] = LLVMBuildLoad(gallivm
->builder
, result
, "");
851 static void emit_store_mem(struct lp_build_nir_context
*bld_base
,
859 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
860 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
861 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
862 LLVMValueRef ssbo_ptr
;
863 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
864 LLVMValueRef ssbo_limit
= NULL
;
867 LLVMValueRef ssbo_size_ptr
= lp_build_array_get(gallivm
, bld
->ssbo_sizes_ptr
, LLVMBuildExtractElement(builder
, index
, lp_build_const_int32(gallivm
, 0), ""));
868 ssbo_limit
= LLVMBuildAShr(gallivm
->builder
, ssbo_size_ptr
, lp_build_const_int32(gallivm
, bit_size
== 64 ? 3 : 2), "");
869 ssbo_limit
= lp_build_broadcast_scalar(uint_bld
, ssbo_limit
);
870 ssbo_ptr
= lp_build_array_get(gallivm
, bld
->ssbo_ptr
, LLVMBuildExtractElement(builder
, index
, lp_build_const_int32(gallivm
, 0), ""));
872 ssbo_ptr
= bld
->shared_ptr
;
874 offset
= lp_build_shr_imm(uint_bld
, offset
, bit_size
== 64 ? 3 : 2);
875 for (unsigned c
= 0; c
< nc
; c
++) {
876 if (!(writemask
& (1u << c
)))
878 LLVMValueRef loop_index
= lp_build_add(uint_bld
, offset
, lp_build_const_int_vec(gallivm
, uint_bld
->type
, c
));
879 LLVMValueRef val
= (nc
== 1) ? dst
: LLVMBuildExtractValue(builder
, dst
, c
, "");
881 LLVMValueRef exec_mask
= mask_vec(bld_base
);
883 LLVMValueRef ssbo_oob_cmp
= lp_build_cmp(uint_bld
, PIPE_FUNC_LESS
, loop_index
, ssbo_limit
);
884 exec_mask
= LLVMBuildAnd(builder
, exec_mask
, ssbo_oob_cmp
, "");
887 struct lp_build_loop_state loop_state
;
888 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
889 LLVMValueRef value_ptr
= LLVMBuildExtractElement(gallivm
->builder
, val
,
890 loop_state
.counter
, "");
892 value_ptr
= LLVMBuildBitCast(gallivm
->builder
, value_ptr
, bld_base
->uint64_bld
.elem_type
, "");
894 value_ptr
= LLVMBuildBitCast(gallivm
->builder
, value_ptr
, uint_bld
->elem_type
, "");
895 struct lp_build_if_state ifthen
;
898 loop_index
= LLVMBuildExtractElement(gallivm
->builder
, loop_index
,
899 loop_state
.counter
, "");
900 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, exec_mask
, uint_bld
->zero
, "");
901 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, loop_state
.counter
, "");
902 lp_build_if(&ifthen
, gallivm
, cond
);
903 if (bit_size
== 64) {
904 LLVMValueRef ssbo_ptr2
= LLVMBuildBitCast(builder
, ssbo_ptr
, LLVMPointerType(bld_base
->uint64_bld
.elem_type
, 0), "");
905 lp_build_pointer_set(builder
, ssbo_ptr2
, loop_index
, value_ptr
);
907 lp_build_pointer_set(builder
, ssbo_ptr
, loop_index
, value_ptr
);
908 lp_build_endif(&ifthen
);
909 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, uint_bld
->type
.length
),
914 static void emit_atomic_mem(struct lp_build_nir_context
*bld_base
,
915 nir_intrinsic_op nir_op
,
916 LLVMValueRef index
, LLVMValueRef offset
,
917 LLVMValueRef val
, LLVMValueRef val2
,
918 LLVMValueRef
*result
)
920 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
921 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
922 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
923 LLVMValueRef ssbo_ptr
;
924 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
925 LLVMAtomicRMWBinOp op
;
926 LLVMValueRef ssbo_limit
= NULL
;
929 LLVMValueRef ssbo_size_ptr
= lp_build_array_get(gallivm
, bld
->ssbo_sizes_ptr
, LLVMBuildExtractElement(builder
, index
, lp_build_const_int32(gallivm
, 0), ""));
930 ssbo_limit
= LLVMBuildAShr(gallivm
->builder
, ssbo_size_ptr
, lp_build_const_int32(gallivm
, 2), "");
931 ssbo_limit
= lp_build_broadcast_scalar(uint_bld
, ssbo_limit
);
932 ssbo_ptr
= lp_build_array_get(gallivm
, bld
->ssbo_ptr
, LLVMBuildExtractElement(builder
, index
, lp_build_const_int32(gallivm
, 0), ""));
934 ssbo_ptr
= bld
->shared_ptr
;
937 case nir_intrinsic_shared_atomic_add
:
938 case nir_intrinsic_ssbo_atomic_add
:
939 op
= LLVMAtomicRMWBinOpAdd
;
941 case nir_intrinsic_shared_atomic_exchange
:
942 case nir_intrinsic_ssbo_atomic_exchange
:
943 op
= LLVMAtomicRMWBinOpXchg
;
945 case nir_intrinsic_shared_atomic_and
:
946 case nir_intrinsic_ssbo_atomic_and
:
947 op
= LLVMAtomicRMWBinOpAnd
;
949 case nir_intrinsic_shared_atomic_or
:
950 case nir_intrinsic_ssbo_atomic_or
:
951 op
= LLVMAtomicRMWBinOpOr
;
953 case nir_intrinsic_shared_atomic_xor
:
954 case nir_intrinsic_ssbo_atomic_xor
:
955 op
= LLVMAtomicRMWBinOpXor
;
957 case nir_intrinsic_shared_atomic_umin
:
958 case nir_intrinsic_ssbo_atomic_umin
:
959 op
= LLVMAtomicRMWBinOpUMin
;
961 case nir_intrinsic_shared_atomic_umax
:
962 case nir_intrinsic_ssbo_atomic_umax
:
963 op
= LLVMAtomicRMWBinOpUMax
;
965 case nir_intrinsic_ssbo_atomic_imin
:
966 case nir_intrinsic_shared_atomic_imin
:
967 op
= LLVMAtomicRMWBinOpMin
;
969 case nir_intrinsic_ssbo_atomic_imax
:
970 case nir_intrinsic_shared_atomic_imax
:
971 op
= LLVMAtomicRMWBinOpMax
;
977 offset
= lp_build_shr_imm(uint_bld
, offset
, 2);
978 LLVMValueRef atom_res
= lp_build_alloca(gallivm
,
979 uint_bld
->vec_type
, "");
981 LLVMValueRef exec_mask
= mask_vec(bld_base
);
983 LLVMValueRef ssbo_oob_cmp
= lp_build_cmp(uint_bld
, PIPE_FUNC_LESS
, offset
, ssbo_limit
);
984 exec_mask
= LLVMBuildAnd(builder
, exec_mask
, ssbo_oob_cmp
, "");
987 struct lp_build_loop_state loop_state
;
988 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
990 LLVMValueRef value_ptr
= LLVMBuildExtractElement(gallivm
->builder
, val
,
991 loop_state
.counter
, "");
992 value_ptr
= LLVMBuildBitCast(gallivm
->builder
, value_ptr
, uint_bld
->elem_type
, "");
994 offset
= LLVMBuildExtractElement(gallivm
->builder
, offset
,
995 loop_state
.counter
, "");
997 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, ssbo_ptr
,
1000 struct lp_build_if_state ifthen
;
1001 LLVMValueRef cond
, temp_res
;
1002 LLVMValueRef scalar
;
1003 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, exec_mask
, uint_bld
->zero
, "");
1004 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, loop_state
.counter
, "");
1005 lp_build_if(&ifthen
, gallivm
, cond
);
1007 if (nir_op
== nir_intrinsic_ssbo_atomic_comp_swap
|| nir_op
== nir_intrinsic_shared_atomic_comp_swap
) {
1008 LLVMValueRef cas_src_ptr
= LLVMBuildExtractElement(gallivm
->builder
, val2
,
1009 loop_state
.counter
, "");
1010 cas_src_ptr
= LLVMBuildBitCast(gallivm
->builder
, cas_src_ptr
, uint_bld
->elem_type
, "");
1011 scalar
= LLVMBuildAtomicCmpXchg(builder
, scalar_ptr
, value_ptr
,
1013 LLVMAtomicOrderingSequentiallyConsistent
,
1014 LLVMAtomicOrderingSequentiallyConsistent
,
1016 scalar
= LLVMBuildExtractValue(gallivm
->builder
, scalar
, 0, "");
1018 scalar
= LLVMBuildAtomicRMW(builder
, op
,
1019 scalar_ptr
, value_ptr
,
1020 LLVMAtomicOrderingSequentiallyConsistent
,
1023 temp_res
= LLVMBuildLoad(builder
, atom_res
, "");
1024 temp_res
= LLVMBuildInsertElement(builder
, temp_res
, scalar
, loop_state
.counter
, "");
1025 LLVMBuildStore(builder
, temp_res
, atom_res
);
1026 lp_build_else(&ifthen
);
1027 temp_res
= LLVMBuildLoad(builder
, atom_res
, "");
1028 temp_res
= LLVMBuildInsertElement(builder
, temp_res
, lp_build_const_int32(gallivm
, 0), loop_state
.counter
, "");
1029 LLVMBuildStore(builder
, temp_res
, atom_res
);
1030 lp_build_endif(&ifthen
);
1032 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, uint_bld
->type
.length
),
1034 *result
= LLVMBuildLoad(builder
, atom_res
, "");
1037 static void emit_barrier(struct lp_build_nir_context
*bld_base
)
1039 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1040 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1042 LLVMBasicBlockRef resume
= lp_build_insert_new_block(gallivm
, "resume");
1044 lp_build_coro_suspend_switch(gallivm
, bld
->coro
, resume
, false);
1045 LLVMPositionBuilderAtEnd(gallivm
->builder
, resume
);
1048 static LLVMValueRef
emit_get_buffer_size(struct lp_build_nir_context
*bld_base
,
1051 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1052 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1053 struct lp_build_context
*bld_broad
= &bld_base
->uint_bld
;
1054 LLVMValueRef size_ptr
= lp_build_array_get(bld_base
->base
.gallivm
, bld
->ssbo_sizes_ptr
, LLVMBuildExtractElement(builder
, index
, bld_broad
->zero
, ""));
1055 return lp_build_broadcast_scalar(bld_broad
, size_ptr
);
1058 static void emit_image_op(struct lp_build_nir_context
*bld_base
,
1059 struct lp_img_params
*params
)
1061 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1062 params
->type
= bld_base
->base
.type
;
1063 params
->context_ptr
= bld
->context_ptr
;
1064 params
->thread_data_ptr
= bld
->thread_data_ptr
;
1065 params
->exec_mask
= mask_vec(bld_base
);
1066 bld
->image
->emit_op(bld
->image
,
1067 bld
->bld_base
.base
.gallivm
,
1072 static void emit_image_size(struct lp_build_nir_context
*bld_base
,
1073 struct lp_sampler_size_query_params
*params
)
1075 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1077 params
->int_type
= bld_base
->int_bld
.type
;
1078 params
->context_ptr
= bld
->context_ptr
;
1080 bld
->image
->emit_size_query(bld
->image
,
1081 bld
->bld_base
.base
.gallivm
,
1086 static void init_var_slots(struct lp_build_nir_context
*bld_base
,
1087 nir_variable
*var
, unsigned sc
)
1089 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1090 unsigned slots
= glsl_count_attribute_slots(var
->type
, false) * 4;
1092 for (unsigned comp
= sc
; comp
< slots
+ sc
; comp
++) {
1093 unsigned this_loc
= var
->data
.driver_location
+ (comp
/ 4);
1094 unsigned this_chan
= comp
% 4;
1096 if (!bld
->outputs
[this_loc
][this_chan
])
1097 bld
->outputs
[this_loc
][this_chan
] = lp_build_alloca(bld_base
->base
.gallivm
,
1098 bld_base
->base
.vec_type
, "output");
1102 static void emit_var_decl(struct lp_build_nir_context
*bld_base
,
1105 unsigned sc
= var
->data
.location_frac
;
1106 switch (var
->data
.mode
) {
1107 case nir_var_shader_out
: {
1108 if (bld_base
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1109 if (var
->data
.location
== FRAG_RESULT_STENCIL
)
1111 else if (var
->data
.location
== FRAG_RESULT_DEPTH
)
1114 init_var_slots(bld_base
, var
, sc
);
1122 static void emit_tex(struct lp_build_nir_context
*bld_base
,
1123 struct lp_sampler_params
*params
)
1125 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1127 params
->type
= bld_base
->base
.type
;
1128 params
->context_ptr
= bld
->context_ptr
;
1129 params
->thread_data_ptr
= bld
->thread_data_ptr
;
1131 bld
->sampler
->emit_tex_sample(bld
->sampler
,
1132 bld
->bld_base
.base
.gallivm
,
1136 static void emit_tex_size(struct lp_build_nir_context
*bld_base
,
1137 struct lp_sampler_size_query_params
*params
)
1139 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1141 params
->int_type
= bld_base
->int_bld
.type
;
1142 params
->context_ptr
= bld
->context_ptr
;
1144 bld
->sampler
->emit_size_query(bld
->sampler
,
1145 bld
->bld_base
.base
.gallivm
,
1149 static void emit_sysval_intrin(struct lp_build_nir_context
*bld_base
,
1150 nir_intrinsic_instr
*instr
,
1151 LLVMValueRef result
[NIR_MAX_VEC_COMPONENTS
])
1153 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1154 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1155 switch (instr
->intrinsic
) {
1156 case nir_intrinsic_load_instance_id
:
1157 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.instance_id
);
1159 case nir_intrinsic_load_base_instance
:
1160 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.base_instance
);
1162 case nir_intrinsic_load_base_vertex
:
1163 result
[0] = bld
->system_values
.basevertex
;
1165 case nir_intrinsic_load_vertex_id
:
1166 result
[0] = bld
->system_values
.vertex_id
;
1168 case nir_intrinsic_load_primitive_id
:
1169 result
[0] = bld
->system_values
.prim_id
;
1171 case nir_intrinsic_load_work_group_id
:
1172 for (unsigned i
= 0; i
< 3; i
++)
1173 result
[i
] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, LLVMBuildExtractElement(gallivm
->builder
, bld
->system_values
.block_id
, lp_build_const_int32(gallivm
, i
), ""));
1175 case nir_intrinsic_load_local_invocation_id
:
1176 for (unsigned i
= 0; i
< 3; i
++)
1177 result
[i
] = LLVMBuildExtractValue(gallivm
->builder
, bld
->system_values
.thread_id
, i
, "");
1179 case nir_intrinsic_load_num_work_groups
:
1180 for (unsigned i
= 0; i
< 3; i
++)
1181 result
[i
] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, LLVMBuildExtractElement(gallivm
->builder
, bld
->system_values
.grid_size
, lp_build_const_int32(gallivm
, i
), ""));
1183 case nir_intrinsic_load_invocation_id
:
1184 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.invocation_id
);
1186 case nir_intrinsic_load_front_face
:
1187 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.front_facing
);
1189 case nir_intrinsic_load_draw_id
:
1190 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.draw_id
);
1194 case nir_intrinsic_load_local_group_size
:
1195 for (unsigned i
= 0; i
< 3; i
++)
1196 result
[i
] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, LLVMBuildExtractElement(gallivm
->builder
, bld
->system_values
.block_size
, lp_build_const_int32(gallivm
, i
), ""));
1198 case nir_intrinsic_load_work_dim
:
1199 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.work_dim
);
1204 static void bgnloop(struct lp_build_nir_context
*bld_base
)
1206 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1207 lp_exec_bgnloop(&bld
->exec_mask
, true);
1210 static void endloop(struct lp_build_nir_context
*bld_base
)
1212 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1213 lp_exec_endloop(bld_base
->base
.gallivm
, &bld
->exec_mask
);
1216 static void if_cond(struct lp_build_nir_context
*bld_base
, LLVMValueRef cond
)
1218 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1219 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1220 lp_exec_mask_cond_push(&bld
->exec_mask
, LLVMBuildBitCast(builder
, cond
, bld_base
->base
.int_vec_type
, ""));
1223 static void else_stmt(struct lp_build_nir_context
*bld_base
)
1225 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1226 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1229 static void endif_stmt(struct lp_build_nir_context
*bld_base
)
1231 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1232 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1235 static void break_stmt(struct lp_build_nir_context
*bld_base
)
1237 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1239 lp_exec_break(&bld
->exec_mask
, NULL
, false);
1242 static void continue_stmt(struct lp_build_nir_context
*bld_base
)
1244 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1245 lp_exec_continue(&bld
->exec_mask
);
1248 static void discard(struct lp_build_nir_context
*bld_base
, LLVMValueRef cond
)
1250 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1251 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1255 if (bld
->exec_mask
.has_mask
) {
1256 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1258 mask
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
1261 mask
= LLVMBuildNot(builder
, cond
, "");
1262 if (bld
->exec_mask
.has_mask
) {
1263 LLVMValueRef invmask
;
1264 invmask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1265 mask
= LLVMBuildOr(builder
, mask
, invmask
, "");
1268 lp_build_mask_update(bld
->mask
, mask
);
1272 increment_vec_ptr_by_mask(struct lp_build_nir_context
* bld_base
,
1276 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1277 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
1279 current_vec
= LLVMBuildSub(builder
, current_vec
, mask
, "");
1281 LLVMBuildStore(builder
, current_vec
, ptr
);
1285 clear_uint_vec_ptr_from_mask(struct lp_build_nir_context
* bld_base
,
1289 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1290 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
1292 current_vec
= lp_build_select(&bld_base
->uint_bld
,
1294 bld_base
->uint_bld
.zero
,
1297 LLVMBuildStore(builder
, current_vec
, ptr
);
1301 clamp_mask_to_max_output_vertices(struct lp_build_nir_soa_context
* bld
,
1302 LLVMValueRef current_mask_vec
,
1303 LLVMValueRef total_emitted_vertices_vec
)
1305 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1306 struct lp_build_context
*int_bld
= &bld
->bld_base
.int_bld
;
1307 LLVMValueRef max_mask
= lp_build_cmp(int_bld
, PIPE_FUNC_LESS
,
1308 total_emitted_vertices_vec
,
1309 bld
->max_output_vertices_vec
);
1311 return LLVMBuildAnd(builder
, current_mask_vec
, max_mask
, "");
1314 static void emit_vertex(struct lp_build_nir_context
*bld_base
, uint32_t stream_id
)
1316 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1317 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1319 assert(bld
->gs_iface
->emit_vertex
);
1320 LLVMValueRef total_emitted_vertices_vec
=
1321 LLVMBuildLoad(builder
, bld
->total_emitted_vertices_vec_ptr
, "");
1322 LLVMValueRef mask
= mask_vec(bld_base
);
1323 mask
= clamp_mask_to_max_output_vertices(bld
, mask
,
1324 total_emitted_vertices_vec
);
1325 bld
->gs_iface
->emit_vertex(bld
->gs_iface
, &bld
->bld_base
.base
,
1327 total_emitted_vertices_vec
,
1328 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, bld
->bld_base
.base
.type
, stream_id
));
1330 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_vertices_vec_ptr
,
1332 increment_vec_ptr_by_mask(bld_base
, bld
->total_emitted_vertices_vec_ptr
,
1337 end_primitive_masked(struct lp_build_nir_context
* bld_base
,
1340 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1341 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1343 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1344 LLVMValueRef emitted_vertices_vec
=
1345 LLVMBuildLoad(builder
, bld
->emitted_vertices_vec_ptr
, "");
1346 LLVMValueRef emitted_prims_vec
=
1347 LLVMBuildLoad(builder
, bld
->emitted_prims_vec_ptr
, "");
1348 LLVMValueRef total_emitted_vertices_vec
=
1349 LLVMBuildLoad(builder
, bld
->total_emitted_vertices_vec_ptr
, "");
1351 LLVMValueRef emitted_mask
= lp_build_cmp(uint_bld
,
1353 emitted_vertices_vec
,
1355 mask
= LLVMBuildAnd(builder
, mask
, emitted_mask
, "");
1356 bld
->gs_iface
->end_primitive(bld
->gs_iface
, &bld
->bld_base
.base
,
1357 total_emitted_vertices_vec
,
1358 emitted_vertices_vec
, emitted_prims_vec
, mask_vec(bld_base
));
1359 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_prims_vec_ptr
,
1361 clear_uint_vec_ptr_from_mask(bld_base
, bld
->emitted_vertices_vec_ptr
,
1365 static void end_primitive(struct lp_build_nir_context
*bld_base
, uint32_t stream_id
)
1367 struct lp_build_nir_soa_context
*bld
= (struct lp_build_nir_soa_context
*)bld_base
;
1369 assert(bld
->gs_iface
->end_primitive
);
1371 LLVMValueRef mask
= mask_vec(bld_base
);
1372 end_primitive_masked(bld_base
, mask
);
1376 emit_prologue(struct lp_build_nir_soa_context
*bld
)
1378 struct gallivm_state
* gallivm
= bld
->bld_base
.base
.gallivm
;
1379 if (bld
->indirects
& nir_var_shader_in
&& !bld
->gs_iface
) {
1380 uint32_t num_inputs
= util_bitcount64(bld
->bld_base
.shader
->info
.inputs_read
);
1381 unsigned index
, chan
;
1382 LLVMTypeRef vec_type
= bld
->bld_base
.base
.vec_type
;
1383 LLVMValueRef array_size
= lp_build_const_int32(gallivm
, num_inputs
* 4);
1384 bld
->inputs_array
= lp_build_array_alloca(gallivm
,
1385 vec_type
, array_size
,
1388 for (index
= 0; index
< num_inputs
; ++index
) {
1389 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1390 LLVMValueRef lindex
=
1391 lp_build_const_int32(gallivm
, index
* 4 + chan
);
1392 LLVMValueRef input_ptr
=
1393 LLVMBuildGEP(gallivm
->builder
, bld
->inputs_array
,
1395 LLVMValueRef value
= bld
->inputs
[index
][chan
];
1397 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
1403 static void emit_vote(struct lp_build_nir_context
*bld_base
, LLVMValueRef src
, nir_intrinsic_instr
*instr
, LLVMValueRef result
[4])
1405 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1406 LLVMBuilderRef builder
= gallivm
->builder
;
1408 LLVMValueRef exec_mask
= mask_vec(bld_base
);
1409 struct lp_build_loop_state loop_state
;
1411 LLVMValueRef outer_cond
= LLVMBuildICmp(builder
, LLVMIntNE
, exec_mask
, bld_base
->uint_bld
.zero
, "");
1413 LLVMValueRef res_store
= lp_build_alloca(gallivm
, bld_base
->int_bld
.elem_type
, "");
1414 LLVMValueRef init_val
;
1415 if (instr
->intrinsic
== nir_intrinsic_vote_ieq
) {
1416 /* for equal we unfortunately have to loop and find the first valid one. */
1417 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
1418 LLVMValueRef if_cond
= LLVMBuildExtractElement(gallivm
->builder
, outer_cond
, loop_state
.counter
, "");
1420 struct lp_build_if_state ifthen
;
1421 lp_build_if(&ifthen
, gallivm
, if_cond
);
1422 LLVMValueRef value_ptr
= LLVMBuildExtractElement(gallivm
->builder
, src
,
1423 loop_state
.counter
, "");
1424 LLVMBuildStore(builder
, value_ptr
, res_store
);
1425 lp_build_endif(&ifthen
);
1426 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, bld_base
->uint_bld
.type
.length
),
1428 lp_build_print_value(gallivm
, "init_val is ", LLVMBuildLoad(builder
, res_store
, ""));
1429 init_val
= LLVMBuildLoad(builder
, res_store
, "");
1431 LLVMBuildStore(builder
, lp_build_const_int32(gallivm
, instr
->intrinsic
== nir_intrinsic_vote_any
? 0 : -1), res_store
);
1435 lp_build_loop_begin(&loop_state
, gallivm
, lp_build_const_int32(gallivm
, 0));
1436 LLVMValueRef value_ptr
= LLVMBuildExtractElement(gallivm
->builder
, src
,
1437 loop_state
.counter
, "");
1438 struct lp_build_if_state ifthen
;
1439 LLVMValueRef if_cond
;
1440 if_cond
= LLVMBuildExtractElement(gallivm
->builder
, outer_cond
, loop_state
.counter
, "");
1442 lp_build_if(&ifthen
, gallivm
, if_cond
);
1443 res
= LLVMBuildLoad(builder
, res_store
, "");
1445 if (instr
->intrinsic
== nir_intrinsic_vote_ieq
) {
1446 LLVMValueRef tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, init_val
, value_ptr
, "");
1447 tmp
= LLVMBuildSExt(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
1448 res
= LLVMBuildOr(builder
, res
, tmp
, "");
1449 } else if (instr
->intrinsic
== nir_intrinsic_vote_any
)
1450 res
= LLVMBuildOr(builder
, res
, value_ptr
, "");
1452 res
= LLVMBuildAnd(builder
, res
, value_ptr
, "");
1453 LLVMBuildStore(builder
, res
, res_store
);
1454 lp_build_endif(&ifthen
);
1455 lp_build_loop_end_cond(&loop_state
, lp_build_const_int32(gallivm
, bld_base
->uint_bld
.type
.length
),
1457 result
[0] = lp_build_broadcast_scalar(&bld_base
->uint_bld
, LLVMBuildLoad(builder
, res_store
, ""));
1460 void lp_build_nir_soa(struct gallivm_state
*gallivm
,
1461 struct nir_shader
*shader
,
1462 const struct lp_build_tgsi_params
*params
,
1463 LLVMValueRef (*outputs
)[4])
1465 struct lp_build_nir_soa_context bld
;
1466 struct lp_type type
= params
->type
;
1467 struct lp_type res_type
;
1469 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
1470 memset(&res_type
, 0, sizeof res_type
);
1471 res_type
.width
= type
.width
;
1472 res_type
.length
= type
.length
;
1475 /* Setup build context */
1476 memset(&bld
, 0, sizeof bld
);
1477 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
1478 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
1479 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
1480 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
1481 lp_build_context_init(&bld
.uint_elem_bld
, gallivm
, lp_elem_type(lp_uint_type(type
)));
1483 struct lp_type dbl_type
;
1485 dbl_type
.width
*= 2;
1486 lp_build_context_init(&bld
.bld_base
.dbl_bld
, gallivm
, dbl_type
);
1489 struct lp_type uint64_type
;
1490 uint64_type
= lp_uint_type(type
);
1491 uint64_type
.width
*= 2;
1492 lp_build_context_init(&bld
.bld_base
.uint64_bld
, gallivm
, uint64_type
);
1495 struct lp_type int64_type
;
1496 int64_type
= lp_int_type(type
);
1497 int64_type
.width
*= 2;
1498 lp_build_context_init(&bld
.bld_base
.int64_bld
, gallivm
, int64_type
);
1501 struct lp_type uint16_type
;
1502 uint16_type
= lp_uint_type(type
);
1503 uint16_type
.width
/= 2;
1504 lp_build_context_init(&bld
.bld_base
.uint16_bld
, gallivm
, uint16_type
);
1507 struct lp_type int16_type
;
1508 int16_type
= lp_int_type(type
);
1509 int16_type
.width
/= 2;
1510 lp_build_context_init(&bld
.bld_base
.int16_bld
, gallivm
, int16_type
);
1513 struct lp_type uint8_type
;
1514 uint8_type
= lp_uint_type(type
);
1515 uint8_type
.width
/= 4;
1516 lp_build_context_init(&bld
.bld_base
.uint8_bld
, gallivm
, uint8_type
);
1519 struct lp_type int8_type
;
1520 int8_type
= lp_int_type(type
);
1521 int8_type
.width
/= 4;
1522 lp_build_context_init(&bld
.bld_base
.int8_bld
, gallivm
, int8_type
);
1524 bld
.bld_base
.load_var
= emit_load_var
;
1525 bld
.bld_base
.store_var
= emit_store_var
;
1526 bld
.bld_base
.load_reg
= emit_load_reg
;
1527 bld
.bld_base
.store_reg
= emit_store_reg
;
1528 bld
.bld_base
.emit_var_decl
= emit_var_decl
;
1529 bld
.bld_base
.load_ubo
= emit_load_ubo
;
1530 bld
.bld_base
.load_kernel_arg
= emit_load_kernel_arg
;
1531 bld
.bld_base
.load_global
= emit_load_global
;
1532 bld
.bld_base
.store_global
= emit_store_global
;
1533 bld
.bld_base
.atomic_global
= emit_atomic_global
;
1534 bld
.bld_base
.tex
= emit_tex
;
1535 bld
.bld_base
.tex_size
= emit_tex_size
;
1536 bld
.bld_base
.bgnloop
= bgnloop
;
1537 bld
.bld_base
.endloop
= endloop
;
1538 bld
.bld_base
.if_cond
= if_cond
;
1539 bld
.bld_base
.else_stmt
= else_stmt
;
1540 bld
.bld_base
.endif_stmt
= endif_stmt
;
1541 bld
.bld_base
.break_stmt
= break_stmt
;
1542 bld
.bld_base
.continue_stmt
= continue_stmt
;
1543 bld
.bld_base
.sysval_intrin
= emit_sysval_intrin
;
1544 bld
.bld_base
.discard
= discard
;
1545 bld
.bld_base
.emit_vertex
= emit_vertex
;
1546 bld
.bld_base
.end_primitive
= end_primitive
;
1547 bld
.bld_base
.load_mem
= emit_load_mem
;
1548 bld
.bld_base
.store_mem
= emit_store_mem
;
1549 bld
.bld_base
.get_buffer_size
= emit_get_buffer_size
;
1550 bld
.bld_base
.atomic_mem
= emit_atomic_mem
;
1551 bld
.bld_base
.barrier
= emit_barrier
;
1552 bld
.bld_base
.image_op
= emit_image_op
;
1553 bld
.bld_base
.image_size
= emit_image_size
;
1554 bld
.bld_base
.vote
= emit_vote
;
1556 bld
.mask
= params
->mask
;
1557 bld
.inputs
= params
->inputs
;
1558 bld
.outputs
= outputs
;
1559 bld
.consts_ptr
= params
->consts_ptr
;
1560 bld
.const_sizes_ptr
= params
->const_sizes_ptr
;
1561 bld
.ssbo_ptr
= params
->ssbo_ptr
;
1562 bld
.ssbo_sizes_ptr
= params
->ssbo_sizes_ptr
;
1563 bld
.sampler
= params
->sampler
;
1564 // bld.bld_base.info = params->info;
1566 bld
.context_ptr
= params
->context_ptr
;
1567 bld
.thread_data_ptr
= params
->thread_data_ptr
;
1568 bld
.image
= params
->image
;
1569 bld
.shared_ptr
= params
->shared_ptr
;
1570 bld
.coro
= params
->coro
;
1571 bld
.kernel_args_ptr
= params
->kernel_args
;
1573 if (params
->info
->indirect_files
& (1 << TGSI_FILE_INPUT
))
1574 bld
.indirects
|= nir_var_shader_in
;
1576 bld
.gs_iface
= params
->gs_iface
;
1578 struct lp_build_context
*uint_bld
= &bld
.bld_base
.uint_bld
;
1580 bld
.max_output_vertices_vec
= lp_build_const_int_vec(gallivm
, bld
.bld_base
.int_bld
.type
,
1581 shader
->info
.gs
.vertices_out
);
1582 bld
.emitted_prims_vec_ptr
=
1583 lp_build_alloca(gallivm
, uint_bld
->vec_type
, "emitted_prims_ptr");
1584 bld
.emitted_vertices_vec_ptr
=
1585 lp_build_alloca(gallivm
, uint_bld
->vec_type
, "emitted_vertices_ptr");
1586 bld
.total_emitted_vertices_vec_ptr
=
1587 lp_build_alloca(gallivm
, uint_bld
->vec_type
, "total_emitted_vertices_ptr");
1589 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.int_bld
);
1591 bld
.system_values
= *params
->system_values
;
1593 bld
.bld_base
.shader
= shader
;
1595 emit_prologue(&bld
);
1596 lp_build_nir_llvm(&bld
.bld_base
, shader
);
1599 LLVMBuilderRef builder
= bld
.bld_base
.base
.gallivm
->builder
;
1600 LLVMValueRef total_emitted_vertices_vec
;
1601 LLVMValueRef emitted_prims_vec
;
1602 end_primitive_masked(&bld
.bld_base
, lp_build_mask_value(bld
.mask
));
1603 total_emitted_vertices_vec
=
1604 LLVMBuildLoad(builder
, bld
.total_emitted_vertices_vec_ptr
, "");
1606 LLVMBuildLoad(builder
, bld
.emitted_prims_vec_ptr
, "");
1608 bld
.gs_iface
->gs_epilogue(bld
.gs_iface
,
1609 total_emitted_vertices_vec
,
1612 lp_exec_mask_fini(&bld
.exec_mask
);