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 RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
48 struct ac_nir_context
{
49 struct ac_llvm_context ac
;
50 struct ac_shader_abi
*abi
;
52 gl_shader_stage stage
;
54 struct hash_table
*defs
;
55 struct hash_table
*phis
;
56 struct hash_table
*vars
;
58 LLVMValueRef main_function
;
59 LLVMBasicBlockRef continue_block
;
60 LLVMBasicBlockRef break_block
;
66 struct radv_shader_context
{
67 struct ac_llvm_context ac
;
68 const struct ac_nir_compiler_options
*options
;
69 struct ac_shader_variant_info
*shader_info
;
70 struct ac_shader_abi abi
;
72 unsigned max_workgroup_size
;
73 LLVMContextRef context
;
74 LLVMValueRef main_function
;
76 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
77 LLVMValueRef ring_offsets
;
79 LLVMValueRef vertex_buffers
;
80 LLVMValueRef rel_auto_id
;
81 LLVMValueRef vs_prim_id
;
82 LLVMValueRef ls_out_layout
;
83 LLVMValueRef es2gs_offset
;
85 LLVMValueRef tcs_offchip_layout
;
86 LLVMValueRef tcs_out_offsets
;
87 LLVMValueRef tcs_out_layout
;
88 LLVMValueRef tcs_in_layout
;
90 LLVMValueRef merged_wave_info
;
91 LLVMValueRef tess_factor_offset
;
92 LLVMValueRef tes_rel_patch_id
;
96 LLVMValueRef gsvs_ring_stride
;
97 LLVMValueRef gsvs_num_entries
;
98 LLVMValueRef gs2vs_offset
;
99 LLVMValueRef gs_wave_id
;
100 LLVMValueRef gs_vtx_offset
[6];
102 LLVMValueRef esgs_ring
;
103 LLVMValueRef gsvs_ring
;
104 LLVMValueRef hs_ring_tess_offchip
;
105 LLVMValueRef hs_ring_tess_factor
;
107 LLVMValueRef sample_pos_offset
;
108 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
109 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
111 gl_shader_stage stage
;
113 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
116 uint64_t output_mask
;
117 uint8_t num_output_clips
;
118 uint8_t num_output_culls
;
120 bool is_gs_copy_shader
;
121 LLVMValueRef gs_next_vertex
;
122 unsigned gs_max_out_vertices
;
124 unsigned tes_primitive_mode
;
125 uint64_t tess_outputs_written
;
126 uint64_t tess_patch_outputs_written
;
128 uint32_t tcs_patch_outputs_read
;
129 uint64_t tcs_outputs_read
;
132 static inline struct radv_shader_context
*
133 radv_shader_context_from_abi(struct ac_shader_abi
*abi
)
135 struct radv_shader_context
*ctx
= NULL
;
136 return container_of(abi
, ctx
, abi
);
139 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
140 const nir_deref_var
*deref
,
141 enum ac_descriptor_type desc_type
,
142 const nir_tex_instr
*instr
,
143 bool image
, bool write
);
145 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
147 return (index
* 4) + chan
;
150 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
152 /* handle patch indices separate */
153 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
155 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
157 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
158 return 2 + (slot
- VARYING_SLOT_PATCH0
);
160 if (slot
== VARYING_SLOT_POS
)
162 if (slot
== VARYING_SLOT_PSIZ
)
164 if (slot
== VARYING_SLOT_CLIP_DIST0
)
166 /* 3 is reserved for clip dist as well */
167 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
168 return 4 + (slot
- VARYING_SLOT_VAR0
);
169 unreachable("illegal slot in get unique index\n");
172 static void set_llvm_calling_convention(LLVMValueRef func
,
173 gl_shader_stage stage
)
175 enum radeon_llvm_calling_convention calling_conv
;
178 case MESA_SHADER_VERTEX
:
179 case MESA_SHADER_TESS_EVAL
:
180 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
182 case MESA_SHADER_GEOMETRY
:
183 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
185 case MESA_SHADER_TESS_CTRL
:
186 calling_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
: RADEON_LLVM_AMDGPU_VS
;
188 case MESA_SHADER_FRAGMENT
:
189 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
191 case MESA_SHADER_COMPUTE
:
192 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
195 unreachable("Unhandle shader type");
198 LLVMSetFunctionCallConv(func
, calling_conv
);
203 LLVMTypeRef types
[MAX_ARGS
];
204 LLVMValueRef
*assign
[MAX_ARGS
];
205 unsigned array_params_mask
;
208 uint8_t num_sgprs_used
;
209 uint8_t num_vgprs_used
;
212 enum ac_arg_regfile
{
218 add_arg(struct arg_info
*info
, enum ac_arg_regfile regfile
, LLVMTypeRef type
,
219 LLVMValueRef
*param_ptr
)
221 assert(info
->count
< MAX_ARGS
);
223 info
->assign
[info
->count
] = param_ptr
;
224 info
->types
[info
->count
] = type
;
227 if (regfile
== ARG_SGPR
) {
228 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
231 assert(regfile
== ARG_VGPR
);
232 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
237 add_array_arg(struct arg_info
*info
, LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
239 info
->array_params_mask
|= (1 << info
->count
);
240 add_arg(info
, ARG_SGPR
, type
, param_ptr
);
243 static void assign_arguments(LLVMValueRef main_function
,
244 struct arg_info
*info
)
247 for (i
= 0; i
< info
->count
; i
++) {
249 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
254 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
255 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
256 unsigned num_return_elems
,
257 struct arg_info
*args
,
258 unsigned max_workgroup_size
,
261 LLVMTypeRef main_function_type
, ret_type
;
262 LLVMBasicBlockRef main_function_body
;
264 if (num_return_elems
)
265 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
266 num_return_elems
, true);
268 ret_type
= LLVMVoidTypeInContext(ctx
);
270 /* Setup the function */
272 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
273 LLVMValueRef main_function
=
274 LLVMAddFunction(module
, "main", main_function_type
);
276 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
277 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
279 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
280 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
281 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
283 if (args
->array_params_mask
& (1 << i
)) {
284 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
285 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_NOALIAS
);
286 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
290 if (max_workgroup_size
) {
291 ac_llvm_add_target_dep_function_attr(main_function
,
292 "amdgpu-max-work-group-size",
296 /* These were copied from some LLVM test. */
297 LLVMAddTargetDependentFunctionAttr(main_function
,
298 "less-precise-fpmad",
300 LLVMAddTargetDependentFunctionAttr(main_function
,
303 LLVMAddTargetDependentFunctionAttr(main_function
,
306 LLVMAddTargetDependentFunctionAttr(main_function
,
309 LLVMAddTargetDependentFunctionAttr(main_function
,
310 "no-signed-zeros-fp-math",
313 return main_function
;
316 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
317 LLVMValueRef param
, unsigned rshift
,
320 LLVMValueRef value
= param
;
322 value
= LLVMBuildLShr(ctx
->builder
, value
,
323 LLVMConstInt(ctx
->i32
, rshift
, false), "");
325 if (rshift
+ bitwidth
< 32) {
326 unsigned mask
= (1 << bitwidth
) - 1;
327 value
= LLVMBuildAnd(ctx
->builder
, value
,
328 LLVMConstInt(ctx
->i32
, mask
, false), "");
333 static LLVMValueRef
get_rel_patch_id(struct radv_shader_context
*ctx
)
335 switch (ctx
->stage
) {
336 case MESA_SHADER_TESS_CTRL
:
337 return unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
338 case MESA_SHADER_TESS_EVAL
:
339 return ctx
->tes_rel_patch_id
;
342 unreachable("Illegal stage");
346 /* Tessellation shaders pass outputs to the next shader using LDS.
348 * LS outputs = TCS inputs
349 * TCS outputs = TES inputs
352 * - TCS inputs for patch 0
353 * - TCS inputs for patch 1
354 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
356 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
357 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
358 * - TCS outputs for patch 1
359 * - Per-patch TCS outputs for patch 1
360 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
361 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
364 * All three shaders VS(LS), TCS, TES share the same LDS space.
367 get_tcs_in_patch_stride(struct radv_shader_context
*ctx
)
369 if (ctx
->stage
== MESA_SHADER_VERTEX
)
370 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
371 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
372 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
380 get_tcs_out_patch_stride(struct radv_shader_context
*ctx
)
382 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
386 get_tcs_out_patch0_offset(struct radv_shader_context
*ctx
)
388 return LLVMBuildMul(ctx
->ac
.builder
,
389 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
390 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
394 get_tcs_out_patch0_patch_data_offset(struct radv_shader_context
*ctx
)
396 return LLVMBuildMul(ctx
->ac
.builder
,
397 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
398 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
402 get_tcs_in_current_patch_offset(struct radv_shader_context
*ctx
)
404 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
405 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
407 return LLVMBuildMul(ctx
->ac
.builder
, patch_stride
, rel_patch_id
, "");
411 get_tcs_out_current_patch_offset(struct radv_shader_context
*ctx
)
413 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
414 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
415 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
417 return LLVMBuildAdd(ctx
->ac
.builder
, patch0_offset
,
418 LLVMBuildMul(ctx
->ac
.builder
, patch_stride
,
424 get_tcs_out_current_patch_data_offset(struct radv_shader_context
*ctx
)
426 LLVMValueRef patch0_patch_data_offset
=
427 get_tcs_out_patch0_patch_data_offset(ctx
);
428 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
429 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
431 return LLVMBuildAdd(ctx
->ac
.builder
, patch0_patch_data_offset
,
432 LLVMBuildMul(ctx
->ac
.builder
, patch_stride
,
438 set_loc(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
,
439 uint32_t indirect_offset
)
441 ud_info
->sgpr_idx
= *sgpr_idx
;
442 ud_info
->num_sgprs
= num_sgprs
;
443 ud_info
->indirect
= indirect_offset
> 0;
444 ud_info
->indirect_offset
= indirect_offset
;
445 *sgpr_idx
+= num_sgprs
;
449 set_loc_shader(struct radv_shader_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
452 struct ac_userdata_info
*ud_info
=
453 &ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
];
456 set_loc(ud_info
, sgpr_idx
, num_sgprs
, 0);
460 set_loc_desc(struct radv_shader_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
461 uint32_t indirect_offset
)
463 struct ac_userdata_info
*ud_info
=
464 &ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[idx
];
467 set_loc(ud_info
, sgpr_idx
, 2, indirect_offset
);
470 struct user_sgpr_info
{
471 bool need_ring_offsets
;
473 bool indirect_all_descriptor_sets
;
476 static bool needs_view_index_sgpr(struct radv_shader_context
*ctx
,
477 gl_shader_stage stage
)
480 case MESA_SHADER_VERTEX
:
481 if (ctx
->shader_info
->info
.needs_multiview_view_index
||
482 (!ctx
->options
->key
.vs
.as_es
&& !ctx
->options
->key
.vs
.as_ls
&& ctx
->options
->key
.has_multiview_view_index
))
485 case MESA_SHADER_TESS_EVAL
:
486 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
489 case MESA_SHADER_GEOMETRY
:
490 case MESA_SHADER_TESS_CTRL
:
491 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
501 count_vs_user_sgprs(struct radv_shader_context
*ctx
)
505 count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
506 count
+= ctx
->shader_info
->info
.vs
.needs_draw_id
? 3 : 2;
511 static void allocate_user_sgprs(struct radv_shader_context
*ctx
,
512 gl_shader_stage stage
,
513 bool has_previous_stage
,
514 gl_shader_stage previous_stage
,
515 bool needs_view_index
,
516 struct user_sgpr_info
*user_sgpr_info
)
518 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
520 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
521 if (stage
== MESA_SHADER_GEOMETRY
||
522 stage
== MESA_SHADER_VERTEX
||
523 stage
== MESA_SHADER_TESS_CTRL
||
524 stage
== MESA_SHADER_TESS_EVAL
||
525 ctx
->is_gs_copy_shader
)
526 user_sgpr_info
->need_ring_offsets
= true;
528 if (stage
== MESA_SHADER_FRAGMENT
&&
529 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
530 user_sgpr_info
->need_ring_offsets
= true;
532 /* 2 user sgprs will nearly always be allocated for scratch/rings */
533 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
534 user_sgpr_info
->sgpr_count
+= 2;
538 case MESA_SHADER_COMPUTE
:
539 if (ctx
->shader_info
->info
.cs
.uses_grid_size
)
540 user_sgpr_info
->sgpr_count
+= 3;
542 case MESA_SHADER_FRAGMENT
:
543 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
545 case MESA_SHADER_VERTEX
:
546 if (!ctx
->is_gs_copy_shader
)
547 user_sgpr_info
->sgpr_count
+= count_vs_user_sgprs(ctx
);
548 if (ctx
->options
->key
.vs
.as_ls
)
549 user_sgpr_info
->sgpr_count
++;
551 case MESA_SHADER_TESS_CTRL
:
552 if (has_previous_stage
) {
553 if (previous_stage
== MESA_SHADER_VERTEX
)
554 user_sgpr_info
->sgpr_count
+= count_vs_user_sgprs(ctx
);
555 user_sgpr_info
->sgpr_count
++;
557 user_sgpr_info
->sgpr_count
+= 4;
559 case MESA_SHADER_TESS_EVAL
:
560 user_sgpr_info
->sgpr_count
+= 1;
562 case MESA_SHADER_GEOMETRY
:
563 if (has_previous_stage
) {
564 if (previous_stage
== MESA_SHADER_VERTEX
) {
565 user_sgpr_info
->sgpr_count
+= count_vs_user_sgprs(ctx
);
567 user_sgpr_info
->sgpr_count
++;
570 user_sgpr_info
->sgpr_count
+= 2;
576 if (needs_view_index
)
577 user_sgpr_info
->sgpr_count
++;
579 if (ctx
->shader_info
->info
.loads_push_constants
)
580 user_sgpr_info
->sgpr_count
+= 2;
582 uint32_t available_sgprs
= ctx
->options
->chip_class
>= GFX9
? 32 : 16;
583 uint32_t remaining_sgprs
= available_sgprs
- user_sgpr_info
->sgpr_count
;
585 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
586 user_sgpr_info
->sgpr_count
+= 2;
587 user_sgpr_info
->indirect_all_descriptor_sets
= true;
589 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
594 declare_global_input_sgprs(struct radv_shader_context
*ctx
,
595 gl_shader_stage stage
,
596 bool has_previous_stage
,
597 gl_shader_stage previous_stage
,
598 const struct user_sgpr_info
*user_sgpr_info
,
599 struct arg_info
*args
,
600 LLVMValueRef
*desc_sets
)
602 LLVMTypeRef type
= ac_array_in_const_addr_space(ctx
->ac
.i8
);
603 unsigned num_sets
= ctx
->options
->layout
?
604 ctx
->options
->layout
->num_sets
: 0;
605 unsigned stage_mask
= 1 << stage
;
607 if (has_previous_stage
)
608 stage_mask
|= 1 << previous_stage
;
610 /* 1 for each descriptor set */
611 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
612 for (unsigned i
= 0; i
< num_sets
; ++i
) {
613 if ((ctx
->shader_info
->info
.desc_set_used_mask
& (1 << i
)) &&
614 ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
615 add_array_arg(args
, type
,
616 &ctx
->descriptor_sets
[i
]);
620 add_array_arg(args
, ac_array_in_const_addr_space(type
), desc_sets
);
623 if (ctx
->shader_info
->info
.loads_push_constants
) {
624 /* 1 for push constants and dynamic descriptors */
625 add_array_arg(args
, type
, &ctx
->abi
.push_constants
);
630 declare_vs_specific_input_sgprs(struct radv_shader_context
*ctx
,
631 gl_shader_stage stage
,
632 bool has_previous_stage
,
633 gl_shader_stage previous_stage
,
634 struct arg_info
*args
)
636 if (!ctx
->is_gs_copy_shader
&&
637 (stage
== MESA_SHADER_VERTEX
||
638 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
639 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
640 add_arg(args
, ARG_SGPR
, ac_array_in_const_addr_space(ctx
->ac
.v4i32
),
641 &ctx
->vertex_buffers
);
643 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
);
644 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);
645 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
646 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
);
652 declare_vs_input_vgprs(struct radv_shader_context
*ctx
, struct arg_info
*args
)
654 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
);
655 if (!ctx
->is_gs_copy_shader
) {
656 if (ctx
->options
->key
.vs
.as_ls
) {
657 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->rel_auto_id
);
658 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
660 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
661 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->vs_prim_id
);
663 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* unused */
668 declare_tes_input_vgprs(struct radv_shader_context
*ctx
, struct arg_info
*args
)
670 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_u
);
671 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_v
);
672 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
);
673 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.tes_patch_id
);
677 set_global_input_locs(struct radv_shader_context
*ctx
, gl_shader_stage stage
,
678 bool has_previous_stage
, gl_shader_stage previous_stage
,
679 const struct user_sgpr_info
*user_sgpr_info
,
680 LLVMValueRef desc_sets
, uint8_t *user_sgpr_idx
)
682 unsigned num_sets
= ctx
->options
->layout
?
683 ctx
->options
->layout
->num_sets
: 0;
684 unsigned stage_mask
= 1 << stage
;
686 if (has_previous_stage
)
687 stage_mask
|= 1 << previous_stage
;
689 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
690 for (unsigned i
= 0; i
< num_sets
; ++i
) {
691 if ((ctx
->shader_info
->info
.desc_set_used_mask
& (1 << i
)) &&
692 ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
693 set_loc_desc(ctx
, i
, user_sgpr_idx
, 0);
695 ctx
->descriptor_sets
[i
] = NULL
;
698 set_loc_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
,
701 for (unsigned i
= 0; i
< num_sets
; ++i
) {
702 if ((ctx
->shader_info
->info
.desc_set_used_mask
& (1 << i
)) &&
703 ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
704 set_loc_desc(ctx
, i
, user_sgpr_idx
, i
* 8);
705 ctx
->descriptor_sets
[i
] =
706 ac_build_load_to_sgpr(&ctx
->ac
,
708 LLVMConstInt(ctx
->ac
.i32
, i
, false));
711 ctx
->descriptor_sets
[i
] = NULL
;
713 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
716 if (ctx
->shader_info
->info
.loads_push_constants
) {
717 set_loc_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
722 set_vs_specific_input_locs(struct radv_shader_context
*ctx
,
723 gl_shader_stage stage
, bool has_previous_stage
,
724 gl_shader_stage previous_stage
,
725 uint8_t *user_sgpr_idx
)
727 if (!ctx
->is_gs_copy_shader
&&
728 (stage
== MESA_SHADER_VERTEX
||
729 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
730 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
731 set_loc_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
,
736 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
739 set_loc_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
740 user_sgpr_idx
, vs_num
);
744 static void create_function(struct radv_shader_context
*ctx
,
745 gl_shader_stage stage
,
746 bool has_previous_stage
,
747 gl_shader_stage previous_stage
)
749 uint8_t user_sgpr_idx
;
750 struct user_sgpr_info user_sgpr_info
;
751 struct arg_info args
= {};
752 LLVMValueRef desc_sets
;
753 bool needs_view_index
= needs_view_index_sgpr(ctx
, stage
);
754 allocate_user_sgprs(ctx
, stage
, has_previous_stage
,
755 previous_stage
, needs_view_index
, &user_sgpr_info
);
757 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
758 add_arg(&args
, ARG_SGPR
, ac_array_in_const_addr_space(ctx
->ac
.v4i32
),
763 case MESA_SHADER_COMPUTE
:
764 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
765 previous_stage
, &user_sgpr_info
,
768 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
769 add_arg(&args
, ARG_SGPR
, ctx
->ac
.v3i32
,
770 &ctx
->abi
.num_work_groups
);
773 for (int i
= 0; i
< 3; i
++) {
774 ctx
->abi
.workgroup_ids
[i
] = NULL
;
775 if (ctx
->shader_info
->info
.cs
.uses_block_id
[i
]) {
776 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
777 &ctx
->abi
.workgroup_ids
[i
]);
781 if (ctx
->shader_info
->info
.cs
.uses_local_invocation_idx
)
782 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.tg_size
);
783 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
,
784 &ctx
->abi
.local_invocation_ids
);
786 case MESA_SHADER_VERTEX
:
787 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
788 previous_stage
, &user_sgpr_info
,
790 declare_vs_specific_input_sgprs(ctx
, stage
, has_previous_stage
,
791 previous_stage
, &args
);
793 if (needs_view_index
)
794 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
795 &ctx
->abi
.view_index
);
796 if (ctx
->options
->key
.vs
.as_es
)
797 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
799 else if (ctx
->options
->key
.vs
.as_ls
)
800 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
801 &ctx
->ls_out_layout
);
803 declare_vs_input_vgprs(ctx
, &args
);
805 case MESA_SHADER_TESS_CTRL
:
806 if (has_previous_stage
) {
807 // First 6 system regs
808 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
809 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
810 &ctx
->merged_wave_info
);
811 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
812 &ctx
->tess_factor_offset
);
814 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
815 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
816 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
818 declare_global_input_sgprs(ctx
, stage
,
821 &user_sgpr_info
, &args
,
823 declare_vs_specific_input_sgprs(ctx
, stage
,
825 previous_stage
, &args
);
827 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
828 &ctx
->ls_out_layout
);
830 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
831 &ctx
->tcs_offchip_layout
);
832 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
833 &ctx
->tcs_out_offsets
);
834 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
835 &ctx
->tcs_out_layout
);
836 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
837 &ctx
->tcs_in_layout
);
838 if (needs_view_index
)
839 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
840 &ctx
->abi
.view_index
);
842 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
843 &ctx
->abi
.tcs_patch_id
);
844 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
845 &ctx
->abi
.tcs_rel_ids
);
847 declare_vs_input_vgprs(ctx
, &args
);
849 declare_global_input_sgprs(ctx
, stage
,
852 &user_sgpr_info
, &args
,
855 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
856 &ctx
->tcs_offchip_layout
);
857 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
858 &ctx
->tcs_out_offsets
);
859 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
860 &ctx
->tcs_out_layout
);
861 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
862 &ctx
->tcs_in_layout
);
863 if (needs_view_index
)
864 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
865 &ctx
->abi
.view_index
);
867 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
868 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
869 &ctx
->tess_factor_offset
);
870 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
871 &ctx
->abi
.tcs_patch_id
);
872 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
873 &ctx
->abi
.tcs_rel_ids
);
876 case MESA_SHADER_TESS_EVAL
:
877 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
878 previous_stage
, &user_sgpr_info
,
881 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
);
882 if (needs_view_index
)
883 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
884 &ctx
->abi
.view_index
);
886 if (ctx
->options
->key
.tes
.as_es
) {
887 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
888 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
889 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
892 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
893 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
895 declare_tes_input_vgprs(ctx
, &args
);
897 case MESA_SHADER_GEOMETRY
:
898 if (has_previous_stage
) {
899 // First 6 system regs
900 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
902 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
903 &ctx
->merged_wave_info
);
904 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
906 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
907 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
908 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
910 declare_global_input_sgprs(ctx
, stage
,
913 &user_sgpr_info
, &args
,
916 if (previous_stage
== MESA_SHADER_TESS_EVAL
) {
917 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
918 &ctx
->tcs_offchip_layout
);
920 declare_vs_specific_input_sgprs(ctx
, stage
,
926 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
927 &ctx
->gsvs_ring_stride
);
928 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
929 &ctx
->gsvs_num_entries
);
930 if (needs_view_index
)
931 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
932 &ctx
->abi
.view_index
);
934 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
935 &ctx
->gs_vtx_offset
[0]);
936 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
937 &ctx
->gs_vtx_offset
[2]);
938 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
939 &ctx
->abi
.gs_prim_id
);
940 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
941 &ctx
->abi
.gs_invocation_id
);
942 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
943 &ctx
->gs_vtx_offset
[4]);
945 if (previous_stage
== MESA_SHADER_VERTEX
) {
946 declare_vs_input_vgprs(ctx
, &args
);
948 declare_tes_input_vgprs(ctx
, &args
);
951 declare_global_input_sgprs(ctx
, stage
,
954 &user_sgpr_info
, &args
,
957 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
958 &ctx
->gsvs_ring_stride
);
959 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
960 &ctx
->gsvs_num_entries
);
961 if (needs_view_index
)
962 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
963 &ctx
->abi
.view_index
);
965 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
);
966 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs_wave_id
);
967 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
968 &ctx
->gs_vtx_offset
[0]);
969 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
970 &ctx
->gs_vtx_offset
[1]);
971 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
972 &ctx
->abi
.gs_prim_id
);
973 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
974 &ctx
->gs_vtx_offset
[2]);
975 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
976 &ctx
->gs_vtx_offset
[3]);
977 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
978 &ctx
->gs_vtx_offset
[4]);
979 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
980 &ctx
->gs_vtx_offset
[5]);
981 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
982 &ctx
->abi
.gs_invocation_id
);
985 case MESA_SHADER_FRAGMENT
:
986 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
987 previous_stage
, &user_sgpr_info
,
990 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
991 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
992 &ctx
->sample_pos_offset
);
994 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.prim_mask
);
995 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_sample
);
996 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_center
);
997 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
);
998 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
999 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_sample
);
1000 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_center
);
1001 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
);
1002 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
1003 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]);
1004 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]);
1005 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]);
1006 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]);
1007 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.front_face
);
1008 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
);
1009 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
);
1010 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* fixed pt */
1013 unreachable("Shader stage not implemented");
1016 ctx
->main_function
= create_llvm_function(
1017 ctx
->context
, ctx
->ac
.module
, ctx
->ac
.builder
, NULL
, 0, &args
,
1018 ctx
->max_workgroup_size
,
1019 ctx
->options
->unsafe_math
);
1020 set_llvm_calling_convention(ctx
->main_function
, stage
);
1023 ctx
->shader_info
->num_input_vgprs
= 0;
1024 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
1026 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
1028 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1029 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
1031 assign_arguments(ctx
->main_function
, &args
);
1035 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
1036 set_loc_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
,
1038 if (ctx
->options
->supports_spill
) {
1039 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
1040 LLVMPointerType(ctx
->ac
.i8
, AC_CONST_ADDR_SPACE
),
1041 NULL
, 0, AC_FUNC_ATTR_READNONE
);
1042 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->ac
.builder
, ctx
->ring_offsets
,
1043 ac_array_in_const_addr_space(ctx
->ac
.v4i32
), "");
1047 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
1048 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
1049 if (has_previous_stage
)
1052 set_global_input_locs(ctx
, stage
, has_previous_stage
, previous_stage
,
1053 &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
1056 case MESA_SHADER_COMPUTE
:
1057 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
1058 set_loc_shader(ctx
, AC_UD_CS_GRID_SIZE
,
1062 case MESA_SHADER_VERTEX
:
1063 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1064 previous_stage
, &user_sgpr_idx
);
1065 if (ctx
->abi
.view_index
)
1066 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1067 if (ctx
->options
->key
.vs
.as_ls
) {
1068 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1072 case MESA_SHADER_TESS_CTRL
:
1073 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1074 previous_stage
, &user_sgpr_idx
);
1075 if (has_previous_stage
)
1076 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1078 set_loc_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
1079 if (ctx
->abi
.view_index
)
1080 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1082 case MESA_SHADER_TESS_EVAL
:
1083 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
1084 if (ctx
->abi
.view_index
)
1085 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1087 case MESA_SHADER_GEOMETRY
:
1088 if (has_previous_stage
) {
1089 if (previous_stage
== MESA_SHADER_VERTEX
)
1090 set_vs_specific_input_locs(ctx
, stage
,
1095 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
,
1098 set_loc_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
,
1100 if (ctx
->abi
.view_index
)
1101 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1103 case MESA_SHADER_FRAGMENT
:
1104 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
1105 set_loc_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
,
1110 unreachable("Shader stage not implemented");
1113 if (stage
== MESA_SHADER_TESS_CTRL
||
1114 (stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_ls
) ||
1115 /* GFX9 has the ESGS ring buffer in LDS. */
1116 (stage
== MESA_SHADER_GEOMETRY
&& has_previous_stage
)) {
1117 ac_declare_lds_as_pointer(&ctx
->ac
);
1120 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
1123 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1124 LLVMValueRef value
, unsigned count
)
1126 unsigned num_components
= ac_get_llvm_num_components(value
);
1127 if (count
== num_components
)
1130 LLVMValueRef masks
[] = {
1131 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1132 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1135 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1138 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1139 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1143 build_store_values_extended(struct ac_llvm_context
*ac
,
1144 LLVMValueRef
*values
,
1145 unsigned value_count
,
1146 unsigned value_stride
,
1149 LLVMBuilderRef builder
= ac
->builder
;
1152 for (i
= 0; i
< value_count
; i
++) {
1153 LLVMValueRef ptr
= values
[i
* value_stride
];
1154 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1155 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1156 LLVMBuildStore(builder
, value
, ptr
);
1160 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1161 const nir_ssa_def
*def
)
1163 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1164 if (def
->num_components
> 1) {
1165 type
= LLVMVectorType(type
, def
->num_components
);
1170 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1173 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1174 return (LLVMValueRef
)entry
->data
;
1178 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
1180 LLVMValueRef ptr
= get_src(ctx
, src
);
1181 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
1182 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1184 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
1185 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
1188 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1189 const struct nir_block
*b
)
1191 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1192 return (LLVMBasicBlockRef
)entry
->data
;
1195 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1197 unsigned num_components
)
1199 LLVMValueRef value
= get_src(ctx
, src
.src
);
1200 bool need_swizzle
= false;
1203 LLVMTypeRef type
= LLVMTypeOf(value
);
1204 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1205 ? LLVMGetVectorSize(type
)
1208 for (unsigned i
= 0; i
< num_components
; ++i
) {
1209 assert(src
.swizzle
[i
] < src_components
);
1210 if (src
.swizzle
[i
] != i
)
1211 need_swizzle
= true;
1214 if (need_swizzle
|| num_components
!= src_components
) {
1215 LLVMValueRef masks
[] = {
1216 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1217 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1218 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1219 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1221 if (src_components
> 1 && num_components
== 1) {
1222 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1224 } else if (src_components
== 1 && num_components
> 1) {
1225 LLVMValueRef values
[] = {value
, value
, value
, value
};
1226 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1228 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1229 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1233 assert(!src
.negate
);
1238 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1239 LLVMIntPredicate pred
, LLVMValueRef src0
,
1242 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1243 return LLVMBuildSelect(ctx
->builder
, result
,
1244 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1248 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1249 LLVMRealPredicate pred
, LLVMValueRef src0
,
1252 LLVMValueRef result
;
1253 src0
= ac_to_float(ctx
, src0
);
1254 src1
= ac_to_float(ctx
, src1
);
1255 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1256 return LLVMBuildSelect(ctx
->builder
, result
,
1257 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1261 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1263 LLVMTypeRef result_type
,
1267 LLVMValueRef params
[] = {
1268 ac_to_float(ctx
, src0
),
1271 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1272 ac_get_elem_bits(ctx
, result_type
));
1273 assert(length
< sizeof(name
));
1274 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1277 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1279 LLVMTypeRef result_type
,
1280 LLVMValueRef src0
, LLVMValueRef src1
)
1283 LLVMValueRef params
[] = {
1284 ac_to_float(ctx
, src0
),
1285 ac_to_float(ctx
, src1
),
1288 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1289 ac_get_elem_bits(ctx
, result_type
));
1290 assert(length
< sizeof(name
));
1291 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1294 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1296 LLVMTypeRef result_type
,
1297 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1300 LLVMValueRef params
[] = {
1301 ac_to_float(ctx
, src0
),
1302 ac_to_float(ctx
, src1
),
1303 ac_to_float(ctx
, src2
),
1306 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1307 ac_get_elem_bits(ctx
, result_type
));
1308 assert(length
< sizeof(name
));
1309 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1312 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1313 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1315 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1317 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1320 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1321 LLVMIntPredicate pred
,
1322 LLVMValueRef src0
, LLVMValueRef src1
)
1324 return LLVMBuildSelect(ctx
->builder
,
1325 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1330 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1333 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1334 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1337 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1341 LLVMValueRef cmp
, val
, zero
, one
;
1344 if (bitsize
== 32) {
1354 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
1355 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1356 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
1357 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
1361 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1362 LLVMValueRef src0
, unsigned bitsize
)
1364 LLVMValueRef cmp
, val
, zero
, one
;
1367 if (bitsize
== 32) {
1377 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
1378 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1379 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
1380 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
1384 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1385 LLVMValueRef src0
, unsigned bitsize
)
1390 if (bitsize
== 32) {
1391 intr
= "llvm.floor.f32";
1394 intr
= "llvm.floor.f64";
1398 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1399 LLVMValueRef params
[] = {
1402 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
1403 AC_FUNC_ATTR_READNONE
);
1404 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1407 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1409 LLVMValueRef src0
, LLVMValueRef src1
)
1411 LLVMTypeRef ret_type
;
1412 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1414 LLVMValueRef params
[] = { src0
, src1
};
1415 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1418 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1419 params
, 2, AC_FUNC_ATTR_READNONE
);
1421 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1422 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1426 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1429 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1432 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1435 src0
= ac_to_float(ctx
, src0
);
1436 return LLVMBuildSExt(ctx
->builder
,
1437 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1441 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1445 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1450 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
1453 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1456 return LLVMBuildSExt(ctx
->builder
,
1457 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1461 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
1464 LLVMValueRef result
;
1465 LLVMValueRef cond
= NULL
;
1467 src0
= ac_to_float(ctx
, src0
);
1468 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
1470 if (ctx
->chip_class
>= VI
) {
1471 LLVMValueRef args
[2];
1472 /* Check if the result is a denormal - and flush to 0 if so. */
1474 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1475 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1478 /* need to convert back up to f32 */
1479 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1481 if (ctx
->chip_class
>= VI
)
1482 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
1485 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1486 * so compare the result and flush to 0 if it's smaller.
1488 LLVMValueRef temp
, cond2
;
1489 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
1490 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1491 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
1493 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1494 temp
, ctx
->f32_0
, "");
1495 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1496 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
1501 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1502 LLVMValueRef src0
, LLVMValueRef src1
)
1504 LLVMValueRef dst64
, result
;
1505 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1506 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1508 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1509 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1510 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1514 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1515 LLVMValueRef src0
, LLVMValueRef src1
)
1517 LLVMValueRef dst64
, result
;
1518 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1519 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1521 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1522 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1523 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1527 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1529 const LLVMValueRef srcs
[3])
1531 LLVMValueRef result
;
1532 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1534 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1535 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1539 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1540 LLVMValueRef src0
, LLVMValueRef src1
,
1541 LLVMValueRef src2
, LLVMValueRef src3
)
1543 LLVMValueRef bfi_args
[3], result
;
1545 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1546 LLVMBuildSub(ctx
->builder
,
1547 LLVMBuildShl(ctx
->builder
,
1552 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1555 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1558 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1559 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1561 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1562 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1563 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1565 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1569 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1572 LLVMValueRef comp
[2];
1574 src0
= ac_to_float(ctx
, src0
);
1575 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1576 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1578 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
1581 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1584 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1585 LLVMValueRef temps
[2], result
, val
;
1588 for (i
= 0; i
< 2; i
++) {
1589 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1590 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1591 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1592 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1595 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1597 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1602 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1608 LLVMValueRef result
;
1610 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1611 mask
= AC_TID_MASK_LEFT
;
1612 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1613 mask
= AC_TID_MASK_TOP
;
1615 mask
= AC_TID_MASK_TOP_LEFT
;
1617 /* for DDX we want to next X pixel, DDY next Y pixel. */
1618 if (op
== nir_op_fddx_fine
||
1619 op
== nir_op_fddx_coarse
||
1625 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1630 * this takes an I,J coordinate pair,
1631 * and works out the X and Y derivatives.
1632 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1634 static LLVMValueRef
emit_ddxy_interp(
1635 struct ac_nir_context
*ctx
,
1636 LLVMValueRef interp_ij
)
1638 LLVMValueRef result
[4], a
;
1641 for (i
= 0; i
< 2; i
++) {
1642 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1643 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1644 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1645 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1647 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1650 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1652 LLVMValueRef src
[4], result
= NULL
;
1653 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1654 unsigned src_components
;
1655 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1657 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1658 switch (instr
->op
) {
1664 case nir_op_pack_half_2x16
:
1667 case nir_op_unpack_half_2x16
:
1671 src_components
= num_components
;
1674 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1675 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1677 switch (instr
->op
) {
1683 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1684 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1687 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1690 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1693 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1696 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1697 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1698 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1701 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1702 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1703 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1706 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1709 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1712 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1715 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1718 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1719 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1720 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1721 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1722 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1723 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1724 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1727 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1728 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1729 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1732 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1735 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1738 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1741 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1742 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1743 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1746 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1747 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
1751 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1754 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1757 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1760 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1761 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1762 LLVMTypeOf(src
[0]), ""),
1766 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1767 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1768 LLVMTypeOf(src
[0]), ""),
1772 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1773 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1774 LLVMTypeOf(src
[0]), ""),
1778 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1781 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1784 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1787 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1790 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1793 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1796 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1799 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1802 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1805 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1808 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1809 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1812 result
= emit_iabs(&ctx
->ac
, src
[0]);
1815 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1818 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1821 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1824 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1827 result
= emit_isign(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1830 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1831 result
= emit_fsign(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1834 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1835 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1838 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1839 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1842 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1843 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1845 case nir_op_fround_even
:
1846 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1847 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1850 result
= emit_ffract(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1853 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1854 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1857 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1858 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1861 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1862 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1865 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1866 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1869 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1870 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1873 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1874 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1875 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
1879 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1880 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1883 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1884 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1885 if (ctx
->ac
.chip_class
< GFX9
&&
1886 instr
->dest
.dest
.ssa
.bit_size
== 32) {
1887 /* Only pre-GFX9 chips do not flush denorms. */
1888 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1889 ac_to_float_type(&ctx
->ac
, def_type
),
1894 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1895 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1896 if (ctx
->ac
.chip_class
< GFX9
&&
1897 instr
->dest
.dest
.ssa
.bit_size
== 32) {
1898 /* Only pre-GFX9 chips do not flush denorms. */
1899 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1900 ac_to_float_type(&ctx
->ac
, def_type
),
1905 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1906 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1908 case nir_op_ibitfield_extract
:
1909 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1911 case nir_op_ubitfield_extract
:
1912 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1914 case nir_op_bitfield_insert
:
1915 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1917 case nir_op_bitfield_reverse
:
1918 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1920 case nir_op_bit_count
:
1921 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
1922 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1924 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i64", ctx
->ac
.i64
, src
, 1, AC_FUNC_ATTR_READNONE
);
1925 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
1931 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1932 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1933 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1937 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1938 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1942 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1943 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1947 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1948 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1952 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1953 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1956 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1957 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1960 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1961 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1965 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1966 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1967 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1969 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1973 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1974 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1975 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1977 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1980 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1982 case nir_op_find_lsb
:
1983 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1984 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1986 case nir_op_ufind_msb
:
1987 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1988 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1990 case nir_op_ifind_msb
:
1991 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1992 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1994 case nir_op_uadd_carry
:
1995 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1996 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1997 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1999 case nir_op_usub_borrow
:
2000 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2001 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2002 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
2005 result
= emit_b2f(&ctx
->ac
, src
[0]);
2008 result
= emit_f2b(&ctx
->ac
, src
[0]);
2011 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
2014 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2015 result
= emit_i2b(&ctx
->ac
, src
[0]);
2017 case nir_op_fquantize2f16
:
2018 result
= emit_f2f16(&ctx
->ac
, src
[0]);
2020 case nir_op_umul_high
:
2021 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2022 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2023 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
2025 case nir_op_imul_high
:
2026 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2027 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2028 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
2030 case nir_op_pack_half_2x16
:
2031 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
2033 case nir_op_unpack_half_2x16
:
2034 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
2038 case nir_op_fddx_fine
:
2039 case nir_op_fddy_fine
:
2040 case nir_op_fddx_coarse
:
2041 case nir_op_fddy_coarse
:
2042 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
2045 case nir_op_unpack_64_2x32_split_x
: {
2046 assert(ac_get_llvm_num_components(src
[0]) == 1);
2047 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2050 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2055 case nir_op_unpack_64_2x32_split_y
: {
2056 assert(ac_get_llvm_num_components(src
[0]) == 1);
2057 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2060 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2065 case nir_op_pack_64_2x32_split
: {
2066 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
2067 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2068 src
[0], ctx
->ac
.i32_0
, "");
2069 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2070 src
[1], ctx
->ac
.i32_1
, "");
2071 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
2076 fprintf(stderr
, "Unknown NIR alu instr: ");
2077 nir_print_instr(&instr
->instr
, stderr
);
2078 fprintf(stderr
, "\n");
2083 assert(instr
->dest
.dest
.is_ssa
);
2084 result
= ac_to_integer(&ctx
->ac
, result
);
2085 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
2090 static void visit_load_const(struct ac_nir_context
*ctx
,
2091 const nir_load_const_instr
*instr
)
2093 LLVMValueRef values
[4], value
= NULL
;
2094 LLVMTypeRef element_type
=
2095 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
2097 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
2098 switch (instr
->def
.bit_size
) {
2100 values
[i
] = LLVMConstInt(element_type
,
2101 instr
->value
.u32
[i
], false);
2104 values
[i
] = LLVMConstInt(element_type
,
2105 instr
->value
.u64
[i
], false);
2109 "unsupported nir load_const bit_size: %d\n",
2110 instr
->def
.bit_size
);
2114 if (instr
->def
.num_components
> 1) {
2115 value
= LLVMConstVector(values
, instr
->def
.num_components
);
2119 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
2122 static LLVMValueRef
cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
2125 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2126 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2127 LLVMPointerType(type
, addr_space
), "");
2131 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
2134 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2135 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
2138 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
2139 /* On VI, the descriptor contains the size in bytes,
2140 * but TXQ must return the size in elements.
2141 * The stride is always non-zero for resources using TXQ.
2143 LLVMValueRef stride
=
2144 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2146 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2147 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2148 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2149 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2151 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2157 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2160 static void build_int_type_name(
2162 char *buf
, unsigned bufsize
)
2164 assert(bufsize
>= 6);
2166 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2167 snprintf(buf
, bufsize
, "v%ui32",
2168 LLVMGetVectorSize(type
));
2173 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2174 struct ac_image_args
*args
,
2175 const nir_tex_instr
*instr
)
2177 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2178 LLVMValueRef coord
= args
->addr
;
2179 LLVMValueRef half_texel
[2];
2180 LLVMValueRef compare_cube_wa
= NULL
;
2181 LLVMValueRef result
;
2183 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2187 struct ac_image_args txq_args
= { 0 };
2189 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2190 txq_args
.opcode
= ac_image_get_resinfo
;
2191 txq_args
.dmask
= 0xf;
2192 txq_args
.addr
= ctx
->i32_0
;
2193 txq_args
.resource
= args
->resource
;
2194 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2196 for (c
= 0; c
< 2; c
++) {
2197 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2198 LLVMConstInt(ctx
->i32
, c
, false), "");
2199 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2200 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2201 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2202 LLVMConstReal(ctx
->f32
, -0.5), "");
2206 LLVMValueRef orig_coords
= args
->addr
;
2208 for (c
= 0; c
< 2; c
++) {
2210 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2211 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2212 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2213 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2214 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2215 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2220 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2221 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2222 * workaround by sampling using a scaled type and converting.
2223 * This is taken from amdgpu-pro shaders.
2225 /* NOTE this produces some ugly code compared to amdgpu-pro,
2226 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2227 * and then reads them back. -pro generates two selects,
2228 * one s_cmp for the descriptor rewriting
2229 * one v_cmp for the coordinate and result changes.
2231 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2232 LLVMValueRef tmp
, tmp2
;
2234 /* workaround 8/8/8/8 uint/sint cube gather bug */
2235 /* first detect it then change to a scaled read and f2i */
2236 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2239 /* extract the DATA_FORMAT */
2240 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2241 LLVMConstInt(ctx
->i32
, 6, false), false);
2243 /* is the DATA_FORMAT == 8_8_8_8 */
2244 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2246 if (stype
== GLSL_TYPE_UINT
)
2247 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2248 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2249 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2251 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2252 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2253 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2255 /* replace the NUM FORMAT in the descriptor */
2256 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2257 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2259 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2261 /* don't modify the coordinates for this case */
2262 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2265 result
= ac_build_image_opcode(ctx
, args
);
2267 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2268 LLVMValueRef tmp
, tmp2
;
2270 /* if the cube workaround is in place, f2i the result. */
2271 for (c
= 0; c
< 4; c
++) {
2272 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2273 if (stype
== GLSL_TYPE_UINT
)
2274 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2276 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2277 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2278 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2279 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2280 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2281 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2287 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2288 const nir_tex_instr
*instr
,
2290 struct ac_image_args
*args
)
2292 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2293 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2295 return ac_build_buffer_load_format(&ctx
->ac
,
2299 util_last_bit(mask
),
2303 args
->opcode
= ac_image_sample
;
2304 args
->compare
= instr
->is_shadow
;
2306 switch (instr
->op
) {
2308 case nir_texop_txf_ms
:
2309 case nir_texop_samples_identical
:
2310 args
->opcode
= lod_is_zero
||
2311 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
2312 ac_image_load
: ac_image_load_mip
;
2313 args
->compare
= false;
2314 args
->offset
= false;
2321 args
->level_zero
= true;
2326 case nir_texop_query_levels
:
2327 args
->opcode
= ac_image_get_resinfo
;
2330 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2331 args
->level_zero
= true;
2337 args
->opcode
= ac_image_gather4
;
2338 args
->level_zero
= true;
2341 args
->opcode
= ac_image_get_lod
;
2342 args
->compare
= false;
2343 args
->offset
= false;
2349 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2350 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2351 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2352 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2355 return ac_build_image_opcode(&ctx
->ac
, args
);
2359 radv_load_resource(struct ac_shader_abi
*abi
, LLVMValueRef index
,
2360 unsigned desc_set
, unsigned binding
)
2362 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
2363 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2364 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2365 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2366 unsigned base_offset
= layout
->binding
[binding
].offset
;
2367 LLVMValueRef offset
, stride
;
2369 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2370 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2371 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2372 layout
->binding
[binding
].dynamic_offset_offset
;
2373 desc_ptr
= ctx
->abi
.push_constants
;
2374 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2375 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2377 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2379 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2380 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, stride
, "");
2381 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
2383 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2384 desc_ptr
= cast_ptr(&ctx
->ac
, desc_ptr
, ctx
->ac
.v4i32
);
2385 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2390 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
2391 nir_intrinsic_instr
*instr
)
2393 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
2394 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
2396 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2397 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2401 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
2402 nir_intrinsic_instr
*instr
)
2404 LLVMValueRef ptr
, addr
;
2406 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2407 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
2408 get_src(ctx
, instr
->src
[0]), "");
2410 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
2411 ptr
= cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
2413 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
2416 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2417 const nir_intrinsic_instr
*instr
)
2419 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2421 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
2424 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2426 uint32_t new_mask
= 0;
2427 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2428 if (mask
& (1u << i
))
2429 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2433 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
2434 unsigned start
, unsigned count
)
2436 LLVMTypeRef type
= LLVMTypeOf(src
);
2438 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
2444 unsigned src_elements
= LLVMGetVectorSize(type
);
2445 assert(start
< src_elements
);
2446 assert(start
+ count
<= src_elements
);
2448 if (start
== 0 && count
== src_elements
)
2452 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
2455 LLVMValueRef indices
[8];
2456 for (unsigned i
= 0; i
< count
; ++i
)
2457 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
2459 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
2460 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
2463 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2464 nir_intrinsic_instr
*instr
)
2466 const char *store_name
;
2467 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2468 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2469 int elem_size_mult
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2470 int components_32bit
= elem_size_mult
* instr
->num_components
;
2471 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2472 LLVMValueRef base_data
, base_offset
;
2473 LLVMValueRef params
[6];
2475 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2476 get_src(ctx
, instr
->src
[1]), true);
2477 params
[2] = ctx
->ac
.i32_0
; /* vindex */
2478 params
[4] = ctx
->ac
.i1false
; /* glc */
2479 params
[5] = ctx
->ac
.i1false
; /* slc */
2481 if (components_32bit
> 1)
2482 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2484 writemask
= widen_mask(writemask
, elem_size_mult
);
2486 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2487 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2488 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2490 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2494 LLVMValueRef offset
;
2496 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2498 /* Due to an LLVM limitation, split 3-element writes
2499 * into a 2-element and a 1-element write. */
2501 writemask
|= 1 << (start
+ 2);
2506 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2511 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2512 } else if (count
== 2) {
2513 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2517 store_name
= "llvm.amdgcn.buffer.store.f32";
2519 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
2521 offset
= base_offset
;
2523 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2527 ac_build_intrinsic(&ctx
->ac
, store_name
,
2528 ctx
->ac
.voidt
, params
, 6, 0);
2532 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2533 const nir_intrinsic_instr
*instr
)
2536 LLVMValueRef params
[6];
2539 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2540 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2542 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2543 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2544 get_src(ctx
, instr
->src
[0]),
2546 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
2547 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2548 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2550 switch (instr
->intrinsic
) {
2551 case nir_intrinsic_ssbo_atomic_add
:
2552 name
= "llvm.amdgcn.buffer.atomic.add";
2554 case nir_intrinsic_ssbo_atomic_imin
:
2555 name
= "llvm.amdgcn.buffer.atomic.smin";
2557 case nir_intrinsic_ssbo_atomic_umin
:
2558 name
= "llvm.amdgcn.buffer.atomic.umin";
2560 case nir_intrinsic_ssbo_atomic_imax
:
2561 name
= "llvm.amdgcn.buffer.atomic.smax";
2563 case nir_intrinsic_ssbo_atomic_umax
:
2564 name
= "llvm.amdgcn.buffer.atomic.umax";
2566 case nir_intrinsic_ssbo_atomic_and
:
2567 name
= "llvm.amdgcn.buffer.atomic.and";
2569 case nir_intrinsic_ssbo_atomic_or
:
2570 name
= "llvm.amdgcn.buffer.atomic.or";
2572 case nir_intrinsic_ssbo_atomic_xor
:
2573 name
= "llvm.amdgcn.buffer.atomic.xor";
2575 case nir_intrinsic_ssbo_atomic_exchange
:
2576 name
= "llvm.amdgcn.buffer.atomic.swap";
2578 case nir_intrinsic_ssbo_atomic_comp_swap
:
2579 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2585 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2588 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2589 const nir_intrinsic_instr
*instr
)
2591 LLVMValueRef results
[2];
2592 int load_components
;
2593 int num_components
= instr
->num_components
;
2594 if (instr
->dest
.ssa
.bit_size
== 64)
2595 num_components
*= 2;
2597 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2598 load_components
= MIN2(num_components
- i
, 4);
2599 const char *load_name
;
2600 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2601 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2602 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2604 if (load_components
== 3)
2605 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2606 else if (load_components
> 1)
2607 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2609 if (load_components
>= 3)
2610 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2611 else if (load_components
== 2)
2612 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2613 else if (load_components
== 1)
2614 load_name
= "llvm.amdgcn.buffer.load.f32";
2616 unreachable("unhandled number of components");
2618 LLVMValueRef params
[] = {
2619 ctx
->abi
->load_ssbo(ctx
->abi
,
2620 get_src(ctx
, instr
->src
[0]),
2628 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2632 LLVMValueRef ret
= results
[0];
2633 if (num_components
> 4 || num_components
== 3) {
2634 LLVMValueRef masks
[] = {
2635 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2636 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2637 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2638 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2641 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2642 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2643 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2646 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2647 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2650 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2651 const nir_intrinsic_instr
*instr
)
2654 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2655 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2656 int num_components
= instr
->num_components
;
2658 if (ctx
->abi
->load_ubo
)
2659 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2661 if (instr
->dest
.ssa
.bit_size
== 64)
2662 num_components
*= 2;
2664 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2665 NULL
, 0, false, false, true, true);
2666 ret
= trim_vector(&ctx
->ac
, ret
, num_components
);
2667 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2668 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2672 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2673 bool vs_in
, unsigned *vertex_index_out
,
2674 LLVMValueRef
*vertex_index_ref
,
2675 unsigned *const_out
, LLVMValueRef
*indir_out
)
2677 unsigned const_offset
= 0;
2678 nir_deref
*tail
= &deref
->deref
;
2679 LLVMValueRef offset
= NULL
;
2681 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2683 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2684 if (vertex_index_out
)
2685 *vertex_index_out
= deref_array
->base_offset
;
2687 if (vertex_index_ref
) {
2688 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2689 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2690 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2692 *vertex_index_ref
= vtx
;
2696 if (deref
->var
->data
.compact
) {
2697 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2698 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2699 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2700 /* We always lower indirect dereferences for "compact" array vars. */
2701 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2703 const_offset
= deref_array
->base_offset
;
2707 while (tail
->child
!= NULL
) {
2708 const struct glsl_type
*parent_type
= tail
->type
;
2711 if (tail
->deref_type
== nir_deref_type_array
) {
2712 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2713 LLVMValueRef index
, stride
, local_offset
;
2714 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2716 const_offset
+= size
* deref_array
->base_offset
;
2717 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2720 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2721 index
= get_src(ctx
, deref_array
->indirect
);
2722 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2723 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2726 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2728 offset
= local_offset
;
2729 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2730 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2732 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2733 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2734 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2737 unreachable("unsupported deref type");
2741 if (const_offset
&& offset
)
2742 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2743 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2746 *const_out
= const_offset
;
2747 *indir_out
= offset
;
2751 /* The offchip buffer layout for TCS->TES is
2753 * - attribute 0 of patch 0 vertex 0
2754 * - attribute 0 of patch 0 vertex 1
2755 * - attribute 0 of patch 0 vertex 2
2757 * - attribute 0 of patch 1 vertex 0
2758 * - attribute 0 of patch 1 vertex 1
2760 * - attribute 1 of patch 0 vertex 0
2761 * - attribute 1 of patch 0 vertex 1
2763 * - per patch attribute 0 of patch 0
2764 * - per patch attribute 0 of patch 1
2767 * Note that every attribute has 4 components.
2769 static LLVMValueRef
get_tcs_tes_buffer_address(struct radv_shader_context
*ctx
,
2770 LLVMValueRef vertex_index
,
2771 LLVMValueRef param_index
)
2773 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2774 LLVMValueRef param_stride
, constant16
;
2775 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2777 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2778 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2779 total_vertices
= LLVMBuildMul(ctx
->ac
.builder
, vertices_per_patch
,
2782 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2784 base_addr
= LLVMBuildMul(ctx
->ac
.builder
, rel_patch_id
,
2785 vertices_per_patch
, "");
2787 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
2790 param_stride
= total_vertices
;
2792 base_addr
= rel_patch_id
;
2793 param_stride
= num_patches
;
2796 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
2797 LLVMBuildMul(ctx
->ac
.builder
, param_index
,
2798 param_stride
, ""), "");
2800 base_addr
= LLVMBuildMul(ctx
->ac
.builder
, base_addr
, constant16
, "");
2802 if (!vertex_index
) {
2803 LLVMValueRef patch_data_offset
=
2804 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2806 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
2807 patch_data_offset
, "");
2812 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct radv_shader_context
*ctx
,
2814 unsigned const_index
,
2816 LLVMValueRef vertex_index
,
2817 LLVMValueRef indir_index
)
2819 LLVMValueRef param_index
;
2822 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2825 if (const_index
&& !is_compact
)
2826 param
+= const_index
;
2827 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2829 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2833 mark_tess_output(struct radv_shader_context
*ctx
,
2834 bool is_patch
, uint32_t param
)
2838 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2840 ctx
->tess_outputs_written
|= (1ull << param
);
2844 get_dw_address(struct radv_shader_context
*ctx
,
2845 LLVMValueRef dw_addr
,
2847 unsigned const_index
,
2848 bool compact_const_index
,
2849 LLVMValueRef vertex_index
,
2850 LLVMValueRef stride
,
2851 LLVMValueRef indir_index
)
2856 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2857 LLVMBuildMul(ctx
->ac
.builder
,
2863 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2864 LLVMBuildMul(ctx
->ac
.builder
, indir_index
,
2865 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2866 else if (const_index
&& !compact_const_index
)
2867 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2868 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2870 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2871 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2873 if (const_index
&& compact_const_index
)
2874 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2875 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2880 load_tcs_varyings(struct ac_shader_abi
*abi
,
2881 LLVMValueRef vertex_index
,
2882 LLVMValueRef indir_index
,
2883 unsigned const_index
,
2885 unsigned driver_location
,
2887 unsigned num_components
,
2892 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
2893 LLVMValueRef dw_addr
, stride
;
2894 LLVMValueRef value
[4], result
;
2895 unsigned param
= shader_io_get_unique_index(location
);
2898 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2899 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2902 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2903 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2905 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2910 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2913 for (unsigned i
= 0; i
< num_components
+ component
; i
++) {
2914 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2915 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2918 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
2923 store_tcs_output(struct ac_shader_abi
*abi
,
2924 LLVMValueRef vertex_index
,
2925 LLVMValueRef param_index
,
2926 unsigned const_index
,
2928 unsigned driver_location
,
2935 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
2936 LLVMValueRef dw_addr
;
2937 LLVMValueRef stride
= NULL
;
2938 LLVMValueRef buf_addr
= NULL
;
2940 bool store_lds
= true;
2943 if (!(ctx
->tcs_patch_outputs_read
& (1U << (location
- VARYING_SLOT_PATCH0
))))
2946 if (!(ctx
->tcs_outputs_read
& (1ULL << location
)))
2950 param
= shader_io_get_unique_index(location
);
2951 if (location
== VARYING_SLOT_CLIP_DIST0
&&
2952 is_compact
&& const_index
> 3) {
2958 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2959 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2961 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2964 mark_tess_output(ctx
, is_patch
, param
);
2966 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2968 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2969 vertex_index
, param_index
);
2971 bool is_tess_factor
= false;
2972 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2973 location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2974 is_tess_factor
= true;
2976 unsigned base
= is_compact
? const_index
: 0;
2977 for (unsigned chan
= 0; chan
< 8; chan
++) {
2978 if (!(writemask
& (1 << chan
)))
2980 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
2982 if (store_lds
|| is_tess_factor
) {
2983 LLVMValueRef dw_addr_chan
=
2984 LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2985 LLVMConstInt(ctx
->ac
.i32
, chan
, false), "");
2986 ac_lds_store(&ctx
->ac
, dw_addr_chan
, value
);
2989 if (!is_tess_factor
&& writemask
!= 0xF)
2990 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2991 buf_addr
, ctx
->oc_lds
,
2992 4 * (base
+ chan
), 1, 0, true, false);
2995 if (writemask
== 0xF) {
2996 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2997 buf_addr
, ctx
->oc_lds
,
2998 (base
* 4), 1, 0, true, false);
3003 load_tes_input(struct ac_shader_abi
*abi
,
3004 LLVMValueRef vertex_index
,
3005 LLVMValueRef param_index
,
3006 unsigned const_index
,
3008 unsigned driver_location
,
3010 unsigned num_components
,
3015 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
3016 LLVMValueRef buf_addr
;
3017 LLVMValueRef result
;
3018 unsigned param
= shader_io_get_unique_index(location
);
3020 if (location
== VARYING_SLOT_CLIP_DIST0
&& is_compact
&& const_index
> 3) {
3025 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
3026 is_compact
, vertex_index
, param_index
);
3028 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, component
* 4, false);
3029 buf_addr
= LLVMBuildAdd(ctx
->ac
.builder
, buf_addr
, comp_offset
, "");
3031 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, num_components
, NULL
,
3032 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
3033 result
= trim_vector(&ctx
->ac
, result
, num_components
);
3038 load_gs_input(struct ac_shader_abi
*abi
,
3040 unsigned driver_location
,
3042 unsigned num_components
,
3043 unsigned vertex_index
,
3044 unsigned const_index
,
3047 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
3048 LLVMValueRef vtx_offset
;
3049 unsigned param
, vtx_offset_param
;
3050 LLVMValueRef value
[4], result
;
3052 vtx_offset_param
= vertex_index
;
3053 assert(vtx_offset_param
< 6);
3054 vtx_offset
= LLVMBuildMul(ctx
->ac
.builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
3055 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3057 param
= shader_io_get_unique_index(location
);
3059 for (unsigned i
= component
; i
< num_components
+ component
; i
++) {
3060 if (ctx
->ac
.chip_class
>= GFX9
) {
3061 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
3062 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
3063 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
3064 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
3066 LLVMValueRef soffset
=
3067 LLVMConstInt(ctx
->ac
.i32
,
3068 (param
* 4 + i
+ const_index
) * 256,
3071 value
[i
] = ac_build_buffer_load(&ctx
->ac
,
3074 vtx_offset
, soffset
,
3075 0, 1, 0, true, false);
3077 value
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
, value
[i
],
3081 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
3082 result
= ac_to_integer(&ctx
->ac
, result
);
3087 build_gep_for_deref(struct ac_nir_context
*ctx
,
3088 nir_deref_var
*deref
)
3090 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
3091 assert(entry
->data
);
3092 LLVMValueRef val
= entry
->data
;
3093 nir_deref
*tail
= deref
->deref
.child
;
3094 while (tail
!= NULL
) {
3095 LLVMValueRef offset
;
3096 switch (tail
->deref_type
) {
3097 case nir_deref_type_array
: {
3098 nir_deref_array
*array
= nir_deref_as_array(tail
);
3099 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
3100 if (array
->deref_array_type
==
3101 nir_deref_array_type_indirect
) {
3102 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
3109 case nir_deref_type_struct
: {
3110 nir_deref_struct
*deref_struct
=
3111 nir_deref_as_struct(tail
);
3112 offset
= LLVMConstInt(ctx
->ac
.i32
,
3113 deref_struct
->index
, 0);
3117 unreachable("bad deref type");
3119 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3125 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
3126 nir_intrinsic_instr
*instr
,
3129 LLVMValueRef result
;
3130 LLVMValueRef vertex_index
= NULL
;
3131 LLVMValueRef indir_index
= NULL
;
3132 unsigned const_index
= 0;
3133 unsigned location
= instr
->variables
[0]->var
->data
.location
;
3134 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3135 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3136 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3138 get_deref_offset(ctx
, instr
->variables
[0],
3139 false, NULL
, is_patch
? NULL
: &vertex_index
,
3140 &const_index
, &indir_index
);
3142 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, vertex_index
, indir_index
,
3143 const_index
, location
, driver_location
,
3144 instr
->variables
[0]->var
->data
.location_frac
,
3145 instr
->num_components
,
3146 is_patch
, is_compact
, load_inputs
);
3147 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3150 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3151 nir_intrinsic_instr
*instr
)
3153 LLVMValueRef values
[8];
3154 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3155 int ve
= instr
->dest
.ssa
.num_components
;
3156 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3157 LLVMValueRef indir_index
;
3159 unsigned const_index
;
3160 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
3161 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3162 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3163 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3164 &const_index
, &indir_index
);
3166 if (instr
->dest
.ssa
.bit_size
== 64)
3169 switch (instr
->variables
[0]->var
->data
.mode
) {
3170 case nir_var_shader_in
:
3171 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3172 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3173 return load_tess_varyings(ctx
, instr
, true);
3176 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3177 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
3178 LLVMValueRef indir_index
;
3179 unsigned const_index
, vertex_index
;
3180 get_deref_offset(ctx
, instr
->variables
[0],
3181 false, &vertex_index
, NULL
,
3182 &const_index
, &indir_index
);
3184 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
3185 instr
->variables
[0]->var
->data
.driver_location
,
3186 instr
->variables
[0]->var
->data
.location_frac
, ve
,
3187 vertex_index
, const_index
, type
);
3190 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3192 unsigned count
= glsl_count_attribute_slots(
3193 instr
->variables
[0]->var
->type
,
3194 ctx
->stage
== MESA_SHADER_VERTEX
);
3196 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3197 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3198 stride
, false, true);
3200 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3204 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
3208 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3210 unsigned count
= glsl_count_attribute_slots(
3211 instr
->variables
[0]->var
->type
, false);
3213 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3214 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3215 stride
, true, true);
3217 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3221 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
3225 case nir_var_shared
: {
3226 LLVMValueRef address
= build_gep_for_deref(ctx
,
3227 instr
->variables
[0]);
3228 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3229 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3230 get_def_type(ctx
, &instr
->dest
.ssa
),
3233 case nir_var_shader_out
:
3234 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3235 return load_tess_varyings(ctx
, instr
, false);
3238 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3240 unsigned count
= glsl_count_attribute_slots(
3241 instr
->variables
[0]->var
->type
, false);
3243 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3244 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
3245 stride
, true, true);
3247 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3251 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3252 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
3258 unreachable("unhandle variable mode");
3260 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3261 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3265 visit_store_var(struct ac_nir_context
*ctx
,
3266 nir_intrinsic_instr
*instr
)
3268 LLVMValueRef temp_ptr
, value
;
3269 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3270 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3271 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3272 int writemask
= instr
->const_index
[0] << comp
;
3273 LLVMValueRef indir_index
;
3274 unsigned const_index
;
3275 get_deref_offset(ctx
, instr
->variables
[0], false,
3276 NULL
, NULL
, &const_index
, &indir_index
);
3278 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3280 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3281 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
3284 writemask
= widen_mask(writemask
, 2);
3287 switch (instr
->variables
[0]->var
->data
.mode
) {
3288 case nir_var_shader_out
:
3290 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3291 LLVMValueRef vertex_index
= NULL
;
3292 LLVMValueRef indir_index
= NULL
;
3293 unsigned const_index
= 0;
3294 const unsigned location
= instr
->variables
[0]->var
->data
.location
;
3295 const unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3296 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3297 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3298 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3300 get_deref_offset(ctx
, instr
->variables
[0],
3301 false, NULL
, is_patch
? NULL
: &vertex_index
,
3302 &const_index
, &indir_index
);
3304 ctx
->abi
->store_tcs_outputs(ctx
->abi
, vertex_index
, indir_index
,
3305 const_index
, location
, driver_location
,
3306 src
, comp
, is_patch
, is_compact
, writemask
);
3310 for (unsigned chan
= 0; chan
< 8; chan
++) {
3312 if (!(writemask
& (1 << chan
)))
3315 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3317 if (instr
->variables
[0]->var
->data
.compact
)
3320 unsigned count
= glsl_count_attribute_slots(
3321 instr
->variables
[0]->var
->type
, false);
3323 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3324 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
3325 stride
, true, true);
3327 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3328 value
, indir_index
, "");
3329 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
3330 count
, stride
, tmp_vec
);
3333 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
3335 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3340 for (unsigned chan
= 0; chan
< 8; chan
++) {
3341 if (!(writemask
& (1 << chan
)))
3344 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3346 unsigned count
= glsl_count_attribute_slots(
3347 instr
->variables
[0]->var
->type
, false);
3349 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3350 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3353 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3354 value
, indir_index
, "");
3355 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3358 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3360 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3364 case nir_var_shared
: {
3365 int writemask
= instr
->const_index
[0];
3366 LLVMValueRef address
= build_gep_for_deref(ctx
,
3367 instr
->variables
[0]);
3368 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3369 unsigned components
=
3370 glsl_get_vector_elements(
3371 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3372 if (writemask
== (1 << components
) - 1) {
3373 val
= LLVMBuildBitCast(
3374 ctx
->ac
.builder
, val
,
3375 LLVMGetElementType(LLVMTypeOf(address
)), "");
3376 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3378 for (unsigned chan
= 0; chan
< 4; chan
++) {
3379 if (!(writemask
& (1 << chan
)))
3382 LLVMBuildStructGEP(ctx
->ac
.builder
,
3384 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
3386 src
= LLVMBuildBitCast(
3387 ctx
->ac
.builder
, src
,
3388 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3389 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3399 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3402 case GLSL_SAMPLER_DIM_BUF
:
3404 case GLSL_SAMPLER_DIM_1D
:
3405 return array
? 2 : 1;
3406 case GLSL_SAMPLER_DIM_2D
:
3407 return array
? 3 : 2;
3408 case GLSL_SAMPLER_DIM_MS
:
3409 return array
? 4 : 3;
3410 case GLSL_SAMPLER_DIM_3D
:
3411 case GLSL_SAMPLER_DIM_CUBE
:
3413 case GLSL_SAMPLER_DIM_RECT
:
3414 case GLSL_SAMPLER_DIM_SUBPASS
:
3416 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3426 /* Adjust the sample index according to FMASK.
3428 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3429 * which is the identity mapping. Each nibble says which physical sample
3430 * should be fetched to get that sample.
3432 * For example, 0x11111100 means there are only 2 samples stored and
3433 * the second sample covers 3/4 of the pixel. When reading samples 0
3434 * and 1, return physical sample 0 (determined by the first two 0s
3435 * in FMASK), otherwise return physical sample 1.
3437 * The sample index should be adjusted as follows:
3438 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3440 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3441 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3442 LLVMValueRef coord_z
,
3443 LLVMValueRef sample_index
,
3444 LLVMValueRef fmask_desc_ptr
)
3446 LLVMValueRef fmask_load_address
[4];
3449 fmask_load_address
[0] = coord_x
;
3450 fmask_load_address
[1] = coord_y
;
3452 fmask_load_address
[2] = coord_z
;
3453 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3456 struct ac_image_args args
= {0};
3458 args
.opcode
= ac_image_load
;
3459 args
.da
= coord_z
? true : false;
3460 args
.resource
= fmask_desc_ptr
;
3462 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3464 res
= ac_build_image_opcode(ctx
, &args
);
3466 res
= ac_to_integer(ctx
, res
);
3467 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3468 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3470 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3474 LLVMValueRef sample_index4
=
3475 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3476 LLVMValueRef shifted_fmask
=
3477 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3478 LLVMValueRef final_sample
=
3479 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3481 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3482 * resource descriptor is 0 (invalid),
3484 LLVMValueRef fmask_desc
=
3485 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3488 LLVMValueRef fmask_word1
=
3489 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3492 LLVMValueRef word1_is_nonzero
=
3493 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3494 fmask_word1
, ctx
->i32_0
, "");
3496 /* Replace the MSAA sample index. */
3498 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3499 final_sample
, sample_index
, "");
3500 return sample_index
;
3503 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3504 const nir_intrinsic_instr
*instr
)
3506 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
3508 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3509 LLVMValueRef coords
[4];
3510 LLVMValueRef masks
[] = {
3511 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3512 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3515 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3518 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3519 bool is_array
= glsl_sampler_type_is_array(type
);
3520 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3521 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3522 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3523 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3524 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3525 count
= image_type_to_components_count(dim
, is_array
);
3528 LLVMValueRef fmask_load_address
[3];
3531 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3532 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3534 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3536 fmask_load_address
[2] = NULL
;
3538 for (chan
= 0; chan
< 2; ++chan
)
3539 fmask_load_address
[chan
] =
3540 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3541 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3542 ctx
->ac
.i32
, ""), "");
3543 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3545 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3546 fmask_load_address
[0],
3547 fmask_load_address
[1],
3548 fmask_load_address
[2],
3550 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3552 if (count
== 1 && !gfx9_1d
) {
3553 if (instr
->src
[0].ssa
->num_components
)
3554 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3561 for (chan
= 0; chan
< count
; ++chan
) {
3562 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3565 for (chan
= 0; chan
< 2; ++chan
)
3566 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3567 ctx
->ac
.i32
, ""), "");
3568 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3574 coords
[2] = coords
[1];
3575 coords
[1] = ctx
->ac
.i32_0
;
3577 coords
[1] = ctx
->ac
.i32_0
;
3582 coords
[count
] = sample_index
;
3587 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3590 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3595 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3596 const nir_intrinsic_instr
*instr
)
3598 LLVMValueRef params
[7];
3600 char intrinsic_name
[64];
3601 const nir_variable
*var
= instr
->variables
[0]->var
;
3602 const struct glsl_type
*type
= var
->type
;
3604 if(instr
->variables
[0]->deref
.child
)
3605 type
= instr
->variables
[0]->deref
.child
->type
;
3607 type
= glsl_without_array(type
);
3609 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3610 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3611 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
3612 unsigned num_channels
= util_last_bit(mask
);
3613 LLVMValueRef rsrc
, vindex
;
3615 rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3616 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3619 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
3620 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
3621 ctx
->ac
.i32_0
, num_channels
,
3623 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
3625 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3626 res
= ac_to_integer(&ctx
->ac
, res
);
3628 bool is_da
= glsl_sampler_type_is_array(type
) ||
3629 dim
== GLSL_SAMPLER_DIM_CUBE
||
3630 dim
== GLSL_SAMPLER_DIM_3D
||
3631 dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3632 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
;
3633 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3634 LLVMValueRef glc
= ctx
->ac
.i1false
;
3635 LLVMValueRef slc
= ctx
->ac
.i1false
;
3637 params
[0] = get_image_coords(ctx
, instr
);
3638 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3639 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3642 params
[5] = ctx
->ac
.i1false
;
3645 ac_get_image_intr_name("llvm.amdgcn.image.load",
3646 ctx
->ac
.v4f32
, /* vdata */
3647 LLVMTypeOf(params
[0]), /* coords */
3648 LLVMTypeOf(params
[1]), /* rsrc */
3649 intrinsic_name
, sizeof(intrinsic_name
));
3651 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3652 params
, 7, AC_FUNC_ATTR_READONLY
);
3654 return ac_to_integer(&ctx
->ac
, res
);
3657 static void visit_image_store(struct ac_nir_context
*ctx
,
3658 nir_intrinsic_instr
*instr
)
3660 LLVMValueRef params
[8];
3661 char intrinsic_name
[64];
3662 const nir_variable
*var
= instr
->variables
[0]->var
;
3663 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3664 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3665 LLVMValueRef glc
= ctx
->ac
.i1false
;
3666 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3668 glc
= ctx
->ac
.i1true
;
3670 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3671 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3672 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3673 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3674 ctx
->ac
.i32_0
, ""); /* vindex */
3675 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3676 params
[4] = glc
; /* glc */
3677 params
[5] = ctx
->ac
.i1false
; /* slc */
3678 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3681 bool is_da
= glsl_sampler_type_is_array(type
) ||
3682 dim
== GLSL_SAMPLER_DIM_CUBE
||
3683 dim
== GLSL_SAMPLER_DIM_3D
;
3684 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3685 LLVMValueRef slc
= ctx
->ac
.i1false
;
3687 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3688 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3689 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3690 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3693 params
[6] = ctx
->ac
.i1false
;
3696 ac_get_image_intr_name("llvm.amdgcn.image.store",
3697 LLVMTypeOf(params
[0]), /* vdata */
3698 LLVMTypeOf(params
[1]), /* coords */
3699 LLVMTypeOf(params
[2]), /* rsrc */
3700 intrinsic_name
, sizeof(intrinsic_name
));
3702 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3708 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3709 const nir_intrinsic_instr
*instr
)
3711 LLVMValueRef params
[7];
3712 int param_count
= 0;
3713 const nir_variable
*var
= instr
->variables
[0]->var
;
3715 const char *atomic_name
;
3716 char intrinsic_name
[41];
3717 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3718 MAYBE_UNUSED
int length
;
3720 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3722 switch (instr
->intrinsic
) {
3723 case nir_intrinsic_image_atomic_add
:
3724 atomic_name
= "add";
3726 case nir_intrinsic_image_atomic_min
:
3727 atomic_name
= is_unsigned
? "umin" : "smin";
3729 case nir_intrinsic_image_atomic_max
:
3730 atomic_name
= is_unsigned
? "umax" : "smax";
3732 case nir_intrinsic_image_atomic_and
:
3733 atomic_name
= "and";
3735 case nir_intrinsic_image_atomic_or
:
3738 case nir_intrinsic_image_atomic_xor
:
3739 atomic_name
= "xor";
3741 case nir_intrinsic_image_atomic_exchange
:
3742 atomic_name
= "swap";
3744 case nir_intrinsic_image_atomic_comp_swap
:
3745 atomic_name
= "cmpswap";
3751 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3752 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3753 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3755 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3756 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3758 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3759 ctx
->ac
.i32_0
, ""); /* vindex */
3760 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3761 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3763 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3764 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3766 char coords_type
[8];
3768 bool da
= glsl_sampler_type_is_array(type
) ||
3769 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3771 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3772 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3774 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3775 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3776 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3778 build_int_type_name(LLVMTypeOf(coords
),
3779 coords_type
, sizeof(coords_type
));
3781 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3782 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3785 assert(length
< sizeof(intrinsic_name
));
3786 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3789 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
3790 const nir_intrinsic_instr
*instr
)
3792 const nir_variable
*var
= instr
->variables
[0]->var
;
3793 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3794 bool da
= glsl_sampler_type_is_array(type
) ||
3795 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3796 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_3D
;
3798 struct ac_image_args args
= { 0 };
3801 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
3802 AC_DESC_IMAGE
, NULL
, true, false);
3803 args
.opcode
= ac_image_get_resinfo
;
3804 args
.addr
= ctx
->ac
.i32_0
;
3806 return ac_build_image_opcode(&ctx
->ac
, &args
);
3809 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3810 const nir_intrinsic_instr
*instr
)
3813 const nir_variable
*var
= instr
->variables
[0]->var
;
3814 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3815 bool da
= glsl_sampler_type_is_array(type
) ||
3816 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3817 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_3D
;
3819 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3820 return get_buffer_size(ctx
,
3821 get_sampler_desc(ctx
, instr
->variables
[0],
3822 AC_DESC_BUFFER
, NULL
, true, false), true);
3824 struct ac_image_args args
= { 0 };
3828 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3829 args
.opcode
= ac_image_get_resinfo
;
3830 args
.addr
= ctx
->ac
.i32_0
;
3832 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3834 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3836 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3837 glsl_sampler_type_is_array(type
)) {
3838 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3839 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3840 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3841 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3843 if (ctx
->ac
.chip_class
>= GFX9
&&
3844 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3845 glsl_sampler_type_is_array(type
)) {
3846 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3847 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3854 #define NOOP_WAITCNT 0xf7f
3855 #define LGKM_CNT 0x07f
3856 #define VM_CNT 0xf70
3858 static void emit_membar(struct ac_llvm_context
*ac
,
3859 const nir_intrinsic_instr
*instr
)
3861 unsigned waitcnt
= NOOP_WAITCNT
;
3863 switch (instr
->intrinsic
) {
3864 case nir_intrinsic_memory_barrier
:
3865 case nir_intrinsic_group_memory_barrier
:
3866 waitcnt
&= VM_CNT
& LGKM_CNT
;
3868 case nir_intrinsic_memory_barrier_atomic_counter
:
3869 case nir_intrinsic_memory_barrier_buffer
:
3870 case nir_intrinsic_memory_barrier_image
:
3873 case nir_intrinsic_memory_barrier_shared
:
3874 waitcnt
&= LGKM_CNT
;
3879 if (waitcnt
!= NOOP_WAITCNT
)
3880 ac_build_waitcnt(ac
, waitcnt
);
3883 static void emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3885 /* SI only (thanks to a hw bug workaround):
3886 * The real barrier instruction isn’t needed, because an entire patch
3887 * always fits into a single wave.
3889 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
3890 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
3893 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
3894 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3897 static void emit_discard(struct ac_nir_context
*ctx
,
3898 const nir_intrinsic_instr
*instr
)
3902 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3903 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3904 get_src(ctx
, instr
->src
[0]),
3907 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3908 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
3911 ac_build_kill_if_false(&ctx
->ac
, cond
);
3915 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
3917 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
3918 "llvm.amdgcn.ps.live",
3919 ctx
->ac
.i1
, NULL
, 0,
3920 AC_FUNC_ATTR_READNONE
);
3921 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
3922 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
3926 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
3928 LLVMValueRef result
;
3929 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3930 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
3931 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3933 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
3937 visit_load_shared(struct ac_nir_context
*ctx
,
3938 const nir_intrinsic_instr
*instr
)
3940 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
3942 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3944 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
3945 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3946 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
3947 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
3950 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
3951 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3955 visit_store_shared(struct ac_nir_context
*ctx
,
3956 const nir_intrinsic_instr
*instr
)
3958 LLVMValueRef derived_ptr
, data
,index
;
3959 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3961 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
3962 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3964 int writemask
= nir_intrinsic_write_mask(instr
);
3965 for (int chan
= 0; chan
< 4; chan
++) {
3966 if (!(writemask
& (1 << chan
))) {
3969 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3970 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3971 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3972 LLVMBuildStore(builder
, data
, derived_ptr
);
3976 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
3977 const nir_intrinsic_instr
*instr
,
3980 LLVMValueRef result
;
3981 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3983 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
||
3984 instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
) {
3985 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
3986 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
3988 LLVMAtomicOrderingSequentiallyConsistent
,
3989 LLVMAtomicOrderingSequentiallyConsistent
,
3992 LLVMAtomicRMWBinOp op
;
3993 switch (instr
->intrinsic
) {
3994 case nir_intrinsic_var_atomic_add
:
3995 case nir_intrinsic_shared_atomic_add
:
3996 op
= LLVMAtomicRMWBinOpAdd
;
3998 case nir_intrinsic_var_atomic_umin
:
3999 case nir_intrinsic_shared_atomic_umin
:
4000 op
= LLVMAtomicRMWBinOpUMin
;
4002 case nir_intrinsic_var_atomic_umax
:
4003 case nir_intrinsic_shared_atomic_umax
:
4004 op
= LLVMAtomicRMWBinOpUMax
;
4006 case nir_intrinsic_var_atomic_imin
:
4007 case nir_intrinsic_shared_atomic_imin
:
4008 op
= LLVMAtomicRMWBinOpMin
;
4010 case nir_intrinsic_var_atomic_imax
:
4011 case nir_intrinsic_shared_atomic_imax
:
4012 op
= LLVMAtomicRMWBinOpMax
;
4014 case nir_intrinsic_var_atomic_and
:
4015 case nir_intrinsic_shared_atomic_and
:
4016 op
= LLVMAtomicRMWBinOpAnd
;
4018 case nir_intrinsic_var_atomic_or
:
4019 case nir_intrinsic_shared_atomic_or
:
4020 op
= LLVMAtomicRMWBinOpOr
;
4022 case nir_intrinsic_var_atomic_xor
:
4023 case nir_intrinsic_shared_atomic_xor
:
4024 op
= LLVMAtomicRMWBinOpXor
;
4026 case nir_intrinsic_var_atomic_exchange
:
4027 case nir_intrinsic_shared_atomic_exchange
:
4028 op
= LLVMAtomicRMWBinOpXchg
;
4034 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
4035 LLVMAtomicOrderingSequentiallyConsistent
,
4041 static LLVMValueRef
lookup_interp_param(struct ac_shader_abi
*abi
,
4042 enum glsl_interp_mode interp
, unsigned location
)
4044 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4047 case INTERP_MODE_FLAT
:
4050 case INTERP_MODE_SMOOTH
:
4051 case INTERP_MODE_NONE
:
4052 if (location
== INTERP_CENTER
)
4053 return ctx
->persp_center
;
4054 else if (location
== INTERP_CENTROID
)
4055 return ctx
->persp_centroid
;
4056 else if (location
== INTERP_SAMPLE
)
4057 return ctx
->persp_sample
;
4059 case INTERP_MODE_NOPERSPECTIVE
:
4060 if (location
== INTERP_CENTER
)
4061 return ctx
->linear_center
;
4062 else if (location
== INTERP_CENTROID
)
4063 return ctx
->linear_centroid
;
4064 else if (location
== INTERP_SAMPLE
)
4065 return ctx
->linear_sample
;
4071 static LLVMValueRef
load_sample_position(struct ac_shader_abi
*abi
,
4072 LLVMValueRef sample_id
)
4074 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4076 LLVMValueRef result
;
4077 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
4079 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4080 ac_array_in_const_addr_space(ctx
->ac
.v2f32
), "");
4082 sample_id
= LLVMBuildAdd(ctx
->ac
.builder
, sample_id
, ctx
->sample_pos_offset
, "");
4083 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
4088 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
4090 LLVMValueRef values
[2];
4092 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0], 32);
4093 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1], 32);
4094 return ac_build_gather_values(&ctx
->ac
, values
, 2);
4097 static LLVMValueRef
load_sample_mask_in(struct ac_shader_abi
*abi
)
4099 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4100 uint8_t log2_ps_iter_samples
= ctx
->shader_info
->info
.ps
.force_persample
?
4101 ctx
->options
->key
.fs
.log2_num_samples
:
4102 ctx
->options
->key
.fs
.log2_ps_iter_samples
;
4104 /* The bit pattern matches that used by fixed function fragment
4106 static const uint16_t ps_iter_masks
[] = {
4107 0xffff, /* not used */
4113 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
4115 uint32_t ps_iter_mask
= ps_iter_masks
[log2_ps_iter_samples
];
4117 LLVMValueRef result
, sample_id
;
4118 sample_id
= unpack_param(&ctx
->ac
, abi
->ancillary
, 8, 4);
4119 sample_id
= LLVMBuildShl(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, ps_iter_mask
, false), sample_id
, "");
4120 result
= LLVMBuildAnd(ctx
->ac
.builder
, sample_id
, abi
->sample_coverage
, "");
4124 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
4125 const nir_intrinsic_instr
*instr
)
4127 LLVMValueRef result
[4];
4128 LLVMValueRef interp_param
, attr_number
;
4131 LLVMValueRef src_c0
= NULL
;
4132 LLVMValueRef src_c1
= NULL
;
4133 LLVMValueRef src0
= NULL
;
4134 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
4135 switch (instr
->intrinsic
) {
4136 case nir_intrinsic_interp_var_at_centroid
:
4137 location
= INTERP_CENTROID
;
4139 case nir_intrinsic_interp_var_at_sample
:
4140 case nir_intrinsic_interp_var_at_offset
:
4141 location
= INTERP_CENTER
;
4142 src0
= get_src(ctx
, instr
->src
[0]);
4148 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
4149 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
4150 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
4151 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
4152 LLVMValueRef sample_position
;
4153 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
4155 /* fetch sample ID */
4156 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
4158 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
4159 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
4160 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
4161 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
4163 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, instr
->variables
[0]->var
->data
.interpolation
, location
);
4164 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
4166 if (location
== INTERP_CENTER
) {
4167 LLVMValueRef ij_out
[2];
4168 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
4171 * take the I then J parameters, and the DDX/Y for it, and
4172 * calculate the IJ inputs for the interpolator.
4173 * temp1 = ddx * offset/sample.x + I;
4174 * interp_param.I = ddy * offset/sample.y + temp1;
4175 * temp1 = ddx * offset/sample.x + J;
4176 * interp_param.J = ddy * offset/sample.y + temp1;
4178 for (unsigned i
= 0; i
< 2; i
++) {
4179 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
4180 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
4181 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4182 ddxy_out
, ix_ll
, "");
4183 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4184 ddxy_out
, iy_ll
, "");
4185 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4186 interp_param
, ix_ll
, "");
4187 LLVMValueRef temp1
, temp2
;
4189 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
4192 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
4193 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
4195 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
4196 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
4198 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4199 temp2
, ctx
->ac
.i32
, "");
4201 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
4205 for (chan
= 0; chan
< 4; chan
++) {
4206 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
4209 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
4210 interp_param
, ctx
->ac
.v2f32
, "");
4211 LLVMValueRef i
= LLVMBuildExtractElement(
4212 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
4213 LLVMValueRef j
= LLVMBuildExtractElement(
4214 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
4216 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4217 llvm_chan
, attr_number
,
4218 ctx
->abi
->prim_mask
, i
, j
);
4220 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4221 LLVMConstInt(ctx
->ac
.i32
, 2, false),
4222 llvm_chan
, attr_number
,
4223 ctx
->abi
->prim_mask
);
4226 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
4227 instr
->variables
[0]->var
->data
.location_frac
);
4231 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
4233 LLVMValueRef gs_next_vertex
;
4234 LLVMValueRef can_emit
;
4236 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4238 assert(stream
== 0);
4240 /* Write vertex attribute values to GSVS ring */
4241 gs_next_vertex
= LLVMBuildLoad(ctx
->ac
.builder
,
4242 ctx
->gs_next_vertex
,
4245 /* If this thread has already emitted the declared maximum number of
4246 * vertices, kill it: excessive vertex emissions are not supposed to
4247 * have any effect, and GS threads have no externally observable
4248 * effects other than emitting vertices.
4250 can_emit
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, gs_next_vertex
,
4251 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
4252 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4254 /* loop num outputs */
4256 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
4257 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
4262 if (!(ctx
->output_mask
& (1ull << i
)))
4265 if (i
== VARYING_SLOT_CLIP_DIST0
) {
4266 /* pack clip and cull into a single set of slots */
4267 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4271 for (unsigned j
= 0; j
< length
; j
++) {
4272 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
,
4274 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
4275 voffset
= LLVMBuildAdd(ctx
->ac
.builder
, voffset
, gs_next_vertex
, "");
4276 voffset
= LLVMBuildMul(ctx
->ac
.builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
4278 out_val
= LLVMBuildBitCast(ctx
->ac
.builder
, out_val
, ctx
->ac
.i32
, "");
4280 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
4282 voffset
, ctx
->gs2vs_offset
, 0,
4288 gs_next_vertex
= LLVMBuildAdd(ctx
->ac
.builder
, gs_next_vertex
,
4290 LLVMBuildStore(ctx
->ac
.builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4292 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4296 visit_end_primitive(struct ac_shader_abi
*abi
, unsigned stream
)
4298 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4299 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8), ctx
->gs_wave_id
);
4303 load_tess_coord(struct ac_shader_abi
*abi
)
4305 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4307 LLVMValueRef coord
[4] = {
4314 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4315 coord
[2] = LLVMBuildFSub(ctx
->ac
.builder
, ctx
->ac
.f32_1
,
4316 LLVMBuildFAdd(ctx
->ac
.builder
, coord
[0], coord
[1], ""), "");
4318 return ac_build_gather_values(&ctx
->ac
, coord
, 3);
4322 load_patch_vertices_in(struct ac_shader_abi
*abi
)
4324 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4325 return LLVMConstInt(ctx
->ac
.i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
4328 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4329 nir_intrinsic_instr
*instr
)
4331 LLVMValueRef result
= NULL
;
4333 switch (instr
->intrinsic
) {
4334 case nir_intrinsic_ballot
:
4335 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4337 case nir_intrinsic_read_invocation
:
4338 case nir_intrinsic_read_first_invocation
: {
4339 LLVMValueRef args
[2];
4342 args
[0] = get_src(ctx
, instr
->src
[0]);
4345 const char *intr_name
;
4346 if (instr
->intrinsic
== nir_intrinsic_read_invocation
) {
4348 intr_name
= "llvm.amdgcn.readlane";
4351 args
[1] = get_src(ctx
, instr
->src
[1]);
4354 intr_name
= "llvm.amdgcn.readfirstlane";
4357 /* We currently have no other way to prevent LLVM from lifting the icmp
4358 * calls to a dominating basic block.
4360 ac_build_optimization_barrier(&ctx
->ac
, &args
[0]);
4362 result
= ac_build_intrinsic(&ctx
->ac
, intr_name
,
4363 ctx
->ac
.i32
, args
, num_args
,
4364 AC_FUNC_ATTR_READNONE
|
4365 AC_FUNC_ATTR_CONVERGENT
);
4368 case nir_intrinsic_load_subgroup_invocation
:
4369 result
= ac_get_thread_id(&ctx
->ac
);
4371 case nir_intrinsic_load_work_group_id
: {
4372 LLVMValueRef values
[3];
4374 for (int i
= 0; i
< 3; i
++) {
4375 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
4376 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
4379 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
4382 case nir_intrinsic_load_base_vertex
: {
4383 result
= ctx
->abi
->base_vertex
;
4386 case nir_intrinsic_load_local_group_size
:
4387 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
4389 case nir_intrinsic_load_vertex_id_zero_base
: {
4390 result
= ctx
->abi
->vertex_id
;
4393 case nir_intrinsic_load_local_invocation_id
: {
4394 result
= ctx
->abi
->local_invocation_ids
;
4397 case nir_intrinsic_load_base_instance
:
4398 result
= ctx
->abi
->start_instance
;
4400 case nir_intrinsic_load_draw_id
:
4401 result
= ctx
->abi
->draw_id
;
4403 case nir_intrinsic_load_view_index
:
4404 result
= ctx
->abi
->view_index
;
4406 case nir_intrinsic_load_invocation_id
:
4407 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4408 result
= unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
4410 result
= ctx
->abi
->gs_invocation_id
;
4412 case nir_intrinsic_load_primitive_id
:
4413 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4414 result
= ctx
->abi
->gs_prim_id
;
4415 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4416 result
= ctx
->abi
->tcs_patch_id
;
4417 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4418 result
= ctx
->abi
->tes_patch_id
;
4420 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4422 case nir_intrinsic_load_sample_id
:
4423 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4425 case nir_intrinsic_load_sample_pos
:
4426 result
= load_sample_pos(ctx
);
4428 case nir_intrinsic_load_sample_mask_in
:
4429 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
4431 case nir_intrinsic_load_frag_coord
: {
4432 LLVMValueRef values
[4] = {
4433 ctx
->abi
->frag_pos
[0],
4434 ctx
->abi
->frag_pos
[1],
4435 ctx
->abi
->frag_pos
[2],
4436 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4438 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4441 case nir_intrinsic_load_front_face
:
4442 result
= ctx
->abi
->front_face
;
4444 case nir_intrinsic_load_helper_invocation
:
4445 result
= visit_load_helper_invocation(ctx
);
4447 case nir_intrinsic_load_instance_id
:
4448 result
= ctx
->abi
->instance_id
;
4450 case nir_intrinsic_load_num_work_groups
:
4451 result
= ctx
->abi
->num_work_groups
;
4453 case nir_intrinsic_load_local_invocation_index
:
4454 result
= visit_load_local_invocation_index(ctx
);
4456 case nir_intrinsic_load_push_constant
:
4457 result
= visit_load_push_constant(ctx
, instr
);
4459 case nir_intrinsic_vulkan_resource_index
: {
4460 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
4461 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
4462 unsigned binding
= nir_intrinsic_binding(instr
);
4464 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
4468 case nir_intrinsic_vulkan_resource_reindex
:
4469 result
= visit_vulkan_resource_reindex(ctx
, instr
);
4471 case nir_intrinsic_store_ssbo
:
4472 visit_store_ssbo(ctx
, instr
);
4474 case nir_intrinsic_load_ssbo
:
4475 result
= visit_load_buffer(ctx
, instr
);
4477 case nir_intrinsic_ssbo_atomic_add
:
4478 case nir_intrinsic_ssbo_atomic_imin
:
4479 case nir_intrinsic_ssbo_atomic_umin
:
4480 case nir_intrinsic_ssbo_atomic_imax
:
4481 case nir_intrinsic_ssbo_atomic_umax
:
4482 case nir_intrinsic_ssbo_atomic_and
:
4483 case nir_intrinsic_ssbo_atomic_or
:
4484 case nir_intrinsic_ssbo_atomic_xor
:
4485 case nir_intrinsic_ssbo_atomic_exchange
:
4486 case nir_intrinsic_ssbo_atomic_comp_swap
:
4487 result
= visit_atomic_ssbo(ctx
, instr
);
4489 case nir_intrinsic_load_ubo
:
4490 result
= visit_load_ubo_buffer(ctx
, instr
);
4492 case nir_intrinsic_get_buffer_size
:
4493 result
= visit_get_buffer_size(ctx
, instr
);
4495 case nir_intrinsic_load_var
:
4496 result
= visit_load_var(ctx
, instr
);
4498 case nir_intrinsic_store_var
:
4499 visit_store_var(ctx
, instr
);
4501 case nir_intrinsic_load_shared
:
4502 result
= visit_load_shared(ctx
, instr
);
4504 case nir_intrinsic_store_shared
:
4505 visit_store_shared(ctx
, instr
);
4507 case nir_intrinsic_image_samples
:
4508 result
= visit_image_samples(ctx
, instr
);
4510 case nir_intrinsic_image_load
:
4511 result
= visit_image_load(ctx
, instr
);
4513 case nir_intrinsic_image_store
:
4514 visit_image_store(ctx
, instr
);
4516 case nir_intrinsic_image_atomic_add
:
4517 case nir_intrinsic_image_atomic_min
:
4518 case nir_intrinsic_image_atomic_max
:
4519 case nir_intrinsic_image_atomic_and
:
4520 case nir_intrinsic_image_atomic_or
:
4521 case nir_intrinsic_image_atomic_xor
:
4522 case nir_intrinsic_image_atomic_exchange
:
4523 case nir_intrinsic_image_atomic_comp_swap
:
4524 result
= visit_image_atomic(ctx
, instr
);
4526 case nir_intrinsic_image_size
:
4527 result
= visit_image_size(ctx
, instr
);
4529 case nir_intrinsic_shader_clock
:
4530 result
= ac_build_shader_clock(&ctx
->ac
);
4532 case nir_intrinsic_discard
:
4533 case nir_intrinsic_discard_if
:
4534 emit_discard(ctx
, instr
);
4536 case nir_intrinsic_memory_barrier
:
4537 case nir_intrinsic_group_memory_barrier
:
4538 case nir_intrinsic_memory_barrier_atomic_counter
:
4539 case nir_intrinsic_memory_barrier_buffer
:
4540 case nir_intrinsic_memory_barrier_image
:
4541 case nir_intrinsic_memory_barrier_shared
:
4542 emit_membar(&ctx
->ac
, instr
);
4544 case nir_intrinsic_barrier
:
4545 emit_barrier(&ctx
->ac
, ctx
->stage
);
4547 case nir_intrinsic_shared_atomic_add
:
4548 case nir_intrinsic_shared_atomic_imin
:
4549 case nir_intrinsic_shared_atomic_umin
:
4550 case nir_intrinsic_shared_atomic_imax
:
4551 case nir_intrinsic_shared_atomic_umax
:
4552 case nir_intrinsic_shared_atomic_and
:
4553 case nir_intrinsic_shared_atomic_or
:
4554 case nir_intrinsic_shared_atomic_xor
:
4555 case nir_intrinsic_shared_atomic_exchange
:
4556 case nir_intrinsic_shared_atomic_comp_swap
: {
4557 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
4558 result
= visit_var_atomic(ctx
, instr
, ptr
);
4561 case nir_intrinsic_var_atomic_add
:
4562 case nir_intrinsic_var_atomic_imin
:
4563 case nir_intrinsic_var_atomic_umin
:
4564 case nir_intrinsic_var_atomic_imax
:
4565 case nir_intrinsic_var_atomic_umax
:
4566 case nir_intrinsic_var_atomic_and
:
4567 case nir_intrinsic_var_atomic_or
:
4568 case nir_intrinsic_var_atomic_xor
:
4569 case nir_intrinsic_var_atomic_exchange
:
4570 case nir_intrinsic_var_atomic_comp_swap
: {
4571 LLVMValueRef ptr
= build_gep_for_deref(ctx
, instr
->variables
[0]);
4572 result
= visit_var_atomic(ctx
, instr
, ptr
);
4575 case nir_intrinsic_interp_var_at_centroid
:
4576 case nir_intrinsic_interp_var_at_sample
:
4577 case nir_intrinsic_interp_var_at_offset
:
4578 result
= visit_interp(ctx
, instr
);
4580 case nir_intrinsic_emit_vertex
:
4581 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
4583 case nir_intrinsic_end_primitive
:
4584 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
4586 case nir_intrinsic_load_tess_coord
:
4587 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
4589 case nir_intrinsic_load_tess_level_outer
:
4590 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
4592 case nir_intrinsic_load_tess_level_inner
:
4593 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
4595 case nir_intrinsic_load_patch_vertices_in
:
4596 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
4598 case nir_intrinsic_vote_all
: {
4599 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4600 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4603 case nir_intrinsic_vote_any
: {
4604 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4605 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4608 case nir_intrinsic_vote_eq
: {
4609 LLVMValueRef tmp
= ac_build_vote_eq(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4610 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4614 fprintf(stderr
, "Unknown intrinsic: ");
4615 nir_print_instr(&instr
->instr
, stderr
);
4616 fprintf(stderr
, "\n");
4620 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4624 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4625 LLVMValueRef buffer_ptr
, bool write
)
4627 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4628 LLVMValueRef result
;
4630 LLVMSetMetadata(buffer_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
4632 result
= LLVMBuildLoad(ctx
->ac
.builder
, buffer_ptr
, "");
4633 LLVMSetMetadata(result
, ctx
->ac
.invariant_load_md_kind
, ctx
->ac
.empty_md
);
4638 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef buffer_ptr
)
4640 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4641 LLVMValueRef result
;
4643 LLVMSetMetadata(buffer_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
4645 result
= LLVMBuildLoad(ctx
->ac
.builder
, buffer_ptr
, "");
4646 LLVMSetMetadata(result
, ctx
->ac
.invariant_load_md_kind
, ctx
->ac
.empty_md
);
4651 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4652 unsigned descriptor_set
,
4653 unsigned base_index
,
4654 unsigned constant_index
,
4656 enum ac_descriptor_type desc_type
,
4657 bool image
, bool write
)
4659 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
4660 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4661 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4662 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4663 unsigned offset
= binding
->offset
;
4664 unsigned stride
= binding
->size
;
4666 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4669 assert(base_index
< layout
->binding_count
);
4671 switch (desc_type
) {
4673 type
= ctx
->ac
.v8i32
;
4677 type
= ctx
->ac
.v8i32
;
4681 case AC_DESC_SAMPLER
:
4682 type
= ctx
->ac
.v4i32
;
4683 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4688 case AC_DESC_BUFFER
:
4689 type
= ctx
->ac
.v4i32
;
4693 unreachable("invalid desc_type\n");
4696 offset
+= constant_index
* stride
;
4698 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4699 (!index
|| binding
->immutable_samplers_equal
)) {
4700 if (binding
->immutable_samplers_equal
)
4703 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4705 LLVMValueRef constants
[] = {
4706 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4707 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4708 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4709 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4711 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4714 assert(stride
% type_size
== 0);
4717 index
= ctx
->ac
.i32_0
;
4719 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4721 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4722 list
= LLVMBuildPointerCast(builder
, list
, ac_array_in_const_addr_space(type
), "");
4724 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4727 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4728 const nir_deref_var
*deref
,
4729 enum ac_descriptor_type desc_type
,
4730 const nir_tex_instr
*tex_instr
,
4731 bool image
, bool write
)
4733 LLVMValueRef index
= NULL
;
4734 unsigned constant_index
= 0;
4735 unsigned descriptor_set
;
4736 unsigned base_index
;
4739 assert(tex_instr
&& !image
);
4741 base_index
= tex_instr
->sampler_index
;
4743 const nir_deref
*tail
= &deref
->deref
;
4744 while (tail
->child
) {
4745 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4746 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4751 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4753 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4754 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4756 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4757 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4762 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4765 constant_index
+= child
->base_offset
* array_size
;
4767 tail
= &child
->deref
;
4769 descriptor_set
= deref
->var
->data
.descriptor_set
;
4770 base_index
= deref
->var
->data
.binding
;
4773 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4776 constant_index
, index
,
4777 desc_type
, image
, write
);
4780 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4781 struct ac_image_args
*args
,
4782 const nir_tex_instr
*instr
,
4784 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4785 LLVMValueRef
*param
, unsigned count
,
4788 unsigned is_rect
= 0;
4789 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4791 if (op
== nir_texop_lod
)
4793 /* Pad to power of two vector */
4794 while (count
< util_next_power_of_two(count
))
4795 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4798 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4800 args
->addr
= param
[0];
4802 args
->resource
= res_ptr
;
4803 args
->sampler
= samp_ptr
;
4805 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4806 args
->addr
= param
[0];
4810 args
->dmask
= dmask
;
4811 args
->unorm
= is_rect
;
4815 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4818 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4819 * filtering manually. The driver sets img7 to a mask clearing
4820 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4821 * s_and_b32 samp0, samp0, img7
4824 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4826 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4827 LLVMValueRef res
, LLVMValueRef samp
)
4829 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4830 LLVMValueRef img7
, samp0
;
4832 if (ctx
->ac
.chip_class
>= VI
)
4835 img7
= LLVMBuildExtractElement(builder
, res
,
4836 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4837 samp0
= LLVMBuildExtractElement(builder
, samp
,
4838 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4839 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4840 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4841 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4844 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4845 nir_tex_instr
*instr
,
4846 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4847 LLVMValueRef
*fmask_ptr
)
4849 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4850 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4852 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4855 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4857 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4858 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4859 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4861 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4862 instr
->op
== nir_texop_samples_identical
))
4863 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4866 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4869 coord
= ac_to_float(ctx
, coord
);
4870 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4871 coord
= ac_to_integer(ctx
, coord
);
4875 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4877 LLVMValueRef result
= NULL
;
4878 struct ac_image_args args
= { 0 };
4879 unsigned dmask
= 0xf;
4880 LLVMValueRef address
[16];
4881 LLVMValueRef coords
[5];
4882 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4883 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4884 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4885 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4886 LLVMValueRef derivs
[6];
4887 unsigned chan
, count
= 0;
4888 unsigned const_src
= 0, num_deriv_comp
= 0;
4889 bool lod_is_zero
= false;
4891 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4893 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4894 switch (instr
->src
[i
].src_type
) {
4895 case nir_tex_src_coord
:
4896 coord
= get_src(ctx
, instr
->src
[i
].src
);
4898 case nir_tex_src_projector
:
4900 case nir_tex_src_comparator
:
4901 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4903 case nir_tex_src_offset
:
4904 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4907 case nir_tex_src_bias
:
4908 bias
= get_src(ctx
, instr
->src
[i
].src
);
4910 case nir_tex_src_lod
: {
4911 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4913 if (val
&& val
->i32
[0] == 0)
4915 lod
= get_src(ctx
, instr
->src
[i
].src
);
4918 case nir_tex_src_ms_index
:
4919 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4921 case nir_tex_src_ms_mcs
:
4923 case nir_tex_src_ddx
:
4924 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4925 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4927 case nir_tex_src_ddy
:
4928 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4930 case nir_tex_src_texture_offset
:
4931 case nir_tex_src_sampler_offset
:
4932 case nir_tex_src_plane
:
4938 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4939 result
= get_buffer_size(ctx
, res_ptr
, true);
4943 if (instr
->op
== nir_texop_texture_samples
) {
4944 LLVMValueRef res
, samples
, is_msaa
;
4945 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4946 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4947 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4948 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4949 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4950 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4951 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4952 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4953 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4955 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4956 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4957 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4958 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4959 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4961 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4968 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4969 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4971 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4972 LLVMValueRef offset
[3], pack
;
4973 for (chan
= 0; chan
< 3; ++chan
)
4974 offset
[chan
] = ctx
->ac
.i32_0
;
4977 for (chan
= 0; chan
< ac_get_llvm_num_components(offsets
); chan
++) {
4978 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4979 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4980 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4982 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4983 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4985 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4986 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4987 address
[count
++] = pack
;
4990 /* pack LOD bias value */
4991 if (instr
->op
== nir_texop_txb
&& bias
) {
4992 address
[count
++] = bias
;
4995 /* Pack depth comparison value */
4996 if (instr
->is_shadow
&& comparator
) {
4997 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4998 ac_llvm_extract_elem(&ctx
->ac
, comparator
, 0));
5000 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
5001 * so the depth comparison value isn't clamped for Z16 and
5002 * Z24 anymore. Do it manually here.
5004 * It's unnecessary if the original texture format was
5005 * Z32_FLOAT, but we don't know that here.
5007 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
5008 z
= ac_build_clamp(&ctx
->ac
, z
);
5010 address
[count
++] = z
;
5013 /* pack derivatives */
5015 int num_src_deriv_channels
, num_dest_deriv_channels
;
5016 switch (instr
->sampler_dim
) {
5017 case GLSL_SAMPLER_DIM_3D
:
5018 case GLSL_SAMPLER_DIM_CUBE
:
5020 num_src_deriv_channels
= 3;
5021 num_dest_deriv_channels
= 3;
5023 case GLSL_SAMPLER_DIM_2D
:
5025 num_src_deriv_channels
= 2;
5026 num_dest_deriv_channels
= 2;
5029 case GLSL_SAMPLER_DIM_1D
:
5030 num_src_deriv_channels
= 1;
5031 if (ctx
->ac
.chip_class
>= GFX9
) {
5032 num_dest_deriv_channels
= 2;
5035 num_dest_deriv_channels
= 1;
5041 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
5042 derivs
[i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
5043 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
5045 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
5046 derivs
[i
] = ctx
->ac
.f32_0
;
5047 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
5051 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
5052 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
5053 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
5054 if (instr
->coord_components
== 3)
5055 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5056 ac_prepare_cube_coords(&ctx
->ac
,
5057 instr
->op
== nir_texop_txd
, instr
->is_array
,
5058 instr
->op
== nir_texop_lod
, coords
, derivs
);
5064 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
5065 address
[count
++] = derivs
[i
];
5068 /* Pack texture coordinates */
5070 address
[count
++] = coords
[0];
5071 if (instr
->coord_components
> 1) {
5072 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
5073 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
5075 address
[count
++] = coords
[1];
5077 if (instr
->coord_components
> 2) {
5078 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
5079 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
5080 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
5081 instr
->op
!= nir_texop_txf
) {
5082 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
5084 address
[count
++] = coords
[2];
5087 if (ctx
->ac
.chip_class
>= GFX9
) {
5088 LLVMValueRef filler
;
5089 if (instr
->op
== nir_texop_txf
)
5090 filler
= ctx
->ac
.i32_0
;
5092 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
5094 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
5095 /* No nir_texop_lod, because it does not take a slice
5096 * even with array textures. */
5097 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
5098 address
[count
] = address
[count
- 1];
5099 address
[count
- 1] = filler
;
5102 address
[count
++] = filler
;
5108 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
5109 instr
->op
== nir_texop_txf
)) {
5110 address
[count
++] = lod
;
5111 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
5112 address
[count
++] = sample_index
;
5113 } else if(instr
->op
== nir_texop_txs
) {
5116 address
[count
++] = lod
;
5118 address
[count
++] = ctx
->ac
.i32_0
;
5121 for (chan
= 0; chan
< count
; chan
++) {
5122 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
5123 address
[chan
], ctx
->ac
.i32
, "");
5126 if (instr
->op
== nir_texop_samples_identical
) {
5127 LLVMValueRef txf_address
[4];
5128 struct ac_image_args txf_args
= { 0 };
5129 unsigned txf_count
= count
;
5130 memcpy(txf_address
, address
, sizeof(txf_address
));
5132 if (!instr
->is_array
)
5133 txf_address
[2] = ctx
->ac
.i32_0
;
5134 txf_address
[3] = ctx
->ac
.i32_0
;
5136 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
5138 txf_address
, txf_count
, 0xf);
5140 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
5142 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
5143 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
5147 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
5148 instr
->op
!= nir_texop_txs
) {
5149 unsigned sample_chan
= instr
->is_array
? 3 : 2;
5150 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
5153 instr
->is_array
? address
[2] : NULL
,
5154 address
[sample_chan
],
5158 if (offsets
&& instr
->op
== nir_texop_txf
) {
5159 nir_const_value
*const_offset
=
5160 nir_src_as_const_value(instr
->src
[const_src
].src
);
5161 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
5162 assert(const_offset
);
5163 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
5164 if (num_offsets
> 2)
5165 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
5166 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
5167 if (num_offsets
> 1)
5168 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
5169 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
5170 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
5171 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
5175 /* TODO TG4 support */
5176 if (instr
->op
== nir_texop_tg4
) {
5177 if (instr
->is_shadow
)
5180 dmask
= 1 << instr
->component
;
5182 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
5183 res_ptr
, samp_ptr
, address
, count
, dmask
);
5185 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
5187 if (instr
->op
== nir_texop_query_levels
)
5188 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
5189 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
5190 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
5191 instr
->op
!= nir_texop_tg4
)
5192 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
5193 else if (instr
->op
== nir_texop_txs
&&
5194 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
5196 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
5197 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
5198 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
5199 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
5200 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
5201 } else if (ctx
->ac
.chip_class
>= GFX9
&&
5202 instr
->op
== nir_texop_txs
&&
5203 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
5205 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
5206 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
5207 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
5209 } else if (instr
->dest
.ssa
.num_components
!= 4)
5210 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
5214 assert(instr
->dest
.is_ssa
);
5215 result
= ac_to_integer(&ctx
->ac
, result
);
5216 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
5221 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
5223 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
5224 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
5226 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
5227 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
5230 static void visit_post_phi(struct ac_nir_context
*ctx
,
5231 nir_phi_instr
*instr
,
5232 LLVMValueRef llvm_phi
)
5234 nir_foreach_phi_src(src
, instr
) {
5235 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
5236 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
5238 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
5242 static void phi_post_pass(struct ac_nir_context
*ctx
)
5244 struct hash_entry
*entry
;
5245 hash_table_foreach(ctx
->phis
, entry
) {
5246 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
5247 (LLVMValueRef
)entry
->data
);
5252 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
5253 const nir_ssa_undef_instr
*instr
)
5255 unsigned num_components
= instr
->def
.num_components
;
5256 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
5259 if (num_components
== 1)
5260 undef
= LLVMGetUndef(type
);
5262 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
5264 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
5267 static void visit_jump(struct ac_nir_context
*ctx
,
5268 const nir_jump_instr
*instr
)
5270 switch (instr
->type
) {
5271 case nir_jump_break
:
5272 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
5273 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5275 case nir_jump_continue
:
5276 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5277 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5280 fprintf(stderr
, "Unknown NIR jump instr: ");
5281 nir_print_instr(&instr
->instr
, stderr
);
5282 fprintf(stderr
, "\n");
5287 static void visit_cf_list(struct ac_nir_context
*ctx
,
5288 struct exec_list
*list
);
5290 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5292 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
5293 nir_foreach_instr(instr
, block
)
5295 switch (instr
->type
) {
5296 case nir_instr_type_alu
:
5297 visit_alu(ctx
, nir_instr_as_alu(instr
));
5299 case nir_instr_type_load_const
:
5300 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5302 case nir_instr_type_intrinsic
:
5303 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5305 case nir_instr_type_tex
:
5306 visit_tex(ctx
, nir_instr_as_tex(instr
));
5308 case nir_instr_type_phi
:
5309 visit_phi(ctx
, nir_instr_as_phi(instr
));
5311 case nir_instr_type_ssa_undef
:
5312 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5314 case nir_instr_type_jump
:
5315 visit_jump(ctx
, nir_instr_as_jump(instr
));
5318 fprintf(stderr
, "Unknown NIR instr type: ");
5319 nir_print_instr(instr
, stderr
);
5320 fprintf(stderr
, "\n");
5325 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
5328 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5330 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5332 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5333 LLVMBasicBlockRef merge_block
=
5334 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5335 LLVMBasicBlockRef if_block
=
5336 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5337 LLVMBasicBlockRef else_block
= merge_block
;
5338 if (!exec_list_is_empty(&if_stmt
->else_list
))
5339 else_block
= LLVMAppendBasicBlockInContext(
5340 ctx
->ac
.context
, fn
, "");
5342 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
5344 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
5346 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
5347 visit_cf_list(ctx
, &if_stmt
->then_list
);
5348 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5349 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5351 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5352 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
5353 visit_cf_list(ctx
, &if_stmt
->else_list
);
5354 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5355 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5358 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
5361 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5363 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5364 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
5365 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
5367 ctx
->continue_block
=
5368 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5370 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5372 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5373 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
5374 visit_cf_list(ctx
, &loop
->body
);
5376 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5377 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5378 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
5380 ctx
->continue_block
= continue_parent
;
5381 ctx
->break_block
= break_parent
;
5384 static void visit_cf_list(struct ac_nir_context
*ctx
,
5385 struct exec_list
*list
)
5387 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5389 switch (node
->type
) {
5390 case nir_cf_node_block
:
5391 visit_block(ctx
, nir_cf_node_as_block(node
));
5394 case nir_cf_node_if
:
5395 visit_if(ctx
, nir_cf_node_as_if(node
));
5398 case nir_cf_node_loop
:
5399 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5409 handle_vs_input_decl(struct radv_shader_context
*ctx
,
5410 struct nir_variable
*variable
)
5412 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5413 LLVMValueRef t_offset
;
5414 LLVMValueRef t_list
;
5416 LLVMValueRef buffer_index
;
5417 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5418 int idx
= variable
->data
.location
;
5419 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5420 uint8_t input_usage_mask
=
5421 ctx
->shader_info
->info
.vs
.input_usage_mask
[variable
->data
.location
];
5422 unsigned num_channels
= util_last_bit(input_usage_mask
);
5424 variable
->data
.driver_location
= idx
* 4;
5426 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5427 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << (index
+ i
))) {
5428 buffer_index
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
.instance_id
,
5429 ctx
->abi
.start_instance
, "");
5430 if (ctx
->options
->key
.vs
.as_ls
) {
5431 ctx
->shader_info
->vs
.vgpr_comp_cnt
=
5432 MAX2(2, ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5434 ctx
->shader_info
->vs
.vgpr_comp_cnt
=
5435 MAX2(1, ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5438 buffer_index
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
.vertex_id
,
5439 ctx
->abi
.base_vertex
, "");
5440 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5442 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5444 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5447 num_channels
, false, true);
5449 input
= ac_build_expand_to_vec4(&ctx
->ac
, input
, num_channels
);
5451 for (unsigned chan
= 0; chan
< 4; chan
++) {
5452 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5453 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5454 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
,
5455 input
, llvm_chan
, ""));
5460 static void interp_fs_input(struct radv_shader_context
*ctx
,
5462 LLVMValueRef interp_param
,
5463 LLVMValueRef prim_mask
,
5464 LLVMValueRef result
[4])
5466 LLVMValueRef attr_number
;
5469 bool interp
= interp_param
!= NULL
;
5471 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5473 /* fs.constant returns the param from the middle vertex, so it's not
5474 * really useful for flat shading. It's meant to be used for custom
5475 * interpolation (but the intrinsic can't fetch from the other two
5478 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5479 * to do the right thing. The only reason we use fs.constant is that
5480 * fs.interp cannot be used on integers, because they can be equal
5484 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
,
5487 i
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
5489 j
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
5493 for (chan
= 0; chan
< 4; chan
++) {
5494 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5497 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5502 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5503 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5512 handle_fs_input_decl(struct radv_shader_context
*ctx
,
5513 struct nir_variable
*variable
)
5515 int idx
= variable
->data
.location
;
5516 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5517 LLVMValueRef interp
;
5519 variable
->data
.driver_location
= idx
* 4;
5520 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5522 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5523 unsigned interp_type
;
5524 if (variable
->data
.sample
)
5525 interp_type
= INTERP_SAMPLE
;
5526 else if (variable
->data
.centroid
)
5527 interp_type
= INTERP_CENTROID
;
5529 interp_type
= INTERP_CENTER
;
5531 interp
= lookup_interp_param(&ctx
->abi
, variable
->data
.interpolation
, interp_type
);
5535 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5536 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5541 handle_vs_inputs(struct radv_shader_context
*ctx
,
5542 struct nir_shader
*nir
) {
5543 nir_foreach_variable(variable
, &nir
->inputs
)
5544 handle_vs_input_decl(ctx
, variable
);
5548 prepare_interp_optimize(struct radv_shader_context
*ctx
,
5549 struct nir_shader
*nir
)
5551 if (!ctx
->options
->key
.fs
.multisample
)
5554 bool uses_center
= false;
5555 bool uses_centroid
= false;
5556 nir_foreach_variable(variable
, &nir
->inputs
) {
5557 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5558 variable
->data
.sample
)
5561 if (variable
->data
.centroid
)
5562 uses_centroid
= true;
5567 if (uses_center
&& uses_centroid
) {
5568 LLVMValueRef sel
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntSLT
, ctx
->abi
.prim_mask
, ctx
->ac
.i32_0
, "");
5569 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->ac
.builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5570 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->ac
.builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5575 handle_fs_inputs(struct radv_shader_context
*ctx
,
5576 struct nir_shader
*nir
)
5578 prepare_interp_optimize(ctx
, nir
);
5580 nir_foreach_variable(variable
, &nir
->inputs
)
5581 handle_fs_input_decl(ctx
, variable
);
5585 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5586 ctx
->shader_info
->info
.needs_multiview_view_index
)
5587 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5589 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5590 LLVMValueRef interp_param
;
5591 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5593 if (!(ctx
->input_mask
& (1ull << i
)))
5596 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5597 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5598 interp_param
= *inputs
;
5599 interp_fs_input(ctx
, index
, interp_param
, ctx
->abi
.prim_mask
,
5603 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5605 } else if (i
== VARYING_SLOT_POS
) {
5606 for(int i
= 0; i
< 3; ++i
)
5607 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5609 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5610 ctx
->abi
.frag_pos
[3]);
5613 ctx
->shader_info
->fs
.num_interp
= index
;
5614 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5615 ctx
->shader_info
->fs
.has_pcoord
= true;
5616 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5617 ctx
->shader_info
->fs
.prim_id_input
= true;
5618 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5619 ctx
->shader_info
->fs
.layer_input
= true;
5620 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5622 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5623 ctx
->abi
.view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5627 ac_build_alloca(struct ac_llvm_context
*ac
,
5631 LLVMBuilderRef builder
= ac
->builder
;
5632 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5633 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5634 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5635 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5636 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5640 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5642 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5645 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5646 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5648 LLVMDisposeBuilder(first_builder
);
5653 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5657 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5658 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5663 scan_shader_output_decl(struct radv_shader_context
*ctx
,
5664 struct nir_variable
*variable
,
5665 struct nir_shader
*shader
,
5666 gl_shader_stage stage
)
5668 int idx
= variable
->data
.location
+ variable
->data
.index
;
5669 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5670 uint64_t mask_attribs
;
5672 variable
->data
.driver_location
= idx
* 4;
5674 /* tess ctrl has it's own load/store paths for outputs */
5675 if (stage
== MESA_SHADER_TESS_CTRL
)
5678 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5679 if (stage
== MESA_SHADER_VERTEX
||
5680 stage
== MESA_SHADER_TESS_EVAL
||
5681 stage
== MESA_SHADER_GEOMETRY
) {
5682 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5683 int length
= shader
->info
.clip_distance_array_size
+
5684 shader
->info
.cull_distance_array_size
;
5685 if (stage
== MESA_SHADER_VERTEX
) {
5686 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5687 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5689 if (stage
== MESA_SHADER_TESS_EVAL
) {
5690 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5691 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5698 mask_attribs
= 1ull << idx
;
5702 ctx
->output_mask
|= mask_attribs
;
5706 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5707 struct nir_shader
*nir
,
5708 struct nir_variable
*variable
)
5710 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5711 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5713 /* tess ctrl has it's own load/store paths for outputs */
5714 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5717 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5718 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5719 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5720 int idx
= variable
->data
.location
+ variable
->data
.index
;
5721 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5722 int length
= nir
->info
.clip_distance_array_size
+
5723 nir
->info
.cull_distance_array_size
;
5732 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5733 for (unsigned chan
= 0; chan
< 4; chan
++) {
5734 ctx
->abi
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5735 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5741 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
5742 enum glsl_base_type type
)
5746 case GLSL_TYPE_UINT
:
5747 case GLSL_TYPE_BOOL
:
5748 case GLSL_TYPE_SUBROUTINE
:
5750 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5752 case GLSL_TYPE_INT64
:
5753 case GLSL_TYPE_UINT64
:
5755 case GLSL_TYPE_DOUBLE
:
5758 unreachable("unknown GLSL type");
5763 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
5764 const struct glsl_type
*type
)
5766 if (glsl_type_is_scalar(type
)) {
5767 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
5770 if (glsl_type_is_vector(type
)) {
5771 return LLVMVectorType(
5772 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
5773 glsl_get_vector_elements(type
));
5776 if (glsl_type_is_matrix(type
)) {
5777 return LLVMArrayType(
5778 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
5779 glsl_get_matrix_columns(type
));
5782 if (glsl_type_is_array(type
)) {
5783 return LLVMArrayType(
5784 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
5785 glsl_get_length(type
));
5788 assert(glsl_type_is_struct(type
));
5790 LLVMTypeRef member_types
[glsl_get_length(type
)];
5792 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5794 glsl_to_llvm_type(ac
,
5795 glsl_get_struct_field(type
, i
));
5798 return LLVMStructTypeInContext(ac
->context
, member_types
,
5799 glsl_get_length(type
), false);
5803 setup_locals(struct ac_nir_context
*ctx
,
5804 struct nir_function
*func
)
5807 ctx
->num_locals
= 0;
5808 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5809 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5810 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5811 variable
->data
.location_frac
= 0;
5812 ctx
->num_locals
+= attrib_count
;
5814 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5818 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5819 for (j
= 0; j
< 4; j
++) {
5820 ctx
->locals
[i
* 4 + j
] =
5821 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5827 setup_shared(struct ac_nir_context
*ctx
,
5828 struct nir_shader
*nir
)
5830 nir_foreach_variable(variable
, &nir
->shared
) {
5831 LLVMValueRef shared
=
5832 LLVMAddGlobalInAddressSpace(
5833 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
5834 variable
->name
? variable
->name
: "",
5835 AC_LOCAL_ADDR_SPACE
);
5836 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5840 /* Initialize arguments for the shader export intrinsic */
5842 si_llvm_init_export_args(struct radv_shader_context
*ctx
,
5843 LLVMValueRef
*values
,
5845 struct ac_export_args
*args
)
5847 /* Default is 0xf. Adjusted below depending on the format. */
5848 args
->enabled_channels
= 0xf;
5850 /* Specify whether the EXEC mask represents the valid mask */
5851 args
->valid_mask
= 0;
5853 /* Specify whether this is the last export */
5856 /* Specify the target we are exporting */
5857 args
->target
= target
;
5859 args
->compr
= false;
5860 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
5861 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
5862 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
5863 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5865 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5866 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5867 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5868 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5869 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5872 LLVMValueRef (*packf
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2]) = NULL
;
5873 LLVMValueRef (*packi
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2],
5874 unsigned bits
, bool hi
) = NULL
;
5876 switch(col_format
) {
5877 case V_028714_SPI_SHADER_ZERO
:
5878 args
->enabled_channels
= 0; /* writemask */
5879 args
->target
= V_008DFC_SQ_EXP_NULL
;
5882 case V_028714_SPI_SHADER_32_R
:
5883 args
->enabled_channels
= 1;
5884 args
->out
[0] = values
[0];
5887 case V_028714_SPI_SHADER_32_GR
:
5888 args
->enabled_channels
= 0x3;
5889 args
->out
[0] = values
[0];
5890 args
->out
[1] = values
[1];
5893 case V_028714_SPI_SHADER_32_AR
:
5894 args
->enabled_channels
= 0x9;
5895 args
->out
[0] = values
[0];
5896 args
->out
[3] = values
[3];
5899 case V_028714_SPI_SHADER_FP16_ABGR
:
5900 packf
= ac_build_cvt_pkrtz_f16
;
5903 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5904 packf
= ac_build_cvt_pknorm_u16
;
5907 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5908 packf
= ac_build_cvt_pknorm_i16
;
5911 case V_028714_SPI_SHADER_UINT16_ABGR
:
5912 packi
= ac_build_cvt_pk_u16
;
5915 case V_028714_SPI_SHADER_SINT16_ABGR
:
5916 packi
= ac_build_cvt_pk_i16
;
5920 case V_028714_SPI_SHADER_32_ABGR
:
5921 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5925 /* Pack f16 or norm_i16/u16. */
5927 for (chan
= 0; chan
< 2; chan
++) {
5928 LLVMValueRef pack_args
[2] = {
5930 values
[2 * chan
+ 1]
5932 LLVMValueRef packed
;
5934 packed
= packf(&ctx
->ac
, pack_args
);
5935 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
5937 args
->compr
= 1; /* COMPR flag */
5942 for (chan
= 0; chan
< 2; chan
++) {
5943 LLVMValueRef pack_args
[2] = {
5944 ac_to_integer(&ctx
->ac
, values
[2 * chan
]),
5945 ac_to_integer(&ctx
->ac
, values
[2 * chan
+ 1])
5947 LLVMValueRef packed
;
5949 packed
= packi(&ctx
->ac
, pack_args
,
5950 is_int8
? 8 : is_int10
? 10 : 16,
5952 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
5954 args
->compr
= 1; /* COMPR flag */
5959 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5961 for (unsigned i
= 0; i
< 4; ++i
)
5962 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5966 radv_export_param(struct radv_shader_context
*ctx
, unsigned index
,
5967 LLVMValueRef
*values
)
5969 struct ac_export_args args
;
5971 si_llvm_init_export_args(ctx
, values
,
5972 V_008DFC_SQ_EXP_PARAM
+ index
, &args
);
5973 ac_build_export(&ctx
->ac
, &args
);
5977 radv_load_output(struct radv_shader_context
*ctx
, unsigned index
, unsigned chan
)
5979 LLVMValueRef output
=
5980 ctx
->abi
.outputs
[radeon_llvm_reg_index_soa(index
, chan
)];
5982 return LLVMBuildLoad(ctx
->ac
.builder
, output
, "");
5986 handle_vs_outputs_post(struct radv_shader_context
*ctx
,
5987 bool export_prim_id
,
5988 struct ac_vs_output_info
*outinfo
)
5990 uint32_t param_count
= 0;
5992 unsigned pos_idx
, num_pos_exports
= 0;
5993 struct ac_export_args args
, pos_args
[4] = {};
5994 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5997 if (ctx
->options
->key
.has_multiview_view_index
) {
5998 LLVMValueRef
* tmp_out
= &ctx
->abi
.outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
6000 for(unsigned i
= 0; i
< 4; ++i
)
6001 ctx
->abi
.outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
6002 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
6005 LLVMBuildStore(ctx
->ac
.builder
, ac_to_float(&ctx
->ac
, ctx
->abi
.view_index
), *tmp_out
);
6006 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
6009 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
6010 sizeof(outinfo
->vs_output_param_offset
));
6012 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
6013 LLVMValueRef slots
[8];
6016 if (outinfo
->cull_dist_mask
)
6017 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
6019 i
= VARYING_SLOT_CLIP_DIST0
;
6020 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
6021 slots
[j
] = ac_to_float(&ctx
->ac
, radv_load_output(ctx
, i
, j
));
6023 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
6024 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
6026 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
6027 target
= V_008DFC_SQ_EXP_POS
+ 3;
6028 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
6029 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
6030 &args
, sizeof(args
));
6033 target
= V_008DFC_SQ_EXP_POS
+ 2;
6034 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
6035 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
6036 &args
, sizeof(args
));
6040 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
6041 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
6042 for (unsigned j
= 0; j
< 4; j
++)
6043 pos_values
[j
] = radv_load_output(ctx
, VARYING_SLOT_POS
, j
);
6045 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
6047 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
6048 outinfo
->writes_pointsize
= true;
6049 psize_value
= radv_load_output(ctx
, VARYING_SLOT_PSIZ
, 0);
6052 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
6053 outinfo
->writes_layer
= true;
6054 layer_value
= radv_load_output(ctx
, VARYING_SLOT_LAYER
, 0);
6057 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
6058 outinfo
->writes_viewport_index
= true;
6059 viewport_index_value
= radv_load_output(ctx
, VARYING_SLOT_VIEWPORT
, 0);
6062 if (outinfo
->writes_pointsize
||
6063 outinfo
->writes_layer
||
6064 outinfo
->writes_viewport_index
) {
6065 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
6066 (outinfo
->writes_layer
== true ? 4 : 0));
6067 pos_args
[1].valid_mask
= 0;
6068 pos_args
[1].done
= 0;
6069 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
6070 pos_args
[1].compr
= 0;
6071 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
6072 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
6073 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
6074 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
6076 if (outinfo
->writes_pointsize
== true)
6077 pos_args
[1].out
[0] = psize_value
;
6078 if (outinfo
->writes_layer
== true)
6079 pos_args
[1].out
[2] = layer_value
;
6080 if (outinfo
->writes_viewport_index
== true) {
6081 if (ctx
->options
->chip_class
>= GFX9
) {
6082 /* GFX9 has the layer in out.z[10:0] and the viewport
6083 * index in out.z[19:16].
6085 LLVMValueRef v
= viewport_index_value
;
6086 v
= ac_to_integer(&ctx
->ac
, v
);
6087 v
= LLVMBuildShl(ctx
->ac
.builder
, v
,
6088 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6090 v
= LLVMBuildOr(ctx
->ac
.builder
, v
,
6091 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
6093 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
6094 pos_args
[1].enabled_channels
|= 1 << 2;
6096 pos_args
[1].out
[3] = viewport_index_value
;
6097 pos_args
[1].enabled_channels
|= 1 << 3;
6101 for (i
= 0; i
< 4; i
++) {
6102 if (pos_args
[i
].out
[0])
6107 for (i
= 0; i
< 4; i
++) {
6108 if (!pos_args
[i
].out
[0])
6111 /* Specify the target we are exporting */
6112 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
6113 if (pos_idx
== num_pos_exports
)
6114 pos_args
[i
].done
= 1;
6115 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
6118 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
6119 LLVMValueRef values
[4];
6120 if (!(ctx
->output_mask
& (1ull << i
)))
6123 if (i
!= VARYING_SLOT_LAYER
&&
6124 i
!= VARYING_SLOT_PRIMITIVE_ID
&&
6125 i
< VARYING_SLOT_VAR0
)
6128 for (unsigned j
= 0; j
< 4; j
++)
6129 values
[j
] = ac_to_float(&ctx
->ac
, radv_load_output(ctx
, i
, j
));
6131 radv_export_param(ctx
, param_count
, values
);
6133 outinfo
->vs_output_param_offset
[i
] = param_count
++;
6136 if (export_prim_id
) {
6137 LLVMValueRef values
[4];
6139 values
[0] = ctx
->vs_prim_id
;
6140 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
6141 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
6142 for (unsigned j
= 1; j
< 4; j
++)
6143 values
[j
] = ctx
->ac
.f32_0
;
6145 radv_export_param(ctx
, param_count
, values
);
6147 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
++;
6148 outinfo
->export_prim_id
= true;
6151 outinfo
->pos_exports
= num_pos_exports
;
6152 outinfo
->param_exports
= param_count
;
6156 handle_es_outputs_post(struct radv_shader_context
*ctx
,
6157 struct ac_es_output_info
*outinfo
)
6160 uint64_t max_output_written
= 0;
6161 LLVMValueRef lds_base
= NULL
;
6163 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
6167 if (!(ctx
->output_mask
& (1ull << i
)))
6170 if (i
== VARYING_SLOT_CLIP_DIST0
)
6171 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6173 param_index
= shader_io_get_unique_index(i
);
6175 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
6178 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
6180 if (ctx
->ac
.chip_class
>= GFX9
) {
6181 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
6182 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
6183 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6184 LLVMConstInt(ctx
->ac
.i32
, 24, false),
6185 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
6186 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
6187 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
6188 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
6189 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
6190 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
6193 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
6194 LLVMValueRef dw_addr
= NULL
;
6195 LLVMValueRef
*out_ptr
= &ctx
->abi
.outputs
[i
* 4];
6199 if (!(ctx
->output_mask
& (1ull << i
)))
6202 if (i
== VARYING_SLOT_CLIP_DIST0
)
6203 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6205 param_index
= shader_io_get_unique_index(i
);
6208 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
6209 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
6212 for (j
= 0; j
< length
; j
++) {
6213 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
, out_ptr
[j
], "");
6214 out_val
= LLVMBuildBitCast(ctx
->ac
.builder
, out_val
, ctx
->ac
.i32
, "");
6216 if (ctx
->ac
.chip_class
>= GFX9
) {
6217 ac_lds_store(&ctx
->ac
, dw_addr
,
6218 LLVMBuildLoad(ctx
->ac
.builder
, out_ptr
[j
], ""));
6219 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6221 ac_build_buffer_store_dword(&ctx
->ac
,
6224 NULL
, ctx
->es2gs_offset
,
6225 (4 * param_index
+ j
) * 4,
6233 handle_ls_outputs_post(struct radv_shader_context
*ctx
)
6235 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
6236 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
6237 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->ac
.builder
, vertex_id
,
6238 vertex_dw_stride
, "");
6240 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
6241 LLVMValueRef
*out_ptr
= &ctx
->abi
.outputs
[i
* 4];
6244 if (!(ctx
->output_mask
& (1ull << i
)))
6247 if (i
== VARYING_SLOT_CLIP_DIST0
)
6248 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6249 int param
= shader_io_get_unique_index(i
);
6250 mark_tess_output(ctx
, false, param
);
6252 mark_tess_output(ctx
, false, param
+ 1);
6253 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_dw_addr
,
6254 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
6256 for (unsigned j
= 0; j
< length
; j
++) {
6257 ac_lds_store(&ctx
->ac
, dw_addr
,
6258 LLVMBuildLoad(ctx
->ac
.builder
, out_ptr
[j
], ""));
6259 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6264 struct ac_build_if_state
6266 struct radv_shader_context
*ctx
;
6267 LLVMValueRef condition
;
6268 LLVMBasicBlockRef entry_block
;
6269 LLVMBasicBlockRef true_block
;
6270 LLVMBasicBlockRef false_block
;
6271 LLVMBasicBlockRef merge_block
;
6274 static LLVMBasicBlockRef
6275 ac_build_insert_new_block(struct radv_shader_context
*ctx
, const char *name
)
6277 LLVMBasicBlockRef current_block
;
6278 LLVMBasicBlockRef next_block
;
6279 LLVMBasicBlockRef new_block
;
6281 /* get current basic block */
6282 current_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
6284 /* chqeck if there's another block after this one */
6285 next_block
= LLVMGetNextBasicBlock(current_block
);
6287 /* insert the new block before the next block */
6288 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
6291 /* append new block after current block */
6292 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
6293 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
6299 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
6300 struct radv_shader_context
*ctx
,
6301 LLVMValueRef condition
)
6303 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
6305 memset(ifthen
, 0, sizeof *ifthen
);
6307 ifthen
->condition
= condition
;
6308 ifthen
->entry_block
= block
;
6310 /* create endif/merge basic block for the phi functions */
6311 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
6313 /* create/insert true_block before merge_block */
6314 ifthen
->true_block
=
6315 LLVMInsertBasicBlockInContext(ctx
->context
,
6316 ifthen
->merge_block
,
6319 /* successive code goes into the true block */
6320 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ifthen
->true_block
);
6324 * End a conditional.
6327 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
6329 LLVMBuilderRef builder
= ifthen
->ctx
->ac
.builder
;
6331 /* Insert branch to the merge block from current block */
6332 LLVMBuildBr(builder
, ifthen
->merge_block
);
6335 * Now patch in the various branch instructions.
6338 /* Insert the conditional branch instruction at the end of entry_block */
6339 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
6340 if (ifthen
->false_block
) {
6341 /* we have an else clause */
6342 LLVMBuildCondBr(builder
, ifthen
->condition
,
6343 ifthen
->true_block
, ifthen
->false_block
);
6346 /* no else clause */
6347 LLVMBuildCondBr(builder
, ifthen
->condition
,
6348 ifthen
->true_block
, ifthen
->merge_block
);
6351 /* Resume building code at end of the ifthen->merge_block */
6352 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
6356 write_tess_factors(struct radv_shader_context
*ctx
)
6358 unsigned stride
, outer_comps
, inner_comps
;
6359 struct ac_build_if_state if_ctx
, inner_if_ctx
;
6360 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 8, 5);
6361 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
6362 unsigned tess_inner_index
, tess_outer_index
;
6363 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6364 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6366 emit_barrier(&ctx
->ac
, ctx
->stage
);
6368 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6388 ac_nir_build_if(&if_ctx
, ctx
,
6389 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
6390 invocation_id
, ctx
->ac
.i32_0
, ""));
6392 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6393 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6395 mark_tess_output(ctx
, true, tess_inner_index
);
6396 mark_tess_output(ctx
, true, tess_outer_index
);
6397 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6398 lds_inner
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
6399 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6400 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
6401 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6403 for (i
= 0; i
< 4; i
++) {
6404 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6405 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6409 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6410 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6411 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_outer
,
6413 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6415 for (i
= 0; i
< outer_comps
; i
++) {
6417 ac_lds_load(&ctx
->ac
, lds_outer
);
6418 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_outer
,
6421 for (i
= 0; i
< inner_comps
; i
++) {
6422 inner
[i
] = out
[outer_comps
+i
] =
6423 ac_lds_load(&ctx
->ac
, lds_inner
);
6424 lds_inner
= LLVMBuildAdd(ctx
->ac
.builder
, lds_inner
,
6429 /* Convert the outputs to vectors for stores. */
6430 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6434 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6437 buffer
= ctx
->hs_ring_tess_factor
;
6438 tf_base
= ctx
->tess_factor_offset
;
6439 byteoffset
= LLVMBuildMul(ctx
->ac
.builder
, rel_patch_id
,
6440 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6441 unsigned tf_offset
= 0;
6443 if (ctx
->options
->chip_class
<= VI
) {
6444 ac_nir_build_if(&inner_if_ctx
, ctx
,
6445 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
6446 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6448 /* Store the dynamic HS control word. */
6449 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6450 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6451 1, ctx
->ac
.i32_0
, tf_base
,
6452 0, 1, 0, true, false);
6455 ac_nir_build_endif(&inner_if_ctx
);
6458 /* Store the tessellation factors. */
6459 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6460 MIN2(stride
, 4), byteoffset
, tf_base
,
6461 tf_offset
, 1, 0, true, false);
6463 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6464 stride
- 4, byteoffset
, tf_base
,
6465 16 + tf_offset
, 1, 0, true, false);
6467 //store to offchip for TES to read - only if TES reads them
6468 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6469 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6470 LLVMValueRef tf_inner_offset
;
6471 unsigned param_outer
, param_inner
;
6473 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6474 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6475 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6477 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6478 util_next_power_of_two(outer_comps
));
6480 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6481 outer_comps
, tf_outer_offset
,
6482 ctx
->oc_lds
, 0, 1, 0, true, false);
6484 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6485 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6486 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6488 inner_vec
= inner_comps
== 1 ? inner
[0] :
6489 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6490 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6491 inner_comps
, tf_inner_offset
,
6492 ctx
->oc_lds
, 0, 1, 0, true, false);
6495 ac_nir_build_endif(&if_ctx
);
6499 handle_tcs_outputs_post(struct radv_shader_context
*ctx
)
6501 write_tess_factors(ctx
);
6505 si_export_mrt_color(struct radv_shader_context
*ctx
,
6506 LLVMValueRef
*color
, unsigned index
, bool is_last
,
6507 struct ac_export_args
*args
)
6510 si_llvm_init_export_args(ctx
, color
,
6511 V_008DFC_SQ_EXP_MRT
+ index
, args
);
6514 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6515 args
->done
= 1; /* DONE bit */
6516 } else if (!args
->enabled_channels
)
6517 return false; /* unnecessary NULL export */
6523 radv_export_mrt_z(struct radv_shader_context
*ctx
,
6524 LLVMValueRef depth
, LLVMValueRef stencil
,
6525 LLVMValueRef samplemask
)
6527 struct ac_export_args args
;
6529 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
6531 ac_build_export(&ctx
->ac
, &args
);
6535 handle_fs_outputs_post(struct radv_shader_context
*ctx
)
6538 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6539 struct ac_export_args color_args
[8];
6541 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
6542 LLVMValueRef values
[4];
6545 if (!(ctx
->output_mask
& (1ull << i
)))
6548 if (i
< FRAG_RESULT_DATA0
)
6551 for (unsigned j
= 0; j
< 4; j
++)
6552 values
[j
] = ac_to_float(&ctx
->ac
,
6553 radv_load_output(ctx
, i
, j
));
6555 if (!ctx
->shader_info
->info
.ps
.writes_z
&&
6556 !ctx
->shader_info
->info
.ps
.writes_stencil
&&
6557 !ctx
->shader_info
->info
.ps
.writes_sample_mask
)
6558 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6560 bool ret
= si_export_mrt_color(ctx
, values
,
6561 i
- FRAG_RESULT_DATA0
,
6562 last
, &color_args
[index
]);
6567 /* Process depth, stencil, samplemask. */
6568 if (ctx
->shader_info
->info
.ps
.writes_z
) {
6569 depth
= ac_to_float(&ctx
->ac
,
6570 radv_load_output(ctx
, FRAG_RESULT_DEPTH
, 0));
6572 if (ctx
->shader_info
->info
.ps
.writes_stencil
) {
6573 stencil
= ac_to_float(&ctx
->ac
,
6574 radv_load_output(ctx
, FRAG_RESULT_STENCIL
, 0));
6576 if (ctx
->shader_info
->info
.ps
.writes_sample_mask
) {
6577 samplemask
= ac_to_float(&ctx
->ac
,
6578 radv_load_output(ctx
, FRAG_RESULT_SAMPLE_MASK
, 0));
6581 /* Export PS outputs. */
6582 for (unsigned i
= 0; i
< index
; i
++)
6583 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6585 if (depth
|| stencil
|| samplemask
)
6586 radv_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6588 ac_build_export_null(&ctx
->ac
);
6592 emit_gs_epilogue(struct radv_shader_context
*ctx
)
6594 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6598 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6599 LLVMValueRef
*addrs
)
6601 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
6603 switch (ctx
->stage
) {
6604 case MESA_SHADER_VERTEX
:
6605 if (ctx
->options
->key
.vs
.as_ls
)
6606 handle_ls_outputs_post(ctx
);
6607 else if (ctx
->options
->key
.vs
.as_es
)
6608 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6610 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6611 &ctx
->shader_info
->vs
.outinfo
);
6613 case MESA_SHADER_FRAGMENT
:
6614 handle_fs_outputs_post(ctx
);
6616 case MESA_SHADER_GEOMETRY
:
6617 emit_gs_epilogue(ctx
);
6619 case MESA_SHADER_TESS_CTRL
:
6620 handle_tcs_outputs_post(ctx
);
6622 case MESA_SHADER_TESS_EVAL
:
6623 if (ctx
->options
->key
.tes
.as_es
)
6624 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6626 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6627 &ctx
->shader_info
->tes
.outinfo
);
6634 static void ac_llvm_finalize_module(struct radv_shader_context
*ctx
)
6636 LLVMPassManagerRef passmgr
;
6637 /* Create the pass manager */
6638 passmgr
= LLVMCreateFunctionPassManagerForModule(
6641 /* This pass should eliminate all the load and store instructions */
6642 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6644 /* Add some optimization passes */
6645 LLVMAddScalarReplAggregatesPass(passmgr
);
6646 LLVMAddLICMPass(passmgr
);
6647 LLVMAddAggressiveDCEPass(passmgr
);
6648 LLVMAddCFGSimplificationPass(passmgr
);
6649 LLVMAddInstructionCombiningPass(passmgr
);
6652 LLVMInitializeFunctionPassManager(passmgr
);
6653 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6654 LLVMFinalizeFunctionPassManager(passmgr
);
6656 LLVMDisposeBuilder(ctx
->ac
.builder
);
6657 LLVMDisposePassManager(passmgr
);
6661 ac_nir_eliminate_const_vs_outputs(struct radv_shader_context
*ctx
)
6663 struct ac_vs_output_info
*outinfo
;
6665 switch (ctx
->stage
) {
6666 case MESA_SHADER_FRAGMENT
:
6667 case MESA_SHADER_COMPUTE
:
6668 case MESA_SHADER_TESS_CTRL
:
6669 case MESA_SHADER_GEOMETRY
:
6671 case MESA_SHADER_VERTEX
:
6672 if (ctx
->options
->key
.vs
.as_ls
||
6673 ctx
->options
->key
.vs
.as_es
)
6675 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6677 case MESA_SHADER_TESS_EVAL
:
6678 if (ctx
->options
->key
.vs
.as_es
)
6680 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6683 unreachable("Unhandled shader type");
6686 ac_optimize_vs_outputs(&ctx
->ac
,
6688 outinfo
->vs_output_param_offset
,
6690 &outinfo
->param_exports
);
6694 ac_setup_rings(struct radv_shader_context
*ctx
)
6696 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6697 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6698 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6701 if (ctx
->is_gs_copy_shader
) {
6702 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6704 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6706 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6707 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6709 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->ac
.builder
, ctx
->gsvs_ring
, ctx
->ac
.v4i32
, "");
6711 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->ac
.builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6712 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6713 tmp
= LLVMBuildOr(ctx
->ac
.builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6714 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->ac
.builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6717 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6718 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6719 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6720 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6725 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6726 const struct nir_shader
*nir
)
6728 switch (nir
->info
.stage
) {
6729 case MESA_SHADER_TESS_CTRL
:
6730 return chip_class
>= CIK
? 128 : 64;
6731 case MESA_SHADER_GEOMETRY
:
6732 return chip_class
>= GFX9
? 128 : 64;
6733 case MESA_SHADER_COMPUTE
:
6739 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6740 nir
->info
.cs
.local_size
[1] *
6741 nir
->info
.cs
.local_size
[2];
6742 return max_workgroup_size
;
6745 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6746 static void ac_nir_fixup_ls_hs_input_vgprs(struct radv_shader_context
*ctx
)
6748 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6749 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6750 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6751 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6753 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6754 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6755 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_rel_ids
, ctx
->rel_auto_id
, "");
6756 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6759 static void prepare_gs_input_vgprs(struct radv_shader_context
*ctx
)
6761 for(int i
= 5; i
>= 0; --i
) {
6762 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6763 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6764 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6767 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6768 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6769 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6772 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6773 struct nir_shader
*nir
)
6775 struct ac_nir_context ctx
= {};
6776 struct nir_function
*func
;
6781 ctx
.stage
= nir
->info
.stage
;
6783 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6785 nir_foreach_variable(variable
, &nir
->outputs
)
6786 handle_shader_output_decl(&ctx
, nir
, variable
);
6788 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6789 _mesa_key_pointer_equal
);
6790 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6791 _mesa_key_pointer_equal
);
6792 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6793 _mesa_key_pointer_equal
);
6795 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6797 setup_locals(&ctx
, func
);
6799 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6800 setup_shared(&ctx
, nir
);
6802 visit_cf_list(&ctx
, &func
->impl
->body
);
6803 phi_post_pass(&ctx
);
6805 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
6806 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
6810 ralloc_free(ctx
.defs
);
6811 ralloc_free(ctx
.phis
);
6812 ralloc_free(ctx
.vars
);
6816 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6817 struct nir_shader
*const *shaders
,
6819 struct ac_shader_variant_info
*shader_info
,
6820 const struct ac_nir_compiler_options
*options
)
6822 struct radv_shader_context ctx
= {0};
6824 ctx
.options
= options
;
6825 ctx
.shader_info
= shader_info
;
6826 ctx
.context
= LLVMContextCreate();
6828 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
6830 ctx
.ac
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6831 LLVMSetTarget(ctx
.ac
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6833 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6834 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6835 LLVMSetDataLayout(ctx
.ac
.module
, data_layout_str
);
6836 LLVMDisposeTargetData(data_layout
);
6837 LLVMDisposeMessage(data_layout_str
);
6839 enum ac_float_mode float_mode
=
6840 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
6841 AC_FLOAT_MODE_DEFAULT
;
6843 ctx
.ac
.builder
= ac_create_builder(ctx
.context
, float_mode
);
6845 memset(shader_info
, 0, sizeof(*shader_info
));
6847 for(int i
= 0; i
< shader_count
; ++i
)
6848 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6850 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6851 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6852 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6853 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6855 ctx
.max_workgroup_size
= 0;
6856 for (int i
= 0; i
< shader_count
; ++i
) {
6857 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6858 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6862 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6863 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6865 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6866 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6867 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
6868 ctx
.abi
.load_ubo
= radv_load_ubo
;
6869 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6870 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6871 ctx
.abi
.load_resource
= radv_load_resource
;
6872 ctx
.abi
.clamp_shadow_reference
= false;
6874 if (shader_count
>= 2)
6875 ac_init_exec_full_mask(&ctx
.ac
);
6877 if (ctx
.ac
.chip_class
== GFX9
&&
6878 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6879 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6881 for(int i
= 0; i
< shader_count
; ++i
) {
6882 ctx
.stage
= shaders
[i
]->info
.stage
;
6883 ctx
.output_mask
= 0;
6884 ctx
.tess_outputs_written
= 0;
6885 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6886 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6888 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6889 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6890 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6891 ctx
.abi
.load_inputs
= load_gs_input
;
6892 ctx
.abi
.emit_primitive
= visit_end_primitive
;
6893 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6894 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
6895 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
6896 ctx
.abi
.load_tess_varyings
= load_tcs_varyings
;
6897 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
6898 ctx
.abi
.store_tcs_outputs
= store_tcs_output
;
6899 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6900 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6901 ctx
.abi
.load_tess_varyings
= load_tes_input
;
6902 ctx
.abi
.load_tess_coord
= load_tess_coord
;
6903 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
6904 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6905 if (shader_info
->info
.vs
.needs_instance_id
) {
6906 if (ctx
.options
->key
.vs
.as_ls
) {
6907 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6908 MAX2(2, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6910 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6911 MAX2(1, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6914 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6915 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6916 ctx
.abi
.lookup_interp_param
= lookup_interp_param
;
6917 ctx
.abi
.load_sample_position
= load_sample_position
;
6918 ctx
.abi
.load_sample_mask_in
= load_sample_mask_in
;
6922 emit_barrier(&ctx
.ac
, ctx
.stage
);
6924 ac_setup_rings(&ctx
);
6926 LLVMBasicBlockRef merge_block
;
6927 if (shader_count
>= 2) {
6928 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6929 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6930 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6932 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6933 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6934 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6935 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6936 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6937 thread_id
, count
, "");
6938 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6940 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6943 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6944 handle_fs_inputs(&ctx
, shaders
[i
]);
6945 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6946 handle_vs_inputs(&ctx
, shaders
[i
]);
6947 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6948 prepare_gs_input_vgprs(&ctx
);
6950 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6951 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6953 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
]);
6955 if (shader_count
>= 2) {
6956 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6957 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6960 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6961 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6962 shaders
[i
]->info
.cull_distance_array_size
> 4;
6963 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6964 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6965 shaders
[i
]->info
.gs
.vertices_out
;
6966 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6967 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6968 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6969 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6970 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6974 LLVMBuildRetVoid(ctx
.ac
.builder
);
6976 if (options
->dump_preoptir
)
6977 ac_dump_module(ctx
.ac
.module
);
6979 ac_llvm_finalize_module(&ctx
);
6981 if (shader_count
== 1)
6982 ac_nir_eliminate_const_vs_outputs(&ctx
);
6984 return ctx
.ac
.module
;
6987 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6989 unsigned *retval
= (unsigned *)context
;
6990 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6991 char *description
= LLVMGetDiagInfoDescription(di
);
6993 if (severity
== LLVMDSError
) {
6995 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6999 LLVMDisposeMessage(description
);
7002 static unsigned ac_llvm_compile(LLVMModuleRef M
,
7003 struct ac_shader_binary
*binary
,
7004 LLVMTargetMachineRef tm
)
7006 unsigned retval
= 0;
7008 LLVMContextRef llvm_ctx
;
7009 LLVMMemoryBufferRef out_buffer
;
7010 unsigned buffer_size
;
7011 const char *buffer_data
;
7014 /* Setup Diagnostic Handler*/
7015 llvm_ctx
= LLVMGetModuleContext(M
);
7017 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
7021 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
7024 /* Process Errors/Warnings */
7026 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
7032 /* Extract Shader Code*/
7033 buffer_size
= LLVMGetBufferSize(out_buffer
);
7034 buffer_data
= LLVMGetBufferStart(out_buffer
);
7036 ac_elf_read(buffer_data
, buffer_size
, binary
);
7039 LLVMDisposeMemoryBuffer(out_buffer
);
7045 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
7046 LLVMModuleRef llvm_module
,
7047 struct ac_shader_binary
*binary
,
7048 struct ac_shader_config
*config
,
7049 struct ac_shader_variant_info
*shader_info
,
7050 gl_shader_stage stage
,
7051 bool dump_shader
, bool supports_spill
)
7054 ac_dump_module(llvm_module
);
7056 memset(binary
, 0, sizeof(*binary
));
7057 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
7059 fprintf(stderr
, "compile failed\n");
7063 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
7065 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
7067 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
7068 LLVMDisposeModule(llvm_module
);
7069 LLVMContextDispose(ctx
);
7071 if (stage
== MESA_SHADER_FRAGMENT
) {
7072 shader_info
->num_input_vgprs
= 0;
7073 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
7074 shader_info
->num_input_vgprs
+= 2;
7075 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
7076 shader_info
->num_input_vgprs
+= 2;
7077 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
7078 shader_info
->num_input_vgprs
+= 2;
7079 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
7080 shader_info
->num_input_vgprs
+= 3;
7081 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
7082 shader_info
->num_input_vgprs
+= 2;
7083 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
7084 shader_info
->num_input_vgprs
+= 2;
7085 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
7086 shader_info
->num_input_vgprs
+= 2;
7087 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
7088 shader_info
->num_input_vgprs
+= 1;
7089 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
7090 shader_info
->num_input_vgprs
+= 1;
7091 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
7092 shader_info
->num_input_vgprs
+= 1;
7093 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
7094 shader_info
->num_input_vgprs
+= 1;
7095 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
7096 shader_info
->num_input_vgprs
+= 1;
7097 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
7098 shader_info
->num_input_vgprs
+= 1;
7099 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
7100 shader_info
->num_input_vgprs
+= 1;
7101 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
7102 shader_info
->num_input_vgprs
+= 1;
7103 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
7104 shader_info
->num_input_vgprs
+= 1;
7106 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
7108 /* +3 for scratch wave offset and VCC */
7109 config
->num_sgprs
= MAX2(config
->num_sgprs
,
7110 shader_info
->num_input_sgprs
+ 3);
7112 /* Enable 64-bit and 16-bit denormals, because there is no performance
7115 * If denormals are enabled, all floating-point output modifiers are
7118 * Don't enable denormals for 32-bit floats, because:
7119 * - Floating-point output modifiers would be ignored by the hw.
7120 * - Some opcodes don't support denormals, such as v_mad_f32. We would
7121 * have to stop using those.
7122 * - SI & CI would be very slow.
7124 config
->float_mode
|= V_00B028_FP_64_DENORMS
;
7128 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
7130 switch (nir
->info
.stage
) {
7131 case MESA_SHADER_COMPUTE
:
7132 for (int i
= 0; i
< 3; ++i
)
7133 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
7135 case MESA_SHADER_FRAGMENT
:
7136 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
7138 case MESA_SHADER_GEOMETRY
:
7139 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
7140 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
7141 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
7142 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
7144 case MESA_SHADER_TESS_EVAL
:
7145 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
7146 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
7147 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
7148 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
7149 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
7151 case MESA_SHADER_TESS_CTRL
:
7152 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
7154 case MESA_SHADER_VERTEX
:
7155 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
7156 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
7157 /* in LS mode we need at least 1, invocation id needs 2, handled elsewhere */
7158 if (options
->key
.vs
.as_ls
)
7159 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
7166 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
7167 struct ac_shader_binary
*binary
,
7168 struct ac_shader_config
*config
,
7169 struct ac_shader_variant_info
*shader_info
,
7170 struct nir_shader
*const *nir
,
7172 const struct ac_nir_compiler_options
*options
,
7176 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
7179 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
7180 for (int i
= 0; i
< nir_count
; ++i
)
7181 ac_fill_shader_info(shader_info
, nir
[i
], options
);
7183 /* Determine the ES type (VS or TES) for the GS on GFX9. */
7184 if (options
->chip_class
== GFX9
) {
7185 if (nir_count
== 2 &&
7186 nir
[1]->info
.stage
== MESA_SHADER_GEOMETRY
) {
7187 shader_info
->gs
.es_type
= nir
[0]->info
.stage
;
7193 ac_gs_copy_shader_emit(struct radv_shader_context
*ctx
)
7195 LLVMValueRef vtx_offset
=
7196 LLVMBuildMul(ctx
->ac
.builder
, ctx
->abi
.vertex_id
,
7197 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
7200 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
7204 if (!(ctx
->output_mask
& (1ull << i
)))
7207 if (i
== VARYING_SLOT_CLIP_DIST0
) {
7208 /* unpack clip and cull from a single set of slots */
7209 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
7214 for (unsigned j
= 0; j
< length
; j
++) {
7215 LLVMValueRef value
, soffset
;
7217 soffset
= LLVMConstInt(ctx
->ac
.i32
,
7219 ctx
->gs_max_out_vertices
* 16 * 4, false);
7221 value
= ac_build_buffer_load(&ctx
->ac
, ctx
->gsvs_ring
,
7223 vtx_offset
, soffset
,
7224 0, 1, 1, true, false);
7226 LLVMBuildStore(ctx
->ac
.builder
,
7227 ac_to_float(&ctx
->ac
, value
), ctx
->abi
.outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
7231 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
7234 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
7235 struct nir_shader
*geom_shader
,
7236 struct ac_shader_binary
*binary
,
7237 struct ac_shader_config
*config
,
7238 struct ac_shader_variant_info
*shader_info
,
7239 const struct ac_nir_compiler_options
*options
,
7242 struct radv_shader_context ctx
= {0};
7243 ctx
.context
= LLVMContextCreate();
7244 ctx
.options
= options
;
7245 ctx
.shader_info
= shader_info
;
7247 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
7249 ctx
.ac
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
7251 ctx
.is_gs_copy_shader
= true;
7252 LLVMSetTarget(ctx
.ac
.module
, "amdgcn--");
7254 enum ac_float_mode float_mode
=
7255 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
7256 AC_FLOAT_MODE_DEFAULT
;
7258 ctx
.ac
.builder
= ac_create_builder(ctx
.context
, float_mode
);
7259 ctx
.stage
= MESA_SHADER_VERTEX
;
7261 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
7263 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
7264 ac_setup_rings(&ctx
);
7266 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
7267 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
7269 struct ac_nir_context nir_ctx
= {};
7270 nir_ctx
.ac
= ctx
.ac
;
7271 nir_ctx
.abi
= &ctx
.abi
;
7273 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
7274 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
7275 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
7278 ac_gs_copy_shader_emit(&ctx
);
7280 LLVMBuildRetVoid(ctx
.ac
.builder
);
7282 ac_llvm_finalize_module(&ctx
);
7284 ac_compile_llvm_module(tm
, ctx
.ac
.module
, binary
, config
, shader_info
,
7286 dump_shader
, options
->supports_spill
);