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_exp_param.h"
37 enum radeon_llvm_calling_convention
{
38 RADEON_LLVM_AMDGPU_VS
= 87,
39 RADEON_LLVM_AMDGPU_GS
= 88,
40 RADEON_LLVM_AMDGPU_PS
= 89,
41 RADEON_LLVM_AMDGPU_CS
= 90,
42 RADEON_LLVM_AMDGPU_HS
= 93,
45 #define CONST_ADDR_SPACE 2
46 #define LOCAL_ADDR_SPACE 3
48 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
49 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
51 struct nir_to_llvm_context
;
53 struct ac_nir_context
{
54 struct ac_llvm_context ac
;
55 struct ac_shader_abi
*abi
;
57 gl_shader_stage stage
;
59 struct hash_table
*defs
;
60 struct hash_table
*phis
;
61 struct hash_table
*vars
;
63 LLVMValueRef main_function
;
64 LLVMBasicBlockRef continue_block
;
65 LLVMBasicBlockRef break_block
;
67 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
72 struct nir_to_llvm_context
*nctx
; /* TODO get rid of this */
75 struct nir_to_llvm_context
{
76 struct ac_llvm_context ac
;
77 const struct ac_nir_compiler_options
*options
;
78 struct ac_shader_variant_info
*shader_info
;
79 struct ac_shader_abi abi
;
80 struct ac_nir_context
*nir
;
82 unsigned max_workgroup_size
;
83 LLVMContextRef context
;
85 LLVMBuilderRef builder
;
86 LLVMValueRef main_function
;
88 struct hash_table
*defs
;
89 struct hash_table
*phis
;
91 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
92 LLVMValueRef ring_offsets
;
93 LLVMValueRef push_constants
;
94 LLVMValueRef view_index
;
95 LLVMValueRef num_work_groups
;
96 LLVMValueRef workgroup_ids
;
97 LLVMValueRef local_invocation_ids
;
100 LLVMValueRef vertex_buffers
;
101 LLVMValueRef rel_auto_id
;
102 LLVMValueRef vs_prim_id
;
103 LLVMValueRef ls_out_layout
;
104 LLVMValueRef es2gs_offset
;
106 LLVMValueRef tcs_offchip_layout
;
107 LLVMValueRef tcs_out_offsets
;
108 LLVMValueRef tcs_out_layout
;
109 LLVMValueRef tcs_in_layout
;
111 LLVMValueRef merged_wave_info
;
112 LLVMValueRef tess_factor_offset
;
113 LLVMValueRef tcs_patch_id
;
114 LLVMValueRef tcs_rel_ids
;
115 LLVMValueRef tes_rel_patch_id
;
116 LLVMValueRef tes_patch_id
;
120 LLVMValueRef gsvs_ring_stride
;
121 LLVMValueRef gsvs_num_entries
;
122 LLVMValueRef gs2vs_offset
;
123 LLVMValueRef gs_wave_id
;
124 LLVMValueRef gs_vtx_offset
[6];
125 LLVMValueRef gs_prim_id
, gs_invocation_id
;
127 LLVMValueRef esgs_ring
;
128 LLVMValueRef gsvs_ring
;
129 LLVMValueRef hs_ring_tess_offchip
;
130 LLVMValueRef hs_ring_tess_factor
;
132 LLVMValueRef prim_mask
;
133 LLVMValueRef sample_pos_offset
;
134 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
135 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
148 unsigned uniform_md_kind
;
149 LLVMValueRef empty_md
;
150 gl_shader_stage stage
;
152 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
155 uint64_t output_mask
;
156 uint8_t num_output_clips
;
157 uint8_t num_output_culls
;
159 bool is_gs_copy_shader
;
160 LLVMValueRef gs_next_vertex
;
161 unsigned gs_max_out_vertices
;
163 unsigned tes_primitive_mode
;
164 uint64_t tess_outputs_written
;
165 uint64_t tess_patch_outputs_written
;
168 static inline struct nir_to_llvm_context
*
169 nir_to_llvm_context_from_abi(struct ac_shader_abi
*abi
)
171 struct nir_to_llvm_context
*ctx
= NULL
;
172 return container_of(abi
, ctx
, abi
);
175 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
176 const nir_deref_var
*deref
,
177 enum ac_descriptor_type desc_type
,
178 const nir_tex_instr
*instr
,
179 bool image
, bool write
);
181 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
183 return (index
* 4) + chan
;
186 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
188 /* handle patch indices separate */
189 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
191 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
193 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
194 return 2 + (slot
- VARYING_SLOT_PATCH0
);
196 if (slot
== VARYING_SLOT_POS
)
198 if (slot
== VARYING_SLOT_PSIZ
)
200 if (slot
== VARYING_SLOT_CLIP_DIST0
)
202 /* 3 is reserved for clip dist as well */
203 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
204 return 4 + (slot
- VARYING_SLOT_VAR0
);
205 unreachable("illegal slot in get unique index\n");
208 static void set_llvm_calling_convention(LLVMValueRef func
,
209 gl_shader_stage stage
)
211 enum radeon_llvm_calling_convention calling_conv
;
214 case MESA_SHADER_VERTEX
:
215 case MESA_SHADER_TESS_EVAL
:
216 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
218 case MESA_SHADER_GEOMETRY
:
219 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
221 case MESA_SHADER_TESS_CTRL
:
222 calling_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
: RADEON_LLVM_AMDGPU_VS
;
224 case MESA_SHADER_FRAGMENT
:
225 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
227 case MESA_SHADER_COMPUTE
:
228 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
231 unreachable("Unhandle shader type");
234 LLVMSetFunctionCallConv(func
, calling_conv
);
239 LLVMTypeRef types
[MAX_ARGS
];
240 LLVMValueRef
*assign
[MAX_ARGS
];
241 unsigned array_params_mask
;
243 uint8_t user_sgpr_count
;
245 uint8_t num_user_sgprs_used
;
246 uint8_t num_sgprs_used
;
247 uint8_t num_vgprs_used
;
251 add_argument(struct arg_info
*info
,
252 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
254 assert(info
->count
< MAX_ARGS
);
255 info
->assign
[info
->count
] = param_ptr
;
256 info
->types
[info
->count
] = type
;
261 add_sgpr_argument(struct arg_info
*info
,
262 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
264 add_argument(info
, type
, param_ptr
);
265 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
270 add_user_sgpr_argument(struct arg_info
*info
,
272 LLVMValueRef
*param_ptr
)
274 add_sgpr_argument(info
, type
, param_ptr
);
275 info
->num_user_sgprs_used
+= ac_get_type_size(type
) / 4;
276 info
->user_sgpr_count
++;
280 add_vgpr_argument(struct arg_info
*info
,
282 LLVMValueRef
*param_ptr
)
284 add_argument(info
, type
, param_ptr
);
285 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
289 add_user_sgpr_array_argument(struct arg_info
*info
,
291 LLVMValueRef
*param_ptr
)
293 info
->array_params_mask
|= (1 << info
->count
);
294 add_user_sgpr_argument(info
, type
, param_ptr
);
297 static void assign_arguments(LLVMValueRef main_function
,
298 struct arg_info
*info
)
301 for (i
= 0; i
< info
->count
; i
++) {
303 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
308 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
309 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
310 unsigned num_return_elems
,
311 struct arg_info
*args
,
312 unsigned max_workgroup_size
,
315 LLVMTypeRef main_function_type
, ret_type
;
316 LLVMBasicBlockRef main_function_body
;
318 if (num_return_elems
)
319 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
320 num_return_elems
, true);
322 ret_type
= LLVMVoidTypeInContext(ctx
);
324 /* Setup the function */
326 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
327 LLVMValueRef main_function
=
328 LLVMAddFunction(module
, "main", main_function_type
);
330 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
331 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
333 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
334 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
335 if (args
->array_params_mask
& (1 << i
)) {
336 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
337 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
338 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
341 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
345 if (max_workgroup_size
) {
346 ac_llvm_add_target_dep_function_attr(main_function
,
347 "amdgpu-max-work-group-size",
351 /* These were copied from some LLVM test. */
352 LLVMAddTargetDependentFunctionAttr(main_function
,
353 "less-precise-fpmad",
355 LLVMAddTargetDependentFunctionAttr(main_function
,
358 LLVMAddTargetDependentFunctionAttr(main_function
,
361 LLVMAddTargetDependentFunctionAttr(main_function
,
365 return main_function
;
368 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
370 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
374 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
376 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
377 type
= LLVMGetElementType(type
);
379 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
380 return LLVMGetIntTypeWidth(type
);
382 if (type
== ctx
->f16
)
384 if (type
== ctx
->f32
)
386 if (type
== ctx
->f64
)
389 unreachable("Unhandled type kind in get_elem_bits");
392 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
393 LLVMValueRef param
, unsigned rshift
,
396 LLVMValueRef value
= param
;
398 value
= LLVMBuildLShr(ctx
->builder
, value
,
399 LLVMConstInt(ctx
->i32
, rshift
, false), "");
401 if (rshift
+ bitwidth
< 32) {
402 unsigned mask
= (1 << bitwidth
) - 1;
403 value
= LLVMBuildAnd(ctx
->builder
, value
,
404 LLVMConstInt(ctx
->i32
, mask
, false), "");
409 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
411 switch (ctx
->stage
) {
412 case MESA_SHADER_TESS_CTRL
:
413 return unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
414 case MESA_SHADER_TESS_EVAL
:
415 return ctx
->tes_rel_patch_id
;
418 unreachable("Illegal stage");
422 /* Tessellation shaders pass outputs to the next shader using LDS.
424 * LS outputs = TCS inputs
425 * TCS outputs = TES inputs
428 * - TCS inputs for patch 0
429 * - TCS inputs for patch 1
430 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
432 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
433 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
434 * - TCS outputs for patch 1
435 * - Per-patch TCS outputs for patch 1
436 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
437 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
440 * All three shaders VS(LS), TCS, TES share the same LDS space.
443 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
445 if (ctx
->stage
== MESA_SHADER_VERTEX
)
446 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
447 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
448 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
456 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
458 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
462 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
464 return LLVMBuildMul(ctx
->builder
,
465 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
466 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
470 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
472 return LLVMBuildMul(ctx
->builder
,
473 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
474 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
478 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
480 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
481 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
483 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
487 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
489 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
490 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
491 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
493 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
494 LLVMBuildMul(ctx
->builder
, patch_stride
,
500 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
502 LLVMValueRef patch0_patch_data_offset
=
503 get_tcs_out_patch0_patch_data_offset(ctx
);
504 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
505 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
507 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
508 LLVMBuildMul(ctx
->builder
, patch_stride
,
513 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
515 ud_info
->sgpr_idx
= *sgpr_idx
;
516 ud_info
->num_sgprs
= num_sgprs
;
517 ud_info
->indirect
= false;
518 ud_info
->indirect_offset
= 0;
519 *sgpr_idx
+= num_sgprs
;
522 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
523 int idx
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
525 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
529 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
530 uint32_t indirect_offset
)
532 ud_info
->sgpr_idx
= sgpr_idx
;
533 ud_info
->num_sgprs
= num_sgprs
;
534 ud_info
->indirect
= true;
535 ud_info
->indirect_offset
= indirect_offset
;
538 struct user_sgpr_info
{
539 bool need_ring_offsets
;
541 bool indirect_all_descriptor_sets
;
544 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
545 struct user_sgpr_info
*user_sgpr_info
)
547 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
549 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
550 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
551 ctx
->stage
== MESA_SHADER_VERTEX
||
552 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
553 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
554 ctx
->is_gs_copy_shader
)
555 user_sgpr_info
->need_ring_offsets
= true;
557 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
558 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
559 user_sgpr_info
->need_ring_offsets
= true;
561 /* 2 user sgprs will nearly always be allocated for scratch/rings */
562 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
563 user_sgpr_info
->sgpr_count
+= 2;
566 switch (ctx
->stage
) {
567 case MESA_SHADER_COMPUTE
:
568 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
570 case MESA_SHADER_FRAGMENT
:
571 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
573 case MESA_SHADER_VERTEX
:
574 if (!ctx
->is_gs_copy_shader
) {
575 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
576 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
577 user_sgpr_info
->sgpr_count
+= 3;
579 user_sgpr_info
->sgpr_count
+= 2;
582 if (ctx
->options
->key
.vs
.as_ls
)
583 user_sgpr_info
->sgpr_count
++;
585 case MESA_SHADER_TESS_CTRL
:
586 user_sgpr_info
->sgpr_count
+= 4;
588 case MESA_SHADER_TESS_EVAL
:
589 user_sgpr_info
->sgpr_count
+= 1;
591 case MESA_SHADER_GEOMETRY
:
592 user_sgpr_info
->sgpr_count
+= 2;
598 if (ctx
->shader_info
->info
.needs_push_constants
)
599 user_sgpr_info
->sgpr_count
+= 2;
601 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
602 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
603 user_sgpr_info
->sgpr_count
+= 2;
604 user_sgpr_info
->indirect_all_descriptor_sets
= true;
606 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
611 radv_define_common_user_sgprs_phase1(struct nir_to_llvm_context
*ctx
,
612 gl_shader_stage stage
,
613 bool has_previous_stage
,
614 gl_shader_stage previous_stage
,
615 const struct user_sgpr_info
*user_sgpr_info
,
616 struct arg_info
*args
,
617 LLVMValueRef
*desc_sets
)
619 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
620 unsigned stage_mask
= 1 << stage
;
621 if (has_previous_stage
)
622 stage_mask
|= 1 << previous_stage
;
624 /* 1 for each descriptor set */
625 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
626 for (unsigned i
= 0; i
< num_sets
; ++i
) {
627 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
628 add_user_sgpr_array_argument(args
, const_array(ctx
->ac
.i8
, 1024 * 1024), &ctx
->descriptor_sets
[i
]);
632 add_user_sgpr_array_argument(args
, const_array(const_array(ctx
->ac
.i8
, 1024 * 1024), 32), desc_sets
);
634 if (ctx
->shader_info
->info
.needs_push_constants
) {
635 /* 1 for push constants and dynamic descriptors */
636 add_user_sgpr_array_argument(args
, const_array(ctx
->ac
.i8
, 1024 * 1024), &ctx
->push_constants
);
641 radv_define_common_user_sgprs_phase2(struct nir_to_llvm_context
*ctx
,
642 gl_shader_stage stage
,
643 bool has_previous_stage
,
644 gl_shader_stage previous_stage
,
645 const struct user_sgpr_info
*user_sgpr_info
,
646 LLVMValueRef desc_sets
,
647 uint8_t *user_sgpr_idx
)
649 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
650 unsigned stage_mask
= 1 << stage
;
651 if (has_previous_stage
)
652 stage_mask
|= 1 << previous_stage
;
654 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
655 for (unsigned i
= 0; i
< num_sets
; ++i
) {
656 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
657 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
659 ctx
->descriptor_sets
[i
] = NULL
;
662 uint32_t desc_sgpr_idx
= *user_sgpr_idx
;
663 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, user_sgpr_idx
, 2);
665 for (unsigned i
= 0; i
< num_sets
; ++i
) {
666 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
667 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
668 ctx
->descriptor_sets
[i
] = ac_build_load_to_sgpr(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->ac
.i32
, i
, false));
671 ctx
->descriptor_sets
[i
] = NULL
;
673 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
676 if (ctx
->shader_info
->info
.needs_push_constants
) {
677 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
682 radv_define_vs_user_sgprs_phase1(struct nir_to_llvm_context
*ctx
,
683 gl_shader_stage stage
,
684 bool has_previous_stage
,
685 gl_shader_stage previous_stage
,
686 struct arg_info
*args
)
688 if (!ctx
->is_gs_copy_shader
&& (stage
== MESA_SHADER_VERTEX
|| (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
689 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
690 add_user_sgpr_argument(args
, const_array(ctx
->v4i32
, 16), &ctx
->vertex_buffers
); /* vertex buffers */
691 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
); // base vertex
692 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);// start instance
693 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
694 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
); // draw id
699 radv_define_vs_user_sgprs_phase2(struct nir_to_llvm_context
*ctx
,
700 gl_shader_stage stage
,
701 bool has_previous_stage
,
702 gl_shader_stage previous_stage
,
703 uint8_t *user_sgpr_idx
)
705 if (!ctx
->is_gs_copy_shader
&& (stage
== MESA_SHADER_VERTEX
|| (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
706 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
707 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
710 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
713 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, vs_num
);
718 static void create_function(struct nir_to_llvm_context
*ctx
,
719 gl_shader_stage stage
,
720 bool has_previous_stage
,
721 gl_shader_stage previous_stage
)
723 uint8_t user_sgpr_idx
;
724 struct user_sgpr_info user_sgpr_info
;
725 struct arg_info args
= {};
726 LLVMValueRef desc_sets
;
728 allocate_user_sgprs(ctx
, &user_sgpr_info
);
730 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
731 add_user_sgpr_argument(&args
, const_array(ctx
->v4i32
, 16), &ctx
->ring_offsets
); /* address of rings */
735 case MESA_SHADER_COMPUTE
:
736 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
737 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
738 add_user_sgpr_argument(&args
, LLVMVectorType(ctx
->ac
.i32
, ctx
->shader_info
->info
.cs
.grid_components_used
), &ctx
->num_work_groups
); /* grid size */
739 add_sgpr_argument(&args
, LLVMVectorType(ctx
->ac
.i32
, 3), &ctx
->workgroup_ids
);
740 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tg_size
);
741 add_vgpr_argument(&args
, LLVMVectorType(ctx
->ac
.i32
, 3), &ctx
->local_invocation_ids
);
743 case MESA_SHADER_VERTEX
:
744 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
745 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
746 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.vs
.as_es
&& !ctx
->options
->key
.vs
.as_ls
&& ctx
->options
->key
.has_multiview_view_index
))
747 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
748 if (ctx
->options
->key
.vs
.as_es
)
749 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->es2gs_offset
); // es2gs offset
750 else if (ctx
->options
->key
.vs
.as_ls
)
751 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->ls_out_layout
); // ls out layout
752 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
753 if (!ctx
->is_gs_copy_shader
) {
754 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
755 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
756 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
759 case MESA_SHADER_TESS_CTRL
:
760 if (has_previous_stage
) {
761 // First 6 system regs
762 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
763 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->merged_wave_info
); // merged wave info
764 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tess_factor_offset
); // tess factor offset
766 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // scratch offset
767 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
768 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
770 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
771 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
772 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->ls_out_layout
); // ls out layout
774 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
775 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
776 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_layout
); // tcs out layout
777 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_in_layout
); // tcs in layout
778 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
779 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
781 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_patch_id
); // patch id
782 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_rel_ids
); // rel ids;
783 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
784 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
785 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
786 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
788 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
789 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
790 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
791 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_layout
); // tcs out layout
792 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_in_layout
); // tcs in layout
793 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
794 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
795 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
796 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tess_factor_offset
); // tess factor offset
797 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_patch_id
); // patch id
798 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_rel_ids
); // rel ids;
801 case MESA_SHADER_TESS_EVAL
:
802 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
803 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
804 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
805 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
806 if (ctx
->options
->key
.tes
.as_es
) {
807 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // OC LDS
808 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); //
809 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->es2gs_offset
); // es2gs offset
811 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); //
812 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // OC LDS
814 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_u
); // tes_u
815 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_v
); // tes_v
816 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
817 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_patch_id
); // tes patch id
819 case MESA_SHADER_GEOMETRY
:
820 if (has_previous_stage
) {
821 // First 6 system regs
822 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
); // tess factor offset
823 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->merged_wave_info
); // merged wave info
824 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
826 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // scratch offset
827 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
828 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
830 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
831 if (previous_stage
== MESA_SHADER_TESS_EVAL
)
832 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
834 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
835 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
836 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
837 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
838 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
840 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[0]); // vtx01
841 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[2]); // vtx23
842 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_prim_id
); // prim id
843 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_invocation_id
);
844 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[4]);
846 if (previous_stage
== MESA_SHADER_VERTEX
) {
847 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
848 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
849 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
850 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
852 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_u
); // tes_u
853 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_v
); // tes_v
854 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
855 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_patch_id
); // tes patch id
858 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
859 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
860 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
861 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
862 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
863 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
864 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
); // gs2vs offset
865 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_wave_id
); // wave id
866 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[0]); // vtx0
867 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[1]); // vtx1
868 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_prim_id
); // prim id
869 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[2]);
870 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[3]);
871 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[4]);
872 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[5]);
873 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_invocation_id
);
876 case MESA_SHADER_FRAGMENT
:
877 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
878 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
879 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->sample_pos_offset
); /* sample position offset */
880 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->prim_mask
); /* prim mask */
881 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_sample
); /* persp sample */
882 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_center
); /* persp center */
883 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_centroid
); /* persp centroid */
884 add_vgpr_argument(&args
, ctx
->v3i32
, NULL
); /* persp pull model */
885 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_sample
); /* linear sample */
886 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_center
); /* linear center */
887 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_centroid
); /* linear centroid */
888 add_vgpr_argument(&args
, ctx
->f32
, NULL
); /* line stipple tex */
889 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[0]); /* pos x float */
890 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[1]); /* pos y float */
891 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[2]); /* pos z float */
892 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[3]); /* pos w float */
893 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.front_face
); /* front face */
894 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
); /* ancillary */
895 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
); /* sample coverage */
896 add_vgpr_argument(&args
, ctx
->ac
.i32
, NULL
); /* fixed pt */
899 unreachable("Shader stage not implemented");
902 ctx
->main_function
= create_llvm_function(
903 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
904 ctx
->max_workgroup_size
,
905 ctx
->options
->unsafe_math
);
906 set_llvm_calling_convention(ctx
->main_function
, stage
);
909 ctx
->shader_info
->num_input_vgprs
= 0;
910 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
912 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
914 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
915 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
917 assign_arguments(ctx
->main_function
, &args
);
921 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
922 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, &user_sgpr_idx
, 2);
923 if (ctx
->options
->supports_spill
) {
924 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
925 LLVMPointerType(ctx
->ac
.i8
, CONST_ADDR_SPACE
),
926 NULL
, 0, AC_FUNC_ATTR_READNONE
);
927 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
928 const_array(ctx
->v4i32
, 16), "");
932 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
933 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
934 if (has_previous_stage
)
937 radv_define_common_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
940 case MESA_SHADER_COMPUTE
:
941 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
942 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, &user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
945 case MESA_SHADER_VERTEX
:
946 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
948 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
949 if (ctx
->options
->key
.vs
.as_ls
) {
950 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
952 if (ctx
->options
->key
.vs
.as_ls
)
953 ac_declare_lds_as_pointer(&ctx
->ac
);
955 case MESA_SHADER_TESS_CTRL
:
956 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
957 if (has_previous_stage
)
958 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
959 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
961 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
962 ac_declare_lds_as_pointer(&ctx
->ac
);
964 case MESA_SHADER_TESS_EVAL
:
965 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
967 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
969 case MESA_SHADER_GEOMETRY
:
970 if (has_previous_stage
) {
971 if (previous_stage
== MESA_SHADER_VERTEX
)
972 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
974 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
976 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, &user_sgpr_idx
, 2);
978 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
979 if (has_previous_stage
)
980 ac_declare_lds_as_pointer(&ctx
->ac
);
982 case MESA_SHADER_FRAGMENT
:
983 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
984 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, &user_sgpr_idx
, 1);
988 unreachable("Shader stage not implemented");
991 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
994 static void setup_types(struct nir_to_llvm_context
*ctx
)
996 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
997 ctx
->v2i32
= LLVMVectorType(ctx
->ac
.i32
, 2);
998 ctx
->v3i32
= LLVMVectorType(ctx
->ac
.i32
, 3);
999 ctx
->v4i32
= LLVMVectorType(ctx
->ac
.i32
, 4);
1000 ctx
->v8i32
= LLVMVectorType(ctx
->ac
.i32
, 8);
1001 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
1002 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
1003 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
1004 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
1005 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
1007 ctx
->uniform_md_kind
=
1008 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
1009 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
1012 static int get_llvm_num_components(LLVMValueRef value
)
1014 LLVMTypeRef type
= LLVMTypeOf(value
);
1015 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1016 ? LLVMGetVectorSize(type
)
1018 return num_components
;
1021 static LLVMValueRef
llvm_extract_elem(struct ac_llvm_context
*ac
,
1025 int count
= get_llvm_num_components(value
);
1030 return LLVMBuildExtractElement(ac
->builder
, value
,
1031 LLVMConstInt(ac
->i32
, index
, false), "");
1034 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1035 LLVMValueRef value
, unsigned count
)
1037 unsigned num_components
= get_llvm_num_components(value
);
1038 if (count
== num_components
)
1041 LLVMValueRef masks
[] = {
1042 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1043 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1046 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1049 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1050 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1054 build_store_values_extended(struct ac_llvm_context
*ac
,
1055 LLVMValueRef
*values
,
1056 unsigned value_count
,
1057 unsigned value_stride
,
1060 LLVMBuilderRef builder
= ac
->builder
;
1063 for (i
= 0; i
< value_count
; i
++) {
1064 LLVMValueRef ptr
= values
[i
* value_stride
];
1065 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1066 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1067 LLVMBuildStore(builder
, value
, ptr
);
1071 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1072 const nir_ssa_def
*def
)
1074 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1075 if (def
->num_components
> 1) {
1076 type
= LLVMVectorType(type
, def
->num_components
);
1081 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1084 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1085 return (LLVMValueRef
)entry
->data
;
1089 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1090 const struct nir_block
*b
)
1092 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1093 return (LLVMBasicBlockRef
)entry
->data
;
1096 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1098 unsigned num_components
)
1100 LLVMValueRef value
= get_src(ctx
, src
.src
);
1101 bool need_swizzle
= false;
1104 LLVMTypeRef type
= LLVMTypeOf(value
);
1105 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1106 ? LLVMGetVectorSize(type
)
1109 for (unsigned i
= 0; i
< num_components
; ++i
) {
1110 assert(src
.swizzle
[i
] < src_components
);
1111 if (src
.swizzle
[i
] != i
)
1112 need_swizzle
= true;
1115 if (need_swizzle
|| num_components
!= src_components
) {
1116 LLVMValueRef masks
[] = {
1117 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1118 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1119 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1120 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1122 if (src_components
> 1 && num_components
== 1) {
1123 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1125 } else if (src_components
== 1 && num_components
> 1) {
1126 LLVMValueRef values
[] = {value
, value
, value
, value
};
1127 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1129 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1130 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1134 assert(!src
.negate
);
1139 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1140 LLVMIntPredicate pred
, LLVMValueRef src0
,
1143 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1144 return LLVMBuildSelect(ctx
->builder
, result
,
1145 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1146 LLVMConstInt(ctx
->i32
, 0, false), "");
1149 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1150 LLVMRealPredicate pred
, LLVMValueRef src0
,
1153 LLVMValueRef result
;
1154 src0
= ac_to_float(ctx
, src0
);
1155 src1
= ac_to_float(ctx
, src1
);
1156 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1157 return LLVMBuildSelect(ctx
->builder
, result
,
1158 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1159 LLVMConstInt(ctx
->i32
, 0, false), "");
1162 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1164 LLVMTypeRef result_type
,
1168 LLVMValueRef params
[] = {
1169 ac_to_float(ctx
, src0
),
1172 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1173 get_elem_bits(ctx
, result_type
));
1174 assert(length
< sizeof(name
));
1175 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1178 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1180 LLVMTypeRef result_type
,
1181 LLVMValueRef src0
, LLVMValueRef src1
)
1184 LLVMValueRef params
[] = {
1185 ac_to_float(ctx
, src0
),
1186 ac_to_float(ctx
, src1
),
1189 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1190 get_elem_bits(ctx
, result_type
));
1191 assert(length
< sizeof(name
));
1192 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1195 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1197 LLVMTypeRef result_type
,
1198 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1201 LLVMValueRef params
[] = {
1202 ac_to_float(ctx
, src0
),
1203 ac_to_float(ctx
, src1
),
1204 ac_to_float(ctx
, src2
),
1207 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1208 get_elem_bits(ctx
, result_type
));
1209 assert(length
< sizeof(name
));
1210 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1213 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1214 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1216 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1218 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1221 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1222 LLVMIntPredicate pred
,
1223 LLVMValueRef src0
, LLVMValueRef src1
)
1225 return LLVMBuildSelect(ctx
->builder
,
1226 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1231 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1234 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1235 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1238 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1241 LLVMValueRef cmp
, val
;
1243 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32_0
, "");
1244 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32_1
, src0
, "");
1245 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32_0
, "");
1246 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1250 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1253 LLVMValueRef cmp
, val
;
1255 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32_0
, "");
1256 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32_1
, src0
, "");
1257 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32_0
, "");
1258 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1262 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1265 const char *intr
= "llvm.floor.f32";
1266 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1267 LLVMValueRef params
[] = {
1270 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1271 ctx
->f32
, params
, 1,
1272 AC_FUNC_ATTR_READNONE
);
1273 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1276 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1278 LLVMValueRef src0
, LLVMValueRef src1
)
1280 LLVMTypeRef ret_type
;
1281 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1283 LLVMValueRef params
[] = { src0
, src1
};
1284 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1287 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1288 params
, 2, AC_FUNC_ATTR_READNONE
);
1290 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1291 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1295 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1298 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1301 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1304 src0
= ac_to_float(ctx
, src0
);
1305 return LLVMBuildSExt(ctx
->builder
,
1306 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1310 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1313 return LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1316 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1319 return LLVMBuildSExt(ctx
->builder
,
1320 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1324 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1327 LLVMValueRef result
;
1328 LLVMValueRef cond
= NULL
;
1330 src0
= ac_to_float(&ctx
->ac
, src0
);
1331 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
1333 if (ctx
->options
->chip_class
>= VI
) {
1334 LLVMValueRef args
[2];
1335 /* Check if the result is a denormal - and flush to 0 if so. */
1337 args
[1] = LLVMConstInt(ctx
->ac
.i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1338 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->ac
.i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1341 /* need to convert back up to f32 */
1342 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1344 if (ctx
->options
->chip_class
>= VI
)
1345 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1348 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1349 * so compare the result and flush to 0 if it's smaller.
1351 LLVMValueRef temp
, cond2
;
1352 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1354 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1355 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, 0x38800000, false), ctx
->f32
, ""),
1357 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1358 temp
, ctx
->ac
.f32_0
, "");
1359 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1360 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1365 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1366 LLVMValueRef src0
, LLVMValueRef src1
)
1368 LLVMValueRef dst64
, result
;
1369 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1370 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1372 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1373 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1374 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1378 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1379 LLVMValueRef src0
, LLVMValueRef src1
)
1381 LLVMValueRef dst64
, result
;
1382 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1383 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1385 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1386 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1387 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1391 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1393 const LLVMValueRef srcs
[3])
1395 LLVMValueRef result
;
1396 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1398 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1399 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1403 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1404 LLVMValueRef src0
, LLVMValueRef src1
,
1405 LLVMValueRef src2
, LLVMValueRef src3
)
1407 LLVMValueRef bfi_args
[3], result
;
1409 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1410 LLVMBuildSub(ctx
->builder
,
1411 LLVMBuildShl(ctx
->builder
,
1416 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1419 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1422 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1423 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1425 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1426 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1427 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1429 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1433 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1436 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1438 LLVMValueRef comp
[2];
1440 src0
= ac_to_float(ctx
, src0
);
1441 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1442 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1443 for (i
= 0; i
< 2; i
++) {
1444 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1445 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1446 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1449 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1450 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1455 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1458 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1459 LLVMValueRef temps
[2], result
, val
;
1462 for (i
= 0; i
< 2; i
++) {
1463 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1464 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1465 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1466 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1469 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
1470 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(v2f32
), temps
[0],
1472 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1477 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1483 LLVMValueRef result
;
1485 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1486 mask
= AC_TID_MASK_LEFT
;
1487 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1488 mask
= AC_TID_MASK_TOP
;
1490 mask
= AC_TID_MASK_TOP_LEFT
;
1492 /* for DDX we want to next X pixel, DDY next Y pixel. */
1493 if (op
== nir_op_fddx_fine
||
1494 op
== nir_op_fddx_coarse
||
1500 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1505 * this takes an I,J coordinate pair,
1506 * and works out the X and Y derivatives.
1507 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1509 static LLVMValueRef
emit_ddxy_interp(
1510 struct ac_nir_context
*ctx
,
1511 LLVMValueRef interp_ij
)
1513 LLVMValueRef result
[4], a
;
1516 for (i
= 0; i
< 2; i
++) {
1517 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1518 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1519 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1520 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1522 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1525 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1527 LLVMValueRef src
[4], result
= NULL
;
1528 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1529 unsigned src_components
;
1530 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1532 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1533 switch (instr
->op
) {
1539 case nir_op_pack_half_2x16
:
1542 case nir_op_unpack_half_2x16
:
1546 src_components
= num_components
;
1549 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1550 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1552 switch (instr
->op
) {
1558 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1559 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1562 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1565 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1568 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1571 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1572 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1573 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1576 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1577 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1578 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1581 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1584 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1587 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1590 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1593 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1594 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1595 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1596 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1597 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1598 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1599 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1602 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1603 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1604 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1607 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1610 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1613 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1616 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1617 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1618 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1621 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1622 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1623 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1626 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1627 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, src
[0]);
1630 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1633 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1636 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1639 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1640 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1641 LLVMTypeOf(src
[0]), ""),
1645 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1646 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1647 LLVMTypeOf(src
[0]), ""),
1651 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1652 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1653 LLVMTypeOf(src
[0]), ""),
1657 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1660 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1663 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1666 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1669 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1672 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1675 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1678 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1681 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1684 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1687 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1688 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1691 result
= emit_iabs(&ctx
->ac
, src
[0]);
1694 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1697 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1700 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1703 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1706 result
= emit_isign(&ctx
->ac
, src
[0]);
1709 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1710 result
= emit_fsign(&ctx
->ac
, src
[0]);
1713 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1714 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1717 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1718 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1721 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1722 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1724 case nir_op_fround_even
:
1725 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1726 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1729 result
= emit_ffract(&ctx
->ac
, src
[0]);
1732 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1733 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1736 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1737 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1740 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1741 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1744 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1745 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1748 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1749 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1752 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1753 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1754 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, result
);
1757 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1758 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1761 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1762 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1763 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1764 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1765 ac_to_float_type(&ctx
->ac
, def_type
),
1769 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1770 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1771 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1772 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1773 ac_to_float_type(&ctx
->ac
, def_type
),
1777 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1778 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1780 case nir_op_ibitfield_extract
:
1781 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1783 case nir_op_ubitfield_extract
:
1784 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1786 case nir_op_bitfield_insert
:
1787 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1789 case nir_op_bitfield_reverse
:
1790 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1792 case nir_op_bit_count
:
1793 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1798 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1799 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1800 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1804 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1805 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1809 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1810 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1814 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1815 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1819 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1820 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1823 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1826 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1830 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1831 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1832 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1834 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1838 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1839 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1840 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1842 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1845 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1847 case nir_op_find_lsb
:
1848 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1849 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1851 case nir_op_ufind_msb
:
1852 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1853 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1855 case nir_op_ifind_msb
:
1856 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1857 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1859 case nir_op_uadd_carry
:
1860 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1861 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1862 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1864 case nir_op_usub_borrow
:
1865 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1866 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1867 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1870 result
= emit_b2f(&ctx
->ac
, src
[0]);
1873 result
= emit_f2b(&ctx
->ac
, src
[0]);
1876 result
= emit_b2i(&ctx
->ac
, src
[0]);
1879 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1880 result
= emit_i2b(&ctx
->ac
, src
[0]);
1882 case nir_op_fquantize2f16
:
1883 result
= emit_f2f16(ctx
->nctx
, src
[0]);
1885 case nir_op_umul_high
:
1886 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1887 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1888 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1890 case nir_op_imul_high
:
1891 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1892 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1893 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1895 case nir_op_pack_half_2x16
:
1896 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
1898 case nir_op_unpack_half_2x16
:
1899 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1903 case nir_op_fddx_fine
:
1904 case nir_op_fddy_fine
:
1905 case nir_op_fddx_coarse
:
1906 case nir_op_fddy_coarse
:
1907 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1910 case nir_op_unpack_64_2x32_split_x
: {
1911 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1912 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1913 LLVMVectorType(ctx
->ac
.i32
, 2),
1915 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1920 case nir_op_unpack_64_2x32_split_y
: {
1921 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1922 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1923 LLVMVectorType(ctx
->ac
.i32
, 2),
1925 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1930 case nir_op_pack_64_2x32_split
: {
1931 LLVMValueRef tmp
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, 2));
1932 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1933 src
[0], ctx
->ac
.i32_0
, "");
1934 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1935 src
[1], ctx
->ac
.i32_1
, "");
1936 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1941 fprintf(stderr
, "Unknown NIR alu instr: ");
1942 nir_print_instr(&instr
->instr
, stderr
);
1943 fprintf(stderr
, "\n");
1948 assert(instr
->dest
.dest
.is_ssa
);
1949 result
= ac_to_integer(&ctx
->ac
, result
);
1950 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1955 static void visit_load_const(struct ac_nir_context
*ctx
,
1956 const nir_load_const_instr
*instr
)
1958 LLVMValueRef values
[4], value
= NULL
;
1959 LLVMTypeRef element_type
=
1960 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1962 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1963 switch (instr
->def
.bit_size
) {
1965 values
[i
] = LLVMConstInt(element_type
,
1966 instr
->value
.u32
[i
], false);
1969 values
[i
] = LLVMConstInt(element_type
,
1970 instr
->value
.u64
[i
], false);
1974 "unsupported nir load_const bit_size: %d\n",
1975 instr
->def
.bit_size
);
1979 if (instr
->def
.num_components
> 1) {
1980 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1984 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1987 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1990 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1991 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1992 LLVMPointerType(type
, addr_space
), "");
1996 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1999 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2000 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
2003 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
2004 /* On VI, the descriptor contains the size in bytes,
2005 * but TXQ must return the size in elements.
2006 * The stride is always non-zero for resources using TXQ.
2008 LLVMValueRef stride
=
2009 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2010 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
2011 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2012 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2013 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2014 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2016 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2022 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2025 static void build_int_type_name(
2027 char *buf
, unsigned bufsize
)
2029 assert(bufsize
>= 6);
2031 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2032 snprintf(buf
, bufsize
, "v%ui32",
2033 LLVMGetVectorSize(type
));
2038 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2039 struct ac_image_args
*args
,
2040 const nir_tex_instr
*instr
)
2042 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2043 LLVMValueRef coord
= args
->addr
;
2044 LLVMValueRef half_texel
[2];
2045 LLVMValueRef compare_cube_wa
= NULL
;
2046 LLVMValueRef result
;
2048 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2052 struct ac_image_args txq_args
= { 0 };
2054 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2055 txq_args
.opcode
= ac_image_get_resinfo
;
2056 txq_args
.dmask
= 0xf;
2057 txq_args
.addr
= ctx
->i32_0
;
2058 txq_args
.resource
= args
->resource
;
2059 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2061 for (c
= 0; c
< 2; c
++) {
2062 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2063 LLVMConstInt(ctx
->i32
, c
, false), "");
2064 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2065 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2066 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2067 LLVMConstReal(ctx
->f32
, -0.5), "");
2071 LLVMValueRef orig_coords
= args
->addr
;
2073 for (c
= 0; c
< 2; c
++) {
2075 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2076 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2077 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2078 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2079 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2080 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2085 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2086 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2087 * workaround by sampling using a scaled type and converting.
2088 * This is taken from amdgpu-pro shaders.
2090 /* NOTE this produces some ugly code compared to amdgpu-pro,
2091 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2092 * and then reads them back. -pro generates two selects,
2093 * one s_cmp for the descriptor rewriting
2094 * one v_cmp for the coordinate and result changes.
2096 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2097 LLVMValueRef tmp
, tmp2
;
2099 /* workaround 8/8/8/8 uint/sint cube gather bug */
2100 /* first detect it then change to a scaled read and f2i */
2101 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2104 /* extract the DATA_FORMAT */
2105 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2106 LLVMConstInt(ctx
->i32
, 6, false), false);
2108 /* is the DATA_FORMAT == 8_8_8_8 */
2109 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2111 if (stype
== GLSL_TYPE_UINT
)
2112 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2113 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2114 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2116 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2117 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2118 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2120 /* replace the NUM FORMAT in the descriptor */
2121 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2122 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2124 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2126 /* don't modify the coordinates for this case */
2127 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2130 result
= ac_build_image_opcode(ctx
, args
);
2132 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2133 LLVMValueRef tmp
, tmp2
;
2135 /* if the cube workaround is in place, f2i the result. */
2136 for (c
= 0; c
< 4; c
++) {
2137 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2138 if (stype
== GLSL_TYPE_UINT
)
2139 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2141 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2142 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2143 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2144 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2145 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2146 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2152 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2153 const nir_tex_instr
*instr
,
2155 struct ac_image_args
*args
)
2157 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2158 return ac_build_buffer_load_format(&ctx
->ac
,
2161 LLVMConstInt(ctx
->ac
.i32
, 0, false),
2165 args
->opcode
= ac_image_sample
;
2166 args
->compare
= instr
->is_shadow
;
2168 switch (instr
->op
) {
2170 case nir_texop_txf_ms
:
2171 case nir_texop_samples_identical
:
2172 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2173 args
->compare
= false;
2174 args
->offset
= false;
2181 args
->level_zero
= true;
2186 case nir_texop_query_levels
:
2187 args
->opcode
= ac_image_get_resinfo
;
2190 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2191 args
->level_zero
= true;
2197 args
->opcode
= ac_image_gather4
;
2198 args
->level_zero
= true;
2201 args
->opcode
= ac_image_get_lod
;
2202 args
->compare
= false;
2203 args
->offset
= false;
2209 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2210 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2211 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2212 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2215 return ac_build_image_opcode(&ctx
->ac
, args
);
2218 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2219 nir_intrinsic_instr
*instr
)
2221 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2222 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2223 unsigned binding
= nir_intrinsic_binding(instr
);
2224 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2225 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2226 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2227 unsigned base_offset
= layout
->binding
[binding
].offset
;
2228 LLVMValueRef offset
, stride
;
2230 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2231 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2232 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2233 layout
->binding
[binding
].dynamic_offset_offset
;
2234 desc_ptr
= ctx
->push_constants
;
2235 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2236 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2238 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2240 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2241 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2242 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2244 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2245 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2246 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2248 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2251 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2252 nir_intrinsic_instr
*instr
)
2254 LLVMValueRef ptr
, addr
;
2256 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2257 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2259 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2260 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2262 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2265 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2266 const nir_intrinsic_instr
*instr
)
2268 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2270 return get_buffer_size(ctx
, desc
, false);
2272 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2273 nir_intrinsic_instr
*instr
)
2275 const char *store_name
;
2276 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2277 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2278 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2279 int components_32bit
= elem_size_mult
* instr
->num_components
;
2280 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2281 LLVMValueRef base_data
, base_offset
;
2282 LLVMValueRef params
[6];
2284 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2285 get_src(ctx
, instr
->src
[1]), true);
2286 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 0, false); /* vindex */
2287 params
[4] = ctx
->ac
.i1false
; /* glc */
2288 params
[5] = ctx
->ac
.i1false
; /* slc */
2290 if (components_32bit
> 1)
2291 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2293 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2294 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2295 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2297 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2301 LLVMValueRef offset
;
2303 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2305 /* Due to an LLVM limitation, split 3-element writes
2306 * into a 2-element and a 1-element write. */
2308 writemask
|= 1 << (start
+ 2);
2312 start
*= elem_size_mult
;
2313 count
*= elem_size_mult
;
2316 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2321 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2323 } else if (count
== 2) {
2324 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->ac
.f32
, 2);
2326 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2327 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2328 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(v2f32
), tmp
,
2331 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2332 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2333 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2335 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2339 if (get_llvm_num_components(base_data
) > 1)
2340 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2341 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2344 store_name
= "llvm.amdgcn.buffer.store.f32";
2347 offset
= base_offset
;
2349 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2353 ac_build_intrinsic(&ctx
->ac
, store_name
,
2354 ctx
->ac
.voidt
, params
, 6, 0);
2358 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2359 const nir_intrinsic_instr
*instr
)
2362 LLVMValueRef params
[6];
2365 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2366 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2368 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2369 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2370 get_src(ctx
, instr
->src
[0]),
2372 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i32
, 0, false); /* vindex */
2373 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2374 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2376 switch (instr
->intrinsic
) {
2377 case nir_intrinsic_ssbo_atomic_add
:
2378 name
= "llvm.amdgcn.buffer.atomic.add";
2380 case nir_intrinsic_ssbo_atomic_imin
:
2381 name
= "llvm.amdgcn.buffer.atomic.smin";
2383 case nir_intrinsic_ssbo_atomic_umin
:
2384 name
= "llvm.amdgcn.buffer.atomic.umin";
2386 case nir_intrinsic_ssbo_atomic_imax
:
2387 name
= "llvm.amdgcn.buffer.atomic.smax";
2389 case nir_intrinsic_ssbo_atomic_umax
:
2390 name
= "llvm.amdgcn.buffer.atomic.umax";
2392 case nir_intrinsic_ssbo_atomic_and
:
2393 name
= "llvm.amdgcn.buffer.atomic.and";
2395 case nir_intrinsic_ssbo_atomic_or
:
2396 name
= "llvm.amdgcn.buffer.atomic.or";
2398 case nir_intrinsic_ssbo_atomic_xor
:
2399 name
= "llvm.amdgcn.buffer.atomic.xor";
2401 case nir_intrinsic_ssbo_atomic_exchange
:
2402 name
= "llvm.amdgcn.buffer.atomic.swap";
2404 case nir_intrinsic_ssbo_atomic_comp_swap
:
2405 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2411 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2414 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2415 const nir_intrinsic_instr
*instr
)
2417 LLVMValueRef results
[2];
2418 int load_components
;
2419 int num_components
= instr
->num_components
;
2420 if (instr
->dest
.ssa
.bit_size
== 64)
2421 num_components
*= 2;
2423 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2424 load_components
= MIN2(num_components
- i
, 4);
2425 const char *load_name
;
2426 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2427 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2428 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2430 if (load_components
== 3)
2431 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2432 else if (load_components
> 1)
2433 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2435 if (load_components
>= 3)
2436 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2437 else if (load_components
== 2)
2438 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2439 else if (load_components
== 1)
2440 load_name
= "llvm.amdgcn.buffer.load.f32";
2442 unreachable("unhandled number of components");
2444 LLVMValueRef params
[] = {
2445 ctx
->abi
->load_ssbo(ctx
->abi
,
2446 get_src(ctx
, instr
->src
[0]),
2448 LLVMConstInt(ctx
->ac
.i32
, 0, false),
2454 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2459 LLVMValueRef ret
= results
[0];
2460 if (num_components
> 4 || num_components
== 3) {
2461 LLVMValueRef masks
[] = {
2462 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2463 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2464 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2465 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2468 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2469 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2470 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2473 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2474 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2477 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2478 const nir_intrinsic_instr
*instr
)
2480 LLVMValueRef results
[8], ret
;
2481 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2482 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2483 int num_components
= instr
->num_components
;
2485 if (ctx
->abi
->load_ubo
)
2486 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2488 if (instr
->dest
.ssa
.bit_size
== 64)
2489 num_components
*= 2;
2491 for (unsigned i
= 0; i
< num_components
; ++i
) {
2492 LLVMValueRef params
[] = {
2494 LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, 4 * i
, 0),
2497 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const.v4i32", ctx
->ac
.f32
,
2499 AC_FUNC_ATTR_READNONE
|
2500 AC_FUNC_ATTR_LEGACY
);
2504 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2505 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2506 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2510 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2511 bool vs_in
, unsigned *vertex_index_out
,
2512 LLVMValueRef
*vertex_index_ref
,
2513 unsigned *const_out
, LLVMValueRef
*indir_out
)
2515 unsigned const_offset
= 0;
2516 nir_deref
*tail
= &deref
->deref
;
2517 LLVMValueRef offset
= NULL
;
2519 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2521 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2522 if (vertex_index_out
)
2523 *vertex_index_out
= deref_array
->base_offset
;
2525 if (vertex_index_ref
) {
2526 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2527 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2528 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2530 *vertex_index_ref
= vtx
;
2534 if (deref
->var
->data
.compact
) {
2535 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2536 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2537 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2538 /* We always lower indirect dereferences for "compact" array vars. */
2539 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2541 const_offset
= deref_array
->base_offset
;
2545 while (tail
->child
!= NULL
) {
2546 const struct glsl_type
*parent_type
= tail
->type
;
2549 if (tail
->deref_type
== nir_deref_type_array
) {
2550 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2551 LLVMValueRef index
, stride
, local_offset
;
2552 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2554 const_offset
+= size
* deref_array
->base_offset
;
2555 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2558 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2559 index
= get_src(ctx
, deref_array
->indirect
);
2560 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2561 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2564 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2566 offset
= local_offset
;
2567 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2568 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2570 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2571 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2572 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2575 unreachable("unsupported deref type");
2579 if (const_offset
&& offset
)
2580 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2581 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2584 *const_out
= const_offset
;
2585 *indir_out
= offset
;
2589 /* The offchip buffer layout for TCS->TES is
2591 * - attribute 0 of patch 0 vertex 0
2592 * - attribute 0 of patch 0 vertex 1
2593 * - attribute 0 of patch 0 vertex 2
2595 * - attribute 0 of patch 1 vertex 0
2596 * - attribute 0 of patch 1 vertex 1
2598 * - attribute 1 of patch 0 vertex 0
2599 * - attribute 1 of patch 0 vertex 1
2601 * - per patch attribute 0 of patch 0
2602 * - per patch attribute 0 of patch 1
2605 * Note that every attribute has 4 components.
2607 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2608 LLVMValueRef vertex_index
,
2609 LLVMValueRef param_index
)
2611 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2612 LLVMValueRef param_stride
, constant16
;
2613 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2615 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2616 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2617 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2620 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2622 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2623 vertices_per_patch
, "");
2625 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2628 param_stride
= total_vertices
;
2630 base_addr
= rel_patch_id
;
2631 param_stride
= num_patches
;
2634 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2635 LLVMBuildMul(ctx
->builder
, param_index
,
2636 param_stride
, ""), "");
2638 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2640 if (!vertex_index
) {
2641 LLVMValueRef patch_data_offset
=
2642 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2644 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2645 patch_data_offset
, "");
2650 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2652 unsigned const_index
,
2654 LLVMValueRef vertex_index
,
2655 LLVMValueRef indir_index
)
2657 LLVMValueRef param_index
;
2660 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2663 if (const_index
&& !is_compact
)
2664 param
+= const_index
;
2665 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2667 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2671 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2672 bool is_patch
, uint32_t param
)
2676 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2678 ctx
->tess_outputs_written
|= (1ull << param
);
2682 get_dw_address(struct nir_to_llvm_context
*ctx
,
2683 LLVMValueRef dw_addr
,
2685 unsigned const_index
,
2686 bool compact_const_index
,
2687 LLVMValueRef vertex_index
,
2688 LLVMValueRef stride
,
2689 LLVMValueRef indir_index
)
2694 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2695 LLVMBuildMul(ctx
->builder
,
2701 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2702 LLVMBuildMul(ctx
->builder
, indir_index
,
2703 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2704 else if (const_index
&& !compact_const_index
)
2705 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2706 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2708 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2709 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2711 if (const_index
&& compact_const_index
)
2712 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2713 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2718 build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
2719 unsigned value_count
, unsigned component
)
2721 LLVMValueRef vec
= NULL
;
2723 if (value_count
== 1) {
2724 return values
[component
];
2725 } else if (!value_count
)
2726 unreachable("value_count is 0");
2728 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
2729 LLVMValueRef value
= values
[i
];
2732 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
2733 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
2734 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
2740 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2741 nir_intrinsic_instr
*instr
)
2743 LLVMValueRef dw_addr
, stride
;
2744 unsigned const_index
;
2745 LLVMValueRef vertex_index
;
2746 LLVMValueRef indir_index
;
2748 LLVMValueRef value
[4], result
;
2749 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2750 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2751 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2752 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2753 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2754 &const_index
, &indir_index
);
2756 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2757 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2758 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2761 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2762 for (unsigned i
= 0; i
< instr
->num_components
+ comp
; i
++) {
2763 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2764 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2767 result
= build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2768 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2773 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2774 nir_intrinsic_instr
*instr
)
2776 LLVMValueRef dw_addr
;
2777 LLVMValueRef stride
= NULL
;
2778 LLVMValueRef value
[4], result
;
2779 LLVMValueRef vertex_index
= NULL
;
2780 LLVMValueRef indir_index
= NULL
;
2781 unsigned const_index
= 0;
2783 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2784 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2785 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2786 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2787 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2788 &const_index
, &indir_index
);
2790 if (!instr
->variables
[0]->var
->data
.patch
) {
2791 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2792 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2794 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2797 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2800 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2801 for (unsigned i
= comp
; i
< instr
->num_components
+ comp
; i
++) {
2802 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2803 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2806 result
= build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2807 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2812 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2813 nir_intrinsic_instr
*instr
,
2817 LLVMValueRef dw_addr
;
2818 LLVMValueRef stride
= NULL
;
2819 LLVMValueRef buf_addr
= NULL
;
2820 LLVMValueRef vertex_index
= NULL
;
2821 LLVMValueRef indir_index
= NULL
;
2822 unsigned const_index
= 0;
2824 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2825 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2826 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2828 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2829 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2830 &const_index
, &indir_index
);
2832 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2833 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2834 is_compact
&& const_index
> 3) {
2839 if (!instr
->variables
[0]->var
->data
.patch
) {
2840 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2841 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2843 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2846 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2848 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2850 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2851 vertex_index
, indir_index
);
2853 bool is_tess_factor
= false;
2854 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2855 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2856 is_tess_factor
= true;
2858 unsigned base
= is_compact
? const_index
: 0;
2859 for (unsigned chan
= 0; chan
< 8; chan
++) {
2860 if (!(writemask
& (1 << chan
)))
2862 LLVMValueRef value
= llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2864 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
2866 if (!is_tess_factor
&& writemask
!= 0xF)
2867 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2868 buf_addr
, ctx
->oc_lds
,
2869 4 * (base
+ chan
), 1, 0, true, false);
2871 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2875 if (writemask
== 0xF) {
2876 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2877 buf_addr
, ctx
->oc_lds
,
2878 (base
* 4), 1, 0, true, false);
2883 load_tes_input(struct nir_to_llvm_context
*ctx
,
2884 const nir_intrinsic_instr
*instr
)
2886 LLVMValueRef buf_addr
;
2887 LLVMValueRef result
;
2888 LLVMValueRef vertex_index
= NULL
;
2889 LLVMValueRef indir_index
= NULL
;
2890 unsigned const_index
= 0;
2892 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2893 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2895 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2896 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2897 &const_index
, &indir_index
);
2898 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2899 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2900 is_compact
&& const_index
> 3) {
2905 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2906 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2907 is_compact
, vertex_index
, indir_index
);
2909 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, comp
* 4, false);
2910 buf_addr
= LLVMBuildAdd(ctx
->builder
, buf_addr
, comp_offset
, "");
2912 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2913 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2914 result
= trim_vector(&ctx
->ac
, result
, instr
->num_components
);
2915 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2920 load_gs_input(struct nir_to_llvm_context
*ctx
,
2921 nir_intrinsic_instr
*instr
)
2923 LLVMValueRef indir_index
, vtx_offset
;
2924 unsigned const_index
;
2925 LLVMValueRef args
[9];
2926 unsigned param
, vtx_offset_param
;
2927 LLVMValueRef value
[4], result
;
2928 unsigned vertex_index
;
2929 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2930 false, &vertex_index
, NULL
,
2931 &const_index
, &indir_index
);
2932 vtx_offset_param
= vertex_index
;
2933 assert(vtx_offset_param
< 6);
2934 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2935 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
2937 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2939 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2940 for (unsigned i
= comp
; i
< instr
->num_components
+ comp
; i
++) {
2941 if (ctx
->ac
.chip_class
>= GFX9
) {
2942 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
2943 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2944 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
2945 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2947 args
[0] = ctx
->esgs_ring
;
2948 args
[1] = vtx_offset
;
2949 args
[2] = LLVMConstInt(ctx
->ac
.i32
, (param
* 4 + i
+ const_index
) * 256, false);
2950 args
[3] = ctx
->ac
.i32_0
;
2951 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
2952 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
2953 args
[6] = ctx
->ac
.i32_1
; /* GLC */
2954 args
[7] = ctx
->ac
.i32_0
; /* SLC */
2955 args
[8] = ctx
->ac
.i32_0
; /* TFE */
2957 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2958 ctx
->ac
.i32
, args
, 9,
2959 AC_FUNC_ATTR_READONLY
|
2960 AC_FUNC_ATTR_LEGACY
);
2963 result
= build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2969 build_gep_for_deref(struct ac_nir_context
*ctx
,
2970 nir_deref_var
*deref
)
2972 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
2973 assert(entry
->data
);
2974 LLVMValueRef val
= entry
->data
;
2975 nir_deref
*tail
= deref
->deref
.child
;
2976 while (tail
!= NULL
) {
2977 LLVMValueRef offset
;
2978 switch (tail
->deref_type
) {
2979 case nir_deref_type_array
: {
2980 nir_deref_array
*array
= nir_deref_as_array(tail
);
2981 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
2982 if (array
->deref_array_type
==
2983 nir_deref_array_type_indirect
) {
2984 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2991 case nir_deref_type_struct
: {
2992 nir_deref_struct
*deref_struct
=
2993 nir_deref_as_struct(tail
);
2994 offset
= LLVMConstInt(ctx
->ac
.i32
,
2995 deref_struct
->index
, 0);
2999 unreachable("bad deref type");
3001 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3007 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3008 nir_intrinsic_instr
*instr
)
3010 LLVMValueRef values
[8];
3011 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3012 int ve
= instr
->dest
.ssa
.num_components
;
3013 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3014 LLVMValueRef indir_index
;
3016 unsigned const_index
;
3017 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3018 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3019 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3020 &const_index
, &indir_index
);
3022 if (instr
->dest
.ssa
.bit_size
== 64)
3025 switch (instr
->variables
[0]->var
->data
.mode
) {
3026 case nir_var_shader_in
:
3027 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3028 return load_tcs_input(ctx
->nctx
, instr
);
3029 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3030 return load_tes_input(ctx
->nctx
, instr
);
3031 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3032 return load_gs_input(ctx
->nctx
, instr
);
3035 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3037 unsigned count
= glsl_count_attribute_slots(
3038 instr
->variables
[0]->var
->type
,
3039 ctx
->stage
== MESA_SHADER_VERTEX
);
3041 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3042 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3045 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3049 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* 4];
3053 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3055 unsigned count
= glsl_count_attribute_slots(
3056 instr
->variables
[0]->var
->type
, false);
3058 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3059 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3062 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3066 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
3070 case nir_var_shared
: {
3071 LLVMValueRef address
= build_gep_for_deref(ctx
,
3072 instr
->variables
[0]);
3073 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3074 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3075 get_def_type(ctx
, &instr
->dest
.ssa
),
3078 case nir_var_shader_out
:
3079 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3080 return load_tcs_output(ctx
->nctx
, instr
);
3082 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3084 unsigned count
= glsl_count_attribute_slots(
3085 instr
->variables
[0]->var
->type
, false);
3087 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3088 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3091 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3095 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3096 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
3102 unreachable("unhandle variable mode");
3104 ret
= build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3105 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3109 visit_store_var(struct ac_nir_context
*ctx
,
3110 nir_intrinsic_instr
*instr
)
3112 LLVMValueRef temp_ptr
, value
;
3113 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3114 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3115 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3116 int writemask
= instr
->const_index
[0] << comp
;
3117 LLVMValueRef indir_index
;
3118 unsigned const_index
;
3119 get_deref_offset(ctx
, instr
->variables
[0], false,
3120 NULL
, NULL
, &const_index
, &indir_index
);
3122 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3123 int old_writemask
= writemask
;
3125 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3126 LLVMVectorType(ctx
->ac
.f32
, get_llvm_num_components(src
) * 2),
3130 for (unsigned chan
= 0; chan
< 4; chan
++) {
3131 if (old_writemask
& (1 << chan
))
3132 writemask
|= 3u << (2 * chan
);
3136 switch (instr
->variables
[0]->var
->data
.mode
) {
3137 case nir_var_shader_out
:
3139 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3140 store_tcs_output(ctx
->nctx
, instr
, src
, writemask
);
3144 for (unsigned chan
= 0; chan
< 8; chan
++) {
3146 if (!(writemask
& (1 << chan
)))
3149 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3151 if (instr
->variables
[0]->var
->data
.compact
)
3154 unsigned count
= glsl_count_attribute_slots(
3155 instr
->variables
[0]->var
->type
, false);
3157 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3158 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3159 stride
, true, true);
3161 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3162 value
, indir_index
, "");
3163 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3164 count
, stride
, tmp_vec
);
3167 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3169 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3174 for (unsigned chan
= 0; chan
< 8; chan
++) {
3175 if (!(writemask
& (1 << chan
)))
3178 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
3180 unsigned count
= glsl_count_attribute_slots(
3181 instr
->variables
[0]->var
->type
, false);
3183 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3184 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3187 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3188 value
, indir_index
, "");
3189 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3192 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3194 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3198 case nir_var_shared
: {
3199 int writemask
= instr
->const_index
[0];
3200 LLVMValueRef address
= build_gep_for_deref(ctx
,
3201 instr
->variables
[0]);
3202 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3203 unsigned components
=
3204 glsl_get_vector_elements(
3205 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3206 if (writemask
== (1 << components
) - 1) {
3207 val
= LLVMBuildBitCast(
3208 ctx
->ac
.builder
, val
,
3209 LLVMGetElementType(LLVMTypeOf(address
)), "");
3210 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3212 for (unsigned chan
= 0; chan
< 4; chan
++) {
3213 if (!(writemask
& (1 << chan
)))
3216 LLVMBuildStructGEP(ctx
->ac
.builder
,
3218 LLVMValueRef src
= llvm_extract_elem(&ctx
->ac
, val
,
3220 src
= LLVMBuildBitCast(
3221 ctx
->ac
.builder
, src
,
3222 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3223 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3233 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3236 case GLSL_SAMPLER_DIM_BUF
:
3238 case GLSL_SAMPLER_DIM_1D
:
3239 return array
? 2 : 1;
3240 case GLSL_SAMPLER_DIM_2D
:
3241 return array
? 3 : 2;
3242 case GLSL_SAMPLER_DIM_MS
:
3243 return array
? 4 : 3;
3244 case GLSL_SAMPLER_DIM_3D
:
3245 case GLSL_SAMPLER_DIM_CUBE
:
3247 case GLSL_SAMPLER_DIM_RECT
:
3248 case GLSL_SAMPLER_DIM_SUBPASS
:
3250 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3260 /* Adjust the sample index according to FMASK.
3262 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3263 * which is the identity mapping. Each nibble says which physical sample
3264 * should be fetched to get that sample.
3266 * For example, 0x11111100 means there are only 2 samples stored and
3267 * the second sample covers 3/4 of the pixel. When reading samples 0
3268 * and 1, return physical sample 0 (determined by the first two 0s
3269 * in FMASK), otherwise return physical sample 1.
3271 * The sample index should be adjusted as follows:
3272 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3274 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3275 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3276 LLVMValueRef coord_z
,
3277 LLVMValueRef sample_index
,
3278 LLVMValueRef fmask_desc_ptr
)
3280 LLVMValueRef fmask_load_address
[4];
3283 fmask_load_address
[0] = coord_x
;
3284 fmask_load_address
[1] = coord_y
;
3286 fmask_load_address
[2] = coord_z
;
3287 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3290 struct ac_image_args args
= {0};
3292 args
.opcode
= ac_image_load
;
3293 args
.da
= coord_z
? true : false;
3294 args
.resource
= fmask_desc_ptr
;
3296 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3298 res
= ac_build_image_opcode(ctx
, &args
);
3300 res
= ac_to_integer(ctx
, res
);
3301 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3302 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3304 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3308 LLVMValueRef sample_index4
=
3309 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3310 LLVMValueRef shifted_fmask
=
3311 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3312 LLVMValueRef final_sample
=
3313 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3315 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3316 * resource descriptor is 0 (invalid),
3318 LLVMValueRef fmask_desc
=
3319 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3322 LLVMValueRef fmask_word1
=
3323 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3326 LLVMValueRef word1_is_nonzero
=
3327 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3328 fmask_word1
, ctx
->i32_0
, "");
3330 /* Replace the MSAA sample index. */
3332 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3333 final_sample
, sample_index
, "");
3334 return sample_index
;
3337 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3338 const nir_intrinsic_instr
*instr
)
3340 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3341 if(instr
->variables
[0]->deref
.child
)
3342 type
= instr
->variables
[0]->deref
.child
->type
;
3344 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3345 LLVMValueRef coords
[4];
3346 LLVMValueRef masks
[] = {
3347 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3348 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3351 LLVMValueRef sample_index
= llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3354 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3355 bool is_array
= glsl_sampler_type_is_array(type
);
3356 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3357 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3358 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3359 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3360 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3361 count
= image_type_to_components_count(dim
, is_array
);
3364 LLVMValueRef fmask_load_address
[3];
3367 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3368 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3370 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3372 fmask_load_address
[2] = NULL
;
3374 for (chan
= 0; chan
< 2; ++chan
)
3375 fmask_load_address
[chan
] =
3376 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3377 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3378 ctx
->ac
.i32
, ""), "");
3379 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3381 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3382 fmask_load_address
[0],
3383 fmask_load_address
[1],
3384 fmask_load_address
[2],
3386 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3388 if (count
== 1 && !gfx9_1d
) {
3389 if (instr
->src
[0].ssa
->num_components
)
3390 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3397 for (chan
= 0; chan
< count
; ++chan
) {
3398 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3401 for (chan
= 0; chan
< 2; ++chan
)
3402 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3403 ctx
->ac
.i32
, ""), "");
3404 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3410 coords
[2] = coords
[1];
3411 coords
[1] = ctx
->ac
.i32_0
;
3413 coords
[1] = ctx
->ac
.i32_0
;
3418 coords
[count
] = sample_index
;
3423 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3426 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3431 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3432 const nir_intrinsic_instr
*instr
)
3434 LLVMValueRef params
[7];
3436 char intrinsic_name
[64];
3437 const nir_variable
*var
= instr
->variables
[0]->var
;
3438 const struct glsl_type
*type
= var
->type
;
3440 if(instr
->variables
[0]->deref
.child
)
3441 type
= instr
->variables
[0]->deref
.child
->type
;
3443 type
= glsl_without_array(type
);
3444 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3445 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3446 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3447 ctx
->ac
.i32_0
, ""); /* vindex */
3448 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3449 params
[3] = ctx
->ac
.i1false
; /* glc */
3450 params
[4] = ctx
->ac
.i1false
; /* slc */
3451 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3454 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3455 res
= ac_to_integer(&ctx
->ac
, res
);
3457 bool is_da
= glsl_sampler_type_is_array(type
) ||
3458 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3459 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3460 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3461 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3462 LLVMValueRef glc
= ctx
->ac
.i1false
;
3463 LLVMValueRef slc
= ctx
->ac
.i1false
;
3465 params
[0] = get_image_coords(ctx
, instr
);
3466 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3467 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3468 if (HAVE_LLVM
<= 0x0309) {
3469 params
[3] = ctx
->ac
.i1false
; /* r128 */
3474 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3481 ac_get_image_intr_name("llvm.amdgcn.image.load",
3482 ctx
->ac
.v4f32
, /* vdata */
3483 LLVMTypeOf(params
[0]), /* coords */
3484 LLVMTypeOf(params
[1]), /* rsrc */
3485 intrinsic_name
, sizeof(intrinsic_name
));
3487 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3488 params
, 7, AC_FUNC_ATTR_READONLY
);
3490 return ac_to_integer(&ctx
->ac
, res
);
3493 static void visit_image_store(struct ac_nir_context
*ctx
,
3494 nir_intrinsic_instr
*instr
)
3496 LLVMValueRef params
[8];
3497 char intrinsic_name
[64];
3498 const nir_variable
*var
= instr
->variables
[0]->var
;
3499 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3500 LLVMValueRef glc
= ctx
->ac
.i1false
;
3501 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3503 glc
= ctx
->ac
.i1true
;
3505 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3506 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3507 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3508 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3509 ctx
->ac
.i32_0
, ""); /* vindex */
3510 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3511 params
[4] = glc
; /* glc */
3512 params
[5] = ctx
->ac
.i1false
; /* slc */
3513 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3516 bool is_da
= glsl_sampler_type_is_array(type
) ||
3517 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3518 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3519 LLVMValueRef slc
= ctx
->ac
.i1false
;
3521 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3522 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3523 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3524 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3525 if (HAVE_LLVM
<= 0x0309) {
3526 params
[4] = ctx
->ac
.i1false
; /* r128 */
3531 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3538 ac_get_image_intr_name("llvm.amdgcn.image.store",
3539 LLVMTypeOf(params
[0]), /* vdata */
3540 LLVMTypeOf(params
[1]), /* coords */
3541 LLVMTypeOf(params
[2]), /* rsrc */
3542 intrinsic_name
, sizeof(intrinsic_name
));
3544 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3550 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3551 const nir_intrinsic_instr
*instr
)
3553 LLVMValueRef params
[7];
3554 int param_count
= 0;
3555 const nir_variable
*var
= instr
->variables
[0]->var
;
3557 const char *atomic_name
;
3558 char intrinsic_name
[41];
3559 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3560 MAYBE_UNUSED
int length
;
3562 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3564 switch (instr
->intrinsic
) {
3565 case nir_intrinsic_image_atomic_add
:
3566 atomic_name
= "add";
3568 case nir_intrinsic_image_atomic_min
:
3569 atomic_name
= is_unsigned
? "umin" : "smin";
3571 case nir_intrinsic_image_atomic_max
:
3572 atomic_name
= is_unsigned
? "umax" : "smax";
3574 case nir_intrinsic_image_atomic_and
:
3575 atomic_name
= "and";
3577 case nir_intrinsic_image_atomic_or
:
3580 case nir_intrinsic_image_atomic_xor
:
3581 atomic_name
= "xor";
3583 case nir_intrinsic_image_atomic_exchange
:
3584 atomic_name
= "swap";
3586 case nir_intrinsic_image_atomic_comp_swap
:
3587 atomic_name
= "cmpswap";
3593 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3594 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3595 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3597 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3598 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3600 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3601 ctx
->ac
.i32_0
, ""); /* vindex */
3602 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3603 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3605 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3606 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3608 char coords_type
[8];
3610 bool da
= glsl_sampler_type_is_array(type
) ||
3611 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3613 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3614 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3616 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3617 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3618 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3620 build_int_type_name(LLVMTypeOf(coords
),
3621 coords_type
, sizeof(coords_type
));
3623 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3624 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3627 assert(length
< sizeof(intrinsic_name
));
3628 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3631 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3632 const nir_intrinsic_instr
*instr
)
3635 const nir_variable
*var
= instr
->variables
[0]->var
;
3636 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3637 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3638 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3639 if(instr
->variables
[0]->deref
.child
)
3640 type
= instr
->variables
[0]->deref
.child
->type
;
3642 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3643 return get_buffer_size(ctx
,
3644 get_sampler_desc(ctx
, instr
->variables
[0],
3645 AC_DESC_BUFFER
, NULL
, true, false), true);
3647 struct ac_image_args args
= { 0 };
3651 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3652 args
.opcode
= ac_image_get_resinfo
;
3653 args
.addr
= ctx
->ac
.i32_0
;
3655 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3657 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3659 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3660 glsl_sampler_type_is_array(type
)) {
3661 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3662 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3663 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3664 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3666 if (ctx
->ac
.chip_class
>= GFX9
&&
3667 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3668 glsl_sampler_type_is_array(type
)) {
3669 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3670 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3677 #define NOOP_WAITCNT 0xf7f
3678 #define LGKM_CNT 0x07f
3679 #define VM_CNT 0xf70
3681 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3684 LLVMValueRef args
[1] = {
3685 LLVMConstInt(ctx
->ac
.i32
, simm16
, false),
3687 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3688 ctx
->ac
.voidt
, args
, 1, 0);
3691 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3693 /* SI only (thanks to a hw bug workaround):
3694 * The real barrier instruction isn’t needed, because an entire patch
3695 * always fits into a single wave.
3697 if (ctx
->options
->chip_class
== SI
&&
3698 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3699 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3702 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3703 ctx
->ac
.voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3706 static void emit_discard_if(struct ac_nir_context
*ctx
,
3707 const nir_intrinsic_instr
*instr
)
3711 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3712 get_src(ctx
, instr
->src
[0]),
3714 ac_build_kill_if_false(&ctx
->ac
, cond
);
3718 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3720 LLVMValueRef result
;
3721 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3722 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3723 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3725 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3728 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3729 const nir_intrinsic_instr
*instr
)
3731 LLVMValueRef ptr
, result
;
3732 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3733 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3735 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3736 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3737 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3739 LLVMAtomicOrderingSequentiallyConsistent
,
3740 LLVMAtomicOrderingSequentiallyConsistent
,
3743 LLVMAtomicRMWBinOp op
;
3744 switch (instr
->intrinsic
) {
3745 case nir_intrinsic_var_atomic_add
:
3746 op
= LLVMAtomicRMWBinOpAdd
;
3748 case nir_intrinsic_var_atomic_umin
:
3749 op
= LLVMAtomicRMWBinOpUMin
;
3751 case nir_intrinsic_var_atomic_umax
:
3752 op
= LLVMAtomicRMWBinOpUMax
;
3754 case nir_intrinsic_var_atomic_imin
:
3755 op
= LLVMAtomicRMWBinOpMin
;
3757 case nir_intrinsic_var_atomic_imax
:
3758 op
= LLVMAtomicRMWBinOpMax
;
3760 case nir_intrinsic_var_atomic_and
:
3761 op
= LLVMAtomicRMWBinOpAnd
;
3763 case nir_intrinsic_var_atomic_or
:
3764 op
= LLVMAtomicRMWBinOpOr
;
3766 case nir_intrinsic_var_atomic_xor
:
3767 op
= LLVMAtomicRMWBinOpXor
;
3769 case nir_intrinsic_var_atomic_exchange
:
3770 op
= LLVMAtomicRMWBinOpXchg
;
3776 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3777 LLVMAtomicOrderingSequentiallyConsistent
,
3783 #define INTERP_CENTER 0
3784 #define INTERP_CENTROID 1
3785 #define INTERP_SAMPLE 2
3787 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3788 enum glsl_interp_mode interp
, unsigned location
)
3791 case INTERP_MODE_FLAT
:
3794 case INTERP_MODE_SMOOTH
:
3795 case INTERP_MODE_NONE
:
3796 if (location
== INTERP_CENTER
)
3797 return ctx
->persp_center
;
3798 else if (location
== INTERP_CENTROID
)
3799 return ctx
->persp_centroid
;
3800 else if (location
== INTERP_SAMPLE
)
3801 return ctx
->persp_sample
;
3803 case INTERP_MODE_NOPERSPECTIVE
:
3804 if (location
== INTERP_CENTER
)
3805 return ctx
->linear_center
;
3806 else if (location
== INTERP_CENTROID
)
3807 return ctx
->linear_centroid
;
3808 else if (location
== INTERP_SAMPLE
)
3809 return ctx
->linear_sample
;
3815 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3816 LLVMValueRef sample_id
)
3818 LLVMValueRef result
;
3819 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
3821 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3822 const_array(ctx
->v2f32
, 64), "");
3824 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3825 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
3830 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3832 LLVMValueRef values
[2];
3834 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3835 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3836 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3839 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3840 const nir_intrinsic_instr
*instr
)
3842 LLVMValueRef result
[4];
3843 LLVMValueRef interp_param
, attr_number
;
3846 LLVMValueRef src_c0
= NULL
;
3847 LLVMValueRef src_c1
= NULL
;
3848 LLVMValueRef src0
= NULL
;
3849 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3850 switch (instr
->intrinsic
) {
3851 case nir_intrinsic_interp_var_at_centroid
:
3852 location
= INTERP_CENTROID
;
3854 case nir_intrinsic_interp_var_at_sample
:
3855 case nir_intrinsic_interp_var_at_offset
:
3856 location
= INTERP_CENTER
;
3857 src0
= get_src(ctx
->nir
, instr
->src
[0]);
3863 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3864 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_0
, ""));
3865 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_1
, ""));
3866 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3867 LLVMValueRef sample_position
;
3868 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3870 /* fetch sample ID */
3871 sample_position
= load_sample_position(ctx
, src0
);
3873 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_0
, "");
3874 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3875 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_1
, "");
3876 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3878 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3879 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
3881 if (location
== INTERP_CENTER
) {
3882 LLVMValueRef ij_out
[2];
3883 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
3886 * take the I then J parameters, and the DDX/Y for it, and
3887 * calculate the IJ inputs for the interpolator.
3888 * temp1 = ddx * offset/sample.x + I;
3889 * interp_param.I = ddy * offset/sample.y + temp1;
3890 * temp1 = ddx * offset/sample.x + J;
3891 * interp_param.J = ddy * offset/sample.y + temp1;
3893 for (unsigned i
= 0; i
< 2; i
++) {
3894 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3895 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3896 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3897 ddxy_out
, ix_ll
, "");
3898 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3899 ddxy_out
, iy_ll
, "");
3900 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3901 interp_param
, ix_ll
, "");
3902 LLVMValueRef temp1
, temp2
;
3904 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3907 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3908 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3910 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3911 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3913 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3914 temp2
, ctx
->ac
.i32
, "");
3916 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3920 for (chan
= 0; chan
< 4; chan
++) {
3921 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3924 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3925 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3926 LLVMValueRef i
= LLVMBuildExtractElement(
3927 ctx
->builder
, interp_param
, ctx
->ac
.i32_0
, "");
3928 LLVMValueRef j
= LLVMBuildExtractElement(
3929 ctx
->builder
, interp_param
, ctx
->ac
.i32_1
, "");
3931 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3932 llvm_chan
, attr_number
,
3933 ctx
->prim_mask
, i
, j
);
3935 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3936 LLVMConstInt(ctx
->ac
.i32
, 2, false),
3937 llvm_chan
, attr_number
,
3941 return build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3942 instr
->variables
[0]->var
->data
.location_frac
);
3946 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3947 const nir_intrinsic_instr
*instr
)
3949 LLVMValueRef gs_next_vertex
;
3950 LLVMValueRef can_emit
;
3953 assert(instr
->const_index
[0] == 0);
3954 /* Write vertex attribute values to GSVS ring */
3955 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3956 ctx
->gs_next_vertex
,
3959 /* If this thread has already emitted the declared maximum number of
3960 * vertices, kill it: excessive vertex emissions are not supposed to
3961 * have any effect, and GS threads have no externally observable
3962 * effects other than emitting vertices.
3964 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3965 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
3966 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
3968 /* loop num outputs */
3970 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3971 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
3976 if (!(ctx
->output_mask
& (1ull << i
)))
3979 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3980 /* pack clip and cull into a single set of slots */
3981 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3985 for (unsigned j
= 0; j
< length
; j
++) {
3986 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3988 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3989 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3990 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3992 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
3994 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3996 voffset
, ctx
->gs2vs_offset
, 0,
4002 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4004 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4006 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4010 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
4011 const nir_intrinsic_instr
*instr
)
4013 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4017 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
4018 const nir_intrinsic_instr
*instr
)
4020 LLVMValueRef coord
[4] = {
4027 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4028 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->ac
.f32_1
,
4029 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4031 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
4032 return LLVMBuildBitCast(ctx
->builder
, result
,
4033 get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
4036 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4037 nir_intrinsic_instr
*instr
)
4039 LLVMValueRef result
= NULL
;
4041 switch (instr
->intrinsic
) {
4042 case nir_intrinsic_load_work_group_id
: {
4043 result
= ctx
->nctx
->workgroup_ids
;
4046 case nir_intrinsic_load_base_vertex
: {
4047 result
= ctx
->abi
->base_vertex
;
4050 case nir_intrinsic_load_vertex_id_zero_base
: {
4051 result
= ctx
->abi
->vertex_id
;
4054 case nir_intrinsic_load_local_invocation_id
: {
4055 result
= ctx
->nctx
->local_invocation_ids
;
4058 case nir_intrinsic_load_base_instance
:
4059 result
= ctx
->abi
->start_instance
;
4061 case nir_intrinsic_load_draw_id
:
4062 result
= ctx
->abi
->draw_id
;
4064 case nir_intrinsic_load_view_index
:
4065 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4067 case nir_intrinsic_load_invocation_id
:
4068 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4069 result
= unpack_param(&ctx
->ac
, ctx
->nctx
->tcs_rel_ids
, 8, 5);
4071 result
= ctx
->nctx
->gs_invocation_id
;
4073 case nir_intrinsic_load_primitive_id
:
4074 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4075 ctx
->nctx
->shader_info
->gs
.uses_prim_id
= true;
4076 result
= ctx
->nctx
->gs_prim_id
;
4077 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4078 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4079 result
= ctx
->nctx
->tcs_patch_id
;
4080 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4081 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4082 result
= ctx
->nctx
->tes_patch_id
;
4084 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4086 case nir_intrinsic_load_sample_id
:
4087 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4089 case nir_intrinsic_load_sample_pos
:
4090 result
= load_sample_pos(ctx
);
4092 case nir_intrinsic_load_sample_mask_in
:
4093 result
= ctx
->abi
->sample_coverage
;
4095 case nir_intrinsic_load_frag_coord
: {
4096 LLVMValueRef values
[4] = {
4097 ctx
->abi
->frag_pos
[0],
4098 ctx
->abi
->frag_pos
[1],
4099 ctx
->abi
->frag_pos
[2],
4100 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4102 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4105 case nir_intrinsic_load_front_face
:
4106 result
= ctx
->abi
->front_face
;
4108 case nir_intrinsic_load_instance_id
:
4109 result
= ctx
->abi
->instance_id
;
4111 case nir_intrinsic_load_num_work_groups
:
4112 result
= ctx
->nctx
->num_work_groups
;
4114 case nir_intrinsic_load_local_invocation_index
:
4115 result
= visit_load_local_invocation_index(ctx
->nctx
);
4117 case nir_intrinsic_load_push_constant
:
4118 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4120 case nir_intrinsic_vulkan_resource_index
:
4121 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4123 case nir_intrinsic_store_ssbo
:
4124 visit_store_ssbo(ctx
, instr
);
4126 case nir_intrinsic_load_ssbo
:
4127 result
= visit_load_buffer(ctx
, instr
);
4129 case nir_intrinsic_ssbo_atomic_add
:
4130 case nir_intrinsic_ssbo_atomic_imin
:
4131 case nir_intrinsic_ssbo_atomic_umin
:
4132 case nir_intrinsic_ssbo_atomic_imax
:
4133 case nir_intrinsic_ssbo_atomic_umax
:
4134 case nir_intrinsic_ssbo_atomic_and
:
4135 case nir_intrinsic_ssbo_atomic_or
:
4136 case nir_intrinsic_ssbo_atomic_xor
:
4137 case nir_intrinsic_ssbo_atomic_exchange
:
4138 case nir_intrinsic_ssbo_atomic_comp_swap
:
4139 result
= visit_atomic_ssbo(ctx
, instr
);
4141 case nir_intrinsic_load_ubo
:
4142 result
= visit_load_ubo_buffer(ctx
, instr
);
4144 case nir_intrinsic_get_buffer_size
:
4145 result
= visit_get_buffer_size(ctx
, instr
);
4147 case nir_intrinsic_load_var
:
4148 result
= visit_load_var(ctx
, instr
);
4150 case nir_intrinsic_store_var
:
4151 visit_store_var(ctx
, instr
);
4153 case nir_intrinsic_image_load
:
4154 result
= visit_image_load(ctx
, instr
);
4156 case nir_intrinsic_image_store
:
4157 visit_image_store(ctx
, instr
);
4159 case nir_intrinsic_image_atomic_add
:
4160 case nir_intrinsic_image_atomic_min
:
4161 case nir_intrinsic_image_atomic_max
:
4162 case nir_intrinsic_image_atomic_and
:
4163 case nir_intrinsic_image_atomic_or
:
4164 case nir_intrinsic_image_atomic_xor
:
4165 case nir_intrinsic_image_atomic_exchange
:
4166 case nir_intrinsic_image_atomic_comp_swap
:
4167 result
= visit_image_atomic(ctx
, instr
);
4169 case nir_intrinsic_image_size
:
4170 result
= visit_image_size(ctx
, instr
);
4172 case nir_intrinsic_discard
:
4173 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4174 LLVMVoidTypeInContext(ctx
->ac
.context
),
4175 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4177 case nir_intrinsic_discard_if
:
4178 emit_discard_if(ctx
, instr
);
4180 case nir_intrinsic_memory_barrier
:
4181 emit_waitcnt(ctx
->nctx
, VM_CNT
);
4183 case nir_intrinsic_barrier
:
4184 emit_barrier(ctx
->nctx
);
4186 case nir_intrinsic_var_atomic_add
:
4187 case nir_intrinsic_var_atomic_imin
:
4188 case nir_intrinsic_var_atomic_umin
:
4189 case nir_intrinsic_var_atomic_imax
:
4190 case nir_intrinsic_var_atomic_umax
:
4191 case nir_intrinsic_var_atomic_and
:
4192 case nir_intrinsic_var_atomic_or
:
4193 case nir_intrinsic_var_atomic_xor
:
4194 case nir_intrinsic_var_atomic_exchange
:
4195 case nir_intrinsic_var_atomic_comp_swap
:
4196 result
= visit_var_atomic(ctx
->nctx
, instr
);
4198 case nir_intrinsic_interp_var_at_centroid
:
4199 case nir_intrinsic_interp_var_at_sample
:
4200 case nir_intrinsic_interp_var_at_offset
:
4201 result
= visit_interp(ctx
->nctx
, instr
);
4203 case nir_intrinsic_emit_vertex
:
4204 visit_emit_vertex(ctx
->nctx
, instr
);
4206 case nir_intrinsic_end_primitive
:
4207 visit_end_primitive(ctx
->nctx
, instr
);
4209 case nir_intrinsic_load_tess_coord
:
4210 result
= visit_load_tess_coord(ctx
->nctx
, instr
);
4212 case nir_intrinsic_load_patch_vertices_in
:
4213 result
= LLVMConstInt(ctx
->ac
.i32
, ctx
->nctx
->options
->key
.tcs
.input_vertices
, false);
4216 fprintf(stderr
, "Unknown intrinsic: ");
4217 nir_print_instr(&instr
->instr
, stderr
);
4218 fprintf(stderr
, "\n");
4222 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4226 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4227 LLVMValueRef buffer
, bool write
)
4229 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4231 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4232 ctx
->shader_info
->fs
.writes_memory
= true;
4237 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4238 unsigned descriptor_set
,
4239 unsigned base_index
,
4240 unsigned constant_index
,
4242 enum ac_descriptor_type desc_type
,
4243 bool image
, bool write
)
4245 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4246 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4247 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4248 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4249 unsigned offset
= binding
->offset
;
4250 unsigned stride
= binding
->size
;
4252 LLVMBuilderRef builder
= ctx
->builder
;
4255 assert(base_index
< layout
->binding_count
);
4257 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4258 ctx
->shader_info
->fs
.writes_memory
= true;
4260 switch (desc_type
) {
4270 case AC_DESC_SAMPLER
:
4272 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4277 case AC_DESC_BUFFER
:
4282 unreachable("invalid desc_type\n");
4285 offset
+= constant_index
* stride
;
4287 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4288 (!index
|| binding
->immutable_samplers_equal
)) {
4289 if (binding
->immutable_samplers_equal
)
4292 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4294 LLVMValueRef constants
[] = {
4295 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4296 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4297 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4298 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4300 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4303 assert(stride
% type_size
== 0);
4306 index
= ctx
->ac
.i32_0
;
4308 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4310 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4311 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4313 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4316 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4317 const nir_deref_var
*deref
,
4318 enum ac_descriptor_type desc_type
,
4319 const nir_tex_instr
*tex_instr
,
4320 bool image
, bool write
)
4322 LLVMValueRef index
= NULL
;
4323 unsigned constant_index
= 0;
4324 unsigned descriptor_set
;
4325 unsigned base_index
;
4328 assert(tex_instr
&& !image
);
4330 base_index
= tex_instr
->sampler_index
;
4332 const nir_deref
*tail
= &deref
->deref
;
4333 while (tail
->child
) {
4334 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4335 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4340 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4342 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4343 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4345 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4346 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4351 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4354 constant_index
+= child
->base_offset
* array_size
;
4356 tail
= &child
->deref
;
4358 descriptor_set
= deref
->var
->data
.descriptor_set
;
4359 base_index
= deref
->var
->data
.binding
;
4362 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4365 constant_index
, index
,
4366 desc_type
, image
, write
);
4369 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4370 struct ac_image_args
*args
,
4371 const nir_tex_instr
*instr
,
4373 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4374 LLVMValueRef
*param
, unsigned count
,
4377 unsigned is_rect
= 0;
4378 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4380 if (op
== nir_texop_lod
)
4382 /* Pad to power of two vector */
4383 while (count
< util_next_power_of_two(count
))
4384 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4387 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4389 args
->addr
= param
[0];
4391 args
->resource
= res_ptr
;
4392 args
->sampler
= samp_ptr
;
4394 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4395 args
->addr
= param
[0];
4399 args
->dmask
= dmask
;
4400 args
->unorm
= is_rect
;
4404 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4407 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4408 * filtering manually. The driver sets img7 to a mask clearing
4409 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4410 * s_and_b32 samp0, samp0, img7
4413 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4415 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4416 LLVMValueRef res
, LLVMValueRef samp
)
4418 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4419 LLVMValueRef img7
, samp0
;
4421 if (ctx
->ac
.chip_class
>= VI
)
4424 img7
= LLVMBuildExtractElement(builder
, res
,
4425 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4426 samp0
= LLVMBuildExtractElement(builder
, samp
,
4427 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4428 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4429 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4430 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4433 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4434 nir_tex_instr
*instr
,
4435 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4436 LLVMValueRef
*fmask_ptr
)
4438 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4439 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4441 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4444 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4446 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4447 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4448 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4450 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4451 instr
->op
== nir_texop_samples_identical
))
4452 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4455 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4458 coord
= ac_to_float(ctx
, coord
);
4459 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4460 coord
= ac_to_integer(ctx
, coord
);
4464 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4466 LLVMValueRef result
= NULL
;
4467 struct ac_image_args args
= { 0 };
4468 unsigned dmask
= 0xf;
4469 LLVMValueRef address
[16];
4470 LLVMValueRef coords
[5];
4471 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4472 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4473 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4474 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4475 LLVMValueRef derivs
[6];
4476 unsigned chan
, count
= 0;
4477 unsigned const_src
= 0, num_deriv_comp
= 0;
4478 bool lod_is_zero
= false;
4480 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4482 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4483 switch (instr
->src
[i
].src_type
) {
4484 case nir_tex_src_coord
:
4485 coord
= get_src(ctx
, instr
->src
[i
].src
);
4487 case nir_tex_src_projector
:
4489 case nir_tex_src_comparator
:
4490 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4492 case nir_tex_src_offset
:
4493 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4496 case nir_tex_src_bias
:
4497 bias
= get_src(ctx
, instr
->src
[i
].src
);
4499 case nir_tex_src_lod
: {
4500 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4502 if (val
&& val
->i32
[0] == 0)
4504 lod
= get_src(ctx
, instr
->src
[i
].src
);
4507 case nir_tex_src_ms_index
:
4508 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4510 case nir_tex_src_ms_mcs
:
4512 case nir_tex_src_ddx
:
4513 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4514 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4516 case nir_tex_src_ddy
:
4517 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4519 case nir_tex_src_texture_offset
:
4520 case nir_tex_src_sampler_offset
:
4521 case nir_tex_src_plane
:
4527 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4528 result
= get_buffer_size(ctx
, res_ptr
, true);
4532 if (instr
->op
== nir_texop_texture_samples
) {
4533 LLVMValueRef res
, samples
, is_msaa
;
4534 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4535 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4536 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4537 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4538 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4539 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4540 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4541 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4542 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4544 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4545 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4546 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4547 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4548 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4550 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4557 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4558 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4560 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4561 LLVMValueRef offset
[3], pack
;
4562 for (chan
= 0; chan
< 3; ++chan
)
4563 offset
[chan
] = ctx
->ac
.i32_0
;
4566 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4567 offset
[chan
] = llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4568 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4569 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4571 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4572 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4574 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4575 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4576 address
[count
++] = pack
;
4579 /* pack LOD bias value */
4580 if (instr
->op
== nir_texop_txb
&& bias
) {
4581 address
[count
++] = bias
;
4584 /* Pack depth comparison value */
4585 if (instr
->is_shadow
&& comparator
) {
4586 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4587 llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4589 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
4590 * so the depth comparison value isn't clamped for Z16 and
4591 * Z24 anymore. Do it manually here.
4593 * It's unnecessary if the original texture format was
4594 * Z32_FLOAT, but we don't know that here.
4596 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
4597 z
= ac_build_clamp(&ctx
->ac
, z
);
4599 address
[count
++] = z
;
4602 /* pack derivatives */
4604 int num_src_deriv_channels
, num_dest_deriv_channels
;
4605 switch (instr
->sampler_dim
) {
4606 case GLSL_SAMPLER_DIM_3D
:
4607 case GLSL_SAMPLER_DIM_CUBE
:
4609 num_src_deriv_channels
= 3;
4610 num_dest_deriv_channels
= 3;
4612 case GLSL_SAMPLER_DIM_2D
:
4614 num_src_deriv_channels
= 2;
4615 num_dest_deriv_channels
= 2;
4618 case GLSL_SAMPLER_DIM_1D
:
4619 num_src_deriv_channels
= 1;
4620 if (ctx
->ac
.chip_class
>= GFX9
) {
4621 num_dest_deriv_channels
= 2;
4624 num_dest_deriv_channels
= 1;
4630 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4631 derivs
[i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4632 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4634 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4635 derivs
[i
] = ctx
->ac
.f32_0
;
4636 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4640 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4641 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4642 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4643 if (instr
->coord_components
== 3)
4644 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4645 ac_prepare_cube_coords(&ctx
->ac
,
4646 instr
->op
== nir_texop_txd
, instr
->is_array
,
4647 instr
->op
== nir_texop_lod
, coords
, derivs
);
4653 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4654 address
[count
++] = derivs
[i
];
4657 /* Pack texture coordinates */
4659 address
[count
++] = coords
[0];
4660 if (instr
->coord_components
> 1) {
4661 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4662 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4664 address
[count
++] = coords
[1];
4666 if (instr
->coord_components
> 2) {
4667 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4668 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4669 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4670 instr
->op
!= nir_texop_txf
) {
4671 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4673 address
[count
++] = coords
[2];
4676 if (ctx
->ac
.chip_class
>= GFX9
) {
4677 LLVMValueRef filler
;
4678 if (instr
->op
== nir_texop_txf
)
4679 filler
= ctx
->ac
.i32_0
;
4681 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4683 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4684 /* No nir_texop_lod, because it does not take a slice
4685 * even with array textures. */
4686 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
4687 address
[count
] = address
[count
- 1];
4688 address
[count
- 1] = filler
;
4691 address
[count
++] = filler
;
4697 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4698 instr
->op
== nir_texop_txf
)) {
4699 address
[count
++] = lod
;
4700 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4701 address
[count
++] = sample_index
;
4702 } else if(instr
->op
== nir_texop_txs
) {
4705 address
[count
++] = lod
;
4707 address
[count
++] = ctx
->ac
.i32_0
;
4710 for (chan
= 0; chan
< count
; chan
++) {
4711 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4712 address
[chan
], ctx
->ac
.i32
, "");
4715 if (instr
->op
== nir_texop_samples_identical
) {
4716 LLVMValueRef txf_address
[4];
4717 struct ac_image_args txf_args
= { 0 };
4718 unsigned txf_count
= count
;
4719 memcpy(txf_address
, address
, sizeof(txf_address
));
4721 if (!instr
->is_array
)
4722 txf_address
[2] = ctx
->ac
.i32_0
;
4723 txf_address
[3] = ctx
->ac
.i32_0
;
4725 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4727 txf_address
, txf_count
, 0xf);
4729 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4731 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4732 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4736 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4737 instr
->op
!= nir_texop_txs
) {
4738 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4739 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4742 instr
->is_array
? address
[2] : NULL
,
4743 address
[sample_chan
],
4747 if (offsets
&& instr
->op
== nir_texop_txf
) {
4748 nir_const_value
*const_offset
=
4749 nir_src_as_const_value(instr
->src
[const_src
].src
);
4750 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4751 assert(const_offset
);
4752 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4753 if (num_offsets
> 2)
4754 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4755 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4756 if (num_offsets
> 1)
4757 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4758 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4759 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4760 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4764 /* TODO TG4 support */
4765 if (instr
->op
== nir_texop_tg4
) {
4766 if (instr
->is_shadow
)
4769 dmask
= 1 << instr
->component
;
4771 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4772 res_ptr
, samp_ptr
, address
, count
, dmask
);
4774 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4776 if (instr
->op
== nir_texop_query_levels
)
4777 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4778 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4779 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4780 instr
->op
!= nir_texop_tg4
)
4781 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4782 else if (instr
->op
== nir_texop_txs
&&
4783 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4785 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4786 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4787 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4788 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4789 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4790 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4791 instr
->op
== nir_texop_txs
&&
4792 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4794 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4795 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4796 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4798 } else if (instr
->dest
.ssa
.num_components
!= 4)
4799 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4803 assert(instr
->dest
.is_ssa
);
4804 result
= ac_to_integer(&ctx
->ac
, result
);
4805 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4810 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4812 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4813 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4815 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4816 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4819 static void visit_post_phi(struct ac_nir_context
*ctx
,
4820 nir_phi_instr
*instr
,
4821 LLVMValueRef llvm_phi
)
4823 nir_foreach_phi_src(src
, instr
) {
4824 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4825 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4827 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4831 static void phi_post_pass(struct ac_nir_context
*ctx
)
4833 struct hash_entry
*entry
;
4834 hash_table_foreach(ctx
->phis
, entry
) {
4835 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4836 (LLVMValueRef
)entry
->data
);
4841 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4842 const nir_ssa_undef_instr
*instr
)
4844 unsigned num_components
= instr
->def
.num_components
;
4847 if (num_components
== 1)
4848 undef
= LLVMGetUndef(ctx
->ac
.i32
);
4850 undef
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, num_components
));
4852 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4855 static void visit_jump(struct ac_nir_context
*ctx
,
4856 const nir_jump_instr
*instr
)
4858 switch (instr
->type
) {
4859 case nir_jump_break
:
4860 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
4861 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4863 case nir_jump_continue
:
4864 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4865 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4868 fprintf(stderr
, "Unknown NIR jump instr: ");
4869 nir_print_instr(&instr
->instr
, stderr
);
4870 fprintf(stderr
, "\n");
4875 static void visit_cf_list(struct ac_nir_context
*ctx
,
4876 struct exec_list
*list
);
4878 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4880 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4881 nir_foreach_instr(instr
, block
)
4883 switch (instr
->type
) {
4884 case nir_instr_type_alu
:
4885 visit_alu(ctx
, nir_instr_as_alu(instr
));
4887 case nir_instr_type_load_const
:
4888 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4890 case nir_instr_type_intrinsic
:
4891 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4893 case nir_instr_type_tex
:
4894 visit_tex(ctx
, nir_instr_as_tex(instr
));
4896 case nir_instr_type_phi
:
4897 visit_phi(ctx
, nir_instr_as_phi(instr
));
4899 case nir_instr_type_ssa_undef
:
4900 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4902 case nir_instr_type_jump
:
4903 visit_jump(ctx
, nir_instr_as_jump(instr
));
4906 fprintf(stderr
, "Unknown NIR instr type: ");
4907 nir_print_instr(instr
, stderr
);
4908 fprintf(stderr
, "\n");
4913 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4916 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4918 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4920 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4921 LLVMBasicBlockRef merge_block
=
4922 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4923 LLVMBasicBlockRef if_block
=
4924 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4925 LLVMBasicBlockRef else_block
= merge_block
;
4926 if (!exec_list_is_empty(&if_stmt
->else_list
))
4927 else_block
= LLVMAppendBasicBlockInContext(
4928 ctx
->ac
.context
, fn
, "");
4930 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
4931 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4932 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
4934 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
4935 visit_cf_list(ctx
, &if_stmt
->then_list
);
4936 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4937 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4939 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4940 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
4941 visit_cf_list(ctx
, &if_stmt
->else_list
);
4942 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4943 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4946 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
4949 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4951 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4952 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4953 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4955 ctx
->continue_block
=
4956 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4958 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4960 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4961 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
4962 visit_cf_list(ctx
, &loop
->body
);
4964 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4965 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4966 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
4968 ctx
->continue_block
= continue_parent
;
4969 ctx
->break_block
= break_parent
;
4972 static void visit_cf_list(struct ac_nir_context
*ctx
,
4973 struct exec_list
*list
)
4975 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4977 switch (node
->type
) {
4978 case nir_cf_node_block
:
4979 visit_block(ctx
, nir_cf_node_as_block(node
));
4982 case nir_cf_node_if
:
4983 visit_if(ctx
, nir_cf_node_as_if(node
));
4986 case nir_cf_node_loop
:
4987 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4997 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4998 struct nir_variable
*variable
)
5000 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5001 LLVMValueRef t_offset
;
5002 LLVMValueRef t_list
;
5004 LLVMValueRef buffer_index
;
5005 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5006 int idx
= variable
->data
.location
;
5007 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5009 variable
->data
.driver_location
= idx
* 4;
5011 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
5012 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5013 ctx
->abi
.start_instance
, "");
5014 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
5015 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5017 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5018 ctx
->abi
.base_vertex
, "");
5020 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5021 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5023 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5025 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5027 LLVMConstInt(ctx
->ac
.i32
, 0, false),
5030 for (unsigned chan
= 0; chan
< 4; chan
++) {
5031 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5032 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5033 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5034 input
, llvm_chan
, ""));
5039 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5041 LLVMValueRef interp_param
,
5042 LLVMValueRef prim_mask
,
5043 LLVMValueRef result
[4])
5045 LLVMValueRef attr_number
;
5048 bool interp
= interp_param
!= NULL
;
5050 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5052 /* fs.constant returns the param from the middle vertex, so it's not
5053 * really useful for flat shading. It's meant to be used for custom
5054 * interpolation (but the intrinsic can't fetch from the other two
5057 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5058 * to do the right thing. The only reason we use fs.constant is that
5059 * fs.interp cannot be used on integers, because they can be equal
5063 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5064 LLVMVectorType(ctx
->f32
, 2), "");
5066 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5068 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5072 for (chan
= 0; chan
< 4; chan
++) {
5073 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5076 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5081 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5082 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5091 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5092 struct nir_variable
*variable
)
5094 int idx
= variable
->data
.location
;
5095 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5096 LLVMValueRef interp
;
5098 variable
->data
.driver_location
= idx
* 4;
5099 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5101 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5102 unsigned interp_type
;
5103 if (variable
->data
.sample
) {
5104 interp_type
= INTERP_SAMPLE
;
5105 ctx
->shader_info
->info
.ps
.force_persample
= true;
5106 } else if (variable
->data
.centroid
)
5107 interp_type
= INTERP_CENTROID
;
5109 interp_type
= INTERP_CENTER
;
5111 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5115 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5116 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5121 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5122 struct nir_shader
*nir
) {
5123 nir_foreach_variable(variable
, &nir
->inputs
)
5124 handle_vs_input_decl(ctx
, variable
);
5128 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5129 struct nir_shader
*nir
)
5131 if (!ctx
->options
->key
.fs
.multisample
)
5134 bool uses_center
= false;
5135 bool uses_centroid
= false;
5136 nir_foreach_variable(variable
, &nir
->inputs
) {
5137 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5138 variable
->data
.sample
)
5141 if (variable
->data
.centroid
)
5142 uses_centroid
= true;
5147 if (uses_center
&& uses_centroid
) {
5148 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5149 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5150 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5155 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5156 struct nir_shader
*nir
)
5158 prepare_interp_optimize(ctx
, nir
);
5160 nir_foreach_variable(variable
, &nir
->inputs
)
5161 handle_fs_input_decl(ctx
, variable
);
5165 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5166 ctx
->shader_info
->info
.needs_multiview_view_index
)
5167 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5169 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5170 LLVMValueRef interp_param
;
5171 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5173 if (!(ctx
->input_mask
& (1ull << i
)))
5176 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5177 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5178 interp_param
= *inputs
;
5179 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5183 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5185 } else if (i
== VARYING_SLOT_POS
) {
5186 for(int i
= 0; i
< 3; ++i
)
5187 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5189 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5190 ctx
->abi
.frag_pos
[3]);
5193 ctx
->shader_info
->fs
.num_interp
= index
;
5194 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5195 ctx
->shader_info
->fs
.has_pcoord
= true;
5196 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5197 ctx
->shader_info
->fs
.prim_id_input
= true;
5198 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5199 ctx
->shader_info
->fs
.layer_input
= true;
5200 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5202 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5203 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5207 ac_build_alloca(struct ac_llvm_context
*ac
,
5211 LLVMBuilderRef builder
= ac
->builder
;
5212 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5213 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5214 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5215 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5216 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5220 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5222 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5225 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5226 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5228 LLVMDisposeBuilder(first_builder
);
5233 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5237 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5238 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5243 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5244 struct nir_variable
*variable
,
5245 struct nir_shader
*shader
,
5246 gl_shader_stage stage
)
5248 int idx
= variable
->data
.location
+ variable
->data
.index
;
5249 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5250 uint64_t mask_attribs
;
5252 variable
->data
.driver_location
= idx
* 4;
5254 /* tess ctrl has it's own load/store paths for outputs */
5255 if (stage
== MESA_SHADER_TESS_CTRL
)
5258 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5259 if (stage
== MESA_SHADER_VERTEX
||
5260 stage
== MESA_SHADER_TESS_EVAL
||
5261 stage
== MESA_SHADER_GEOMETRY
) {
5262 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5263 int length
= shader
->info
.clip_distance_array_size
+
5264 shader
->info
.cull_distance_array_size
;
5265 if (stage
== MESA_SHADER_VERTEX
) {
5266 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5267 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5269 if (stage
== MESA_SHADER_TESS_EVAL
) {
5270 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5271 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5278 mask_attribs
= 1ull << idx
;
5282 ctx
->output_mask
|= mask_attribs
;
5286 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5287 struct nir_shader
*nir
,
5288 struct nir_variable
*variable
)
5290 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5291 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5293 /* tess ctrl has it's own load/store paths for outputs */
5294 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5297 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5298 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5299 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5300 int idx
= variable
->data
.location
+ variable
->data
.index
;
5301 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5302 int length
= nir
->info
.clip_distance_array_size
+
5303 nir
->info
.cull_distance_array_size
;
5312 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5313 for (unsigned chan
= 0; chan
< 4; chan
++) {
5314 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5315 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5321 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5322 enum glsl_base_type type
)
5326 case GLSL_TYPE_UINT
:
5327 case GLSL_TYPE_BOOL
:
5328 case GLSL_TYPE_SUBROUTINE
:
5330 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5332 case GLSL_TYPE_INT64
:
5333 case GLSL_TYPE_UINT64
:
5335 case GLSL_TYPE_DOUBLE
:
5338 unreachable("unknown GLSL type");
5343 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5344 const struct glsl_type
*type
)
5346 if (glsl_type_is_scalar(type
)) {
5347 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5350 if (glsl_type_is_vector(type
)) {
5351 return LLVMVectorType(
5352 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5353 glsl_get_vector_elements(type
));
5356 if (glsl_type_is_matrix(type
)) {
5357 return LLVMArrayType(
5358 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5359 glsl_get_matrix_columns(type
));
5362 if (glsl_type_is_array(type
)) {
5363 return LLVMArrayType(
5364 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5365 glsl_get_length(type
));
5368 assert(glsl_type_is_struct(type
));
5370 LLVMTypeRef member_types
[glsl_get_length(type
)];
5372 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5374 glsl_to_llvm_type(ctx
,
5375 glsl_get_struct_field(type
, i
));
5378 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5379 glsl_get_length(type
), false);
5383 setup_locals(struct ac_nir_context
*ctx
,
5384 struct nir_function
*func
)
5387 ctx
->num_locals
= 0;
5388 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5389 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5390 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5391 ctx
->num_locals
+= attrib_count
;
5393 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5397 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5398 for (j
= 0; j
< 4; j
++) {
5399 ctx
->locals
[i
* 4 + j
] =
5400 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5406 setup_shared(struct ac_nir_context
*ctx
,
5407 struct nir_shader
*nir
)
5409 nir_foreach_variable(variable
, &nir
->shared
) {
5410 LLVMValueRef shared
=
5411 LLVMAddGlobalInAddressSpace(
5412 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5413 variable
->name
? variable
->name
: "",
5415 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5420 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5422 v
= ac_to_float(ctx
, v
);
5423 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5424 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5428 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5429 LLVMValueRef src0
, LLVMValueRef src1
)
5431 LLVMValueRef const16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
5432 LLVMValueRef comp
[2];
5434 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5435 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5436 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5437 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5440 /* Initialize arguments for the shader export intrinsic */
5442 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5443 LLVMValueRef
*values
,
5445 struct ac_export_args
*args
)
5447 /* Default is 0xf. Adjusted below depending on the format. */
5448 args
->enabled_channels
= 0xf;
5450 /* Specify whether the EXEC mask represents the valid mask */
5451 args
->valid_mask
= 0;
5453 /* Specify whether this is the last export */
5456 /* Specify the target we are exporting */
5457 args
->target
= target
;
5459 args
->compr
= false;
5460 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
5461 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
5462 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
5463 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
5468 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5469 LLVMValueRef val
[4];
5470 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5471 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5472 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5473 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5475 switch(col_format
) {
5476 case V_028714_SPI_SHADER_ZERO
:
5477 args
->enabled_channels
= 0; /* writemask */
5478 args
->target
= V_008DFC_SQ_EXP_NULL
;
5481 case V_028714_SPI_SHADER_32_R
:
5482 args
->enabled_channels
= 1;
5483 args
->out
[0] = values
[0];
5486 case V_028714_SPI_SHADER_32_GR
:
5487 args
->enabled_channels
= 0x3;
5488 args
->out
[0] = values
[0];
5489 args
->out
[1] = values
[1];
5492 case V_028714_SPI_SHADER_32_AR
:
5493 args
->enabled_channels
= 0x9;
5494 args
->out
[0] = values
[0];
5495 args
->out
[3] = values
[3];
5498 case V_028714_SPI_SHADER_FP16_ABGR
:
5501 for (unsigned chan
= 0; chan
< 2; chan
++) {
5502 LLVMValueRef pack_args
[2] = {
5504 values
[2 * chan
+ 1]
5506 LLVMValueRef packed
;
5508 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5509 args
->out
[chan
] = packed
;
5513 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5514 for (unsigned chan
= 0; chan
< 4; chan
++) {
5515 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5516 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5517 LLVMConstReal(ctx
->f32
, 65535), "");
5518 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5519 LLVMConstReal(ctx
->f32
, 0.5), "");
5520 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5525 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5526 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5529 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5530 for (unsigned chan
= 0; chan
< 4; chan
++) {
5531 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5532 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5533 LLVMConstReal(ctx
->f32
, 32767), "");
5535 /* If positive, add 0.5, else add -0.5. */
5536 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5537 LLVMBuildSelect(ctx
->builder
,
5538 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5539 val
[chan
], ctx
->ac
.f32_0
, ""),
5540 LLVMConstReal(ctx
->f32
, 0.5),
5541 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
5542 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->ac
.i32
, "");
5546 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5547 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5550 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5551 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5552 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5553 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->ac
.i32
, 3, 0);
5555 for (unsigned chan
= 0; chan
< 4; chan
++) {
5556 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5557 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5561 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5562 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5566 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5567 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5568 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5569 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5570 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5571 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->ac
.i32_1
;
5572 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->ac
.i32
, -2, 0);
5575 for (unsigned chan
= 0; chan
< 4; chan
++) {
5576 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5577 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5578 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5582 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5583 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5588 case V_028714_SPI_SHADER_32_ABGR
:
5589 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5593 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5595 for (unsigned i
= 0; i
< 4; ++i
)
5596 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5600 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5601 bool export_prim_id
,
5602 struct ac_vs_output_info
*outinfo
)
5604 uint32_t param_count
= 0;
5606 unsigned pos_idx
, num_pos_exports
= 0;
5607 struct ac_export_args args
, pos_args
[4] = {};
5608 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5611 if (ctx
->options
->key
.has_multiview_view_index
) {
5612 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5614 for(unsigned i
= 0; i
< 4; ++i
)
5615 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5616 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5619 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5620 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5623 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5624 sizeof(outinfo
->vs_output_param_offset
));
5626 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5627 LLVMValueRef slots
[8];
5630 if (outinfo
->cull_dist_mask
)
5631 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5633 i
= VARYING_SLOT_CLIP_DIST0
;
5634 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5635 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5636 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5638 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5639 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5641 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5642 target
= V_008DFC_SQ_EXP_POS
+ 3;
5643 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5644 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5645 &args
, sizeof(args
));
5648 target
= V_008DFC_SQ_EXP_POS
+ 2;
5649 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5650 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5651 &args
, sizeof(args
));
5655 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
5656 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5657 for (unsigned j
= 0; j
< 4; j
++)
5658 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5659 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5661 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5663 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5664 outinfo
->writes_pointsize
= true;
5665 psize_value
= LLVMBuildLoad(ctx
->builder
,
5666 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5669 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5670 outinfo
->writes_layer
= true;
5671 layer_value
= LLVMBuildLoad(ctx
->builder
,
5672 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5675 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5676 outinfo
->writes_viewport_index
= true;
5677 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5678 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5681 if (outinfo
->writes_pointsize
||
5682 outinfo
->writes_layer
||
5683 outinfo
->writes_viewport_index
) {
5684 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5685 (outinfo
->writes_layer
== true ? 4 : 0));
5686 pos_args
[1].valid_mask
= 0;
5687 pos_args
[1].done
= 0;
5688 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5689 pos_args
[1].compr
= 0;
5690 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
5691 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
5692 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
5693 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
5695 if (outinfo
->writes_pointsize
== true)
5696 pos_args
[1].out
[0] = psize_value
;
5697 if (outinfo
->writes_layer
== true)
5698 pos_args
[1].out
[2] = layer_value
;
5699 if (outinfo
->writes_viewport_index
== true) {
5700 if (ctx
->options
->chip_class
>= GFX9
) {
5701 /* GFX9 has the layer in out.z[10:0] and the viewport
5702 * index in out.z[19:16].
5704 LLVMValueRef v
= viewport_index_value
;
5705 v
= ac_to_integer(&ctx
->ac
, v
);
5706 v
= LLVMBuildShl(ctx
->builder
, v
,
5707 LLVMConstInt(ctx
->ac
.i32
, 16, false),
5709 v
= LLVMBuildOr(ctx
->builder
, v
,
5710 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5712 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5713 pos_args
[1].enabled_channels
|= 1 << 2;
5715 pos_args
[1].out
[3] = viewport_index_value
;
5716 pos_args
[1].enabled_channels
|= 1 << 3;
5720 for (i
= 0; i
< 4; i
++) {
5721 if (pos_args
[i
].out
[0])
5726 for (i
= 0; i
< 4; i
++) {
5727 if (!pos_args
[i
].out
[0])
5730 /* Specify the target we are exporting */
5731 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5732 if (pos_idx
== num_pos_exports
)
5733 pos_args
[i
].done
= 1;
5734 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5737 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5738 LLVMValueRef values
[4];
5739 if (!(ctx
->output_mask
& (1ull << i
)))
5742 for (unsigned j
= 0; j
< 4; j
++)
5743 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5744 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5746 if (i
== VARYING_SLOT_LAYER
) {
5747 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5748 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5750 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5751 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5752 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5754 } else if (i
>= VARYING_SLOT_VAR0
) {
5755 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5756 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5757 outinfo
->vs_output_param_offset
[i
] = param_count
;
5762 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5764 if (target
>= V_008DFC_SQ_EXP_POS
&&
5765 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5766 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5767 &args
, sizeof(args
));
5769 ac_build_export(&ctx
->ac
, &args
);
5773 if (export_prim_id
) {
5774 LLVMValueRef values
[4];
5775 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5776 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5779 values
[0] = ctx
->vs_prim_id
;
5780 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5781 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5782 for (unsigned j
= 1; j
< 4; j
++)
5783 values
[j
] = ctx
->ac
.f32_0
;
5784 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5785 ac_build_export(&ctx
->ac
, &args
);
5786 outinfo
->export_prim_id
= true;
5789 outinfo
->pos_exports
= num_pos_exports
;
5790 outinfo
->param_exports
= param_count
;
5794 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5795 struct ac_es_output_info
*outinfo
)
5798 uint64_t max_output_written
= 0;
5799 LLVMValueRef lds_base
= NULL
;
5801 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5805 if (!(ctx
->output_mask
& (1ull << i
)))
5808 if (i
== VARYING_SLOT_CLIP_DIST0
)
5809 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5811 param_index
= shader_io_get_unique_index(i
);
5813 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5816 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5818 if (ctx
->ac
.chip_class
>= GFX9
) {
5819 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
5820 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
5821 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
5822 LLVMConstInt(ctx
->ac
.i32
, 24, false),
5823 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
5824 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
5825 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
5826 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
5827 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
5828 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
5831 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5832 LLVMValueRef dw_addr
;
5833 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5837 if (!(ctx
->output_mask
& (1ull << i
)))
5840 if (i
== VARYING_SLOT_CLIP_DIST0
)
5841 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5843 param_index
= shader_io_get_unique_index(i
);
5846 dw_addr
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5847 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
5850 for (j
= 0; j
< length
; j
++) {
5851 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5852 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
5854 if (ctx
->ac
.chip_class
>= GFX9
) {
5855 ac_lds_store(&ctx
->ac
, dw_addr
,
5856 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5857 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
5859 ac_build_buffer_store_dword(&ctx
->ac
,
5862 NULL
, ctx
->es2gs_offset
,
5863 (4 * param_index
+ j
) * 4,
5871 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5873 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5874 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
5875 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5876 vertex_dw_stride
, "");
5878 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5879 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5882 if (!(ctx
->output_mask
& (1ull << i
)))
5885 if (i
== VARYING_SLOT_CLIP_DIST0
)
5886 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5887 int param
= shader_io_get_unique_index(i
);
5888 mark_tess_output(ctx
, false, param
);
5890 mark_tess_output(ctx
, false, param
+ 1);
5891 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5892 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
5894 for (unsigned j
= 0; j
< length
; j
++) {
5895 ac_lds_store(&ctx
->ac
, dw_addr
,
5896 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5897 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
5902 struct ac_build_if_state
5904 struct nir_to_llvm_context
*ctx
;
5905 LLVMValueRef condition
;
5906 LLVMBasicBlockRef entry_block
;
5907 LLVMBasicBlockRef true_block
;
5908 LLVMBasicBlockRef false_block
;
5909 LLVMBasicBlockRef merge_block
;
5912 static LLVMBasicBlockRef
5913 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5915 LLVMBasicBlockRef current_block
;
5916 LLVMBasicBlockRef next_block
;
5917 LLVMBasicBlockRef new_block
;
5919 /* get current basic block */
5920 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5922 /* chqeck if there's another block after this one */
5923 next_block
= LLVMGetNextBasicBlock(current_block
);
5925 /* insert the new block before the next block */
5926 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5929 /* append new block after current block */
5930 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5931 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5937 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5938 struct nir_to_llvm_context
*ctx
,
5939 LLVMValueRef condition
)
5941 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5943 memset(ifthen
, 0, sizeof *ifthen
);
5945 ifthen
->condition
= condition
;
5946 ifthen
->entry_block
= block
;
5948 /* create endif/merge basic block for the phi functions */
5949 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5951 /* create/insert true_block before merge_block */
5952 ifthen
->true_block
=
5953 LLVMInsertBasicBlockInContext(ctx
->context
,
5954 ifthen
->merge_block
,
5957 /* successive code goes into the true block */
5958 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5962 * End a conditional.
5965 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5967 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5969 /* Insert branch to the merge block from current block */
5970 LLVMBuildBr(builder
, ifthen
->merge_block
);
5973 * Now patch in the various branch instructions.
5976 /* Insert the conditional branch instruction at the end of entry_block */
5977 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5978 if (ifthen
->false_block
) {
5979 /* we have an else clause */
5980 LLVMBuildCondBr(builder
, ifthen
->condition
,
5981 ifthen
->true_block
, ifthen
->false_block
);
5984 /* no else clause */
5985 LLVMBuildCondBr(builder
, ifthen
->condition
,
5986 ifthen
->true_block
, ifthen
->merge_block
);
5989 /* Resume building code at end of the ifthen->merge_block */
5990 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5994 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5996 unsigned stride
, outer_comps
, inner_comps
;
5997 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5998 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 8, 5);
5999 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
6000 unsigned tess_inner_index
, tess_outer_index
;
6001 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6002 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6006 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6026 ac_nir_build_if(&if_ctx
, ctx
,
6027 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6028 invocation_id
, ctx
->ac
.i32_0
, ""));
6030 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6031 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6033 mark_tess_output(ctx
, true, tess_inner_index
);
6034 mark_tess_output(ctx
, true, tess_outer_index
);
6035 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6036 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6037 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6038 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6039 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6041 for (i
= 0; i
< 4; i
++) {
6042 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6043 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6047 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6048 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6049 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6050 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
6051 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6053 for (i
= 0; i
< outer_comps
; i
++) {
6055 ac_lds_load(&ctx
->ac
, lds_outer
);
6056 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6057 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
6059 for (i
= 0; i
< inner_comps
; i
++) {
6060 inner
[i
] = out
[outer_comps
+i
] =
6061 ac_lds_load(&ctx
->ac
, lds_inner
);
6062 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6063 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
6067 /* Convert the outputs to vectors for stores. */
6068 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6072 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6075 buffer
= ctx
->hs_ring_tess_factor
;
6076 tf_base
= ctx
->tess_factor_offset
;
6077 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6078 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6079 unsigned tf_offset
= 0;
6081 if (ctx
->options
->chip_class
<= VI
) {
6082 ac_nir_build_if(&inner_if_ctx
, ctx
,
6083 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6084 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6086 /* Store the dynamic HS control word. */
6087 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6088 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6089 1, ctx
->ac
.i32_0
, tf_base
,
6090 0, 1, 0, true, false);
6093 ac_nir_build_endif(&inner_if_ctx
);
6096 /* Store the tessellation factors. */
6097 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6098 MIN2(stride
, 4), byteoffset
, tf_base
,
6099 tf_offset
, 1, 0, true, false);
6101 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6102 stride
- 4, byteoffset
, tf_base
,
6103 16 + tf_offset
, 1, 0, true, false);
6105 //store to offchip for TES to read - only if TES reads them
6106 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6107 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6108 LLVMValueRef tf_inner_offset
;
6109 unsigned param_outer
, param_inner
;
6111 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6112 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6113 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6115 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6116 util_next_power_of_two(outer_comps
));
6118 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6119 outer_comps
, tf_outer_offset
,
6120 ctx
->oc_lds
, 0, 1, 0, true, false);
6122 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6123 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6124 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6126 inner_vec
= inner_comps
== 1 ? inner
[0] :
6127 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6128 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6129 inner_comps
, tf_inner_offset
,
6130 ctx
->oc_lds
, 0, 1, 0, true, false);
6133 ac_nir_build_endif(&if_ctx
);
6137 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6139 write_tess_factors(ctx
);
6143 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6144 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6145 struct ac_export_args
*args
)
6148 si_llvm_init_export_args(ctx
, color
, param
,
6152 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6153 args
->done
= 1; /* DONE bit */
6154 } else if (!args
->enabled_channels
)
6155 return false; /* unnecessary NULL export */
6161 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6162 LLVMValueRef depth
, LLVMValueRef stencil
,
6163 LLVMValueRef samplemask
)
6165 struct ac_export_args args
;
6167 args
.enabled_channels
= 0;
6168 args
.valid_mask
= 1;
6170 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
6173 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
6174 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
6175 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
6176 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
6179 args
.out
[0] = depth
;
6180 args
.enabled_channels
|= 0x1;
6184 args
.out
[1] = stencil
;
6185 args
.enabled_channels
|= 0x2;
6189 args
.out
[2] = samplemask
;
6190 args
.enabled_channels
|= 0x4;
6193 /* SI (except OLAND and HAINAN) has a bug that it only looks
6194 * at the X writemask component. */
6195 if (ctx
->options
->chip_class
== SI
&&
6196 ctx
->options
->family
!= CHIP_OLAND
&&
6197 ctx
->options
->family
!= CHIP_HAINAN
)
6198 args
.enabled_channels
|= 0x1;
6200 ac_build_export(&ctx
->ac
, &args
);
6204 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6207 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6208 struct ac_export_args color_args
[8];
6210 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6211 LLVMValueRef values
[4];
6213 if (!(ctx
->output_mask
& (1ull << i
)))
6216 if (i
== FRAG_RESULT_DEPTH
) {
6217 ctx
->shader_info
->fs
.writes_z
= true;
6218 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6219 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6220 } else if (i
== FRAG_RESULT_STENCIL
) {
6221 ctx
->shader_info
->fs
.writes_stencil
= true;
6222 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6223 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6224 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6225 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6226 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6227 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6230 for (unsigned j
= 0; j
< 4; j
++)
6231 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6232 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6234 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6235 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6237 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6243 for (unsigned i
= 0; i
< index
; i
++)
6244 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6245 if (depth
|| stencil
|| samplemask
)
6246 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6248 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6249 ac_build_export(&ctx
->ac
, &color_args
[0]);
6252 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
6256 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6258 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6262 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6263 LLVMValueRef
*addrs
)
6265 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6267 switch (ctx
->stage
) {
6268 case MESA_SHADER_VERTEX
:
6269 if (ctx
->options
->key
.vs
.as_ls
)
6270 handle_ls_outputs_post(ctx
);
6271 else if (ctx
->options
->key
.vs
.as_es
)
6272 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6274 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6275 &ctx
->shader_info
->vs
.outinfo
);
6277 case MESA_SHADER_FRAGMENT
:
6278 handle_fs_outputs_post(ctx
);
6280 case MESA_SHADER_GEOMETRY
:
6281 emit_gs_epilogue(ctx
);
6283 case MESA_SHADER_TESS_CTRL
:
6284 handle_tcs_outputs_post(ctx
);
6286 case MESA_SHADER_TESS_EVAL
:
6287 if (ctx
->options
->key
.tes
.as_es
)
6288 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6290 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6291 &ctx
->shader_info
->tes
.outinfo
);
6298 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6300 LLVMPassManagerRef passmgr
;
6301 /* Create the pass manager */
6302 passmgr
= LLVMCreateFunctionPassManagerForModule(
6305 /* This pass should eliminate all the load and store instructions */
6306 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6308 /* Add some optimization passes */
6309 LLVMAddScalarReplAggregatesPass(passmgr
);
6310 LLVMAddLICMPass(passmgr
);
6311 LLVMAddAggressiveDCEPass(passmgr
);
6312 LLVMAddCFGSimplificationPass(passmgr
);
6313 LLVMAddInstructionCombiningPass(passmgr
);
6316 LLVMInitializeFunctionPassManager(passmgr
);
6317 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6318 LLVMFinalizeFunctionPassManager(passmgr
);
6320 LLVMDisposeBuilder(ctx
->builder
);
6321 LLVMDisposePassManager(passmgr
);
6325 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6327 struct ac_vs_output_info
*outinfo
;
6329 switch (ctx
->stage
) {
6330 case MESA_SHADER_FRAGMENT
:
6331 case MESA_SHADER_COMPUTE
:
6332 case MESA_SHADER_TESS_CTRL
:
6333 case MESA_SHADER_GEOMETRY
:
6335 case MESA_SHADER_VERTEX
:
6336 if (ctx
->options
->key
.vs
.as_ls
||
6337 ctx
->options
->key
.vs
.as_es
)
6339 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6341 case MESA_SHADER_TESS_EVAL
:
6342 if (ctx
->options
->key
.vs
.as_es
)
6344 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6347 unreachable("Unhandled shader type");
6350 ac_optimize_vs_outputs(&ctx
->ac
,
6352 outinfo
->vs_output_param_offset
,
6354 &outinfo
->param_exports
);
6358 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6360 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6361 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6362 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6365 if (ctx
->is_gs_copy_shader
) {
6366 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6368 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6370 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6371 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6373 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
6375 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6376 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6377 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6378 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6381 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6382 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6383 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6384 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6389 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6390 const struct nir_shader
*nir
)
6392 switch (nir
->info
.stage
) {
6393 case MESA_SHADER_TESS_CTRL
:
6394 return chip_class
>= CIK
? 128 : 64;
6395 case MESA_SHADER_GEOMETRY
:
6396 return chip_class
>= GFX9
? 128 : 64;
6397 case MESA_SHADER_COMPUTE
:
6403 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6404 nir
->info
.cs
.local_size
[1] *
6405 nir
->info
.cs
.local_size
[2];
6406 return max_workgroup_size
;
6409 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6410 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6412 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6413 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6414 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6415 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6416 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
6417 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6418 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6419 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_rel_ids
, ctx
->rel_auto_id
, "");
6420 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6423 static void prepare_gs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6425 for(int i
= 5; i
>= 0; --i
) {
6426 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6427 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6428 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6431 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6432 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6433 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6436 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6437 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6439 struct ac_nir_context ctx
= {};
6440 struct nir_function
*func
;
6449 ctx
.stage
= nir
->info
.stage
;
6451 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6453 nir_foreach_variable(variable
, &nir
->outputs
)
6454 handle_shader_output_decl(&ctx
, nir
, variable
);
6456 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6457 _mesa_key_pointer_equal
);
6458 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6459 _mesa_key_pointer_equal
);
6460 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6461 _mesa_key_pointer_equal
);
6463 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6465 setup_locals(&ctx
, func
);
6467 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6468 setup_shared(&ctx
, nir
);
6470 visit_cf_list(&ctx
, &func
->impl
->body
);
6471 phi_post_pass(&ctx
);
6473 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6477 ralloc_free(ctx
.defs
);
6478 ralloc_free(ctx
.phis
);
6479 ralloc_free(ctx
.vars
);
6486 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6487 struct nir_shader
*const *shaders
,
6489 struct ac_shader_variant_info
*shader_info
,
6490 const struct ac_nir_compiler_options
*options
)
6492 struct nir_to_llvm_context ctx
= {0};
6494 ctx
.options
= options
;
6495 ctx
.shader_info
= shader_info
;
6496 ctx
.context
= LLVMContextCreate();
6497 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6499 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
);
6500 ctx
.ac
.module
= ctx
.module
;
6501 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6503 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6504 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6505 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6506 LLVMDisposeTargetData(data_layout
);
6507 LLVMDisposeMessage(data_layout_str
);
6510 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6511 ctx
.ac
.builder
= ctx
.builder
;
6513 memset(shader_info
, 0, sizeof(*shader_info
));
6515 for(int i
= 0; i
< shader_count
; ++i
)
6516 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6518 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6519 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6520 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6521 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6523 ctx
.max_workgroup_size
= 0;
6524 for (int i
= 0; i
< shader_count
; ++i
) {
6525 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6526 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6530 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6531 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6533 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6534 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6535 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6536 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6537 ctx
.abi
.clamp_shadow_reference
= false;
6539 if (shader_count
>= 2)
6540 ac_init_exec_full_mask(&ctx
.ac
);
6542 if (ctx
.ac
.chip_class
== GFX9
&&
6543 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6544 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6546 for(int i
= 0; i
< shader_count
; ++i
) {
6547 ctx
.stage
= shaders
[i
]->info
.stage
;
6548 ctx
.output_mask
= 0;
6549 ctx
.tess_outputs_written
= 0;
6550 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6551 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6553 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6554 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6556 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6557 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6558 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6559 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6560 if (shader_info
->info
.vs
.needs_instance_id
) {
6561 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6562 MAX2(3, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6564 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6565 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6571 ac_setup_rings(&ctx
);
6573 LLVMBasicBlockRef merge_block
;
6574 if (shader_count
>= 2) {
6575 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6576 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6577 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6579 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6580 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6581 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6582 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6583 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6584 thread_id
, count
, "");
6585 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6587 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6590 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6591 handle_fs_inputs(&ctx
, shaders
[i
]);
6592 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6593 handle_vs_inputs(&ctx
, shaders
[i
]);
6594 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6595 prepare_gs_input_vgprs(&ctx
);
6597 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6598 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6600 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6602 if (shader_count
>= 2) {
6603 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6604 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6607 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6608 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6609 shaders
[i
]->info
.cull_distance_array_size
> 4;
6610 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6611 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6612 shaders
[i
]->info
.gs
.vertices_out
;
6613 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6614 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6615 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6616 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6617 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6621 LLVMBuildRetVoid(ctx
.builder
);
6623 ac_llvm_finalize_module(&ctx
);
6625 if (shader_count
== 1)
6626 ac_nir_eliminate_const_vs_outputs(&ctx
);
6631 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6633 unsigned *retval
= (unsigned *)context
;
6634 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6635 char *description
= LLVMGetDiagInfoDescription(di
);
6637 if (severity
== LLVMDSError
) {
6639 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6643 LLVMDisposeMessage(description
);
6646 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6647 struct ac_shader_binary
*binary
,
6648 LLVMTargetMachineRef tm
)
6650 unsigned retval
= 0;
6652 LLVMContextRef llvm_ctx
;
6653 LLVMMemoryBufferRef out_buffer
;
6654 unsigned buffer_size
;
6655 const char *buffer_data
;
6658 /* Setup Diagnostic Handler*/
6659 llvm_ctx
= LLVMGetModuleContext(M
);
6661 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6665 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6668 /* Process Errors/Warnings */
6670 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6676 /* Extract Shader Code*/
6677 buffer_size
= LLVMGetBufferSize(out_buffer
);
6678 buffer_data
= LLVMGetBufferStart(out_buffer
);
6680 ac_elf_read(buffer_data
, buffer_size
, binary
);
6683 LLVMDisposeMemoryBuffer(out_buffer
);
6689 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6690 LLVMModuleRef llvm_module
,
6691 struct ac_shader_binary
*binary
,
6692 struct ac_shader_config
*config
,
6693 struct ac_shader_variant_info
*shader_info
,
6694 gl_shader_stage stage
,
6695 bool dump_shader
, bool supports_spill
)
6698 ac_dump_module(llvm_module
);
6700 memset(binary
, 0, sizeof(*binary
));
6701 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6703 fprintf(stderr
, "compile failed\n");
6707 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6709 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6711 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6712 LLVMDisposeModule(llvm_module
);
6713 LLVMContextDispose(ctx
);
6715 if (stage
== MESA_SHADER_FRAGMENT
) {
6716 shader_info
->num_input_vgprs
= 0;
6717 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6718 shader_info
->num_input_vgprs
+= 2;
6719 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6720 shader_info
->num_input_vgprs
+= 2;
6721 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6722 shader_info
->num_input_vgprs
+= 2;
6723 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6724 shader_info
->num_input_vgprs
+= 3;
6725 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6726 shader_info
->num_input_vgprs
+= 2;
6727 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6728 shader_info
->num_input_vgprs
+= 2;
6729 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6730 shader_info
->num_input_vgprs
+= 2;
6731 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6732 shader_info
->num_input_vgprs
+= 1;
6733 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6734 shader_info
->num_input_vgprs
+= 1;
6735 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6736 shader_info
->num_input_vgprs
+= 1;
6737 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6738 shader_info
->num_input_vgprs
+= 1;
6739 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6740 shader_info
->num_input_vgprs
+= 1;
6741 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6742 shader_info
->num_input_vgprs
+= 1;
6743 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6744 shader_info
->num_input_vgprs
+= 1;
6745 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6746 shader_info
->num_input_vgprs
+= 1;
6747 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6748 shader_info
->num_input_vgprs
+= 1;
6750 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6752 /* +3 for scratch wave offset and VCC */
6753 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6754 shader_info
->num_input_sgprs
+ 3);
6758 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
6760 switch (nir
->info
.stage
) {
6761 case MESA_SHADER_COMPUTE
:
6762 for (int i
= 0; i
< 3; ++i
)
6763 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6765 case MESA_SHADER_FRAGMENT
:
6766 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6768 case MESA_SHADER_GEOMETRY
:
6769 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6770 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6771 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6772 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6774 case MESA_SHADER_TESS_EVAL
:
6775 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6776 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6777 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6778 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6779 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6781 case MESA_SHADER_TESS_CTRL
:
6782 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6784 case MESA_SHADER_VERTEX
:
6785 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6786 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6787 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6788 if (options
->key
.vs
.as_ls
)
6789 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6796 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6797 struct ac_shader_binary
*binary
,
6798 struct ac_shader_config
*config
,
6799 struct ac_shader_variant_info
*shader_info
,
6800 struct nir_shader
*const *nir
,
6802 const struct ac_nir_compiler_options
*options
,
6806 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
6809 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
6810 for (int i
= 0; i
< nir_count
; ++i
)
6811 ac_fill_shader_info(shader_info
, nir
[i
], options
);
6815 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6817 LLVMValueRef args
[9];
6818 args
[0] = ctx
->gsvs_ring
;
6819 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
6820 args
[3] = ctx
->ac
.i32_0
;
6821 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
6822 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
6823 args
[6] = ctx
->ac
.i32_1
; /* GLC */
6824 args
[7] = ctx
->ac
.i32_1
; /* SLC */
6825 args
[8] = ctx
->ac
.i32_0
; /* TFE */
6829 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6833 if (!(ctx
->output_mask
& (1ull << i
)))
6836 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6837 /* unpack clip and cull from a single set of slots */
6838 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6843 for (unsigned j
= 0; j
< length
; j
++) {
6845 args
[2] = LLVMConstInt(ctx
->ac
.i32
,
6847 ctx
->gs_max_out_vertices
* 16 * 4, false);
6849 value
= ac_build_intrinsic(&ctx
->ac
,
6850 "llvm.SI.buffer.load.dword.i32.i32",
6851 ctx
->ac
.i32
, args
, 9,
6852 AC_FUNC_ATTR_READONLY
|
6853 AC_FUNC_ATTR_LEGACY
);
6855 LLVMBuildStore(ctx
->builder
,
6856 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6860 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
6863 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6864 struct nir_shader
*geom_shader
,
6865 struct ac_shader_binary
*binary
,
6866 struct ac_shader_config
*config
,
6867 struct ac_shader_variant_info
*shader_info
,
6868 const struct ac_nir_compiler_options
*options
,
6871 struct nir_to_llvm_context ctx
= {0};
6872 ctx
.context
= LLVMContextCreate();
6873 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6874 ctx
.options
= options
;
6875 ctx
.shader_info
= shader_info
;
6877 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
);
6878 ctx
.ac
.module
= ctx
.module
;
6880 ctx
.is_gs_copy_shader
= true;
6881 LLVMSetTarget(ctx
.module
, "amdgcn--");
6884 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6885 ctx
.ac
.builder
= ctx
.builder
;
6886 ctx
.stage
= MESA_SHADER_VERTEX
;
6888 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
6890 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6891 ac_setup_rings(&ctx
);
6893 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6894 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6896 struct ac_nir_context nir_ctx
= {};
6897 nir_ctx
.ac
= ctx
.ac
;
6898 nir_ctx
.abi
= &ctx
.abi
;
6900 nir_ctx
.nctx
= &ctx
;
6903 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
6904 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
6905 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
6908 ac_gs_copy_shader_emit(&ctx
);
6912 LLVMBuildRetVoid(ctx
.builder
);
6914 ac_llvm_finalize_module(&ctx
);
6916 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6918 dump_shader
, options
->supports_spill
);