2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "util/bitscan.h"
31 #include "util/u_math.h"
32 #include "ac_shader_abi.h"
33 #include "ac_shader_util.h"
35 struct ac_nir_context
{
36 struct ac_llvm_context ac
;
37 struct ac_shader_abi
*abi
;
39 gl_shader_stage stage
;
41 LLVMValueRef
*ssa_defs
;
43 struct hash_table
*defs
;
44 struct hash_table
*phis
;
45 struct hash_table
*vars
;
47 LLVMValueRef main_function
;
48 LLVMBasicBlockRef continue_block
;
49 LLVMBasicBlockRef break_block
;
55 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
56 const nir_deref_var
*deref
,
57 nir_deref_instr
*deref_instr
,
58 enum ac_descriptor_type desc_type
,
59 const nir_tex_instr
*instr
,
60 bool image
, bool write
);
63 build_store_values_extended(struct ac_llvm_context
*ac
,
66 unsigned value_stride
,
69 LLVMBuilderRef builder
= ac
->builder
;
72 for (i
= 0; i
< value_count
; i
++) {
73 LLVMValueRef ptr
= values
[i
* value_stride
];
74 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
75 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
76 LLVMBuildStore(builder
, value
, ptr
);
80 static enum ac_image_dim
81 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
85 case GLSL_SAMPLER_DIM_1D
:
86 if (ctx
->chip_class
>= GFX9
)
87 return is_array
? ac_image_2darray
: ac_image_2d
;
88 return is_array
? ac_image_1darray
: ac_image_1d
;
89 case GLSL_SAMPLER_DIM_2D
:
90 case GLSL_SAMPLER_DIM_RECT
:
91 case GLSL_SAMPLER_DIM_EXTERNAL
:
92 return is_array
? ac_image_2darray
: ac_image_2d
;
93 case GLSL_SAMPLER_DIM_3D
:
95 case GLSL_SAMPLER_DIM_CUBE
:
97 case GLSL_SAMPLER_DIM_MS
:
98 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
99 case GLSL_SAMPLER_DIM_SUBPASS
:
100 return ac_image_2darray
;
101 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
102 return ac_image_2darraymsaa
;
104 unreachable("bad sampler dim");
108 static enum ac_image_dim
109 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
112 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
114 if (dim
== ac_image_cube
||
115 (ctx
->chip_class
<= VI
&& dim
== ac_image_3d
))
116 dim
= ac_image_2darray
;
121 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
122 const nir_ssa_def
*def
)
124 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
125 if (def
->num_components
> 1) {
126 type
= LLVMVectorType(type
, def
->num_components
);
131 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
134 return nir
->ssa_defs
[src
.ssa
->index
];
138 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
140 LLVMValueRef ptr
= get_src(ctx
, src
);
141 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
142 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
144 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
145 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
148 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
149 const struct nir_block
*b
)
151 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
152 return (LLVMBasicBlockRef
)entry
->data
;
155 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
157 unsigned num_components
)
159 LLVMValueRef value
= get_src(ctx
, src
.src
);
160 bool need_swizzle
= false;
163 unsigned src_components
= ac_get_llvm_num_components(value
);
164 for (unsigned i
= 0; i
< num_components
; ++i
) {
165 assert(src
.swizzle
[i
] < src_components
);
166 if (src
.swizzle
[i
] != i
)
170 if (need_swizzle
|| num_components
!= src_components
) {
171 LLVMValueRef masks
[] = {
172 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
175 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
177 if (src_components
> 1 && num_components
== 1) {
178 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
180 } else if (src_components
== 1 && num_components
> 1) {
181 LLVMValueRef values
[] = {value
, value
, value
, value
};
182 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
184 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
185 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
194 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
195 LLVMIntPredicate pred
, LLVMValueRef src0
,
198 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
199 return LLVMBuildSelect(ctx
->builder
, result
,
200 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
204 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
205 LLVMRealPredicate pred
, LLVMValueRef src0
,
209 src0
= ac_to_float(ctx
, src0
);
210 src1
= ac_to_float(ctx
, src1
);
211 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
212 return LLVMBuildSelect(ctx
->builder
, result
,
213 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
217 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
219 LLVMTypeRef result_type
,
223 LLVMValueRef params
[] = {
224 ac_to_float(ctx
, src0
),
227 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
228 ac_get_elem_bits(ctx
, result_type
));
229 assert(length
< sizeof(name
));
230 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
233 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
235 LLVMTypeRef result_type
,
236 LLVMValueRef src0
, LLVMValueRef src1
)
239 LLVMValueRef params
[] = {
240 ac_to_float(ctx
, src0
),
241 ac_to_float(ctx
, src1
),
244 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
245 ac_get_elem_bits(ctx
, result_type
));
246 assert(length
< sizeof(name
));
247 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
250 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
252 LLVMTypeRef result_type
,
253 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
256 LLVMValueRef params
[] = {
257 ac_to_float(ctx
, src0
),
258 ac_to_float(ctx
, src1
),
259 ac_to_float(ctx
, src2
),
262 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
263 ac_get_elem_bits(ctx
, result_type
));
264 assert(length
< sizeof(name
));
265 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
268 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
269 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
271 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
273 return LLVMBuildSelect(ctx
->builder
, v
, ac_to_integer(ctx
, src1
),
274 ac_to_integer(ctx
, src2
), "");
277 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
278 LLVMIntPredicate pred
,
279 LLVMValueRef src0
, LLVMValueRef src1
)
281 return LLVMBuildSelect(ctx
->builder
,
282 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
287 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
290 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
291 LLVMBuildNeg(ctx
->builder
, src0
, ""));
294 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
296 LLVMValueRef src0
, LLVMValueRef src1
)
298 LLVMTypeRef ret_type
;
299 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
301 LLVMValueRef params
[] = { src0
, src1
};
302 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
305 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
306 params
, 2, AC_FUNC_ATTR_READNONE
);
308 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
309 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
313 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
316 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
319 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
322 src0
= ac_to_float(ctx
, src0
);
323 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
324 return LLVMBuildSExt(ctx
->builder
,
325 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
329 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
333 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
338 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
341 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
344 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
345 return LLVMBuildSExt(ctx
->builder
,
346 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
350 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
354 LLVMValueRef cond
= NULL
;
356 src0
= ac_to_float(ctx
, src0
);
357 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
359 if (ctx
->chip_class
>= VI
) {
360 LLVMValueRef args
[2];
361 /* Check if the result is a denormal - and flush to 0 if so. */
363 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
364 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
367 /* need to convert back up to f32 */
368 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
370 if (ctx
->chip_class
>= VI
)
371 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
374 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
375 * so compare the result and flush to 0 if it's smaller.
377 LLVMValueRef temp
, cond2
;
378 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
379 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
380 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
382 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
383 temp
, ctx
->f32_0
, "");
384 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
385 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
390 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
391 LLVMValueRef src0
, LLVMValueRef src1
)
393 LLVMValueRef dst64
, result
;
394 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
395 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
397 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
398 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
399 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
403 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
404 LLVMValueRef src0
, LLVMValueRef src1
)
406 LLVMValueRef dst64
, result
;
407 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
408 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
410 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
411 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
412 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
416 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
418 const LLVMValueRef srcs
[3])
421 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
423 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
424 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
428 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
429 LLVMValueRef src0
, LLVMValueRef src1
,
430 LLVMValueRef src2
, LLVMValueRef src3
)
432 LLVMValueRef bfi_args
[3], result
;
434 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
435 LLVMBuildSub(ctx
->builder
,
436 LLVMBuildShl(ctx
->builder
,
441 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
444 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
447 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
448 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
450 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
451 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
452 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
454 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
458 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
461 LLVMValueRef comp
[2];
463 src0
= ac_to_float(ctx
, src0
);
464 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
465 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
467 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
470 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
473 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
474 LLVMValueRef temps
[2], result
, val
;
477 for (i
= 0; i
< 2; i
++) {
478 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
479 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
480 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
481 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
484 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
486 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
491 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
499 if (op
== nir_op_fddx_fine
)
500 mask
= AC_TID_MASK_LEFT
;
501 else if (op
== nir_op_fddy_fine
)
502 mask
= AC_TID_MASK_TOP
;
504 mask
= AC_TID_MASK_TOP_LEFT
;
506 /* for DDX we want to next X pixel, DDY next Y pixel. */
507 if (op
== nir_op_fddx_fine
||
508 op
== nir_op_fddx_coarse
||
514 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
519 * this takes an I,J coordinate pair,
520 * and works out the X and Y derivatives.
521 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
523 static LLVMValueRef
emit_ddxy_interp(
524 struct ac_nir_context
*ctx
,
525 LLVMValueRef interp_ij
)
527 LLVMValueRef result
[4], a
;
530 for (i
= 0; i
< 2; i
++) {
531 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
532 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
533 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
534 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
536 return ac_build_gather_values(&ctx
->ac
, result
, 4);
539 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
541 LLVMValueRef src
[4], result
= NULL
;
542 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
543 unsigned src_components
;
544 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
546 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
553 case nir_op_pack_half_2x16
:
556 case nir_op_unpack_half_2x16
:
559 case nir_op_cube_face_coord
:
560 case nir_op_cube_face_index
:
564 src_components
= num_components
;
567 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
568 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
576 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
577 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
580 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
583 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
586 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
589 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
590 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
591 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
594 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
595 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
596 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
599 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
602 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
605 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
608 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
611 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
612 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
613 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
614 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
615 ac_to_float_type(&ctx
->ac
, def_type
), result
);
616 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
617 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
620 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
621 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
622 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
625 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
628 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
631 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
634 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
635 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
636 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
639 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
640 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
644 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
647 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
650 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
653 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
654 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
655 LLVMTypeOf(src
[0]), ""),
659 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
660 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
661 LLVMTypeOf(src
[0]), ""),
665 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
666 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
667 LLVMTypeOf(src
[0]), ""),
671 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
674 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
677 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
680 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
683 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
686 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
689 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
692 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
695 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
698 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
701 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
702 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
705 result
= emit_iabs(&ctx
->ac
, src
[0]);
708 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
711 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
714 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
717 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
720 result
= ac_build_isign(&ctx
->ac
, src
[0],
721 instr
->dest
.dest
.ssa
.bit_size
);
724 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
725 result
= ac_build_fsign(&ctx
->ac
, src
[0],
726 instr
->dest
.dest
.ssa
.bit_size
);
729 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
730 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
733 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
734 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
737 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
738 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
740 case nir_op_fround_even
:
741 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
742 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
745 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
746 result
= ac_build_fract(&ctx
->ac
, src
[0],
747 instr
->dest
.dest
.ssa
.bit_size
);
750 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
751 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
754 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
755 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
758 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
759 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
762 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
763 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
766 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
767 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
770 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
771 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
772 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
775 case nir_op_frexp_exp
:
776 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
777 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
778 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
781 case nir_op_frexp_sig
:
782 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
783 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
784 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
787 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
788 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
789 if (ctx
->ac
.chip_class
< GFX9
&&
790 instr
->dest
.dest
.ssa
.bit_size
== 32) {
791 /* Only pre-GFX9 chips do not flush denorms. */
792 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
793 ac_to_float_type(&ctx
->ac
, def_type
),
798 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
799 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
800 if (ctx
->ac
.chip_class
< GFX9
&&
801 instr
->dest
.dest
.ssa
.bit_size
== 32) {
802 /* Only pre-GFX9 chips do not flush denorms. */
803 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
804 ac_to_float_type(&ctx
->ac
, def_type
),
809 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
810 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
813 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
814 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
815 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
817 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
819 case nir_op_ibitfield_extract
:
820 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
822 case nir_op_ubitfield_extract
:
823 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
825 case nir_op_bitfield_insert
:
826 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
828 case nir_op_bitfield_reverse
:
829 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
831 case nir_op_bit_count
:
832 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
833 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
835 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i64", ctx
->ac
.i64
, src
, 1, AC_FUNC_ATTR_READNONE
);
836 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
842 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
843 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
844 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
848 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
849 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
853 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
854 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
858 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
859 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
863 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
864 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
867 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
868 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
871 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
872 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
876 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
877 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
878 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
880 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
884 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
885 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
886 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
888 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
891 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
893 case nir_op_find_lsb
:
894 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
895 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
897 case nir_op_ufind_msb
:
898 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
899 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
901 case nir_op_ifind_msb
:
902 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
903 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
905 case nir_op_uadd_carry
:
906 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
907 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
908 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
910 case nir_op_usub_borrow
:
911 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
912 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
913 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
916 result
= emit_b2f(&ctx
->ac
, src
[0]);
919 result
= emit_f2b(&ctx
->ac
, src
[0]);
922 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
925 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
926 result
= emit_i2b(&ctx
->ac
, src
[0]);
928 case nir_op_fquantize2f16
:
929 result
= emit_f2f16(&ctx
->ac
, src
[0]);
931 case nir_op_umul_high
:
932 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
933 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
934 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
936 case nir_op_imul_high
:
937 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
938 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
939 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
941 case nir_op_pack_half_2x16
:
942 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
944 case nir_op_unpack_half_2x16
:
945 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
949 case nir_op_fddx_fine
:
950 case nir_op_fddy_fine
:
951 case nir_op_fddx_coarse
:
952 case nir_op_fddy_coarse
:
953 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
956 case nir_op_unpack_64_2x32_split_x
: {
957 assert(ac_get_llvm_num_components(src
[0]) == 1);
958 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
961 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
966 case nir_op_unpack_64_2x32_split_y
: {
967 assert(ac_get_llvm_num_components(src
[0]) == 1);
968 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
971 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
976 case nir_op_pack_64_2x32_split
: {
977 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
978 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
979 src
[0], ctx
->ac
.i32_0
, "");
980 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
981 src
[1], ctx
->ac
.i32_1
, "");
982 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
986 case nir_op_cube_face_coord
: {
987 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
988 LLVMValueRef results
[2];
990 for (unsigned chan
= 0; chan
< 3; chan
++)
991 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
992 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
993 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
994 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
995 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
996 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1000 case nir_op_cube_face_index
: {
1001 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1003 for (unsigned chan
= 0; chan
< 3; chan
++)
1004 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1005 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1006 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1011 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1012 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1013 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1014 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1017 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1018 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1021 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1022 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
1025 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1026 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1027 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1028 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1031 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1032 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1035 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1036 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1038 case nir_op_fmed3
: {
1039 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1040 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1041 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1042 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1043 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1044 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1045 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1046 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1049 case nir_op_imed3
: {
1050 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1051 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1052 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1053 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1056 case nir_op_umed3
: {
1057 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1058 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1059 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1060 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1065 fprintf(stderr
, "Unknown NIR alu instr: ");
1066 nir_print_instr(&instr
->instr
, stderr
);
1067 fprintf(stderr
, "\n");
1072 assert(instr
->dest
.dest
.is_ssa
);
1073 result
= ac_to_integer(&ctx
->ac
, result
);
1074 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1078 static void visit_load_const(struct ac_nir_context
*ctx
,
1079 const nir_load_const_instr
*instr
)
1081 LLVMValueRef values
[4], value
= NULL
;
1082 LLVMTypeRef element_type
=
1083 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1085 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1086 switch (instr
->def
.bit_size
) {
1088 values
[i
] = LLVMConstInt(element_type
,
1089 instr
->value
.u32
[i
], false);
1092 values
[i
] = LLVMConstInt(element_type
,
1093 instr
->value
.u64
[i
], false);
1097 "unsupported nir load_const bit_size: %d\n",
1098 instr
->def
.bit_size
);
1102 if (instr
->def
.num_components
> 1) {
1103 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1107 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1111 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1114 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1115 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1118 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1119 /* On VI, the descriptor contains the size in bytes,
1120 * but TXQ must return the size in elements.
1121 * The stride is always non-zero for resources using TXQ.
1123 LLVMValueRef stride
=
1124 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1126 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1127 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1128 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1129 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1131 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1136 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1137 struct ac_image_args
*args
,
1138 const nir_tex_instr
*instr
)
1140 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1141 LLVMValueRef half_texel
[2];
1142 LLVMValueRef compare_cube_wa
= NULL
;
1143 LLVMValueRef result
;
1147 struct ac_image_args txq_args
= { 0 };
1149 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1150 txq_args
.opcode
= ac_image_get_resinfo
;
1151 txq_args
.dmask
= 0xf;
1152 txq_args
.lod
= ctx
->i32_0
;
1153 txq_args
.resource
= args
->resource
;
1154 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1155 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1157 for (unsigned c
= 0; c
< 2; c
++) {
1158 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1159 LLVMConstInt(ctx
->i32
, c
, false), "");
1160 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1161 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1162 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1163 LLVMConstReal(ctx
->f32
, -0.5), "");
1167 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1169 for (unsigned c
= 0; c
< 2; c
++) {
1171 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1172 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1176 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1177 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1178 * workaround by sampling using a scaled type and converting.
1179 * This is taken from amdgpu-pro shaders.
1181 /* NOTE this produces some ugly code compared to amdgpu-pro,
1182 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1183 * and then reads them back. -pro generates two selects,
1184 * one s_cmp for the descriptor rewriting
1185 * one v_cmp for the coordinate and result changes.
1187 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1188 LLVMValueRef tmp
, tmp2
;
1190 /* workaround 8/8/8/8 uint/sint cube gather bug */
1191 /* first detect it then change to a scaled read and f2i */
1192 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1195 /* extract the DATA_FORMAT */
1196 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1197 LLVMConstInt(ctx
->i32
, 6, false), false);
1199 /* is the DATA_FORMAT == 8_8_8_8 */
1200 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1202 if (stype
== GLSL_TYPE_UINT
)
1203 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1204 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1205 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1207 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1208 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1209 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1211 /* replace the NUM FORMAT in the descriptor */
1212 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1213 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1215 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1217 /* don't modify the coordinates for this case */
1218 for (unsigned c
= 0; c
< 2; ++c
)
1219 args
->coords
[c
] = LLVMBuildSelect(
1220 ctx
->builder
, compare_cube_wa
,
1221 orig_coords
[c
], args
->coords
[c
], "");
1224 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1225 result
= ac_build_image_opcode(ctx
, args
);
1227 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1228 LLVMValueRef tmp
, tmp2
;
1230 /* if the cube workaround is in place, f2i the result. */
1231 for (unsigned c
= 0; c
< 4; c
++) {
1232 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1233 if (stype
== GLSL_TYPE_UINT
)
1234 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1236 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1237 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1238 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1239 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1240 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1241 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1247 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1248 const nir_tex_instr
*instr
,
1249 struct ac_image_args
*args
)
1251 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1252 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1254 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1255 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1259 util_last_bit(mask
),
1262 return ac_build_buffer_load_format(&ctx
->ac
,
1266 util_last_bit(mask
),
1271 args
->opcode
= ac_image_sample
;
1273 switch (instr
->op
) {
1275 case nir_texop_txf_ms
:
1276 case nir_texop_samples_identical
:
1277 args
->opcode
= args
->level_zero
||
1278 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1279 ac_image_load
: ac_image_load_mip
;
1280 args
->level_zero
= false;
1283 case nir_texop_query_levels
:
1284 args
->opcode
= ac_image_get_resinfo
;
1286 args
->lod
= ctx
->ac
.i32_0
;
1287 args
->level_zero
= false;
1290 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1292 args
->level_zero
= true;
1296 args
->opcode
= ac_image_gather4
;
1297 args
->level_zero
= true;
1300 args
->opcode
= ac_image_get_lod
;
1306 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1307 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1308 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1309 return lower_gather4_integer(&ctx
->ac
, args
, instr
);
1313 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1314 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1315 if ((args
->dim
== ac_image_2darray
||
1316 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1317 args
->coords
[1] = ctx
->ac
.i32_0
;
1321 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1322 return ac_build_image_opcode(&ctx
->ac
, args
);
1325 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1326 nir_intrinsic_instr
*instr
)
1328 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1329 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1331 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1332 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1336 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1337 nir_intrinsic_instr
*instr
)
1339 LLVMValueRef ptr
, addr
;
1341 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1342 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1343 get_src(ctx
, instr
->src
[0]), "");
1345 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1346 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1348 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1351 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1352 const nir_intrinsic_instr
*instr
)
1354 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1356 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1359 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1361 uint32_t new_mask
= 0;
1362 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1363 if (mask
& (1u << i
))
1364 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1368 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1369 unsigned start
, unsigned count
)
1371 LLVMTypeRef type
= LLVMTypeOf(src
);
1373 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
1379 unsigned src_elements
= LLVMGetVectorSize(type
);
1380 assert(start
< src_elements
);
1381 assert(start
+ count
<= src_elements
);
1383 if (start
== 0 && count
== src_elements
)
1387 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
1390 LLVMValueRef indices
[8];
1391 for (unsigned i
= 0; i
< count
; ++i
)
1392 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
1394 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
1395 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1398 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1399 nir_intrinsic_instr
*instr
)
1401 const char *store_name
;
1402 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1403 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1404 int elem_size_mult
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
1405 int components_32bit
= elem_size_mult
* instr
->num_components
;
1406 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1407 LLVMValueRef base_data
, base_offset
;
1408 LLVMValueRef params
[6];
1410 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
1411 get_src(ctx
, instr
->src
[1]), true);
1412 params
[2] = ctx
->ac
.i32_0
; /* vindex */
1413 params
[4] = ctx
->ac
.i1false
; /* glc */
1414 params
[5] = ctx
->ac
.i1false
; /* slc */
1416 if (components_32bit
> 1)
1417 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
1419 writemask
= widen_mask(writemask
, elem_size_mult
);
1421 base_data
= ac_to_float(&ctx
->ac
, src_data
);
1422 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1423 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
1425 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1429 LLVMValueRef offset
;
1431 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1433 /* Due to an LLVM limitation, split 3-element writes
1434 * into a 2-element and a 1-element write. */
1436 writemask
|= 1 << (start
+ 2);
1441 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1446 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1447 } else if (count
== 2) {
1448 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1452 store_name
= "llvm.amdgcn.buffer.store.f32";
1454 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1456 offset
= base_offset
;
1458 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
1462 ac_build_intrinsic(&ctx
->ac
, store_name
,
1463 ctx
->ac
.voidt
, params
, 6, 0);
1467 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1468 const nir_intrinsic_instr
*instr
)
1471 LLVMValueRef params
[6];
1474 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1475 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1477 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1478 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1479 get_src(ctx
, instr
->src
[0]),
1481 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1482 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1483 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1485 switch (instr
->intrinsic
) {
1486 case nir_intrinsic_ssbo_atomic_add
:
1487 name
= "llvm.amdgcn.buffer.atomic.add";
1489 case nir_intrinsic_ssbo_atomic_imin
:
1490 name
= "llvm.amdgcn.buffer.atomic.smin";
1492 case nir_intrinsic_ssbo_atomic_umin
:
1493 name
= "llvm.amdgcn.buffer.atomic.umin";
1495 case nir_intrinsic_ssbo_atomic_imax
:
1496 name
= "llvm.amdgcn.buffer.atomic.smax";
1498 case nir_intrinsic_ssbo_atomic_umax
:
1499 name
= "llvm.amdgcn.buffer.atomic.umax";
1501 case nir_intrinsic_ssbo_atomic_and
:
1502 name
= "llvm.amdgcn.buffer.atomic.and";
1504 case nir_intrinsic_ssbo_atomic_or
:
1505 name
= "llvm.amdgcn.buffer.atomic.or";
1507 case nir_intrinsic_ssbo_atomic_xor
:
1508 name
= "llvm.amdgcn.buffer.atomic.xor";
1510 case nir_intrinsic_ssbo_atomic_exchange
:
1511 name
= "llvm.amdgcn.buffer.atomic.swap";
1513 case nir_intrinsic_ssbo_atomic_comp_swap
:
1514 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1520 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1523 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1524 const nir_intrinsic_instr
*instr
)
1526 LLVMValueRef results
[2];
1527 int load_components
;
1528 int num_components
= instr
->num_components
;
1529 if (instr
->dest
.ssa
.bit_size
== 64)
1530 num_components
*= 2;
1532 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1533 load_components
= MIN2(num_components
- i
, 4);
1534 const char *load_name
;
1535 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1536 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
1537 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1539 if (load_components
== 3)
1540 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
1541 else if (load_components
> 1)
1542 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
1544 if (load_components
>= 3)
1545 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1546 else if (load_components
== 2)
1547 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1548 else if (load_components
== 1)
1549 load_name
= "llvm.amdgcn.buffer.load.f32";
1551 unreachable("unhandled number of components");
1553 LLVMValueRef params
[] = {
1554 ctx
->abi
->load_ssbo(ctx
->abi
,
1555 get_src(ctx
, instr
->src
[0]),
1563 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1567 LLVMValueRef ret
= results
[0];
1568 if (num_components
> 4 || num_components
== 3) {
1569 LLVMValueRef masks
[] = {
1570 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1571 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1572 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
1573 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
1576 if (num_components
== 6) {
1577 /* we end up with a v4f32 and v2f32 but shuffle fails on that */
1578 results
[1] = ac_build_expand_to_vec4(&ctx
->ac
, results
[1], 4);
1581 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1582 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1583 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
1586 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1587 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1590 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1591 const nir_intrinsic_instr
*instr
)
1594 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1595 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1596 int num_components
= instr
->num_components
;
1598 if (ctx
->abi
->load_ubo
)
1599 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1601 if (instr
->dest
.ssa
.bit_size
== 64)
1602 num_components
*= 2;
1604 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1605 NULL
, 0, false, false, true, true);
1606 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1607 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1608 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1612 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
1613 bool vs_in
, unsigned *vertex_index_out
,
1614 LLVMValueRef
*vertex_index_ref
,
1615 unsigned *const_out
, LLVMValueRef
*indir_out
)
1617 unsigned const_offset
= 0;
1618 nir_deref
*tail
= &deref
->deref
;
1619 LLVMValueRef offset
= NULL
;
1621 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1623 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1624 if (vertex_index_out
)
1625 *vertex_index_out
= deref_array
->base_offset
;
1627 if (vertex_index_ref
) {
1628 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
1629 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1630 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
1632 *vertex_index_ref
= vtx
;
1636 if (deref
->var
->data
.compact
) {
1637 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1638 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
1639 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1640 /* We always lower indirect dereferences for "compact" array vars. */
1641 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
1643 const_offset
= deref_array
->base_offset
;
1647 while (tail
->child
!= NULL
) {
1648 const struct glsl_type
*parent_type
= tail
->type
;
1651 if (tail
->deref_type
== nir_deref_type_array
) {
1652 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1653 LLVMValueRef index
, stride
, local_offset
;
1654 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
1656 const_offset
+= size
* deref_array
->base_offset
;
1657 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
1660 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
1661 index
= get_src(ctx
, deref_array
->indirect
);
1662 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
1663 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
1666 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
1668 offset
= local_offset
;
1669 } else if (tail
->deref_type
== nir_deref_type_struct
) {
1670 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
1672 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
1673 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1674 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1677 unreachable("unsupported deref type");
1681 if (const_offset
&& offset
)
1682 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1683 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1686 *const_out
= const_offset
;
1687 *indir_out
= offset
;
1691 build_gep_for_deref(struct ac_nir_context
*ctx
,
1692 nir_deref_var
*deref
)
1694 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1695 assert(entry
->data
);
1696 LLVMValueRef val
= entry
->data
;
1697 nir_deref
*tail
= deref
->deref
.child
;
1698 while (tail
!= NULL
) {
1699 LLVMValueRef offset
;
1700 switch (tail
->deref_type
) {
1701 case nir_deref_type_array
: {
1702 nir_deref_array
*array
= nir_deref_as_array(tail
);
1703 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
1704 if (array
->deref_array_type
==
1705 nir_deref_array_type_indirect
) {
1706 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1713 case nir_deref_type_struct
: {
1714 nir_deref_struct
*deref_struct
=
1715 nir_deref_as_struct(tail
);
1716 offset
= LLVMConstInt(ctx
->ac
.i32
,
1717 deref_struct
->index
, 0);
1721 unreachable("bad deref type");
1723 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
1729 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1730 nir_intrinsic_instr
*instr
,
1733 LLVMValueRef result
;
1734 LLVMValueRef vertex_index
= NULL
;
1735 LLVMValueRef indir_index
= NULL
;
1736 unsigned const_index
= 0;
1737 unsigned location
= instr
->variables
[0]->var
->data
.location
;
1738 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
1739 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1740 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
1742 get_deref_offset(ctx
, instr
->variables
[0],
1743 false, NULL
, is_patch
? NULL
: &vertex_index
,
1744 &const_index
, &indir_index
);
1746 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1748 LLVMTypeRef src_component_type
;
1749 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1750 src_component_type
= LLVMGetElementType(dest_type
);
1752 src_component_type
= dest_type
;
1754 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1755 vertex_index
, indir_index
,
1756 const_index
, location
, driver_location
,
1757 instr
->variables
[0]->var
->data
.location_frac
,
1758 instr
->num_components
,
1759 is_patch
, is_compact
, load_inputs
);
1760 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1763 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1764 nir_intrinsic_instr
*instr
)
1766 LLVMValueRef values
[8];
1767 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1768 int ve
= instr
->dest
.ssa
.num_components
;
1769 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1770 LLVMValueRef indir_index
;
1772 unsigned const_index
;
1773 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
1774 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1775 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
1776 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
1777 &const_index
, &indir_index
);
1779 if (instr
->dest
.ssa
.bit_size
== 64)
1782 switch (instr
->variables
[0]->var
->data
.mode
) {
1783 case nir_var_shader_in
:
1784 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1785 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1786 return load_tess_varyings(ctx
, instr
, true);
1789 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1790 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1791 LLVMValueRef indir_index
;
1792 unsigned const_index
, vertex_index
;
1793 get_deref_offset(ctx
, instr
->variables
[0],
1794 false, &vertex_index
, NULL
,
1795 &const_index
, &indir_index
);
1797 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
1798 instr
->variables
[0]->var
->data
.driver_location
,
1799 instr
->variables
[0]->var
->data
.location_frac
,
1800 instr
->num_components
, vertex_index
, const_index
, type
);
1803 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1805 unsigned count
= glsl_count_attribute_slots(
1806 instr
->variables
[0]->var
->type
,
1807 ctx
->stage
== MESA_SHADER_VERTEX
);
1809 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1810 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1811 stride
, false, true);
1813 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1817 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1821 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1823 unsigned count
= glsl_count_attribute_slots(
1824 instr
->variables
[0]->var
->type
, false);
1826 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1827 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1828 stride
, true, true);
1830 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1834 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1838 case nir_var_shared
: {
1839 LLVMValueRef address
= build_gep_for_deref(ctx
,
1840 instr
->variables
[0]);
1841 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1842 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1843 get_def_type(ctx
, &instr
->dest
.ssa
),
1846 case nir_var_shader_out
:
1847 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1848 return load_tess_varyings(ctx
, instr
, false);
1851 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1853 unsigned count
= glsl_count_attribute_slots(
1854 instr
->variables
[0]->var
->type
, false);
1856 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1857 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1858 stride
, true, true);
1860 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1864 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1865 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1871 unreachable("unhandle variable mode");
1873 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1874 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1878 visit_store_var(struct ac_nir_context
*ctx
,
1879 nir_intrinsic_instr
*instr
)
1881 LLVMValueRef temp_ptr
, value
;
1882 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1883 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1884 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
1885 int writemask
= instr
->const_index
[0];
1886 LLVMValueRef indir_index
;
1887 unsigned const_index
;
1888 get_deref_offset(ctx
, instr
->variables
[0], false,
1889 NULL
, NULL
, &const_index
, &indir_index
);
1891 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1893 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1894 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1897 writemask
= widen_mask(writemask
, 2);
1900 writemask
= writemask
<< comp
;
1902 switch (instr
->variables
[0]->var
->data
.mode
) {
1903 case nir_var_shader_out
:
1905 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1906 LLVMValueRef vertex_index
= NULL
;
1907 LLVMValueRef indir_index
= NULL
;
1908 unsigned const_index
= 0;
1909 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1911 get_deref_offset(ctx
, instr
->variables
[0],
1912 false, NULL
, is_patch
? NULL
: &vertex_index
,
1913 &const_index
, &indir_index
);
1915 ctx
->abi
->store_tcs_outputs(ctx
->abi
, instr
->variables
[0]->var
,
1916 vertex_index
, indir_index
,
1917 const_index
, src
, writemask
);
1921 for (unsigned chan
= 0; chan
< 8; chan
++) {
1923 if (!(writemask
& (1 << chan
)))
1926 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
1928 if (instr
->variables
[0]->var
->data
.compact
)
1931 unsigned count
= glsl_count_attribute_slots(
1932 instr
->variables
[0]->var
->type
, false);
1934 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1935 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1936 stride
, true, true);
1938 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1939 value
, indir_index
, "");
1940 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
1941 count
, stride
, tmp_vec
);
1944 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
1946 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1951 for (unsigned chan
= 0; chan
< 8; chan
++) {
1952 if (!(writemask
& (1 << chan
)))
1955 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
1957 unsigned count
= glsl_count_attribute_slots(
1958 instr
->variables
[0]->var
->type
, false);
1960 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1961 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1964 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1965 value
, indir_index
, "");
1966 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
1969 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
1971 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1975 case nir_var_shared
: {
1976 int writemask
= instr
->const_index
[0];
1977 LLVMValueRef address
= build_gep_for_deref(ctx
,
1978 instr
->variables
[0]);
1979 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
1980 unsigned components
=
1981 glsl_get_vector_elements(
1982 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
1983 if (writemask
== (1 << components
) - 1) {
1984 val
= LLVMBuildBitCast(
1985 ctx
->ac
.builder
, val
,
1986 LLVMGetElementType(LLVMTypeOf(address
)), "");
1987 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
1989 for (unsigned chan
= 0; chan
< 4; chan
++) {
1990 if (!(writemask
& (1 << chan
)))
1993 LLVMBuildStructGEP(ctx
->ac
.builder
,
1995 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
1997 src
= LLVMBuildBitCast(
1998 ctx
->ac
.builder
, src
,
1999 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2000 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2010 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2013 case GLSL_SAMPLER_DIM_BUF
:
2015 case GLSL_SAMPLER_DIM_1D
:
2016 return array
? 2 : 1;
2017 case GLSL_SAMPLER_DIM_2D
:
2018 return array
? 3 : 2;
2019 case GLSL_SAMPLER_DIM_MS
:
2020 return array
? 4 : 3;
2021 case GLSL_SAMPLER_DIM_3D
:
2022 case GLSL_SAMPLER_DIM_CUBE
:
2024 case GLSL_SAMPLER_DIM_RECT
:
2025 case GLSL_SAMPLER_DIM_SUBPASS
:
2027 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2036 /* Adjust the sample index according to FMASK.
2038 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2039 * which is the identity mapping. Each nibble says which physical sample
2040 * should be fetched to get that sample.
2042 * For example, 0x11111100 means there are only 2 samples stored and
2043 * the second sample covers 3/4 of the pixel. When reading samples 0
2044 * and 1, return physical sample 0 (determined by the first two 0s
2045 * in FMASK), otherwise return physical sample 1.
2047 * The sample index should be adjusted as follows:
2048 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2050 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2051 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2052 LLVMValueRef coord_z
,
2053 LLVMValueRef sample_index
,
2054 LLVMValueRef fmask_desc_ptr
)
2056 struct ac_image_args args
= {0};
2059 args
.coords
[0] = coord_x
;
2060 args
.coords
[1] = coord_y
;
2062 args
.coords
[2] = coord_z
;
2064 args
.opcode
= ac_image_load
;
2065 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2066 args
.resource
= fmask_desc_ptr
;
2068 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2070 res
= ac_build_image_opcode(ctx
, &args
);
2072 res
= ac_to_integer(ctx
, res
);
2073 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2074 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2076 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2080 LLVMValueRef sample_index4
=
2081 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2082 LLVMValueRef shifted_fmask
=
2083 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2084 LLVMValueRef final_sample
=
2085 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2087 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2088 * resource descriptor is 0 (invalid),
2090 LLVMValueRef fmask_desc
=
2091 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2094 LLVMValueRef fmask_word1
=
2095 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2098 LLVMValueRef word1_is_nonzero
=
2099 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2100 fmask_word1
, ctx
->i32_0
, "");
2102 /* Replace the MSAA sample index. */
2104 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2105 final_sample
, sample_index
, "");
2106 return sample_index
;
2109 static void get_image_coords(struct ac_nir_context
*ctx
,
2110 const nir_intrinsic_instr
*instr
,
2111 struct ac_image_args
*args
)
2113 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
2115 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2116 LLVMValueRef masks
[] = {
2117 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2118 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2120 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
2123 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2124 bool is_array
= glsl_sampler_type_is_array(type
);
2125 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2126 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2127 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2128 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2129 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2130 count
= image_type_to_components_count(dim
, is_array
);
2133 LLVMValueRef fmask_load_address
[3];
2136 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2137 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2139 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2141 fmask_load_address
[2] = NULL
;
2143 for (chan
= 0; chan
< 2; ++chan
)
2144 fmask_load_address
[chan
] =
2145 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2146 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2147 ctx
->ac
.i32
, ""), "");
2148 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2150 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2151 fmask_load_address
[0],
2152 fmask_load_address
[1],
2153 fmask_load_address
[2],
2155 get_sampler_desc(ctx
, instr
->variables
[0], NULL
, AC_DESC_FMASK
, NULL
, true, false));
2157 if (count
== 1 && !gfx9_1d
) {
2158 if (instr
->src
[0].ssa
->num_components
)
2159 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2161 args
->coords
[0] = src0
;
2166 for (chan
= 0; chan
< count
; ++chan
) {
2167 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2170 for (chan
= 0; chan
< 2; ++chan
) {
2171 args
->coords
[chan
] = LLVMBuildAdd(
2172 ctx
->ac
.builder
, args
->coords
[chan
],
2174 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2175 ctx
->ac
.i32
, ""), "");
2177 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2178 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2184 args
->coords
[2] = args
->coords
[1];
2185 args
->coords
[1] = ctx
->ac
.i32_0
;
2187 args
->coords
[1] = ctx
->ac
.i32_0
;
2192 args
->coords
[count
] = sample_index
;
2198 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2199 const nir_intrinsic_instr
*instr
, bool write
)
2201 LLVMValueRef rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
, AC_DESC_BUFFER
, NULL
, true, write
);
2202 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2203 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2204 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2205 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2207 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2208 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2209 elem_count
, stride
, "");
2211 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2212 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2217 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2218 const nir_intrinsic_instr
*instr
)
2221 const nir_variable
*var
= instr
->variables
[0]->var
;
2222 const struct glsl_type
*type
= var
->type
;
2224 if(instr
->variables
[0]->deref
.child
)
2225 type
= instr
->variables
[0]->deref
.child
->type
;
2227 type
= glsl_without_array(type
);
2229 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2230 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2231 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2232 unsigned num_channels
= util_last_bit(mask
);
2233 LLVMValueRef rsrc
, vindex
;
2235 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2236 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2239 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2240 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2241 ctx
->ac
.i32_0
, num_channels
,
2243 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2245 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2246 res
= ac_to_integer(&ctx
->ac
, res
);
2248 struct ac_image_args args
= {};
2249 args
.opcode
= ac_image_load
;
2250 get_image_coords(ctx
, instr
, &args
);
2251 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2252 AC_DESC_IMAGE
, NULL
, true, false);
2253 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2254 glsl_sampler_type_is_array(type
));
2256 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2257 if (var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2258 args
.cache_policy
|= ac_glc
;
2260 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2262 return ac_to_integer(&ctx
->ac
, res
);
2265 static void visit_image_store(struct ac_nir_context
*ctx
,
2266 nir_intrinsic_instr
*instr
)
2268 LLVMValueRef params
[8];
2269 const nir_variable
*var
= instr
->variables
[0]->var
;
2270 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2271 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2272 LLVMValueRef glc
= ctx
->ac
.i1false
;
2273 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2275 glc
= ctx
->ac
.i1true
;
2277 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2278 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2280 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
2282 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2283 ctx
->ac
.i32_0
, ""); /* vindex */
2284 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2285 params
[4] = glc
; /* glc */
2286 params
[5] = ctx
->ac
.i1false
; /* slc */
2287 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2290 struct ac_image_args args
= {};
2291 args
.opcode
= ac_image_store
;
2292 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
2293 get_image_coords(ctx
, instr
, &args
);
2294 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2295 AC_DESC_IMAGE
, NULL
, true, false);
2296 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2297 glsl_sampler_type_is_array(type
));
2299 if (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2300 args
.cache_policy
|= ac_glc
;
2302 ac_build_image_opcode(&ctx
->ac
, &args
);
2307 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2308 const nir_intrinsic_instr
*instr
)
2310 LLVMValueRef params
[7];
2311 int param_count
= 0;
2312 const nir_variable
*var
= instr
->variables
[0]->var
;
2314 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_var_atomic_comp_swap
;
2315 const char *atomic_name
;
2316 char intrinsic_name
[41];
2317 enum ac_atomic_op atomic_subop
;
2318 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2319 MAYBE_UNUSED
int length
;
2321 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2323 switch (instr
->intrinsic
) {
2324 case nir_intrinsic_image_var_atomic_add
:
2325 atomic_name
= "add";
2326 atomic_subop
= ac_atomic_add
;
2328 case nir_intrinsic_image_var_atomic_min
:
2329 atomic_name
= is_unsigned
? "umin" : "smin";
2330 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2332 case nir_intrinsic_image_var_atomic_max
:
2333 atomic_name
= is_unsigned
? "umax" : "smax";
2334 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2336 case nir_intrinsic_image_var_atomic_and
:
2337 atomic_name
= "and";
2338 atomic_subop
= ac_atomic_and
;
2340 case nir_intrinsic_image_var_atomic_or
:
2342 atomic_subop
= ac_atomic_or
;
2344 case nir_intrinsic_image_var_atomic_xor
:
2345 atomic_name
= "xor";
2346 atomic_subop
= ac_atomic_xor
;
2348 case nir_intrinsic_image_var_atomic_exchange
:
2349 atomic_name
= "swap";
2350 atomic_subop
= ac_atomic_swap
;
2352 case nir_intrinsic_image_var_atomic_comp_swap
:
2353 atomic_name
= "cmpswap";
2354 atomic_subop
= 0; /* not used */
2361 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2362 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2364 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2365 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2366 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2367 ctx
->ac
.i32_0
, ""); /* vindex */
2368 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2369 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2371 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2372 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2374 assert(length
< sizeof(intrinsic_name
));
2375 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2376 params
, param_count
, 0);
2378 struct ac_image_args args
= {};
2379 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2380 args
.atomic
= atomic_subop
;
2381 args
.data
[0] = params
[0];
2383 args
.data
[1] = params
[1];
2384 get_image_coords(ctx
, instr
, &args
);
2385 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2386 AC_DESC_IMAGE
, NULL
, true, false);
2387 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2388 glsl_sampler_type_is_array(type
));
2390 return ac_build_image_opcode(&ctx
->ac
, &args
);
2394 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2395 const nir_intrinsic_instr
*instr
)
2397 const nir_variable
*var
= instr
->variables
[0]->var
;
2398 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2400 struct ac_image_args args
= { 0 };
2401 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2402 glsl_sampler_type_is_array(type
));
2404 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2405 AC_DESC_IMAGE
, NULL
, true, false);
2406 args
.opcode
= ac_image_get_resinfo
;
2407 args
.lod
= ctx
->ac
.i32_0
;
2408 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2410 return ac_build_image_opcode(&ctx
->ac
, &args
);
2413 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2414 const nir_intrinsic_instr
*instr
)
2417 const nir_variable
*var
= instr
->variables
[0]->var
;
2418 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2420 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2421 return get_buffer_size(ctx
,
2422 get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2423 AC_DESC_BUFFER
, NULL
, true, false), true);
2425 struct ac_image_args args
= { 0 };
2427 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2428 glsl_sampler_type_is_array(type
));
2430 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
, AC_DESC_IMAGE
, NULL
, true, false);
2431 args
.opcode
= ac_image_get_resinfo
;
2432 args
.lod
= ctx
->ac
.i32_0
;
2433 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2435 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2437 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2439 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2440 glsl_sampler_type_is_array(type
)) {
2441 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2442 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2443 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2444 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2446 if (ctx
->ac
.chip_class
>= GFX9
&&
2447 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2448 glsl_sampler_type_is_array(type
)) {
2449 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2450 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2457 #define NOOP_WAITCNT 0xf7f
2458 #define LGKM_CNT 0x07f
2459 #define VM_CNT 0xf70
2461 static void emit_membar(struct ac_llvm_context
*ac
,
2462 const nir_intrinsic_instr
*instr
)
2464 unsigned waitcnt
= NOOP_WAITCNT
;
2466 switch (instr
->intrinsic
) {
2467 case nir_intrinsic_memory_barrier
:
2468 case nir_intrinsic_group_memory_barrier
:
2469 waitcnt
&= VM_CNT
& LGKM_CNT
;
2471 case nir_intrinsic_memory_barrier_atomic_counter
:
2472 case nir_intrinsic_memory_barrier_buffer
:
2473 case nir_intrinsic_memory_barrier_image
:
2476 case nir_intrinsic_memory_barrier_shared
:
2477 waitcnt
&= LGKM_CNT
;
2482 if (waitcnt
!= NOOP_WAITCNT
)
2483 ac_build_waitcnt(ac
, waitcnt
);
2486 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2488 /* SI only (thanks to a hw bug workaround):
2489 * The real barrier instruction isn’t needed, because an entire patch
2490 * always fits into a single wave.
2492 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2493 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2496 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2497 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2500 static void emit_discard(struct ac_nir_context
*ctx
,
2501 const nir_intrinsic_instr
*instr
)
2505 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2506 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2507 get_src(ctx
, instr
->src
[0]),
2510 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2511 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2514 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2518 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2520 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2521 "llvm.amdgcn.ps.live",
2522 ctx
->ac
.i1
, NULL
, 0,
2523 AC_FUNC_ATTR_READNONE
);
2524 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2525 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2529 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2531 LLVMValueRef result
;
2532 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2533 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2534 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2536 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2540 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2542 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2543 LLVMValueRef result
;
2544 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2545 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2546 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2548 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2553 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2555 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2556 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2557 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2559 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2564 visit_first_invocation(struct ac_nir_context
*ctx
)
2566 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2568 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2569 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2570 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2572 ctx
->ac
.i64
, args
, 2,
2573 AC_FUNC_ATTR_NOUNWIND
|
2574 AC_FUNC_ATTR_READNONE
);
2576 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2580 visit_load_shared(struct ac_nir_context
*ctx
,
2581 const nir_intrinsic_instr
*instr
)
2583 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2585 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2587 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2588 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2589 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2590 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2593 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2594 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2598 visit_store_shared(struct ac_nir_context
*ctx
,
2599 const nir_intrinsic_instr
*instr
)
2601 LLVMValueRef derived_ptr
, data
,index
;
2602 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2604 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2605 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2607 int writemask
= nir_intrinsic_write_mask(instr
);
2608 for (int chan
= 0; chan
< 4; chan
++) {
2609 if (!(writemask
& (1 << chan
))) {
2612 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2613 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2614 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2615 LLVMBuildStore(builder
, data
, derived_ptr
);
2619 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2620 const nir_intrinsic_instr
*instr
,
2621 LLVMValueRef ptr
, int src_idx
)
2623 LLVMValueRef result
;
2624 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2626 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
||
2627 instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
) {
2628 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2629 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2631 LLVMAtomicOrderingSequentiallyConsistent
,
2632 LLVMAtomicOrderingSequentiallyConsistent
,
2634 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2636 LLVMAtomicRMWBinOp op
;
2637 switch (instr
->intrinsic
) {
2638 case nir_intrinsic_var_atomic_add
:
2639 case nir_intrinsic_shared_atomic_add
:
2640 op
= LLVMAtomicRMWBinOpAdd
;
2642 case nir_intrinsic_var_atomic_umin
:
2643 case nir_intrinsic_shared_atomic_umin
:
2644 op
= LLVMAtomicRMWBinOpUMin
;
2646 case nir_intrinsic_var_atomic_umax
:
2647 case nir_intrinsic_shared_atomic_umax
:
2648 op
= LLVMAtomicRMWBinOpUMax
;
2650 case nir_intrinsic_var_atomic_imin
:
2651 case nir_intrinsic_shared_atomic_imin
:
2652 op
= LLVMAtomicRMWBinOpMin
;
2654 case nir_intrinsic_var_atomic_imax
:
2655 case nir_intrinsic_shared_atomic_imax
:
2656 op
= LLVMAtomicRMWBinOpMax
;
2658 case nir_intrinsic_var_atomic_and
:
2659 case nir_intrinsic_shared_atomic_and
:
2660 op
= LLVMAtomicRMWBinOpAnd
;
2662 case nir_intrinsic_var_atomic_or
:
2663 case nir_intrinsic_shared_atomic_or
:
2664 op
= LLVMAtomicRMWBinOpOr
;
2666 case nir_intrinsic_var_atomic_xor
:
2667 case nir_intrinsic_shared_atomic_xor
:
2668 op
= LLVMAtomicRMWBinOpXor
;
2670 case nir_intrinsic_var_atomic_exchange
:
2671 case nir_intrinsic_shared_atomic_exchange
:
2672 op
= LLVMAtomicRMWBinOpXchg
;
2678 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2679 LLVMAtomicOrderingSequentiallyConsistent
,
2685 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2687 LLVMValueRef values
[2];
2688 LLVMValueRef pos
[2];
2690 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2691 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2693 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2694 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2695 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2698 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2699 const nir_intrinsic_instr
*instr
)
2701 LLVMValueRef result
[4];
2702 LLVMValueRef interp_param
, attr_number
;
2705 LLVMValueRef src_c0
= NULL
;
2706 LLVMValueRef src_c1
= NULL
;
2707 LLVMValueRef src0
= NULL
;
2708 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2709 switch (instr
->intrinsic
) {
2710 case nir_intrinsic_interp_var_at_centroid
:
2711 location
= INTERP_CENTROID
;
2713 case nir_intrinsic_interp_var_at_sample
:
2714 case nir_intrinsic_interp_var_at_offset
:
2715 location
= INTERP_CENTER
;
2716 src0
= get_src(ctx
, instr
->src
[0]);
2722 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2723 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2724 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2725 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2726 LLVMValueRef sample_position
;
2727 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2729 /* fetch sample ID */
2730 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2732 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2733 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2734 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2735 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2737 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2738 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2740 if (location
== INTERP_CENTER
) {
2741 LLVMValueRef ij_out
[2];
2742 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2745 * take the I then J parameters, and the DDX/Y for it, and
2746 * calculate the IJ inputs for the interpolator.
2747 * temp1 = ddx * offset/sample.x + I;
2748 * interp_param.I = ddy * offset/sample.y + temp1;
2749 * temp1 = ddx * offset/sample.x + J;
2750 * interp_param.J = ddy * offset/sample.y + temp1;
2752 for (unsigned i
= 0; i
< 2; i
++) {
2753 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2754 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2755 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2756 ddxy_out
, ix_ll
, "");
2757 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2758 ddxy_out
, iy_ll
, "");
2759 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2760 interp_param
, ix_ll
, "");
2761 LLVMValueRef temp1
, temp2
;
2763 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2766 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2767 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2769 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2770 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2772 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2773 temp2
, ctx
->ac
.i32
, "");
2775 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2779 for (chan
= 0; chan
< 4; chan
++) {
2780 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2783 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2784 interp_param
, ctx
->ac
.v2f32
, "");
2785 LLVMValueRef i
= LLVMBuildExtractElement(
2786 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2787 LLVMValueRef j
= LLVMBuildExtractElement(
2788 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2790 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2791 llvm_chan
, attr_number
,
2792 ctx
->abi
->prim_mask
, i
, j
);
2794 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2795 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2796 llvm_chan
, attr_number
,
2797 ctx
->abi
->prim_mask
);
2800 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2801 instr
->variables
[0]->var
->data
.location_frac
);
2804 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2805 nir_intrinsic_instr
*instr
)
2807 LLVMValueRef result
= NULL
;
2809 switch (instr
->intrinsic
) {
2810 case nir_intrinsic_ballot
:
2811 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2813 case nir_intrinsic_read_invocation
:
2814 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2815 get_src(ctx
, instr
->src
[1]));
2817 case nir_intrinsic_read_first_invocation
:
2818 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2820 case nir_intrinsic_load_subgroup_invocation
:
2821 result
= ac_get_thread_id(&ctx
->ac
);
2823 case nir_intrinsic_load_work_group_id
: {
2824 LLVMValueRef values
[3];
2826 for (int i
= 0; i
< 3; i
++) {
2827 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2828 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2831 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2834 case nir_intrinsic_load_base_vertex
:
2835 case nir_intrinsic_load_first_vertex
:
2836 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2838 case nir_intrinsic_load_local_group_size
:
2839 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2841 case nir_intrinsic_load_vertex_id
:
2842 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2843 ctx
->abi
->base_vertex
, "");
2845 case nir_intrinsic_load_vertex_id_zero_base
: {
2846 result
= ctx
->abi
->vertex_id
;
2849 case nir_intrinsic_load_local_invocation_id
: {
2850 result
= ctx
->abi
->local_invocation_ids
;
2853 case nir_intrinsic_load_base_instance
:
2854 result
= ctx
->abi
->start_instance
;
2856 case nir_intrinsic_load_draw_id
:
2857 result
= ctx
->abi
->draw_id
;
2859 case nir_intrinsic_load_view_index
:
2860 result
= ctx
->abi
->view_index
;
2862 case nir_intrinsic_load_invocation_id
:
2863 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2864 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2866 result
= ctx
->abi
->gs_invocation_id
;
2868 case nir_intrinsic_load_primitive_id
:
2869 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2870 result
= ctx
->abi
->gs_prim_id
;
2871 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2872 result
= ctx
->abi
->tcs_patch_id
;
2873 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2874 result
= ctx
->abi
->tes_patch_id
;
2876 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2878 case nir_intrinsic_load_sample_id
:
2879 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2881 case nir_intrinsic_load_sample_pos
:
2882 result
= load_sample_pos(ctx
);
2884 case nir_intrinsic_load_sample_mask_in
:
2885 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2887 case nir_intrinsic_load_frag_coord
: {
2888 LLVMValueRef values
[4] = {
2889 ctx
->abi
->frag_pos
[0],
2890 ctx
->abi
->frag_pos
[1],
2891 ctx
->abi
->frag_pos
[2],
2892 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2894 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2897 case nir_intrinsic_load_front_face
:
2898 result
= ctx
->abi
->front_face
;
2900 case nir_intrinsic_load_helper_invocation
:
2901 result
= visit_load_helper_invocation(ctx
);
2903 case nir_intrinsic_load_instance_id
:
2904 result
= ctx
->abi
->instance_id
;
2906 case nir_intrinsic_load_num_work_groups
:
2907 result
= ctx
->abi
->num_work_groups
;
2909 case nir_intrinsic_load_local_invocation_index
:
2910 result
= visit_load_local_invocation_index(ctx
);
2912 case nir_intrinsic_load_subgroup_id
:
2913 result
= visit_load_subgroup_id(ctx
);
2915 case nir_intrinsic_load_num_subgroups
:
2916 result
= visit_load_num_subgroups(ctx
);
2918 case nir_intrinsic_first_invocation
:
2919 result
= visit_first_invocation(ctx
);
2921 case nir_intrinsic_load_push_constant
:
2922 result
= visit_load_push_constant(ctx
, instr
);
2924 case nir_intrinsic_vulkan_resource_index
: {
2925 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2926 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2927 unsigned binding
= nir_intrinsic_binding(instr
);
2929 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
2933 case nir_intrinsic_vulkan_resource_reindex
:
2934 result
= visit_vulkan_resource_reindex(ctx
, instr
);
2936 case nir_intrinsic_store_ssbo
:
2937 visit_store_ssbo(ctx
, instr
);
2939 case nir_intrinsic_load_ssbo
:
2940 result
= visit_load_buffer(ctx
, instr
);
2942 case nir_intrinsic_ssbo_atomic_add
:
2943 case nir_intrinsic_ssbo_atomic_imin
:
2944 case nir_intrinsic_ssbo_atomic_umin
:
2945 case nir_intrinsic_ssbo_atomic_imax
:
2946 case nir_intrinsic_ssbo_atomic_umax
:
2947 case nir_intrinsic_ssbo_atomic_and
:
2948 case nir_intrinsic_ssbo_atomic_or
:
2949 case nir_intrinsic_ssbo_atomic_xor
:
2950 case nir_intrinsic_ssbo_atomic_exchange
:
2951 case nir_intrinsic_ssbo_atomic_comp_swap
:
2952 result
= visit_atomic_ssbo(ctx
, instr
);
2954 case nir_intrinsic_load_ubo
:
2955 result
= visit_load_ubo_buffer(ctx
, instr
);
2957 case nir_intrinsic_get_buffer_size
:
2958 result
= visit_get_buffer_size(ctx
, instr
);
2960 case nir_intrinsic_load_var
:
2961 result
= visit_load_var(ctx
, instr
);
2963 case nir_intrinsic_store_var
:
2964 visit_store_var(ctx
, instr
);
2966 case nir_intrinsic_load_shared
:
2967 result
= visit_load_shared(ctx
, instr
);
2969 case nir_intrinsic_store_shared
:
2970 visit_store_shared(ctx
, instr
);
2972 case nir_intrinsic_image_var_samples
:
2973 result
= visit_image_samples(ctx
, instr
);
2975 case nir_intrinsic_image_var_load
:
2976 result
= visit_image_load(ctx
, instr
);
2978 case nir_intrinsic_image_var_store
:
2979 visit_image_store(ctx
, instr
);
2981 case nir_intrinsic_image_var_atomic_add
:
2982 case nir_intrinsic_image_var_atomic_min
:
2983 case nir_intrinsic_image_var_atomic_max
:
2984 case nir_intrinsic_image_var_atomic_and
:
2985 case nir_intrinsic_image_var_atomic_or
:
2986 case nir_intrinsic_image_var_atomic_xor
:
2987 case nir_intrinsic_image_var_atomic_exchange
:
2988 case nir_intrinsic_image_var_atomic_comp_swap
:
2989 result
= visit_image_atomic(ctx
, instr
);
2991 case nir_intrinsic_image_var_size
:
2992 result
= visit_image_size(ctx
, instr
);
2994 case nir_intrinsic_shader_clock
:
2995 result
= ac_build_shader_clock(&ctx
->ac
);
2997 case nir_intrinsic_discard
:
2998 case nir_intrinsic_discard_if
:
2999 emit_discard(ctx
, instr
);
3001 case nir_intrinsic_memory_barrier
:
3002 case nir_intrinsic_group_memory_barrier
:
3003 case nir_intrinsic_memory_barrier_atomic_counter
:
3004 case nir_intrinsic_memory_barrier_buffer
:
3005 case nir_intrinsic_memory_barrier_image
:
3006 case nir_intrinsic_memory_barrier_shared
:
3007 emit_membar(&ctx
->ac
, instr
);
3009 case nir_intrinsic_barrier
:
3010 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3012 case nir_intrinsic_shared_atomic_add
:
3013 case nir_intrinsic_shared_atomic_imin
:
3014 case nir_intrinsic_shared_atomic_umin
:
3015 case nir_intrinsic_shared_atomic_imax
:
3016 case nir_intrinsic_shared_atomic_umax
:
3017 case nir_intrinsic_shared_atomic_and
:
3018 case nir_intrinsic_shared_atomic_or
:
3019 case nir_intrinsic_shared_atomic_xor
:
3020 case nir_intrinsic_shared_atomic_exchange
:
3021 case nir_intrinsic_shared_atomic_comp_swap
: {
3022 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3023 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3026 case nir_intrinsic_var_atomic_add
:
3027 case nir_intrinsic_var_atomic_imin
:
3028 case nir_intrinsic_var_atomic_umin
:
3029 case nir_intrinsic_var_atomic_imax
:
3030 case nir_intrinsic_var_atomic_umax
:
3031 case nir_intrinsic_var_atomic_and
:
3032 case nir_intrinsic_var_atomic_or
:
3033 case nir_intrinsic_var_atomic_xor
:
3034 case nir_intrinsic_var_atomic_exchange
:
3035 case nir_intrinsic_var_atomic_comp_swap
: {
3036 LLVMValueRef ptr
= build_gep_for_deref(ctx
, instr
->variables
[0]);
3037 result
= visit_var_atomic(ctx
, instr
, ptr
, 0);
3040 case nir_intrinsic_interp_var_at_centroid
:
3041 case nir_intrinsic_interp_var_at_sample
:
3042 case nir_intrinsic_interp_var_at_offset
:
3043 result
= visit_interp(ctx
, instr
);
3045 case nir_intrinsic_emit_vertex
:
3046 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3048 case nir_intrinsic_end_primitive
:
3049 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3051 case nir_intrinsic_load_tess_coord
:
3052 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3054 case nir_intrinsic_load_tess_level_outer
:
3055 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3057 case nir_intrinsic_load_tess_level_inner
:
3058 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3060 case nir_intrinsic_load_patch_vertices_in
:
3061 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3063 case nir_intrinsic_vote_all
: {
3064 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3065 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3068 case nir_intrinsic_vote_any
: {
3069 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3070 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3073 case nir_intrinsic_shuffle
:
3074 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3075 get_src(ctx
, instr
->src
[1]));
3077 case nir_intrinsic_reduce
:
3078 result
= ac_build_reduce(&ctx
->ac
,
3079 get_src(ctx
, instr
->src
[0]),
3080 instr
->const_index
[0],
3081 instr
->const_index
[1]);
3083 case nir_intrinsic_inclusive_scan
:
3084 result
= ac_build_inclusive_scan(&ctx
->ac
,
3085 get_src(ctx
, instr
->src
[0]),
3086 instr
->const_index
[0]);
3088 case nir_intrinsic_exclusive_scan
:
3089 result
= ac_build_exclusive_scan(&ctx
->ac
,
3090 get_src(ctx
, instr
->src
[0]),
3091 instr
->const_index
[0]);
3093 case nir_intrinsic_quad_broadcast
: {
3094 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3095 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3096 lane
, lane
, lane
, lane
);
3099 case nir_intrinsic_quad_swap_horizontal
:
3100 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3102 case nir_intrinsic_quad_swap_vertical
:
3103 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3105 case nir_intrinsic_quad_swap_diagonal
:
3106 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3109 fprintf(stderr
, "Unknown intrinsic: ");
3110 nir_print_instr(&instr
->instr
, stderr
);
3111 fprintf(stderr
, "\n");
3115 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3119 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3120 const nir_deref_var
*deref
,
3121 nir_deref_instr
*deref_instr
,
3122 enum ac_descriptor_type desc_type
,
3123 const nir_tex_instr
*tex_instr
,
3124 bool image
, bool write
)
3126 LLVMValueRef index
= NULL
;
3127 unsigned constant_index
= 0;
3128 unsigned descriptor_set
;
3129 unsigned base_index
;
3130 bool bindless
= false;
3132 if (!deref
&& !deref_instr
) {
3133 assert(tex_instr
&& !image
);
3135 base_index
= tex_instr
->sampler_index
;
3136 } else if(deref_instr
) {
3137 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3138 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3142 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3143 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3145 constant_index
+= array_size
* const_value
->u32
[0];
3147 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3149 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3150 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3155 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3158 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3160 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3161 base_index
= deref_instr
->var
->data
.binding
;
3163 const nir_deref
*tail
= &deref
->deref
;
3164 while (tail
->child
) {
3165 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
3166 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
3171 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3173 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3174 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
3176 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3177 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3182 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3185 constant_index
+= child
->base_offset
* array_size
;
3187 tail
= &child
->deref
;
3189 descriptor_set
= deref
->var
->data
.descriptor_set
;
3191 if (deref
->var
->data
.bindless
) {
3192 bindless
= deref
->var
->data
.bindless
;
3193 base_index
= deref
->var
->data
.driver_location
;
3195 base_index
= deref
->var
->data
.binding
;
3199 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3202 constant_index
, index
,
3203 desc_type
, image
, write
, bindless
);
3206 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3209 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3210 * filtering manually. The driver sets img7 to a mask clearing
3211 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3212 * s_and_b32 samp0, samp0, img7
3215 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3217 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3218 LLVMValueRef res
, LLVMValueRef samp
)
3220 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3221 LLVMValueRef img7
, samp0
;
3223 if (ctx
->ac
.chip_class
>= VI
)
3226 img7
= LLVMBuildExtractElement(builder
, res
,
3227 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3228 samp0
= LLVMBuildExtractElement(builder
, samp
,
3229 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3230 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3231 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3232 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3235 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3236 nir_tex_instr
*instr
,
3237 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3238 LLVMValueRef
*fmask_ptr
)
3240 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3241 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, NULL
, AC_DESC_BUFFER
, instr
, false, false);
3243 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, NULL
, AC_DESC_IMAGE
, instr
, false, false);
3246 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, NULL
, AC_DESC_SAMPLER
, instr
, false, false);
3248 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, NULL
, AC_DESC_SAMPLER
, instr
, false, false);
3249 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3250 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3252 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3253 instr
->op
== nir_texop_samples_identical
))
3254 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, NULL
, AC_DESC_FMASK
, instr
, false, false);
3257 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3260 coord
= ac_to_float(ctx
, coord
);
3261 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3262 coord
= ac_to_integer(ctx
, coord
);
3266 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3268 LLVMValueRef result
= NULL
;
3269 struct ac_image_args args
= { 0 };
3270 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3271 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3272 unsigned offset_src
= 0;
3274 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3276 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3277 switch (instr
->src
[i
].src_type
) {
3278 case nir_tex_src_coord
: {
3279 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3280 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3281 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3284 case nir_tex_src_projector
:
3286 case nir_tex_src_comparator
:
3287 if (instr
->is_shadow
)
3288 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3290 case nir_tex_src_offset
:
3291 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3294 case nir_tex_src_bias
:
3295 if (instr
->op
== nir_texop_txb
)
3296 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3298 case nir_tex_src_lod
: {
3299 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3301 if (val
&& val
->i32
[0] == 0)
3302 args
.level_zero
= true;
3304 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3307 case nir_tex_src_ms_index
:
3308 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3310 case nir_tex_src_ms_mcs
:
3312 case nir_tex_src_ddx
:
3313 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3315 case nir_tex_src_ddy
:
3316 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3318 case nir_tex_src_texture_offset
:
3319 case nir_tex_src_sampler_offset
:
3320 case nir_tex_src_plane
:
3326 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3327 result
= get_buffer_size(ctx
, args
.resource
, true);
3331 if (instr
->op
== nir_texop_texture_samples
) {
3332 LLVMValueRef res
, samples
, is_msaa
;
3333 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3334 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3335 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3336 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3337 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3338 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3339 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3340 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3341 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3343 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3344 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3345 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3346 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3347 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3349 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3355 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3356 LLVMValueRef offset
[3], pack
;
3357 for (unsigned chan
= 0; chan
< 3; ++chan
)
3358 offset
[chan
] = ctx
->ac
.i32_0
;
3360 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3361 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3362 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3363 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3364 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3366 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3367 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3369 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3370 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3374 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3375 * so the depth comparison value isn't clamped for Z16 and
3376 * Z24 anymore. Do it manually here.
3378 * It's unnecessary if the original texture format was
3379 * Z32_FLOAT, but we don't know that here.
3381 if (args
.compare
&& ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3382 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3384 /* pack derivatives */
3386 int num_src_deriv_channels
, num_dest_deriv_channels
;
3387 switch (instr
->sampler_dim
) {
3388 case GLSL_SAMPLER_DIM_3D
:
3389 case GLSL_SAMPLER_DIM_CUBE
:
3390 num_src_deriv_channels
= 3;
3391 num_dest_deriv_channels
= 3;
3393 case GLSL_SAMPLER_DIM_2D
:
3395 num_src_deriv_channels
= 2;
3396 num_dest_deriv_channels
= 2;
3398 case GLSL_SAMPLER_DIM_1D
:
3399 num_src_deriv_channels
= 1;
3400 if (ctx
->ac
.chip_class
>= GFX9
) {
3401 num_dest_deriv_channels
= 2;
3403 num_dest_deriv_channels
= 1;
3408 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3409 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3410 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3411 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3412 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3414 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3415 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3416 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3420 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3421 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3422 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3423 if (instr
->coord_components
== 3)
3424 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3425 ac_prepare_cube_coords(&ctx
->ac
,
3426 instr
->op
== nir_texop_txd
, instr
->is_array
,
3427 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3430 /* Texture coordinates fixups */
3431 if (instr
->coord_components
> 1 &&
3432 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3434 instr
->op
!= nir_texop_txf
) {
3435 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3438 if (instr
->coord_components
> 2 &&
3439 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3440 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3441 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3442 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3444 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3445 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3448 if (ctx
->ac
.chip_class
>= GFX9
&&
3449 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3450 instr
->op
!= nir_texop_lod
) {
3451 LLVMValueRef filler
;
3452 if (instr
->op
== nir_texop_txf
)
3453 filler
= ctx
->ac
.i32_0
;
3455 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3457 if (instr
->is_array
)
3458 args
.coords
[2] = args
.coords
[1];
3459 args
.coords
[1] = filler
;
3462 /* Pack sample index */
3463 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3464 args
.coords
[instr
->coord_components
] = sample_index
;
3466 if (instr
->op
== nir_texop_samples_identical
) {
3467 struct ac_image_args txf_args
= { 0 };
3468 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3470 txf_args
.dmask
= 0xf;
3471 txf_args
.resource
= fmask_ptr
;
3472 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3473 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3475 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3476 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3480 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3481 instr
->op
!= nir_texop_txs
) {
3482 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3483 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3484 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3485 instr
->is_array
? args
.coords
[2] : NULL
,
3486 args
.coords
[sample_chan
], fmask_ptr
);
3489 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3490 nir_const_value
*const_offset
=
3491 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3492 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3493 assert(const_offset
);
3494 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3495 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3496 args
.coords
[i
] = LLVMBuildAdd(
3497 ctx
->ac
.builder
, args
.coords
[i
],
3498 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3503 /* TODO TG4 support */
3505 if (instr
->op
== nir_texop_tg4
) {
3506 if (instr
->is_shadow
)
3509 args
.dmask
= 1 << instr
->component
;
3512 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3513 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3514 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3516 if (instr
->op
== nir_texop_query_levels
)
3517 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3518 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3519 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3520 instr
->op
!= nir_texop_tg4
)
3521 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3522 else if (instr
->op
== nir_texop_txs
&&
3523 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3525 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3526 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3527 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3528 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3529 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3530 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3531 instr
->op
== nir_texop_txs
&&
3532 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3534 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3535 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3536 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3538 } else if (instr
->dest
.ssa
.num_components
!= 4)
3539 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3543 assert(instr
->dest
.is_ssa
);
3544 result
= ac_to_integer(&ctx
->ac
, result
);
3545 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3550 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3552 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3553 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3555 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3556 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3559 static void visit_post_phi(struct ac_nir_context
*ctx
,
3560 nir_phi_instr
*instr
,
3561 LLVMValueRef llvm_phi
)
3563 nir_foreach_phi_src(src
, instr
) {
3564 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3565 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3567 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3571 static void phi_post_pass(struct ac_nir_context
*ctx
)
3573 struct hash_entry
*entry
;
3574 hash_table_foreach(ctx
->phis
, entry
) {
3575 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3576 (LLVMValueRef
)entry
->data
);
3581 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3582 const nir_ssa_undef_instr
*instr
)
3584 unsigned num_components
= instr
->def
.num_components
;
3585 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3588 if (num_components
== 1)
3589 undef
= LLVMGetUndef(type
);
3591 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3593 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3596 static void visit_jump(struct ac_llvm_context
*ctx
,
3597 const nir_jump_instr
*instr
)
3599 switch (instr
->type
) {
3600 case nir_jump_break
:
3601 ac_build_break(ctx
);
3603 case nir_jump_continue
:
3604 ac_build_continue(ctx
);
3607 fprintf(stderr
, "Unknown NIR jump instr: ");
3608 nir_print_instr(&instr
->instr
, stderr
);
3609 fprintf(stderr
, "\n");
3614 static void visit_deref(struct ac_nir_context
*ctx
,
3615 nir_deref_instr
*instr
)
3617 if (instr
->mode
!= nir_var_shared
)
3620 LLVMValueRef result
= NULL
;
3621 switch(instr
->deref_type
) {
3622 case nir_deref_type_var
: {
3623 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
3624 result
= entry
->data
;
3627 case nir_deref_type_struct
:
3628 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3629 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
3631 case nir_deref_type_array
:
3632 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3633 get_src(ctx
, instr
->arr
.index
));
3636 unreachable("Unhandled deref_instr deref type");
3639 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3642 static void visit_cf_list(struct ac_nir_context
*ctx
,
3643 struct exec_list
*list
);
3645 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3647 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3648 nir_foreach_instr(instr
, block
)
3650 switch (instr
->type
) {
3651 case nir_instr_type_alu
:
3652 visit_alu(ctx
, nir_instr_as_alu(instr
));
3654 case nir_instr_type_load_const
:
3655 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3657 case nir_instr_type_intrinsic
:
3658 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3660 case nir_instr_type_tex
:
3661 visit_tex(ctx
, nir_instr_as_tex(instr
));
3663 case nir_instr_type_phi
:
3664 visit_phi(ctx
, nir_instr_as_phi(instr
));
3666 case nir_instr_type_ssa_undef
:
3667 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3669 case nir_instr_type_jump
:
3670 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3672 case nir_instr_type_deref
:
3673 visit_deref(ctx
, nir_instr_as_deref(instr
));
3676 fprintf(stderr
, "Unknown NIR instr type: ");
3677 nir_print_instr(instr
, stderr
);
3678 fprintf(stderr
, "\n");
3683 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3686 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3688 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3690 nir_block
*then_block
=
3691 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3693 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3695 visit_cf_list(ctx
, &if_stmt
->then_list
);
3697 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3698 nir_block
*else_block
=
3699 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3701 ac_build_else(&ctx
->ac
, else_block
->index
);
3702 visit_cf_list(ctx
, &if_stmt
->else_list
);
3705 ac_build_endif(&ctx
->ac
, then_block
->index
);
3708 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3710 nir_block
*first_loop_block
=
3711 (nir_block
*) exec_list_get_head(&loop
->body
);
3713 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3715 visit_cf_list(ctx
, &loop
->body
);
3717 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3720 static void visit_cf_list(struct ac_nir_context
*ctx
,
3721 struct exec_list
*list
)
3723 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3725 switch (node
->type
) {
3726 case nir_cf_node_block
:
3727 visit_block(ctx
, nir_cf_node_as_block(node
));
3730 case nir_cf_node_if
:
3731 visit_if(ctx
, nir_cf_node_as_if(node
));
3734 case nir_cf_node_loop
:
3735 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3745 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3746 struct ac_shader_abi
*abi
,
3747 struct nir_shader
*nir
,
3748 struct nir_variable
*variable
,
3749 gl_shader_stage stage
)
3751 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3752 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3754 /* tess ctrl has it's own load/store paths for outputs */
3755 if (stage
== MESA_SHADER_TESS_CTRL
)
3758 if (stage
== MESA_SHADER_VERTEX
||
3759 stage
== MESA_SHADER_TESS_EVAL
||
3760 stage
== MESA_SHADER_GEOMETRY
) {
3761 int idx
= variable
->data
.location
+ variable
->data
.index
;
3762 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3763 int length
= nir
->info
.clip_distance_array_size
+
3764 nir
->info
.cull_distance_array_size
;
3773 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3774 for (unsigned chan
= 0; chan
< 4; chan
++) {
3775 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3776 ac_build_alloca_undef(ctx
, ctx
->f32
, "");
3782 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3783 enum glsl_base_type type
)
3787 case GLSL_TYPE_UINT
:
3788 case GLSL_TYPE_BOOL
:
3789 case GLSL_TYPE_SUBROUTINE
:
3791 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3793 case GLSL_TYPE_INT64
:
3794 case GLSL_TYPE_UINT64
:
3796 case GLSL_TYPE_DOUBLE
:
3799 unreachable("unknown GLSL type");
3804 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3805 const struct glsl_type
*type
)
3807 if (glsl_type_is_scalar(type
)) {
3808 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3811 if (glsl_type_is_vector(type
)) {
3812 return LLVMVectorType(
3813 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3814 glsl_get_vector_elements(type
));
3817 if (glsl_type_is_matrix(type
)) {
3818 return LLVMArrayType(
3819 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3820 glsl_get_matrix_columns(type
));
3823 if (glsl_type_is_array(type
)) {
3824 return LLVMArrayType(
3825 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3826 glsl_get_length(type
));
3829 assert(glsl_type_is_struct(type
));
3831 LLVMTypeRef member_types
[glsl_get_length(type
)];
3833 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3835 glsl_to_llvm_type(ac
,
3836 glsl_get_struct_field(type
, i
));
3839 return LLVMStructTypeInContext(ac
->context
, member_types
,
3840 glsl_get_length(type
), false);
3844 setup_locals(struct ac_nir_context
*ctx
,
3845 struct nir_function
*func
)
3848 ctx
->num_locals
= 0;
3849 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3850 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3851 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3852 variable
->data
.location_frac
= 0;
3853 ctx
->num_locals
+= attrib_count
;
3855 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3859 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3860 for (j
= 0; j
< 4; j
++) {
3861 ctx
->locals
[i
* 4 + j
] =
3862 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3868 setup_shared(struct ac_nir_context
*ctx
,
3869 struct nir_shader
*nir
)
3871 nir_foreach_variable(variable
, &nir
->shared
) {
3872 LLVMValueRef shared
=
3873 LLVMAddGlobalInAddressSpace(
3874 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3875 variable
->name
? variable
->name
: "",
3876 AC_LOCAL_ADDR_SPACE
);
3877 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3881 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3882 struct nir_shader
*nir
)
3884 struct ac_nir_context ctx
= {};
3885 struct nir_function
*func
;
3890 ctx
.stage
= nir
->info
.stage
;
3892 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3894 nir_foreach_variable(variable
, &nir
->outputs
)
3895 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3898 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3899 _mesa_key_pointer_equal
);
3900 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3901 _mesa_key_pointer_equal
);
3902 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3903 _mesa_key_pointer_equal
);
3905 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3907 nir_index_ssa_defs(func
->impl
);
3908 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
3910 setup_locals(&ctx
, func
);
3912 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3913 setup_shared(&ctx
, nir
);
3915 visit_cf_list(&ctx
, &func
->impl
->body
);
3916 phi_post_pass(&ctx
);
3918 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3919 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3924 ralloc_free(ctx
.defs
);
3925 ralloc_free(ctx
.phis
);
3926 ralloc_free(ctx
.vars
);
3930 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3932 /* While it would be nice not to have this flag, we are constrained
3933 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3936 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
3938 /* TODO: Indirect indexing of GS inputs is unimplemented.
3940 * TCS and TES load inputs directly from LDS or offchip memory, so
3941 * indirect indexing is trivial.
3943 nir_variable_mode indirect_mask
= 0;
3944 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
3945 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
3946 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
3947 !llvm_has_working_vgpr_indexing
)) {
3948 indirect_mask
|= nir_var_shader_in
;
3950 if (!llvm_has_working_vgpr_indexing
&&
3951 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
3952 indirect_mask
|= nir_var_shader_out
;
3954 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3955 * smart enough to handle indirects without causing excess spilling
3956 * causing the gpu to hang.
3958 * See the following thread for more details of the problem:
3959 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3961 indirect_mask
|= nir_var_local
;
3963 nir_lower_indirect_derefs(nir
, indirect_mask
);