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_arit.h"
28 #include "lp_bld_bitarit.h"
29 #include "lp_bld_const.h"
30 #include "lp_bld_gather.h"
31 #include "lp_bld_logic.h"
32 #include "lp_bld_quad.h"
33 #include "lp_bld_flow.h"
34 #include "lp_bld_struct.h"
35 #include "lp_bld_debug.h"
36 #include "lp_bld_printf.h"
37 #include "nir_deref.h"
39 static void visit_cf_list(struct lp_build_nir_context
*bld_base
,
40 struct exec_list
*list
);
42 static LLVMValueRef
cast_type(struct lp_build_nir_context
*bld_base
, LLVMValueRef val
,
43 nir_alu_type alu_type
, unsigned bit_size
)
45 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
50 return LLVMBuildBitCast(builder
, val
, bld_base
->base
.vec_type
, "");
52 return LLVMBuildBitCast(builder
, val
, bld_base
->dbl_bld
.vec_type
, "");
61 return LLVMBuildBitCast(builder
, val
, bld_base
->int_bld
.vec_type
, "");
63 return LLVMBuildBitCast(builder
, val
, bld_base
->int64_bld
.vec_type
, "");
72 return LLVMBuildBitCast(builder
, val
, bld_base
->uint_bld
.vec_type
, "");
74 return LLVMBuildBitCast(builder
, val
, bld_base
->uint64_bld
.vec_type
, "");
81 return LLVMBuildBitCast(builder
, val
, bld_base
->uint_bld
.vec_type
, "");
88 static struct lp_build_context
*get_int_bld(struct lp_build_nir_context
*bld_base
,
93 if (op_bit_size
== 64)
94 return &bld_base
->uint64_bld
;
96 return &bld_base
->uint_bld
;
97 else if (op_bit_size
== 64)
98 return &bld_base
->int64_bld
;
100 return &bld_base
->int_bld
;
103 static struct lp_build_context
*get_flt_bld(struct lp_build_nir_context
*bld_base
,
104 unsigned op_bit_size
)
106 if (op_bit_size
== 64)
107 return &bld_base
->dbl_bld
;
109 return &bld_base
->base
;
112 static unsigned glsl_sampler_to_pipe(int sampler_dim
, bool is_array
)
114 unsigned pipe_target
= PIPE_BUFFER
;
115 switch (sampler_dim
) {
116 case GLSL_SAMPLER_DIM_1D
:
117 pipe_target
= is_array
? PIPE_TEXTURE_1D_ARRAY
: PIPE_TEXTURE_1D
;
119 case GLSL_SAMPLER_DIM_2D
:
120 pipe_target
= is_array
? PIPE_TEXTURE_2D_ARRAY
: PIPE_TEXTURE_2D
;
122 case GLSL_SAMPLER_DIM_3D
:
123 pipe_target
= PIPE_TEXTURE_3D
;
125 case GLSL_SAMPLER_DIM_CUBE
:
126 pipe_target
= is_array
? PIPE_TEXTURE_CUBE_ARRAY
: PIPE_TEXTURE_CUBE
;
128 case GLSL_SAMPLER_DIM_RECT
:
129 pipe_target
= PIPE_TEXTURE_RECT
;
131 case GLSL_SAMPLER_DIM_BUF
:
132 pipe_target
= PIPE_BUFFER
;
140 static LLVMValueRef
get_ssa_src(struct lp_build_nir_context
*bld_base
, nir_ssa_def
*ssa
)
142 return bld_base
->ssa_defs
[ssa
->index
];
145 static LLVMValueRef
get_src(struct lp_build_nir_context
*bld_base
, nir_src src
);
147 static LLVMValueRef
get_reg_src(struct lp_build_nir_context
*bld_base
, nir_reg_src src
)
149 struct hash_entry
*entry
= _mesa_hash_table_search(bld_base
->regs
, src
.reg
);
150 LLVMValueRef reg_storage
= (LLVMValueRef
)entry
->data
;
151 struct lp_build_context
*reg_bld
= get_int_bld(bld_base
, true, src
.reg
->bit_size
);
152 LLVMValueRef indir_src
= NULL
;
154 indir_src
= get_src(bld_base
, *src
.indirect
);
155 return bld_base
->load_reg(bld_base
, reg_bld
, &src
, indir_src
, reg_storage
);
158 static LLVMValueRef
get_src(struct lp_build_nir_context
*bld_base
, nir_src src
)
161 return get_ssa_src(bld_base
, src
.ssa
);
163 return get_reg_src(bld_base
, src
.reg
);
166 static void assign_ssa(struct lp_build_nir_context
*bld_base
, int idx
, LLVMValueRef ptr
)
168 bld_base
->ssa_defs
[idx
] = ptr
;
171 static void assign_ssa_dest(struct lp_build_nir_context
*bld_base
, const nir_ssa_def
*ssa
,
172 LLVMValueRef vals
[4])
174 assign_ssa(bld_base
, ssa
->index
, ssa
->num_components
== 1 ? vals
[0] : lp_nir_array_build_gather_values(bld_base
->base
.gallivm
->builder
, vals
, ssa
->num_components
));
177 static void assign_reg(struct lp_build_nir_context
*bld_base
, const nir_reg_dest
*reg
,
179 LLVMValueRef vals
[4])
181 struct hash_entry
*entry
= _mesa_hash_table_search(bld_base
->regs
, reg
->reg
);
182 LLVMValueRef reg_storage
= (LLVMValueRef
)entry
->data
;
183 struct lp_build_context
*reg_bld
= get_int_bld(bld_base
, true, reg
->reg
->bit_size
);
184 LLVMValueRef indir_src
= NULL
;
186 indir_src
= get_src(bld_base
, *reg
->indirect
);
187 bld_base
->store_reg(bld_base
, reg_bld
, reg
, write_mask
? write_mask
: 0xf, indir_src
, reg_storage
, vals
);
190 static void assign_dest(struct lp_build_nir_context
*bld_base
, const nir_dest
*dest
, LLVMValueRef vals
[4])
193 assign_ssa_dest(bld_base
, &dest
->ssa
, vals
);
195 assign_reg(bld_base
, &dest
->reg
, 0, vals
);
198 static void assign_alu_dest(struct lp_build_nir_context
*bld_base
, const nir_alu_dest
*dest
, LLVMValueRef vals
[4])
200 if (dest
->dest
.is_ssa
)
201 assign_ssa_dest(bld_base
, &dest
->dest
.ssa
, vals
);
203 assign_reg(bld_base
, &dest
->dest
.reg
, dest
->write_mask
, vals
);
206 static LLVMValueRef
int_to_bool32(struct lp_build_nir_context
*bld_base
,
207 uint32_t src_bit_size
,
211 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
212 struct lp_build_context
*int_bld
= get_int_bld(bld_base
, is_unsigned
, src_bit_size
);
213 LLVMValueRef result
= lp_build_compare(bld_base
->base
.gallivm
, int_bld
->type
, PIPE_FUNC_NOTEQUAL
, val
, int_bld
->zero
);
214 if (src_bit_size
== 64)
215 result
= LLVMBuildTrunc(builder
, result
, bld_base
->int_bld
.vec_type
, "");
219 static LLVMValueRef
flt_to_bool32(struct lp_build_nir_context
*bld_base
,
220 uint32_t src_bit_size
,
223 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
224 struct lp_build_context
*flt_bld
= get_flt_bld(bld_base
, src_bit_size
);
225 LLVMValueRef result
= lp_build_cmp(flt_bld
, PIPE_FUNC_NOTEQUAL
, val
, flt_bld
->zero
);
226 if (src_bit_size
== 64)
227 result
= LLVMBuildTrunc(builder
, result
, bld_base
->int_bld
.vec_type
, "");
231 static LLVMValueRef
fcmp32(struct lp_build_nir_context
*bld_base
,
232 enum pipe_compare_func compare
,
233 uint32_t src_bit_size
,
236 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
237 struct lp_build_context
*flt_bld
= get_flt_bld(bld_base
, src_bit_size
);
240 if (compare
!= PIPE_FUNC_NOTEQUAL
)
241 result
= lp_build_cmp_ordered(flt_bld
, compare
, src
[0], src
[1]);
243 result
= lp_build_cmp(flt_bld
, compare
, src
[0], src
[1]);
244 if (src_bit_size
== 64)
245 result
= LLVMBuildTrunc(builder
, result
, bld_base
->int_bld
.vec_type
, "");
249 static LLVMValueRef
icmp32(struct lp_build_nir_context
*bld_base
,
250 enum pipe_compare_func compare
,
252 uint32_t src_bit_size
,
255 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
256 struct lp_build_context
*i_bld
= get_int_bld(bld_base
, is_unsigned
, src_bit_size
);
257 LLVMValueRef result
= lp_build_cmp(i_bld
, compare
, src
[0], src
[1]);
258 if (src_bit_size
== 64)
259 result
= LLVMBuildTrunc(builder
, result
, bld_base
->int_bld
.vec_type
, "");
263 static LLVMValueRef
get_alu_src(struct lp_build_nir_context
*bld_base
,
265 unsigned num_components
)
267 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
268 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
269 LLVMValueRef value
= get_src(bld_base
, src
.src
);
270 bool need_swizzle
= false;
273 unsigned src_components
= nir_src_num_components(src
.src
);
274 for (unsigned i
= 0; i
< num_components
; ++i
) {
275 assert(src
.swizzle
[i
] < src_components
);
276 if (src
.swizzle
[i
] != i
)
280 if (need_swizzle
|| num_components
!= src_components
) {
281 if (src_components
> 1 && num_components
== 1) {
282 value
= LLVMBuildExtractValue(gallivm
->builder
, value
,
284 } else if (src_components
== 1 && num_components
> 1) {
285 LLVMValueRef values
[] = {value
, value
, value
, value
};
286 value
= lp_nir_array_build_gather_values(builder
, values
, num_components
);
288 LLVMValueRef arr
= LLVMGetUndef(LLVMArrayType(LLVMTypeOf(LLVMBuildExtractValue(builder
, value
, 0, "")), num_components
));
289 for (unsigned i
= 0; i
< num_components
; i
++)
290 arr
= LLVMBuildInsertValue(builder
, arr
, LLVMBuildExtractValue(builder
, value
, src
.swizzle
[i
], ""), i
, "");
299 static LLVMValueRef
emit_b2f(struct lp_build_nir_context
*bld_base
,
303 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
304 LLVMValueRef result
= LLVMBuildAnd(builder
, cast_type(bld_base
, src0
, nir_type_int
, 32),
305 LLVMBuildBitCast(builder
, lp_build_const_vec(bld_base
->base
.gallivm
, bld_base
->base
.type
,
306 1.0), bld_base
->int_bld
.vec_type
, ""),
308 result
= LLVMBuildBitCast(builder
, result
, bld_base
->base
.vec_type
, "");
313 result
= LLVMBuildFPExt(builder
, result
, bld_base
->dbl_bld
.vec_type
, "");
316 unreachable("unsupported bit size.");
321 static LLVMValueRef
emit_b2i(struct lp_build_nir_context
*bld_base
,
325 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
326 LLVMValueRef result
= LLVMBuildAnd(builder
, cast_type(bld_base
, src0
, nir_type_int
, 32),
327 lp_build_const_int_vec(bld_base
->base
.gallivm
, bld_base
->base
.type
, 1), "");
332 return LLVMBuildZExt(builder
, result
, bld_base
->int64_bld
.vec_type
, "");
334 unreachable("unsupported bit size.");
338 static LLVMValueRef
emit_b32csel(struct lp_build_nir_context
*bld_base
,
339 unsigned src_bit_size
[4],
342 LLVMValueRef sel
= cast_type(bld_base
, src
[0], nir_type_int
, 32);
343 LLVMValueRef v
= lp_build_compare(bld_base
->base
.gallivm
, bld_base
->int_bld
.type
, PIPE_FUNC_NOTEQUAL
, sel
, bld_base
->int_bld
.zero
);
344 struct lp_build_context
*bld
= get_int_bld(bld_base
, false, src_bit_size
[1]);
345 return lp_build_select(bld
, v
, src
[1], src
[2]);
348 static LLVMValueRef
split_64bit(struct lp_build_nir_context
*bld_base
,
352 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
353 LLVMValueRef shuffles
[LP_MAX_VECTOR_WIDTH
/32];
354 LLVMValueRef shuffles2
[LP_MAX_VECTOR_WIDTH
/32];
355 int len
= bld_base
->base
.type
.length
* 2;
356 for (unsigned i
= 0; i
< bld_base
->base
.type
.length
; i
++) {
357 shuffles
[i
] = lp_build_const_int32(gallivm
, i
* 2);
358 shuffles2
[i
] = lp_build_const_int32(gallivm
, (i
* 2) + 1);
361 src
= LLVMBuildBitCast(gallivm
->builder
, src
, LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), len
), "");
362 return LLVMBuildShuffleVector(gallivm
->builder
, src
,
363 LLVMGetUndef(LLVMTypeOf(src
)),
364 LLVMConstVector(hi
? shuffles2
: shuffles
,
365 bld_base
->base
.type
.length
),
370 merge_64bit(struct lp_build_nir_context
*bld_base
,
374 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
375 LLVMBuilderRef builder
= gallivm
->builder
;
377 LLVMValueRef shuffles
[2 * (LP_MAX_VECTOR_WIDTH
/32)];
378 int len
= bld_base
->base
.type
.length
* 2;
379 assert(len
<= (2 * (LP_MAX_VECTOR_WIDTH
/32)));
381 for (i
= 0; i
< bld_base
->base
.type
.length
* 2; i
+=2) {
382 shuffles
[i
] = lp_build_const_int32(gallivm
, i
/ 2);
383 shuffles
[i
+ 1] = lp_build_const_int32(gallivm
, i
/ 2 + bld_base
->base
.type
.length
);
385 return LLVMBuildShuffleVector(builder
, input
, input2
, LLVMConstVector(shuffles
, len
), "");
389 do_int_divide(struct lp_build_nir_context
*bld_base
,
390 bool is_unsigned
, unsigned src_bit_size
,
391 LLVMValueRef src
, LLVMValueRef src2
)
393 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
394 LLVMBuilderRef builder
= gallivm
->builder
;
395 struct lp_build_context
*int_bld
= get_int_bld(bld_base
, is_unsigned
, src_bit_size
);
396 LLVMValueRef div_mask
= lp_build_cmp(int_bld
, PIPE_FUNC_EQUAL
, src2
,
398 LLVMValueRef divisor
= LLVMBuildOr(builder
,
401 LLVMValueRef result
= lp_build_div(int_bld
, src
, divisor
);
402 /* udiv by zero is guaranteed to return 0xffffffff at least with d3d10
403 * may as well do same for idiv */
404 return LLVMBuildOr(builder
, div_mask
, result
, "");
407 static LLVMValueRef
do_alu_action(struct lp_build_nir_context
*bld_base
,
408 nir_op op
, unsigned src_bit_size
[4], LLVMValueRef src
[4])
410 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
411 LLVMBuilderRef builder
= gallivm
->builder
;
415 result
= emit_b2f(bld_base
, src
[0], 32);
418 result
= emit_b2f(bld_base
, src
[0], 64);
421 result
= emit_b2i(bld_base
, src
[0], 32);
424 result
= emit_b2i(bld_base
, src
[0], 64);
427 result
= emit_b32csel(bld_base
, src_bit_size
, src
);
429 case nir_op_bit_count
:
430 result
= lp_build_popcount(get_int_bld(bld_base
, false, src_bit_size
[0]), src
[0]);
432 case nir_op_bitfield_select
:
433 result
= lp_build_xor(&bld_base
->uint_bld
, src
[2], lp_build_and(&bld_base
->uint_bld
, src
[0], lp_build_xor(&bld_base
->uint_bld
, src
[1], src
[2])));
435 case nir_op_bitfield_reverse
:
436 result
= lp_build_bitfield_reverse(get_int_bld(bld_base
, false, src_bit_size
[0]), src
[0]);
439 result
= flt_to_bool32(bld_base
, src_bit_size
[0], src
[0]);
442 result
= LLVMBuildFPTrunc(builder
, src
[0],
443 bld_base
->base
.vec_type
, "");
446 result
= LLVMBuildFPExt(builder
, src
[0],
447 bld_base
->dbl_bld
.vec_type
, "");
450 result
= LLVMBuildFPToSI(builder
, src
[0], bld_base
->base
.int_vec_type
, "");
453 result
= LLVMBuildFPToUI(builder
,
455 bld_base
->base
.int_vec_type
, "");
458 result
= LLVMBuildFPToSI(builder
,
460 bld_base
->int64_bld
.vec_type
, "");
463 result
= LLVMBuildFPToUI(builder
,
465 bld_base
->uint64_bld
.vec_type
, "");
468 result
= lp_build_abs(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
471 result
= lp_build_add(get_flt_bld(bld_base
, src_bit_size
[0]),
475 result
= lp_build_ceil(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
478 result
= lp_build_cos(&bld_base
->base
, src
[0]);
481 result
= lp_build_ddx(&bld_base
->base
, src
[0]);
484 result
= lp_build_ddy(&bld_base
->base
, src
[0]);
487 result
= lp_build_div(get_flt_bld(bld_base
, src_bit_size
[0]),
491 result
= fcmp32(bld_base
, PIPE_FUNC_EQUAL
, src_bit_size
[0], src
);
494 result
= lp_build_exp2(&bld_base
->base
, src
[0]);
497 result
= lp_build_floor(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
500 result
= lp_build_fmuladd(builder
, src
[0], src
[1], src
[2]);
502 case nir_op_ffract
: {
503 struct lp_build_context
*flt_bld
= get_flt_bld(bld_base
, src_bit_size
[0]);
504 LLVMValueRef tmp
= lp_build_floor(flt_bld
, src
[0]);
505 result
= lp_build_sub(flt_bld
, src
[0], tmp
);
509 result
= fcmp32(bld_base
, PIPE_FUNC_GEQUAL
, src_bit_size
[0], src
);
511 case nir_op_find_lsb
:
512 result
= lp_build_cttz(get_int_bld(bld_base
, false, src_bit_size
[0]), src
[0]);
515 result
= lp_build_log2_safe(&bld_base
->base
, src
[0]);
518 result
= fcmp32(bld_base
, PIPE_FUNC_LESS
, src_bit_size
[0], src
);
521 result
= lp_build_min(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0], src
[1]);
524 struct lp_build_context
*flt_bld
= get_flt_bld(bld_base
, src_bit_size
[0]);
525 result
= lp_build_div(flt_bld
, src
[0], src
[1]);
526 result
= lp_build_floor(flt_bld
, result
);
527 result
= lp_build_mul(flt_bld
, src
[1], result
);
528 result
= lp_build_sub(flt_bld
, src
[0], result
);
532 result
= lp_build_mul(get_flt_bld(bld_base
, src_bit_size
[0]),
536 result
= lp_build_max(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0], src
[1]);
539 result
= fcmp32(bld_base
, PIPE_FUNC_NOTEQUAL
, src_bit_size
[0], src
);
542 result
= lp_build_negate(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
545 result
= lp_build_pow(&bld_base
->base
, src
[0], src
[1]);
548 result
= lp_build_rcp(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
550 case nir_op_fround_even
:
551 result
= lp_build_round(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
554 result
= lp_build_rsqrt(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
557 result
= lp_build_clamp_zero_one_nanzero(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
560 result
= lp_build_sgn(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
563 result
= lp_build_sin(&bld_base
->base
, src
[0]);
566 result
= lp_build_sqrt(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
569 result
= lp_build_trunc(get_flt_bld(bld_base
, src_bit_size
[0]), src
[0]);
572 result
= int_to_bool32(bld_base
, src_bit_size
[0], false, src
[0]);
575 result
= lp_build_int_to_float(&bld_base
->base
, src
[0]);
578 result
= lp_build_int_to_float(&bld_base
->dbl_bld
, src
[0]);
581 result
= LLVMBuildTrunc(builder
, src
[0], bld_base
->int_bld
.vec_type
, "");
584 result
= LLVMBuildSExt(builder
, src
[0], bld_base
->int64_bld
.vec_type
, "");
587 result
= lp_build_abs(&bld_base
->int_bld
, src
[0]);
590 result
= lp_build_add(get_int_bld(bld_base
, false, src_bit_size
[0]),
594 result
= lp_build_and(get_int_bld(bld_base
, false, src_bit_size
[0]),
598 result
= do_int_divide(bld_base
, false, src_bit_size
[0], src
[0], src
[1]);
601 result
= icmp32(bld_base
, PIPE_FUNC_EQUAL
, false, src_bit_size
[0], src
);
604 result
= icmp32(bld_base
, PIPE_FUNC_GEQUAL
, false, src_bit_size
[0], src
);
607 result
= icmp32(bld_base
, PIPE_FUNC_LESS
, false, src_bit_size
[0], src
);
610 result
= lp_build_max(&bld_base
->int_bld
, src
[0], src
[1]);
613 result
= lp_build_min(&bld_base
->int_bld
, src
[0], src
[1]);
616 result
= lp_build_mul(&bld_base
->int_bld
,
619 case nir_op_imul_high
: {
620 LLVMValueRef hi_bits
;
621 lp_build_mul_32_lohi(&bld_base
->int_bld
, src
[0], src
[1], &hi_bits
);
626 result
= icmp32(bld_base
, PIPE_FUNC_NOTEQUAL
, false, src_bit_size
[0], src
);
629 result
= lp_build_negate(get_int_bld(bld_base
, false, src_bit_size
[0]), src
[0]);
632 result
= lp_build_not(get_int_bld(bld_base
, false, src_bit_size
[0]), src
[0]);
635 result
= lp_build_or(get_int_bld(bld_base
, false, src_bit_size
[0]),
639 src
[1] = lp_build_and(&bld_base
->uint_bld
, src
[1], lp_build_const_int_vec(gallivm
, bld_base
->uint_bld
.type
, (src_bit_size
[0] - 1)));
640 result
= lp_build_shl(&bld_base
->int_bld
, src
[0], src
[1]);
643 src
[1] = lp_build_and(&bld_base
->uint_bld
, src
[1], lp_build_const_int_vec(gallivm
, bld_base
->uint_bld
.type
, (src_bit_size
[0] - 1)));
644 result
= lp_build_shr(&bld_base
->int_bld
, src
[0], src
[1]);
647 result
= lp_build_sgn(&bld_base
->int_bld
, src
[0]);
650 result
= lp_build_xor(get_int_bld(bld_base
, false, src_bit_size
[0]),
656 case nir_op_unpack_64_2x32_split_x
:
657 result
= split_64bit(bld_base
, src
[0], false);
659 case nir_op_unpack_64_2x32_split_y
:
660 result
= split_64bit(bld_base
, src
[0], true);
663 case nir_op_pack_64_2x32_split
: {
664 LLVMValueRef tmp
= merge_64bit(bld_base
, src
[0], src
[1]);
665 result
= LLVMBuildBitCast(builder
, tmp
, bld_base
->dbl_bld
.vec_type
, "");
669 result
= LLVMBuildUIToFP(builder
, src
[0], bld_base
->base
.vec_type
, "");
672 result
= LLVMBuildUIToFP(builder
, src
[0], bld_base
->dbl_bld
.vec_type
, "");
675 result
= LLVMBuildTrunc(builder
, src
[0], bld_base
->uint_bld
.vec_type
, "");
678 result
= LLVMBuildZExt(builder
, src
[0], bld_base
->uint64_bld
.vec_type
, "");
681 result
= do_int_divide(bld_base
, true, src_bit_size
[0], src
[0], src
[1]);
683 case nir_op_ufind_msb
: {
684 struct lp_build_context
*uint_bld
= get_int_bld(bld_base
, true, src_bit_size
[0]);
685 result
= lp_build_ctlz(uint_bld
, src
[0]);
686 result
= lp_build_sub(uint_bld
, lp_build_const_int_vec(gallivm
, uint_bld
->type
, src_bit_size
[0] - 1), result
);
690 result
= icmp32(bld_base
, PIPE_FUNC_GEQUAL
, true, src_bit_size
[0], src
);
693 result
= icmp32(bld_base
, PIPE_FUNC_LESS
, true, src_bit_size
[0], src
);
696 result
= lp_build_max(&bld_base
->uint_bld
, src
[0], src
[1]);
699 result
= lp_build_min(&bld_base
->uint_bld
, src
[0], src
[1]);
702 result
= lp_build_mod(&bld_base
->uint_bld
, src
[0], src
[1]);
704 case nir_op_umul_high
: {
705 LLVMValueRef hi_bits
;
706 lp_build_mul_32_lohi(&bld_base
->uint_bld
, src
[0], src
[1], &hi_bits
);
711 src
[1] = lp_build_and(&bld_base
->uint_bld
, src
[1], lp_build_const_int_vec(gallivm
, bld_base
->uint_bld
.type
, (src_bit_size
[0] - 1)));
712 result
= lp_build_shr(&bld_base
->uint_bld
, src
[0], src
[1]);
721 static void visit_alu(struct lp_build_nir_context
*bld_base
, const nir_alu_instr
*instr
)
723 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
725 unsigned src_bit_size
[4];
726 unsigned num_components
= nir_dest_num_components(instr
->dest
.dest
);
727 unsigned src_components
;
734 case nir_op_pack_half_2x16
:
737 case nir_op_unpack_half_2x16
:
740 case nir_op_cube_face_coord
:
741 case nir_op_cube_face_index
:
745 src_components
= num_components
;
748 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
749 src
[i
] = get_alu_src(bld_base
, instr
->src
[i
], src_components
);
750 src_bit_size
[i
] = nir_src_bit_size(instr
->src
[i
].src
);
753 LLVMValueRef result
[4];
754 if (instr
->op
== nir_op_vec4
|| instr
->op
== nir_op_vec3
|| instr
->op
== nir_op_vec2
) {
755 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
756 result
[i
] = cast_type(bld_base
, src
[i
], nir_op_infos
[instr
->op
].input_types
[i
], src_bit_size
[i
]);
759 for (unsigned c
= 0; c
< num_components
; c
++) {
760 LLVMValueRef src_chan
[4];
762 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
763 if (num_components
> 1) {
764 src_chan
[i
] = LLVMBuildExtractValue(gallivm
->builder
,
767 src_chan
[i
] = src
[i
];
768 src_chan
[i
] = cast_type(bld_base
, src_chan
[i
], nir_op_infos
[instr
->op
].input_types
[i
], src_bit_size
[i
]);
770 result
[c
] = do_alu_action(bld_base
, instr
->op
, src_bit_size
, src_chan
);
771 result
[c
] = cast_type(bld_base
, result
[c
], nir_op_infos
[instr
->op
].output_type
, nir_dest_bit_size(instr
->dest
.dest
));
774 assign_alu_dest(bld_base
, &instr
->dest
, result
);
777 static void visit_load_const(struct lp_build_nir_context
*bld_base
,
778 const nir_load_const_instr
*instr
)
780 LLVMValueRef result
[4];
781 struct lp_build_context
*int_bld
= get_int_bld(bld_base
, true, instr
->def
.bit_size
);
782 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
783 result
[i
] = lp_build_const_int_vec(bld_base
->base
.gallivm
, int_bld
->type
, instr
->value
[i
].u64
);
784 assign_ssa_dest(bld_base
, &instr
->def
, result
);
788 get_deref_offset(struct lp_build_nir_context
*bld_base
, nir_deref_instr
*instr
,
789 bool vs_in
, unsigned *vertex_index_out
,
790 LLVMValueRef
*vertex_index_ref
,
791 unsigned *const_out
, LLVMValueRef
*indir_out
)
793 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
794 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
796 unsigned idx_lvl
= 1;
798 nir_deref_path_init(&path
, instr
, NULL
);
800 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
801 if (vertex_index_ref
) {
802 *vertex_index_ref
= get_src(bld_base
, path
.path
[idx_lvl
]->arr
.index
);
803 if (vertex_index_out
)
804 *vertex_index_out
= 0;
806 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
811 uint32_t const_offset
= 0;
812 LLVMValueRef offset
= NULL
;
814 if (var
->data
.compact
) {
815 assert(instr
->deref_type
== nir_deref_type_array
);
816 const_offset
= nir_src_as_uint(instr
->arr
.index
);
820 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
821 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
822 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
823 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
825 for (unsigned i
= 0; i
< index
; i
++) {
826 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
827 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
829 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
830 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
831 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
)) {
832 const_offset
+= nir_src_comp_as_int(path
.path
[idx_lvl
]->arr
.index
, 0) * size
;
834 LLVMValueRef idx_src
= get_src(bld_base
, path
.path
[idx_lvl
]->arr
.index
);
835 idx_src
= cast_type(bld_base
, idx_src
, nir_type_uint
, 32);
836 LLVMValueRef array_off
= lp_build_mul(&bld_base
->uint_bld
, lp_build_const_int_vec(bld_base
->base
.gallivm
, bld_base
->base
.type
, size
),
839 offset
= lp_build_add(&bld_base
->uint_bld
, offset
, array_off
);
844 unreachable("Uhandled deref type in get_deref_instr_offset");
848 nir_deref_path_finish(&path
);
850 if (const_offset
&& offset
)
851 offset
= LLVMBuildAdd(builder
, offset
,
852 lp_build_const_int_vec(bld_base
->base
.gallivm
, bld_base
->uint_bld
.type
, const_offset
),
854 *const_out
= const_offset
;
858 static void visit_load_var(struct lp_build_nir_context
*bld_base
,
859 nir_intrinsic_instr
*instr
,
860 LLVMValueRef result
[4])
862 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
863 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
864 nir_variable_mode mode
= deref
->mode
;
865 unsigned const_index
;
866 LLVMValueRef indir_index
;
867 unsigned vertex_index
= 0;
868 unsigned nc
= nir_dest_num_components(instr
->dest
);
869 unsigned bit_size
= nir_dest_bit_size(instr
->dest
);
871 bool vs_in
= bld_base
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
872 var
->data
.mode
== nir_var_shader_in
;
873 bool gs_in
= bld_base
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
&&
874 var
->data
.mode
== nir_var_shader_in
;
875 mode
= var
->data
.mode
;
877 get_deref_offset(bld_base
, deref
, vs_in
, gs_in
? &vertex_index
: NULL
, NULL
,
878 &const_index
, &indir_index
);
880 bld_base
->load_var(bld_base
, mode
, nc
, bit_size
, var
, vertex_index
, const_index
, indir_index
, result
);
884 visit_store_var(struct lp_build_nir_context
*bld_base
,
885 nir_intrinsic_instr
*instr
)
887 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
888 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
889 nir_variable_mode mode
= deref
->mode
;
890 int writemask
= instr
->const_index
[0];
891 unsigned bit_size
= nir_src_bit_size(instr
->src
[1]);
892 LLVMValueRef src
= get_src(bld_base
, instr
->src
[1]);
893 unsigned const_index
= 0;
894 LLVMValueRef indir_index
;
896 get_deref_offset(bld_base
, deref
, false, NULL
, NULL
,
897 &const_index
, &indir_index
);
898 bld_base
->store_var(bld_base
, mode
, bit_size
, instr
->num_components
, writemask
, const_index
, var
, src
);
901 static void visit_load_ubo(struct lp_build_nir_context
*bld_base
,
902 nir_intrinsic_instr
*instr
,
903 LLVMValueRef result
[4])
905 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
906 LLVMBuilderRef builder
= gallivm
->builder
;
907 LLVMValueRef idx
= get_src(bld_base
, instr
->src
[0]);
908 LLVMValueRef offset
= get_src(bld_base
, instr
->src
[1]);
910 bool offset_is_uniform
= nir_src_is_dynamically_uniform(instr
->src
[1]);
911 idx
= LLVMBuildExtractElement(builder
, idx
, lp_build_const_int32(gallivm
, 0), "");
912 bld_base
->load_ubo(bld_base
, nir_dest_num_components(instr
->dest
), nir_dest_bit_size(instr
->dest
),
913 offset_is_uniform
, idx
, offset
, result
);
917 static void visit_load_ssbo(struct lp_build_nir_context
*bld_base
,
918 nir_intrinsic_instr
*instr
,
919 LLVMValueRef result
[4])
921 LLVMValueRef idx
= get_src(bld_base
, instr
->src
[0]);
922 LLVMValueRef offset
= get_src(bld_base
, instr
->src
[1]);
923 bld_base
->load_mem(bld_base
, nir_dest_num_components(instr
->dest
), nir_dest_bit_size(instr
->dest
),
924 idx
, offset
, result
);
927 static void visit_store_ssbo(struct lp_build_nir_context
*bld_base
,
928 nir_intrinsic_instr
*instr
)
930 LLVMValueRef val
= get_src(bld_base
, instr
->src
[0]);
931 LLVMValueRef idx
= get_src(bld_base
, instr
->src
[1]);
932 LLVMValueRef offset
= get_src(bld_base
, instr
->src
[2]);
933 int writemask
= instr
->const_index
[0];
934 int nc
= nir_src_num_components(instr
->src
[0]);
935 int bitsize
= nir_src_bit_size(instr
->src
[0]);
936 bld_base
->store_mem(bld_base
, writemask
, nc
, bitsize
, idx
, offset
, val
);
939 static void visit_get_buffer_size(struct lp_build_nir_context
*bld_base
,
940 nir_intrinsic_instr
*instr
,
941 LLVMValueRef result
[4])
943 LLVMValueRef idx
= get_src(bld_base
, instr
->src
[0]);
944 result
[0] = bld_base
->get_buffer_size(bld_base
, idx
);
947 static void visit_ssbo_atomic(struct lp_build_nir_context
*bld_base
,
948 nir_intrinsic_instr
*instr
,
949 LLVMValueRef result
[4])
951 LLVMValueRef idx
= get_src(bld_base
, instr
->src
[0]);
952 LLVMValueRef offset
= get_src(bld_base
, instr
->src
[1]);
953 LLVMValueRef val
= get_src(bld_base
, instr
->src
[2]);
954 LLVMValueRef val2
= NULL
;
955 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
956 val2
= get_src(bld_base
, instr
->src
[3]);
958 bld_base
->atomic_mem(bld_base
, instr
->intrinsic
, idx
, offset
, val
, val2
, &result
[0]);
962 static void visit_load_image(struct lp_build_nir_context
*bld_base
,
963 nir_intrinsic_instr
*instr
,
964 LLVMValueRef result
[4])
966 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
967 LLVMBuilderRef builder
= gallivm
->builder
;
968 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
969 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
970 LLVMValueRef coord_val
= get_src(bld_base
, instr
->src
[1]);
971 LLVMValueRef coords
[5];
972 struct lp_img_params params
;
973 const struct glsl_type
*type
= glsl_without_array(var
->type
);
975 memset(¶ms
, 0, sizeof(params
));
976 params
.target
= glsl_sampler_to_pipe(glsl_get_sampler_dim(type
), glsl_sampler_type_is_array(type
));
977 for (unsigned i
= 0; i
< 4; i
++)
978 coords
[i
] = LLVMBuildExtractValue(builder
, coord_val
, i
, "");
979 if (params
.target
== PIPE_TEXTURE_1D_ARRAY
)
980 coords
[2] = coords
[1];
982 params
.coords
= coords
;
983 params
.outdata
= result
;
984 params
.img_op
= LP_IMG_LOAD
;
985 params
.image_index
= var
->data
.binding
;
986 bld_base
->image_op(bld_base
, ¶ms
);
989 static void visit_store_image(struct lp_build_nir_context
*bld_base
,
990 nir_intrinsic_instr
*instr
)
992 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
993 LLVMBuilderRef builder
= gallivm
->builder
;
994 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
995 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
996 LLVMValueRef coord_val
= get_src(bld_base
, instr
->src
[1]);
997 LLVMValueRef in_val
= get_src(bld_base
, instr
->src
[3]);
998 LLVMValueRef coords
[5];
999 struct lp_img_params params
;
1000 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1002 memset(¶ms
, 0, sizeof(params
));
1003 params
.target
= glsl_sampler_to_pipe(glsl_get_sampler_dim(type
), glsl_sampler_type_is_array(type
));
1004 for (unsigned i
= 0; i
< 4; i
++)
1005 coords
[i
] = LLVMBuildExtractValue(builder
, coord_val
, i
, "");
1006 if (params
.target
== PIPE_TEXTURE_1D_ARRAY
)
1007 coords
[2] = coords
[1];
1008 params
.coords
= coords
;
1010 for (unsigned i
= 0; i
< 4; i
++) {
1011 params
.indata
[i
] = LLVMBuildExtractValue(builder
, in_val
, i
, "");
1012 params
.indata
[i
] = LLVMBuildBitCast(builder
, params
.indata
[i
], bld_base
->base
.vec_type
, "");
1014 params
.img_op
= LP_IMG_STORE
;
1015 params
.image_index
= var
->data
.binding
;
1017 if (params
.target
== PIPE_TEXTURE_1D_ARRAY
)
1018 coords
[2] = coords
[1];
1019 bld_base
->image_op(bld_base
, ¶ms
);
1022 static void visit_atomic_image(struct lp_build_nir_context
*bld_base
,
1023 nir_intrinsic_instr
*instr
,
1024 LLVMValueRef result
[4])
1026 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1027 LLVMBuilderRef builder
= gallivm
->builder
;
1028 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1029 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1030 struct lp_img_params params
;
1031 LLVMValueRef coord_val
= get_src(bld_base
, instr
->src
[1]);
1032 LLVMValueRef in_val
= get_src(bld_base
, instr
->src
[3]);
1033 LLVMValueRef coords
[5];
1034 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1036 memset(¶ms
, 0, sizeof(params
));
1038 switch (instr
->intrinsic
) {
1039 case nir_intrinsic_image_deref_atomic_add
:
1040 params
.op
= LLVMAtomicRMWBinOpAdd
;
1042 case nir_intrinsic_image_deref_atomic_exchange
:
1043 params
.op
= LLVMAtomicRMWBinOpXchg
;
1045 case nir_intrinsic_image_deref_atomic_and
:
1046 params
.op
= LLVMAtomicRMWBinOpAnd
;
1048 case nir_intrinsic_image_deref_atomic_or
:
1049 params
.op
= LLVMAtomicRMWBinOpOr
;
1051 case nir_intrinsic_image_deref_atomic_xor
:
1052 params
.op
= LLVMAtomicRMWBinOpXor
;
1054 case nir_intrinsic_image_deref_atomic_umin
:
1055 params
.op
= LLVMAtomicRMWBinOpUMin
;
1057 case nir_intrinsic_image_deref_atomic_umax
:
1058 params
.op
= LLVMAtomicRMWBinOpUMax
;
1060 case nir_intrinsic_image_deref_atomic_imin
:
1061 params
.op
= LLVMAtomicRMWBinOpMin
;
1063 case nir_intrinsic_image_deref_atomic_imax
:
1064 params
.op
= LLVMAtomicRMWBinOpMax
;
1070 params
.target
= glsl_sampler_to_pipe(glsl_get_sampler_dim(type
), glsl_sampler_type_is_array(type
));
1071 for (unsigned i
= 0; i
< 4; i
++)
1072 coords
[i
] = LLVMBuildExtractValue(builder
, coord_val
, i
, "");
1073 if (params
.target
== PIPE_TEXTURE_1D_ARRAY
)
1074 coords
[2] = coords
[1];
1075 params
.coords
= coords
;
1076 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
) {
1077 LLVMValueRef cas_val
= get_src(bld_base
, instr
->src
[4]);
1078 params
.indata
[0] = in_val
;
1079 params
.indata2
[0] = cas_val
;
1081 params
.indata
[0] = in_val
;
1083 params
.outdata
= result
;
1084 params
.img_op
= (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
) ? LP_IMG_ATOMIC_CAS
: LP_IMG_ATOMIC
;
1085 params
.image_index
= var
->data
.binding
;
1087 bld_base
->image_op(bld_base
, ¶ms
);
1091 static void visit_image_size(struct lp_build_nir_context
*bld_base
,
1092 nir_intrinsic_instr
*instr
,
1093 LLVMValueRef result
[4])
1095 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1096 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1097 struct lp_sampler_size_query_params params
= { 0 };
1098 params
.texture_unit
= var
->data
.binding
;
1099 params
.target
= glsl_sampler_to_pipe(glsl_get_sampler_dim(var
->type
), glsl_sampler_type_is_array(var
->type
));
1100 params
.sizes_out
= result
;
1102 bld_base
->image_size(bld_base
, ¶ms
);
1105 static void visit_shared_load(struct lp_build_nir_context
*bld_base
,
1106 nir_intrinsic_instr
*instr
,
1107 LLVMValueRef result
[4])
1109 LLVMValueRef offset
= get_src(bld_base
, instr
->src
[0]);
1110 bld_base
->load_mem(bld_base
, nir_dest_num_components(instr
->dest
), nir_dest_bit_size(instr
->dest
),
1111 NULL
, offset
, result
);
1114 static void visit_shared_store(struct lp_build_nir_context
*bld_base
,
1115 nir_intrinsic_instr
*instr
)
1117 LLVMValueRef val
= get_src(bld_base
, instr
->src
[0]);
1118 LLVMValueRef offset
= get_src(bld_base
, instr
->src
[1]);
1119 int writemask
= instr
->const_index
[1];
1120 int nc
= nir_src_num_components(instr
->src
[0]);
1121 int bitsize
= nir_src_bit_size(instr
->src
[0]);
1122 bld_base
->store_mem(bld_base
, writemask
, nc
, bitsize
, NULL
, offset
, val
);
1125 static void visit_shared_atomic(struct lp_build_nir_context
*bld_base
,
1126 nir_intrinsic_instr
*instr
,
1127 LLVMValueRef result
[4])
1129 LLVMValueRef offset
= get_src(bld_base
, instr
->src
[0]);
1130 LLVMValueRef val
= get_src(bld_base
, instr
->src
[1]);
1131 LLVMValueRef val2
= NULL
;
1132 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
)
1133 val2
= get_src(bld_base
, instr
->src
[2]);
1135 bld_base
->atomic_mem(bld_base
, instr
->intrinsic
, NULL
, offset
, val
, val2
, &result
[0]);
1139 static void visit_barrier(struct lp_build_nir_context
*bld_base
)
1141 bld_base
->barrier(bld_base
);
1144 static void visit_discard(struct lp_build_nir_context
*bld_base
,
1145 nir_intrinsic_instr
*instr
)
1147 LLVMValueRef cond
= NULL
;
1148 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
1149 cond
= get_src(bld_base
, instr
->src
[0]);
1150 cond
= cast_type(bld_base
, cond
, nir_type_int
, 32);
1152 bld_base
->discard(bld_base
, cond
);
1155 static void visit_load_kernel_input(struct lp_build_nir_context
*bld_base
,
1156 nir_intrinsic_instr
*instr
, LLVMValueRef result
[4])
1158 LLVMValueRef offset
= get_src(bld_base
, instr
->src
[0]);
1160 bool offset_is_uniform
= nir_src_is_dynamically_uniform(instr
->src
[0]);
1161 bld_base
->load_kernel_arg(bld_base
, nir_dest_num_components(instr
->dest
), nir_dest_bit_size(instr
->dest
),
1162 nir_src_bit_size(instr
->src
[0]),
1163 offset_is_uniform
, offset
, result
);
1166 static void visit_intrinsic(struct lp_build_nir_context
*bld_base
,
1167 nir_intrinsic_instr
*instr
)
1169 LLVMValueRef result
[4] = {0};
1170 switch (instr
->intrinsic
) {
1171 case nir_intrinsic_load_deref
:
1172 visit_load_var(bld_base
, instr
, result
);
1174 case nir_intrinsic_store_deref
:
1175 visit_store_var(bld_base
, instr
);
1177 case nir_intrinsic_load_ubo
:
1178 visit_load_ubo(bld_base
, instr
, result
);
1180 case nir_intrinsic_load_ssbo
:
1181 visit_load_ssbo(bld_base
, instr
, result
);
1183 case nir_intrinsic_store_ssbo
:
1184 visit_store_ssbo(bld_base
, instr
);
1186 case nir_intrinsic_get_buffer_size
:
1187 visit_get_buffer_size(bld_base
, instr
, result
);
1189 case nir_intrinsic_load_vertex_id
:
1190 case nir_intrinsic_load_primitive_id
:
1191 case nir_intrinsic_load_instance_id
:
1192 case nir_intrinsic_load_base_instance
:
1193 case nir_intrinsic_load_base_vertex
:
1194 case nir_intrinsic_load_work_group_id
:
1195 case nir_intrinsic_load_local_invocation_id
:
1196 case nir_intrinsic_load_num_work_groups
:
1197 case nir_intrinsic_load_invocation_id
:
1198 case nir_intrinsic_load_front_face
:
1199 case nir_intrinsic_load_draw_id
:
1200 bld_base
->sysval_intrin(bld_base
, instr
, result
);
1202 case nir_intrinsic_discard_if
:
1203 case nir_intrinsic_discard
:
1204 visit_discard(bld_base
, instr
);
1206 case nir_intrinsic_emit_vertex
:
1207 bld_base
->emit_vertex(bld_base
, nir_intrinsic_stream_id(instr
));
1209 case nir_intrinsic_end_primitive
:
1210 bld_base
->end_primitive(bld_base
, nir_intrinsic_stream_id(instr
));
1212 case nir_intrinsic_ssbo_atomic_add
:
1213 case nir_intrinsic_ssbo_atomic_imin
:
1214 case nir_intrinsic_ssbo_atomic_imax
:
1215 case nir_intrinsic_ssbo_atomic_umin
:
1216 case nir_intrinsic_ssbo_atomic_umax
:
1217 case nir_intrinsic_ssbo_atomic_and
:
1218 case nir_intrinsic_ssbo_atomic_or
:
1219 case nir_intrinsic_ssbo_atomic_xor
:
1220 case nir_intrinsic_ssbo_atomic_exchange
:
1221 case nir_intrinsic_ssbo_atomic_comp_swap
:
1222 visit_ssbo_atomic(bld_base
, instr
, result
);
1224 case nir_intrinsic_image_deref_load
:
1225 visit_load_image(bld_base
, instr
, result
);
1227 case nir_intrinsic_image_deref_store
:
1228 visit_store_image(bld_base
, instr
);
1230 case nir_intrinsic_image_deref_atomic_add
:
1231 case nir_intrinsic_image_deref_atomic_imin
:
1232 case nir_intrinsic_image_deref_atomic_imax
:
1233 case nir_intrinsic_image_deref_atomic_umin
:
1234 case nir_intrinsic_image_deref_atomic_umax
:
1235 case nir_intrinsic_image_deref_atomic_and
:
1236 case nir_intrinsic_image_deref_atomic_or
:
1237 case nir_intrinsic_image_deref_atomic_xor
:
1238 case nir_intrinsic_image_deref_atomic_exchange
:
1239 case nir_intrinsic_image_deref_atomic_comp_swap
:
1240 visit_atomic_image(bld_base
, instr
, result
);
1242 case nir_intrinsic_image_deref_size
:
1243 visit_image_size(bld_base
, instr
, result
);
1245 case nir_intrinsic_load_shared
:
1246 visit_shared_load(bld_base
, instr
, result
);
1248 case nir_intrinsic_store_shared
:
1249 visit_shared_store(bld_base
, instr
);
1251 case nir_intrinsic_shared_atomic_add
:
1252 case nir_intrinsic_shared_atomic_imin
:
1253 case nir_intrinsic_shared_atomic_umin
:
1254 case nir_intrinsic_shared_atomic_imax
:
1255 case nir_intrinsic_shared_atomic_umax
:
1256 case nir_intrinsic_shared_atomic_and
:
1257 case nir_intrinsic_shared_atomic_or
:
1258 case nir_intrinsic_shared_atomic_xor
:
1259 case nir_intrinsic_shared_atomic_exchange
:
1260 case nir_intrinsic_shared_atomic_comp_swap
:
1261 visit_shared_atomic(bld_base
, instr
, result
);
1263 case nir_intrinsic_barrier
:
1264 visit_barrier(bld_base
);
1266 case nir_intrinsic_memory_barrier
:
1268 case nir_intrinsic_load_kernel_input
:
1269 visit_load_kernel_input(bld_base
, instr
, result
);
1276 assign_dest(bld_base
, &instr
->dest
, result
);
1280 static void visit_txs(struct lp_build_nir_context
*bld_base
, nir_tex_instr
*instr
)
1282 struct lp_sampler_size_query_params params
;
1283 LLVMValueRef sizes_out
[4];
1284 LLVMValueRef explicit_lod
= NULL
;
1286 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1287 switch (instr
->src
[i
].src_type
) {
1288 case nir_tex_src_lod
:
1289 explicit_lod
= cast_type(bld_base
, get_src(bld_base
, instr
->src
[i
].src
), nir_type_int
, 32);
1296 params
.target
= glsl_sampler_to_pipe(instr
->sampler_dim
, instr
->is_array
);
1297 params
.texture_unit
= instr
->texture_index
;
1298 params
.explicit_lod
= explicit_lod
;
1299 params
.is_sviewinfo
= TRUE
;
1300 params
.sizes_out
= sizes_out
;
1302 if (instr
->op
== nir_texop_query_levels
)
1303 params
.explicit_lod
= bld_base
->uint_bld
.zero
;
1304 bld_base
->tex_size(bld_base
, ¶ms
);
1305 assign_dest(bld_base
, &instr
->dest
, &sizes_out
[instr
->op
== nir_texop_query_levels
? 3 : 0]);
1308 static enum lp_sampler_lod_property
lp_build_nir_lod_property(struct lp_build_nir_context
*bld_base
,
1311 enum lp_sampler_lod_property lod_property
;
1313 if (nir_src_is_dynamically_uniform(lod_src
))
1314 lod_property
= LP_SAMPLER_LOD_SCALAR
;
1315 else if (bld_base
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1316 if (gallivm_perf
& GALLIVM_PERF_NO_QUAD_LOD
)
1317 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
1319 lod_property
= LP_SAMPLER_LOD_PER_QUAD
;
1322 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
1323 return lod_property
;
1326 static void visit_tex(struct lp_build_nir_context
*bld_base
, nir_tex_instr
*instr
)
1328 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1329 LLVMBuilderRef builder
= gallivm
->builder
;
1330 LLVMValueRef coords
[5];
1331 LLVMValueRef offsets
[3] = { NULL
};
1332 LLVMValueRef explicit_lod
= NULL
, projector
= NULL
;
1333 struct lp_sampler_params params
;
1334 struct lp_derivatives derivs
;
1335 unsigned sample_key
= 0;
1336 nir_deref_instr
*texture_deref_instr
= NULL
;
1337 nir_deref_instr
*sampler_deref_instr
= NULL
;
1338 LLVMValueRef texel
[4];
1339 unsigned lod_src
= 0;
1340 LLVMValueRef coord_undef
= LLVMGetUndef(bld_base
->base
.int_vec_type
);
1342 memset(¶ms
, 0, sizeof(params
));
1343 enum lp_sampler_lod_property lod_property
= LP_SAMPLER_LOD_SCALAR
;
1345 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
1346 visit_txs(bld_base
, instr
);
1349 if (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)
1350 sample_key
|= LP_SAMPLER_OP_FETCH
<< LP_SAMPLER_OP_TYPE_SHIFT
;
1351 else if (instr
->op
== nir_texop_tg4
)
1352 sample_key
|= LP_SAMPLER_OP_GATHER
<< LP_SAMPLER_OP_TYPE_SHIFT
;
1353 else if (instr
->op
== nir_texop_lod
)
1354 sample_key
|= LP_SAMPLER_OP_LODQ
<< LP_SAMPLER_OP_TYPE_SHIFT
;
1355 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1356 switch (instr
->src
[i
].src_type
) {
1357 case nir_tex_src_coord
: {
1358 LLVMValueRef coord
= get_src(bld_base
, instr
->src
[i
].src
);
1359 if (instr
->coord_components
== 1)
1362 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
1363 coords
[chan
] = LLVMBuildExtractValue(builder
, coord
,
1366 for (unsigned chan
= instr
->coord_components
; chan
< 5; chan
++)
1367 coords
[chan
] = coord_undef
;
1371 case nir_tex_src_texture_deref
:
1372 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1374 case nir_tex_src_sampler_deref
:
1375 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1377 case nir_tex_src_projector
:
1378 projector
= lp_build_rcp(&bld_base
->base
, cast_type(bld_base
, get_src(bld_base
, instr
->src
[i
].src
), nir_type_float
, 32));
1380 case nir_tex_src_comparator
:
1381 sample_key
|= LP_SAMPLER_SHADOW
;
1382 coords
[4] = get_src(bld_base
, instr
->src
[i
].src
);
1383 coords
[4] = cast_type(bld_base
, coords
[4], nir_type_float
, 32);
1385 case nir_tex_src_bias
:
1386 sample_key
|= LP_SAMPLER_LOD_BIAS
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
1388 explicit_lod
= cast_type(bld_base
, get_src(bld_base
, instr
->src
[i
].src
), nir_type_float
, 32);
1390 case nir_tex_src_lod
:
1391 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
1393 if (instr
->op
== nir_texop_txf
)
1394 explicit_lod
= cast_type(bld_base
, get_src(bld_base
, instr
->src
[i
].src
), nir_type_int
, 32);
1396 explicit_lod
= cast_type(bld_base
, get_src(bld_base
, instr
->src
[i
].src
), nir_type_float
, 32);
1398 case nir_tex_src_ddx
: {
1399 int deriv_cnt
= instr
->coord_components
;
1400 if (instr
->is_array
)
1402 LLVMValueRef deriv_val
= get_src(bld_base
, instr
->src
[i
].src
);
1404 derivs
.ddx
[0] = deriv_val
;
1406 for (unsigned chan
= 0; chan
< deriv_cnt
; ++chan
)
1407 derivs
.ddx
[chan
] = LLVMBuildExtractValue(builder
, deriv_val
,
1409 for (unsigned chan
= 0; chan
< deriv_cnt
; ++chan
)
1410 derivs
.ddx
[chan
] = cast_type(bld_base
, derivs
.ddx
[chan
], nir_type_float
, 32);
1413 case nir_tex_src_ddy
: {
1414 int deriv_cnt
= instr
->coord_components
;
1415 if (instr
->is_array
)
1417 LLVMValueRef deriv_val
= get_src(bld_base
, instr
->src
[i
].src
);
1419 derivs
.ddy
[0] = deriv_val
;
1421 for (unsigned chan
= 0; chan
< deriv_cnt
; ++chan
)
1422 derivs
.ddy
[chan
] = LLVMBuildExtractValue(builder
, deriv_val
,
1424 for (unsigned chan
= 0; chan
< deriv_cnt
; ++chan
)
1425 derivs
.ddy
[chan
] = cast_type(bld_base
, derivs
.ddy
[chan
], nir_type_float
, 32);
1428 case nir_tex_src_offset
: {
1429 int offset_cnt
= instr
->coord_components
;
1430 if (instr
->is_array
)
1432 LLVMValueRef offset_val
= get_src(bld_base
, instr
->src
[i
].src
);
1433 sample_key
|= LP_SAMPLER_OFFSETS
;
1434 if (offset_cnt
== 1)
1435 offsets
[0] = offset_val
;
1437 for (unsigned chan
= 0; chan
< offset_cnt
; ++chan
)
1438 offsets
[chan
] = LLVMBuildExtractValue(builder
, offset_val
,
1443 case nir_tex_src_ms_index
:
1450 if (!sampler_deref_instr
)
1451 sampler_deref_instr
= texture_deref_instr
;
1454 lod_property
= lp_build_nir_lod_property(bld_base
, instr
->src
[lod_src
].src
);
1456 if (instr
->op
== nir_texop_tex
|| instr
->op
== nir_texop_tg4
|| instr
->op
== nir_texop_txb
||
1457 instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txd
|| instr
->op
== nir_texop_lod
)
1458 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
1459 coords
[chan
] = cast_type(bld_base
, coords
[chan
], nir_type_float
, 32);
1460 else if (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)
1461 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
1462 coords
[chan
] = cast_type(bld_base
, coords
[chan
], nir_type_int
, 32);
1464 if (instr
->is_array
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
1465 /* move layer coord for 1d arrays. */
1466 coords
[2] = coords
[1];
1467 coords
[1] = coord_undef
;
1471 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
1472 coords
[chan
] = lp_build_mul(&bld_base
->base
, coords
[chan
], projector
);
1473 if (sample_key
& LP_SAMPLER_SHADOW
)
1474 coords
[4] = lp_build_mul(&bld_base
->base
, coords
[4], projector
);
1477 uint32_t base_index
= 0;
1478 if (!texture_deref_instr
) {
1479 int samp_src_index
= nir_tex_instr_src_index(instr
, nir_tex_src_sampler_handle
);
1480 if (samp_src_index
== -1) {
1481 base_index
= instr
->sampler_index
;
1485 if (instr
->op
== nir_texop_txd
) {
1486 sample_key
|= LP_SAMPLER_LOD_DERIVATIVES
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
1487 params
.derivs
= &derivs
;
1488 if (bld_base
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1489 if (gallivm_perf
& GALLIVM_PERF_NO_QUAD_LOD
)
1490 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
1492 lod_property
= LP_SAMPLER_LOD_PER_QUAD
;
1494 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
1497 sample_key
|= lod_property
<< LP_SAMPLER_LOD_PROPERTY_SHIFT
;
1498 params
.sample_key
= sample_key
;
1499 params
.offsets
= offsets
;
1500 params
.texture_index
= base_index
;
1501 params
.sampler_index
= base_index
;
1502 params
.coords
= coords
;
1503 params
.texel
= texel
;
1504 params
.lod
= explicit_lod
;
1505 bld_base
->tex(bld_base
, ¶ms
);
1506 assign_dest(bld_base
, &instr
->dest
, texel
);
1509 static void visit_ssa_undef(struct lp_build_nir_context
*bld_base
,
1510 const nir_ssa_undef_instr
*instr
)
1512 unsigned num_components
= instr
->def
.num_components
;
1513 LLVMValueRef undef
[4];
1514 for (unsigned i
= 0; i
< num_components
; i
++)
1515 undef
[i
] = LLVMGetUndef(bld_base
->base
.vec_type
);
1516 assign_ssa_dest(bld_base
, &instr
->def
, undef
);
1519 static void visit_jump(struct lp_build_nir_context
*bld_base
,
1520 const nir_jump_instr
*instr
)
1522 switch (instr
->type
) {
1523 case nir_jump_break
:
1524 bld_base
->break_stmt(bld_base
);
1526 case nir_jump_continue
:
1527 bld_base
->continue_stmt(bld_base
);
1530 unreachable("Unknown jump instr\n");
1534 static void visit_deref(struct lp_build_nir_context
*bld_base
,
1535 nir_deref_instr
*instr
)
1537 if (instr
->mode
!= nir_var_mem_shared
&&
1538 instr
->mode
!= nir_var_mem_global
)
1540 LLVMValueRef result
= NULL
;
1541 switch(instr
->deref_type
) {
1542 case nir_deref_type_var
: {
1543 struct hash_entry
*entry
= _mesa_hash_table_search(bld_base
->vars
, instr
->var
);
1544 result
= entry
->data
;
1548 unreachable("Unhandled deref_instr deref type");
1551 assign_ssa(bld_base
, instr
->dest
.ssa
.index
, result
);
1554 static void visit_block(struct lp_build_nir_context
*bld_base
, nir_block
*block
)
1556 nir_foreach_instr(instr
, block
)
1558 switch (instr
->type
) {
1559 case nir_instr_type_alu
:
1560 visit_alu(bld_base
, nir_instr_as_alu(instr
));
1562 case nir_instr_type_load_const
:
1563 visit_load_const(bld_base
, nir_instr_as_load_const(instr
));
1565 case nir_instr_type_intrinsic
:
1566 visit_intrinsic(bld_base
, nir_instr_as_intrinsic(instr
));
1568 case nir_instr_type_tex
:
1569 visit_tex(bld_base
, nir_instr_as_tex(instr
));
1571 case nir_instr_type_phi
:
1574 case nir_instr_type_ssa_undef
:
1575 visit_ssa_undef(bld_base
, nir_instr_as_ssa_undef(instr
));
1577 case nir_instr_type_jump
:
1578 visit_jump(bld_base
, nir_instr_as_jump(instr
));
1580 case nir_instr_type_deref
:
1581 visit_deref(bld_base
, nir_instr_as_deref(instr
));
1584 fprintf(stderr
, "Unknown NIR instr type: ");
1585 nir_print_instr(instr
, stderr
);
1586 fprintf(stderr
, "\n");
1592 static void visit_if(struct lp_build_nir_context
*bld_base
, nir_if
*if_stmt
)
1594 LLVMValueRef cond
= get_src(bld_base
, if_stmt
->condition
);
1596 bld_base
->if_cond(bld_base
, cond
);
1597 visit_cf_list(bld_base
, &if_stmt
->then_list
);
1599 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
1600 bld_base
->else_stmt(bld_base
);
1601 visit_cf_list(bld_base
, &if_stmt
->else_list
);
1603 bld_base
->endif_stmt(bld_base
);
1606 static void visit_loop(struct lp_build_nir_context
*bld_base
, nir_loop
*loop
)
1608 bld_base
->bgnloop(bld_base
);
1609 visit_cf_list(bld_base
, &loop
->body
);
1610 bld_base
->endloop(bld_base
);
1613 static void visit_cf_list(struct lp_build_nir_context
*bld_base
,
1614 struct exec_list
*list
)
1616 foreach_list_typed(nir_cf_node
, node
, node
, list
)
1618 switch (node
->type
) {
1619 case nir_cf_node_block
:
1620 visit_block(bld_base
, nir_cf_node_as_block(node
));
1623 case nir_cf_node_if
:
1624 visit_if(bld_base
, nir_cf_node_as_if(node
));
1627 case nir_cf_node_loop
:
1628 visit_loop(bld_base
, nir_cf_node_as_loop(node
));
1638 handle_shader_output_decl(struct lp_build_nir_context
*bld_base
,
1639 struct nir_shader
*nir
,
1640 struct nir_variable
*variable
)
1642 bld_base
->emit_var_decl(bld_base
, variable
);
1645 /* vector registers are stored as arrays in LLVM side,
1646 so we can use GEP on them, as to do exec mask stores
1647 we need to operate on a single components.
1653 static LLVMTypeRef
get_register_type(struct lp_build_nir_context
*bld_base
,
1656 struct lp_build_context
*int_bld
= get_int_bld(bld_base
, true, reg
->bit_size
);
1658 LLVMTypeRef type
= int_bld
->vec_type
;
1659 if (reg
->num_array_elems
)
1660 type
= LLVMArrayType(type
, reg
->num_array_elems
);
1661 if (reg
->num_components
> 1)
1662 type
= LLVMArrayType(type
, reg
->num_components
);
1668 bool lp_build_nir_llvm(
1669 struct lp_build_nir_context
*bld_base
,
1670 struct nir_shader
*nir
)
1672 struct nir_function
*func
;
1674 nir_convert_from_ssa(nir
, true);
1675 nir_lower_locals_to_regs(nir
);
1676 nir_remove_dead_derefs(nir
);
1677 nir_remove_dead_variables(nir
, nir_var_function_temp
);
1679 nir_foreach_variable(variable
, &nir
->outputs
)
1680 handle_shader_output_decl(bld_base
, nir
, variable
);
1682 bld_base
->regs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1683 _mesa_key_pointer_equal
);
1684 bld_base
->vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1685 _mesa_key_pointer_equal
);
1687 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
1689 nir_foreach_register(reg
, &func
->impl
->registers
) {
1690 LLVMTypeRef type
= get_register_type(bld_base
, reg
);
1691 LLVMValueRef reg_alloc
= lp_build_alloca_undef(bld_base
->base
.gallivm
,
1693 _mesa_hash_table_insert(bld_base
->regs
, reg
, reg_alloc
);
1695 nir_index_ssa_defs(func
->impl
);
1696 bld_base
->ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
1697 visit_cf_list(bld_base
, &func
->impl
->body
);
1699 free(bld_base
->ssa_defs
);
1700 ralloc_free(bld_base
->vars
);
1701 ralloc_free(bld_base
->regs
);
1705 /* do some basic opts to remove some things we don't want to see. */
1706 void lp_build_opt_nir(struct nir_shader
*nir
)
1711 NIR_PASS_V(nir
, nir_opt_constant_folding
);
1712 NIR_PASS_V(nir
, nir_opt_algebraic
);
1714 nir_lower_bool_to_int32(nir
);