2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "../vulkan/radv_descriptor_set.h"
31 #include "util/bitscan.h"
32 #include <llvm-c/Transforms/Scalar.h>
33 #include "ac_shader_abi.h"
34 #include "ac_shader_info.h"
35 #include "ac_shader_util.h"
36 #include "ac_exp_param.h"
38 enum radeon_llvm_calling_convention
{
39 RADEON_LLVM_AMDGPU_VS
= 87,
40 RADEON_LLVM_AMDGPU_GS
= 88,
41 RADEON_LLVM_AMDGPU_PS
= 89,
42 RADEON_LLVM_AMDGPU_CS
= 90,
43 RADEON_LLVM_AMDGPU_HS
= 93,
46 #define CONST_ADDR_SPACE 2
47 #define LOCAL_ADDR_SPACE 3
49 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
50 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
52 struct nir_to_llvm_context
;
54 struct ac_nir_context
{
55 struct ac_llvm_context ac
;
56 struct ac_shader_abi
*abi
;
58 gl_shader_stage stage
;
60 struct hash_table
*defs
;
61 struct hash_table
*phis
;
62 struct hash_table
*vars
;
64 LLVMValueRef main_function
;
65 LLVMBasicBlockRef continue_block
;
66 LLVMBasicBlockRef break_block
;
68 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
73 struct nir_to_llvm_context
*nctx
; /* TODO get rid of this */
76 struct nir_to_llvm_context
{
77 struct ac_llvm_context ac
;
78 const struct ac_nir_compiler_options
*options
;
79 struct ac_shader_variant_info
*shader_info
;
80 struct ac_shader_abi abi
;
81 struct ac_nir_context
*nir
;
83 unsigned max_workgroup_size
;
84 LLVMContextRef context
;
86 LLVMBuilderRef builder
;
87 LLVMValueRef main_function
;
89 struct hash_table
*defs
;
90 struct hash_table
*phis
;
92 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
93 LLVMValueRef ring_offsets
;
94 LLVMValueRef push_constants
;
95 LLVMValueRef view_index
;
96 LLVMValueRef num_work_groups
;
97 LLVMValueRef workgroup_ids
[3];
98 LLVMValueRef local_invocation_ids
;
101 LLVMValueRef vertex_buffers
;
102 LLVMValueRef rel_auto_id
;
103 LLVMValueRef vs_prim_id
;
104 LLVMValueRef ls_out_layout
;
105 LLVMValueRef es2gs_offset
;
107 LLVMValueRef tcs_offchip_layout
;
108 LLVMValueRef tcs_out_offsets
;
109 LLVMValueRef tcs_out_layout
;
110 LLVMValueRef tcs_in_layout
;
112 LLVMValueRef merged_wave_info
;
113 LLVMValueRef tess_factor_offset
;
114 LLVMValueRef tcs_patch_id
;
115 LLVMValueRef tcs_rel_ids
;
116 LLVMValueRef tes_rel_patch_id
;
117 LLVMValueRef tes_patch_id
;
121 LLVMValueRef gsvs_ring_stride
;
122 LLVMValueRef gsvs_num_entries
;
123 LLVMValueRef gs2vs_offset
;
124 LLVMValueRef gs_wave_id
;
125 LLVMValueRef gs_vtx_offset
[6];
127 LLVMValueRef esgs_ring
;
128 LLVMValueRef gsvs_ring
;
129 LLVMValueRef hs_ring_tess_offchip
;
130 LLVMValueRef hs_ring_tess_factor
;
132 LLVMValueRef prim_mask
;
133 LLVMValueRef sample_pos_offset
;
134 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
135 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
137 gl_shader_stage stage
;
139 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
142 uint64_t output_mask
;
143 uint8_t num_output_clips
;
144 uint8_t num_output_culls
;
146 bool is_gs_copy_shader
;
147 LLVMValueRef gs_next_vertex
;
148 unsigned gs_max_out_vertices
;
150 unsigned tes_primitive_mode
;
151 uint64_t tess_outputs_written
;
152 uint64_t tess_patch_outputs_written
;
154 uint32_t tcs_patch_outputs_read
;
155 uint64_t tcs_outputs_read
;
158 static inline struct nir_to_llvm_context
*
159 nir_to_llvm_context_from_abi(struct ac_shader_abi
*abi
)
161 struct nir_to_llvm_context
*ctx
= NULL
;
162 return container_of(abi
, ctx
, abi
);
166 nir2llvmtype(struct ac_nir_context
*ctx
,
167 const struct glsl_type
*type
)
169 switch (glsl_get_base_type(glsl_without_array(type
))) {
173 case GLSL_TYPE_UINT64
:
174 case GLSL_TYPE_INT64
:
176 case GLSL_TYPE_DOUBLE
:
178 case GLSL_TYPE_FLOAT
:
181 assert(!"Unsupported type in nir2llvmtype()");
187 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
188 const nir_deref_var
*deref
,
189 enum ac_descriptor_type desc_type
,
190 const nir_tex_instr
*instr
,
191 bool image
, bool write
);
193 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
195 return (index
* 4) + chan
;
198 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
200 /* handle patch indices separate */
201 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
203 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
205 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
206 return 2 + (slot
- VARYING_SLOT_PATCH0
);
208 if (slot
== VARYING_SLOT_POS
)
210 if (slot
== VARYING_SLOT_PSIZ
)
212 if (slot
== VARYING_SLOT_CLIP_DIST0
)
214 /* 3 is reserved for clip dist as well */
215 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
216 return 4 + (slot
- VARYING_SLOT_VAR0
);
217 unreachable("illegal slot in get unique index\n");
220 static void set_llvm_calling_convention(LLVMValueRef func
,
221 gl_shader_stage stage
)
223 enum radeon_llvm_calling_convention calling_conv
;
226 case MESA_SHADER_VERTEX
:
227 case MESA_SHADER_TESS_EVAL
:
228 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
230 case MESA_SHADER_GEOMETRY
:
231 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
233 case MESA_SHADER_TESS_CTRL
:
234 calling_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
: RADEON_LLVM_AMDGPU_VS
;
236 case MESA_SHADER_FRAGMENT
:
237 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
239 case MESA_SHADER_COMPUTE
:
240 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
243 unreachable("Unhandle shader type");
246 LLVMSetFunctionCallConv(func
, calling_conv
);
251 LLVMTypeRef types
[MAX_ARGS
];
252 LLVMValueRef
*assign
[MAX_ARGS
];
253 unsigned array_params_mask
;
256 uint8_t num_user_sgprs_used
;
257 uint8_t num_sgprs_used
;
258 uint8_t num_vgprs_used
;
262 add_argument(struct arg_info
*info
,
263 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
265 assert(info
->count
< MAX_ARGS
);
266 info
->assign
[info
->count
] = param_ptr
;
267 info
->types
[info
->count
] = type
;
272 add_sgpr_argument(struct arg_info
*info
,
273 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
275 add_argument(info
, type
, param_ptr
);
276 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
281 add_user_sgpr_argument(struct arg_info
*info
,
283 LLVMValueRef
*param_ptr
)
285 add_sgpr_argument(info
, type
, param_ptr
);
286 info
->num_user_sgprs_used
+= ac_get_type_size(type
) / 4;
290 add_vgpr_argument(struct arg_info
*info
,
292 LLVMValueRef
*param_ptr
)
294 add_argument(info
, type
, param_ptr
);
295 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
299 add_user_sgpr_array_argument(struct arg_info
*info
,
301 LLVMValueRef
*param_ptr
)
303 info
->array_params_mask
|= (1 << info
->count
);
304 add_user_sgpr_argument(info
, type
, param_ptr
);
307 static void assign_arguments(LLVMValueRef main_function
,
308 struct arg_info
*info
)
311 for (i
= 0; i
< info
->count
; i
++) {
313 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
318 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
319 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
320 unsigned num_return_elems
,
321 struct arg_info
*args
,
322 unsigned max_workgroup_size
,
325 LLVMTypeRef main_function_type
, ret_type
;
326 LLVMBasicBlockRef main_function_body
;
328 if (num_return_elems
)
329 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
330 num_return_elems
, true);
332 ret_type
= LLVMVoidTypeInContext(ctx
);
334 /* Setup the function */
336 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
337 LLVMValueRef main_function
=
338 LLVMAddFunction(module
, "main", main_function_type
);
340 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
341 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
343 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
344 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
345 if (args
->array_params_mask
& (1 << i
)) {
346 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
347 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
348 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
351 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
355 if (max_workgroup_size
) {
356 ac_llvm_add_target_dep_function_attr(main_function
,
357 "amdgpu-max-work-group-size",
361 /* These were copied from some LLVM test. */
362 LLVMAddTargetDependentFunctionAttr(main_function
,
363 "less-precise-fpmad",
365 LLVMAddTargetDependentFunctionAttr(main_function
,
368 LLVMAddTargetDependentFunctionAttr(main_function
,
371 LLVMAddTargetDependentFunctionAttr(main_function
,
375 return main_function
;
378 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
380 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
384 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
386 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
387 type
= LLVMGetElementType(type
);
389 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
390 return LLVMGetIntTypeWidth(type
);
392 if (type
== ctx
->f16
)
394 if (type
== ctx
->f32
)
396 if (type
== ctx
->f64
)
399 unreachable("Unhandled type kind in get_elem_bits");
402 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
403 LLVMValueRef param
, unsigned rshift
,
406 LLVMValueRef value
= param
;
408 value
= LLVMBuildLShr(ctx
->builder
, value
,
409 LLVMConstInt(ctx
->i32
, rshift
, false), "");
411 if (rshift
+ bitwidth
< 32) {
412 unsigned mask
= (1 << bitwidth
) - 1;
413 value
= LLVMBuildAnd(ctx
->builder
, value
,
414 LLVMConstInt(ctx
->i32
, mask
, false), "");
419 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
421 switch (ctx
->stage
) {
422 case MESA_SHADER_TESS_CTRL
:
423 return unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
424 case MESA_SHADER_TESS_EVAL
:
425 return ctx
->tes_rel_patch_id
;
428 unreachable("Illegal stage");
432 /* Tessellation shaders pass outputs to the next shader using LDS.
434 * LS outputs = TCS inputs
435 * TCS outputs = TES inputs
438 * - TCS inputs for patch 0
439 * - TCS inputs for patch 1
440 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
442 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
443 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
444 * - TCS outputs for patch 1
445 * - Per-patch TCS outputs for patch 1
446 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
447 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
450 * All three shaders VS(LS), TCS, TES share the same LDS space.
453 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
455 if (ctx
->stage
== MESA_SHADER_VERTEX
)
456 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
457 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
458 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
466 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
468 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
472 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
474 return LLVMBuildMul(ctx
->builder
,
475 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
476 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
480 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
482 return LLVMBuildMul(ctx
->builder
,
483 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
484 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
488 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
490 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
491 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
493 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
497 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
499 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
500 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
501 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
503 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
504 LLVMBuildMul(ctx
->builder
, patch_stride
,
510 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
512 LLVMValueRef patch0_patch_data_offset
=
513 get_tcs_out_patch0_patch_data_offset(ctx
);
514 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
515 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
517 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
518 LLVMBuildMul(ctx
->builder
, patch_stride
,
523 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
525 ud_info
->sgpr_idx
= *sgpr_idx
;
526 ud_info
->num_sgprs
= num_sgprs
;
527 ud_info
->indirect
= false;
528 ud_info
->indirect_offset
= 0;
529 *sgpr_idx
+= num_sgprs
;
532 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
533 int idx
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
535 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
539 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
540 uint32_t indirect_offset
)
542 ud_info
->sgpr_idx
= sgpr_idx
;
543 ud_info
->num_sgprs
= num_sgprs
;
544 ud_info
->indirect
= true;
545 ud_info
->indirect_offset
= indirect_offset
;
548 struct user_sgpr_info
{
549 bool need_ring_offsets
;
551 bool indirect_all_descriptor_sets
;
554 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
555 struct user_sgpr_info
*user_sgpr_info
)
557 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
559 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
560 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
561 ctx
->stage
== MESA_SHADER_VERTEX
||
562 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
563 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
564 ctx
->is_gs_copy_shader
)
565 user_sgpr_info
->need_ring_offsets
= true;
567 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
568 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
569 user_sgpr_info
->need_ring_offsets
= true;
571 /* 2 user sgprs will nearly always be allocated for scratch/rings */
572 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
573 user_sgpr_info
->sgpr_count
+= 2;
576 switch (ctx
->stage
) {
577 case MESA_SHADER_COMPUTE
:
578 if (ctx
->shader_info
->info
.cs
.uses_grid_size
)
579 user_sgpr_info
->sgpr_count
+= 3;
581 case MESA_SHADER_FRAGMENT
:
582 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
584 case MESA_SHADER_VERTEX
:
585 if (!ctx
->is_gs_copy_shader
) {
586 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
587 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
588 user_sgpr_info
->sgpr_count
+= 3;
590 user_sgpr_info
->sgpr_count
+= 2;
593 if (ctx
->options
->key
.vs
.as_ls
)
594 user_sgpr_info
->sgpr_count
++;
596 case MESA_SHADER_TESS_CTRL
:
597 user_sgpr_info
->sgpr_count
+= 4;
599 case MESA_SHADER_TESS_EVAL
:
600 user_sgpr_info
->sgpr_count
+= 1;
602 case MESA_SHADER_GEOMETRY
:
603 user_sgpr_info
->sgpr_count
+= 2;
609 if (ctx
->shader_info
->info
.needs_push_constants
)
610 user_sgpr_info
->sgpr_count
+= 2;
612 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
613 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
614 user_sgpr_info
->sgpr_count
+= 2;
615 user_sgpr_info
->indirect_all_descriptor_sets
= true;
617 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
622 radv_define_common_user_sgprs_phase1(struct nir_to_llvm_context
*ctx
,
623 gl_shader_stage stage
,
624 bool has_previous_stage
,
625 gl_shader_stage previous_stage
,
626 const struct user_sgpr_info
*user_sgpr_info
,
627 struct arg_info
*args
,
628 LLVMValueRef
*desc_sets
)
630 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
631 unsigned stage_mask
= 1 << stage
;
632 if (has_previous_stage
)
633 stage_mask
|= 1 << previous_stage
;
635 /* 1 for each descriptor set */
636 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
637 for (unsigned i
= 0; i
< num_sets
; ++i
) {
638 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
639 add_user_sgpr_array_argument(args
, const_array(ctx
->ac
.i8
, 1024 * 1024), &ctx
->descriptor_sets
[i
]);
643 add_user_sgpr_array_argument(args
, const_array(const_array(ctx
->ac
.i8
, 1024 * 1024), 32), desc_sets
);
645 if (ctx
->shader_info
->info
.needs_push_constants
) {
646 /* 1 for push constants and dynamic descriptors */
647 add_user_sgpr_array_argument(args
, const_array(ctx
->ac
.i8
, 1024 * 1024), &ctx
->push_constants
);
652 radv_define_common_user_sgprs_phase2(struct nir_to_llvm_context
*ctx
,
653 gl_shader_stage stage
,
654 bool has_previous_stage
,
655 gl_shader_stage previous_stage
,
656 const struct user_sgpr_info
*user_sgpr_info
,
657 LLVMValueRef desc_sets
,
658 uint8_t *user_sgpr_idx
)
660 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
661 unsigned stage_mask
= 1 << stage
;
662 if (has_previous_stage
)
663 stage_mask
|= 1 << previous_stage
;
665 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
666 for (unsigned i
= 0; i
< num_sets
; ++i
) {
667 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
668 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
670 ctx
->descriptor_sets
[i
] = NULL
;
673 uint32_t desc_sgpr_idx
= *user_sgpr_idx
;
674 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, user_sgpr_idx
, 2);
676 for (unsigned i
= 0; i
< num_sets
; ++i
) {
677 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
678 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
679 ctx
->descriptor_sets
[i
] = ac_build_load_to_sgpr(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->ac
.i32
, i
, false));
682 ctx
->descriptor_sets
[i
] = NULL
;
684 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
687 if (ctx
->shader_info
->info
.needs_push_constants
) {
688 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
693 radv_define_vs_user_sgprs_phase1(struct nir_to_llvm_context
*ctx
,
694 gl_shader_stage stage
,
695 bool has_previous_stage
,
696 gl_shader_stage previous_stage
,
697 struct arg_info
*args
)
699 if (!ctx
->is_gs_copy_shader
&& (stage
== MESA_SHADER_VERTEX
|| (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
700 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
701 add_user_sgpr_argument(args
, const_array(ctx
->ac
.v4i32
, 16), &ctx
->vertex_buffers
); /* vertex buffers */
702 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
); // base vertex
703 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);// start instance
704 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
705 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
); // draw id
710 radv_define_vs_user_sgprs_phase2(struct nir_to_llvm_context
*ctx
,
711 gl_shader_stage stage
,
712 bool has_previous_stage
,
713 gl_shader_stage previous_stage
,
714 uint8_t *user_sgpr_idx
)
716 if (!ctx
->is_gs_copy_shader
&& (stage
== MESA_SHADER_VERTEX
|| (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
717 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
718 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
721 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
724 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, vs_num
);
729 static void create_function(struct nir_to_llvm_context
*ctx
,
730 gl_shader_stage stage
,
731 bool has_previous_stage
,
732 gl_shader_stage previous_stage
)
734 uint8_t user_sgpr_idx
;
735 struct user_sgpr_info user_sgpr_info
;
736 struct arg_info args
= {};
737 LLVMValueRef desc_sets
;
739 allocate_user_sgprs(ctx
, &user_sgpr_info
);
741 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
742 add_user_sgpr_argument(&args
, const_array(ctx
->ac
.v4i32
, 16), &ctx
->ring_offsets
); /* address of rings */
746 case MESA_SHADER_COMPUTE
:
747 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
748 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
749 add_user_sgpr_argument(&args
, ctx
->ac
.v3i32
,
750 &ctx
->num_work_groups
);
753 for (int i
= 0; i
< 3; i
++) {
754 ctx
->workgroup_ids
[i
] = NULL
;
755 if (ctx
->shader_info
->info
.cs
.uses_block_id
[i
]) {
756 add_sgpr_argument(&args
, ctx
->ac
.i32
,
757 &ctx
->workgroup_ids
[i
]);
761 if (ctx
->shader_info
->info
.cs
.uses_local_invocation_idx
)
762 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tg_size
);
763 add_vgpr_argument(&args
, ctx
->ac
.v3i32
, &ctx
->local_invocation_ids
);
765 case MESA_SHADER_VERTEX
:
766 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
767 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
768 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
))
769 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
770 if (ctx
->options
->key
.vs
.as_es
)
771 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->es2gs_offset
); // es2gs offset
772 else if (ctx
->options
->key
.vs
.as_ls
)
773 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->ls_out_layout
); // ls out layout
774 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
775 if (!ctx
->is_gs_copy_shader
) {
776 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
777 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
778 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
781 case MESA_SHADER_TESS_CTRL
:
782 if (has_previous_stage
) {
783 // First 6 system regs
784 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
785 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->merged_wave_info
); // merged wave info
786 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tess_factor_offset
); // tess factor offset
788 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // scratch offset
789 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
790 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
792 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
793 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
794 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->ls_out_layout
); // ls out layout
796 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
797 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
798 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_layout
); // tcs out layout
799 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_in_layout
); // tcs in layout
800 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
801 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
803 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_patch_id
); // patch id
804 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_rel_ids
); // rel ids;
805 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
806 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
807 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
808 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
810 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
811 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
812 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
813 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_layout
); // tcs out layout
814 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_in_layout
); // tcs in layout
815 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
816 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
817 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
818 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tess_factor_offset
); // tess factor offset
819 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_patch_id
); // patch id
820 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_rel_ids
); // rel ids;
823 case MESA_SHADER_TESS_EVAL
:
824 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
825 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
826 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
827 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
828 if (ctx
->options
->key
.tes
.as_es
) {
829 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // OC LDS
830 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); //
831 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->es2gs_offset
); // es2gs offset
833 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); //
834 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // OC LDS
836 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->tes_u
); // tes_u
837 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->tes_v
); // tes_v
838 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
839 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_patch_id
); // tes patch id
841 case MESA_SHADER_GEOMETRY
:
842 if (has_previous_stage
) {
843 // First 6 system regs
844 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
); // tess factor offset
845 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->merged_wave_info
); // merged wave info
846 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
848 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // scratch offset
849 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
850 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
852 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
853 if (previous_stage
== MESA_SHADER_TESS_EVAL
)
854 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
856 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
857 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
858 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
859 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
860 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
862 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[0]); // vtx01
863 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[2]); // vtx23
864 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.gs_prim_id
); // prim id
865 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.gs_invocation_id
);
866 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[4]);
868 if (previous_stage
== MESA_SHADER_VERTEX
) {
869 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
870 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
871 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
872 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
874 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->tes_u
); // tes_u
875 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->tes_v
); // tes_v
876 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
877 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_patch_id
); // tes patch id
880 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
881 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
882 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
883 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
884 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
885 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
886 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
); // gs2vs offset
887 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_wave_id
); // wave id
888 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[0]); // vtx0
889 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[1]); // vtx1
890 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.gs_prim_id
); // prim id
891 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[2]);
892 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[3]);
893 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[4]);
894 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[5]);
895 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.gs_invocation_id
);
898 case MESA_SHADER_FRAGMENT
:
899 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
900 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
901 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->sample_pos_offset
); /* sample position offset */
902 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->prim_mask
); /* prim mask */
903 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->persp_sample
); /* persp sample */
904 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->persp_center
); /* persp center */
905 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
); /* persp centroid */
906 add_vgpr_argument(&args
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
907 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->linear_sample
); /* linear sample */
908 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->linear_center
); /* linear center */
909 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
); /* linear centroid */
910 add_vgpr_argument(&args
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
911 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]); /* pos x float */
912 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]); /* pos y float */
913 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]); /* pos z float */
914 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]); /* pos w float */
915 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.front_face
); /* front face */
916 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
); /* ancillary */
917 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
); /* sample coverage */
918 add_vgpr_argument(&args
, ctx
->ac
.i32
, NULL
); /* fixed pt */
921 unreachable("Shader stage not implemented");
924 ctx
->main_function
= create_llvm_function(
925 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
926 ctx
->max_workgroup_size
,
927 ctx
->options
->unsafe_math
);
928 set_llvm_calling_convention(ctx
->main_function
, stage
);
931 ctx
->shader_info
->num_input_vgprs
= 0;
932 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
934 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
936 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
937 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
939 assign_arguments(ctx
->main_function
, &args
);
943 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
944 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, &user_sgpr_idx
, 2);
945 if (ctx
->options
->supports_spill
) {
946 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
947 LLVMPointerType(ctx
->ac
.i8
, CONST_ADDR_SPACE
),
948 NULL
, 0, AC_FUNC_ATTR_READNONE
);
949 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
950 const_array(ctx
->ac
.v4i32
, 16), "");
954 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
955 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
956 if (has_previous_stage
)
959 radv_define_common_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
962 case MESA_SHADER_COMPUTE
:
963 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
964 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
,
968 case MESA_SHADER_VERTEX
:
969 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
971 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
972 if (ctx
->options
->key
.vs
.as_ls
) {
973 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
975 if (ctx
->options
->key
.vs
.as_ls
)
976 ac_declare_lds_as_pointer(&ctx
->ac
);
978 case MESA_SHADER_TESS_CTRL
:
979 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
980 if (has_previous_stage
)
981 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
982 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
984 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
985 ac_declare_lds_as_pointer(&ctx
->ac
);
987 case MESA_SHADER_TESS_EVAL
:
988 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
990 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
992 case MESA_SHADER_GEOMETRY
:
993 if (has_previous_stage
) {
994 if (previous_stage
== MESA_SHADER_VERTEX
)
995 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
997 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
999 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, &user_sgpr_idx
, 2);
1000 if (ctx
->view_index
)
1001 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1002 if (has_previous_stage
)
1003 ac_declare_lds_as_pointer(&ctx
->ac
);
1005 case MESA_SHADER_FRAGMENT
:
1006 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
1007 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, &user_sgpr_idx
, 1);
1011 unreachable("Shader stage not implemented");
1014 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
1017 static int get_llvm_num_components(LLVMValueRef value
)
1019 LLVMTypeRef type
= LLVMTypeOf(value
);
1020 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1021 ? LLVMGetVectorSize(type
)
1023 return num_components
;
1026 static LLVMValueRef
llvm_extract_elem(struct ac_llvm_context
*ac
,
1030 int count
= get_llvm_num_components(value
);
1035 return LLVMBuildExtractElement(ac
->builder
, value
,
1036 LLVMConstInt(ac
->i32
, index
, false), "");
1039 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1040 LLVMValueRef value
, unsigned count
)
1042 unsigned num_components
= get_llvm_num_components(value
);
1043 if (count
== num_components
)
1046 LLVMValueRef masks
[] = {
1047 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1048 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1051 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1054 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1055 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1059 build_store_values_extended(struct ac_llvm_context
*ac
,
1060 LLVMValueRef
*values
,
1061 unsigned value_count
,
1062 unsigned value_stride
,
1065 LLVMBuilderRef builder
= ac
->builder
;
1068 for (i
= 0; i
< value_count
; i
++) {
1069 LLVMValueRef ptr
= values
[i
* value_stride
];
1070 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1071 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1072 LLVMBuildStore(builder
, value
, ptr
);
1076 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1077 const nir_ssa_def
*def
)
1079 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1080 if (def
->num_components
> 1) {
1081 type
= LLVMVectorType(type
, def
->num_components
);
1086 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1089 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1090 return (LLVMValueRef
)entry
->data
;
1094 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1095 const struct nir_block
*b
)
1097 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1098 return (LLVMBasicBlockRef
)entry
->data
;
1101 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1103 unsigned num_components
)
1105 LLVMValueRef value
= get_src(ctx
, src
.src
);
1106 bool need_swizzle
= false;
1109 LLVMTypeRef type
= LLVMTypeOf(value
);
1110 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1111 ? LLVMGetVectorSize(type
)
1114 for (unsigned i
= 0; i
< num_components
; ++i
) {
1115 assert(src
.swizzle
[i
] < src_components
);
1116 if (src
.swizzle
[i
] != i
)
1117 need_swizzle
= true;
1120 if (need_swizzle
|| num_components
!= src_components
) {
1121 LLVMValueRef masks
[] = {
1122 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1123 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1124 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1125 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1127 if (src_components
> 1 && num_components
== 1) {
1128 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1130 } else if (src_components
== 1 && num_components
> 1) {
1131 LLVMValueRef values
[] = {value
, value
, value
, value
};
1132 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1134 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1135 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1139 assert(!src
.negate
);
1144 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1145 LLVMIntPredicate pred
, LLVMValueRef src0
,
1148 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1149 return LLVMBuildSelect(ctx
->builder
, result
,
1150 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1154 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1155 LLVMRealPredicate pred
, LLVMValueRef src0
,
1158 LLVMValueRef result
;
1159 src0
= ac_to_float(ctx
, src0
);
1160 src1
= ac_to_float(ctx
, src1
);
1161 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1162 return LLVMBuildSelect(ctx
->builder
, result
,
1163 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1167 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1169 LLVMTypeRef result_type
,
1173 LLVMValueRef params
[] = {
1174 ac_to_float(ctx
, src0
),
1177 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1178 get_elem_bits(ctx
, result_type
));
1179 assert(length
< sizeof(name
));
1180 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1183 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1185 LLVMTypeRef result_type
,
1186 LLVMValueRef src0
, LLVMValueRef src1
)
1189 LLVMValueRef params
[] = {
1190 ac_to_float(ctx
, src0
),
1191 ac_to_float(ctx
, src1
),
1194 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1195 get_elem_bits(ctx
, result_type
));
1196 assert(length
< sizeof(name
));
1197 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1200 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1202 LLVMTypeRef result_type
,
1203 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1206 LLVMValueRef params
[] = {
1207 ac_to_float(ctx
, src0
),
1208 ac_to_float(ctx
, src1
),
1209 ac_to_float(ctx
, src2
),
1212 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1213 get_elem_bits(ctx
, result_type
));
1214 assert(length
< sizeof(name
));
1215 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1218 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1219 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1221 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1223 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1226 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1227 LLVMIntPredicate pred
,
1228 LLVMValueRef src0
, LLVMValueRef src1
)
1230 return LLVMBuildSelect(ctx
->builder
,
1231 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1236 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1239 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1240 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1243 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1246 LLVMValueRef cmp
, val
;
1248 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32_0
, "");
1249 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32_1
, src0
, "");
1250 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32_0
, "");
1251 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1255 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1258 LLVMValueRef cmp
, val
;
1260 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32_0
, "");
1261 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32_1
, src0
, "");
1262 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32_0
, "");
1263 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1267 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1270 const char *intr
= "llvm.floor.f32";
1271 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1272 LLVMValueRef params
[] = {
1275 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1276 ctx
->f32
, params
, 1,
1277 AC_FUNC_ATTR_READNONE
);
1278 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1281 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1283 LLVMValueRef src0
, LLVMValueRef src1
)
1285 LLVMTypeRef ret_type
;
1286 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1288 LLVMValueRef params
[] = { src0
, src1
};
1289 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1292 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1293 params
, 2, AC_FUNC_ATTR_READNONE
);
1295 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1296 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1300 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1303 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1306 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1309 src0
= ac_to_float(ctx
, src0
);
1310 return LLVMBuildSExt(ctx
->builder
,
1311 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1315 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1318 return LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1321 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1324 return LLVMBuildSExt(ctx
->builder
,
1325 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1329 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1332 LLVMValueRef result
;
1333 LLVMValueRef cond
= NULL
;
1335 src0
= ac_to_float(&ctx
->ac
, src0
);
1336 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->ac
.f16
, "");
1338 if (ctx
->options
->chip_class
>= VI
) {
1339 LLVMValueRef args
[2];
1340 /* Check if the result is a denormal - and flush to 0 if so. */
1342 args
[1] = LLVMConstInt(ctx
->ac
.i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1343 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->ac
.i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1346 /* need to convert back up to f32 */
1347 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->ac
.f32
, "");
1349 if (ctx
->options
->chip_class
>= VI
)
1350 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1353 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1354 * so compare the result and flush to 0 if it's smaller.
1356 LLVMValueRef temp
, cond2
;
1357 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1358 ctx
->ac
.f32
, result
);
1359 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1360 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, 0x38800000, false), ctx
->ac
.f32
, ""),
1362 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1363 temp
, ctx
->ac
.f32_0
, "");
1364 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1365 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1370 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1371 LLVMValueRef src0
, LLVMValueRef src1
)
1373 LLVMValueRef dst64
, result
;
1374 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1375 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1377 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1378 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1379 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1383 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1384 LLVMValueRef src0
, LLVMValueRef src1
)
1386 LLVMValueRef dst64
, result
;
1387 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1388 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1390 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1391 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1392 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1396 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1398 const LLVMValueRef srcs
[3])
1400 LLVMValueRef result
;
1401 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1403 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1404 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1408 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1409 LLVMValueRef src0
, LLVMValueRef src1
,
1410 LLVMValueRef src2
, LLVMValueRef src3
)
1412 LLVMValueRef bfi_args
[3], result
;
1414 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1415 LLVMBuildSub(ctx
->builder
,
1416 LLVMBuildShl(ctx
->builder
,
1421 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1424 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1427 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1428 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1430 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1431 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1432 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1434 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1438 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1441 LLVMValueRef comp
[2];
1443 src0
= ac_to_float(ctx
, src0
);
1444 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1445 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1447 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
1450 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1453 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1454 LLVMValueRef temps
[2], result
, val
;
1457 for (i
= 0; i
< 2; i
++) {
1458 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1459 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1460 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1461 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1464 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1466 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1471 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1477 LLVMValueRef result
;
1479 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1480 mask
= AC_TID_MASK_LEFT
;
1481 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1482 mask
= AC_TID_MASK_TOP
;
1484 mask
= AC_TID_MASK_TOP_LEFT
;
1486 /* for DDX we want to next X pixel, DDY next Y pixel. */
1487 if (op
== nir_op_fddx_fine
||
1488 op
== nir_op_fddx_coarse
||
1494 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1499 * this takes an I,J coordinate pair,
1500 * and works out the X and Y derivatives.
1501 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1503 static LLVMValueRef
emit_ddxy_interp(
1504 struct ac_nir_context
*ctx
,
1505 LLVMValueRef interp_ij
)
1507 LLVMValueRef result
[4], a
;
1510 for (i
= 0; i
< 2; i
++) {
1511 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1512 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1513 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1514 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1516 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1519 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1521 LLVMValueRef src
[4], result
= NULL
;
1522 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1523 unsigned src_components
;
1524 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1526 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1527 switch (instr
->op
) {
1533 case nir_op_pack_half_2x16
:
1536 case nir_op_unpack_half_2x16
:
1540 src_components
= num_components
;
1543 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1544 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1546 switch (instr
->op
) {
1552 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1553 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1556 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1559 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1562 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1565 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1566 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1567 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1570 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1571 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1572 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1575 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1578 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1581 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1584 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1587 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1588 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1589 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1590 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1591 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1592 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1593 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1596 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1597 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1598 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1601 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1604 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1607 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1610 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1611 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1612 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1615 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1616 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1617 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1620 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1621 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, src
[0]);
1624 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1627 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1630 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1633 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1634 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1635 LLVMTypeOf(src
[0]), ""),
1639 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1640 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1641 LLVMTypeOf(src
[0]), ""),
1645 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1646 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1647 LLVMTypeOf(src
[0]), ""),
1651 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1654 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1657 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1660 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1663 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1666 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1669 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1672 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1675 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1678 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1681 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1682 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1685 result
= emit_iabs(&ctx
->ac
, src
[0]);
1688 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1691 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1694 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1697 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1700 result
= emit_isign(&ctx
->ac
, src
[0]);
1703 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1704 result
= emit_fsign(&ctx
->ac
, src
[0]);
1707 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1708 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1711 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1712 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1715 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1716 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1718 case nir_op_fround_even
:
1719 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1720 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1723 result
= emit_ffract(&ctx
->ac
, src
[0]);
1726 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1727 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1730 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1731 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1734 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1735 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1738 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1739 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1742 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1743 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1746 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1747 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1748 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, result
);
1751 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1752 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1755 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1756 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1757 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1758 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1759 ac_to_float_type(&ctx
->ac
, def_type
),
1763 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1764 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1765 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1766 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1767 ac_to_float_type(&ctx
->ac
, def_type
),
1771 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1772 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1774 case nir_op_ibitfield_extract
:
1775 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1777 case nir_op_ubitfield_extract
:
1778 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1780 case nir_op_bitfield_insert
:
1781 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1783 case nir_op_bitfield_reverse
:
1784 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1786 case nir_op_bit_count
:
1787 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1792 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1793 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1794 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1798 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1799 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1803 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1804 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1808 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1809 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1813 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1814 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1817 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1818 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1821 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1825 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1826 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1827 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1829 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1833 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1834 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1835 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1837 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1840 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1842 case nir_op_find_lsb
:
1843 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1844 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1846 case nir_op_ufind_msb
:
1847 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1848 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1850 case nir_op_ifind_msb
:
1851 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1852 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1854 case nir_op_uadd_carry
:
1855 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1856 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1857 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1859 case nir_op_usub_borrow
:
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.usub.with.overflow.i32", src
[0], src
[1]);
1865 result
= emit_b2f(&ctx
->ac
, src
[0]);
1868 result
= emit_f2b(&ctx
->ac
, src
[0]);
1871 result
= emit_b2i(&ctx
->ac
, src
[0]);
1874 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1875 result
= emit_i2b(&ctx
->ac
, src
[0]);
1877 case nir_op_fquantize2f16
:
1878 result
= emit_f2f16(ctx
->nctx
, src
[0]);
1880 case nir_op_umul_high
:
1881 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1882 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1883 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1885 case nir_op_imul_high
:
1886 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1887 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1888 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1890 case nir_op_pack_half_2x16
:
1891 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
1893 case nir_op_unpack_half_2x16
:
1894 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1898 case nir_op_fddx_fine
:
1899 case nir_op_fddy_fine
:
1900 case nir_op_fddx_coarse
:
1901 case nir_op_fddy_coarse
:
1902 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1905 case nir_op_unpack_64_2x32_split_x
: {
1906 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1907 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1910 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1915 case nir_op_unpack_64_2x32_split_y
: {
1916 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1917 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1920 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1925 case nir_op_pack_64_2x32_split
: {
1926 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1927 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1928 src
[0], ctx
->ac
.i32_0
, "");
1929 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1930 src
[1], ctx
->ac
.i32_1
, "");
1931 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1936 fprintf(stderr
, "Unknown NIR alu instr: ");
1937 nir_print_instr(&instr
->instr
, stderr
);
1938 fprintf(stderr
, "\n");
1943 assert(instr
->dest
.dest
.is_ssa
);
1944 result
= ac_to_integer(&ctx
->ac
, result
);
1945 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1950 static void visit_load_const(struct ac_nir_context
*ctx
,
1951 const nir_load_const_instr
*instr
)
1953 LLVMValueRef values
[4], value
= NULL
;
1954 LLVMTypeRef element_type
=
1955 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1957 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1958 switch (instr
->def
.bit_size
) {
1960 values
[i
] = LLVMConstInt(element_type
,
1961 instr
->value
.u32
[i
], false);
1964 values
[i
] = LLVMConstInt(element_type
,
1965 instr
->value
.u64
[i
], false);
1969 "unsupported nir load_const bit_size: %d\n",
1970 instr
->def
.bit_size
);
1974 if (instr
->def
.num_components
> 1) {
1975 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1979 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1982 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1985 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1986 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1987 LLVMPointerType(type
, addr_space
), "");
1991 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1994 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1995 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1998 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1999 /* On VI, the descriptor contains the size in bytes,
2000 * but TXQ must return the size in elements.
2001 * The stride is always non-zero for resources using TXQ.
2003 LLVMValueRef stride
=
2004 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2006 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2007 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2008 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2009 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2011 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2017 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2020 static void build_int_type_name(
2022 char *buf
, unsigned bufsize
)
2024 assert(bufsize
>= 6);
2026 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2027 snprintf(buf
, bufsize
, "v%ui32",
2028 LLVMGetVectorSize(type
));
2033 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2034 struct ac_image_args
*args
,
2035 const nir_tex_instr
*instr
)
2037 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2038 LLVMValueRef coord
= args
->addr
;
2039 LLVMValueRef half_texel
[2];
2040 LLVMValueRef compare_cube_wa
= NULL
;
2041 LLVMValueRef result
;
2043 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2047 struct ac_image_args txq_args
= { 0 };
2049 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2050 txq_args
.opcode
= ac_image_get_resinfo
;
2051 txq_args
.dmask
= 0xf;
2052 txq_args
.addr
= ctx
->i32_0
;
2053 txq_args
.resource
= args
->resource
;
2054 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2056 for (c
= 0; c
< 2; c
++) {
2057 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2058 LLVMConstInt(ctx
->i32
, c
, false), "");
2059 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2060 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2061 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2062 LLVMConstReal(ctx
->f32
, -0.5), "");
2066 LLVMValueRef orig_coords
= args
->addr
;
2068 for (c
= 0; c
< 2; c
++) {
2070 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2071 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2072 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2073 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2074 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2075 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2080 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2081 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2082 * workaround by sampling using a scaled type and converting.
2083 * This is taken from amdgpu-pro shaders.
2085 /* NOTE this produces some ugly code compared to amdgpu-pro,
2086 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2087 * and then reads them back. -pro generates two selects,
2088 * one s_cmp for the descriptor rewriting
2089 * one v_cmp for the coordinate and result changes.
2091 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2092 LLVMValueRef tmp
, tmp2
;
2094 /* workaround 8/8/8/8 uint/sint cube gather bug */
2095 /* first detect it then change to a scaled read and f2i */
2096 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2099 /* extract the DATA_FORMAT */
2100 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2101 LLVMConstInt(ctx
->i32
, 6, false), false);
2103 /* is the DATA_FORMAT == 8_8_8_8 */
2104 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2106 if (stype
== GLSL_TYPE_UINT
)
2107 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2108 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2109 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2111 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2112 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2113 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2115 /* replace the NUM FORMAT in the descriptor */
2116 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2117 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2119 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2121 /* don't modify the coordinates for this case */
2122 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2125 result
= ac_build_image_opcode(ctx
, args
);
2127 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2128 LLVMValueRef tmp
, tmp2
;
2130 /* if the cube workaround is in place, f2i the result. */
2131 for (c
= 0; c
< 4; c
++) {
2132 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2133 if (stype
== GLSL_TYPE_UINT
)
2134 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2136 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2137 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2138 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2139 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2140 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2141 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2147 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2148 const nir_tex_instr
*instr
,
2150 struct ac_image_args
*args
)
2152 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2153 return ac_build_buffer_load_format(&ctx
->ac
,
2160 args
->opcode
= ac_image_sample
;
2161 args
->compare
= instr
->is_shadow
;
2163 switch (instr
->op
) {
2165 case nir_texop_txf_ms
:
2166 case nir_texop_samples_identical
:
2167 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2168 args
->compare
= false;
2169 args
->offset
= false;
2176 args
->level_zero
= true;
2181 case nir_texop_query_levels
:
2182 args
->opcode
= ac_image_get_resinfo
;
2185 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2186 args
->level_zero
= true;
2192 args
->opcode
= ac_image_gather4
;
2193 args
->level_zero
= true;
2196 args
->opcode
= ac_image_get_lod
;
2197 args
->compare
= false;
2198 args
->offset
= false;
2204 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2205 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2206 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2207 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2210 return ac_build_image_opcode(&ctx
->ac
, args
);
2213 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2214 nir_intrinsic_instr
*instr
)
2216 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2217 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2218 unsigned binding
= nir_intrinsic_binding(instr
);
2219 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2220 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2221 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2222 unsigned base_offset
= layout
->binding
[binding
].offset
;
2223 LLVMValueRef offset
, stride
;
2225 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2226 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2227 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2228 layout
->binding
[binding
].dynamic_offset_offset
;
2229 desc_ptr
= ctx
->push_constants
;
2230 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2231 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2233 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2235 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2236 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2237 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2239 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2240 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->ac
.v4i32
);
2241 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2246 static LLVMValueRef
visit_vulkan_resource_reindex(struct nir_to_llvm_context
*ctx
,
2247 nir_intrinsic_instr
*instr
)
2249 LLVMValueRef ptr
= get_src(ctx
->nir
, instr
->src
[0]);
2250 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[1]);
2252 LLVMValueRef result
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2253 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2257 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2258 nir_intrinsic_instr
*instr
)
2260 LLVMValueRef ptr
, addr
;
2262 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2263 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2265 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2266 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2268 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2271 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2272 const nir_intrinsic_instr
*instr
)
2274 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
2276 return get_buffer_size(ctx
, LLVMBuildLoad(ctx
->ac
.builder
, ptr
, ""), false);
2278 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2279 nir_intrinsic_instr
*instr
)
2281 const char *store_name
;
2282 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2283 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2284 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2285 int components_32bit
= elem_size_mult
* instr
->num_components
;
2286 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2287 LLVMValueRef base_data
, base_offset
;
2288 LLVMValueRef params
[6];
2290 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2291 get_src(ctx
, instr
->src
[1]), true);
2292 params
[2] = ctx
->ac
.i32_0
; /* vindex */
2293 params
[4] = ctx
->ac
.i1false
; /* glc */
2294 params
[5] = ctx
->ac
.i1false
; /* slc */
2296 if (components_32bit
> 1)
2297 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2299 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2300 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2301 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2303 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2307 LLVMValueRef offset
;
2309 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2311 /* Due to an LLVM limitation, split 3-element writes
2312 * into a 2-element and a 1-element write. */
2314 writemask
|= 1 << (start
+ 2);
2318 start
*= elem_size_mult
;
2319 count
*= elem_size_mult
;
2322 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2327 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2329 } else if (count
== 2) {
2330 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2331 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2332 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(ctx
->ac
.v2f32
), tmp
,
2335 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2336 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2337 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2339 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2343 if (get_llvm_num_components(base_data
) > 1)
2344 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2345 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2348 store_name
= "llvm.amdgcn.buffer.store.f32";
2351 offset
= base_offset
;
2353 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2357 ac_build_intrinsic(&ctx
->ac
, store_name
,
2358 ctx
->ac
.voidt
, params
, 6, 0);
2362 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2363 const nir_intrinsic_instr
*instr
)
2366 LLVMValueRef params
[6];
2369 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2370 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2372 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2373 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2374 get_src(ctx
, instr
->src
[0]),
2376 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
2377 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2378 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2380 switch (instr
->intrinsic
) {
2381 case nir_intrinsic_ssbo_atomic_add
:
2382 name
= "llvm.amdgcn.buffer.atomic.add";
2384 case nir_intrinsic_ssbo_atomic_imin
:
2385 name
= "llvm.amdgcn.buffer.atomic.smin";
2387 case nir_intrinsic_ssbo_atomic_umin
:
2388 name
= "llvm.amdgcn.buffer.atomic.umin";
2390 case nir_intrinsic_ssbo_atomic_imax
:
2391 name
= "llvm.amdgcn.buffer.atomic.smax";
2393 case nir_intrinsic_ssbo_atomic_umax
:
2394 name
= "llvm.amdgcn.buffer.atomic.umax";
2396 case nir_intrinsic_ssbo_atomic_and
:
2397 name
= "llvm.amdgcn.buffer.atomic.and";
2399 case nir_intrinsic_ssbo_atomic_or
:
2400 name
= "llvm.amdgcn.buffer.atomic.or";
2402 case nir_intrinsic_ssbo_atomic_xor
:
2403 name
= "llvm.amdgcn.buffer.atomic.xor";
2405 case nir_intrinsic_ssbo_atomic_exchange
:
2406 name
= "llvm.amdgcn.buffer.atomic.swap";
2408 case nir_intrinsic_ssbo_atomic_comp_swap
:
2409 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2415 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2418 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2419 const nir_intrinsic_instr
*instr
)
2421 LLVMValueRef results
[2];
2422 int load_components
;
2423 int num_components
= instr
->num_components
;
2424 if (instr
->dest
.ssa
.bit_size
== 64)
2425 num_components
*= 2;
2427 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2428 load_components
= MIN2(num_components
- i
, 4);
2429 const char *load_name
;
2430 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2431 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2432 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2434 if (load_components
== 3)
2435 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2436 else if (load_components
> 1)
2437 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2439 if (load_components
>= 3)
2440 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2441 else if (load_components
== 2)
2442 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2443 else if (load_components
== 1)
2444 load_name
= "llvm.amdgcn.buffer.load.f32";
2446 unreachable("unhandled number of components");
2448 LLVMValueRef params
[] = {
2449 ctx
->abi
->load_ssbo(ctx
->abi
,
2450 get_src(ctx
, instr
->src
[0]),
2458 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2463 LLVMValueRef ret
= results
[0];
2464 if (num_components
> 4 || num_components
== 3) {
2465 LLVMValueRef masks
[] = {
2466 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2467 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2468 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2469 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2472 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2473 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2474 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2477 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2478 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2481 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2482 const nir_intrinsic_instr
*instr
)
2484 LLVMValueRef results
[8], ret
;
2485 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2486 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2487 int num_components
= instr
->num_components
;
2489 if (ctx
->abi
->load_ubo
)
2490 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2492 if (instr
->dest
.ssa
.bit_size
== 64)
2493 num_components
*= 2;
2495 for (unsigned i
= 0; i
< num_components
; ++i
) {
2496 LLVMValueRef params
[] = {
2498 LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, 4 * i
, 0),
2501 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const.v4i32", ctx
->ac
.f32
,
2503 AC_FUNC_ATTR_READNONE
|
2504 AC_FUNC_ATTR_LEGACY
);
2508 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2509 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2510 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2514 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2515 bool vs_in
, unsigned *vertex_index_out
,
2516 LLVMValueRef
*vertex_index_ref
,
2517 unsigned *const_out
, LLVMValueRef
*indir_out
)
2519 unsigned const_offset
= 0;
2520 nir_deref
*tail
= &deref
->deref
;
2521 LLVMValueRef offset
= NULL
;
2523 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2525 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2526 if (vertex_index_out
)
2527 *vertex_index_out
= deref_array
->base_offset
;
2529 if (vertex_index_ref
) {
2530 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2531 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2532 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2534 *vertex_index_ref
= vtx
;
2538 if (deref
->var
->data
.compact
) {
2539 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2540 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2541 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2542 /* We always lower indirect dereferences for "compact" array vars. */
2543 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2545 const_offset
= deref_array
->base_offset
;
2549 while (tail
->child
!= NULL
) {
2550 const struct glsl_type
*parent_type
= tail
->type
;
2553 if (tail
->deref_type
== nir_deref_type_array
) {
2554 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2555 LLVMValueRef index
, stride
, local_offset
;
2556 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2558 const_offset
+= size
* deref_array
->base_offset
;
2559 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2562 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2563 index
= get_src(ctx
, deref_array
->indirect
);
2564 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2565 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2568 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2570 offset
= local_offset
;
2571 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2572 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2574 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2575 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2576 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2579 unreachable("unsupported deref type");
2583 if (const_offset
&& offset
)
2584 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2585 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2588 *const_out
= const_offset
;
2589 *indir_out
= offset
;
2593 /* The offchip buffer layout for TCS->TES is
2595 * - attribute 0 of patch 0 vertex 0
2596 * - attribute 0 of patch 0 vertex 1
2597 * - attribute 0 of patch 0 vertex 2
2599 * - attribute 0 of patch 1 vertex 0
2600 * - attribute 0 of patch 1 vertex 1
2602 * - attribute 1 of patch 0 vertex 0
2603 * - attribute 1 of patch 0 vertex 1
2605 * - per patch attribute 0 of patch 0
2606 * - per patch attribute 0 of patch 1
2609 * Note that every attribute has 4 components.
2611 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2612 LLVMValueRef vertex_index
,
2613 LLVMValueRef param_index
)
2615 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2616 LLVMValueRef param_stride
, constant16
;
2617 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2619 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2620 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2621 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2624 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2626 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2627 vertices_per_patch
, "");
2629 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2632 param_stride
= total_vertices
;
2634 base_addr
= rel_patch_id
;
2635 param_stride
= num_patches
;
2638 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2639 LLVMBuildMul(ctx
->builder
, param_index
,
2640 param_stride
, ""), "");
2642 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2644 if (!vertex_index
) {
2645 LLVMValueRef patch_data_offset
=
2646 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2648 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2649 patch_data_offset
, "");
2654 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2656 unsigned const_index
,
2658 LLVMValueRef vertex_index
,
2659 LLVMValueRef indir_index
)
2661 LLVMValueRef param_index
;
2664 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2667 if (const_index
&& !is_compact
)
2668 param
+= const_index
;
2669 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2671 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2675 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2676 bool is_patch
, uint32_t param
)
2680 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2682 ctx
->tess_outputs_written
|= (1ull << param
);
2686 get_dw_address(struct nir_to_llvm_context
*ctx
,
2687 LLVMValueRef dw_addr
,
2689 unsigned const_index
,
2690 bool compact_const_index
,
2691 LLVMValueRef vertex_index
,
2692 LLVMValueRef stride
,
2693 LLVMValueRef indir_index
)
2698 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2699 LLVMBuildMul(ctx
->builder
,
2705 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2706 LLVMBuildMul(ctx
->builder
, indir_index
,
2707 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2708 else if (const_index
&& !compact_const_index
)
2709 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2710 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2712 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2713 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2715 if (const_index
&& compact_const_index
)
2716 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2717 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2722 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2723 nir_intrinsic_instr
*instr
)
2725 LLVMValueRef dw_addr
, stride
;
2726 unsigned const_index
;
2727 LLVMValueRef vertex_index
;
2728 LLVMValueRef indir_index
;
2730 LLVMValueRef value
[4], result
;
2731 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2732 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2733 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2734 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2735 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2736 &const_index
, &indir_index
);
2738 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2739 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2740 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2743 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2744 for (unsigned i
= 0; i
< instr
->num_components
+ comp
; i
++) {
2745 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2746 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2749 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2750 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2755 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2756 nir_intrinsic_instr
*instr
)
2758 LLVMValueRef dw_addr
;
2759 LLVMValueRef stride
= NULL
;
2760 LLVMValueRef value
[4], result
;
2761 LLVMValueRef vertex_index
= NULL
;
2762 LLVMValueRef indir_index
= NULL
;
2763 unsigned const_index
= 0;
2765 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2766 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2767 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2768 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2769 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2770 &const_index
, &indir_index
);
2772 if (!instr
->variables
[0]->var
->data
.patch
) {
2773 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2774 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2776 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2779 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2782 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2783 for (unsigned i
= comp
; i
< instr
->num_components
+ comp
; i
++) {
2784 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2785 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2788 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2789 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2794 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2795 nir_intrinsic_instr
*instr
,
2799 LLVMValueRef dw_addr
;
2800 LLVMValueRef stride
= NULL
;
2801 LLVMValueRef buf_addr
= NULL
;
2802 LLVMValueRef vertex_index
= NULL
;
2803 LLVMValueRef indir_index
= NULL
;
2804 unsigned const_index
= 0;
2806 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2807 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2808 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2809 bool store_lds
= true;
2811 if (instr
->variables
[0]->var
->data
.patch
) {
2812 if (!(ctx
->tcs_patch_outputs_read
& (1U << (instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_PATCH0
))))
2815 if (!(ctx
->tcs_outputs_read
& (1ULL << instr
->variables
[0]->var
->data
.location
)))
2818 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2819 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2820 &const_index
, &indir_index
);
2822 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2823 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2824 is_compact
&& const_index
> 3) {
2829 if (!instr
->variables
[0]->var
->data
.patch
) {
2830 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2831 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2833 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2836 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2838 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2840 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2841 vertex_index
, indir_index
);
2843 bool is_tess_factor
= false;
2844 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2845 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2846 is_tess_factor
= true;
2848 unsigned base
= is_compact
? const_index
: 0;
2849 for (unsigned chan
= 0; chan
< 8; chan
++) {
2850 if (!(writemask
& (1 << chan
)))
2852 LLVMValueRef value
= llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2854 if (store_lds
|| is_tess_factor
)
2855 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
2857 if (!is_tess_factor
&& writemask
!= 0xF)
2858 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2859 buf_addr
, ctx
->oc_lds
,
2860 4 * (base
+ chan
), 1, 0, true, false);
2862 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2866 if (writemask
== 0xF) {
2867 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2868 buf_addr
, ctx
->oc_lds
,
2869 (base
* 4), 1, 0, true, false);
2874 load_tes_input(struct nir_to_llvm_context
*ctx
,
2875 const nir_intrinsic_instr
*instr
)
2877 LLVMValueRef buf_addr
;
2878 LLVMValueRef result
;
2879 LLVMValueRef vertex_index
= NULL
;
2880 LLVMValueRef indir_index
= NULL
;
2881 unsigned const_index
= 0;
2883 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2884 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2886 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2887 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2888 &const_index
, &indir_index
);
2889 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2890 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2891 is_compact
&& const_index
> 3) {
2896 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2897 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2898 is_compact
, vertex_index
, indir_index
);
2900 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, comp
* 4, false);
2901 buf_addr
= LLVMBuildAdd(ctx
->builder
, buf_addr
, comp_offset
, "");
2903 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2904 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2905 result
= trim_vector(&ctx
->ac
, result
, instr
->num_components
);
2906 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2911 load_gs_input(struct ac_shader_abi
*abi
,
2913 unsigned driver_location
,
2915 unsigned num_components
,
2916 unsigned vertex_index
,
2917 unsigned const_index
,
2920 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2921 LLVMValueRef vtx_offset
;
2922 LLVMValueRef args
[9];
2923 unsigned param
, vtx_offset_param
;
2924 LLVMValueRef value
[4], result
;
2926 vtx_offset_param
= vertex_index
;
2927 assert(vtx_offset_param
< 6);
2928 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2929 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
2931 param
= shader_io_get_unique_index(location
);
2933 for (unsigned i
= component
; i
< num_components
+ component
; i
++) {
2934 if (ctx
->ac
.chip_class
>= GFX9
) {
2935 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
2936 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2937 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
2938 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2940 args
[0] = ctx
->esgs_ring
;
2941 args
[1] = vtx_offset
;
2942 args
[2] = LLVMConstInt(ctx
->ac
.i32
, (param
* 4 + i
+ const_index
) * 256, false);
2943 args
[3] = ctx
->ac
.i32_0
;
2944 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
2945 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
2946 args
[6] = ctx
->ac
.i32_1
; /* GLC */
2947 args
[7] = ctx
->ac
.i32_0
; /* SLC */
2948 args
[8] = ctx
->ac
.i32_0
; /* TFE */
2950 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2951 ctx
->ac
.i32
, args
, 9,
2952 AC_FUNC_ATTR_READONLY
|
2953 AC_FUNC_ATTR_LEGACY
);
2956 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
2962 build_gep_for_deref(struct ac_nir_context
*ctx
,
2963 nir_deref_var
*deref
)
2965 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
2966 assert(entry
->data
);
2967 LLVMValueRef val
= entry
->data
;
2968 nir_deref
*tail
= deref
->deref
.child
;
2969 while (tail
!= NULL
) {
2970 LLVMValueRef offset
;
2971 switch (tail
->deref_type
) {
2972 case nir_deref_type_array
: {
2973 nir_deref_array
*array
= nir_deref_as_array(tail
);
2974 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
2975 if (array
->deref_array_type
==
2976 nir_deref_array_type_indirect
) {
2977 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2984 case nir_deref_type_struct
: {
2985 nir_deref_struct
*deref_struct
=
2986 nir_deref_as_struct(tail
);
2987 offset
= LLVMConstInt(ctx
->ac
.i32
,
2988 deref_struct
->index
, 0);
2992 unreachable("bad deref type");
2994 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3000 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3001 nir_intrinsic_instr
*instr
)
3003 LLVMValueRef values
[8];
3004 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3005 int ve
= instr
->dest
.ssa
.num_components
;
3006 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3007 LLVMValueRef indir_index
;
3009 unsigned const_index
;
3010 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3011 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3012 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3013 &const_index
, &indir_index
);
3015 if (instr
->dest
.ssa
.bit_size
== 64)
3018 switch (instr
->variables
[0]->var
->data
.mode
) {
3019 case nir_var_shader_in
:
3020 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3021 return load_tcs_input(ctx
->nctx
, instr
);
3022 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3023 return load_tes_input(ctx
->nctx
, instr
);
3024 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3025 LLVMValueRef indir_index
;
3026 unsigned const_index
, vertex_index
;
3027 get_deref_offset(ctx
, instr
->variables
[0],
3028 false, &vertex_index
, NULL
,
3029 &const_index
, &indir_index
);
3030 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
3031 instr
->variables
[0]->var
->data
.driver_location
,
3032 instr
->variables
[0]->var
->data
.location_frac
, ve
,
3033 vertex_index
, const_index
,
3034 nir2llvmtype(ctx
, instr
->variables
[0]->var
->type
));
3037 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3039 unsigned count
= glsl_count_attribute_slots(
3040 instr
->variables
[0]->var
->type
,
3041 ctx
->stage
== MESA_SHADER_VERTEX
);
3043 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3044 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3047 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3051 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* 4];
3055 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3057 unsigned count
= glsl_count_attribute_slots(
3058 instr
->variables
[0]->var
->type
, false);
3060 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3061 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3064 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3068 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
3072 case nir_var_shared
: {
3073 LLVMValueRef address
= build_gep_for_deref(ctx
,
3074 instr
->variables
[0]);
3075 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3076 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3077 get_def_type(ctx
, &instr
->dest
.ssa
),
3080 case nir_var_shader_out
:
3081 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3082 return load_tcs_output(ctx
->nctx
, instr
);
3084 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3086 unsigned count
= glsl_count_attribute_slots(
3087 instr
->variables
[0]->var
->type
, false);
3089 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3090 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3093 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3097 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3098 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
3104 unreachable("unhandle variable mode");
3106 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3107 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3111 visit_store_var(struct ac_nir_context
*ctx
,
3112 nir_intrinsic_instr
*instr
)
3114 LLVMValueRef temp_ptr
, value
;
3115 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3116 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3117 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3118 int writemask
= instr
->const_index
[0] << comp
;
3119 LLVMValueRef indir_index
;
3120 unsigned const_index
;
3121 get_deref_offset(ctx
, instr
->variables
[0], false,
3122 NULL
, NULL
, &const_index
, &indir_index
);
3124 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3125 int old_writemask
= writemask
;
3127 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3128 LLVMVectorType(ctx
->ac
.f32
, get_llvm_num_components(src
) * 2),
3132 for (unsigned chan
= 0; chan
< 4; chan
++) {
3133 if (old_writemask
& (1 << chan
))
3134 writemask
|= 3u << (2 * chan
);
3138 switch (instr
->variables
[0]->var
->data
.mode
) {
3139 case nir_var_shader_out
:
3141 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3142 store_tcs_output(ctx
->nctx
, instr
, src
, writemask
);
3146 for (unsigned chan
= 0; chan
< 8; chan
++) {
3148 if (!(writemask
& (1 << chan
)))
3151 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3153 if (instr
->variables
[0]->var
->data
.compact
)
3156 unsigned count
= glsl_count_attribute_slots(
3157 instr
->variables
[0]->var
->type
, false);
3159 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3160 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3161 stride
, true, true);
3163 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3164 value
, indir_index
, "");
3165 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3166 count
, stride
, tmp_vec
);
3169 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3171 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3176 for (unsigned chan
= 0; chan
< 8; chan
++) {
3177 if (!(writemask
& (1 << chan
)))
3180 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
3182 unsigned count
= glsl_count_attribute_slots(
3183 instr
->variables
[0]->var
->type
, false);
3185 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3186 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3189 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3190 value
, indir_index
, "");
3191 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3194 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3196 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3200 case nir_var_shared
: {
3201 int writemask
= instr
->const_index
[0];
3202 LLVMValueRef address
= build_gep_for_deref(ctx
,
3203 instr
->variables
[0]);
3204 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3205 unsigned components
=
3206 glsl_get_vector_elements(
3207 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3208 if (writemask
== (1 << components
) - 1) {
3209 val
= LLVMBuildBitCast(
3210 ctx
->ac
.builder
, val
,
3211 LLVMGetElementType(LLVMTypeOf(address
)), "");
3212 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3214 for (unsigned chan
= 0; chan
< 4; chan
++) {
3215 if (!(writemask
& (1 << chan
)))
3218 LLVMBuildStructGEP(ctx
->ac
.builder
,
3220 LLVMValueRef src
= llvm_extract_elem(&ctx
->ac
, val
,
3222 src
= LLVMBuildBitCast(
3223 ctx
->ac
.builder
, src
,
3224 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3225 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3235 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3238 case GLSL_SAMPLER_DIM_BUF
:
3240 case GLSL_SAMPLER_DIM_1D
:
3241 return array
? 2 : 1;
3242 case GLSL_SAMPLER_DIM_2D
:
3243 return array
? 3 : 2;
3244 case GLSL_SAMPLER_DIM_MS
:
3245 return array
? 4 : 3;
3246 case GLSL_SAMPLER_DIM_3D
:
3247 case GLSL_SAMPLER_DIM_CUBE
:
3249 case GLSL_SAMPLER_DIM_RECT
:
3250 case GLSL_SAMPLER_DIM_SUBPASS
:
3252 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3262 /* Adjust the sample index according to FMASK.
3264 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3265 * which is the identity mapping. Each nibble says which physical sample
3266 * should be fetched to get that sample.
3268 * For example, 0x11111100 means there are only 2 samples stored and
3269 * the second sample covers 3/4 of the pixel. When reading samples 0
3270 * and 1, return physical sample 0 (determined by the first two 0s
3271 * in FMASK), otherwise return physical sample 1.
3273 * The sample index should be adjusted as follows:
3274 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3276 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3277 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3278 LLVMValueRef coord_z
,
3279 LLVMValueRef sample_index
,
3280 LLVMValueRef fmask_desc_ptr
)
3282 LLVMValueRef fmask_load_address
[4];
3285 fmask_load_address
[0] = coord_x
;
3286 fmask_load_address
[1] = coord_y
;
3288 fmask_load_address
[2] = coord_z
;
3289 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3292 struct ac_image_args args
= {0};
3294 args
.opcode
= ac_image_load
;
3295 args
.da
= coord_z
? true : false;
3296 args
.resource
= fmask_desc_ptr
;
3298 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3300 res
= ac_build_image_opcode(ctx
, &args
);
3302 res
= ac_to_integer(ctx
, res
);
3303 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3304 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3306 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3310 LLVMValueRef sample_index4
=
3311 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3312 LLVMValueRef shifted_fmask
=
3313 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3314 LLVMValueRef final_sample
=
3315 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3317 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3318 * resource descriptor is 0 (invalid),
3320 LLVMValueRef fmask_desc
=
3321 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3324 LLVMValueRef fmask_word1
=
3325 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3328 LLVMValueRef word1_is_nonzero
=
3329 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3330 fmask_word1
, ctx
->i32_0
, "");
3332 /* Replace the MSAA sample index. */
3334 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3335 final_sample
, sample_index
, "");
3336 return sample_index
;
3339 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3340 const nir_intrinsic_instr
*instr
)
3342 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3343 if(instr
->variables
[0]->deref
.child
)
3344 type
= instr
->variables
[0]->deref
.child
->type
;
3346 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3347 LLVMValueRef coords
[4];
3348 LLVMValueRef masks
[] = {
3349 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3350 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3353 LLVMValueRef sample_index
= llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3356 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3357 bool is_array
= glsl_sampler_type_is_array(type
);
3358 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3359 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3360 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3361 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3362 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3363 count
= image_type_to_components_count(dim
, is_array
);
3366 LLVMValueRef fmask_load_address
[3];
3369 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3370 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3372 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3374 fmask_load_address
[2] = NULL
;
3376 for (chan
= 0; chan
< 2; ++chan
)
3377 fmask_load_address
[chan
] =
3378 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3379 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3380 ctx
->ac
.i32
, ""), "");
3381 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3383 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3384 fmask_load_address
[0],
3385 fmask_load_address
[1],
3386 fmask_load_address
[2],
3388 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3390 if (count
== 1 && !gfx9_1d
) {
3391 if (instr
->src
[0].ssa
->num_components
)
3392 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3399 for (chan
= 0; chan
< count
; ++chan
) {
3400 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3403 for (chan
= 0; chan
< 2; ++chan
)
3404 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3405 ctx
->ac
.i32
, ""), "");
3406 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3412 coords
[2] = coords
[1];
3413 coords
[1] = ctx
->ac
.i32_0
;
3415 coords
[1] = ctx
->ac
.i32_0
;
3420 coords
[count
] = sample_index
;
3425 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3428 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3433 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3434 const nir_intrinsic_instr
*instr
)
3436 LLVMValueRef params
[7];
3438 char intrinsic_name
[64];
3439 const nir_variable
*var
= instr
->variables
[0]->var
;
3440 const struct glsl_type
*type
= var
->type
;
3442 if(instr
->variables
[0]->deref
.child
)
3443 type
= instr
->variables
[0]->deref
.child
->type
;
3445 type
= glsl_without_array(type
);
3446 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3447 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3448 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3449 ctx
->ac
.i32_0
, ""); /* vindex */
3450 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3451 params
[3] = ctx
->ac
.i1false
; /* glc */
3452 params
[4] = ctx
->ac
.i1false
; /* slc */
3453 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3456 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3457 res
= ac_to_integer(&ctx
->ac
, res
);
3459 bool is_da
= glsl_sampler_type_is_array(type
) ||
3460 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3461 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3462 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3463 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3464 LLVMValueRef glc
= ctx
->ac
.i1false
;
3465 LLVMValueRef slc
= ctx
->ac
.i1false
;
3467 params
[0] = get_image_coords(ctx
, instr
);
3468 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3469 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3470 if (HAVE_LLVM
<= 0x0309) {
3471 params
[3] = ctx
->ac
.i1false
; /* r128 */
3476 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3483 ac_get_image_intr_name("llvm.amdgcn.image.load",
3484 ctx
->ac
.v4f32
, /* vdata */
3485 LLVMTypeOf(params
[0]), /* coords */
3486 LLVMTypeOf(params
[1]), /* rsrc */
3487 intrinsic_name
, sizeof(intrinsic_name
));
3489 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3490 params
, 7, AC_FUNC_ATTR_READONLY
);
3492 return ac_to_integer(&ctx
->ac
, res
);
3495 static void visit_image_store(struct ac_nir_context
*ctx
,
3496 nir_intrinsic_instr
*instr
)
3498 LLVMValueRef params
[8];
3499 char intrinsic_name
[64];
3500 const nir_variable
*var
= instr
->variables
[0]->var
;
3501 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3502 LLVMValueRef glc
= ctx
->ac
.i1false
;
3503 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3505 glc
= ctx
->ac
.i1true
;
3507 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3508 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3509 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3510 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3511 ctx
->ac
.i32_0
, ""); /* vindex */
3512 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3513 params
[4] = glc
; /* glc */
3514 params
[5] = ctx
->ac
.i1false
; /* slc */
3515 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3518 bool is_da
= glsl_sampler_type_is_array(type
) ||
3519 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3520 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3521 LLVMValueRef slc
= ctx
->ac
.i1false
;
3523 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3524 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3525 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3526 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3527 if (HAVE_LLVM
<= 0x0309) {
3528 params
[4] = ctx
->ac
.i1false
; /* r128 */
3533 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3540 ac_get_image_intr_name("llvm.amdgcn.image.store",
3541 LLVMTypeOf(params
[0]), /* vdata */
3542 LLVMTypeOf(params
[1]), /* coords */
3543 LLVMTypeOf(params
[2]), /* rsrc */
3544 intrinsic_name
, sizeof(intrinsic_name
));
3546 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3552 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3553 const nir_intrinsic_instr
*instr
)
3555 LLVMValueRef params
[7];
3556 int param_count
= 0;
3557 const nir_variable
*var
= instr
->variables
[0]->var
;
3559 const char *atomic_name
;
3560 char intrinsic_name
[41];
3561 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3562 MAYBE_UNUSED
int length
;
3564 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3566 switch (instr
->intrinsic
) {
3567 case nir_intrinsic_image_atomic_add
:
3568 atomic_name
= "add";
3570 case nir_intrinsic_image_atomic_min
:
3571 atomic_name
= is_unsigned
? "umin" : "smin";
3573 case nir_intrinsic_image_atomic_max
:
3574 atomic_name
= is_unsigned
? "umax" : "smax";
3576 case nir_intrinsic_image_atomic_and
:
3577 atomic_name
= "and";
3579 case nir_intrinsic_image_atomic_or
:
3582 case nir_intrinsic_image_atomic_xor
:
3583 atomic_name
= "xor";
3585 case nir_intrinsic_image_atomic_exchange
:
3586 atomic_name
= "swap";
3588 case nir_intrinsic_image_atomic_comp_swap
:
3589 atomic_name
= "cmpswap";
3595 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3596 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3597 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3599 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3600 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3602 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3603 ctx
->ac
.i32_0
, ""); /* vindex */
3604 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3605 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3607 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3608 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3610 char coords_type
[8];
3612 bool da
= glsl_sampler_type_is_array(type
) ||
3613 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3615 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3616 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3618 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3619 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3620 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3622 build_int_type_name(LLVMTypeOf(coords
),
3623 coords_type
, sizeof(coords_type
));
3625 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3626 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3629 assert(length
< sizeof(intrinsic_name
));
3630 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3633 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3634 const nir_intrinsic_instr
*instr
)
3637 const nir_variable
*var
= instr
->variables
[0]->var
;
3638 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3639 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3640 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3641 if(instr
->variables
[0]->deref
.child
)
3642 type
= instr
->variables
[0]->deref
.child
->type
;
3644 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3645 return get_buffer_size(ctx
,
3646 get_sampler_desc(ctx
, instr
->variables
[0],
3647 AC_DESC_BUFFER
, NULL
, true, false), true);
3649 struct ac_image_args args
= { 0 };
3653 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3654 args
.opcode
= ac_image_get_resinfo
;
3655 args
.addr
= ctx
->ac
.i32_0
;
3657 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3659 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3661 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3662 glsl_sampler_type_is_array(type
)) {
3663 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3664 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3665 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3666 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3668 if (ctx
->ac
.chip_class
>= GFX9
&&
3669 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3670 glsl_sampler_type_is_array(type
)) {
3671 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3672 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3679 #define NOOP_WAITCNT 0xf7f
3680 #define LGKM_CNT 0x07f
3681 #define VM_CNT 0xf70
3683 static void emit_membar(struct nir_to_llvm_context
*ctx
,
3684 const nir_intrinsic_instr
*instr
)
3686 unsigned waitcnt
= NOOP_WAITCNT
;
3688 switch (instr
->intrinsic
) {
3689 case nir_intrinsic_memory_barrier
:
3690 case nir_intrinsic_group_memory_barrier
:
3691 waitcnt
&= VM_CNT
& LGKM_CNT
;
3693 case nir_intrinsic_memory_barrier_atomic_counter
:
3694 case nir_intrinsic_memory_barrier_buffer
:
3695 case nir_intrinsic_memory_barrier_image
:
3698 case nir_intrinsic_memory_barrier_shared
:
3699 waitcnt
&= LGKM_CNT
;
3704 if (waitcnt
!= NOOP_WAITCNT
)
3705 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3708 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3710 /* SI only (thanks to a hw bug workaround):
3711 * The real barrier instruction isn’t needed, because an entire patch
3712 * always fits into a single wave.
3714 if (ctx
->options
->chip_class
== SI
&&
3715 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3716 ac_build_waitcnt(&ctx
->ac
, LGKM_CNT
& VM_CNT
);
3719 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3720 ctx
->ac
.voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3723 static void emit_discard_if(struct ac_nir_context
*ctx
,
3724 const nir_intrinsic_instr
*instr
)
3728 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3729 get_src(ctx
, instr
->src
[0]),
3731 ac_build_kill_if_false(&ctx
->ac
, cond
);
3735 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3737 LLVMValueRef result
;
3738 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3739 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3740 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3742 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3745 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3746 const nir_intrinsic_instr
*instr
)
3748 LLVMValueRef ptr
, result
;
3749 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3750 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3752 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3753 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3754 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3756 LLVMAtomicOrderingSequentiallyConsistent
,
3757 LLVMAtomicOrderingSequentiallyConsistent
,
3760 LLVMAtomicRMWBinOp op
;
3761 switch (instr
->intrinsic
) {
3762 case nir_intrinsic_var_atomic_add
:
3763 op
= LLVMAtomicRMWBinOpAdd
;
3765 case nir_intrinsic_var_atomic_umin
:
3766 op
= LLVMAtomicRMWBinOpUMin
;
3768 case nir_intrinsic_var_atomic_umax
:
3769 op
= LLVMAtomicRMWBinOpUMax
;
3771 case nir_intrinsic_var_atomic_imin
:
3772 op
= LLVMAtomicRMWBinOpMin
;
3774 case nir_intrinsic_var_atomic_imax
:
3775 op
= LLVMAtomicRMWBinOpMax
;
3777 case nir_intrinsic_var_atomic_and
:
3778 op
= LLVMAtomicRMWBinOpAnd
;
3780 case nir_intrinsic_var_atomic_or
:
3781 op
= LLVMAtomicRMWBinOpOr
;
3783 case nir_intrinsic_var_atomic_xor
:
3784 op
= LLVMAtomicRMWBinOpXor
;
3786 case nir_intrinsic_var_atomic_exchange
:
3787 op
= LLVMAtomicRMWBinOpXchg
;
3793 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3794 LLVMAtomicOrderingSequentiallyConsistent
,
3800 #define INTERP_CENTER 0
3801 #define INTERP_CENTROID 1
3802 #define INTERP_SAMPLE 2
3804 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3805 enum glsl_interp_mode interp
, unsigned location
)
3808 case INTERP_MODE_FLAT
:
3811 case INTERP_MODE_SMOOTH
:
3812 case INTERP_MODE_NONE
:
3813 if (location
== INTERP_CENTER
)
3814 return ctx
->persp_center
;
3815 else if (location
== INTERP_CENTROID
)
3816 return ctx
->persp_centroid
;
3817 else if (location
== INTERP_SAMPLE
)
3818 return ctx
->persp_sample
;
3820 case INTERP_MODE_NOPERSPECTIVE
:
3821 if (location
== INTERP_CENTER
)
3822 return ctx
->linear_center
;
3823 else if (location
== INTERP_CENTROID
)
3824 return ctx
->linear_centroid
;
3825 else if (location
== INTERP_SAMPLE
)
3826 return ctx
->linear_sample
;
3832 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3833 LLVMValueRef sample_id
)
3835 LLVMValueRef result
;
3836 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
3838 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3839 const_array(ctx
->ac
.v2f32
, 64), "");
3841 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3842 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
3847 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3849 LLVMValueRef values
[2];
3851 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3852 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3853 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3856 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3857 const nir_intrinsic_instr
*instr
)
3859 LLVMValueRef result
[4];
3860 LLVMValueRef interp_param
, attr_number
;
3863 LLVMValueRef src_c0
= NULL
;
3864 LLVMValueRef src_c1
= NULL
;
3865 LLVMValueRef src0
= NULL
;
3866 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3867 switch (instr
->intrinsic
) {
3868 case nir_intrinsic_interp_var_at_centroid
:
3869 location
= INTERP_CENTROID
;
3871 case nir_intrinsic_interp_var_at_sample
:
3872 case nir_intrinsic_interp_var_at_offset
:
3873 location
= INTERP_CENTER
;
3874 src0
= get_src(ctx
->nir
, instr
->src
[0]);
3880 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3881 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_0
, ""));
3882 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_1
, ""));
3883 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3884 LLVMValueRef sample_position
;
3885 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3887 /* fetch sample ID */
3888 sample_position
= load_sample_position(ctx
, src0
);
3890 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_0
, "");
3891 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3892 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_1
, "");
3893 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3895 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3896 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
3898 if (location
== INTERP_CENTER
) {
3899 LLVMValueRef ij_out
[2];
3900 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
3903 * take the I then J parameters, and the DDX/Y for it, and
3904 * calculate the IJ inputs for the interpolator.
3905 * temp1 = ddx * offset/sample.x + I;
3906 * interp_param.I = ddy * offset/sample.y + temp1;
3907 * temp1 = ddx * offset/sample.x + J;
3908 * interp_param.J = ddy * offset/sample.y + temp1;
3910 for (unsigned i
= 0; i
< 2; i
++) {
3911 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3912 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3913 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3914 ddxy_out
, ix_ll
, "");
3915 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3916 ddxy_out
, iy_ll
, "");
3917 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3918 interp_param
, ix_ll
, "");
3919 LLVMValueRef temp1
, temp2
;
3921 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3924 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3925 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3927 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3928 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3930 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3931 temp2
, ctx
->ac
.i32
, "");
3933 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3937 for (chan
= 0; chan
< 4; chan
++) {
3938 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3941 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3942 interp_param
, ctx
->ac
.v2f32
, "");
3943 LLVMValueRef i
= LLVMBuildExtractElement(
3944 ctx
->builder
, interp_param
, ctx
->ac
.i32_0
, "");
3945 LLVMValueRef j
= LLVMBuildExtractElement(
3946 ctx
->builder
, interp_param
, ctx
->ac
.i32_1
, "");
3948 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3949 llvm_chan
, attr_number
,
3950 ctx
->prim_mask
, i
, j
);
3952 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3953 LLVMConstInt(ctx
->ac
.i32
, 2, false),
3954 llvm_chan
, attr_number
,
3958 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3959 instr
->variables
[0]->var
->data
.location_frac
);
3963 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
3965 LLVMValueRef gs_next_vertex
;
3966 LLVMValueRef can_emit
;
3968 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3970 /* Write vertex attribute values to GSVS ring */
3971 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3972 ctx
->gs_next_vertex
,
3975 /* If this thread has already emitted the declared maximum number of
3976 * vertices, kill it: excessive vertex emissions are not supposed to
3977 * have any effect, and GS threads have no externally observable
3978 * effects other than emitting vertices.
3980 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3981 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
3982 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
3984 /* loop num outputs */
3986 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3987 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
3992 if (!(ctx
->output_mask
& (1ull << i
)))
3995 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3996 /* pack clip and cull into a single set of slots */
3997 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4001 for (unsigned j
= 0; j
< length
; j
++) {
4002 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
4004 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
4005 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
4006 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
4008 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
4010 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
4012 voffset
, ctx
->gs2vs_offset
, 0,
4018 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4020 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4022 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4026 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
4027 const nir_intrinsic_instr
*instr
)
4029 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4033 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
4034 const nir_intrinsic_instr
*instr
)
4036 LLVMValueRef coord
[4] = {
4043 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4044 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->ac
.f32_1
,
4045 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4047 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
4048 return LLVMBuildBitCast(ctx
->builder
, result
,
4049 get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
4052 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4053 nir_intrinsic_instr
*instr
)
4055 LLVMValueRef result
= NULL
;
4057 switch (instr
->intrinsic
) {
4058 case nir_intrinsic_load_work_group_id
: {
4059 LLVMValueRef values
[3];
4061 for (int i
= 0; i
< 3; i
++) {
4062 values
[i
] = ctx
->nctx
->workgroup_ids
[i
] ?
4063 ctx
->nctx
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
4066 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
4069 case nir_intrinsic_load_base_vertex
: {
4070 result
= ctx
->abi
->base_vertex
;
4073 case nir_intrinsic_load_vertex_id_zero_base
: {
4074 result
= ctx
->abi
->vertex_id
;
4077 case nir_intrinsic_load_local_invocation_id
: {
4078 result
= ctx
->nctx
->local_invocation_ids
;
4081 case nir_intrinsic_load_base_instance
:
4082 result
= ctx
->abi
->start_instance
;
4084 case nir_intrinsic_load_draw_id
:
4085 result
= ctx
->abi
->draw_id
;
4087 case nir_intrinsic_load_view_index
:
4088 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4090 case nir_intrinsic_load_invocation_id
:
4091 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4092 result
= unpack_param(&ctx
->ac
, ctx
->nctx
->tcs_rel_ids
, 8, 5);
4094 result
= ctx
->abi
->gs_invocation_id
;
4096 case nir_intrinsic_load_primitive_id
:
4097 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4099 ctx
->nctx
->shader_info
->gs
.uses_prim_id
= true;
4100 result
= ctx
->abi
->gs_prim_id
;
4101 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4102 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4103 result
= ctx
->nctx
->tcs_patch_id
;
4104 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4105 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4106 result
= ctx
->nctx
->tes_patch_id
;
4108 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4110 case nir_intrinsic_load_sample_id
:
4111 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4113 case nir_intrinsic_load_sample_pos
:
4114 result
= load_sample_pos(ctx
);
4116 case nir_intrinsic_load_sample_mask_in
:
4117 result
= ctx
->abi
->sample_coverage
;
4119 case nir_intrinsic_load_frag_coord
: {
4120 LLVMValueRef values
[4] = {
4121 ctx
->abi
->frag_pos
[0],
4122 ctx
->abi
->frag_pos
[1],
4123 ctx
->abi
->frag_pos
[2],
4124 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4126 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4129 case nir_intrinsic_load_front_face
:
4130 result
= ctx
->abi
->front_face
;
4132 case nir_intrinsic_load_instance_id
:
4133 result
= ctx
->abi
->instance_id
;
4135 case nir_intrinsic_load_num_work_groups
:
4136 result
= ctx
->nctx
->num_work_groups
;
4138 case nir_intrinsic_load_local_invocation_index
:
4139 result
= visit_load_local_invocation_index(ctx
->nctx
);
4141 case nir_intrinsic_load_push_constant
:
4142 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4144 case nir_intrinsic_vulkan_resource_index
:
4145 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4147 case nir_intrinsic_vulkan_resource_reindex
:
4148 result
= visit_vulkan_resource_reindex(ctx
->nctx
, instr
);
4150 case nir_intrinsic_store_ssbo
:
4151 visit_store_ssbo(ctx
, instr
);
4153 case nir_intrinsic_load_ssbo
:
4154 result
= visit_load_buffer(ctx
, instr
);
4156 case nir_intrinsic_ssbo_atomic_add
:
4157 case nir_intrinsic_ssbo_atomic_imin
:
4158 case nir_intrinsic_ssbo_atomic_umin
:
4159 case nir_intrinsic_ssbo_atomic_imax
:
4160 case nir_intrinsic_ssbo_atomic_umax
:
4161 case nir_intrinsic_ssbo_atomic_and
:
4162 case nir_intrinsic_ssbo_atomic_or
:
4163 case nir_intrinsic_ssbo_atomic_xor
:
4164 case nir_intrinsic_ssbo_atomic_exchange
:
4165 case nir_intrinsic_ssbo_atomic_comp_swap
:
4166 result
= visit_atomic_ssbo(ctx
, instr
);
4168 case nir_intrinsic_load_ubo
:
4169 result
= visit_load_ubo_buffer(ctx
, instr
);
4171 case nir_intrinsic_get_buffer_size
:
4172 result
= visit_get_buffer_size(ctx
, instr
);
4174 case nir_intrinsic_load_var
:
4175 result
= visit_load_var(ctx
, instr
);
4177 case nir_intrinsic_store_var
:
4178 visit_store_var(ctx
, instr
);
4180 case nir_intrinsic_image_load
:
4181 result
= visit_image_load(ctx
, instr
);
4183 case nir_intrinsic_image_store
:
4184 visit_image_store(ctx
, instr
);
4186 case nir_intrinsic_image_atomic_add
:
4187 case nir_intrinsic_image_atomic_min
:
4188 case nir_intrinsic_image_atomic_max
:
4189 case nir_intrinsic_image_atomic_and
:
4190 case nir_intrinsic_image_atomic_or
:
4191 case nir_intrinsic_image_atomic_xor
:
4192 case nir_intrinsic_image_atomic_exchange
:
4193 case nir_intrinsic_image_atomic_comp_swap
:
4194 result
= visit_image_atomic(ctx
, instr
);
4196 case nir_intrinsic_image_size
:
4197 result
= visit_image_size(ctx
, instr
);
4199 case nir_intrinsic_discard
:
4200 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4201 LLVMVoidTypeInContext(ctx
->ac
.context
),
4202 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4204 case nir_intrinsic_discard_if
:
4205 emit_discard_if(ctx
, instr
);
4207 case nir_intrinsic_memory_barrier
:
4208 case nir_intrinsic_group_memory_barrier
:
4209 case nir_intrinsic_memory_barrier_atomic_counter
:
4210 case nir_intrinsic_memory_barrier_buffer
:
4211 case nir_intrinsic_memory_barrier_image
:
4212 case nir_intrinsic_memory_barrier_shared
:
4213 emit_membar(ctx
->nctx
, instr
);
4215 case nir_intrinsic_barrier
:
4216 emit_barrier(ctx
->nctx
);
4218 case nir_intrinsic_var_atomic_add
:
4219 case nir_intrinsic_var_atomic_imin
:
4220 case nir_intrinsic_var_atomic_umin
:
4221 case nir_intrinsic_var_atomic_imax
:
4222 case nir_intrinsic_var_atomic_umax
:
4223 case nir_intrinsic_var_atomic_and
:
4224 case nir_intrinsic_var_atomic_or
:
4225 case nir_intrinsic_var_atomic_xor
:
4226 case nir_intrinsic_var_atomic_exchange
:
4227 case nir_intrinsic_var_atomic_comp_swap
:
4228 result
= visit_var_atomic(ctx
->nctx
, instr
);
4230 case nir_intrinsic_interp_var_at_centroid
:
4231 case nir_intrinsic_interp_var_at_sample
:
4232 case nir_intrinsic_interp_var_at_offset
:
4233 result
= visit_interp(ctx
->nctx
, instr
);
4235 case nir_intrinsic_emit_vertex
:
4236 assert(instr
->const_index
[0] == 0);
4237 ctx
->abi
->emit_vertex(ctx
->abi
, 0, ctx
->outputs
);
4239 case nir_intrinsic_end_primitive
:
4240 visit_end_primitive(ctx
->nctx
, instr
);
4242 case nir_intrinsic_load_tess_coord
:
4243 result
= visit_load_tess_coord(ctx
->nctx
, instr
);
4245 case nir_intrinsic_load_patch_vertices_in
:
4246 result
= LLVMConstInt(ctx
->ac
.i32
, ctx
->nctx
->options
->key
.tcs
.input_vertices
, false);
4249 fprintf(stderr
, "Unknown intrinsic: ");
4250 nir_print_instr(&instr
->instr
, stderr
);
4251 fprintf(stderr
, "\n");
4255 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4259 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4260 LLVMValueRef buffer_ptr
, bool write
)
4262 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4264 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4265 ctx
->shader_info
->fs
.writes_memory
= true;
4267 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4270 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef buffer_ptr
)
4272 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4274 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4277 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4278 unsigned descriptor_set
,
4279 unsigned base_index
,
4280 unsigned constant_index
,
4282 enum ac_descriptor_type desc_type
,
4283 bool image
, bool write
)
4285 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4286 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4287 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4288 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4289 unsigned offset
= binding
->offset
;
4290 unsigned stride
= binding
->size
;
4292 LLVMBuilderRef builder
= ctx
->builder
;
4295 assert(base_index
< layout
->binding_count
);
4297 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4298 ctx
->shader_info
->fs
.writes_memory
= true;
4300 switch (desc_type
) {
4302 type
= ctx
->ac
.v8i32
;
4306 type
= ctx
->ac
.v8i32
;
4310 case AC_DESC_SAMPLER
:
4311 type
= ctx
->ac
.v4i32
;
4312 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4317 case AC_DESC_BUFFER
:
4318 type
= ctx
->ac
.v4i32
;
4322 unreachable("invalid desc_type\n");
4325 offset
+= constant_index
* stride
;
4327 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4328 (!index
|| binding
->immutable_samplers_equal
)) {
4329 if (binding
->immutable_samplers_equal
)
4332 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4334 LLVMValueRef constants
[] = {
4335 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4336 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4337 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4338 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4340 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4343 assert(stride
% type_size
== 0);
4346 index
= ctx
->ac
.i32_0
;
4348 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4350 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4351 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4353 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4356 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4357 const nir_deref_var
*deref
,
4358 enum ac_descriptor_type desc_type
,
4359 const nir_tex_instr
*tex_instr
,
4360 bool image
, bool write
)
4362 LLVMValueRef index
= NULL
;
4363 unsigned constant_index
= 0;
4364 unsigned descriptor_set
;
4365 unsigned base_index
;
4368 assert(tex_instr
&& !image
);
4370 base_index
= tex_instr
->sampler_index
;
4372 const nir_deref
*tail
= &deref
->deref
;
4373 while (tail
->child
) {
4374 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4375 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4380 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4382 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4383 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4385 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4386 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4391 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4394 constant_index
+= child
->base_offset
* array_size
;
4396 tail
= &child
->deref
;
4398 descriptor_set
= deref
->var
->data
.descriptor_set
;
4399 base_index
= deref
->var
->data
.binding
;
4402 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4405 constant_index
, index
,
4406 desc_type
, image
, write
);
4409 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4410 struct ac_image_args
*args
,
4411 const nir_tex_instr
*instr
,
4413 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4414 LLVMValueRef
*param
, unsigned count
,
4417 unsigned is_rect
= 0;
4418 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4420 if (op
== nir_texop_lod
)
4422 /* Pad to power of two vector */
4423 while (count
< util_next_power_of_two(count
))
4424 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4427 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4429 args
->addr
= param
[0];
4431 args
->resource
= res_ptr
;
4432 args
->sampler
= samp_ptr
;
4434 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4435 args
->addr
= param
[0];
4439 args
->dmask
= dmask
;
4440 args
->unorm
= is_rect
;
4444 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4447 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4448 * filtering manually. The driver sets img7 to a mask clearing
4449 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4450 * s_and_b32 samp0, samp0, img7
4453 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4455 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4456 LLVMValueRef res
, LLVMValueRef samp
)
4458 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4459 LLVMValueRef img7
, samp0
;
4461 if (ctx
->ac
.chip_class
>= VI
)
4464 img7
= LLVMBuildExtractElement(builder
, res
,
4465 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4466 samp0
= LLVMBuildExtractElement(builder
, samp
,
4467 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4468 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4469 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4470 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4473 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4474 nir_tex_instr
*instr
,
4475 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4476 LLVMValueRef
*fmask_ptr
)
4478 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4479 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4481 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4484 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4486 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4487 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4488 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4490 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4491 instr
->op
== nir_texop_samples_identical
))
4492 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4495 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4498 coord
= ac_to_float(ctx
, coord
);
4499 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4500 coord
= ac_to_integer(ctx
, coord
);
4504 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4506 LLVMValueRef result
= NULL
;
4507 struct ac_image_args args
= { 0 };
4508 unsigned dmask
= 0xf;
4509 LLVMValueRef address
[16];
4510 LLVMValueRef coords
[5];
4511 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4512 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4513 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4514 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4515 LLVMValueRef derivs
[6];
4516 unsigned chan
, count
= 0;
4517 unsigned const_src
= 0, num_deriv_comp
= 0;
4518 bool lod_is_zero
= false;
4520 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4522 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4523 switch (instr
->src
[i
].src_type
) {
4524 case nir_tex_src_coord
:
4525 coord
= get_src(ctx
, instr
->src
[i
].src
);
4527 case nir_tex_src_projector
:
4529 case nir_tex_src_comparator
:
4530 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4532 case nir_tex_src_offset
:
4533 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4536 case nir_tex_src_bias
:
4537 bias
= get_src(ctx
, instr
->src
[i
].src
);
4539 case nir_tex_src_lod
: {
4540 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4542 if (val
&& val
->i32
[0] == 0)
4544 lod
= get_src(ctx
, instr
->src
[i
].src
);
4547 case nir_tex_src_ms_index
:
4548 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4550 case nir_tex_src_ms_mcs
:
4552 case nir_tex_src_ddx
:
4553 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4554 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4556 case nir_tex_src_ddy
:
4557 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4559 case nir_tex_src_texture_offset
:
4560 case nir_tex_src_sampler_offset
:
4561 case nir_tex_src_plane
:
4567 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4568 result
= get_buffer_size(ctx
, res_ptr
, true);
4572 if (instr
->op
== nir_texop_texture_samples
) {
4573 LLVMValueRef res
, samples
, is_msaa
;
4574 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4575 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4576 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4577 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4578 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4579 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4580 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4581 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4582 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4584 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4585 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4586 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4587 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4588 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4590 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4597 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4598 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4600 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4601 LLVMValueRef offset
[3], pack
;
4602 for (chan
= 0; chan
< 3; ++chan
)
4603 offset
[chan
] = ctx
->ac
.i32_0
;
4606 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4607 offset
[chan
] = llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4608 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4609 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4611 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4612 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4614 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4615 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4616 address
[count
++] = pack
;
4619 /* pack LOD bias value */
4620 if (instr
->op
== nir_texop_txb
&& bias
) {
4621 address
[count
++] = bias
;
4624 /* Pack depth comparison value */
4625 if (instr
->is_shadow
&& comparator
) {
4626 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4627 llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4629 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
4630 * so the depth comparison value isn't clamped for Z16 and
4631 * Z24 anymore. Do it manually here.
4633 * It's unnecessary if the original texture format was
4634 * Z32_FLOAT, but we don't know that here.
4636 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
4637 z
= ac_build_clamp(&ctx
->ac
, z
);
4639 address
[count
++] = z
;
4642 /* pack derivatives */
4644 int num_src_deriv_channels
, num_dest_deriv_channels
;
4645 switch (instr
->sampler_dim
) {
4646 case GLSL_SAMPLER_DIM_3D
:
4647 case GLSL_SAMPLER_DIM_CUBE
:
4649 num_src_deriv_channels
= 3;
4650 num_dest_deriv_channels
= 3;
4652 case GLSL_SAMPLER_DIM_2D
:
4654 num_src_deriv_channels
= 2;
4655 num_dest_deriv_channels
= 2;
4658 case GLSL_SAMPLER_DIM_1D
:
4659 num_src_deriv_channels
= 1;
4660 if (ctx
->ac
.chip_class
>= GFX9
) {
4661 num_dest_deriv_channels
= 2;
4664 num_dest_deriv_channels
= 1;
4670 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4671 derivs
[i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4672 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4674 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4675 derivs
[i
] = ctx
->ac
.f32_0
;
4676 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4680 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4681 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4682 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4683 if (instr
->coord_components
== 3)
4684 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4685 ac_prepare_cube_coords(&ctx
->ac
,
4686 instr
->op
== nir_texop_txd
, instr
->is_array
,
4687 instr
->op
== nir_texop_lod
, coords
, derivs
);
4693 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4694 address
[count
++] = derivs
[i
];
4697 /* Pack texture coordinates */
4699 address
[count
++] = coords
[0];
4700 if (instr
->coord_components
> 1) {
4701 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4702 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4704 address
[count
++] = coords
[1];
4706 if (instr
->coord_components
> 2) {
4707 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4708 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4709 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4710 instr
->op
!= nir_texop_txf
) {
4711 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4713 address
[count
++] = coords
[2];
4716 if (ctx
->ac
.chip_class
>= GFX9
) {
4717 LLVMValueRef filler
;
4718 if (instr
->op
== nir_texop_txf
)
4719 filler
= ctx
->ac
.i32_0
;
4721 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4723 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4724 /* No nir_texop_lod, because it does not take a slice
4725 * even with array textures. */
4726 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
4727 address
[count
] = address
[count
- 1];
4728 address
[count
- 1] = filler
;
4731 address
[count
++] = filler
;
4737 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4738 instr
->op
== nir_texop_txf
)) {
4739 address
[count
++] = lod
;
4740 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4741 address
[count
++] = sample_index
;
4742 } else if(instr
->op
== nir_texop_txs
) {
4745 address
[count
++] = lod
;
4747 address
[count
++] = ctx
->ac
.i32_0
;
4750 for (chan
= 0; chan
< count
; chan
++) {
4751 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4752 address
[chan
], ctx
->ac
.i32
, "");
4755 if (instr
->op
== nir_texop_samples_identical
) {
4756 LLVMValueRef txf_address
[4];
4757 struct ac_image_args txf_args
= { 0 };
4758 unsigned txf_count
= count
;
4759 memcpy(txf_address
, address
, sizeof(txf_address
));
4761 if (!instr
->is_array
)
4762 txf_address
[2] = ctx
->ac
.i32_0
;
4763 txf_address
[3] = ctx
->ac
.i32_0
;
4765 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4767 txf_address
, txf_count
, 0xf);
4769 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4771 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4772 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4776 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4777 instr
->op
!= nir_texop_txs
) {
4778 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4779 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4782 instr
->is_array
? address
[2] : NULL
,
4783 address
[sample_chan
],
4787 if (offsets
&& instr
->op
== nir_texop_txf
) {
4788 nir_const_value
*const_offset
=
4789 nir_src_as_const_value(instr
->src
[const_src
].src
);
4790 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4791 assert(const_offset
);
4792 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4793 if (num_offsets
> 2)
4794 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4795 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4796 if (num_offsets
> 1)
4797 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4798 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4799 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4800 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4804 /* TODO TG4 support */
4805 if (instr
->op
== nir_texop_tg4
) {
4806 if (instr
->is_shadow
)
4809 dmask
= 1 << instr
->component
;
4811 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4812 res_ptr
, samp_ptr
, address
, count
, dmask
);
4814 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4816 if (instr
->op
== nir_texop_query_levels
)
4817 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4818 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4819 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4820 instr
->op
!= nir_texop_tg4
)
4821 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4822 else if (instr
->op
== nir_texop_txs
&&
4823 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4825 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4826 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4827 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4828 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4829 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4830 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4831 instr
->op
== nir_texop_txs
&&
4832 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4834 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4835 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4836 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4838 } else if (instr
->dest
.ssa
.num_components
!= 4)
4839 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4843 assert(instr
->dest
.is_ssa
);
4844 result
= ac_to_integer(&ctx
->ac
, result
);
4845 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4850 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4852 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4853 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4855 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4856 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4859 static void visit_post_phi(struct ac_nir_context
*ctx
,
4860 nir_phi_instr
*instr
,
4861 LLVMValueRef llvm_phi
)
4863 nir_foreach_phi_src(src
, instr
) {
4864 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4865 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4867 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4871 static void phi_post_pass(struct ac_nir_context
*ctx
)
4873 struct hash_entry
*entry
;
4874 hash_table_foreach(ctx
->phis
, entry
) {
4875 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4876 (LLVMValueRef
)entry
->data
);
4881 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4882 const nir_ssa_undef_instr
*instr
)
4884 unsigned num_components
= instr
->def
.num_components
;
4887 if (num_components
== 1)
4888 undef
= LLVMGetUndef(ctx
->ac
.i32
);
4890 undef
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, num_components
));
4892 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4895 static void visit_jump(struct ac_nir_context
*ctx
,
4896 const nir_jump_instr
*instr
)
4898 switch (instr
->type
) {
4899 case nir_jump_break
:
4900 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
4901 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4903 case nir_jump_continue
:
4904 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4905 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4908 fprintf(stderr
, "Unknown NIR jump instr: ");
4909 nir_print_instr(&instr
->instr
, stderr
);
4910 fprintf(stderr
, "\n");
4915 static void visit_cf_list(struct ac_nir_context
*ctx
,
4916 struct exec_list
*list
);
4918 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4920 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4921 nir_foreach_instr(instr
, block
)
4923 switch (instr
->type
) {
4924 case nir_instr_type_alu
:
4925 visit_alu(ctx
, nir_instr_as_alu(instr
));
4927 case nir_instr_type_load_const
:
4928 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4930 case nir_instr_type_intrinsic
:
4931 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4933 case nir_instr_type_tex
:
4934 visit_tex(ctx
, nir_instr_as_tex(instr
));
4936 case nir_instr_type_phi
:
4937 visit_phi(ctx
, nir_instr_as_phi(instr
));
4939 case nir_instr_type_ssa_undef
:
4940 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4942 case nir_instr_type_jump
:
4943 visit_jump(ctx
, nir_instr_as_jump(instr
));
4946 fprintf(stderr
, "Unknown NIR instr type: ");
4947 nir_print_instr(instr
, stderr
);
4948 fprintf(stderr
, "\n");
4953 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4956 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4958 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4960 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4961 LLVMBasicBlockRef merge_block
=
4962 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4963 LLVMBasicBlockRef if_block
=
4964 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4965 LLVMBasicBlockRef else_block
= merge_block
;
4966 if (!exec_list_is_empty(&if_stmt
->else_list
))
4967 else_block
= LLVMAppendBasicBlockInContext(
4968 ctx
->ac
.context
, fn
, "");
4970 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
4972 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
4974 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
4975 visit_cf_list(ctx
, &if_stmt
->then_list
);
4976 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4977 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4979 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4980 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
4981 visit_cf_list(ctx
, &if_stmt
->else_list
);
4982 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4983 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4986 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
4989 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4991 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4992 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4993 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4995 ctx
->continue_block
=
4996 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4998 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5000 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5001 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
5002 visit_cf_list(ctx
, &loop
->body
);
5004 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5005 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5006 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
5008 ctx
->continue_block
= continue_parent
;
5009 ctx
->break_block
= break_parent
;
5012 static void visit_cf_list(struct ac_nir_context
*ctx
,
5013 struct exec_list
*list
)
5015 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5017 switch (node
->type
) {
5018 case nir_cf_node_block
:
5019 visit_block(ctx
, nir_cf_node_as_block(node
));
5022 case nir_cf_node_if
:
5023 visit_if(ctx
, nir_cf_node_as_if(node
));
5026 case nir_cf_node_loop
:
5027 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5037 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
5038 struct nir_variable
*variable
)
5040 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5041 LLVMValueRef t_offset
;
5042 LLVMValueRef t_list
;
5044 LLVMValueRef buffer_index
;
5045 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5046 int idx
= variable
->data
.location
;
5047 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5049 variable
->data
.driver_location
= idx
* 4;
5051 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
5052 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5053 ctx
->abi
.start_instance
, "");
5054 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
5055 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5057 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5058 ctx
->abi
.base_vertex
, "");
5060 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5061 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5063 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5065 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5070 for (unsigned chan
= 0; chan
< 4; chan
++) {
5071 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5072 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5073 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5074 input
, llvm_chan
, ""));
5079 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5081 LLVMValueRef interp_param
,
5082 LLVMValueRef prim_mask
,
5083 LLVMValueRef result
[4])
5085 LLVMValueRef attr_number
;
5088 bool interp
= interp_param
!= NULL
;
5090 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5092 /* fs.constant returns the param from the middle vertex, so it's not
5093 * really useful for flat shading. It's meant to be used for custom
5094 * interpolation (but the intrinsic can't fetch from the other two
5097 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5098 * to do the right thing. The only reason we use fs.constant is that
5099 * fs.interp cannot be used on integers, because they can be equal
5103 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5106 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5108 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5112 for (chan
= 0; chan
< 4; chan
++) {
5113 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5116 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5121 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5122 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5131 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5132 struct nir_variable
*variable
)
5134 int idx
= variable
->data
.location
;
5135 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5136 LLVMValueRef interp
;
5138 variable
->data
.driver_location
= idx
* 4;
5139 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5141 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5142 unsigned interp_type
;
5143 if (variable
->data
.sample
) {
5144 interp_type
= INTERP_SAMPLE
;
5145 ctx
->shader_info
->info
.ps
.force_persample
= true;
5146 } else if (variable
->data
.centroid
)
5147 interp_type
= INTERP_CENTROID
;
5149 interp_type
= INTERP_CENTER
;
5151 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5155 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5156 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5161 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5162 struct nir_shader
*nir
) {
5163 nir_foreach_variable(variable
, &nir
->inputs
)
5164 handle_vs_input_decl(ctx
, variable
);
5168 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5169 struct nir_shader
*nir
)
5171 if (!ctx
->options
->key
.fs
.multisample
)
5174 bool uses_center
= false;
5175 bool uses_centroid
= false;
5176 nir_foreach_variable(variable
, &nir
->inputs
) {
5177 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5178 variable
->data
.sample
)
5181 if (variable
->data
.centroid
)
5182 uses_centroid
= true;
5187 if (uses_center
&& uses_centroid
) {
5188 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5189 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5190 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5195 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5196 struct nir_shader
*nir
)
5198 prepare_interp_optimize(ctx
, nir
);
5200 nir_foreach_variable(variable
, &nir
->inputs
)
5201 handle_fs_input_decl(ctx
, variable
);
5205 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5206 ctx
->shader_info
->info
.needs_multiview_view_index
)
5207 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5209 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5210 LLVMValueRef interp_param
;
5211 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5213 if (!(ctx
->input_mask
& (1ull << i
)))
5216 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5217 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5218 interp_param
= *inputs
;
5219 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5223 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5225 } else if (i
== VARYING_SLOT_POS
) {
5226 for(int i
= 0; i
< 3; ++i
)
5227 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5229 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5230 ctx
->abi
.frag_pos
[3]);
5233 ctx
->shader_info
->fs
.num_interp
= index
;
5234 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5235 ctx
->shader_info
->fs
.has_pcoord
= true;
5236 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5237 ctx
->shader_info
->fs
.prim_id_input
= true;
5238 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5239 ctx
->shader_info
->fs
.layer_input
= true;
5240 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5242 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5243 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5247 ac_build_alloca(struct ac_llvm_context
*ac
,
5251 LLVMBuilderRef builder
= ac
->builder
;
5252 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5253 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5254 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5255 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5256 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5260 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5262 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5265 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5266 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5268 LLVMDisposeBuilder(first_builder
);
5273 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5277 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5278 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5283 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5284 struct nir_variable
*variable
,
5285 struct nir_shader
*shader
,
5286 gl_shader_stage stage
)
5288 int idx
= variable
->data
.location
+ variable
->data
.index
;
5289 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5290 uint64_t mask_attribs
;
5292 variable
->data
.driver_location
= idx
* 4;
5294 /* tess ctrl has it's own load/store paths for outputs */
5295 if (stage
== MESA_SHADER_TESS_CTRL
)
5298 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5299 if (stage
== MESA_SHADER_VERTEX
||
5300 stage
== MESA_SHADER_TESS_EVAL
||
5301 stage
== MESA_SHADER_GEOMETRY
) {
5302 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5303 int length
= shader
->info
.clip_distance_array_size
+
5304 shader
->info
.cull_distance_array_size
;
5305 if (stage
== MESA_SHADER_VERTEX
) {
5306 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5307 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5309 if (stage
== MESA_SHADER_TESS_EVAL
) {
5310 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5311 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5318 mask_attribs
= 1ull << idx
;
5322 ctx
->output_mask
|= mask_attribs
;
5326 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5327 struct nir_shader
*nir
,
5328 struct nir_variable
*variable
)
5330 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5331 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5333 /* tess ctrl has it's own load/store paths for outputs */
5334 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5337 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5338 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5339 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5340 int idx
= variable
->data
.location
+ variable
->data
.index
;
5341 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5342 int length
= nir
->info
.clip_distance_array_size
+
5343 nir
->info
.cull_distance_array_size
;
5352 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5353 for (unsigned chan
= 0; chan
< 4; chan
++) {
5354 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5355 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5361 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5362 enum glsl_base_type type
)
5366 case GLSL_TYPE_UINT
:
5367 case GLSL_TYPE_BOOL
:
5368 case GLSL_TYPE_SUBROUTINE
:
5370 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5372 case GLSL_TYPE_INT64
:
5373 case GLSL_TYPE_UINT64
:
5375 case GLSL_TYPE_DOUBLE
:
5378 unreachable("unknown GLSL type");
5383 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5384 const struct glsl_type
*type
)
5386 if (glsl_type_is_scalar(type
)) {
5387 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5390 if (glsl_type_is_vector(type
)) {
5391 return LLVMVectorType(
5392 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5393 glsl_get_vector_elements(type
));
5396 if (glsl_type_is_matrix(type
)) {
5397 return LLVMArrayType(
5398 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5399 glsl_get_matrix_columns(type
));
5402 if (glsl_type_is_array(type
)) {
5403 return LLVMArrayType(
5404 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5405 glsl_get_length(type
));
5408 assert(glsl_type_is_struct(type
));
5410 LLVMTypeRef member_types
[glsl_get_length(type
)];
5412 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5414 glsl_to_llvm_type(ctx
,
5415 glsl_get_struct_field(type
, i
));
5418 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5419 glsl_get_length(type
), false);
5423 setup_locals(struct ac_nir_context
*ctx
,
5424 struct nir_function
*func
)
5427 ctx
->num_locals
= 0;
5428 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5429 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5430 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5431 ctx
->num_locals
+= attrib_count
;
5433 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5437 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5438 for (j
= 0; j
< 4; j
++) {
5439 ctx
->locals
[i
* 4 + j
] =
5440 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5446 setup_shared(struct ac_nir_context
*ctx
,
5447 struct nir_shader
*nir
)
5449 nir_foreach_variable(variable
, &nir
->shared
) {
5450 LLVMValueRef shared
=
5451 LLVMAddGlobalInAddressSpace(
5452 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5453 variable
->name
? variable
->name
: "",
5455 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5460 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5462 v
= ac_to_float(ctx
, v
);
5463 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5464 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5468 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5469 LLVMValueRef src0
, LLVMValueRef src1
)
5471 LLVMValueRef const16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
5472 LLVMValueRef comp
[2];
5474 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5475 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5476 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5477 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5480 /* Initialize arguments for the shader export intrinsic */
5482 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5483 LLVMValueRef
*values
,
5485 struct ac_export_args
*args
)
5487 /* Default is 0xf. Adjusted below depending on the format. */
5488 args
->enabled_channels
= 0xf;
5490 /* Specify whether the EXEC mask represents the valid mask */
5491 args
->valid_mask
= 0;
5493 /* Specify whether this is the last export */
5496 /* Specify the target we are exporting */
5497 args
->target
= target
;
5499 args
->compr
= false;
5500 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
5501 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
5502 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
5503 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5508 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5509 LLVMValueRef val
[4];
5510 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5511 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5512 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5513 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5515 switch(col_format
) {
5516 case V_028714_SPI_SHADER_ZERO
:
5517 args
->enabled_channels
= 0; /* writemask */
5518 args
->target
= V_008DFC_SQ_EXP_NULL
;
5521 case V_028714_SPI_SHADER_32_R
:
5522 args
->enabled_channels
= 1;
5523 args
->out
[0] = values
[0];
5526 case V_028714_SPI_SHADER_32_GR
:
5527 args
->enabled_channels
= 0x3;
5528 args
->out
[0] = values
[0];
5529 args
->out
[1] = values
[1];
5532 case V_028714_SPI_SHADER_32_AR
:
5533 args
->enabled_channels
= 0x9;
5534 args
->out
[0] = values
[0];
5535 args
->out
[3] = values
[3];
5538 case V_028714_SPI_SHADER_FP16_ABGR
:
5541 for (unsigned chan
= 0; chan
< 2; chan
++) {
5542 LLVMValueRef pack_args
[2] = {
5544 values
[2 * chan
+ 1]
5546 LLVMValueRef packed
;
5548 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5549 args
->out
[chan
] = packed
;
5553 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5554 for (unsigned chan
= 0; chan
< 4; chan
++) {
5555 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5556 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5557 LLVMConstReal(ctx
->ac
.f32
, 65535), "");
5558 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5559 LLVMConstReal(ctx
->ac
.f32
, 0.5), "");
5560 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5565 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5566 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5569 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5570 for (unsigned chan
= 0; chan
< 4; chan
++) {
5571 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5572 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5573 LLVMConstReal(ctx
->ac
.f32
, 32767), "");
5575 /* If positive, add 0.5, else add -0.5. */
5576 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5577 LLVMBuildSelect(ctx
->builder
,
5578 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5579 val
[chan
], ctx
->ac
.f32_0
, ""),
5580 LLVMConstReal(ctx
->ac
.f32
, 0.5),
5581 LLVMConstReal(ctx
->ac
.f32
, -0.5), ""), "");
5582 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->ac
.i32
, "");
5586 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5587 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5590 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5591 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5592 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5593 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->ac
.i32
, 3, 0);
5595 for (unsigned chan
= 0; chan
< 4; chan
++) {
5596 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5597 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5601 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5602 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5606 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5607 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5608 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5609 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5610 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5611 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->ac
.i32_1
;
5612 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->ac
.i32
, -2, 0);
5615 for (unsigned chan
= 0; chan
< 4; chan
++) {
5616 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5617 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5618 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5622 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5623 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5628 case V_028714_SPI_SHADER_32_ABGR
:
5629 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5633 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5635 for (unsigned i
= 0; i
< 4; ++i
)
5636 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5640 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5641 bool export_prim_id
,
5642 struct ac_vs_output_info
*outinfo
)
5644 uint32_t param_count
= 0;
5646 unsigned pos_idx
, num_pos_exports
= 0;
5647 struct ac_export_args args
, pos_args
[4] = {};
5648 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5651 if (ctx
->options
->key
.has_multiview_view_index
) {
5652 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5654 for(unsigned i
= 0; i
< 4; ++i
)
5655 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5656 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5659 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5660 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5663 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5664 sizeof(outinfo
->vs_output_param_offset
));
5666 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5667 LLVMValueRef slots
[8];
5670 if (outinfo
->cull_dist_mask
)
5671 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5673 i
= VARYING_SLOT_CLIP_DIST0
;
5674 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5675 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5676 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5678 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5679 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
5681 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5682 target
= V_008DFC_SQ_EXP_POS
+ 3;
5683 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5684 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5685 &args
, sizeof(args
));
5688 target
= V_008DFC_SQ_EXP_POS
+ 2;
5689 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5690 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5691 &args
, sizeof(args
));
5695 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
5696 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5697 for (unsigned j
= 0; j
< 4; j
++)
5698 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5699 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5701 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5703 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5704 outinfo
->writes_pointsize
= true;
5705 psize_value
= LLVMBuildLoad(ctx
->builder
,
5706 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5709 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5710 outinfo
->writes_layer
= true;
5711 layer_value
= LLVMBuildLoad(ctx
->builder
,
5712 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5715 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5716 outinfo
->writes_viewport_index
= true;
5717 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5718 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5721 if (outinfo
->writes_pointsize
||
5722 outinfo
->writes_layer
||
5723 outinfo
->writes_viewport_index
) {
5724 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5725 (outinfo
->writes_layer
== true ? 4 : 0));
5726 pos_args
[1].valid_mask
= 0;
5727 pos_args
[1].done
= 0;
5728 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5729 pos_args
[1].compr
= 0;
5730 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
5731 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
5732 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
5733 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
5735 if (outinfo
->writes_pointsize
== true)
5736 pos_args
[1].out
[0] = psize_value
;
5737 if (outinfo
->writes_layer
== true)
5738 pos_args
[1].out
[2] = layer_value
;
5739 if (outinfo
->writes_viewport_index
== true) {
5740 if (ctx
->options
->chip_class
>= GFX9
) {
5741 /* GFX9 has the layer in out.z[10:0] and the viewport
5742 * index in out.z[19:16].
5744 LLVMValueRef v
= viewport_index_value
;
5745 v
= ac_to_integer(&ctx
->ac
, v
);
5746 v
= LLVMBuildShl(ctx
->builder
, v
,
5747 LLVMConstInt(ctx
->ac
.i32
, 16, false),
5749 v
= LLVMBuildOr(ctx
->builder
, v
,
5750 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5752 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5753 pos_args
[1].enabled_channels
|= 1 << 2;
5755 pos_args
[1].out
[3] = viewport_index_value
;
5756 pos_args
[1].enabled_channels
|= 1 << 3;
5760 for (i
= 0; i
< 4; i
++) {
5761 if (pos_args
[i
].out
[0])
5766 for (i
= 0; i
< 4; i
++) {
5767 if (!pos_args
[i
].out
[0])
5770 /* Specify the target we are exporting */
5771 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5772 if (pos_idx
== num_pos_exports
)
5773 pos_args
[i
].done
= 1;
5774 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5777 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5778 LLVMValueRef values
[4];
5779 if (!(ctx
->output_mask
& (1ull << i
)))
5782 for (unsigned j
= 0; j
< 4; j
++)
5783 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5784 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5786 if (i
== VARYING_SLOT_LAYER
) {
5787 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5788 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5790 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5791 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5792 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5794 } else if (i
>= VARYING_SLOT_VAR0
) {
5795 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5796 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5797 outinfo
->vs_output_param_offset
[i
] = param_count
;
5802 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5804 if (target
>= V_008DFC_SQ_EXP_POS
&&
5805 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5806 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5807 &args
, sizeof(args
));
5809 ac_build_export(&ctx
->ac
, &args
);
5813 if (export_prim_id
) {
5814 LLVMValueRef values
[4];
5815 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5816 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5819 values
[0] = ctx
->vs_prim_id
;
5820 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5821 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5822 for (unsigned j
= 1; j
< 4; j
++)
5823 values
[j
] = ctx
->ac
.f32_0
;
5824 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5825 ac_build_export(&ctx
->ac
, &args
);
5826 outinfo
->export_prim_id
= true;
5829 outinfo
->pos_exports
= num_pos_exports
;
5830 outinfo
->param_exports
= param_count
;
5834 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5835 struct ac_es_output_info
*outinfo
)
5838 uint64_t max_output_written
= 0;
5839 LLVMValueRef lds_base
= NULL
;
5841 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5845 if (!(ctx
->output_mask
& (1ull << i
)))
5848 if (i
== VARYING_SLOT_CLIP_DIST0
)
5849 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5851 param_index
= shader_io_get_unique_index(i
);
5853 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5856 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5858 if (ctx
->ac
.chip_class
>= GFX9
) {
5859 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
5860 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
5861 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
5862 LLVMConstInt(ctx
->ac
.i32
, 24, false),
5863 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
5864 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
5865 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
5866 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
5867 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
5868 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
5871 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5872 LLVMValueRef dw_addr
;
5873 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5877 if (!(ctx
->output_mask
& (1ull << i
)))
5880 if (i
== VARYING_SLOT_CLIP_DIST0
)
5881 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5883 param_index
= shader_io_get_unique_index(i
);
5886 dw_addr
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5887 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
5890 for (j
= 0; j
< length
; j
++) {
5891 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5892 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
5894 if (ctx
->ac
.chip_class
>= GFX9
) {
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
, "");
5899 ac_build_buffer_store_dword(&ctx
->ac
,
5902 NULL
, ctx
->es2gs_offset
,
5903 (4 * param_index
+ j
) * 4,
5911 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5913 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5914 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
5915 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5916 vertex_dw_stride
, "");
5918 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5919 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5922 if (!(ctx
->output_mask
& (1ull << i
)))
5925 if (i
== VARYING_SLOT_CLIP_DIST0
)
5926 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5927 int param
= shader_io_get_unique_index(i
);
5928 mark_tess_output(ctx
, false, param
);
5930 mark_tess_output(ctx
, false, param
+ 1);
5931 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5932 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
5934 for (unsigned j
= 0; j
< length
; j
++) {
5935 ac_lds_store(&ctx
->ac
, dw_addr
,
5936 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5937 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
5942 struct ac_build_if_state
5944 struct nir_to_llvm_context
*ctx
;
5945 LLVMValueRef condition
;
5946 LLVMBasicBlockRef entry_block
;
5947 LLVMBasicBlockRef true_block
;
5948 LLVMBasicBlockRef false_block
;
5949 LLVMBasicBlockRef merge_block
;
5952 static LLVMBasicBlockRef
5953 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5955 LLVMBasicBlockRef current_block
;
5956 LLVMBasicBlockRef next_block
;
5957 LLVMBasicBlockRef new_block
;
5959 /* get current basic block */
5960 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5962 /* chqeck if there's another block after this one */
5963 next_block
= LLVMGetNextBasicBlock(current_block
);
5965 /* insert the new block before the next block */
5966 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5969 /* append new block after current block */
5970 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5971 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5977 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5978 struct nir_to_llvm_context
*ctx
,
5979 LLVMValueRef condition
)
5981 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5983 memset(ifthen
, 0, sizeof *ifthen
);
5985 ifthen
->condition
= condition
;
5986 ifthen
->entry_block
= block
;
5988 /* create endif/merge basic block for the phi functions */
5989 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5991 /* create/insert true_block before merge_block */
5992 ifthen
->true_block
=
5993 LLVMInsertBasicBlockInContext(ctx
->context
,
5994 ifthen
->merge_block
,
5997 /* successive code goes into the true block */
5998 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
6002 * End a conditional.
6005 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
6007 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
6009 /* Insert branch to the merge block from current block */
6010 LLVMBuildBr(builder
, ifthen
->merge_block
);
6013 * Now patch in the various branch instructions.
6016 /* Insert the conditional branch instruction at the end of entry_block */
6017 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
6018 if (ifthen
->false_block
) {
6019 /* we have an else clause */
6020 LLVMBuildCondBr(builder
, ifthen
->condition
,
6021 ifthen
->true_block
, ifthen
->false_block
);
6024 /* no else clause */
6025 LLVMBuildCondBr(builder
, ifthen
->condition
,
6026 ifthen
->true_block
, ifthen
->merge_block
);
6029 /* Resume building code at end of the ifthen->merge_block */
6030 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
6034 write_tess_factors(struct nir_to_llvm_context
*ctx
)
6036 unsigned stride
, outer_comps
, inner_comps
;
6037 struct ac_build_if_state if_ctx
, inner_if_ctx
;
6038 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 8, 5);
6039 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
6040 unsigned tess_inner_index
, tess_outer_index
;
6041 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6042 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6046 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6066 ac_nir_build_if(&if_ctx
, ctx
,
6067 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6068 invocation_id
, ctx
->ac
.i32_0
, ""));
6070 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6071 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6073 mark_tess_output(ctx
, true, tess_inner_index
);
6074 mark_tess_output(ctx
, true, tess_outer_index
);
6075 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6076 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6077 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6078 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6079 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6081 for (i
= 0; i
< 4; i
++) {
6082 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6083 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6087 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6088 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6089 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6091 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6093 for (i
= 0; i
< outer_comps
; i
++) {
6095 ac_lds_load(&ctx
->ac
, lds_outer
);
6096 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6099 for (i
= 0; i
< inner_comps
; i
++) {
6100 inner
[i
] = out
[outer_comps
+i
] =
6101 ac_lds_load(&ctx
->ac
, lds_inner
);
6102 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6107 /* Convert the outputs to vectors for stores. */
6108 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6112 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6115 buffer
= ctx
->hs_ring_tess_factor
;
6116 tf_base
= ctx
->tess_factor_offset
;
6117 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6118 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6119 unsigned tf_offset
= 0;
6121 if (ctx
->options
->chip_class
<= VI
) {
6122 ac_nir_build_if(&inner_if_ctx
, ctx
,
6123 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6124 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6126 /* Store the dynamic HS control word. */
6127 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6128 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6129 1, ctx
->ac
.i32_0
, tf_base
,
6130 0, 1, 0, true, false);
6133 ac_nir_build_endif(&inner_if_ctx
);
6136 /* Store the tessellation factors. */
6137 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6138 MIN2(stride
, 4), byteoffset
, tf_base
,
6139 tf_offset
, 1, 0, true, false);
6141 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6142 stride
- 4, byteoffset
, tf_base
,
6143 16 + tf_offset
, 1, 0, true, false);
6145 //store to offchip for TES to read - only if TES reads them
6146 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6147 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6148 LLVMValueRef tf_inner_offset
;
6149 unsigned param_outer
, param_inner
;
6151 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6152 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6153 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6155 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6156 util_next_power_of_two(outer_comps
));
6158 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6159 outer_comps
, tf_outer_offset
,
6160 ctx
->oc_lds
, 0, 1, 0, true, false);
6162 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6163 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6164 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6166 inner_vec
= inner_comps
== 1 ? inner
[0] :
6167 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6168 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6169 inner_comps
, tf_inner_offset
,
6170 ctx
->oc_lds
, 0, 1, 0, true, false);
6173 ac_nir_build_endif(&if_ctx
);
6177 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6179 write_tess_factors(ctx
);
6183 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6184 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6185 struct ac_export_args
*args
)
6188 si_llvm_init_export_args(ctx
, color
, param
,
6192 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6193 args
->done
= 1; /* DONE bit */
6194 } else if (!args
->enabled_channels
)
6195 return false; /* unnecessary NULL export */
6201 radv_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6202 LLVMValueRef depth
, LLVMValueRef stencil
,
6203 LLVMValueRef samplemask
)
6205 struct ac_export_args args
;
6207 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
6209 ac_build_export(&ctx
->ac
, &args
);
6213 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6216 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6217 struct ac_export_args color_args
[8];
6219 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6220 LLVMValueRef values
[4];
6222 if (!(ctx
->output_mask
& (1ull << i
)))
6225 if (i
== FRAG_RESULT_DEPTH
) {
6226 ctx
->shader_info
->fs
.writes_z
= true;
6227 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6228 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6229 } else if (i
== FRAG_RESULT_STENCIL
) {
6230 ctx
->shader_info
->fs
.writes_stencil
= true;
6231 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6232 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6233 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6234 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6235 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6236 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6239 for (unsigned j
= 0; j
< 4; j
++)
6240 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6241 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6243 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6244 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6246 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6252 for (unsigned i
= 0; i
< index
; i
++)
6253 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6254 if (depth
|| stencil
|| samplemask
)
6255 radv_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6257 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6258 ac_build_export(&ctx
->ac
, &color_args
[0]);
6261 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
6265 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6267 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6271 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6272 LLVMValueRef
*addrs
)
6274 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6276 switch (ctx
->stage
) {
6277 case MESA_SHADER_VERTEX
:
6278 if (ctx
->options
->key
.vs
.as_ls
)
6279 handle_ls_outputs_post(ctx
);
6280 else if (ctx
->options
->key
.vs
.as_es
)
6281 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6283 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6284 &ctx
->shader_info
->vs
.outinfo
);
6286 case MESA_SHADER_FRAGMENT
:
6287 handle_fs_outputs_post(ctx
);
6289 case MESA_SHADER_GEOMETRY
:
6290 emit_gs_epilogue(ctx
);
6292 case MESA_SHADER_TESS_CTRL
:
6293 handle_tcs_outputs_post(ctx
);
6295 case MESA_SHADER_TESS_EVAL
:
6296 if (ctx
->options
->key
.tes
.as_es
)
6297 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6299 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6300 &ctx
->shader_info
->tes
.outinfo
);
6307 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6309 LLVMPassManagerRef passmgr
;
6310 /* Create the pass manager */
6311 passmgr
= LLVMCreateFunctionPassManagerForModule(
6314 /* This pass should eliminate all the load and store instructions */
6315 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6317 /* Add some optimization passes */
6318 LLVMAddScalarReplAggregatesPass(passmgr
);
6319 LLVMAddLICMPass(passmgr
);
6320 LLVMAddAggressiveDCEPass(passmgr
);
6321 LLVMAddCFGSimplificationPass(passmgr
);
6322 LLVMAddInstructionCombiningPass(passmgr
);
6325 LLVMInitializeFunctionPassManager(passmgr
);
6326 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6327 LLVMFinalizeFunctionPassManager(passmgr
);
6329 LLVMDisposeBuilder(ctx
->builder
);
6330 LLVMDisposePassManager(passmgr
);
6334 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6336 struct ac_vs_output_info
*outinfo
;
6338 switch (ctx
->stage
) {
6339 case MESA_SHADER_FRAGMENT
:
6340 case MESA_SHADER_COMPUTE
:
6341 case MESA_SHADER_TESS_CTRL
:
6342 case MESA_SHADER_GEOMETRY
:
6344 case MESA_SHADER_VERTEX
:
6345 if (ctx
->options
->key
.vs
.as_ls
||
6346 ctx
->options
->key
.vs
.as_es
)
6348 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6350 case MESA_SHADER_TESS_EVAL
:
6351 if (ctx
->options
->key
.vs
.as_es
)
6353 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6356 unreachable("Unhandled shader type");
6359 ac_optimize_vs_outputs(&ctx
->ac
,
6361 outinfo
->vs_output_param_offset
,
6363 &outinfo
->param_exports
);
6367 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6369 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6370 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6371 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6374 if (ctx
->is_gs_copy_shader
) {
6375 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6377 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6379 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6380 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6382 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.v4i32
, "");
6384 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6385 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6386 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6387 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6390 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6391 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6392 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6393 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6398 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6399 const struct nir_shader
*nir
)
6401 switch (nir
->info
.stage
) {
6402 case MESA_SHADER_TESS_CTRL
:
6403 return chip_class
>= CIK
? 128 : 64;
6404 case MESA_SHADER_GEOMETRY
:
6405 return chip_class
>= GFX9
? 128 : 64;
6406 case MESA_SHADER_COMPUTE
:
6412 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6413 nir
->info
.cs
.local_size
[1] *
6414 nir
->info
.cs
.local_size
[2];
6415 return max_workgroup_size
;
6418 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6419 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6421 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6422 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6423 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6424 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6426 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6427 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6428 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_rel_ids
, ctx
->rel_auto_id
, "");
6429 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6432 static void prepare_gs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6434 for(int i
= 5; i
>= 0; --i
) {
6435 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6436 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6437 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6440 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6441 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6442 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6445 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6446 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6448 struct ac_nir_context ctx
= {};
6449 struct nir_function
*func
;
6458 ctx
.stage
= nir
->info
.stage
;
6460 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6462 nir_foreach_variable(variable
, &nir
->outputs
)
6463 handle_shader_output_decl(&ctx
, nir
, variable
);
6465 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6466 _mesa_key_pointer_equal
);
6467 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6468 _mesa_key_pointer_equal
);
6469 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6470 _mesa_key_pointer_equal
);
6472 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6474 setup_locals(&ctx
, func
);
6476 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6477 setup_shared(&ctx
, nir
);
6479 visit_cf_list(&ctx
, &func
->impl
->body
);
6480 phi_post_pass(&ctx
);
6482 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6486 ralloc_free(ctx
.defs
);
6487 ralloc_free(ctx
.phis
);
6488 ralloc_free(ctx
.vars
);
6495 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6496 struct nir_shader
*const *shaders
,
6498 struct ac_shader_variant_info
*shader_info
,
6499 const struct ac_nir_compiler_options
*options
)
6501 struct nir_to_llvm_context ctx
= {0};
6503 ctx
.options
= options
;
6504 ctx
.shader_info
= shader_info
;
6505 ctx
.context
= LLVMContextCreate();
6506 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6508 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
6510 ctx
.ac
.module
= ctx
.module
;
6511 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6513 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6514 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6515 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6516 LLVMDisposeTargetData(data_layout
);
6517 LLVMDisposeMessage(data_layout_str
);
6519 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6520 ctx
.ac
.builder
= ctx
.builder
;
6522 memset(shader_info
, 0, sizeof(*shader_info
));
6524 for(int i
= 0; i
< shader_count
; ++i
)
6525 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6527 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6528 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6529 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6530 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6532 ctx
.max_workgroup_size
= 0;
6533 for (int i
= 0; i
< shader_count
; ++i
) {
6534 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6535 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6539 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6540 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6542 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6543 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6544 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
6545 ctx
.abi
.load_ubo
= radv_load_ubo
;
6546 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6547 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6548 ctx
.abi
.clamp_shadow_reference
= false;
6550 if (shader_count
>= 2)
6551 ac_init_exec_full_mask(&ctx
.ac
);
6553 if (ctx
.ac
.chip_class
== GFX9
&&
6554 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6555 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6557 for(int i
= 0; i
< shader_count
; ++i
) {
6558 ctx
.stage
= shaders
[i
]->info
.stage
;
6559 ctx
.output_mask
= 0;
6560 ctx
.tess_outputs_written
= 0;
6561 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6562 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6564 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6565 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6566 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6567 ctx
.abi
.load_inputs
= load_gs_input
;
6568 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6569 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
6570 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
6571 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6572 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6573 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6574 if (shader_info
->info
.vs
.needs_instance_id
) {
6575 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6576 MAX2(3, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6578 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6579 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6585 ac_setup_rings(&ctx
);
6587 LLVMBasicBlockRef merge_block
;
6588 if (shader_count
>= 2) {
6589 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6590 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6591 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6593 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6594 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6595 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6596 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6597 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6598 thread_id
, count
, "");
6599 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6601 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6604 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6605 handle_fs_inputs(&ctx
, shaders
[i
]);
6606 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6607 handle_vs_inputs(&ctx
, shaders
[i
]);
6608 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6609 prepare_gs_input_vgprs(&ctx
);
6611 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6612 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6614 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6616 if (shader_count
>= 2) {
6617 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6618 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6621 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6622 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6623 shaders
[i
]->info
.cull_distance_array_size
> 4;
6624 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6625 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6626 shaders
[i
]->info
.gs
.vertices_out
;
6627 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6628 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6629 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6630 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6631 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6635 LLVMBuildRetVoid(ctx
.builder
);
6637 ac_llvm_finalize_module(&ctx
);
6639 if (shader_count
== 1)
6640 ac_nir_eliminate_const_vs_outputs(&ctx
);
6645 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6647 unsigned *retval
= (unsigned *)context
;
6648 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6649 char *description
= LLVMGetDiagInfoDescription(di
);
6651 if (severity
== LLVMDSError
) {
6653 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6657 LLVMDisposeMessage(description
);
6660 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6661 struct ac_shader_binary
*binary
,
6662 LLVMTargetMachineRef tm
)
6664 unsigned retval
= 0;
6666 LLVMContextRef llvm_ctx
;
6667 LLVMMemoryBufferRef out_buffer
;
6668 unsigned buffer_size
;
6669 const char *buffer_data
;
6672 /* Setup Diagnostic Handler*/
6673 llvm_ctx
= LLVMGetModuleContext(M
);
6675 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6679 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6682 /* Process Errors/Warnings */
6684 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6690 /* Extract Shader Code*/
6691 buffer_size
= LLVMGetBufferSize(out_buffer
);
6692 buffer_data
= LLVMGetBufferStart(out_buffer
);
6694 ac_elf_read(buffer_data
, buffer_size
, binary
);
6697 LLVMDisposeMemoryBuffer(out_buffer
);
6703 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6704 LLVMModuleRef llvm_module
,
6705 struct ac_shader_binary
*binary
,
6706 struct ac_shader_config
*config
,
6707 struct ac_shader_variant_info
*shader_info
,
6708 gl_shader_stage stage
,
6709 bool dump_shader
, bool supports_spill
)
6712 ac_dump_module(llvm_module
);
6714 memset(binary
, 0, sizeof(*binary
));
6715 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6717 fprintf(stderr
, "compile failed\n");
6721 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6723 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6725 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6726 LLVMDisposeModule(llvm_module
);
6727 LLVMContextDispose(ctx
);
6729 if (stage
== MESA_SHADER_FRAGMENT
) {
6730 shader_info
->num_input_vgprs
= 0;
6731 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6732 shader_info
->num_input_vgprs
+= 2;
6733 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6734 shader_info
->num_input_vgprs
+= 2;
6735 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6736 shader_info
->num_input_vgprs
+= 2;
6737 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6738 shader_info
->num_input_vgprs
+= 3;
6739 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6740 shader_info
->num_input_vgprs
+= 2;
6741 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6742 shader_info
->num_input_vgprs
+= 2;
6743 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6744 shader_info
->num_input_vgprs
+= 2;
6745 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6746 shader_info
->num_input_vgprs
+= 1;
6747 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6748 shader_info
->num_input_vgprs
+= 1;
6749 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6750 shader_info
->num_input_vgprs
+= 1;
6751 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6752 shader_info
->num_input_vgprs
+= 1;
6753 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6754 shader_info
->num_input_vgprs
+= 1;
6755 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6756 shader_info
->num_input_vgprs
+= 1;
6757 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6758 shader_info
->num_input_vgprs
+= 1;
6759 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6760 shader_info
->num_input_vgprs
+= 1;
6761 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6762 shader_info
->num_input_vgprs
+= 1;
6764 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6766 /* +3 for scratch wave offset and VCC */
6767 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6768 shader_info
->num_input_sgprs
+ 3);
6772 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
6774 switch (nir
->info
.stage
) {
6775 case MESA_SHADER_COMPUTE
:
6776 for (int i
= 0; i
< 3; ++i
)
6777 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6779 case MESA_SHADER_FRAGMENT
:
6780 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6782 case MESA_SHADER_GEOMETRY
:
6783 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6784 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6785 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6786 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6788 case MESA_SHADER_TESS_EVAL
:
6789 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6790 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6791 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6792 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6793 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6795 case MESA_SHADER_TESS_CTRL
:
6796 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6798 case MESA_SHADER_VERTEX
:
6799 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6800 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6801 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6802 if (options
->key
.vs
.as_ls
)
6803 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6810 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6811 struct ac_shader_binary
*binary
,
6812 struct ac_shader_config
*config
,
6813 struct ac_shader_variant_info
*shader_info
,
6814 struct nir_shader
*const *nir
,
6816 const struct ac_nir_compiler_options
*options
,
6820 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
6823 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
6824 for (int i
= 0; i
< nir_count
; ++i
)
6825 ac_fill_shader_info(shader_info
, nir
[i
], options
);
6829 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6831 LLVMValueRef args
[9];
6832 args
[0] = ctx
->gsvs_ring
;
6833 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
6834 args
[3] = ctx
->ac
.i32_0
;
6835 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
6836 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
6837 args
[6] = ctx
->ac
.i32_1
; /* GLC */
6838 args
[7] = ctx
->ac
.i32_1
; /* SLC */
6839 args
[8] = ctx
->ac
.i32_0
; /* TFE */
6843 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6847 if (!(ctx
->output_mask
& (1ull << i
)))
6850 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6851 /* unpack clip and cull from a single set of slots */
6852 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6857 for (unsigned j
= 0; j
< length
; j
++) {
6859 args
[2] = LLVMConstInt(ctx
->ac
.i32
,
6861 ctx
->gs_max_out_vertices
* 16 * 4, false);
6863 value
= ac_build_intrinsic(&ctx
->ac
,
6864 "llvm.SI.buffer.load.dword.i32.i32",
6865 ctx
->ac
.i32
, args
, 9,
6866 AC_FUNC_ATTR_READONLY
|
6867 AC_FUNC_ATTR_LEGACY
);
6869 LLVMBuildStore(ctx
->builder
,
6870 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6874 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
6877 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6878 struct nir_shader
*geom_shader
,
6879 struct ac_shader_binary
*binary
,
6880 struct ac_shader_config
*config
,
6881 struct ac_shader_variant_info
*shader_info
,
6882 const struct ac_nir_compiler_options
*options
,
6885 struct nir_to_llvm_context ctx
= {0};
6886 ctx
.context
= LLVMContextCreate();
6887 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6888 ctx
.options
= options
;
6889 ctx
.shader_info
= shader_info
;
6891 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
6893 ctx
.ac
.module
= ctx
.module
;
6895 ctx
.is_gs_copy_shader
= true;
6896 LLVMSetTarget(ctx
.module
, "amdgcn--");
6898 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6899 ctx
.ac
.builder
= ctx
.builder
;
6900 ctx
.stage
= MESA_SHADER_VERTEX
;
6902 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
6904 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6905 ac_setup_rings(&ctx
);
6907 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6908 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6910 struct ac_nir_context nir_ctx
= {};
6911 nir_ctx
.ac
= ctx
.ac
;
6912 nir_ctx
.abi
= &ctx
.abi
;
6914 nir_ctx
.nctx
= &ctx
;
6917 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
6918 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
6919 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
6922 ac_gs_copy_shader_emit(&ctx
);
6926 LLVMBuildRetVoid(ctx
.builder
);
6928 ac_llvm_finalize_module(&ctx
);
6930 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6932 dump_shader
, options
->supports_spill
);