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 "../vulkan/radv_descriptor_set.h"
31 #include "util/bitscan.h"
32 #include <llvm-c/Transforms/Scalar.h>
33 #include "ac_shader_abi.h"
34 #include "ac_shader_info.h"
35 #include "ac_shader_util.h"
36 #include "ac_exp_param.h"
38 enum radeon_llvm_calling_convention
{
39 RADEON_LLVM_AMDGPU_VS
= 87,
40 RADEON_LLVM_AMDGPU_GS
= 88,
41 RADEON_LLVM_AMDGPU_PS
= 89,
42 RADEON_LLVM_AMDGPU_CS
= 90,
43 RADEON_LLVM_AMDGPU_HS
= 93,
46 #define CONST_ADDR_SPACE 2
47 #define LOCAL_ADDR_SPACE 3
49 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
50 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
52 struct nir_to_llvm_context
;
54 struct ac_nir_context
{
55 struct ac_llvm_context ac
;
56 struct ac_shader_abi
*abi
;
58 gl_shader_stage stage
;
60 struct hash_table
*defs
;
61 struct hash_table
*phis
;
62 struct hash_table
*vars
;
64 LLVMValueRef main_function
;
65 LLVMBasicBlockRef continue_block
;
66 LLVMBasicBlockRef break_block
;
68 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
73 struct nir_to_llvm_context
*nctx
; /* TODO get rid of this */
76 struct nir_to_llvm_context
{
77 struct ac_llvm_context ac
;
78 const struct ac_nir_compiler_options
*options
;
79 struct ac_shader_variant_info
*shader_info
;
80 struct ac_shader_abi abi
;
81 struct ac_nir_context
*nir
;
83 unsigned max_workgroup_size
;
84 LLVMContextRef context
;
86 LLVMBuilderRef builder
;
87 LLVMValueRef main_function
;
89 struct hash_table
*defs
;
90 struct hash_table
*phis
;
92 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
93 LLVMValueRef ring_offsets
;
94 LLVMValueRef push_constants
;
95 LLVMValueRef view_index
;
96 LLVMValueRef num_work_groups
;
97 LLVMValueRef workgroup_ids
[3];
98 LLVMValueRef local_invocation_ids
;
101 LLVMValueRef vertex_buffers
;
102 LLVMValueRef rel_auto_id
;
103 LLVMValueRef vs_prim_id
;
104 LLVMValueRef ls_out_layout
;
105 LLVMValueRef es2gs_offset
;
107 LLVMValueRef tcs_offchip_layout
;
108 LLVMValueRef tcs_out_offsets
;
109 LLVMValueRef tcs_out_layout
;
110 LLVMValueRef tcs_in_layout
;
112 LLVMValueRef merged_wave_info
;
113 LLVMValueRef tess_factor_offset
;
114 LLVMValueRef tcs_patch_id
;
115 LLVMValueRef tcs_rel_ids
;
116 LLVMValueRef tes_rel_patch_id
;
117 LLVMValueRef tes_patch_id
;
121 LLVMValueRef gsvs_ring_stride
;
122 LLVMValueRef gsvs_num_entries
;
123 LLVMValueRef gs2vs_offset
;
124 LLVMValueRef gs_wave_id
;
125 LLVMValueRef gs_vtx_offset
[6];
127 LLVMValueRef esgs_ring
;
128 LLVMValueRef gsvs_ring
;
129 LLVMValueRef hs_ring_tess_offchip
;
130 LLVMValueRef hs_ring_tess_factor
;
132 LLVMValueRef prim_mask
;
133 LLVMValueRef sample_pos_offset
;
134 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
135 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
137 gl_shader_stage stage
;
139 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
142 uint64_t output_mask
;
143 uint8_t num_output_clips
;
144 uint8_t num_output_culls
;
146 bool is_gs_copy_shader
;
147 LLVMValueRef gs_next_vertex
;
148 unsigned gs_max_out_vertices
;
150 unsigned tes_primitive_mode
;
151 uint64_t tess_outputs_written
;
152 uint64_t tess_patch_outputs_written
;
154 uint32_t tcs_patch_outputs_read
;
155 uint64_t tcs_outputs_read
;
158 static inline struct nir_to_llvm_context
*
159 nir_to_llvm_context_from_abi(struct ac_shader_abi
*abi
)
161 struct nir_to_llvm_context
*ctx
= NULL
;
162 return container_of(abi
, ctx
, abi
);
166 nir2llvmtype(struct ac_nir_context
*ctx
,
167 const struct glsl_type
*type
)
169 switch (glsl_get_base_type(glsl_without_array(type
))) {
173 case GLSL_TYPE_UINT64
:
174 case GLSL_TYPE_INT64
:
176 case GLSL_TYPE_DOUBLE
:
178 case GLSL_TYPE_FLOAT
:
181 assert(!"Unsupported type in nir2llvmtype()");
187 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
188 const nir_deref_var
*deref
,
189 enum ac_descriptor_type desc_type
,
190 const nir_tex_instr
*instr
,
191 bool image
, bool write
);
193 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
195 return (index
* 4) + chan
;
198 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
200 /* handle patch indices separate */
201 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
203 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
205 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
206 return 2 + (slot
- VARYING_SLOT_PATCH0
);
208 if (slot
== VARYING_SLOT_POS
)
210 if (slot
== VARYING_SLOT_PSIZ
)
212 if (slot
== VARYING_SLOT_CLIP_DIST0
)
214 /* 3 is reserved for clip dist as well */
215 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
216 return 4 + (slot
- VARYING_SLOT_VAR0
);
217 unreachable("illegal slot in get unique index\n");
220 static void set_llvm_calling_convention(LLVMValueRef func
,
221 gl_shader_stage stage
)
223 enum radeon_llvm_calling_convention calling_conv
;
226 case MESA_SHADER_VERTEX
:
227 case MESA_SHADER_TESS_EVAL
:
228 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
230 case MESA_SHADER_GEOMETRY
:
231 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
233 case MESA_SHADER_TESS_CTRL
:
234 calling_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
: RADEON_LLVM_AMDGPU_VS
;
236 case MESA_SHADER_FRAGMENT
:
237 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
239 case MESA_SHADER_COMPUTE
:
240 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
243 unreachable("Unhandle shader type");
246 LLVMSetFunctionCallConv(func
, calling_conv
);
251 LLVMTypeRef types
[MAX_ARGS
];
252 LLVMValueRef
*assign
[MAX_ARGS
];
253 unsigned array_params_mask
;
256 uint8_t num_sgprs_used
;
257 uint8_t num_vgprs_used
;
260 enum ac_arg_regfile
{
266 add_arg(struct arg_info
*info
, enum ac_arg_regfile regfile
, LLVMTypeRef type
,
267 LLVMValueRef
*param_ptr
)
269 assert(info
->count
< MAX_ARGS
);
271 info
->assign
[info
->count
] = param_ptr
;
272 info
->types
[info
->count
] = type
;
275 if (regfile
== ARG_SGPR
) {
276 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
279 assert(regfile
== ARG_VGPR
);
280 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
285 add_array_arg(struct arg_info
*info
, LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
287 info
->array_params_mask
|= (1 << info
->count
);
288 add_arg(info
, ARG_SGPR
, type
, param_ptr
);
291 static void assign_arguments(LLVMValueRef main_function
,
292 struct arg_info
*info
)
295 for (i
= 0; i
< info
->count
; i
++) {
297 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
302 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
303 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
304 unsigned num_return_elems
,
305 struct arg_info
*args
,
306 unsigned max_workgroup_size
,
309 LLVMTypeRef main_function_type
, ret_type
;
310 LLVMBasicBlockRef main_function_body
;
312 if (num_return_elems
)
313 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
314 num_return_elems
, true);
316 ret_type
= LLVMVoidTypeInContext(ctx
);
318 /* Setup the function */
320 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
321 LLVMValueRef main_function
=
322 LLVMAddFunction(module
, "main", main_function_type
);
324 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
325 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
327 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
328 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
329 if (args
->array_params_mask
& (1 << i
)) {
330 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
331 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
332 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
335 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
339 if (max_workgroup_size
) {
340 ac_llvm_add_target_dep_function_attr(main_function
,
341 "amdgpu-max-work-group-size",
345 /* These were copied from some LLVM test. */
346 LLVMAddTargetDependentFunctionAttr(main_function
,
347 "less-precise-fpmad",
349 LLVMAddTargetDependentFunctionAttr(main_function
,
352 LLVMAddTargetDependentFunctionAttr(main_function
,
355 LLVMAddTargetDependentFunctionAttr(main_function
,
359 return main_function
;
362 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
364 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
368 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
370 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
371 type
= LLVMGetElementType(type
);
373 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
374 return LLVMGetIntTypeWidth(type
);
376 if (type
== ctx
->f16
)
378 if (type
== ctx
->f32
)
380 if (type
== ctx
->f64
)
383 unreachable("Unhandled type kind in get_elem_bits");
386 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
387 LLVMValueRef param
, unsigned rshift
,
390 LLVMValueRef value
= param
;
392 value
= LLVMBuildLShr(ctx
->builder
, value
,
393 LLVMConstInt(ctx
->i32
, rshift
, false), "");
395 if (rshift
+ bitwidth
< 32) {
396 unsigned mask
= (1 << bitwidth
) - 1;
397 value
= LLVMBuildAnd(ctx
->builder
, value
,
398 LLVMConstInt(ctx
->i32
, mask
, false), "");
403 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
405 switch (ctx
->stage
) {
406 case MESA_SHADER_TESS_CTRL
:
407 return unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
408 case MESA_SHADER_TESS_EVAL
:
409 return ctx
->tes_rel_patch_id
;
412 unreachable("Illegal stage");
416 /* Tessellation shaders pass outputs to the next shader using LDS.
418 * LS outputs = TCS inputs
419 * TCS outputs = TES inputs
422 * - TCS inputs for patch 0
423 * - TCS inputs for patch 1
424 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
426 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
427 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
428 * - TCS outputs for patch 1
429 * - Per-patch TCS outputs for patch 1
430 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
431 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
434 * All three shaders VS(LS), TCS, TES share the same LDS space.
437 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
439 if (ctx
->stage
== MESA_SHADER_VERTEX
)
440 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
441 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
442 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
450 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
452 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
456 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
458 return LLVMBuildMul(ctx
->builder
,
459 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
460 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
464 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
466 return LLVMBuildMul(ctx
->builder
,
467 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
468 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
472 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
474 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
475 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
477 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
481 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
483 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
484 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
485 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
487 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
488 LLVMBuildMul(ctx
->builder
, patch_stride
,
494 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
496 LLVMValueRef patch0_patch_data_offset
=
497 get_tcs_out_patch0_patch_data_offset(ctx
);
498 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
499 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
501 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
502 LLVMBuildMul(ctx
->builder
, patch_stride
,
507 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
509 ud_info
->sgpr_idx
= *sgpr_idx
;
510 ud_info
->num_sgprs
= num_sgprs
;
511 ud_info
->indirect
= false;
512 ud_info
->indirect_offset
= 0;
513 *sgpr_idx
+= num_sgprs
;
517 set_loc_shader(struct nir_to_llvm_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
520 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
524 set_loc_desc(struct nir_to_llvm_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
525 uint32_t indirect_offset
)
527 struct ac_userdata_info
*ud_info
=
528 &ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[idx
];
531 ud_info
->sgpr_idx
= *sgpr_idx
;
532 ud_info
->num_sgprs
= 2;
533 ud_info
->indirect
= indirect_offset
> 0;
534 ud_info
->indirect_offset
= indirect_offset
;
537 struct user_sgpr_info
{
538 bool need_ring_offsets
;
540 bool indirect_all_descriptor_sets
;
543 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
544 struct user_sgpr_info
*user_sgpr_info
)
546 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
548 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
549 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
550 ctx
->stage
== MESA_SHADER_VERTEX
||
551 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
552 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
553 ctx
->is_gs_copy_shader
)
554 user_sgpr_info
->need_ring_offsets
= true;
556 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
557 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
558 user_sgpr_info
->need_ring_offsets
= true;
560 /* 2 user sgprs will nearly always be allocated for scratch/rings */
561 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
562 user_sgpr_info
->sgpr_count
+= 2;
565 switch (ctx
->stage
) {
566 case MESA_SHADER_COMPUTE
:
567 if (ctx
->shader_info
->info
.cs
.uses_grid_size
)
568 user_sgpr_info
->sgpr_count
+= 3;
570 case MESA_SHADER_FRAGMENT
:
571 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
573 case MESA_SHADER_VERTEX
:
574 if (!ctx
->is_gs_copy_shader
) {
575 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
576 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
577 user_sgpr_info
->sgpr_count
+= 3;
579 user_sgpr_info
->sgpr_count
+= 2;
582 if (ctx
->options
->key
.vs
.as_ls
)
583 user_sgpr_info
->sgpr_count
++;
585 case MESA_SHADER_TESS_CTRL
:
586 user_sgpr_info
->sgpr_count
+= 4;
588 case MESA_SHADER_TESS_EVAL
:
589 user_sgpr_info
->sgpr_count
+= 1;
591 case MESA_SHADER_GEOMETRY
:
592 user_sgpr_info
->sgpr_count
+= 2;
598 if (ctx
->shader_info
->info
.needs_push_constants
)
599 user_sgpr_info
->sgpr_count
+= 2;
601 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
602 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
603 user_sgpr_info
->sgpr_count
+= 2;
604 user_sgpr_info
->indirect_all_descriptor_sets
= true;
606 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
611 declare_global_input_sgprs(struct nir_to_llvm_context
*ctx
,
612 gl_shader_stage stage
,
613 bool has_previous_stage
,
614 gl_shader_stage previous_stage
,
615 const struct user_sgpr_info
*user_sgpr_info
,
616 struct arg_info
*args
,
617 LLVMValueRef
*desc_sets
)
619 LLVMTypeRef type
= const_array(ctx
->ac
.i8
, 1024 * 1024);
620 unsigned num_sets
= ctx
->options
->layout
?
621 ctx
->options
->layout
->num_sets
: 0;
622 unsigned stage_mask
= 1 << stage
;
624 if (has_previous_stage
)
625 stage_mask
|= 1 << previous_stage
;
627 /* 1 for each descriptor set */
628 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
629 for (unsigned i
= 0; i
< num_sets
; ++i
) {
630 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
631 add_array_arg(args
, type
,
632 &ctx
->descriptor_sets
[i
]);
636 add_array_arg(args
, const_array(type
, 32), desc_sets
);
639 if (ctx
->shader_info
->info
.needs_push_constants
) {
640 /* 1 for push constants and dynamic descriptors */
641 add_array_arg(args
, type
, &ctx
->push_constants
);
646 declare_vs_specific_input_sgprs(struct nir_to_llvm_context
*ctx
,
647 gl_shader_stage stage
,
648 bool has_previous_stage
,
649 gl_shader_stage previous_stage
,
650 struct arg_info
*args
)
652 if (!ctx
->is_gs_copy_shader
&&
653 (stage
== MESA_SHADER_VERTEX
||
654 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
655 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
656 add_arg(args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
657 &ctx
->vertex_buffers
);
659 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
);
660 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);
661 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
662 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
);
668 declare_vs_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
670 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
);
671 if (!ctx
->is_gs_copy_shader
) {
672 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->rel_auto_id
);
673 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->vs_prim_id
);
674 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
679 declare_tes_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
681 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_u
);
682 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_v
);
683 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
);
684 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->tes_patch_id
);
688 set_global_input_locs(struct nir_to_llvm_context
*ctx
, gl_shader_stage stage
,
689 bool has_previous_stage
, gl_shader_stage previous_stage
,
690 const struct user_sgpr_info
*user_sgpr_info
,
691 LLVMValueRef desc_sets
, uint8_t *user_sgpr_idx
)
693 unsigned num_sets
= ctx
->options
->layout
?
694 ctx
->options
->layout
->num_sets
: 0;
695 unsigned stage_mask
= 1 << stage
;
697 if (has_previous_stage
)
698 stage_mask
|= 1 << previous_stage
;
700 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
701 for (unsigned i
= 0; i
< num_sets
; ++i
) {
702 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
703 set_loc_desc(ctx
, i
, user_sgpr_idx
, 0);
705 ctx
->descriptor_sets
[i
] = NULL
;
708 set_loc_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
,
711 for (unsigned i
= 0; i
< num_sets
; ++i
) {
712 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
713 set_loc_desc(ctx
, i
, user_sgpr_idx
, i
* 8);
714 ctx
->descriptor_sets
[i
] =
715 ac_build_load_to_sgpr(&ctx
->ac
,
717 LLVMConstInt(ctx
->ac
.i32
, i
, false));
720 ctx
->descriptor_sets
[i
] = NULL
;
722 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
725 if (ctx
->shader_info
->info
.needs_push_constants
) {
726 set_loc_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
731 set_vs_specific_input_locs(struct nir_to_llvm_context
*ctx
,
732 gl_shader_stage stage
, bool has_previous_stage
,
733 gl_shader_stage previous_stage
,
734 uint8_t *user_sgpr_idx
)
736 if (!ctx
->is_gs_copy_shader
&&
737 (stage
== MESA_SHADER_VERTEX
||
738 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
739 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
740 set_loc_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
,
745 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
748 set_loc_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
749 user_sgpr_idx
, vs_num
);
753 static void create_function(struct nir_to_llvm_context
*ctx
,
754 gl_shader_stage stage
,
755 bool has_previous_stage
,
756 gl_shader_stage previous_stage
)
758 uint8_t user_sgpr_idx
;
759 struct user_sgpr_info user_sgpr_info
;
760 struct arg_info args
= {};
761 LLVMValueRef desc_sets
;
763 allocate_user_sgprs(ctx
, &user_sgpr_info
);
765 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
766 add_arg(&args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
771 case MESA_SHADER_COMPUTE
:
772 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
773 previous_stage
, &user_sgpr_info
,
776 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
777 add_arg(&args
, ARG_SGPR
, ctx
->ac
.v3i32
,
778 &ctx
->num_work_groups
);
781 for (int i
= 0; i
< 3; i
++) {
782 ctx
->workgroup_ids
[i
] = NULL
;
783 if (ctx
->shader_info
->info
.cs
.uses_block_id
[i
]) {
784 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
785 &ctx
->workgroup_ids
[i
]);
789 if (ctx
->shader_info
->info
.cs
.uses_local_invocation_idx
)
790 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tg_size
);
791 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
,
792 &ctx
->local_invocation_ids
);
794 case MESA_SHADER_VERTEX
:
795 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
796 previous_stage
, &user_sgpr_info
,
798 declare_vs_specific_input_sgprs(ctx
, stage
, has_previous_stage
,
799 previous_stage
, &args
);
801 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.vs
.as_es
&& !ctx
->options
->key
.vs
.as_ls
&& ctx
->options
->key
.has_multiview_view_index
))
802 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
803 if (ctx
->options
->key
.vs
.as_es
)
804 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
806 else if (ctx
->options
->key
.vs
.as_ls
)
807 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
808 &ctx
->ls_out_layout
);
810 declare_vs_input_vgprs(ctx
, &args
);
812 case MESA_SHADER_TESS_CTRL
:
813 if (has_previous_stage
) {
814 // First 6 system regs
815 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
816 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
817 &ctx
->merged_wave_info
);
818 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
819 &ctx
->tess_factor_offset
);
821 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
822 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
823 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
825 declare_global_input_sgprs(ctx
, stage
,
828 &user_sgpr_info
, &args
,
830 declare_vs_specific_input_sgprs(ctx
, stage
,
832 previous_stage
, &args
);
834 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
835 &ctx
->ls_out_layout
);
837 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
838 &ctx
->tcs_offchip_layout
);
839 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
840 &ctx
->tcs_out_offsets
);
841 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
842 &ctx
->tcs_out_layout
);
843 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
844 &ctx
->tcs_in_layout
);
845 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
846 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
849 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
851 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
854 declare_vs_input_vgprs(ctx
, &args
);
856 declare_global_input_sgprs(ctx
, stage
,
859 &user_sgpr_info
, &args
,
862 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
863 &ctx
->tcs_offchip_layout
);
864 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
865 &ctx
->tcs_out_offsets
);
866 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
867 &ctx
->tcs_out_layout
);
868 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
869 &ctx
->tcs_in_layout
);
870 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
871 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
874 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
875 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
876 &ctx
->tess_factor_offset
);
877 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
879 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
883 case MESA_SHADER_TESS_EVAL
:
884 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
885 previous_stage
, &user_sgpr_info
,
888 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
);
889 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
890 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
892 if (ctx
->options
->key
.tes
.as_es
) {
893 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
894 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
895 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
898 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
899 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
901 declare_tes_input_vgprs(ctx
, &args
);
903 case MESA_SHADER_GEOMETRY
:
904 if (has_previous_stage
) {
905 // First 6 system regs
906 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
908 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
909 &ctx
->merged_wave_info
);
910 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
912 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
913 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
914 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
916 declare_global_input_sgprs(ctx
, stage
,
919 &user_sgpr_info
, &args
,
922 if (previous_stage
== MESA_SHADER_TESS_EVAL
) {
923 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
924 &ctx
->tcs_offchip_layout
);
926 declare_vs_specific_input_sgprs(ctx
, stage
,
932 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
933 &ctx
->gsvs_ring_stride
);
934 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
935 &ctx
->gsvs_num_entries
);
936 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
937 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
940 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
941 &ctx
->gs_vtx_offset
[0]);
942 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
943 &ctx
->gs_vtx_offset
[2]);
944 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
945 &ctx
->abi
.gs_prim_id
);
946 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
947 &ctx
->abi
.gs_invocation_id
);
948 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
949 &ctx
->gs_vtx_offset
[4]);
951 if (previous_stage
== MESA_SHADER_VERTEX
) {
952 declare_vs_input_vgprs(ctx
, &args
);
954 declare_tes_input_vgprs(ctx
, &args
);
957 declare_global_input_sgprs(ctx
, stage
,
960 &user_sgpr_info
, &args
,
963 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
964 &ctx
->gsvs_ring_stride
);
965 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
966 &ctx
->gsvs_num_entries
);
967 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
968 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
971 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
);
972 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs_wave_id
);
973 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
974 &ctx
->gs_vtx_offset
[0]);
975 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
976 &ctx
->gs_vtx_offset
[1]);
977 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
978 &ctx
->abi
.gs_prim_id
);
979 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
980 &ctx
->gs_vtx_offset
[2]);
981 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
982 &ctx
->gs_vtx_offset
[3]);
983 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
984 &ctx
->gs_vtx_offset
[4]);
985 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
986 &ctx
->gs_vtx_offset
[5]);
987 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
988 &ctx
->abi
.gs_invocation_id
);
991 case MESA_SHADER_FRAGMENT
:
992 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
993 previous_stage
, &user_sgpr_info
,
996 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
997 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
998 &ctx
->sample_pos_offset
);
1000 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->prim_mask
);
1001 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_sample
);
1002 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_center
);
1003 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
);
1004 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
1005 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_sample
);
1006 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_center
);
1007 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
);
1008 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
1009 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]);
1010 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]);
1011 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]);
1012 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]);
1013 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.front_face
);
1014 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
);
1015 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
);
1016 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* fixed pt */
1019 unreachable("Shader stage not implemented");
1022 ctx
->main_function
= create_llvm_function(
1023 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
1024 ctx
->max_workgroup_size
,
1025 ctx
->options
->unsafe_math
);
1026 set_llvm_calling_convention(ctx
->main_function
, stage
);
1029 ctx
->shader_info
->num_input_vgprs
= 0;
1030 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
1032 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
1034 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1035 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
1037 assign_arguments(ctx
->main_function
, &args
);
1041 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
1042 set_loc_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
,
1044 if (ctx
->options
->supports_spill
) {
1045 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
1046 LLVMPointerType(ctx
->ac
.i8
, CONST_ADDR_SPACE
),
1047 NULL
, 0, AC_FUNC_ATTR_READNONE
);
1048 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
1049 const_array(ctx
->ac
.v4i32
, 16), "");
1053 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
1054 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
1055 if (has_previous_stage
)
1058 set_global_input_locs(ctx
, stage
, has_previous_stage
, previous_stage
,
1059 &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
1062 case MESA_SHADER_COMPUTE
:
1063 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
1064 set_loc_shader(ctx
, AC_UD_CS_GRID_SIZE
,
1068 case MESA_SHADER_VERTEX
:
1069 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1070 previous_stage
, &user_sgpr_idx
);
1071 if (ctx
->view_index
)
1072 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1073 if (ctx
->options
->key
.vs
.as_ls
) {
1074 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1077 if (ctx
->options
->key
.vs
.as_ls
)
1078 ac_declare_lds_as_pointer(&ctx
->ac
);
1080 case MESA_SHADER_TESS_CTRL
:
1081 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1082 previous_stage
, &user_sgpr_idx
);
1083 if (has_previous_stage
)
1084 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1086 set_loc_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
1087 if (ctx
->view_index
)
1088 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1089 ac_declare_lds_as_pointer(&ctx
->ac
);
1091 case MESA_SHADER_TESS_EVAL
:
1092 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
1093 if (ctx
->view_index
)
1094 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1096 case MESA_SHADER_GEOMETRY
:
1097 if (has_previous_stage
) {
1098 if (previous_stage
== MESA_SHADER_VERTEX
)
1099 set_vs_specific_input_locs(ctx
, stage
,
1104 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
,
1107 set_loc_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
,
1109 if (ctx
->view_index
)
1110 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1111 if (has_previous_stage
)
1112 ac_declare_lds_as_pointer(&ctx
->ac
);
1114 case MESA_SHADER_FRAGMENT
:
1115 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
1116 set_loc_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
,
1121 unreachable("Shader stage not implemented");
1124 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
1127 static int get_llvm_num_components(LLVMValueRef value
)
1129 LLVMTypeRef type
= LLVMTypeOf(value
);
1130 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1131 ? LLVMGetVectorSize(type
)
1133 return num_components
;
1136 static LLVMValueRef
llvm_extract_elem(struct ac_llvm_context
*ac
,
1140 int count
= get_llvm_num_components(value
);
1145 return LLVMBuildExtractElement(ac
->builder
, value
,
1146 LLVMConstInt(ac
->i32
, index
, false), "");
1149 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1150 LLVMValueRef value
, unsigned count
)
1152 unsigned num_components
= get_llvm_num_components(value
);
1153 if (count
== num_components
)
1156 LLVMValueRef masks
[] = {
1157 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1158 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1161 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1164 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1165 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1169 build_store_values_extended(struct ac_llvm_context
*ac
,
1170 LLVMValueRef
*values
,
1171 unsigned value_count
,
1172 unsigned value_stride
,
1175 LLVMBuilderRef builder
= ac
->builder
;
1178 for (i
= 0; i
< value_count
; i
++) {
1179 LLVMValueRef ptr
= values
[i
* value_stride
];
1180 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1181 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1182 LLVMBuildStore(builder
, value
, ptr
);
1186 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1187 const nir_ssa_def
*def
)
1189 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1190 if (def
->num_components
> 1) {
1191 type
= LLVMVectorType(type
, def
->num_components
);
1196 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1199 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1200 return (LLVMValueRef
)entry
->data
;
1204 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1205 const struct nir_block
*b
)
1207 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1208 return (LLVMBasicBlockRef
)entry
->data
;
1211 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1213 unsigned num_components
)
1215 LLVMValueRef value
= get_src(ctx
, src
.src
);
1216 bool need_swizzle
= false;
1219 LLVMTypeRef type
= LLVMTypeOf(value
);
1220 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1221 ? LLVMGetVectorSize(type
)
1224 for (unsigned i
= 0; i
< num_components
; ++i
) {
1225 assert(src
.swizzle
[i
] < src_components
);
1226 if (src
.swizzle
[i
] != i
)
1227 need_swizzle
= true;
1230 if (need_swizzle
|| num_components
!= src_components
) {
1231 LLVMValueRef masks
[] = {
1232 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1233 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1234 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1235 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1237 if (src_components
> 1 && num_components
== 1) {
1238 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1240 } else if (src_components
== 1 && num_components
> 1) {
1241 LLVMValueRef values
[] = {value
, value
, value
, value
};
1242 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1244 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1245 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1249 assert(!src
.negate
);
1254 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1255 LLVMIntPredicate pred
, LLVMValueRef src0
,
1258 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1259 return LLVMBuildSelect(ctx
->builder
, result
,
1260 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1264 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1265 LLVMRealPredicate pred
, LLVMValueRef src0
,
1268 LLVMValueRef result
;
1269 src0
= ac_to_float(ctx
, src0
);
1270 src1
= ac_to_float(ctx
, src1
);
1271 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1272 return LLVMBuildSelect(ctx
->builder
, result
,
1273 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1277 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1279 LLVMTypeRef result_type
,
1283 LLVMValueRef params
[] = {
1284 ac_to_float(ctx
, src0
),
1287 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1288 get_elem_bits(ctx
, result_type
));
1289 assert(length
< sizeof(name
));
1290 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1293 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1295 LLVMTypeRef result_type
,
1296 LLVMValueRef src0
, LLVMValueRef src1
)
1299 LLVMValueRef params
[] = {
1300 ac_to_float(ctx
, src0
),
1301 ac_to_float(ctx
, src1
),
1304 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1305 get_elem_bits(ctx
, result_type
));
1306 assert(length
< sizeof(name
));
1307 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1310 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1312 LLVMTypeRef result_type
,
1313 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1316 LLVMValueRef params
[] = {
1317 ac_to_float(ctx
, src0
),
1318 ac_to_float(ctx
, src1
),
1319 ac_to_float(ctx
, src2
),
1322 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1323 get_elem_bits(ctx
, result_type
));
1324 assert(length
< sizeof(name
));
1325 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1328 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1329 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1331 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1333 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1336 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1337 LLVMIntPredicate pred
,
1338 LLVMValueRef src0
, LLVMValueRef src1
)
1340 return LLVMBuildSelect(ctx
->builder
,
1341 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1346 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1349 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1350 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1353 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1356 LLVMValueRef cmp
, val
;
1358 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32_0
, "");
1359 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32_1
, src0
, "");
1360 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32_0
, "");
1361 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1365 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1368 LLVMValueRef cmp
, val
;
1370 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32_0
, "");
1371 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32_1
, src0
, "");
1372 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32_0
, "");
1373 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1377 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1380 const char *intr
= "llvm.floor.f32";
1381 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1382 LLVMValueRef params
[] = {
1385 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1386 ctx
->f32
, params
, 1,
1387 AC_FUNC_ATTR_READNONE
);
1388 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1391 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1393 LLVMValueRef src0
, LLVMValueRef src1
)
1395 LLVMTypeRef ret_type
;
1396 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1398 LLVMValueRef params
[] = { src0
, src1
};
1399 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1402 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1403 params
, 2, AC_FUNC_ATTR_READNONE
);
1405 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1406 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1410 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1413 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1416 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1419 src0
= ac_to_float(ctx
, src0
);
1420 return LLVMBuildSExt(ctx
->builder
,
1421 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1425 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1428 return LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1431 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1434 return LLVMBuildSExt(ctx
->builder
,
1435 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1439 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1442 LLVMValueRef result
;
1443 LLVMValueRef cond
= NULL
;
1445 src0
= ac_to_float(&ctx
->ac
, src0
);
1446 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->ac
.f16
, "");
1448 if (ctx
->options
->chip_class
>= VI
) {
1449 LLVMValueRef args
[2];
1450 /* Check if the result is a denormal - and flush to 0 if so. */
1452 args
[1] = LLVMConstInt(ctx
->ac
.i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1453 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->ac
.i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1456 /* need to convert back up to f32 */
1457 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->ac
.f32
, "");
1459 if (ctx
->options
->chip_class
>= VI
)
1460 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1463 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1464 * so compare the result and flush to 0 if it's smaller.
1466 LLVMValueRef temp
, cond2
;
1467 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1468 ctx
->ac
.f32
, result
);
1469 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1470 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, 0x38800000, false), ctx
->ac
.f32
, ""),
1472 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1473 temp
, ctx
->ac
.f32_0
, "");
1474 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1475 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1480 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1481 LLVMValueRef src0
, LLVMValueRef src1
)
1483 LLVMValueRef dst64
, result
;
1484 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1485 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1487 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1488 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1489 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1493 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1494 LLVMValueRef src0
, LLVMValueRef src1
)
1496 LLVMValueRef dst64
, result
;
1497 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1498 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1500 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1501 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1502 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1506 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1508 const LLVMValueRef srcs
[3])
1510 LLVMValueRef result
;
1511 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1513 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1514 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1518 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1519 LLVMValueRef src0
, LLVMValueRef src1
,
1520 LLVMValueRef src2
, LLVMValueRef src3
)
1522 LLVMValueRef bfi_args
[3], result
;
1524 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1525 LLVMBuildSub(ctx
->builder
,
1526 LLVMBuildShl(ctx
->builder
,
1531 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1534 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1537 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1538 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1540 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1541 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1542 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1544 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1548 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1551 LLVMValueRef comp
[2];
1553 src0
= ac_to_float(ctx
, src0
);
1554 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1555 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1557 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
1560 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1563 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1564 LLVMValueRef temps
[2], result
, val
;
1567 for (i
= 0; i
< 2; i
++) {
1568 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1569 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1570 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1571 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1574 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1576 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1581 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1587 LLVMValueRef result
;
1589 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1590 mask
= AC_TID_MASK_LEFT
;
1591 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1592 mask
= AC_TID_MASK_TOP
;
1594 mask
= AC_TID_MASK_TOP_LEFT
;
1596 /* for DDX we want to next X pixel, DDY next Y pixel. */
1597 if (op
== nir_op_fddx_fine
||
1598 op
== nir_op_fddx_coarse
||
1604 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1609 * this takes an I,J coordinate pair,
1610 * and works out the X and Y derivatives.
1611 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1613 static LLVMValueRef
emit_ddxy_interp(
1614 struct ac_nir_context
*ctx
,
1615 LLVMValueRef interp_ij
)
1617 LLVMValueRef result
[4], a
;
1620 for (i
= 0; i
< 2; i
++) {
1621 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1622 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1623 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1624 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1626 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1629 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1631 LLVMValueRef src
[4], result
= NULL
;
1632 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1633 unsigned src_components
;
1634 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1636 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1637 switch (instr
->op
) {
1643 case nir_op_pack_half_2x16
:
1646 case nir_op_unpack_half_2x16
:
1650 src_components
= num_components
;
1653 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1654 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1656 switch (instr
->op
) {
1662 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1663 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1666 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1669 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1672 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1675 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1676 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1677 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1680 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1681 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1682 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1685 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1688 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1691 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1694 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1697 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1698 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1699 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1700 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1701 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1702 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1703 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1706 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1707 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1708 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1711 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1714 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1717 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1720 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1721 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1722 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1725 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1726 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1727 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1730 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1731 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, src
[0]);
1734 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1737 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1740 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1743 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1744 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1745 LLVMTypeOf(src
[0]), ""),
1749 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1750 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1751 LLVMTypeOf(src
[0]), ""),
1755 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1756 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1757 LLVMTypeOf(src
[0]), ""),
1761 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1764 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1767 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1770 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1773 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1776 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1779 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1782 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1785 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1788 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1791 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1792 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1795 result
= emit_iabs(&ctx
->ac
, src
[0]);
1798 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1801 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1804 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1807 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1810 result
= emit_isign(&ctx
->ac
, src
[0]);
1813 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1814 result
= emit_fsign(&ctx
->ac
, src
[0]);
1817 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1818 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1821 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1822 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1825 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1826 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1828 case nir_op_fround_even
:
1829 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1830 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1833 result
= emit_ffract(&ctx
->ac
, src
[0]);
1836 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1837 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1840 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1841 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1844 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1845 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1848 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1849 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1852 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1853 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1856 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1857 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1858 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, result
);
1861 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1862 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1865 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1866 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1867 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1868 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1869 ac_to_float_type(&ctx
->ac
, def_type
),
1873 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1874 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1875 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1876 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1877 ac_to_float_type(&ctx
->ac
, def_type
),
1881 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1882 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1884 case nir_op_ibitfield_extract
:
1885 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1887 case nir_op_ubitfield_extract
:
1888 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1890 case nir_op_bitfield_insert
:
1891 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1893 case nir_op_bitfield_reverse
:
1894 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1896 case nir_op_bit_count
:
1897 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1902 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1903 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1904 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1908 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1909 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1913 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1914 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1918 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1919 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1923 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1924 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1927 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1928 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1931 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1935 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1936 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1937 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1939 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1943 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1944 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1945 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1947 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1950 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1952 case nir_op_find_lsb
:
1953 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1954 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1956 case nir_op_ufind_msb
:
1957 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1958 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1960 case nir_op_ifind_msb
:
1961 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1962 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1964 case nir_op_uadd_carry
:
1965 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1966 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1967 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1969 case nir_op_usub_borrow
:
1970 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1971 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1972 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1975 result
= emit_b2f(&ctx
->ac
, src
[0]);
1978 result
= emit_f2b(&ctx
->ac
, src
[0]);
1981 result
= emit_b2i(&ctx
->ac
, src
[0]);
1984 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1985 result
= emit_i2b(&ctx
->ac
, src
[0]);
1987 case nir_op_fquantize2f16
:
1988 result
= emit_f2f16(ctx
->nctx
, src
[0]);
1990 case nir_op_umul_high
:
1991 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1992 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1993 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1995 case nir_op_imul_high
:
1996 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1997 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1998 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
2000 case nir_op_pack_half_2x16
:
2001 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
2003 case nir_op_unpack_half_2x16
:
2004 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
2008 case nir_op_fddx_fine
:
2009 case nir_op_fddy_fine
:
2010 case nir_op_fddx_coarse
:
2011 case nir_op_fddy_coarse
:
2012 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
2015 case nir_op_unpack_64_2x32_split_x
: {
2016 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2017 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2020 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2025 case nir_op_unpack_64_2x32_split_y
: {
2026 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2027 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2030 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2035 case nir_op_pack_64_2x32_split
: {
2036 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
2037 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2038 src
[0], ctx
->ac
.i32_0
, "");
2039 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2040 src
[1], ctx
->ac
.i32_1
, "");
2041 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
2046 fprintf(stderr
, "Unknown NIR alu instr: ");
2047 nir_print_instr(&instr
->instr
, stderr
);
2048 fprintf(stderr
, "\n");
2053 assert(instr
->dest
.dest
.is_ssa
);
2054 result
= ac_to_integer(&ctx
->ac
, result
);
2055 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
2060 static void visit_load_const(struct ac_nir_context
*ctx
,
2061 const nir_load_const_instr
*instr
)
2063 LLVMValueRef values
[4], value
= NULL
;
2064 LLVMTypeRef element_type
=
2065 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
2067 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
2068 switch (instr
->def
.bit_size
) {
2070 values
[i
] = LLVMConstInt(element_type
,
2071 instr
->value
.u32
[i
], false);
2074 values
[i
] = LLVMConstInt(element_type
,
2075 instr
->value
.u64
[i
], false);
2079 "unsupported nir load_const bit_size: %d\n",
2080 instr
->def
.bit_size
);
2084 if (instr
->def
.num_components
> 1) {
2085 value
= LLVMConstVector(values
, instr
->def
.num_components
);
2089 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
2092 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
2095 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2096 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2097 LLVMPointerType(type
, addr_space
), "");
2101 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
2104 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2105 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
2108 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
2109 /* On VI, the descriptor contains the size in bytes,
2110 * but TXQ must return the size in elements.
2111 * The stride is always non-zero for resources using TXQ.
2113 LLVMValueRef stride
=
2114 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2116 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2117 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2118 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2119 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2121 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2127 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2130 static void build_int_type_name(
2132 char *buf
, unsigned bufsize
)
2134 assert(bufsize
>= 6);
2136 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2137 snprintf(buf
, bufsize
, "v%ui32",
2138 LLVMGetVectorSize(type
));
2143 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2144 struct ac_image_args
*args
,
2145 const nir_tex_instr
*instr
)
2147 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2148 LLVMValueRef coord
= args
->addr
;
2149 LLVMValueRef half_texel
[2];
2150 LLVMValueRef compare_cube_wa
= NULL
;
2151 LLVMValueRef result
;
2153 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2157 struct ac_image_args txq_args
= { 0 };
2159 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2160 txq_args
.opcode
= ac_image_get_resinfo
;
2161 txq_args
.dmask
= 0xf;
2162 txq_args
.addr
= ctx
->i32_0
;
2163 txq_args
.resource
= args
->resource
;
2164 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2166 for (c
= 0; c
< 2; c
++) {
2167 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2168 LLVMConstInt(ctx
->i32
, c
, false), "");
2169 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2170 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2171 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2172 LLVMConstReal(ctx
->f32
, -0.5), "");
2176 LLVMValueRef orig_coords
= args
->addr
;
2178 for (c
= 0; c
< 2; c
++) {
2180 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2181 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2182 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2183 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2184 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2185 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2190 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2191 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2192 * workaround by sampling using a scaled type and converting.
2193 * This is taken from amdgpu-pro shaders.
2195 /* NOTE this produces some ugly code compared to amdgpu-pro,
2196 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2197 * and then reads them back. -pro generates two selects,
2198 * one s_cmp for the descriptor rewriting
2199 * one v_cmp for the coordinate and result changes.
2201 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2202 LLVMValueRef tmp
, tmp2
;
2204 /* workaround 8/8/8/8 uint/sint cube gather bug */
2205 /* first detect it then change to a scaled read and f2i */
2206 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2209 /* extract the DATA_FORMAT */
2210 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2211 LLVMConstInt(ctx
->i32
, 6, false), false);
2213 /* is the DATA_FORMAT == 8_8_8_8 */
2214 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2216 if (stype
== GLSL_TYPE_UINT
)
2217 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2218 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2219 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2221 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2222 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2223 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2225 /* replace the NUM FORMAT in the descriptor */
2226 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2227 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2229 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2231 /* don't modify the coordinates for this case */
2232 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2235 result
= ac_build_image_opcode(ctx
, args
);
2237 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2238 LLVMValueRef tmp
, tmp2
;
2240 /* if the cube workaround is in place, f2i the result. */
2241 for (c
= 0; c
< 4; c
++) {
2242 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2243 if (stype
== GLSL_TYPE_UINT
)
2244 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2246 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2247 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2248 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2249 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2250 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2251 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2257 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2258 const nir_tex_instr
*instr
,
2260 struct ac_image_args
*args
)
2262 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2263 return ac_build_buffer_load_format(&ctx
->ac
,
2270 args
->opcode
= ac_image_sample
;
2271 args
->compare
= instr
->is_shadow
;
2273 switch (instr
->op
) {
2275 case nir_texop_txf_ms
:
2276 case nir_texop_samples_identical
:
2277 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2278 args
->compare
= false;
2279 args
->offset
= false;
2286 args
->level_zero
= true;
2291 case nir_texop_query_levels
:
2292 args
->opcode
= ac_image_get_resinfo
;
2295 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2296 args
->level_zero
= true;
2302 args
->opcode
= ac_image_gather4
;
2303 args
->level_zero
= true;
2306 args
->opcode
= ac_image_get_lod
;
2307 args
->compare
= false;
2308 args
->offset
= false;
2314 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2315 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2316 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2317 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2320 return ac_build_image_opcode(&ctx
->ac
, args
);
2323 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2324 nir_intrinsic_instr
*instr
)
2326 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2327 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2328 unsigned binding
= nir_intrinsic_binding(instr
);
2329 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2330 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2331 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2332 unsigned base_offset
= layout
->binding
[binding
].offset
;
2333 LLVMValueRef offset
, stride
;
2335 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2336 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2337 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2338 layout
->binding
[binding
].dynamic_offset_offset
;
2339 desc_ptr
= ctx
->push_constants
;
2340 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2341 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2343 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2345 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2346 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2347 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2349 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2350 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->ac
.v4i32
);
2351 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2356 static LLVMValueRef
visit_vulkan_resource_reindex(struct nir_to_llvm_context
*ctx
,
2357 nir_intrinsic_instr
*instr
)
2359 LLVMValueRef ptr
= get_src(ctx
->nir
, instr
->src
[0]);
2360 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[1]);
2362 LLVMValueRef result
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2363 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2367 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2368 nir_intrinsic_instr
*instr
)
2370 LLVMValueRef ptr
, addr
;
2372 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2373 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2375 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2376 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2378 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2381 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2382 const nir_intrinsic_instr
*instr
)
2384 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
2386 return get_buffer_size(ctx
, LLVMBuildLoad(ctx
->ac
.builder
, ptr
, ""), false);
2388 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2389 nir_intrinsic_instr
*instr
)
2391 const char *store_name
;
2392 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2393 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2394 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2395 int components_32bit
= elem_size_mult
* instr
->num_components
;
2396 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2397 LLVMValueRef base_data
, base_offset
;
2398 LLVMValueRef params
[6];
2400 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2401 get_src(ctx
, instr
->src
[1]), true);
2402 params
[2] = ctx
->ac
.i32_0
; /* vindex */
2403 params
[4] = ctx
->ac
.i1false
; /* glc */
2404 params
[5] = ctx
->ac
.i1false
; /* slc */
2406 if (components_32bit
> 1)
2407 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2409 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2410 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2411 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2413 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2417 LLVMValueRef offset
;
2419 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2421 /* Due to an LLVM limitation, split 3-element writes
2422 * into a 2-element and a 1-element write. */
2424 writemask
|= 1 << (start
+ 2);
2428 start
*= elem_size_mult
;
2429 count
*= elem_size_mult
;
2432 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2437 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2439 } else if (count
== 2) {
2440 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2441 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2442 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(ctx
->ac
.v2f32
), tmp
,
2445 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2446 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2447 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2449 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2453 if (get_llvm_num_components(base_data
) > 1)
2454 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2455 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2458 store_name
= "llvm.amdgcn.buffer.store.f32";
2461 offset
= base_offset
;
2463 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2467 ac_build_intrinsic(&ctx
->ac
, store_name
,
2468 ctx
->ac
.voidt
, params
, 6, 0);
2472 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2473 const nir_intrinsic_instr
*instr
)
2476 LLVMValueRef params
[6];
2479 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2480 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2482 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2483 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2484 get_src(ctx
, instr
->src
[0]),
2486 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
2487 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2488 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2490 switch (instr
->intrinsic
) {
2491 case nir_intrinsic_ssbo_atomic_add
:
2492 name
= "llvm.amdgcn.buffer.atomic.add";
2494 case nir_intrinsic_ssbo_atomic_imin
:
2495 name
= "llvm.amdgcn.buffer.atomic.smin";
2497 case nir_intrinsic_ssbo_atomic_umin
:
2498 name
= "llvm.amdgcn.buffer.atomic.umin";
2500 case nir_intrinsic_ssbo_atomic_imax
:
2501 name
= "llvm.amdgcn.buffer.atomic.smax";
2503 case nir_intrinsic_ssbo_atomic_umax
:
2504 name
= "llvm.amdgcn.buffer.atomic.umax";
2506 case nir_intrinsic_ssbo_atomic_and
:
2507 name
= "llvm.amdgcn.buffer.atomic.and";
2509 case nir_intrinsic_ssbo_atomic_or
:
2510 name
= "llvm.amdgcn.buffer.atomic.or";
2512 case nir_intrinsic_ssbo_atomic_xor
:
2513 name
= "llvm.amdgcn.buffer.atomic.xor";
2515 case nir_intrinsic_ssbo_atomic_exchange
:
2516 name
= "llvm.amdgcn.buffer.atomic.swap";
2518 case nir_intrinsic_ssbo_atomic_comp_swap
:
2519 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2525 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2528 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2529 const nir_intrinsic_instr
*instr
)
2531 LLVMValueRef results
[2];
2532 int load_components
;
2533 int num_components
= instr
->num_components
;
2534 if (instr
->dest
.ssa
.bit_size
== 64)
2535 num_components
*= 2;
2537 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2538 load_components
= MIN2(num_components
- i
, 4);
2539 const char *load_name
;
2540 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2541 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2542 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2544 if (load_components
== 3)
2545 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2546 else if (load_components
> 1)
2547 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2549 if (load_components
>= 3)
2550 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2551 else if (load_components
== 2)
2552 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2553 else if (load_components
== 1)
2554 load_name
= "llvm.amdgcn.buffer.load.f32";
2556 unreachable("unhandled number of components");
2558 LLVMValueRef params
[] = {
2559 ctx
->abi
->load_ssbo(ctx
->abi
,
2560 get_src(ctx
, instr
->src
[0]),
2568 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2573 LLVMValueRef ret
= results
[0];
2574 if (num_components
> 4 || num_components
== 3) {
2575 LLVMValueRef masks
[] = {
2576 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2577 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2578 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2579 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2582 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2583 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2584 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2587 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2588 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2591 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2592 const nir_intrinsic_instr
*instr
)
2594 LLVMValueRef results
[8], ret
;
2595 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2596 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2597 int num_components
= instr
->num_components
;
2599 if (ctx
->abi
->load_ubo
)
2600 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2602 if (instr
->dest
.ssa
.bit_size
== 64)
2603 num_components
*= 2;
2605 for (unsigned i
= 0; i
< num_components
; ++i
) {
2606 LLVMValueRef params
[] = {
2608 LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, 4 * i
, 0),
2611 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const.v4i32", ctx
->ac
.f32
,
2613 AC_FUNC_ATTR_READNONE
|
2614 AC_FUNC_ATTR_LEGACY
);
2618 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2619 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2620 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2624 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2625 bool vs_in
, unsigned *vertex_index_out
,
2626 LLVMValueRef
*vertex_index_ref
,
2627 unsigned *const_out
, LLVMValueRef
*indir_out
)
2629 unsigned const_offset
= 0;
2630 nir_deref
*tail
= &deref
->deref
;
2631 LLVMValueRef offset
= NULL
;
2633 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2635 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2636 if (vertex_index_out
)
2637 *vertex_index_out
= deref_array
->base_offset
;
2639 if (vertex_index_ref
) {
2640 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2641 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2642 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2644 *vertex_index_ref
= vtx
;
2648 if (deref
->var
->data
.compact
) {
2649 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2650 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2651 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2652 /* We always lower indirect dereferences for "compact" array vars. */
2653 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2655 const_offset
= deref_array
->base_offset
;
2659 while (tail
->child
!= NULL
) {
2660 const struct glsl_type
*parent_type
= tail
->type
;
2663 if (tail
->deref_type
== nir_deref_type_array
) {
2664 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2665 LLVMValueRef index
, stride
, local_offset
;
2666 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2668 const_offset
+= size
* deref_array
->base_offset
;
2669 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2672 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2673 index
= get_src(ctx
, deref_array
->indirect
);
2674 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2675 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2678 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2680 offset
= local_offset
;
2681 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2682 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2684 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2685 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2686 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2689 unreachable("unsupported deref type");
2693 if (const_offset
&& offset
)
2694 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2695 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2698 *const_out
= const_offset
;
2699 *indir_out
= offset
;
2703 /* The offchip buffer layout for TCS->TES is
2705 * - attribute 0 of patch 0 vertex 0
2706 * - attribute 0 of patch 0 vertex 1
2707 * - attribute 0 of patch 0 vertex 2
2709 * - attribute 0 of patch 1 vertex 0
2710 * - attribute 0 of patch 1 vertex 1
2712 * - attribute 1 of patch 0 vertex 0
2713 * - attribute 1 of patch 0 vertex 1
2715 * - per patch attribute 0 of patch 0
2716 * - per patch attribute 0 of patch 1
2719 * Note that every attribute has 4 components.
2721 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2722 LLVMValueRef vertex_index
,
2723 LLVMValueRef param_index
)
2725 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2726 LLVMValueRef param_stride
, constant16
;
2727 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2729 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2730 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2731 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2734 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2736 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2737 vertices_per_patch
, "");
2739 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2742 param_stride
= total_vertices
;
2744 base_addr
= rel_patch_id
;
2745 param_stride
= num_patches
;
2748 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2749 LLVMBuildMul(ctx
->builder
, param_index
,
2750 param_stride
, ""), "");
2752 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2754 if (!vertex_index
) {
2755 LLVMValueRef patch_data_offset
=
2756 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2758 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2759 patch_data_offset
, "");
2764 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2766 unsigned const_index
,
2768 LLVMValueRef vertex_index
,
2769 LLVMValueRef indir_index
)
2771 LLVMValueRef param_index
;
2774 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2777 if (const_index
&& !is_compact
)
2778 param
+= const_index
;
2779 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2781 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2785 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2786 bool is_patch
, uint32_t param
)
2790 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2792 ctx
->tess_outputs_written
|= (1ull << param
);
2796 get_dw_address(struct nir_to_llvm_context
*ctx
,
2797 LLVMValueRef dw_addr
,
2799 unsigned const_index
,
2800 bool compact_const_index
,
2801 LLVMValueRef vertex_index
,
2802 LLVMValueRef stride
,
2803 LLVMValueRef indir_index
)
2808 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2809 LLVMBuildMul(ctx
->builder
,
2815 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2816 LLVMBuildMul(ctx
->builder
, indir_index
,
2817 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2818 else if (const_index
&& !compact_const_index
)
2819 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2820 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2822 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2823 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2825 if (const_index
&& compact_const_index
)
2826 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2827 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2832 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2833 nir_intrinsic_instr
*instr
)
2835 LLVMValueRef dw_addr
, stride
;
2836 unsigned const_index
;
2837 LLVMValueRef vertex_index
;
2838 LLVMValueRef indir_index
;
2840 LLVMValueRef value
[4], result
;
2841 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2842 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2843 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2844 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2845 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2846 &const_index
, &indir_index
);
2848 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2849 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2850 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2853 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2854 for (unsigned i
= 0; i
< instr
->num_components
+ comp
; i
++) {
2855 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2856 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2859 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2860 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2865 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2866 nir_intrinsic_instr
*instr
)
2868 LLVMValueRef dw_addr
;
2869 LLVMValueRef stride
= NULL
;
2870 LLVMValueRef value
[4], result
;
2871 LLVMValueRef vertex_index
= NULL
;
2872 LLVMValueRef indir_index
= NULL
;
2873 unsigned const_index
= 0;
2875 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2876 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2877 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2878 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2879 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2880 &const_index
, &indir_index
);
2882 if (!instr
->variables
[0]->var
->data
.patch
) {
2883 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2884 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2886 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2889 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2892 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2893 for (unsigned i
= comp
; i
< instr
->num_components
+ comp
; i
++) {
2894 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2895 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2898 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2899 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2904 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2905 nir_intrinsic_instr
*instr
,
2909 LLVMValueRef dw_addr
;
2910 LLVMValueRef stride
= NULL
;
2911 LLVMValueRef buf_addr
= NULL
;
2912 LLVMValueRef vertex_index
= NULL
;
2913 LLVMValueRef indir_index
= NULL
;
2914 unsigned const_index
= 0;
2916 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2917 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2918 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2919 bool store_lds
= true;
2921 if (instr
->variables
[0]->var
->data
.patch
) {
2922 if (!(ctx
->tcs_patch_outputs_read
& (1U << (instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_PATCH0
))))
2925 if (!(ctx
->tcs_outputs_read
& (1ULL << instr
->variables
[0]->var
->data
.location
)))
2928 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2929 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2930 &const_index
, &indir_index
);
2932 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2933 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2934 is_compact
&& const_index
> 3) {
2939 if (!instr
->variables
[0]->var
->data
.patch
) {
2940 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2941 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2943 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2946 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2948 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2950 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2951 vertex_index
, indir_index
);
2953 bool is_tess_factor
= false;
2954 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2955 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2956 is_tess_factor
= true;
2958 unsigned base
= is_compact
? const_index
: 0;
2959 for (unsigned chan
= 0; chan
< 8; chan
++) {
2960 if (!(writemask
& (1 << chan
)))
2962 LLVMValueRef value
= llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2964 if (store_lds
|| is_tess_factor
)
2965 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
2967 if (!is_tess_factor
&& writemask
!= 0xF)
2968 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2969 buf_addr
, ctx
->oc_lds
,
2970 4 * (base
+ chan
), 1, 0, true, false);
2972 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2976 if (writemask
== 0xF) {
2977 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2978 buf_addr
, ctx
->oc_lds
,
2979 (base
* 4), 1, 0, true, false);
2984 load_tes_input(struct nir_to_llvm_context
*ctx
,
2985 const nir_intrinsic_instr
*instr
)
2987 LLVMValueRef buf_addr
;
2988 LLVMValueRef result
;
2989 LLVMValueRef vertex_index
= NULL
;
2990 LLVMValueRef indir_index
= NULL
;
2991 unsigned const_index
= 0;
2993 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2994 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2996 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2997 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2998 &const_index
, &indir_index
);
2999 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
3000 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
3001 is_compact
&& const_index
> 3) {
3006 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3007 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
3008 is_compact
, vertex_index
, indir_index
);
3010 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, comp
* 4, false);
3011 buf_addr
= LLVMBuildAdd(ctx
->builder
, buf_addr
, comp_offset
, "");
3013 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
3014 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
3015 result
= trim_vector(&ctx
->ac
, result
, instr
->num_components
);
3016 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
3021 load_gs_input(struct ac_shader_abi
*abi
,
3023 unsigned driver_location
,
3025 unsigned num_components
,
3026 unsigned vertex_index
,
3027 unsigned const_index
,
3030 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3031 LLVMValueRef vtx_offset
;
3032 LLVMValueRef args
[9];
3033 unsigned param
, vtx_offset_param
;
3034 LLVMValueRef value
[4], result
;
3036 vtx_offset_param
= vertex_index
;
3037 assert(vtx_offset_param
< 6);
3038 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
3039 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3041 param
= shader_io_get_unique_index(location
);
3043 for (unsigned i
= component
; i
< num_components
+ component
; i
++) {
3044 if (ctx
->ac
.chip_class
>= GFX9
) {
3045 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
3046 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
3047 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
3048 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
3050 args
[0] = ctx
->esgs_ring
;
3051 args
[1] = vtx_offset
;
3052 args
[2] = LLVMConstInt(ctx
->ac
.i32
, (param
* 4 + i
+ const_index
) * 256, false);
3053 args
[3] = ctx
->ac
.i32_0
;
3054 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
3055 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
3056 args
[6] = ctx
->ac
.i32_1
; /* GLC */
3057 args
[7] = ctx
->ac
.i32_0
; /* SLC */
3058 args
[8] = ctx
->ac
.i32_0
; /* TFE */
3060 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
3061 ctx
->ac
.i32
, args
, 9,
3062 AC_FUNC_ATTR_READONLY
|
3063 AC_FUNC_ATTR_LEGACY
);
3066 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
3072 build_gep_for_deref(struct ac_nir_context
*ctx
,
3073 nir_deref_var
*deref
)
3075 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
3076 assert(entry
->data
);
3077 LLVMValueRef val
= entry
->data
;
3078 nir_deref
*tail
= deref
->deref
.child
;
3079 while (tail
!= NULL
) {
3080 LLVMValueRef offset
;
3081 switch (tail
->deref_type
) {
3082 case nir_deref_type_array
: {
3083 nir_deref_array
*array
= nir_deref_as_array(tail
);
3084 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
3085 if (array
->deref_array_type
==
3086 nir_deref_array_type_indirect
) {
3087 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
3094 case nir_deref_type_struct
: {
3095 nir_deref_struct
*deref_struct
=
3096 nir_deref_as_struct(tail
);
3097 offset
= LLVMConstInt(ctx
->ac
.i32
,
3098 deref_struct
->index
, 0);
3102 unreachable("bad deref type");
3104 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3110 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3111 nir_intrinsic_instr
*instr
)
3113 LLVMValueRef values
[8];
3114 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3115 int ve
= instr
->dest
.ssa
.num_components
;
3116 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3117 LLVMValueRef indir_index
;
3119 unsigned const_index
;
3120 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3121 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3122 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3123 &const_index
, &indir_index
);
3125 if (instr
->dest
.ssa
.bit_size
== 64)
3128 switch (instr
->variables
[0]->var
->data
.mode
) {
3129 case nir_var_shader_in
:
3130 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3131 return load_tcs_input(ctx
->nctx
, instr
);
3132 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3133 return load_tes_input(ctx
->nctx
, instr
);
3134 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3135 LLVMValueRef indir_index
;
3136 unsigned const_index
, vertex_index
;
3137 get_deref_offset(ctx
, instr
->variables
[0],
3138 false, &vertex_index
, NULL
,
3139 &const_index
, &indir_index
);
3140 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
3141 instr
->variables
[0]->var
->data
.driver_location
,
3142 instr
->variables
[0]->var
->data
.location_frac
, ve
,
3143 vertex_index
, const_index
,
3144 nir2llvmtype(ctx
, instr
->variables
[0]->var
->type
));
3147 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3149 unsigned count
= glsl_count_attribute_slots(
3150 instr
->variables
[0]->var
->type
,
3151 ctx
->stage
== MESA_SHADER_VERTEX
);
3153 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3154 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3157 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3161 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* 4];
3165 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3167 unsigned count
= glsl_count_attribute_slots(
3168 instr
->variables
[0]->var
->type
, false);
3170 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3171 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3174 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3178 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
3182 case nir_var_shared
: {
3183 LLVMValueRef address
= build_gep_for_deref(ctx
,
3184 instr
->variables
[0]);
3185 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3186 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3187 get_def_type(ctx
, &instr
->dest
.ssa
),
3190 case nir_var_shader_out
:
3191 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3192 return load_tcs_output(ctx
->nctx
, instr
);
3194 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3196 unsigned count
= glsl_count_attribute_slots(
3197 instr
->variables
[0]->var
->type
, false);
3199 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3200 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3203 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3207 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3208 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
3214 unreachable("unhandle variable mode");
3216 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3217 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3221 visit_store_var(struct ac_nir_context
*ctx
,
3222 nir_intrinsic_instr
*instr
)
3224 LLVMValueRef temp_ptr
, value
;
3225 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3226 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3227 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3228 int writemask
= instr
->const_index
[0] << comp
;
3229 LLVMValueRef indir_index
;
3230 unsigned const_index
;
3231 get_deref_offset(ctx
, instr
->variables
[0], false,
3232 NULL
, NULL
, &const_index
, &indir_index
);
3234 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3235 int old_writemask
= writemask
;
3237 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3238 LLVMVectorType(ctx
->ac
.f32
, get_llvm_num_components(src
) * 2),
3242 for (unsigned chan
= 0; chan
< 4; chan
++) {
3243 if (old_writemask
& (1 << chan
))
3244 writemask
|= 3u << (2 * chan
);
3248 switch (instr
->variables
[0]->var
->data
.mode
) {
3249 case nir_var_shader_out
:
3251 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3252 store_tcs_output(ctx
->nctx
, instr
, src
, writemask
);
3256 for (unsigned chan
= 0; chan
< 8; chan
++) {
3258 if (!(writemask
& (1 << chan
)))
3261 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3263 if (instr
->variables
[0]->var
->data
.compact
)
3266 unsigned count
= glsl_count_attribute_slots(
3267 instr
->variables
[0]->var
->type
, false);
3269 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3270 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3271 stride
, true, true);
3273 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3274 value
, indir_index
, "");
3275 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3276 count
, stride
, tmp_vec
);
3279 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3281 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3286 for (unsigned chan
= 0; chan
< 8; chan
++) {
3287 if (!(writemask
& (1 << chan
)))
3290 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
3292 unsigned count
= glsl_count_attribute_slots(
3293 instr
->variables
[0]->var
->type
, false);
3295 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3296 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3299 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3300 value
, indir_index
, "");
3301 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3304 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3306 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3310 case nir_var_shared
: {
3311 int writemask
= instr
->const_index
[0];
3312 LLVMValueRef address
= build_gep_for_deref(ctx
,
3313 instr
->variables
[0]);
3314 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3315 unsigned components
=
3316 glsl_get_vector_elements(
3317 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3318 if (writemask
== (1 << components
) - 1) {
3319 val
= LLVMBuildBitCast(
3320 ctx
->ac
.builder
, val
,
3321 LLVMGetElementType(LLVMTypeOf(address
)), "");
3322 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3324 for (unsigned chan
= 0; chan
< 4; chan
++) {
3325 if (!(writemask
& (1 << chan
)))
3328 LLVMBuildStructGEP(ctx
->ac
.builder
,
3330 LLVMValueRef src
= llvm_extract_elem(&ctx
->ac
, val
,
3332 src
= LLVMBuildBitCast(
3333 ctx
->ac
.builder
, src
,
3334 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3335 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3345 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3348 case GLSL_SAMPLER_DIM_BUF
:
3350 case GLSL_SAMPLER_DIM_1D
:
3351 return array
? 2 : 1;
3352 case GLSL_SAMPLER_DIM_2D
:
3353 return array
? 3 : 2;
3354 case GLSL_SAMPLER_DIM_MS
:
3355 return array
? 4 : 3;
3356 case GLSL_SAMPLER_DIM_3D
:
3357 case GLSL_SAMPLER_DIM_CUBE
:
3359 case GLSL_SAMPLER_DIM_RECT
:
3360 case GLSL_SAMPLER_DIM_SUBPASS
:
3362 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3372 /* Adjust the sample index according to FMASK.
3374 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3375 * which is the identity mapping. Each nibble says which physical sample
3376 * should be fetched to get that sample.
3378 * For example, 0x11111100 means there are only 2 samples stored and
3379 * the second sample covers 3/4 of the pixel. When reading samples 0
3380 * and 1, return physical sample 0 (determined by the first two 0s
3381 * in FMASK), otherwise return physical sample 1.
3383 * The sample index should be adjusted as follows:
3384 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3386 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3387 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3388 LLVMValueRef coord_z
,
3389 LLVMValueRef sample_index
,
3390 LLVMValueRef fmask_desc_ptr
)
3392 LLVMValueRef fmask_load_address
[4];
3395 fmask_load_address
[0] = coord_x
;
3396 fmask_load_address
[1] = coord_y
;
3398 fmask_load_address
[2] = coord_z
;
3399 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3402 struct ac_image_args args
= {0};
3404 args
.opcode
= ac_image_load
;
3405 args
.da
= coord_z
? true : false;
3406 args
.resource
= fmask_desc_ptr
;
3408 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3410 res
= ac_build_image_opcode(ctx
, &args
);
3412 res
= ac_to_integer(ctx
, res
);
3413 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3414 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3416 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3420 LLVMValueRef sample_index4
=
3421 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3422 LLVMValueRef shifted_fmask
=
3423 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3424 LLVMValueRef final_sample
=
3425 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3427 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3428 * resource descriptor is 0 (invalid),
3430 LLVMValueRef fmask_desc
=
3431 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3434 LLVMValueRef fmask_word1
=
3435 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3438 LLVMValueRef word1_is_nonzero
=
3439 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3440 fmask_word1
, ctx
->i32_0
, "");
3442 /* Replace the MSAA sample index. */
3444 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3445 final_sample
, sample_index
, "");
3446 return sample_index
;
3449 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3450 const nir_intrinsic_instr
*instr
)
3452 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3453 if(instr
->variables
[0]->deref
.child
)
3454 type
= instr
->variables
[0]->deref
.child
->type
;
3456 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3457 LLVMValueRef coords
[4];
3458 LLVMValueRef masks
[] = {
3459 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3460 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3463 LLVMValueRef sample_index
= llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3466 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3467 bool is_array
= glsl_sampler_type_is_array(type
);
3468 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3469 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3470 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3471 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3472 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3473 count
= image_type_to_components_count(dim
, is_array
);
3476 LLVMValueRef fmask_load_address
[3];
3479 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3480 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3482 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3484 fmask_load_address
[2] = NULL
;
3486 for (chan
= 0; chan
< 2; ++chan
)
3487 fmask_load_address
[chan
] =
3488 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3489 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3490 ctx
->ac
.i32
, ""), "");
3491 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3493 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3494 fmask_load_address
[0],
3495 fmask_load_address
[1],
3496 fmask_load_address
[2],
3498 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3500 if (count
== 1 && !gfx9_1d
) {
3501 if (instr
->src
[0].ssa
->num_components
)
3502 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3509 for (chan
= 0; chan
< count
; ++chan
) {
3510 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3513 for (chan
= 0; chan
< 2; ++chan
)
3514 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3515 ctx
->ac
.i32
, ""), "");
3516 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3522 coords
[2] = coords
[1];
3523 coords
[1] = ctx
->ac
.i32_0
;
3525 coords
[1] = ctx
->ac
.i32_0
;
3530 coords
[count
] = sample_index
;
3535 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3538 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3543 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3544 const nir_intrinsic_instr
*instr
)
3546 LLVMValueRef params
[7];
3548 char intrinsic_name
[64];
3549 const nir_variable
*var
= instr
->variables
[0]->var
;
3550 const struct glsl_type
*type
= var
->type
;
3552 if(instr
->variables
[0]->deref
.child
)
3553 type
= instr
->variables
[0]->deref
.child
->type
;
3555 type
= glsl_without_array(type
);
3556 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3557 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3558 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3559 ctx
->ac
.i32_0
, ""); /* vindex */
3560 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3561 params
[3] = ctx
->ac
.i1false
; /* glc */
3562 params
[4] = ctx
->ac
.i1false
; /* slc */
3563 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3566 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3567 res
= ac_to_integer(&ctx
->ac
, res
);
3569 bool is_da
= glsl_sampler_type_is_array(type
) ||
3570 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3571 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3572 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3573 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3574 LLVMValueRef glc
= ctx
->ac
.i1false
;
3575 LLVMValueRef slc
= ctx
->ac
.i1false
;
3577 params
[0] = get_image_coords(ctx
, instr
);
3578 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3579 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3580 if (HAVE_LLVM
<= 0x0309) {
3581 params
[3] = ctx
->ac
.i1false
; /* r128 */
3586 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3593 ac_get_image_intr_name("llvm.amdgcn.image.load",
3594 ctx
->ac
.v4f32
, /* vdata */
3595 LLVMTypeOf(params
[0]), /* coords */
3596 LLVMTypeOf(params
[1]), /* rsrc */
3597 intrinsic_name
, sizeof(intrinsic_name
));
3599 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3600 params
, 7, AC_FUNC_ATTR_READONLY
);
3602 return ac_to_integer(&ctx
->ac
, res
);
3605 static void visit_image_store(struct ac_nir_context
*ctx
,
3606 nir_intrinsic_instr
*instr
)
3608 LLVMValueRef params
[8];
3609 char intrinsic_name
[64];
3610 const nir_variable
*var
= instr
->variables
[0]->var
;
3611 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3612 LLVMValueRef glc
= ctx
->ac
.i1false
;
3613 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3615 glc
= ctx
->ac
.i1true
;
3617 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3618 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3619 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3620 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3621 ctx
->ac
.i32_0
, ""); /* vindex */
3622 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3623 params
[4] = glc
; /* glc */
3624 params
[5] = ctx
->ac
.i1false
; /* slc */
3625 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3628 bool is_da
= glsl_sampler_type_is_array(type
) ||
3629 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3630 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3631 LLVMValueRef slc
= ctx
->ac
.i1false
;
3633 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3634 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3635 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3636 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3637 if (HAVE_LLVM
<= 0x0309) {
3638 params
[4] = ctx
->ac
.i1false
; /* r128 */
3643 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3650 ac_get_image_intr_name("llvm.amdgcn.image.store",
3651 LLVMTypeOf(params
[0]), /* vdata */
3652 LLVMTypeOf(params
[1]), /* coords */
3653 LLVMTypeOf(params
[2]), /* rsrc */
3654 intrinsic_name
, sizeof(intrinsic_name
));
3656 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3662 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3663 const nir_intrinsic_instr
*instr
)
3665 LLVMValueRef params
[7];
3666 int param_count
= 0;
3667 const nir_variable
*var
= instr
->variables
[0]->var
;
3669 const char *atomic_name
;
3670 char intrinsic_name
[41];
3671 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3672 MAYBE_UNUSED
int length
;
3674 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3676 switch (instr
->intrinsic
) {
3677 case nir_intrinsic_image_atomic_add
:
3678 atomic_name
= "add";
3680 case nir_intrinsic_image_atomic_min
:
3681 atomic_name
= is_unsigned
? "umin" : "smin";
3683 case nir_intrinsic_image_atomic_max
:
3684 atomic_name
= is_unsigned
? "umax" : "smax";
3686 case nir_intrinsic_image_atomic_and
:
3687 atomic_name
= "and";
3689 case nir_intrinsic_image_atomic_or
:
3692 case nir_intrinsic_image_atomic_xor
:
3693 atomic_name
= "xor";
3695 case nir_intrinsic_image_atomic_exchange
:
3696 atomic_name
= "swap";
3698 case nir_intrinsic_image_atomic_comp_swap
:
3699 atomic_name
= "cmpswap";
3705 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3706 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3707 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3709 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3710 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3712 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3713 ctx
->ac
.i32_0
, ""); /* vindex */
3714 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3715 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3717 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3718 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3720 char coords_type
[8];
3722 bool da
= glsl_sampler_type_is_array(type
) ||
3723 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3725 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3726 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3728 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3729 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3730 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3732 build_int_type_name(LLVMTypeOf(coords
),
3733 coords_type
, sizeof(coords_type
));
3735 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3736 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3739 assert(length
< sizeof(intrinsic_name
));
3740 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3743 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3744 const nir_intrinsic_instr
*instr
)
3747 const nir_variable
*var
= instr
->variables
[0]->var
;
3748 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3749 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3750 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3751 if(instr
->variables
[0]->deref
.child
)
3752 type
= instr
->variables
[0]->deref
.child
->type
;
3754 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3755 return get_buffer_size(ctx
,
3756 get_sampler_desc(ctx
, instr
->variables
[0],
3757 AC_DESC_BUFFER
, NULL
, true, false), true);
3759 struct ac_image_args args
= { 0 };
3763 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3764 args
.opcode
= ac_image_get_resinfo
;
3765 args
.addr
= ctx
->ac
.i32_0
;
3767 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3769 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3771 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3772 glsl_sampler_type_is_array(type
)) {
3773 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3774 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3775 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3776 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3778 if (ctx
->ac
.chip_class
>= GFX9
&&
3779 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3780 glsl_sampler_type_is_array(type
)) {
3781 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3782 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3789 #define NOOP_WAITCNT 0xf7f
3790 #define LGKM_CNT 0x07f
3791 #define VM_CNT 0xf70
3793 static void emit_membar(struct nir_to_llvm_context
*ctx
,
3794 const nir_intrinsic_instr
*instr
)
3796 unsigned waitcnt
= NOOP_WAITCNT
;
3798 switch (instr
->intrinsic
) {
3799 case nir_intrinsic_memory_barrier
:
3800 case nir_intrinsic_group_memory_barrier
:
3801 waitcnt
&= VM_CNT
& LGKM_CNT
;
3803 case nir_intrinsic_memory_barrier_atomic_counter
:
3804 case nir_intrinsic_memory_barrier_buffer
:
3805 case nir_intrinsic_memory_barrier_image
:
3808 case nir_intrinsic_memory_barrier_shared
:
3809 waitcnt
&= LGKM_CNT
;
3814 if (waitcnt
!= NOOP_WAITCNT
)
3815 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3818 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3820 /* SI only (thanks to a hw bug workaround):
3821 * The real barrier instruction isn’t needed, because an entire patch
3822 * always fits into a single wave.
3824 if (ctx
->options
->chip_class
== SI
&&
3825 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3826 ac_build_waitcnt(&ctx
->ac
, LGKM_CNT
& VM_CNT
);
3829 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3830 ctx
->ac
.voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3833 static void emit_discard_if(struct ac_nir_context
*ctx
,
3834 const nir_intrinsic_instr
*instr
)
3838 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3839 get_src(ctx
, instr
->src
[0]),
3841 ac_build_kill_if_false(&ctx
->ac
, cond
);
3845 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3847 LLVMValueRef result
;
3848 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3849 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3850 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3852 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3855 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3856 const nir_intrinsic_instr
*instr
)
3858 LLVMValueRef ptr
, result
;
3859 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3860 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3862 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3863 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3864 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3866 LLVMAtomicOrderingSequentiallyConsistent
,
3867 LLVMAtomicOrderingSequentiallyConsistent
,
3870 LLVMAtomicRMWBinOp op
;
3871 switch (instr
->intrinsic
) {
3872 case nir_intrinsic_var_atomic_add
:
3873 op
= LLVMAtomicRMWBinOpAdd
;
3875 case nir_intrinsic_var_atomic_umin
:
3876 op
= LLVMAtomicRMWBinOpUMin
;
3878 case nir_intrinsic_var_atomic_umax
:
3879 op
= LLVMAtomicRMWBinOpUMax
;
3881 case nir_intrinsic_var_atomic_imin
:
3882 op
= LLVMAtomicRMWBinOpMin
;
3884 case nir_intrinsic_var_atomic_imax
:
3885 op
= LLVMAtomicRMWBinOpMax
;
3887 case nir_intrinsic_var_atomic_and
:
3888 op
= LLVMAtomicRMWBinOpAnd
;
3890 case nir_intrinsic_var_atomic_or
:
3891 op
= LLVMAtomicRMWBinOpOr
;
3893 case nir_intrinsic_var_atomic_xor
:
3894 op
= LLVMAtomicRMWBinOpXor
;
3896 case nir_intrinsic_var_atomic_exchange
:
3897 op
= LLVMAtomicRMWBinOpXchg
;
3903 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3904 LLVMAtomicOrderingSequentiallyConsistent
,
3910 #define INTERP_CENTER 0
3911 #define INTERP_CENTROID 1
3912 #define INTERP_SAMPLE 2
3914 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3915 enum glsl_interp_mode interp
, unsigned location
)
3918 case INTERP_MODE_FLAT
:
3921 case INTERP_MODE_SMOOTH
:
3922 case INTERP_MODE_NONE
:
3923 if (location
== INTERP_CENTER
)
3924 return ctx
->persp_center
;
3925 else if (location
== INTERP_CENTROID
)
3926 return ctx
->persp_centroid
;
3927 else if (location
== INTERP_SAMPLE
)
3928 return ctx
->persp_sample
;
3930 case INTERP_MODE_NOPERSPECTIVE
:
3931 if (location
== INTERP_CENTER
)
3932 return ctx
->linear_center
;
3933 else if (location
== INTERP_CENTROID
)
3934 return ctx
->linear_centroid
;
3935 else if (location
== INTERP_SAMPLE
)
3936 return ctx
->linear_sample
;
3942 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3943 LLVMValueRef sample_id
)
3945 LLVMValueRef result
;
3946 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
3948 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3949 const_array(ctx
->ac
.v2f32
, 64), "");
3951 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3952 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
3957 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3959 LLVMValueRef values
[2];
3961 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3962 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3963 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3966 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3967 const nir_intrinsic_instr
*instr
)
3969 LLVMValueRef result
[4];
3970 LLVMValueRef interp_param
, attr_number
;
3973 LLVMValueRef src_c0
= NULL
;
3974 LLVMValueRef src_c1
= NULL
;
3975 LLVMValueRef src0
= NULL
;
3976 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3977 switch (instr
->intrinsic
) {
3978 case nir_intrinsic_interp_var_at_centroid
:
3979 location
= INTERP_CENTROID
;
3981 case nir_intrinsic_interp_var_at_sample
:
3982 case nir_intrinsic_interp_var_at_offset
:
3983 location
= INTERP_CENTER
;
3984 src0
= get_src(ctx
->nir
, instr
->src
[0]);
3990 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3991 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_0
, ""));
3992 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_1
, ""));
3993 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3994 LLVMValueRef sample_position
;
3995 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3997 /* fetch sample ID */
3998 sample_position
= load_sample_position(ctx
, src0
);
4000 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_0
, "");
4001 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
4002 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_1
, "");
4003 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
4005 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
4006 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
4008 if (location
== INTERP_CENTER
) {
4009 LLVMValueRef ij_out
[2];
4010 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
4013 * take the I then J parameters, and the DDX/Y for it, and
4014 * calculate the IJ inputs for the interpolator.
4015 * temp1 = ddx * offset/sample.x + I;
4016 * interp_param.I = ddy * offset/sample.y + temp1;
4017 * temp1 = ddx * offset/sample.x + J;
4018 * interp_param.J = ddy * offset/sample.y + temp1;
4020 for (unsigned i
= 0; i
< 2; i
++) {
4021 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
4022 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
4023 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
4024 ddxy_out
, ix_ll
, "");
4025 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
4026 ddxy_out
, iy_ll
, "");
4027 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
4028 interp_param
, ix_ll
, "");
4029 LLVMValueRef temp1
, temp2
;
4031 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
4034 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
4035 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
4037 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
4038 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
4040 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
4041 temp2
, ctx
->ac
.i32
, "");
4043 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
4047 for (chan
= 0; chan
< 4; chan
++) {
4048 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
4051 interp_param
= LLVMBuildBitCast(ctx
->builder
,
4052 interp_param
, ctx
->ac
.v2f32
, "");
4053 LLVMValueRef i
= LLVMBuildExtractElement(
4054 ctx
->builder
, interp_param
, ctx
->ac
.i32_0
, "");
4055 LLVMValueRef j
= LLVMBuildExtractElement(
4056 ctx
->builder
, interp_param
, ctx
->ac
.i32_1
, "");
4058 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4059 llvm_chan
, attr_number
,
4060 ctx
->prim_mask
, i
, j
);
4062 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4063 LLVMConstInt(ctx
->ac
.i32
, 2, false),
4064 llvm_chan
, attr_number
,
4068 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
4069 instr
->variables
[0]->var
->data
.location_frac
);
4073 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
4075 LLVMValueRef gs_next_vertex
;
4076 LLVMValueRef can_emit
;
4078 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4080 /* Write vertex attribute values to GSVS ring */
4081 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
4082 ctx
->gs_next_vertex
,
4085 /* If this thread has already emitted the declared maximum number of
4086 * vertices, kill it: excessive vertex emissions are not supposed to
4087 * have any effect, and GS threads have no externally observable
4088 * effects other than emitting vertices.
4090 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
4091 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
4092 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4094 /* loop num outputs */
4096 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4097 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
4102 if (!(ctx
->output_mask
& (1ull << i
)))
4105 if (i
== VARYING_SLOT_CLIP_DIST0
) {
4106 /* pack clip and cull into a single set of slots */
4107 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4111 for (unsigned j
= 0; j
< length
; j
++) {
4112 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
4114 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
4115 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
4116 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
4118 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
4120 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
4122 voffset
, ctx
->gs2vs_offset
, 0,
4128 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4130 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4132 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4136 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
4137 const nir_intrinsic_instr
*instr
)
4139 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4143 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
4144 const nir_intrinsic_instr
*instr
)
4146 LLVMValueRef coord
[4] = {
4153 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4154 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->ac
.f32_1
,
4155 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4157 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
4158 return LLVMBuildBitCast(ctx
->builder
, result
,
4159 get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
4162 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4163 nir_intrinsic_instr
*instr
)
4165 LLVMValueRef result
= NULL
;
4167 switch (instr
->intrinsic
) {
4168 case nir_intrinsic_load_work_group_id
: {
4169 LLVMValueRef values
[3];
4171 for (int i
= 0; i
< 3; i
++) {
4172 values
[i
] = ctx
->nctx
->workgroup_ids
[i
] ?
4173 ctx
->nctx
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
4176 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
4179 case nir_intrinsic_load_base_vertex
: {
4180 result
= ctx
->abi
->base_vertex
;
4183 case nir_intrinsic_load_vertex_id_zero_base
: {
4184 result
= ctx
->abi
->vertex_id
;
4187 case nir_intrinsic_load_local_invocation_id
: {
4188 result
= ctx
->nctx
->local_invocation_ids
;
4191 case nir_intrinsic_load_base_instance
:
4192 result
= ctx
->abi
->start_instance
;
4194 case nir_intrinsic_load_draw_id
:
4195 result
= ctx
->abi
->draw_id
;
4197 case nir_intrinsic_load_view_index
:
4198 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4200 case nir_intrinsic_load_invocation_id
:
4201 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4202 result
= unpack_param(&ctx
->ac
, ctx
->nctx
->tcs_rel_ids
, 8, 5);
4204 result
= ctx
->abi
->gs_invocation_id
;
4206 case nir_intrinsic_load_primitive_id
:
4207 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4209 ctx
->nctx
->shader_info
->gs
.uses_prim_id
= true;
4210 result
= ctx
->abi
->gs_prim_id
;
4211 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4212 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4213 result
= ctx
->nctx
->tcs_patch_id
;
4214 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4215 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4216 result
= ctx
->nctx
->tes_patch_id
;
4218 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4220 case nir_intrinsic_load_sample_id
:
4221 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4223 case nir_intrinsic_load_sample_pos
:
4224 result
= load_sample_pos(ctx
);
4226 case nir_intrinsic_load_sample_mask_in
:
4227 result
= ctx
->abi
->sample_coverage
;
4229 case nir_intrinsic_load_frag_coord
: {
4230 LLVMValueRef values
[4] = {
4231 ctx
->abi
->frag_pos
[0],
4232 ctx
->abi
->frag_pos
[1],
4233 ctx
->abi
->frag_pos
[2],
4234 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4236 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4239 case nir_intrinsic_load_front_face
:
4240 result
= ctx
->abi
->front_face
;
4242 case nir_intrinsic_load_instance_id
:
4243 result
= ctx
->abi
->instance_id
;
4245 case nir_intrinsic_load_num_work_groups
:
4246 result
= ctx
->nctx
->num_work_groups
;
4248 case nir_intrinsic_load_local_invocation_index
:
4249 result
= visit_load_local_invocation_index(ctx
->nctx
);
4251 case nir_intrinsic_load_push_constant
:
4252 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4254 case nir_intrinsic_vulkan_resource_index
:
4255 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4257 case nir_intrinsic_vulkan_resource_reindex
:
4258 result
= visit_vulkan_resource_reindex(ctx
->nctx
, instr
);
4260 case nir_intrinsic_store_ssbo
:
4261 visit_store_ssbo(ctx
, instr
);
4263 case nir_intrinsic_load_ssbo
:
4264 result
= visit_load_buffer(ctx
, instr
);
4266 case nir_intrinsic_ssbo_atomic_add
:
4267 case nir_intrinsic_ssbo_atomic_imin
:
4268 case nir_intrinsic_ssbo_atomic_umin
:
4269 case nir_intrinsic_ssbo_atomic_imax
:
4270 case nir_intrinsic_ssbo_atomic_umax
:
4271 case nir_intrinsic_ssbo_atomic_and
:
4272 case nir_intrinsic_ssbo_atomic_or
:
4273 case nir_intrinsic_ssbo_atomic_xor
:
4274 case nir_intrinsic_ssbo_atomic_exchange
:
4275 case nir_intrinsic_ssbo_atomic_comp_swap
:
4276 result
= visit_atomic_ssbo(ctx
, instr
);
4278 case nir_intrinsic_load_ubo
:
4279 result
= visit_load_ubo_buffer(ctx
, instr
);
4281 case nir_intrinsic_get_buffer_size
:
4282 result
= visit_get_buffer_size(ctx
, instr
);
4284 case nir_intrinsic_load_var
:
4285 result
= visit_load_var(ctx
, instr
);
4287 case nir_intrinsic_store_var
:
4288 visit_store_var(ctx
, instr
);
4290 case nir_intrinsic_image_load
:
4291 result
= visit_image_load(ctx
, instr
);
4293 case nir_intrinsic_image_store
:
4294 visit_image_store(ctx
, instr
);
4296 case nir_intrinsic_image_atomic_add
:
4297 case nir_intrinsic_image_atomic_min
:
4298 case nir_intrinsic_image_atomic_max
:
4299 case nir_intrinsic_image_atomic_and
:
4300 case nir_intrinsic_image_atomic_or
:
4301 case nir_intrinsic_image_atomic_xor
:
4302 case nir_intrinsic_image_atomic_exchange
:
4303 case nir_intrinsic_image_atomic_comp_swap
:
4304 result
= visit_image_atomic(ctx
, instr
);
4306 case nir_intrinsic_image_size
:
4307 result
= visit_image_size(ctx
, instr
);
4309 case nir_intrinsic_discard
:
4310 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4311 LLVMVoidTypeInContext(ctx
->ac
.context
),
4312 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4314 case nir_intrinsic_discard_if
:
4315 emit_discard_if(ctx
, instr
);
4317 case nir_intrinsic_memory_barrier
:
4318 case nir_intrinsic_group_memory_barrier
:
4319 case nir_intrinsic_memory_barrier_atomic_counter
:
4320 case nir_intrinsic_memory_barrier_buffer
:
4321 case nir_intrinsic_memory_barrier_image
:
4322 case nir_intrinsic_memory_barrier_shared
:
4323 emit_membar(ctx
->nctx
, instr
);
4325 case nir_intrinsic_barrier
:
4326 emit_barrier(ctx
->nctx
);
4328 case nir_intrinsic_var_atomic_add
:
4329 case nir_intrinsic_var_atomic_imin
:
4330 case nir_intrinsic_var_atomic_umin
:
4331 case nir_intrinsic_var_atomic_imax
:
4332 case nir_intrinsic_var_atomic_umax
:
4333 case nir_intrinsic_var_atomic_and
:
4334 case nir_intrinsic_var_atomic_or
:
4335 case nir_intrinsic_var_atomic_xor
:
4336 case nir_intrinsic_var_atomic_exchange
:
4337 case nir_intrinsic_var_atomic_comp_swap
:
4338 result
= visit_var_atomic(ctx
->nctx
, instr
);
4340 case nir_intrinsic_interp_var_at_centroid
:
4341 case nir_intrinsic_interp_var_at_sample
:
4342 case nir_intrinsic_interp_var_at_offset
:
4343 result
= visit_interp(ctx
->nctx
, instr
);
4345 case nir_intrinsic_emit_vertex
:
4346 assert(instr
->const_index
[0] == 0);
4347 ctx
->abi
->emit_vertex(ctx
->abi
, 0, ctx
->outputs
);
4349 case nir_intrinsic_end_primitive
:
4350 visit_end_primitive(ctx
->nctx
, instr
);
4352 case nir_intrinsic_load_tess_coord
:
4353 result
= visit_load_tess_coord(ctx
->nctx
, instr
);
4355 case nir_intrinsic_load_patch_vertices_in
:
4356 result
= LLVMConstInt(ctx
->ac
.i32
, ctx
->nctx
->options
->key
.tcs
.input_vertices
, false);
4359 fprintf(stderr
, "Unknown intrinsic: ");
4360 nir_print_instr(&instr
->instr
, stderr
);
4361 fprintf(stderr
, "\n");
4365 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4369 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4370 LLVMValueRef buffer_ptr
, bool write
)
4372 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4374 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4375 ctx
->shader_info
->fs
.writes_memory
= true;
4377 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4380 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef buffer_ptr
)
4382 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4384 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4387 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4388 unsigned descriptor_set
,
4389 unsigned base_index
,
4390 unsigned constant_index
,
4392 enum ac_descriptor_type desc_type
,
4393 bool image
, bool write
)
4395 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4396 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4397 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4398 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4399 unsigned offset
= binding
->offset
;
4400 unsigned stride
= binding
->size
;
4402 LLVMBuilderRef builder
= ctx
->builder
;
4405 assert(base_index
< layout
->binding_count
);
4407 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4408 ctx
->shader_info
->fs
.writes_memory
= true;
4410 switch (desc_type
) {
4412 type
= ctx
->ac
.v8i32
;
4416 type
= ctx
->ac
.v8i32
;
4420 case AC_DESC_SAMPLER
:
4421 type
= ctx
->ac
.v4i32
;
4422 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4427 case AC_DESC_BUFFER
:
4428 type
= ctx
->ac
.v4i32
;
4432 unreachable("invalid desc_type\n");
4435 offset
+= constant_index
* stride
;
4437 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4438 (!index
|| binding
->immutable_samplers_equal
)) {
4439 if (binding
->immutable_samplers_equal
)
4442 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4444 LLVMValueRef constants
[] = {
4445 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4446 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4447 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4448 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4450 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4453 assert(stride
% type_size
== 0);
4456 index
= ctx
->ac
.i32_0
;
4458 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4460 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4461 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4463 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4466 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4467 const nir_deref_var
*deref
,
4468 enum ac_descriptor_type desc_type
,
4469 const nir_tex_instr
*tex_instr
,
4470 bool image
, bool write
)
4472 LLVMValueRef index
= NULL
;
4473 unsigned constant_index
= 0;
4474 unsigned descriptor_set
;
4475 unsigned base_index
;
4478 assert(tex_instr
&& !image
);
4480 base_index
= tex_instr
->sampler_index
;
4482 const nir_deref
*tail
= &deref
->deref
;
4483 while (tail
->child
) {
4484 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4485 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4490 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4492 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4493 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4495 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4496 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4501 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4504 constant_index
+= child
->base_offset
* array_size
;
4506 tail
= &child
->deref
;
4508 descriptor_set
= deref
->var
->data
.descriptor_set
;
4509 base_index
= deref
->var
->data
.binding
;
4512 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4515 constant_index
, index
,
4516 desc_type
, image
, write
);
4519 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4520 struct ac_image_args
*args
,
4521 const nir_tex_instr
*instr
,
4523 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4524 LLVMValueRef
*param
, unsigned count
,
4527 unsigned is_rect
= 0;
4528 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4530 if (op
== nir_texop_lod
)
4532 /* Pad to power of two vector */
4533 while (count
< util_next_power_of_two(count
))
4534 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4537 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4539 args
->addr
= param
[0];
4541 args
->resource
= res_ptr
;
4542 args
->sampler
= samp_ptr
;
4544 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4545 args
->addr
= param
[0];
4549 args
->dmask
= dmask
;
4550 args
->unorm
= is_rect
;
4554 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4557 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4558 * filtering manually. The driver sets img7 to a mask clearing
4559 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4560 * s_and_b32 samp0, samp0, img7
4563 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4565 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4566 LLVMValueRef res
, LLVMValueRef samp
)
4568 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4569 LLVMValueRef img7
, samp0
;
4571 if (ctx
->ac
.chip_class
>= VI
)
4574 img7
= LLVMBuildExtractElement(builder
, res
,
4575 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4576 samp0
= LLVMBuildExtractElement(builder
, samp
,
4577 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4578 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4579 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4580 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4583 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4584 nir_tex_instr
*instr
,
4585 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4586 LLVMValueRef
*fmask_ptr
)
4588 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4589 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4591 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4594 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4596 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4597 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4598 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4600 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4601 instr
->op
== nir_texop_samples_identical
))
4602 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4605 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4608 coord
= ac_to_float(ctx
, coord
);
4609 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4610 coord
= ac_to_integer(ctx
, coord
);
4614 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4616 LLVMValueRef result
= NULL
;
4617 struct ac_image_args args
= { 0 };
4618 unsigned dmask
= 0xf;
4619 LLVMValueRef address
[16];
4620 LLVMValueRef coords
[5];
4621 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4622 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4623 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4624 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4625 LLVMValueRef derivs
[6];
4626 unsigned chan
, count
= 0;
4627 unsigned const_src
= 0, num_deriv_comp
= 0;
4628 bool lod_is_zero
= false;
4630 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4632 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4633 switch (instr
->src
[i
].src_type
) {
4634 case nir_tex_src_coord
:
4635 coord
= get_src(ctx
, instr
->src
[i
].src
);
4637 case nir_tex_src_projector
:
4639 case nir_tex_src_comparator
:
4640 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4642 case nir_tex_src_offset
:
4643 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4646 case nir_tex_src_bias
:
4647 bias
= get_src(ctx
, instr
->src
[i
].src
);
4649 case nir_tex_src_lod
: {
4650 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4652 if (val
&& val
->i32
[0] == 0)
4654 lod
= get_src(ctx
, instr
->src
[i
].src
);
4657 case nir_tex_src_ms_index
:
4658 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4660 case nir_tex_src_ms_mcs
:
4662 case nir_tex_src_ddx
:
4663 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4664 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4666 case nir_tex_src_ddy
:
4667 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4669 case nir_tex_src_texture_offset
:
4670 case nir_tex_src_sampler_offset
:
4671 case nir_tex_src_plane
:
4677 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4678 result
= get_buffer_size(ctx
, res_ptr
, true);
4682 if (instr
->op
== nir_texop_texture_samples
) {
4683 LLVMValueRef res
, samples
, is_msaa
;
4684 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4685 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4686 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4687 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4688 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4689 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4690 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4691 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4692 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4694 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4695 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4696 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4697 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4698 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4700 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4707 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4708 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4710 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4711 LLVMValueRef offset
[3], pack
;
4712 for (chan
= 0; chan
< 3; ++chan
)
4713 offset
[chan
] = ctx
->ac
.i32_0
;
4716 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4717 offset
[chan
] = llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4718 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4719 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4721 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4722 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4724 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4725 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4726 address
[count
++] = pack
;
4729 /* pack LOD bias value */
4730 if (instr
->op
== nir_texop_txb
&& bias
) {
4731 address
[count
++] = bias
;
4734 /* Pack depth comparison value */
4735 if (instr
->is_shadow
&& comparator
) {
4736 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4737 llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4739 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
4740 * so the depth comparison value isn't clamped for Z16 and
4741 * Z24 anymore. Do it manually here.
4743 * It's unnecessary if the original texture format was
4744 * Z32_FLOAT, but we don't know that here.
4746 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
4747 z
= ac_build_clamp(&ctx
->ac
, z
);
4749 address
[count
++] = z
;
4752 /* pack derivatives */
4754 int num_src_deriv_channels
, num_dest_deriv_channels
;
4755 switch (instr
->sampler_dim
) {
4756 case GLSL_SAMPLER_DIM_3D
:
4757 case GLSL_SAMPLER_DIM_CUBE
:
4759 num_src_deriv_channels
= 3;
4760 num_dest_deriv_channels
= 3;
4762 case GLSL_SAMPLER_DIM_2D
:
4764 num_src_deriv_channels
= 2;
4765 num_dest_deriv_channels
= 2;
4768 case GLSL_SAMPLER_DIM_1D
:
4769 num_src_deriv_channels
= 1;
4770 if (ctx
->ac
.chip_class
>= GFX9
) {
4771 num_dest_deriv_channels
= 2;
4774 num_dest_deriv_channels
= 1;
4780 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4781 derivs
[i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4782 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4784 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4785 derivs
[i
] = ctx
->ac
.f32_0
;
4786 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4790 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4791 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4792 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4793 if (instr
->coord_components
== 3)
4794 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4795 ac_prepare_cube_coords(&ctx
->ac
,
4796 instr
->op
== nir_texop_txd
, instr
->is_array
,
4797 instr
->op
== nir_texop_lod
, coords
, derivs
);
4803 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4804 address
[count
++] = derivs
[i
];
4807 /* Pack texture coordinates */
4809 address
[count
++] = coords
[0];
4810 if (instr
->coord_components
> 1) {
4811 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4812 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4814 address
[count
++] = coords
[1];
4816 if (instr
->coord_components
> 2) {
4817 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4818 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4819 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4820 instr
->op
!= nir_texop_txf
) {
4821 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4823 address
[count
++] = coords
[2];
4826 if (ctx
->ac
.chip_class
>= GFX9
) {
4827 LLVMValueRef filler
;
4828 if (instr
->op
== nir_texop_txf
)
4829 filler
= ctx
->ac
.i32_0
;
4831 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4833 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4834 /* No nir_texop_lod, because it does not take a slice
4835 * even with array textures. */
4836 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
4837 address
[count
] = address
[count
- 1];
4838 address
[count
- 1] = filler
;
4841 address
[count
++] = filler
;
4847 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4848 instr
->op
== nir_texop_txf
)) {
4849 address
[count
++] = lod
;
4850 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4851 address
[count
++] = sample_index
;
4852 } else if(instr
->op
== nir_texop_txs
) {
4855 address
[count
++] = lod
;
4857 address
[count
++] = ctx
->ac
.i32_0
;
4860 for (chan
= 0; chan
< count
; chan
++) {
4861 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4862 address
[chan
], ctx
->ac
.i32
, "");
4865 if (instr
->op
== nir_texop_samples_identical
) {
4866 LLVMValueRef txf_address
[4];
4867 struct ac_image_args txf_args
= { 0 };
4868 unsigned txf_count
= count
;
4869 memcpy(txf_address
, address
, sizeof(txf_address
));
4871 if (!instr
->is_array
)
4872 txf_address
[2] = ctx
->ac
.i32_0
;
4873 txf_address
[3] = ctx
->ac
.i32_0
;
4875 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4877 txf_address
, txf_count
, 0xf);
4879 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4881 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4882 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4886 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4887 instr
->op
!= nir_texop_txs
) {
4888 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4889 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4892 instr
->is_array
? address
[2] : NULL
,
4893 address
[sample_chan
],
4897 if (offsets
&& instr
->op
== nir_texop_txf
) {
4898 nir_const_value
*const_offset
=
4899 nir_src_as_const_value(instr
->src
[const_src
].src
);
4900 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4901 assert(const_offset
);
4902 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4903 if (num_offsets
> 2)
4904 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4905 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4906 if (num_offsets
> 1)
4907 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4908 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4909 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4910 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4914 /* TODO TG4 support */
4915 if (instr
->op
== nir_texop_tg4
) {
4916 if (instr
->is_shadow
)
4919 dmask
= 1 << instr
->component
;
4921 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4922 res_ptr
, samp_ptr
, address
, count
, dmask
);
4924 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4926 if (instr
->op
== nir_texop_query_levels
)
4927 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4928 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4929 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4930 instr
->op
!= nir_texop_tg4
)
4931 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4932 else if (instr
->op
== nir_texop_txs
&&
4933 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4935 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4936 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4937 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4938 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4939 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4940 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4941 instr
->op
== nir_texop_txs
&&
4942 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4944 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4945 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4946 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4948 } else if (instr
->dest
.ssa
.num_components
!= 4)
4949 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4953 assert(instr
->dest
.is_ssa
);
4954 result
= ac_to_integer(&ctx
->ac
, result
);
4955 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4960 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4962 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4963 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4965 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4966 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4969 static void visit_post_phi(struct ac_nir_context
*ctx
,
4970 nir_phi_instr
*instr
,
4971 LLVMValueRef llvm_phi
)
4973 nir_foreach_phi_src(src
, instr
) {
4974 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4975 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4977 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4981 static void phi_post_pass(struct ac_nir_context
*ctx
)
4983 struct hash_entry
*entry
;
4984 hash_table_foreach(ctx
->phis
, entry
) {
4985 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4986 (LLVMValueRef
)entry
->data
);
4991 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4992 const nir_ssa_undef_instr
*instr
)
4994 unsigned num_components
= instr
->def
.num_components
;
4997 if (num_components
== 1)
4998 undef
= LLVMGetUndef(ctx
->ac
.i32
);
5000 undef
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, num_components
));
5002 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
5005 static void visit_jump(struct ac_nir_context
*ctx
,
5006 const nir_jump_instr
*instr
)
5008 switch (instr
->type
) {
5009 case nir_jump_break
:
5010 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
5011 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5013 case nir_jump_continue
:
5014 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5015 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5018 fprintf(stderr
, "Unknown NIR jump instr: ");
5019 nir_print_instr(&instr
->instr
, stderr
);
5020 fprintf(stderr
, "\n");
5025 static void visit_cf_list(struct ac_nir_context
*ctx
,
5026 struct exec_list
*list
);
5028 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5030 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
5031 nir_foreach_instr(instr
, block
)
5033 switch (instr
->type
) {
5034 case nir_instr_type_alu
:
5035 visit_alu(ctx
, nir_instr_as_alu(instr
));
5037 case nir_instr_type_load_const
:
5038 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5040 case nir_instr_type_intrinsic
:
5041 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5043 case nir_instr_type_tex
:
5044 visit_tex(ctx
, nir_instr_as_tex(instr
));
5046 case nir_instr_type_phi
:
5047 visit_phi(ctx
, nir_instr_as_phi(instr
));
5049 case nir_instr_type_ssa_undef
:
5050 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5052 case nir_instr_type_jump
:
5053 visit_jump(ctx
, nir_instr_as_jump(instr
));
5056 fprintf(stderr
, "Unknown NIR instr type: ");
5057 nir_print_instr(instr
, stderr
);
5058 fprintf(stderr
, "\n");
5063 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
5066 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5068 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5070 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5071 LLVMBasicBlockRef merge_block
=
5072 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5073 LLVMBasicBlockRef if_block
=
5074 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5075 LLVMBasicBlockRef else_block
= merge_block
;
5076 if (!exec_list_is_empty(&if_stmt
->else_list
))
5077 else_block
= LLVMAppendBasicBlockInContext(
5078 ctx
->ac
.context
, fn
, "");
5080 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
5082 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
5084 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
5085 visit_cf_list(ctx
, &if_stmt
->then_list
);
5086 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5087 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5089 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5090 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
5091 visit_cf_list(ctx
, &if_stmt
->else_list
);
5092 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5093 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5096 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
5099 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5101 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5102 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
5103 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
5105 ctx
->continue_block
=
5106 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5108 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5110 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5111 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
5112 visit_cf_list(ctx
, &loop
->body
);
5114 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5115 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5116 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
5118 ctx
->continue_block
= continue_parent
;
5119 ctx
->break_block
= break_parent
;
5122 static void visit_cf_list(struct ac_nir_context
*ctx
,
5123 struct exec_list
*list
)
5125 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5127 switch (node
->type
) {
5128 case nir_cf_node_block
:
5129 visit_block(ctx
, nir_cf_node_as_block(node
));
5132 case nir_cf_node_if
:
5133 visit_if(ctx
, nir_cf_node_as_if(node
));
5136 case nir_cf_node_loop
:
5137 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5147 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
5148 struct nir_variable
*variable
)
5150 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5151 LLVMValueRef t_offset
;
5152 LLVMValueRef t_list
;
5154 LLVMValueRef buffer_index
;
5155 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5156 int idx
= variable
->data
.location
;
5157 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5159 variable
->data
.driver_location
= idx
* 4;
5161 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
5162 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5163 ctx
->abi
.start_instance
, "");
5164 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
5165 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5167 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5168 ctx
->abi
.base_vertex
, "");
5170 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5171 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5173 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5175 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5180 for (unsigned chan
= 0; chan
< 4; chan
++) {
5181 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5182 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5183 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5184 input
, llvm_chan
, ""));
5189 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5191 LLVMValueRef interp_param
,
5192 LLVMValueRef prim_mask
,
5193 LLVMValueRef result
[4])
5195 LLVMValueRef attr_number
;
5198 bool interp
= interp_param
!= NULL
;
5200 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5202 /* fs.constant returns the param from the middle vertex, so it's not
5203 * really useful for flat shading. It's meant to be used for custom
5204 * interpolation (but the intrinsic can't fetch from the other two
5207 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5208 * to do the right thing. The only reason we use fs.constant is that
5209 * fs.interp cannot be used on integers, because they can be equal
5213 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5216 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5218 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5222 for (chan
= 0; chan
< 4; chan
++) {
5223 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5226 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5231 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5232 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5241 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5242 struct nir_variable
*variable
)
5244 int idx
= variable
->data
.location
;
5245 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5246 LLVMValueRef interp
;
5248 variable
->data
.driver_location
= idx
* 4;
5249 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5251 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5252 unsigned interp_type
;
5253 if (variable
->data
.sample
) {
5254 interp_type
= INTERP_SAMPLE
;
5255 ctx
->shader_info
->info
.ps
.force_persample
= true;
5256 } else if (variable
->data
.centroid
)
5257 interp_type
= INTERP_CENTROID
;
5259 interp_type
= INTERP_CENTER
;
5261 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5265 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5266 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5271 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5272 struct nir_shader
*nir
) {
5273 nir_foreach_variable(variable
, &nir
->inputs
)
5274 handle_vs_input_decl(ctx
, variable
);
5278 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5279 struct nir_shader
*nir
)
5281 if (!ctx
->options
->key
.fs
.multisample
)
5284 bool uses_center
= false;
5285 bool uses_centroid
= false;
5286 nir_foreach_variable(variable
, &nir
->inputs
) {
5287 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5288 variable
->data
.sample
)
5291 if (variable
->data
.centroid
)
5292 uses_centroid
= true;
5297 if (uses_center
&& uses_centroid
) {
5298 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5299 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5300 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5305 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5306 struct nir_shader
*nir
)
5308 prepare_interp_optimize(ctx
, nir
);
5310 nir_foreach_variable(variable
, &nir
->inputs
)
5311 handle_fs_input_decl(ctx
, variable
);
5315 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5316 ctx
->shader_info
->info
.needs_multiview_view_index
)
5317 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5319 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5320 LLVMValueRef interp_param
;
5321 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5323 if (!(ctx
->input_mask
& (1ull << i
)))
5326 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5327 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5328 interp_param
= *inputs
;
5329 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5333 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5335 } else if (i
== VARYING_SLOT_POS
) {
5336 for(int i
= 0; i
< 3; ++i
)
5337 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5339 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5340 ctx
->abi
.frag_pos
[3]);
5343 ctx
->shader_info
->fs
.num_interp
= index
;
5344 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5345 ctx
->shader_info
->fs
.has_pcoord
= true;
5346 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5347 ctx
->shader_info
->fs
.prim_id_input
= true;
5348 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5349 ctx
->shader_info
->fs
.layer_input
= true;
5350 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5352 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5353 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5357 ac_build_alloca(struct ac_llvm_context
*ac
,
5361 LLVMBuilderRef builder
= ac
->builder
;
5362 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5363 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5364 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5365 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5366 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5370 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5372 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5375 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5376 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5378 LLVMDisposeBuilder(first_builder
);
5383 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5387 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5388 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5393 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5394 struct nir_variable
*variable
,
5395 struct nir_shader
*shader
,
5396 gl_shader_stage stage
)
5398 int idx
= variable
->data
.location
+ variable
->data
.index
;
5399 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5400 uint64_t mask_attribs
;
5402 variable
->data
.driver_location
= idx
* 4;
5404 /* tess ctrl has it's own load/store paths for outputs */
5405 if (stage
== MESA_SHADER_TESS_CTRL
)
5408 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5409 if (stage
== MESA_SHADER_VERTEX
||
5410 stage
== MESA_SHADER_TESS_EVAL
||
5411 stage
== MESA_SHADER_GEOMETRY
) {
5412 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5413 int length
= shader
->info
.clip_distance_array_size
+
5414 shader
->info
.cull_distance_array_size
;
5415 if (stage
== MESA_SHADER_VERTEX
) {
5416 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5417 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5419 if (stage
== MESA_SHADER_TESS_EVAL
) {
5420 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5421 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5428 mask_attribs
= 1ull << idx
;
5432 ctx
->output_mask
|= mask_attribs
;
5436 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5437 struct nir_shader
*nir
,
5438 struct nir_variable
*variable
)
5440 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5441 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5443 /* tess ctrl has it's own load/store paths for outputs */
5444 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5447 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5448 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5449 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5450 int idx
= variable
->data
.location
+ variable
->data
.index
;
5451 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5452 int length
= nir
->info
.clip_distance_array_size
+
5453 nir
->info
.cull_distance_array_size
;
5462 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5463 for (unsigned chan
= 0; chan
< 4; chan
++) {
5464 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5465 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5471 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5472 enum glsl_base_type type
)
5476 case GLSL_TYPE_UINT
:
5477 case GLSL_TYPE_BOOL
:
5478 case GLSL_TYPE_SUBROUTINE
:
5480 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5482 case GLSL_TYPE_INT64
:
5483 case GLSL_TYPE_UINT64
:
5485 case GLSL_TYPE_DOUBLE
:
5488 unreachable("unknown GLSL type");
5493 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5494 const struct glsl_type
*type
)
5496 if (glsl_type_is_scalar(type
)) {
5497 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5500 if (glsl_type_is_vector(type
)) {
5501 return LLVMVectorType(
5502 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5503 glsl_get_vector_elements(type
));
5506 if (glsl_type_is_matrix(type
)) {
5507 return LLVMArrayType(
5508 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5509 glsl_get_matrix_columns(type
));
5512 if (glsl_type_is_array(type
)) {
5513 return LLVMArrayType(
5514 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5515 glsl_get_length(type
));
5518 assert(glsl_type_is_struct(type
));
5520 LLVMTypeRef member_types
[glsl_get_length(type
)];
5522 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5524 glsl_to_llvm_type(ctx
,
5525 glsl_get_struct_field(type
, i
));
5528 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5529 glsl_get_length(type
), false);
5533 setup_locals(struct ac_nir_context
*ctx
,
5534 struct nir_function
*func
)
5537 ctx
->num_locals
= 0;
5538 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5539 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5540 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5541 ctx
->num_locals
+= attrib_count
;
5543 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5547 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5548 for (j
= 0; j
< 4; j
++) {
5549 ctx
->locals
[i
* 4 + j
] =
5550 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5556 setup_shared(struct ac_nir_context
*ctx
,
5557 struct nir_shader
*nir
)
5559 nir_foreach_variable(variable
, &nir
->shared
) {
5560 LLVMValueRef shared
=
5561 LLVMAddGlobalInAddressSpace(
5562 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5563 variable
->name
? variable
->name
: "",
5565 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5570 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5572 v
= ac_to_float(ctx
, v
);
5573 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5574 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5578 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5579 LLVMValueRef src0
, LLVMValueRef src1
)
5581 LLVMValueRef const16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
5582 LLVMValueRef comp
[2];
5584 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5585 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5586 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5587 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5590 /* Initialize arguments for the shader export intrinsic */
5592 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5593 LLVMValueRef
*values
,
5595 struct ac_export_args
*args
)
5597 /* Default is 0xf. Adjusted below depending on the format. */
5598 args
->enabled_channels
= 0xf;
5600 /* Specify whether the EXEC mask represents the valid mask */
5601 args
->valid_mask
= 0;
5603 /* Specify whether this is the last export */
5606 /* Specify the target we are exporting */
5607 args
->target
= target
;
5609 args
->compr
= false;
5610 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
5611 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
5612 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
5613 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5618 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5619 LLVMValueRef val
[4];
5620 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5621 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5622 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5623 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5625 switch(col_format
) {
5626 case V_028714_SPI_SHADER_ZERO
:
5627 args
->enabled_channels
= 0; /* writemask */
5628 args
->target
= V_008DFC_SQ_EXP_NULL
;
5631 case V_028714_SPI_SHADER_32_R
:
5632 args
->enabled_channels
= 1;
5633 args
->out
[0] = values
[0];
5636 case V_028714_SPI_SHADER_32_GR
:
5637 args
->enabled_channels
= 0x3;
5638 args
->out
[0] = values
[0];
5639 args
->out
[1] = values
[1];
5642 case V_028714_SPI_SHADER_32_AR
:
5643 args
->enabled_channels
= 0x9;
5644 args
->out
[0] = values
[0];
5645 args
->out
[3] = values
[3];
5648 case V_028714_SPI_SHADER_FP16_ABGR
:
5651 for (unsigned chan
= 0; chan
< 2; chan
++) {
5652 LLVMValueRef pack_args
[2] = {
5654 values
[2 * chan
+ 1]
5656 LLVMValueRef packed
;
5658 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5659 args
->out
[chan
] = packed
;
5663 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5664 for (unsigned chan
= 0; chan
< 4; chan
++) {
5665 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5666 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5667 LLVMConstReal(ctx
->ac
.f32
, 65535), "");
5668 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5669 LLVMConstReal(ctx
->ac
.f32
, 0.5), "");
5670 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5675 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5676 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5679 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5680 for (unsigned chan
= 0; chan
< 4; chan
++) {
5681 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5682 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5683 LLVMConstReal(ctx
->ac
.f32
, 32767), "");
5685 /* If positive, add 0.5, else add -0.5. */
5686 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5687 LLVMBuildSelect(ctx
->builder
,
5688 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5689 val
[chan
], ctx
->ac
.f32_0
, ""),
5690 LLVMConstReal(ctx
->ac
.f32
, 0.5),
5691 LLVMConstReal(ctx
->ac
.f32
, -0.5), ""), "");
5692 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->ac
.i32
, "");
5696 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5697 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5700 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5701 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5702 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5703 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->ac
.i32
, 3, 0);
5705 for (unsigned chan
= 0; chan
< 4; chan
++) {
5706 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5707 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5711 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5712 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5716 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5717 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5718 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5719 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5720 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5721 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->ac
.i32_1
;
5722 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->ac
.i32
, -2, 0);
5725 for (unsigned chan
= 0; chan
< 4; chan
++) {
5726 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5727 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5728 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5732 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5733 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5738 case V_028714_SPI_SHADER_32_ABGR
:
5739 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5743 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5745 for (unsigned i
= 0; i
< 4; ++i
)
5746 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5750 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5751 bool export_prim_id
,
5752 struct ac_vs_output_info
*outinfo
)
5754 uint32_t param_count
= 0;
5756 unsigned pos_idx
, num_pos_exports
= 0;
5757 struct ac_export_args args
, pos_args
[4] = {};
5758 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5761 if (ctx
->options
->key
.has_multiview_view_index
) {
5762 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5764 for(unsigned i
= 0; i
< 4; ++i
)
5765 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5766 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5769 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5770 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5773 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5774 sizeof(outinfo
->vs_output_param_offset
));
5776 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5777 LLVMValueRef slots
[8];
5780 if (outinfo
->cull_dist_mask
)
5781 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5783 i
= VARYING_SLOT_CLIP_DIST0
;
5784 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5785 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5786 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5788 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5789 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
5791 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5792 target
= V_008DFC_SQ_EXP_POS
+ 3;
5793 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5794 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5795 &args
, sizeof(args
));
5798 target
= V_008DFC_SQ_EXP_POS
+ 2;
5799 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5800 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5801 &args
, sizeof(args
));
5805 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
5806 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5807 for (unsigned j
= 0; j
< 4; j
++)
5808 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5809 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5811 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5813 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5814 outinfo
->writes_pointsize
= true;
5815 psize_value
= LLVMBuildLoad(ctx
->builder
,
5816 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5819 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5820 outinfo
->writes_layer
= true;
5821 layer_value
= LLVMBuildLoad(ctx
->builder
,
5822 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5825 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5826 outinfo
->writes_viewport_index
= true;
5827 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5828 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5831 if (outinfo
->writes_pointsize
||
5832 outinfo
->writes_layer
||
5833 outinfo
->writes_viewport_index
) {
5834 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5835 (outinfo
->writes_layer
== true ? 4 : 0));
5836 pos_args
[1].valid_mask
= 0;
5837 pos_args
[1].done
= 0;
5838 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5839 pos_args
[1].compr
= 0;
5840 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
5841 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
5842 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
5843 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
5845 if (outinfo
->writes_pointsize
== true)
5846 pos_args
[1].out
[0] = psize_value
;
5847 if (outinfo
->writes_layer
== true)
5848 pos_args
[1].out
[2] = layer_value
;
5849 if (outinfo
->writes_viewport_index
== true) {
5850 if (ctx
->options
->chip_class
>= GFX9
) {
5851 /* GFX9 has the layer in out.z[10:0] and the viewport
5852 * index in out.z[19:16].
5854 LLVMValueRef v
= viewport_index_value
;
5855 v
= ac_to_integer(&ctx
->ac
, v
);
5856 v
= LLVMBuildShl(ctx
->builder
, v
,
5857 LLVMConstInt(ctx
->ac
.i32
, 16, false),
5859 v
= LLVMBuildOr(ctx
->builder
, v
,
5860 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5862 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5863 pos_args
[1].enabled_channels
|= 1 << 2;
5865 pos_args
[1].out
[3] = viewport_index_value
;
5866 pos_args
[1].enabled_channels
|= 1 << 3;
5870 for (i
= 0; i
< 4; i
++) {
5871 if (pos_args
[i
].out
[0])
5876 for (i
= 0; i
< 4; i
++) {
5877 if (!pos_args
[i
].out
[0])
5880 /* Specify the target we are exporting */
5881 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5882 if (pos_idx
== num_pos_exports
)
5883 pos_args
[i
].done
= 1;
5884 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5887 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5888 LLVMValueRef values
[4];
5889 if (!(ctx
->output_mask
& (1ull << i
)))
5892 for (unsigned j
= 0; j
< 4; j
++)
5893 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5894 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5896 if (i
== VARYING_SLOT_LAYER
) {
5897 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5898 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5900 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5901 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5902 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5904 } else if (i
>= VARYING_SLOT_VAR0
) {
5905 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5906 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5907 outinfo
->vs_output_param_offset
[i
] = param_count
;
5912 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5914 if (target
>= V_008DFC_SQ_EXP_POS
&&
5915 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5916 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5917 &args
, sizeof(args
));
5919 ac_build_export(&ctx
->ac
, &args
);
5923 if (export_prim_id
) {
5924 LLVMValueRef values
[4];
5925 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5926 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5929 values
[0] = ctx
->vs_prim_id
;
5930 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5931 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5932 for (unsigned j
= 1; j
< 4; j
++)
5933 values
[j
] = ctx
->ac
.f32_0
;
5934 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5935 ac_build_export(&ctx
->ac
, &args
);
5936 outinfo
->export_prim_id
= true;
5939 outinfo
->pos_exports
= num_pos_exports
;
5940 outinfo
->param_exports
= param_count
;
5944 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5945 struct ac_es_output_info
*outinfo
)
5948 uint64_t max_output_written
= 0;
5949 LLVMValueRef lds_base
= NULL
;
5951 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5955 if (!(ctx
->output_mask
& (1ull << i
)))
5958 if (i
== VARYING_SLOT_CLIP_DIST0
)
5959 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5961 param_index
= shader_io_get_unique_index(i
);
5963 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5966 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5968 if (ctx
->ac
.chip_class
>= GFX9
) {
5969 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
5970 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
5971 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
5972 LLVMConstInt(ctx
->ac
.i32
, 24, false),
5973 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
5974 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
5975 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
5976 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
5977 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
5978 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
5981 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5982 LLVMValueRef dw_addr
;
5983 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5987 if (!(ctx
->output_mask
& (1ull << i
)))
5990 if (i
== VARYING_SLOT_CLIP_DIST0
)
5991 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5993 param_index
= shader_io_get_unique_index(i
);
5996 dw_addr
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5997 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
6000 for (j
= 0; j
< length
; j
++) {
6001 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
6002 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
6004 if (ctx
->ac
.chip_class
>= GFX9
) {
6005 ac_lds_store(&ctx
->ac
, dw_addr
,
6006 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6007 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6009 ac_build_buffer_store_dword(&ctx
->ac
,
6012 NULL
, ctx
->es2gs_offset
,
6013 (4 * param_index
+ j
) * 4,
6021 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
6023 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
6024 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
6025 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
6026 vertex_dw_stride
, "");
6028 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6029 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
6032 if (!(ctx
->output_mask
& (1ull << i
)))
6035 if (i
== VARYING_SLOT_CLIP_DIST0
)
6036 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6037 int param
= shader_io_get_unique_index(i
);
6038 mark_tess_output(ctx
, false, param
);
6040 mark_tess_output(ctx
, false, param
+ 1);
6041 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
6042 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
6044 for (unsigned j
= 0; j
< length
; j
++) {
6045 ac_lds_store(&ctx
->ac
, dw_addr
,
6046 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6047 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6052 struct ac_build_if_state
6054 struct nir_to_llvm_context
*ctx
;
6055 LLVMValueRef condition
;
6056 LLVMBasicBlockRef entry_block
;
6057 LLVMBasicBlockRef true_block
;
6058 LLVMBasicBlockRef false_block
;
6059 LLVMBasicBlockRef merge_block
;
6062 static LLVMBasicBlockRef
6063 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
6065 LLVMBasicBlockRef current_block
;
6066 LLVMBasicBlockRef next_block
;
6067 LLVMBasicBlockRef new_block
;
6069 /* get current basic block */
6070 current_block
= LLVMGetInsertBlock(ctx
->builder
);
6072 /* chqeck if there's another block after this one */
6073 next_block
= LLVMGetNextBasicBlock(current_block
);
6075 /* insert the new block before the next block */
6076 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
6079 /* append new block after current block */
6080 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
6081 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
6087 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
6088 struct nir_to_llvm_context
*ctx
,
6089 LLVMValueRef condition
)
6091 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
6093 memset(ifthen
, 0, sizeof *ifthen
);
6095 ifthen
->condition
= condition
;
6096 ifthen
->entry_block
= block
;
6098 /* create endif/merge basic block for the phi functions */
6099 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
6101 /* create/insert true_block before merge_block */
6102 ifthen
->true_block
=
6103 LLVMInsertBasicBlockInContext(ctx
->context
,
6104 ifthen
->merge_block
,
6107 /* successive code goes into the true block */
6108 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
6112 * End a conditional.
6115 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
6117 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
6119 /* Insert branch to the merge block from current block */
6120 LLVMBuildBr(builder
, ifthen
->merge_block
);
6123 * Now patch in the various branch instructions.
6126 /* Insert the conditional branch instruction at the end of entry_block */
6127 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
6128 if (ifthen
->false_block
) {
6129 /* we have an else clause */
6130 LLVMBuildCondBr(builder
, ifthen
->condition
,
6131 ifthen
->true_block
, ifthen
->false_block
);
6134 /* no else clause */
6135 LLVMBuildCondBr(builder
, ifthen
->condition
,
6136 ifthen
->true_block
, ifthen
->merge_block
);
6139 /* Resume building code at end of the ifthen->merge_block */
6140 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
6144 write_tess_factors(struct nir_to_llvm_context
*ctx
)
6146 unsigned stride
, outer_comps
, inner_comps
;
6147 struct ac_build_if_state if_ctx
, inner_if_ctx
;
6148 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 8, 5);
6149 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
6150 unsigned tess_inner_index
, tess_outer_index
;
6151 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6152 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6156 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6176 ac_nir_build_if(&if_ctx
, ctx
,
6177 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6178 invocation_id
, ctx
->ac
.i32_0
, ""));
6180 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6181 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6183 mark_tess_output(ctx
, true, tess_inner_index
);
6184 mark_tess_output(ctx
, true, tess_outer_index
);
6185 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6186 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6187 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6188 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6189 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6191 for (i
= 0; i
< 4; i
++) {
6192 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6193 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6197 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6198 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6199 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6201 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6203 for (i
= 0; i
< outer_comps
; i
++) {
6205 ac_lds_load(&ctx
->ac
, lds_outer
);
6206 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6209 for (i
= 0; i
< inner_comps
; i
++) {
6210 inner
[i
] = out
[outer_comps
+i
] =
6211 ac_lds_load(&ctx
->ac
, lds_inner
);
6212 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6217 /* Convert the outputs to vectors for stores. */
6218 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6222 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6225 buffer
= ctx
->hs_ring_tess_factor
;
6226 tf_base
= ctx
->tess_factor_offset
;
6227 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6228 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6229 unsigned tf_offset
= 0;
6231 if (ctx
->options
->chip_class
<= VI
) {
6232 ac_nir_build_if(&inner_if_ctx
, ctx
,
6233 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6234 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6236 /* Store the dynamic HS control word. */
6237 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6238 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6239 1, ctx
->ac
.i32_0
, tf_base
,
6240 0, 1, 0, true, false);
6243 ac_nir_build_endif(&inner_if_ctx
);
6246 /* Store the tessellation factors. */
6247 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6248 MIN2(stride
, 4), byteoffset
, tf_base
,
6249 tf_offset
, 1, 0, true, false);
6251 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6252 stride
- 4, byteoffset
, tf_base
,
6253 16 + tf_offset
, 1, 0, true, false);
6255 //store to offchip for TES to read - only if TES reads them
6256 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6257 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6258 LLVMValueRef tf_inner_offset
;
6259 unsigned param_outer
, param_inner
;
6261 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6262 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6263 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6265 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6266 util_next_power_of_two(outer_comps
));
6268 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6269 outer_comps
, tf_outer_offset
,
6270 ctx
->oc_lds
, 0, 1, 0, true, false);
6272 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6273 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6274 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6276 inner_vec
= inner_comps
== 1 ? inner
[0] :
6277 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6278 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6279 inner_comps
, tf_inner_offset
,
6280 ctx
->oc_lds
, 0, 1, 0, true, false);
6283 ac_nir_build_endif(&if_ctx
);
6287 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6289 write_tess_factors(ctx
);
6293 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6294 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6295 struct ac_export_args
*args
)
6298 si_llvm_init_export_args(ctx
, color
, param
,
6302 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6303 args
->done
= 1; /* DONE bit */
6304 } else if (!args
->enabled_channels
)
6305 return false; /* unnecessary NULL export */
6311 radv_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6312 LLVMValueRef depth
, LLVMValueRef stencil
,
6313 LLVMValueRef samplemask
)
6315 struct ac_export_args args
;
6317 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
6319 ac_build_export(&ctx
->ac
, &args
);
6323 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6326 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6327 struct ac_export_args color_args
[8];
6329 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6330 LLVMValueRef values
[4];
6332 if (!(ctx
->output_mask
& (1ull << i
)))
6335 if (i
== FRAG_RESULT_DEPTH
) {
6336 ctx
->shader_info
->fs
.writes_z
= true;
6337 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6338 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6339 } else if (i
== FRAG_RESULT_STENCIL
) {
6340 ctx
->shader_info
->fs
.writes_stencil
= true;
6341 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6342 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6343 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6344 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6345 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6346 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6349 for (unsigned j
= 0; j
< 4; j
++)
6350 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6351 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6353 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6354 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6356 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6362 for (unsigned i
= 0; i
< index
; i
++)
6363 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6364 if (depth
|| stencil
|| samplemask
)
6365 radv_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6367 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6368 ac_build_export(&ctx
->ac
, &color_args
[0]);
6371 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
6375 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6377 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6381 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6382 LLVMValueRef
*addrs
)
6384 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6386 switch (ctx
->stage
) {
6387 case MESA_SHADER_VERTEX
:
6388 if (ctx
->options
->key
.vs
.as_ls
)
6389 handle_ls_outputs_post(ctx
);
6390 else if (ctx
->options
->key
.vs
.as_es
)
6391 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6393 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6394 &ctx
->shader_info
->vs
.outinfo
);
6396 case MESA_SHADER_FRAGMENT
:
6397 handle_fs_outputs_post(ctx
);
6399 case MESA_SHADER_GEOMETRY
:
6400 emit_gs_epilogue(ctx
);
6402 case MESA_SHADER_TESS_CTRL
:
6403 handle_tcs_outputs_post(ctx
);
6405 case MESA_SHADER_TESS_EVAL
:
6406 if (ctx
->options
->key
.tes
.as_es
)
6407 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6409 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6410 &ctx
->shader_info
->tes
.outinfo
);
6417 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6419 LLVMPassManagerRef passmgr
;
6420 /* Create the pass manager */
6421 passmgr
= LLVMCreateFunctionPassManagerForModule(
6424 /* This pass should eliminate all the load and store instructions */
6425 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6427 /* Add some optimization passes */
6428 LLVMAddScalarReplAggregatesPass(passmgr
);
6429 LLVMAddLICMPass(passmgr
);
6430 LLVMAddAggressiveDCEPass(passmgr
);
6431 LLVMAddCFGSimplificationPass(passmgr
);
6432 LLVMAddInstructionCombiningPass(passmgr
);
6435 LLVMInitializeFunctionPassManager(passmgr
);
6436 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6437 LLVMFinalizeFunctionPassManager(passmgr
);
6439 LLVMDisposeBuilder(ctx
->builder
);
6440 LLVMDisposePassManager(passmgr
);
6444 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6446 struct ac_vs_output_info
*outinfo
;
6448 switch (ctx
->stage
) {
6449 case MESA_SHADER_FRAGMENT
:
6450 case MESA_SHADER_COMPUTE
:
6451 case MESA_SHADER_TESS_CTRL
:
6452 case MESA_SHADER_GEOMETRY
:
6454 case MESA_SHADER_VERTEX
:
6455 if (ctx
->options
->key
.vs
.as_ls
||
6456 ctx
->options
->key
.vs
.as_es
)
6458 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6460 case MESA_SHADER_TESS_EVAL
:
6461 if (ctx
->options
->key
.vs
.as_es
)
6463 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6466 unreachable("Unhandled shader type");
6469 ac_optimize_vs_outputs(&ctx
->ac
,
6471 outinfo
->vs_output_param_offset
,
6473 &outinfo
->param_exports
);
6477 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6479 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6480 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6481 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6484 if (ctx
->is_gs_copy_shader
) {
6485 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6487 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6489 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6490 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6492 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.v4i32
, "");
6494 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6495 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6496 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6497 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6500 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6501 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6502 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6503 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6508 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6509 const struct nir_shader
*nir
)
6511 switch (nir
->info
.stage
) {
6512 case MESA_SHADER_TESS_CTRL
:
6513 return chip_class
>= CIK
? 128 : 64;
6514 case MESA_SHADER_GEOMETRY
:
6515 return chip_class
>= GFX9
? 128 : 64;
6516 case MESA_SHADER_COMPUTE
:
6522 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6523 nir
->info
.cs
.local_size
[1] *
6524 nir
->info
.cs
.local_size
[2];
6525 return max_workgroup_size
;
6528 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6529 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6531 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6532 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6533 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6534 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6536 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6537 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6538 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_rel_ids
, ctx
->rel_auto_id
, "");
6539 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6542 static void prepare_gs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6544 for(int i
= 5; i
>= 0; --i
) {
6545 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6546 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6547 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6550 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6551 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6552 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6555 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6556 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6558 struct ac_nir_context ctx
= {};
6559 struct nir_function
*func
;
6568 ctx
.stage
= nir
->info
.stage
;
6570 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6572 nir_foreach_variable(variable
, &nir
->outputs
)
6573 handle_shader_output_decl(&ctx
, nir
, variable
);
6575 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6576 _mesa_key_pointer_equal
);
6577 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6578 _mesa_key_pointer_equal
);
6579 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6580 _mesa_key_pointer_equal
);
6582 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6584 setup_locals(&ctx
, func
);
6586 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6587 setup_shared(&ctx
, nir
);
6589 visit_cf_list(&ctx
, &func
->impl
->body
);
6590 phi_post_pass(&ctx
);
6592 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6596 ralloc_free(ctx
.defs
);
6597 ralloc_free(ctx
.phis
);
6598 ralloc_free(ctx
.vars
);
6605 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6606 struct nir_shader
*const *shaders
,
6608 struct ac_shader_variant_info
*shader_info
,
6609 const struct ac_nir_compiler_options
*options
)
6611 struct nir_to_llvm_context ctx
= {0};
6613 ctx
.options
= options
;
6614 ctx
.shader_info
= shader_info
;
6615 ctx
.context
= LLVMContextCreate();
6616 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6618 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
6620 ctx
.ac
.module
= ctx
.module
;
6621 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6623 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6624 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6625 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6626 LLVMDisposeTargetData(data_layout
);
6627 LLVMDisposeMessage(data_layout_str
);
6629 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6630 ctx
.ac
.builder
= ctx
.builder
;
6632 memset(shader_info
, 0, sizeof(*shader_info
));
6634 for(int i
= 0; i
< shader_count
; ++i
)
6635 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6637 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6638 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6639 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6640 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6642 ctx
.max_workgroup_size
= 0;
6643 for (int i
= 0; i
< shader_count
; ++i
) {
6644 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6645 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6649 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6650 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6652 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6653 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6654 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
6655 ctx
.abi
.load_ubo
= radv_load_ubo
;
6656 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6657 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6658 ctx
.abi
.clamp_shadow_reference
= false;
6660 if (shader_count
>= 2)
6661 ac_init_exec_full_mask(&ctx
.ac
);
6663 if (ctx
.ac
.chip_class
== GFX9
&&
6664 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6665 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6667 for(int i
= 0; i
< shader_count
; ++i
) {
6668 ctx
.stage
= shaders
[i
]->info
.stage
;
6669 ctx
.output_mask
= 0;
6670 ctx
.tess_outputs_written
= 0;
6671 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6672 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6674 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6675 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6676 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6677 ctx
.abi
.load_inputs
= load_gs_input
;
6678 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6679 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
6680 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
6681 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6682 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6683 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6684 if (shader_info
->info
.vs
.needs_instance_id
) {
6685 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6686 MAX2(3, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6688 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6689 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6695 ac_setup_rings(&ctx
);
6697 LLVMBasicBlockRef merge_block
;
6698 if (shader_count
>= 2) {
6699 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6700 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6701 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6703 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6704 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6705 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6706 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6707 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6708 thread_id
, count
, "");
6709 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6711 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6714 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6715 handle_fs_inputs(&ctx
, shaders
[i
]);
6716 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6717 handle_vs_inputs(&ctx
, shaders
[i
]);
6718 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6719 prepare_gs_input_vgprs(&ctx
);
6721 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6722 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6724 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6726 if (shader_count
>= 2) {
6727 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6728 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6731 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6732 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6733 shaders
[i
]->info
.cull_distance_array_size
> 4;
6734 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6735 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6736 shaders
[i
]->info
.gs
.vertices_out
;
6737 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6738 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6739 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6740 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6741 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6745 LLVMBuildRetVoid(ctx
.builder
);
6747 ac_llvm_finalize_module(&ctx
);
6749 if (shader_count
== 1)
6750 ac_nir_eliminate_const_vs_outputs(&ctx
);
6755 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6757 unsigned *retval
= (unsigned *)context
;
6758 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6759 char *description
= LLVMGetDiagInfoDescription(di
);
6761 if (severity
== LLVMDSError
) {
6763 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6767 LLVMDisposeMessage(description
);
6770 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6771 struct ac_shader_binary
*binary
,
6772 LLVMTargetMachineRef tm
)
6774 unsigned retval
= 0;
6776 LLVMContextRef llvm_ctx
;
6777 LLVMMemoryBufferRef out_buffer
;
6778 unsigned buffer_size
;
6779 const char *buffer_data
;
6782 /* Setup Diagnostic Handler*/
6783 llvm_ctx
= LLVMGetModuleContext(M
);
6785 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6789 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6792 /* Process Errors/Warnings */
6794 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6800 /* Extract Shader Code*/
6801 buffer_size
= LLVMGetBufferSize(out_buffer
);
6802 buffer_data
= LLVMGetBufferStart(out_buffer
);
6804 ac_elf_read(buffer_data
, buffer_size
, binary
);
6807 LLVMDisposeMemoryBuffer(out_buffer
);
6813 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6814 LLVMModuleRef llvm_module
,
6815 struct ac_shader_binary
*binary
,
6816 struct ac_shader_config
*config
,
6817 struct ac_shader_variant_info
*shader_info
,
6818 gl_shader_stage stage
,
6819 bool dump_shader
, bool supports_spill
)
6822 ac_dump_module(llvm_module
);
6824 memset(binary
, 0, sizeof(*binary
));
6825 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6827 fprintf(stderr
, "compile failed\n");
6831 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6833 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6835 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6836 LLVMDisposeModule(llvm_module
);
6837 LLVMContextDispose(ctx
);
6839 if (stage
== MESA_SHADER_FRAGMENT
) {
6840 shader_info
->num_input_vgprs
= 0;
6841 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6842 shader_info
->num_input_vgprs
+= 2;
6843 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6844 shader_info
->num_input_vgprs
+= 2;
6845 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6846 shader_info
->num_input_vgprs
+= 2;
6847 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6848 shader_info
->num_input_vgprs
+= 3;
6849 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6850 shader_info
->num_input_vgprs
+= 2;
6851 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6852 shader_info
->num_input_vgprs
+= 2;
6853 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6854 shader_info
->num_input_vgprs
+= 2;
6855 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6856 shader_info
->num_input_vgprs
+= 1;
6857 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6858 shader_info
->num_input_vgprs
+= 1;
6859 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6860 shader_info
->num_input_vgprs
+= 1;
6861 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6862 shader_info
->num_input_vgprs
+= 1;
6863 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6864 shader_info
->num_input_vgprs
+= 1;
6865 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6866 shader_info
->num_input_vgprs
+= 1;
6867 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6868 shader_info
->num_input_vgprs
+= 1;
6869 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6870 shader_info
->num_input_vgprs
+= 1;
6871 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6872 shader_info
->num_input_vgprs
+= 1;
6874 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6876 /* +3 for scratch wave offset and VCC */
6877 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6878 shader_info
->num_input_sgprs
+ 3);
6882 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
6884 switch (nir
->info
.stage
) {
6885 case MESA_SHADER_COMPUTE
:
6886 for (int i
= 0; i
< 3; ++i
)
6887 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6889 case MESA_SHADER_FRAGMENT
:
6890 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6892 case MESA_SHADER_GEOMETRY
:
6893 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6894 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6895 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6896 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6898 case MESA_SHADER_TESS_EVAL
:
6899 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6900 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6901 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6902 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6903 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6905 case MESA_SHADER_TESS_CTRL
:
6906 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6908 case MESA_SHADER_VERTEX
:
6909 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6910 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6911 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6912 if (options
->key
.vs
.as_ls
)
6913 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6920 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6921 struct ac_shader_binary
*binary
,
6922 struct ac_shader_config
*config
,
6923 struct ac_shader_variant_info
*shader_info
,
6924 struct nir_shader
*const *nir
,
6926 const struct ac_nir_compiler_options
*options
,
6930 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
6933 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
6934 for (int i
= 0; i
< nir_count
; ++i
)
6935 ac_fill_shader_info(shader_info
, nir
[i
], options
);
6939 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6941 LLVMValueRef args
[9];
6942 args
[0] = ctx
->gsvs_ring
;
6943 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
6944 args
[3] = ctx
->ac
.i32_0
;
6945 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
6946 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
6947 args
[6] = ctx
->ac
.i32_1
; /* GLC */
6948 args
[7] = ctx
->ac
.i32_1
; /* SLC */
6949 args
[8] = ctx
->ac
.i32_0
; /* TFE */
6953 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6957 if (!(ctx
->output_mask
& (1ull << i
)))
6960 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6961 /* unpack clip and cull from a single set of slots */
6962 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6967 for (unsigned j
= 0; j
< length
; j
++) {
6969 args
[2] = LLVMConstInt(ctx
->ac
.i32
,
6971 ctx
->gs_max_out_vertices
* 16 * 4, false);
6973 value
= ac_build_intrinsic(&ctx
->ac
,
6974 "llvm.SI.buffer.load.dword.i32.i32",
6975 ctx
->ac
.i32
, args
, 9,
6976 AC_FUNC_ATTR_READONLY
|
6977 AC_FUNC_ATTR_LEGACY
);
6979 LLVMBuildStore(ctx
->builder
,
6980 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6984 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
6987 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6988 struct nir_shader
*geom_shader
,
6989 struct ac_shader_binary
*binary
,
6990 struct ac_shader_config
*config
,
6991 struct ac_shader_variant_info
*shader_info
,
6992 const struct ac_nir_compiler_options
*options
,
6995 struct nir_to_llvm_context ctx
= {0};
6996 ctx
.context
= LLVMContextCreate();
6997 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6998 ctx
.options
= options
;
6999 ctx
.shader_info
= shader_info
;
7001 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
7003 ctx
.ac
.module
= ctx
.module
;
7005 ctx
.is_gs_copy_shader
= true;
7006 LLVMSetTarget(ctx
.module
, "amdgcn--");
7008 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
7009 ctx
.ac
.builder
= ctx
.builder
;
7010 ctx
.stage
= MESA_SHADER_VERTEX
;
7012 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
7014 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
7015 ac_setup_rings(&ctx
);
7017 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
7018 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
7020 struct ac_nir_context nir_ctx
= {};
7021 nir_ctx
.ac
= ctx
.ac
;
7022 nir_ctx
.abi
= &ctx
.abi
;
7024 nir_ctx
.nctx
= &ctx
;
7027 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
7028 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
7029 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
7032 ac_gs_copy_shader_emit(&ctx
);
7036 LLVMBuildRetVoid(ctx
.builder
);
7038 ac_llvm_finalize_module(&ctx
);
7040 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
7042 dump_shader
, options
->supports_spill
);