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
,
1138 struct ac_image_args
*args
,
1139 const nir_tex_instr
*instr
)
1141 enum glsl_base_type stype
= glsl_get_sampler_result_type(var
->type
);
1142 LLVMValueRef half_texel
[2];
1143 LLVMValueRef compare_cube_wa
= NULL
;
1144 LLVMValueRef result
;
1148 struct ac_image_args txq_args
= { 0 };
1150 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1151 txq_args
.opcode
= ac_image_get_resinfo
;
1152 txq_args
.dmask
= 0xf;
1153 txq_args
.lod
= ctx
->i32_0
;
1154 txq_args
.resource
= args
->resource
;
1155 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1156 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1158 for (unsigned c
= 0; c
< 2; c
++) {
1159 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1160 LLVMConstInt(ctx
->i32
, c
, false), "");
1161 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1162 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1163 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1164 LLVMConstReal(ctx
->f32
, -0.5), "");
1168 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1170 for (unsigned c
= 0; c
< 2; c
++) {
1172 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1173 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1177 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1178 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1179 * workaround by sampling using a scaled type and converting.
1180 * This is taken from amdgpu-pro shaders.
1182 /* NOTE this produces some ugly code compared to amdgpu-pro,
1183 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1184 * and then reads them back. -pro generates two selects,
1185 * one s_cmp for the descriptor rewriting
1186 * one v_cmp for the coordinate and result changes.
1188 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1189 LLVMValueRef tmp
, tmp2
;
1191 /* workaround 8/8/8/8 uint/sint cube gather bug */
1192 /* first detect it then change to a scaled read and f2i */
1193 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1196 /* extract the DATA_FORMAT */
1197 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1198 LLVMConstInt(ctx
->i32
, 6, false), false);
1200 /* is the DATA_FORMAT == 8_8_8_8 */
1201 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1203 if (stype
== GLSL_TYPE_UINT
)
1204 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1205 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1206 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1208 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1209 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1210 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1212 /* replace the NUM FORMAT in the descriptor */
1213 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1214 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1216 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1218 /* don't modify the coordinates for this case */
1219 for (unsigned c
= 0; c
< 2; ++c
)
1220 args
->coords
[c
] = LLVMBuildSelect(
1221 ctx
->builder
, compare_cube_wa
,
1222 orig_coords
[c
], args
->coords
[c
], "");
1225 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1226 result
= ac_build_image_opcode(ctx
, args
);
1228 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1229 LLVMValueRef tmp
, tmp2
;
1231 /* if the cube workaround is in place, f2i the result. */
1232 for (unsigned c
= 0; c
< 4; c
++) {
1233 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1234 if (stype
== GLSL_TYPE_UINT
)
1235 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1237 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1238 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1239 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1240 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1241 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1242 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1248 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1250 nir_deref_instr
*texture_deref_instr
= NULL
;
1252 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1253 switch (instr
->src
[i
].src_type
) {
1254 case nir_tex_src_texture_deref
:
1255 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1261 return texture_deref_instr
;
1264 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1265 const nir_tex_instr
*instr
,
1266 struct ac_image_args
*args
)
1268 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1269 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1271 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1272 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1276 util_last_bit(mask
),
1279 return ac_build_buffer_load_format(&ctx
->ac
,
1283 util_last_bit(mask
),
1288 args
->opcode
= ac_image_sample
;
1290 switch (instr
->op
) {
1292 case nir_texop_txf_ms
:
1293 case nir_texop_samples_identical
:
1294 args
->opcode
= args
->level_zero
||
1295 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1296 ac_image_load
: ac_image_load_mip
;
1297 args
->level_zero
= false;
1300 case nir_texop_query_levels
:
1301 args
->opcode
= ac_image_get_resinfo
;
1303 args
->lod
= ctx
->ac
.i32_0
;
1304 args
->level_zero
= false;
1307 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1309 args
->level_zero
= true;
1313 args
->opcode
= ac_image_gather4
;
1314 args
->level_zero
= true;
1317 args
->opcode
= ac_image_get_lod
;
1323 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1324 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1325 nir_variable
*var
= texture_deref_instr
? nir_deref_instr_get_variable(texture_deref_instr
) : instr
->texture
->var
;
1326 enum glsl_base_type stype
= glsl_get_sampler_result_type(var
->type
);
1327 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1328 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1332 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1333 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1334 if ((args
->dim
== ac_image_2darray
||
1335 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1336 args
->coords
[1] = ctx
->ac
.i32_0
;
1340 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1341 return ac_build_image_opcode(&ctx
->ac
, args
);
1344 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1345 nir_intrinsic_instr
*instr
)
1347 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1348 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1350 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1351 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1355 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1356 nir_intrinsic_instr
*instr
)
1358 LLVMValueRef ptr
, addr
;
1360 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1361 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1362 get_src(ctx
, instr
->src
[0]), "");
1364 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1365 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1367 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1370 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1371 const nir_intrinsic_instr
*instr
)
1373 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1375 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1378 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1380 uint32_t new_mask
= 0;
1381 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1382 if (mask
& (1u << i
))
1383 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1387 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1388 unsigned start
, unsigned count
)
1390 LLVMTypeRef type
= LLVMTypeOf(src
);
1392 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
1398 unsigned src_elements
= LLVMGetVectorSize(type
);
1399 assert(start
< src_elements
);
1400 assert(start
+ count
<= src_elements
);
1402 if (start
== 0 && count
== src_elements
)
1406 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
1409 LLVMValueRef indices
[8];
1410 for (unsigned i
= 0; i
< count
; ++i
)
1411 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
1413 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
1414 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1417 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1418 nir_intrinsic_instr
*instr
)
1420 const char *store_name
;
1421 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1422 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1423 int elem_size_mult
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
1424 int components_32bit
= elem_size_mult
* instr
->num_components
;
1425 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1426 LLVMValueRef base_data
, base_offset
;
1427 LLVMValueRef params
[6];
1429 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
1430 get_src(ctx
, instr
->src
[1]), true);
1431 params
[2] = ctx
->ac
.i32_0
; /* vindex */
1432 params
[4] = ctx
->ac
.i1false
; /* glc */
1433 params
[5] = ctx
->ac
.i1false
; /* slc */
1435 if (components_32bit
> 1)
1436 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
1438 writemask
= widen_mask(writemask
, elem_size_mult
);
1440 base_data
= ac_to_float(&ctx
->ac
, src_data
);
1441 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1442 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
1444 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1448 LLVMValueRef offset
;
1450 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1452 /* Due to an LLVM limitation, split 3-element writes
1453 * into a 2-element and a 1-element write. */
1455 writemask
|= 1 << (start
+ 2);
1460 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1465 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1466 } else if (count
== 2) {
1467 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1471 store_name
= "llvm.amdgcn.buffer.store.f32";
1473 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1475 offset
= base_offset
;
1477 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
1481 ac_build_intrinsic(&ctx
->ac
, store_name
,
1482 ctx
->ac
.voidt
, params
, 6, 0);
1486 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1487 const nir_intrinsic_instr
*instr
)
1490 LLVMValueRef params
[6];
1493 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1494 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1496 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1497 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1498 get_src(ctx
, instr
->src
[0]),
1500 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1501 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1502 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1504 switch (instr
->intrinsic
) {
1505 case nir_intrinsic_ssbo_atomic_add
:
1506 name
= "llvm.amdgcn.buffer.atomic.add";
1508 case nir_intrinsic_ssbo_atomic_imin
:
1509 name
= "llvm.amdgcn.buffer.atomic.smin";
1511 case nir_intrinsic_ssbo_atomic_umin
:
1512 name
= "llvm.amdgcn.buffer.atomic.umin";
1514 case nir_intrinsic_ssbo_atomic_imax
:
1515 name
= "llvm.amdgcn.buffer.atomic.smax";
1517 case nir_intrinsic_ssbo_atomic_umax
:
1518 name
= "llvm.amdgcn.buffer.atomic.umax";
1520 case nir_intrinsic_ssbo_atomic_and
:
1521 name
= "llvm.amdgcn.buffer.atomic.and";
1523 case nir_intrinsic_ssbo_atomic_or
:
1524 name
= "llvm.amdgcn.buffer.atomic.or";
1526 case nir_intrinsic_ssbo_atomic_xor
:
1527 name
= "llvm.amdgcn.buffer.atomic.xor";
1529 case nir_intrinsic_ssbo_atomic_exchange
:
1530 name
= "llvm.amdgcn.buffer.atomic.swap";
1532 case nir_intrinsic_ssbo_atomic_comp_swap
:
1533 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1539 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1542 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1543 const nir_intrinsic_instr
*instr
)
1545 LLVMValueRef results
[2];
1546 int load_components
;
1547 int num_components
= instr
->num_components
;
1548 if (instr
->dest
.ssa
.bit_size
== 64)
1549 num_components
*= 2;
1551 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1552 load_components
= MIN2(num_components
- i
, 4);
1553 const char *load_name
;
1554 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1555 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
1556 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1558 if (load_components
== 3)
1559 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
1560 else if (load_components
> 1)
1561 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
1563 if (load_components
>= 3)
1564 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1565 else if (load_components
== 2)
1566 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1567 else if (load_components
== 1)
1568 load_name
= "llvm.amdgcn.buffer.load.f32";
1570 unreachable("unhandled number of components");
1572 LLVMValueRef params
[] = {
1573 ctx
->abi
->load_ssbo(ctx
->abi
,
1574 get_src(ctx
, instr
->src
[0]),
1582 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1586 LLVMValueRef ret
= results
[0];
1587 if (num_components
> 4 || num_components
== 3) {
1588 LLVMValueRef masks
[] = {
1589 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1590 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1591 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
1592 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
1595 if (num_components
== 6) {
1596 /* we end up with a v4f32 and v2f32 but shuffle fails on that */
1597 results
[1] = ac_build_expand_to_vec4(&ctx
->ac
, results
[1], 4);
1600 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1601 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1602 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
1605 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1606 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1609 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1610 const nir_intrinsic_instr
*instr
)
1613 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1614 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1615 int num_components
= instr
->num_components
;
1617 if (ctx
->abi
->load_ubo
)
1618 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1620 if (instr
->dest
.ssa
.bit_size
== 64)
1621 num_components
*= 2;
1623 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1624 NULL
, 0, false, false, true, true);
1625 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1626 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1627 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1631 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
1632 bool vs_in
, unsigned *vertex_index_out
,
1633 LLVMValueRef
*vertex_index_ref
,
1634 unsigned *const_out
, LLVMValueRef
*indir_out
)
1636 unsigned const_offset
= 0;
1637 nir_deref
*tail
= &deref
->deref
;
1638 LLVMValueRef offset
= NULL
;
1640 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1642 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1643 if (vertex_index_out
)
1644 *vertex_index_out
= deref_array
->base_offset
;
1646 if (vertex_index_ref
) {
1647 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
1648 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1649 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
1651 *vertex_index_ref
= vtx
;
1655 if (deref
->var
->data
.compact
) {
1656 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1657 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
1658 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1659 /* We always lower indirect dereferences for "compact" array vars. */
1660 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
1662 const_offset
= deref_array
->base_offset
;
1666 while (tail
->child
!= NULL
) {
1667 const struct glsl_type
*parent_type
= tail
->type
;
1670 if (tail
->deref_type
== nir_deref_type_array
) {
1671 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1672 LLVMValueRef index
, stride
, local_offset
;
1673 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
1675 const_offset
+= size
* deref_array
->base_offset
;
1676 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
1679 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
1680 index
= get_src(ctx
, deref_array
->indirect
);
1681 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
1682 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
1685 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
1687 offset
= local_offset
;
1688 } else if (tail
->deref_type
== nir_deref_type_struct
) {
1689 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
1691 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
1692 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1693 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1696 unreachable("unsupported deref type");
1700 if (const_offset
&& offset
)
1701 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1702 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1705 *const_out
= const_offset
;
1706 *indir_out
= offset
;
1710 build_gep_for_deref(struct ac_nir_context
*ctx
,
1711 nir_deref_var
*deref
)
1713 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1714 assert(entry
->data
);
1715 LLVMValueRef val
= entry
->data
;
1716 nir_deref
*tail
= deref
->deref
.child
;
1717 while (tail
!= NULL
) {
1718 LLVMValueRef offset
;
1719 switch (tail
->deref_type
) {
1720 case nir_deref_type_array
: {
1721 nir_deref_array
*array
= nir_deref_as_array(tail
);
1722 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
1723 if (array
->deref_array_type
==
1724 nir_deref_array_type_indirect
) {
1725 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1732 case nir_deref_type_struct
: {
1733 nir_deref_struct
*deref_struct
=
1734 nir_deref_as_struct(tail
);
1735 offset
= LLVMConstInt(ctx
->ac
.i32
,
1736 deref_struct
->index
, 0);
1740 unreachable("bad deref type");
1742 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
1748 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1749 nir_intrinsic_instr
*instr
,
1752 LLVMValueRef result
;
1753 LLVMValueRef vertex_index
= NULL
;
1754 LLVMValueRef indir_index
= NULL
;
1755 unsigned const_index
= 0;
1756 unsigned location
= instr
->variables
[0]->var
->data
.location
;
1757 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
1758 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1759 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
1761 get_deref_offset(ctx
, instr
->variables
[0],
1762 false, NULL
, is_patch
? NULL
: &vertex_index
,
1763 &const_index
, &indir_index
);
1765 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1767 LLVMTypeRef src_component_type
;
1768 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1769 src_component_type
= LLVMGetElementType(dest_type
);
1771 src_component_type
= dest_type
;
1773 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1774 vertex_index
, indir_index
,
1775 const_index
, location
, driver_location
,
1776 instr
->variables
[0]->var
->data
.location_frac
,
1777 instr
->num_components
,
1778 is_patch
, is_compact
, load_inputs
);
1779 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1782 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1783 nir_intrinsic_instr
*instr
)
1785 LLVMValueRef values
[8];
1786 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1787 int ve
= instr
->dest
.ssa
.num_components
;
1788 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1789 LLVMValueRef indir_index
;
1791 unsigned const_index
;
1792 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
1793 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1794 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
1795 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
1796 &const_index
, &indir_index
);
1798 if (instr
->dest
.ssa
.bit_size
== 64)
1801 switch (instr
->variables
[0]->var
->data
.mode
) {
1802 case nir_var_shader_in
:
1803 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1804 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1805 return load_tess_varyings(ctx
, instr
, true);
1808 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1809 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1810 LLVMValueRef indir_index
;
1811 unsigned const_index
, vertex_index
;
1812 get_deref_offset(ctx
, instr
->variables
[0],
1813 false, &vertex_index
, NULL
,
1814 &const_index
, &indir_index
);
1816 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
1817 instr
->variables
[0]->var
->data
.driver_location
,
1818 instr
->variables
[0]->var
->data
.location_frac
,
1819 instr
->num_components
, vertex_index
, const_index
, type
);
1822 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1824 unsigned count
= glsl_count_attribute_slots(
1825 instr
->variables
[0]->var
->type
,
1826 ctx
->stage
== MESA_SHADER_VERTEX
);
1828 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1829 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1830 stride
, false, true);
1832 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1836 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1840 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1842 unsigned count
= glsl_count_attribute_slots(
1843 instr
->variables
[0]->var
->type
, false);
1845 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1846 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1847 stride
, true, true);
1849 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1853 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1857 case nir_var_shared
: {
1858 LLVMValueRef address
= build_gep_for_deref(ctx
,
1859 instr
->variables
[0]);
1860 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1861 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1862 get_def_type(ctx
, &instr
->dest
.ssa
),
1865 case nir_var_shader_out
:
1866 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1867 return load_tess_varyings(ctx
, instr
, false);
1870 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1872 unsigned count
= glsl_count_attribute_slots(
1873 instr
->variables
[0]->var
->type
, false);
1875 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1876 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1877 stride
, true, true);
1879 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1883 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1884 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1890 unreachable("unhandle variable mode");
1892 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1893 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1897 visit_store_var(struct ac_nir_context
*ctx
,
1898 nir_intrinsic_instr
*instr
)
1900 LLVMValueRef temp_ptr
, value
;
1901 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1902 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1903 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
1904 int writemask
= instr
->const_index
[0];
1905 LLVMValueRef indir_index
;
1906 unsigned const_index
;
1907 get_deref_offset(ctx
, instr
->variables
[0], false,
1908 NULL
, NULL
, &const_index
, &indir_index
);
1910 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1912 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1913 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1916 writemask
= widen_mask(writemask
, 2);
1919 writemask
= writemask
<< comp
;
1921 switch (instr
->variables
[0]->var
->data
.mode
) {
1922 case nir_var_shader_out
:
1924 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1925 LLVMValueRef vertex_index
= NULL
;
1926 LLVMValueRef indir_index
= NULL
;
1927 unsigned const_index
= 0;
1928 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1930 get_deref_offset(ctx
, instr
->variables
[0],
1931 false, NULL
, is_patch
? NULL
: &vertex_index
,
1932 &const_index
, &indir_index
);
1934 ctx
->abi
->store_tcs_outputs(ctx
->abi
, instr
->variables
[0]->var
,
1935 vertex_index
, indir_index
,
1936 const_index
, src
, writemask
);
1940 for (unsigned chan
= 0; chan
< 8; chan
++) {
1942 if (!(writemask
& (1 << chan
)))
1945 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
1947 if (instr
->variables
[0]->var
->data
.compact
)
1950 unsigned count
= glsl_count_attribute_slots(
1951 instr
->variables
[0]->var
->type
, false);
1953 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1954 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1955 stride
, true, true);
1957 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1958 value
, indir_index
, "");
1959 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
1960 count
, stride
, tmp_vec
);
1963 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
1965 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1970 for (unsigned chan
= 0; chan
< 8; chan
++) {
1971 if (!(writemask
& (1 << chan
)))
1974 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
1976 unsigned count
= glsl_count_attribute_slots(
1977 instr
->variables
[0]->var
->type
, false);
1979 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1980 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1983 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1984 value
, indir_index
, "");
1985 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
1988 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
1990 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1994 case nir_var_shared
: {
1995 int writemask
= instr
->const_index
[0];
1996 LLVMValueRef address
= build_gep_for_deref(ctx
,
1997 instr
->variables
[0]);
1998 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
1999 unsigned components
=
2000 glsl_get_vector_elements(
2001 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
2002 if (writemask
== (1 << components
) - 1) {
2003 val
= LLVMBuildBitCast(
2004 ctx
->ac
.builder
, val
,
2005 LLVMGetElementType(LLVMTypeOf(address
)), "");
2006 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2008 for (unsigned chan
= 0; chan
< 4; chan
++) {
2009 if (!(writemask
& (1 << chan
)))
2012 LLVMBuildStructGEP(ctx
->ac
.builder
,
2014 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2016 src
= LLVMBuildBitCast(
2017 ctx
->ac
.builder
, src
,
2018 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2019 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2029 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2032 case GLSL_SAMPLER_DIM_BUF
:
2034 case GLSL_SAMPLER_DIM_1D
:
2035 return array
? 2 : 1;
2036 case GLSL_SAMPLER_DIM_2D
:
2037 return array
? 3 : 2;
2038 case GLSL_SAMPLER_DIM_MS
:
2039 return array
? 4 : 3;
2040 case GLSL_SAMPLER_DIM_3D
:
2041 case GLSL_SAMPLER_DIM_CUBE
:
2043 case GLSL_SAMPLER_DIM_RECT
:
2044 case GLSL_SAMPLER_DIM_SUBPASS
:
2046 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2055 /* Adjust the sample index according to FMASK.
2057 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2058 * which is the identity mapping. Each nibble says which physical sample
2059 * should be fetched to get that sample.
2061 * For example, 0x11111100 means there are only 2 samples stored and
2062 * the second sample covers 3/4 of the pixel. When reading samples 0
2063 * and 1, return physical sample 0 (determined by the first two 0s
2064 * in FMASK), otherwise return physical sample 1.
2066 * The sample index should be adjusted as follows:
2067 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2069 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2070 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2071 LLVMValueRef coord_z
,
2072 LLVMValueRef sample_index
,
2073 LLVMValueRef fmask_desc_ptr
)
2075 struct ac_image_args args
= {0};
2078 args
.coords
[0] = coord_x
;
2079 args
.coords
[1] = coord_y
;
2081 args
.coords
[2] = coord_z
;
2083 args
.opcode
= ac_image_load
;
2084 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2085 args
.resource
= fmask_desc_ptr
;
2087 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2089 res
= ac_build_image_opcode(ctx
, &args
);
2091 res
= ac_to_integer(ctx
, res
);
2092 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2093 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2095 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2099 LLVMValueRef sample_index4
=
2100 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2101 LLVMValueRef shifted_fmask
=
2102 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2103 LLVMValueRef final_sample
=
2104 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2106 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2107 * resource descriptor is 0 (invalid),
2109 LLVMValueRef fmask_desc
=
2110 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2113 LLVMValueRef fmask_word1
=
2114 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2117 LLVMValueRef word1_is_nonzero
=
2118 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2119 fmask_word1
, ctx
->i32_0
, "");
2121 /* Replace the MSAA sample index. */
2123 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2124 final_sample
, sample_index
, "");
2125 return sample_index
;
2128 static void get_image_coords(struct ac_nir_context
*ctx
,
2129 const nir_intrinsic_instr
*instr
,
2130 struct ac_image_args
*args
)
2132 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
2134 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2135 LLVMValueRef masks
[] = {
2136 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2137 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2139 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
2142 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2143 bool is_array
= glsl_sampler_type_is_array(type
);
2144 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2145 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2146 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2147 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2148 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2149 count
= image_type_to_components_count(dim
, is_array
);
2152 LLVMValueRef fmask_load_address
[3];
2155 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2156 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2158 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2160 fmask_load_address
[2] = NULL
;
2162 for (chan
= 0; chan
< 2; ++chan
)
2163 fmask_load_address
[chan
] =
2164 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2165 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2166 ctx
->ac
.i32
, ""), "");
2167 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2169 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2170 fmask_load_address
[0],
2171 fmask_load_address
[1],
2172 fmask_load_address
[2],
2174 get_sampler_desc(ctx
, instr
->variables
[0], NULL
, AC_DESC_FMASK
, NULL
, true, false));
2176 if (count
== 1 && !gfx9_1d
) {
2177 if (instr
->src
[0].ssa
->num_components
)
2178 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2180 args
->coords
[0] = src0
;
2185 for (chan
= 0; chan
< count
; ++chan
) {
2186 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2189 for (chan
= 0; chan
< 2; ++chan
) {
2190 args
->coords
[chan
] = LLVMBuildAdd(
2191 ctx
->ac
.builder
, args
->coords
[chan
],
2193 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2194 ctx
->ac
.i32
, ""), "");
2196 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2197 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2203 args
->coords
[2] = args
->coords
[1];
2204 args
->coords
[1] = ctx
->ac
.i32_0
;
2206 args
->coords
[1] = ctx
->ac
.i32_0
;
2211 args
->coords
[count
] = sample_index
;
2217 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2218 const nir_intrinsic_instr
*instr
, bool write
)
2220 LLVMValueRef rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
, AC_DESC_BUFFER
, NULL
, true, write
);
2221 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2222 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2223 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2224 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2226 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2227 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2228 elem_count
, stride
, "");
2230 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2231 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2236 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2237 const nir_intrinsic_instr
*instr
)
2240 const nir_variable
*var
= instr
->variables
[0]->var
;
2241 const struct glsl_type
*type
= var
->type
;
2243 if(instr
->variables
[0]->deref
.child
)
2244 type
= instr
->variables
[0]->deref
.child
->type
;
2246 type
= glsl_without_array(type
);
2248 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2249 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2250 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2251 unsigned num_channels
= util_last_bit(mask
);
2252 LLVMValueRef rsrc
, vindex
;
2254 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2255 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2258 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2259 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2260 ctx
->ac
.i32_0
, num_channels
,
2262 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2264 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2265 res
= ac_to_integer(&ctx
->ac
, res
);
2267 struct ac_image_args args
= {};
2268 args
.opcode
= ac_image_load
;
2269 get_image_coords(ctx
, instr
, &args
);
2270 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2271 AC_DESC_IMAGE
, NULL
, true, false);
2272 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2273 glsl_sampler_type_is_array(type
));
2275 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2276 if (var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2277 args
.cache_policy
|= ac_glc
;
2279 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2281 return ac_to_integer(&ctx
->ac
, res
);
2284 static void visit_image_store(struct ac_nir_context
*ctx
,
2285 nir_intrinsic_instr
*instr
)
2287 LLVMValueRef params
[8];
2288 const nir_variable
*var
= instr
->variables
[0]->var
;
2289 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2290 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2291 LLVMValueRef glc
= ctx
->ac
.i1false
;
2292 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2294 glc
= ctx
->ac
.i1true
;
2296 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2297 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2299 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
2301 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2302 ctx
->ac
.i32_0
, ""); /* vindex */
2303 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2304 params
[4] = glc
; /* glc */
2305 params
[5] = ctx
->ac
.i1false
; /* slc */
2306 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2309 struct ac_image_args args
= {};
2310 args
.opcode
= ac_image_store
;
2311 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
2312 get_image_coords(ctx
, instr
, &args
);
2313 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2314 AC_DESC_IMAGE
, NULL
, true, false);
2315 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2316 glsl_sampler_type_is_array(type
));
2318 if (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2319 args
.cache_policy
|= ac_glc
;
2321 ac_build_image_opcode(&ctx
->ac
, &args
);
2326 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2327 const nir_intrinsic_instr
*instr
)
2329 LLVMValueRef params
[7];
2330 int param_count
= 0;
2331 const nir_variable
*var
= instr
->variables
[0]->var
;
2333 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_var_atomic_comp_swap
;
2334 const char *atomic_name
;
2335 char intrinsic_name
[41];
2336 enum ac_atomic_op atomic_subop
;
2337 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2338 MAYBE_UNUSED
int length
;
2340 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2342 switch (instr
->intrinsic
) {
2343 case nir_intrinsic_image_var_atomic_add
:
2344 atomic_name
= "add";
2345 atomic_subop
= ac_atomic_add
;
2347 case nir_intrinsic_image_var_atomic_min
:
2348 atomic_name
= is_unsigned
? "umin" : "smin";
2349 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2351 case nir_intrinsic_image_var_atomic_max
:
2352 atomic_name
= is_unsigned
? "umax" : "smax";
2353 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2355 case nir_intrinsic_image_var_atomic_and
:
2356 atomic_name
= "and";
2357 atomic_subop
= ac_atomic_and
;
2359 case nir_intrinsic_image_var_atomic_or
:
2361 atomic_subop
= ac_atomic_or
;
2363 case nir_intrinsic_image_var_atomic_xor
:
2364 atomic_name
= "xor";
2365 atomic_subop
= ac_atomic_xor
;
2367 case nir_intrinsic_image_var_atomic_exchange
:
2368 atomic_name
= "swap";
2369 atomic_subop
= ac_atomic_swap
;
2371 case nir_intrinsic_image_var_atomic_comp_swap
:
2372 atomic_name
= "cmpswap";
2373 atomic_subop
= 0; /* not used */
2380 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2381 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2383 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2384 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2385 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2386 ctx
->ac
.i32_0
, ""); /* vindex */
2387 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2388 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2390 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2391 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2393 assert(length
< sizeof(intrinsic_name
));
2394 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2395 params
, param_count
, 0);
2397 struct ac_image_args args
= {};
2398 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2399 args
.atomic
= atomic_subop
;
2400 args
.data
[0] = params
[0];
2402 args
.data
[1] = params
[1];
2403 get_image_coords(ctx
, instr
, &args
);
2404 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2405 AC_DESC_IMAGE
, NULL
, true, false);
2406 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2407 glsl_sampler_type_is_array(type
));
2409 return ac_build_image_opcode(&ctx
->ac
, &args
);
2413 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2414 const nir_intrinsic_instr
*instr
)
2416 const nir_variable
*var
= instr
->variables
[0]->var
;
2417 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2419 struct ac_image_args args
= { 0 };
2420 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2421 glsl_sampler_type_is_array(type
));
2423 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2424 AC_DESC_IMAGE
, NULL
, true, false);
2425 args
.opcode
= ac_image_get_resinfo
;
2426 args
.lod
= ctx
->ac
.i32_0
;
2427 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2429 return ac_build_image_opcode(&ctx
->ac
, &args
);
2432 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2433 const nir_intrinsic_instr
*instr
)
2436 const nir_variable
*var
= instr
->variables
[0]->var
;
2437 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2439 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2440 return get_buffer_size(ctx
,
2441 get_sampler_desc(ctx
, instr
->variables
[0], NULL
,
2442 AC_DESC_BUFFER
, NULL
, true, false), true);
2444 struct ac_image_args args
= { 0 };
2446 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2447 glsl_sampler_type_is_array(type
));
2449 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], NULL
, AC_DESC_IMAGE
, NULL
, true, false);
2450 args
.opcode
= ac_image_get_resinfo
;
2451 args
.lod
= ctx
->ac
.i32_0
;
2452 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2454 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2456 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2458 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2459 glsl_sampler_type_is_array(type
)) {
2460 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2461 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2462 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2463 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2465 if (ctx
->ac
.chip_class
>= GFX9
&&
2466 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2467 glsl_sampler_type_is_array(type
)) {
2468 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2469 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2476 #define NOOP_WAITCNT 0xf7f
2477 #define LGKM_CNT 0x07f
2478 #define VM_CNT 0xf70
2480 static void emit_membar(struct ac_llvm_context
*ac
,
2481 const nir_intrinsic_instr
*instr
)
2483 unsigned waitcnt
= NOOP_WAITCNT
;
2485 switch (instr
->intrinsic
) {
2486 case nir_intrinsic_memory_barrier
:
2487 case nir_intrinsic_group_memory_barrier
:
2488 waitcnt
&= VM_CNT
& LGKM_CNT
;
2490 case nir_intrinsic_memory_barrier_atomic_counter
:
2491 case nir_intrinsic_memory_barrier_buffer
:
2492 case nir_intrinsic_memory_barrier_image
:
2495 case nir_intrinsic_memory_barrier_shared
:
2496 waitcnt
&= LGKM_CNT
;
2501 if (waitcnt
!= NOOP_WAITCNT
)
2502 ac_build_waitcnt(ac
, waitcnt
);
2505 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2507 /* SI only (thanks to a hw bug workaround):
2508 * The real barrier instruction isn’t needed, because an entire patch
2509 * always fits into a single wave.
2511 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2512 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2515 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2516 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2519 static void emit_discard(struct ac_nir_context
*ctx
,
2520 const nir_intrinsic_instr
*instr
)
2524 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2525 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2526 get_src(ctx
, instr
->src
[0]),
2529 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2530 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2533 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2537 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2539 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2540 "llvm.amdgcn.ps.live",
2541 ctx
->ac
.i1
, NULL
, 0,
2542 AC_FUNC_ATTR_READNONE
);
2543 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2544 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2548 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2550 LLVMValueRef result
;
2551 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2552 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2553 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2555 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2559 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2561 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2562 LLVMValueRef result
;
2563 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2564 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2565 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2567 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2572 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2574 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2575 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2576 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2578 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2583 visit_first_invocation(struct ac_nir_context
*ctx
)
2585 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2587 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2588 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2589 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2591 ctx
->ac
.i64
, args
, 2,
2592 AC_FUNC_ATTR_NOUNWIND
|
2593 AC_FUNC_ATTR_READNONE
);
2595 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2599 visit_load_shared(struct ac_nir_context
*ctx
,
2600 const nir_intrinsic_instr
*instr
)
2602 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2604 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2606 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2607 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2608 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2609 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2612 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2613 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2617 visit_store_shared(struct ac_nir_context
*ctx
,
2618 const nir_intrinsic_instr
*instr
)
2620 LLVMValueRef derived_ptr
, data
,index
;
2621 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2623 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2624 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2626 int writemask
= nir_intrinsic_write_mask(instr
);
2627 for (int chan
= 0; chan
< 4; chan
++) {
2628 if (!(writemask
& (1 << chan
))) {
2631 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2632 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2633 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2634 LLVMBuildStore(builder
, data
, derived_ptr
);
2638 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2639 const nir_intrinsic_instr
*instr
,
2640 LLVMValueRef ptr
, int src_idx
)
2642 LLVMValueRef result
;
2643 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2645 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
||
2646 instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
) {
2647 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2648 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2650 LLVMAtomicOrderingSequentiallyConsistent
,
2651 LLVMAtomicOrderingSequentiallyConsistent
,
2653 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2655 LLVMAtomicRMWBinOp op
;
2656 switch (instr
->intrinsic
) {
2657 case nir_intrinsic_var_atomic_add
:
2658 case nir_intrinsic_shared_atomic_add
:
2659 op
= LLVMAtomicRMWBinOpAdd
;
2661 case nir_intrinsic_var_atomic_umin
:
2662 case nir_intrinsic_shared_atomic_umin
:
2663 op
= LLVMAtomicRMWBinOpUMin
;
2665 case nir_intrinsic_var_atomic_umax
:
2666 case nir_intrinsic_shared_atomic_umax
:
2667 op
= LLVMAtomicRMWBinOpUMax
;
2669 case nir_intrinsic_var_atomic_imin
:
2670 case nir_intrinsic_shared_atomic_imin
:
2671 op
= LLVMAtomicRMWBinOpMin
;
2673 case nir_intrinsic_var_atomic_imax
:
2674 case nir_intrinsic_shared_atomic_imax
:
2675 op
= LLVMAtomicRMWBinOpMax
;
2677 case nir_intrinsic_var_atomic_and
:
2678 case nir_intrinsic_shared_atomic_and
:
2679 op
= LLVMAtomicRMWBinOpAnd
;
2681 case nir_intrinsic_var_atomic_or
:
2682 case nir_intrinsic_shared_atomic_or
:
2683 op
= LLVMAtomicRMWBinOpOr
;
2685 case nir_intrinsic_var_atomic_xor
:
2686 case nir_intrinsic_shared_atomic_xor
:
2687 op
= LLVMAtomicRMWBinOpXor
;
2689 case nir_intrinsic_var_atomic_exchange
:
2690 case nir_intrinsic_shared_atomic_exchange
:
2691 op
= LLVMAtomicRMWBinOpXchg
;
2697 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2698 LLVMAtomicOrderingSequentiallyConsistent
,
2704 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2706 LLVMValueRef values
[2];
2707 LLVMValueRef pos
[2];
2709 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2710 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2712 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2713 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2714 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2717 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2718 const nir_intrinsic_instr
*instr
)
2720 LLVMValueRef result
[4];
2721 LLVMValueRef interp_param
, attr_number
;
2724 LLVMValueRef src_c0
= NULL
;
2725 LLVMValueRef src_c1
= NULL
;
2726 LLVMValueRef src0
= NULL
;
2727 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2728 switch (instr
->intrinsic
) {
2729 case nir_intrinsic_interp_var_at_centroid
:
2730 location
= INTERP_CENTROID
;
2732 case nir_intrinsic_interp_var_at_sample
:
2733 case nir_intrinsic_interp_var_at_offset
:
2734 location
= INTERP_CENTER
;
2735 src0
= get_src(ctx
, instr
->src
[0]);
2741 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2742 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2743 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2744 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2745 LLVMValueRef sample_position
;
2746 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2748 /* fetch sample ID */
2749 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2751 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2752 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2753 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2754 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2756 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2757 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2759 if (location
== INTERP_CENTER
) {
2760 LLVMValueRef ij_out
[2];
2761 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2764 * take the I then J parameters, and the DDX/Y for it, and
2765 * calculate the IJ inputs for the interpolator.
2766 * temp1 = ddx * offset/sample.x + I;
2767 * interp_param.I = ddy * offset/sample.y + temp1;
2768 * temp1 = ddx * offset/sample.x + J;
2769 * interp_param.J = ddy * offset/sample.y + temp1;
2771 for (unsigned i
= 0; i
< 2; i
++) {
2772 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2773 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2774 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2775 ddxy_out
, ix_ll
, "");
2776 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2777 ddxy_out
, iy_ll
, "");
2778 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2779 interp_param
, ix_ll
, "");
2780 LLVMValueRef temp1
, temp2
;
2782 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2785 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2786 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2788 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2789 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2791 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2792 temp2
, ctx
->ac
.i32
, "");
2794 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2798 for (chan
= 0; chan
< 4; chan
++) {
2799 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2802 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2803 interp_param
, ctx
->ac
.v2f32
, "");
2804 LLVMValueRef i
= LLVMBuildExtractElement(
2805 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2806 LLVMValueRef j
= LLVMBuildExtractElement(
2807 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2809 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2810 llvm_chan
, attr_number
,
2811 ctx
->abi
->prim_mask
, i
, j
);
2813 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2814 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2815 llvm_chan
, attr_number
,
2816 ctx
->abi
->prim_mask
);
2819 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2820 instr
->variables
[0]->var
->data
.location_frac
);
2823 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2824 nir_intrinsic_instr
*instr
)
2826 LLVMValueRef result
= NULL
;
2828 switch (instr
->intrinsic
) {
2829 case nir_intrinsic_ballot
:
2830 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2832 case nir_intrinsic_read_invocation
:
2833 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2834 get_src(ctx
, instr
->src
[1]));
2836 case nir_intrinsic_read_first_invocation
:
2837 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2839 case nir_intrinsic_load_subgroup_invocation
:
2840 result
= ac_get_thread_id(&ctx
->ac
);
2842 case nir_intrinsic_load_work_group_id
: {
2843 LLVMValueRef values
[3];
2845 for (int i
= 0; i
< 3; i
++) {
2846 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2847 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2850 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2853 case nir_intrinsic_load_base_vertex
:
2854 case nir_intrinsic_load_first_vertex
:
2855 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2857 case nir_intrinsic_load_local_group_size
:
2858 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2860 case nir_intrinsic_load_vertex_id
:
2861 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2862 ctx
->abi
->base_vertex
, "");
2864 case nir_intrinsic_load_vertex_id_zero_base
: {
2865 result
= ctx
->abi
->vertex_id
;
2868 case nir_intrinsic_load_local_invocation_id
: {
2869 result
= ctx
->abi
->local_invocation_ids
;
2872 case nir_intrinsic_load_base_instance
:
2873 result
= ctx
->abi
->start_instance
;
2875 case nir_intrinsic_load_draw_id
:
2876 result
= ctx
->abi
->draw_id
;
2878 case nir_intrinsic_load_view_index
:
2879 result
= ctx
->abi
->view_index
;
2881 case nir_intrinsic_load_invocation_id
:
2882 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2883 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2885 result
= ctx
->abi
->gs_invocation_id
;
2887 case nir_intrinsic_load_primitive_id
:
2888 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2889 result
= ctx
->abi
->gs_prim_id
;
2890 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2891 result
= ctx
->abi
->tcs_patch_id
;
2892 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2893 result
= ctx
->abi
->tes_patch_id
;
2895 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2897 case nir_intrinsic_load_sample_id
:
2898 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2900 case nir_intrinsic_load_sample_pos
:
2901 result
= load_sample_pos(ctx
);
2903 case nir_intrinsic_load_sample_mask_in
:
2904 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2906 case nir_intrinsic_load_frag_coord
: {
2907 LLVMValueRef values
[4] = {
2908 ctx
->abi
->frag_pos
[0],
2909 ctx
->abi
->frag_pos
[1],
2910 ctx
->abi
->frag_pos
[2],
2911 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2913 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2916 case nir_intrinsic_load_front_face
:
2917 result
= ctx
->abi
->front_face
;
2919 case nir_intrinsic_load_helper_invocation
:
2920 result
= visit_load_helper_invocation(ctx
);
2922 case nir_intrinsic_load_instance_id
:
2923 result
= ctx
->abi
->instance_id
;
2925 case nir_intrinsic_load_num_work_groups
:
2926 result
= ctx
->abi
->num_work_groups
;
2928 case nir_intrinsic_load_local_invocation_index
:
2929 result
= visit_load_local_invocation_index(ctx
);
2931 case nir_intrinsic_load_subgroup_id
:
2932 result
= visit_load_subgroup_id(ctx
);
2934 case nir_intrinsic_load_num_subgroups
:
2935 result
= visit_load_num_subgroups(ctx
);
2937 case nir_intrinsic_first_invocation
:
2938 result
= visit_first_invocation(ctx
);
2940 case nir_intrinsic_load_push_constant
:
2941 result
= visit_load_push_constant(ctx
, instr
);
2943 case nir_intrinsic_vulkan_resource_index
: {
2944 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2945 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2946 unsigned binding
= nir_intrinsic_binding(instr
);
2948 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
2952 case nir_intrinsic_vulkan_resource_reindex
:
2953 result
= visit_vulkan_resource_reindex(ctx
, instr
);
2955 case nir_intrinsic_store_ssbo
:
2956 visit_store_ssbo(ctx
, instr
);
2958 case nir_intrinsic_load_ssbo
:
2959 result
= visit_load_buffer(ctx
, instr
);
2961 case nir_intrinsic_ssbo_atomic_add
:
2962 case nir_intrinsic_ssbo_atomic_imin
:
2963 case nir_intrinsic_ssbo_atomic_umin
:
2964 case nir_intrinsic_ssbo_atomic_imax
:
2965 case nir_intrinsic_ssbo_atomic_umax
:
2966 case nir_intrinsic_ssbo_atomic_and
:
2967 case nir_intrinsic_ssbo_atomic_or
:
2968 case nir_intrinsic_ssbo_atomic_xor
:
2969 case nir_intrinsic_ssbo_atomic_exchange
:
2970 case nir_intrinsic_ssbo_atomic_comp_swap
:
2971 result
= visit_atomic_ssbo(ctx
, instr
);
2973 case nir_intrinsic_load_ubo
:
2974 result
= visit_load_ubo_buffer(ctx
, instr
);
2976 case nir_intrinsic_get_buffer_size
:
2977 result
= visit_get_buffer_size(ctx
, instr
);
2979 case nir_intrinsic_load_var
:
2980 result
= visit_load_var(ctx
, instr
);
2982 case nir_intrinsic_store_var
:
2983 visit_store_var(ctx
, instr
);
2985 case nir_intrinsic_load_shared
:
2986 result
= visit_load_shared(ctx
, instr
);
2988 case nir_intrinsic_store_shared
:
2989 visit_store_shared(ctx
, instr
);
2991 case nir_intrinsic_image_var_samples
:
2992 result
= visit_image_samples(ctx
, instr
);
2994 case nir_intrinsic_image_var_load
:
2995 result
= visit_image_load(ctx
, instr
);
2997 case nir_intrinsic_image_var_store
:
2998 visit_image_store(ctx
, instr
);
3000 case nir_intrinsic_image_var_atomic_add
:
3001 case nir_intrinsic_image_var_atomic_min
:
3002 case nir_intrinsic_image_var_atomic_max
:
3003 case nir_intrinsic_image_var_atomic_and
:
3004 case nir_intrinsic_image_var_atomic_or
:
3005 case nir_intrinsic_image_var_atomic_xor
:
3006 case nir_intrinsic_image_var_atomic_exchange
:
3007 case nir_intrinsic_image_var_atomic_comp_swap
:
3008 result
= visit_image_atomic(ctx
, instr
);
3010 case nir_intrinsic_image_var_size
:
3011 result
= visit_image_size(ctx
, instr
);
3013 case nir_intrinsic_shader_clock
:
3014 result
= ac_build_shader_clock(&ctx
->ac
);
3016 case nir_intrinsic_discard
:
3017 case nir_intrinsic_discard_if
:
3018 emit_discard(ctx
, instr
);
3020 case nir_intrinsic_memory_barrier
:
3021 case nir_intrinsic_group_memory_barrier
:
3022 case nir_intrinsic_memory_barrier_atomic_counter
:
3023 case nir_intrinsic_memory_barrier_buffer
:
3024 case nir_intrinsic_memory_barrier_image
:
3025 case nir_intrinsic_memory_barrier_shared
:
3026 emit_membar(&ctx
->ac
, instr
);
3028 case nir_intrinsic_barrier
:
3029 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3031 case nir_intrinsic_shared_atomic_add
:
3032 case nir_intrinsic_shared_atomic_imin
:
3033 case nir_intrinsic_shared_atomic_umin
:
3034 case nir_intrinsic_shared_atomic_imax
:
3035 case nir_intrinsic_shared_atomic_umax
:
3036 case nir_intrinsic_shared_atomic_and
:
3037 case nir_intrinsic_shared_atomic_or
:
3038 case nir_intrinsic_shared_atomic_xor
:
3039 case nir_intrinsic_shared_atomic_exchange
:
3040 case nir_intrinsic_shared_atomic_comp_swap
: {
3041 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3042 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3045 case nir_intrinsic_var_atomic_add
:
3046 case nir_intrinsic_var_atomic_imin
:
3047 case nir_intrinsic_var_atomic_umin
:
3048 case nir_intrinsic_var_atomic_imax
:
3049 case nir_intrinsic_var_atomic_umax
:
3050 case nir_intrinsic_var_atomic_and
:
3051 case nir_intrinsic_var_atomic_or
:
3052 case nir_intrinsic_var_atomic_xor
:
3053 case nir_intrinsic_var_atomic_exchange
:
3054 case nir_intrinsic_var_atomic_comp_swap
: {
3055 LLVMValueRef ptr
= build_gep_for_deref(ctx
, instr
->variables
[0]);
3056 result
= visit_var_atomic(ctx
, instr
, ptr
, 0);
3059 case nir_intrinsic_interp_var_at_centroid
:
3060 case nir_intrinsic_interp_var_at_sample
:
3061 case nir_intrinsic_interp_var_at_offset
:
3062 result
= visit_interp(ctx
, instr
);
3064 case nir_intrinsic_emit_vertex
:
3065 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3067 case nir_intrinsic_end_primitive
:
3068 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3070 case nir_intrinsic_load_tess_coord
:
3071 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3073 case nir_intrinsic_load_tess_level_outer
:
3074 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3076 case nir_intrinsic_load_tess_level_inner
:
3077 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3079 case nir_intrinsic_load_patch_vertices_in
:
3080 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3082 case nir_intrinsic_vote_all
: {
3083 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3084 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3087 case nir_intrinsic_vote_any
: {
3088 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3089 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3092 case nir_intrinsic_shuffle
:
3093 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3094 get_src(ctx
, instr
->src
[1]));
3096 case nir_intrinsic_reduce
:
3097 result
= ac_build_reduce(&ctx
->ac
,
3098 get_src(ctx
, instr
->src
[0]),
3099 instr
->const_index
[0],
3100 instr
->const_index
[1]);
3102 case nir_intrinsic_inclusive_scan
:
3103 result
= ac_build_inclusive_scan(&ctx
->ac
,
3104 get_src(ctx
, instr
->src
[0]),
3105 instr
->const_index
[0]);
3107 case nir_intrinsic_exclusive_scan
:
3108 result
= ac_build_exclusive_scan(&ctx
->ac
,
3109 get_src(ctx
, instr
->src
[0]),
3110 instr
->const_index
[0]);
3112 case nir_intrinsic_quad_broadcast
: {
3113 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3114 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3115 lane
, lane
, lane
, lane
);
3118 case nir_intrinsic_quad_swap_horizontal
:
3119 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3121 case nir_intrinsic_quad_swap_vertical
:
3122 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3124 case nir_intrinsic_quad_swap_diagonal
:
3125 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3128 fprintf(stderr
, "Unknown intrinsic: ");
3129 nir_print_instr(&instr
->instr
, stderr
);
3130 fprintf(stderr
, "\n");
3134 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3138 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3139 const nir_deref_var
*deref
,
3140 nir_deref_instr
*deref_instr
,
3141 enum ac_descriptor_type desc_type
,
3142 const nir_tex_instr
*tex_instr
,
3143 bool image
, bool write
)
3145 LLVMValueRef index
= NULL
;
3146 unsigned constant_index
= 0;
3147 unsigned descriptor_set
;
3148 unsigned base_index
;
3149 bool bindless
= false;
3151 if (!deref
&& !deref_instr
) {
3152 assert(tex_instr
&& !image
);
3154 base_index
= tex_instr
->sampler_index
;
3155 } else if(deref_instr
) {
3156 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3157 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3161 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3162 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3164 constant_index
+= array_size
* const_value
->u32
[0];
3166 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3168 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3169 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3174 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3177 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3179 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3180 base_index
= deref_instr
->var
->data
.binding
;
3182 const nir_deref
*tail
= &deref
->deref
;
3183 while (tail
->child
) {
3184 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
3185 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
3190 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3192 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3193 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
3195 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3196 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3201 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3204 constant_index
+= child
->base_offset
* array_size
;
3206 tail
= &child
->deref
;
3208 descriptor_set
= deref
->var
->data
.descriptor_set
;
3210 if (deref
->var
->data
.bindless
) {
3211 bindless
= deref
->var
->data
.bindless
;
3212 base_index
= deref
->var
->data
.driver_location
;
3214 base_index
= deref
->var
->data
.binding
;
3218 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3221 constant_index
, index
,
3222 desc_type
, image
, write
, bindless
);
3225 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3228 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3229 * filtering manually. The driver sets img7 to a mask clearing
3230 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3231 * s_and_b32 samp0, samp0, img7
3234 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3236 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3237 LLVMValueRef res
, LLVMValueRef samp
)
3239 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3240 LLVMValueRef img7
, samp0
;
3242 if (ctx
->ac
.chip_class
>= VI
)
3245 img7
= LLVMBuildExtractElement(builder
, res
,
3246 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3247 samp0
= LLVMBuildExtractElement(builder
, samp
,
3248 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3249 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3250 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3251 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3254 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3255 nir_tex_instr
*instr
,
3256 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3257 LLVMValueRef
*fmask_ptr
)
3259 nir_deref_instr
*texture_deref_instr
= NULL
;
3260 nir_deref_instr
*sampler_deref_instr
= NULL
;
3261 nir_deref_var
*texture_deref_var
= NULL
;
3262 nir_deref_var
*sampler_deref_var
= NULL
;
3264 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3265 switch (instr
->src
[i
].src_type
) {
3266 case nir_tex_src_texture_deref
:
3267 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3269 case nir_tex_src_sampler_deref
:
3270 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3277 if (!sampler_deref_instr
)
3278 sampler_deref_instr
= texture_deref_instr
;
3280 if (!texture_deref_instr
) {
3281 texture_deref_var
= instr
->texture
;
3282 sampler_deref_var
= instr
->sampler
? instr
->sampler
: instr
->texture
;
3285 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3286 *res_ptr
= get_sampler_desc(ctx
, texture_deref_var
, texture_deref_instr
, AC_DESC_BUFFER
, instr
, false, false);
3288 *res_ptr
= get_sampler_desc(ctx
, texture_deref_var
, texture_deref_instr
, AC_DESC_IMAGE
, instr
, false, false);
3290 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_var
, sampler_deref_instr
, AC_DESC_SAMPLER
, instr
, false, false);
3291 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3292 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3294 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3295 instr
->op
== nir_texop_samples_identical
))
3296 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, texture_deref_instr
, AC_DESC_FMASK
, instr
, false, false);
3299 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3302 coord
= ac_to_float(ctx
, coord
);
3303 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3304 coord
= ac_to_integer(ctx
, coord
);
3308 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3310 LLVMValueRef result
= NULL
;
3311 struct ac_image_args args
= { 0 };
3312 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3313 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3314 unsigned offset_src
= 0;
3316 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3318 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3319 switch (instr
->src
[i
].src_type
) {
3320 case nir_tex_src_coord
: {
3321 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3322 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3323 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3326 case nir_tex_src_projector
:
3328 case nir_tex_src_comparator
:
3329 if (instr
->is_shadow
)
3330 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3332 case nir_tex_src_offset
:
3333 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3336 case nir_tex_src_bias
:
3337 if (instr
->op
== nir_texop_txb
)
3338 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3340 case nir_tex_src_lod
: {
3341 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3343 if (val
&& val
->i32
[0] == 0)
3344 args
.level_zero
= true;
3346 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3349 case nir_tex_src_ms_index
:
3350 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3352 case nir_tex_src_ms_mcs
:
3354 case nir_tex_src_ddx
:
3355 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3357 case nir_tex_src_ddy
:
3358 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3360 case nir_tex_src_texture_offset
:
3361 case nir_tex_src_sampler_offset
:
3362 case nir_tex_src_plane
:
3368 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3369 result
= get_buffer_size(ctx
, args
.resource
, true);
3373 if (instr
->op
== nir_texop_texture_samples
) {
3374 LLVMValueRef res
, samples
, is_msaa
;
3375 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3376 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3377 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3378 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3379 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3380 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3381 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3382 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3383 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3385 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3386 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3387 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3388 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3389 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3391 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3397 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3398 LLVMValueRef offset
[3], pack
;
3399 for (unsigned chan
= 0; chan
< 3; ++chan
)
3400 offset
[chan
] = ctx
->ac
.i32_0
;
3402 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3403 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3404 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3405 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3406 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3408 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3409 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3411 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3412 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3416 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3417 * so the depth comparison value isn't clamped for Z16 and
3418 * Z24 anymore. Do it manually here.
3420 * It's unnecessary if the original texture format was
3421 * Z32_FLOAT, but we don't know that here.
3423 if (args
.compare
&& ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3424 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3426 /* pack derivatives */
3428 int num_src_deriv_channels
, num_dest_deriv_channels
;
3429 switch (instr
->sampler_dim
) {
3430 case GLSL_SAMPLER_DIM_3D
:
3431 case GLSL_SAMPLER_DIM_CUBE
:
3432 num_src_deriv_channels
= 3;
3433 num_dest_deriv_channels
= 3;
3435 case GLSL_SAMPLER_DIM_2D
:
3437 num_src_deriv_channels
= 2;
3438 num_dest_deriv_channels
= 2;
3440 case GLSL_SAMPLER_DIM_1D
:
3441 num_src_deriv_channels
= 1;
3442 if (ctx
->ac
.chip_class
>= GFX9
) {
3443 num_dest_deriv_channels
= 2;
3445 num_dest_deriv_channels
= 1;
3450 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3451 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3452 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3453 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3454 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3456 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3457 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3458 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3462 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3463 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3464 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3465 if (instr
->coord_components
== 3)
3466 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3467 ac_prepare_cube_coords(&ctx
->ac
,
3468 instr
->op
== nir_texop_txd
, instr
->is_array
,
3469 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3472 /* Texture coordinates fixups */
3473 if (instr
->coord_components
> 1 &&
3474 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3476 instr
->op
!= nir_texop_txf
) {
3477 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3480 if (instr
->coord_components
> 2 &&
3481 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3482 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3483 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3484 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3486 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3487 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3490 if (ctx
->ac
.chip_class
>= GFX9
&&
3491 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3492 instr
->op
!= nir_texop_lod
) {
3493 LLVMValueRef filler
;
3494 if (instr
->op
== nir_texop_txf
)
3495 filler
= ctx
->ac
.i32_0
;
3497 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3499 if (instr
->is_array
)
3500 args
.coords
[2] = args
.coords
[1];
3501 args
.coords
[1] = filler
;
3504 /* Pack sample index */
3505 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3506 args
.coords
[instr
->coord_components
] = sample_index
;
3508 if (instr
->op
== nir_texop_samples_identical
) {
3509 struct ac_image_args txf_args
= { 0 };
3510 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3512 txf_args
.dmask
= 0xf;
3513 txf_args
.resource
= fmask_ptr
;
3514 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3515 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3517 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3518 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3522 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3523 instr
->op
!= nir_texop_txs
) {
3524 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3525 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3526 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3527 instr
->is_array
? args
.coords
[2] : NULL
,
3528 args
.coords
[sample_chan
], fmask_ptr
);
3531 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3532 nir_const_value
*const_offset
=
3533 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3534 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3535 assert(const_offset
);
3536 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3537 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3538 args
.coords
[i
] = LLVMBuildAdd(
3539 ctx
->ac
.builder
, args
.coords
[i
],
3540 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3545 /* TODO TG4 support */
3547 if (instr
->op
== nir_texop_tg4
) {
3548 if (instr
->is_shadow
)
3551 args
.dmask
= 1 << instr
->component
;
3554 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3555 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3556 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3558 if (instr
->op
== nir_texop_query_levels
)
3559 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3560 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3561 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3562 instr
->op
!= nir_texop_tg4
)
3563 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3564 else if (instr
->op
== nir_texop_txs
&&
3565 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3567 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3568 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3569 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3570 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3571 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3572 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3573 instr
->op
== nir_texop_txs
&&
3574 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3576 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3577 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3578 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3580 } else if (instr
->dest
.ssa
.num_components
!= 4)
3581 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3585 assert(instr
->dest
.is_ssa
);
3586 result
= ac_to_integer(&ctx
->ac
, result
);
3587 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3592 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3594 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3595 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3597 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3598 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3601 static void visit_post_phi(struct ac_nir_context
*ctx
,
3602 nir_phi_instr
*instr
,
3603 LLVMValueRef llvm_phi
)
3605 nir_foreach_phi_src(src
, instr
) {
3606 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3607 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3609 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3613 static void phi_post_pass(struct ac_nir_context
*ctx
)
3615 struct hash_entry
*entry
;
3616 hash_table_foreach(ctx
->phis
, entry
) {
3617 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3618 (LLVMValueRef
)entry
->data
);
3623 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3624 const nir_ssa_undef_instr
*instr
)
3626 unsigned num_components
= instr
->def
.num_components
;
3627 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3630 if (num_components
== 1)
3631 undef
= LLVMGetUndef(type
);
3633 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3635 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3638 static void visit_jump(struct ac_llvm_context
*ctx
,
3639 const nir_jump_instr
*instr
)
3641 switch (instr
->type
) {
3642 case nir_jump_break
:
3643 ac_build_break(ctx
);
3645 case nir_jump_continue
:
3646 ac_build_continue(ctx
);
3649 fprintf(stderr
, "Unknown NIR jump instr: ");
3650 nir_print_instr(&instr
->instr
, stderr
);
3651 fprintf(stderr
, "\n");
3656 static void visit_deref(struct ac_nir_context
*ctx
,
3657 nir_deref_instr
*instr
)
3659 if (instr
->mode
!= nir_var_shared
)
3662 LLVMValueRef result
= NULL
;
3663 switch(instr
->deref_type
) {
3664 case nir_deref_type_var
: {
3665 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
3666 result
= entry
->data
;
3669 case nir_deref_type_struct
:
3670 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3671 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
3673 case nir_deref_type_array
:
3674 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3675 get_src(ctx
, instr
->arr
.index
));
3678 unreachable("Unhandled deref_instr deref type");
3681 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3684 static void visit_cf_list(struct ac_nir_context
*ctx
,
3685 struct exec_list
*list
);
3687 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3689 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3690 nir_foreach_instr(instr
, block
)
3692 switch (instr
->type
) {
3693 case nir_instr_type_alu
:
3694 visit_alu(ctx
, nir_instr_as_alu(instr
));
3696 case nir_instr_type_load_const
:
3697 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3699 case nir_instr_type_intrinsic
:
3700 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3702 case nir_instr_type_tex
:
3703 visit_tex(ctx
, nir_instr_as_tex(instr
));
3705 case nir_instr_type_phi
:
3706 visit_phi(ctx
, nir_instr_as_phi(instr
));
3708 case nir_instr_type_ssa_undef
:
3709 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3711 case nir_instr_type_jump
:
3712 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3714 case nir_instr_type_deref
:
3715 visit_deref(ctx
, nir_instr_as_deref(instr
));
3718 fprintf(stderr
, "Unknown NIR instr type: ");
3719 nir_print_instr(instr
, stderr
);
3720 fprintf(stderr
, "\n");
3725 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3728 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3730 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3732 nir_block
*then_block
=
3733 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3735 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3737 visit_cf_list(ctx
, &if_stmt
->then_list
);
3739 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3740 nir_block
*else_block
=
3741 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3743 ac_build_else(&ctx
->ac
, else_block
->index
);
3744 visit_cf_list(ctx
, &if_stmt
->else_list
);
3747 ac_build_endif(&ctx
->ac
, then_block
->index
);
3750 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3752 nir_block
*first_loop_block
=
3753 (nir_block
*) exec_list_get_head(&loop
->body
);
3755 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3757 visit_cf_list(ctx
, &loop
->body
);
3759 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3762 static void visit_cf_list(struct ac_nir_context
*ctx
,
3763 struct exec_list
*list
)
3765 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3767 switch (node
->type
) {
3768 case nir_cf_node_block
:
3769 visit_block(ctx
, nir_cf_node_as_block(node
));
3772 case nir_cf_node_if
:
3773 visit_if(ctx
, nir_cf_node_as_if(node
));
3776 case nir_cf_node_loop
:
3777 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3787 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3788 struct ac_shader_abi
*abi
,
3789 struct nir_shader
*nir
,
3790 struct nir_variable
*variable
,
3791 gl_shader_stage stage
)
3793 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3794 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3796 /* tess ctrl has it's own load/store paths for outputs */
3797 if (stage
== MESA_SHADER_TESS_CTRL
)
3800 if (stage
== MESA_SHADER_VERTEX
||
3801 stage
== MESA_SHADER_TESS_EVAL
||
3802 stage
== MESA_SHADER_GEOMETRY
) {
3803 int idx
= variable
->data
.location
+ variable
->data
.index
;
3804 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3805 int length
= nir
->info
.clip_distance_array_size
+
3806 nir
->info
.cull_distance_array_size
;
3815 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3816 for (unsigned chan
= 0; chan
< 4; chan
++) {
3817 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3818 ac_build_alloca_undef(ctx
, ctx
->f32
, "");
3824 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3825 enum glsl_base_type type
)
3829 case GLSL_TYPE_UINT
:
3830 case GLSL_TYPE_BOOL
:
3831 case GLSL_TYPE_SUBROUTINE
:
3833 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3835 case GLSL_TYPE_INT64
:
3836 case GLSL_TYPE_UINT64
:
3838 case GLSL_TYPE_DOUBLE
:
3841 unreachable("unknown GLSL type");
3846 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3847 const struct glsl_type
*type
)
3849 if (glsl_type_is_scalar(type
)) {
3850 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3853 if (glsl_type_is_vector(type
)) {
3854 return LLVMVectorType(
3855 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3856 glsl_get_vector_elements(type
));
3859 if (glsl_type_is_matrix(type
)) {
3860 return LLVMArrayType(
3861 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3862 glsl_get_matrix_columns(type
));
3865 if (glsl_type_is_array(type
)) {
3866 return LLVMArrayType(
3867 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3868 glsl_get_length(type
));
3871 assert(glsl_type_is_struct(type
));
3873 LLVMTypeRef member_types
[glsl_get_length(type
)];
3875 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3877 glsl_to_llvm_type(ac
,
3878 glsl_get_struct_field(type
, i
));
3881 return LLVMStructTypeInContext(ac
->context
, member_types
,
3882 glsl_get_length(type
), false);
3886 setup_locals(struct ac_nir_context
*ctx
,
3887 struct nir_function
*func
)
3890 ctx
->num_locals
= 0;
3891 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3892 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3893 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3894 variable
->data
.location_frac
= 0;
3895 ctx
->num_locals
+= attrib_count
;
3897 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3901 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3902 for (j
= 0; j
< 4; j
++) {
3903 ctx
->locals
[i
* 4 + j
] =
3904 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3910 setup_shared(struct ac_nir_context
*ctx
,
3911 struct nir_shader
*nir
)
3913 nir_foreach_variable(variable
, &nir
->shared
) {
3914 LLVMValueRef shared
=
3915 LLVMAddGlobalInAddressSpace(
3916 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3917 variable
->name
? variable
->name
: "",
3918 AC_LOCAL_ADDR_SPACE
);
3919 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3923 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3924 struct nir_shader
*nir
)
3926 struct ac_nir_context ctx
= {};
3927 struct nir_function
*func
;
3932 ctx
.stage
= nir
->info
.stage
;
3934 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3936 nir_foreach_variable(variable
, &nir
->outputs
)
3937 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3940 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3941 _mesa_key_pointer_equal
);
3942 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3943 _mesa_key_pointer_equal
);
3944 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3945 _mesa_key_pointer_equal
);
3947 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3949 nir_index_ssa_defs(func
->impl
);
3950 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
3952 setup_locals(&ctx
, func
);
3954 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3955 setup_shared(&ctx
, nir
);
3957 visit_cf_list(&ctx
, &func
->impl
->body
);
3958 phi_post_pass(&ctx
);
3960 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3961 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3966 ralloc_free(ctx
.defs
);
3967 ralloc_free(ctx
.phis
);
3968 ralloc_free(ctx
.vars
);
3972 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3974 /* While it would be nice not to have this flag, we are constrained
3975 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3978 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
3980 /* TODO: Indirect indexing of GS inputs is unimplemented.
3982 * TCS and TES load inputs directly from LDS or offchip memory, so
3983 * indirect indexing is trivial.
3985 nir_variable_mode indirect_mask
= 0;
3986 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
3987 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
3988 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
3989 !llvm_has_working_vgpr_indexing
)) {
3990 indirect_mask
|= nir_var_shader_in
;
3992 if (!llvm_has_working_vgpr_indexing
&&
3993 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
3994 indirect_mask
|= nir_var_shader_out
;
3996 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3997 * smart enough to handle indirects without causing excess spilling
3998 * causing the gpu to hang.
4000 * See the following thread for more details of the problem:
4001 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4003 indirect_mask
|= nir_var_local
;
4005 nir_lower_indirect_derefs(nir
, indirect_mask
);