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 tes_rel_patch_id
;
118 LLVMValueRef gsvs_ring_stride
;
119 LLVMValueRef gsvs_num_entries
;
120 LLVMValueRef gs2vs_offset
;
121 LLVMValueRef gs_wave_id
;
122 LLVMValueRef gs_vtx_offset
[6];
124 LLVMValueRef esgs_ring
;
125 LLVMValueRef gsvs_ring
;
126 LLVMValueRef hs_ring_tess_offchip
;
127 LLVMValueRef hs_ring_tess_factor
;
129 LLVMValueRef prim_mask
;
130 LLVMValueRef sample_pos_offset
;
131 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
132 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
134 gl_shader_stage stage
;
136 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
139 uint64_t output_mask
;
140 uint8_t num_output_clips
;
141 uint8_t num_output_culls
;
143 bool is_gs_copy_shader
;
144 LLVMValueRef gs_next_vertex
;
145 unsigned gs_max_out_vertices
;
147 unsigned tes_primitive_mode
;
148 uint64_t tess_outputs_written
;
149 uint64_t tess_patch_outputs_written
;
151 uint32_t tcs_patch_outputs_read
;
152 uint64_t tcs_outputs_read
;
155 static inline struct nir_to_llvm_context
*
156 nir_to_llvm_context_from_abi(struct ac_shader_abi
*abi
)
158 struct nir_to_llvm_context
*ctx
= NULL
;
159 return container_of(abi
, ctx
, abi
);
163 nir2llvmtype(struct ac_nir_context
*ctx
,
164 const struct glsl_type
*type
)
166 switch (glsl_get_base_type(glsl_without_array(type
))) {
170 case GLSL_TYPE_UINT64
:
171 case GLSL_TYPE_INT64
:
173 case GLSL_TYPE_DOUBLE
:
175 case GLSL_TYPE_FLOAT
:
178 assert(!"Unsupported type in nir2llvmtype()");
184 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
185 const nir_deref_var
*deref
,
186 enum ac_descriptor_type desc_type
,
187 const nir_tex_instr
*instr
,
188 bool image
, bool write
);
190 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
192 return (index
* 4) + chan
;
195 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
197 /* handle patch indices separate */
198 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
200 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
202 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
203 return 2 + (slot
- VARYING_SLOT_PATCH0
);
205 if (slot
== VARYING_SLOT_POS
)
207 if (slot
== VARYING_SLOT_PSIZ
)
209 if (slot
== VARYING_SLOT_CLIP_DIST0
)
211 /* 3 is reserved for clip dist as well */
212 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
213 return 4 + (slot
- VARYING_SLOT_VAR0
);
214 unreachable("illegal slot in get unique index\n");
217 static void set_llvm_calling_convention(LLVMValueRef func
,
218 gl_shader_stage stage
)
220 enum radeon_llvm_calling_convention calling_conv
;
223 case MESA_SHADER_VERTEX
:
224 case MESA_SHADER_TESS_EVAL
:
225 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
227 case MESA_SHADER_GEOMETRY
:
228 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
230 case MESA_SHADER_TESS_CTRL
:
231 calling_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
: RADEON_LLVM_AMDGPU_VS
;
233 case MESA_SHADER_FRAGMENT
:
234 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
236 case MESA_SHADER_COMPUTE
:
237 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
240 unreachable("Unhandle shader type");
243 LLVMSetFunctionCallConv(func
, calling_conv
);
248 LLVMTypeRef types
[MAX_ARGS
];
249 LLVMValueRef
*assign
[MAX_ARGS
];
250 unsigned array_params_mask
;
253 uint8_t num_sgprs_used
;
254 uint8_t num_vgprs_used
;
257 enum ac_arg_regfile
{
263 add_arg(struct arg_info
*info
, enum ac_arg_regfile regfile
, LLVMTypeRef type
,
264 LLVMValueRef
*param_ptr
)
266 assert(info
->count
< MAX_ARGS
);
268 info
->assign
[info
->count
] = param_ptr
;
269 info
->types
[info
->count
] = type
;
272 if (regfile
== ARG_SGPR
) {
273 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
276 assert(regfile
== ARG_VGPR
);
277 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
282 add_array_arg(struct arg_info
*info
, LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
284 info
->array_params_mask
|= (1 << info
->count
);
285 add_arg(info
, ARG_SGPR
, type
, param_ptr
);
288 static void assign_arguments(LLVMValueRef main_function
,
289 struct arg_info
*info
)
292 for (i
= 0; i
< info
->count
; i
++) {
294 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
299 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
300 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
301 unsigned num_return_elems
,
302 struct arg_info
*args
,
303 unsigned max_workgroup_size
,
306 LLVMTypeRef main_function_type
, ret_type
;
307 LLVMBasicBlockRef main_function_body
;
309 if (num_return_elems
)
310 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
311 num_return_elems
, true);
313 ret_type
= LLVMVoidTypeInContext(ctx
);
315 /* Setup the function */
317 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
318 LLVMValueRef main_function
=
319 LLVMAddFunction(module
, "main", main_function_type
);
321 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
322 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
324 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
325 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
326 if (args
->array_params_mask
& (1 << i
)) {
327 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
328 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
329 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
332 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
336 if (max_workgroup_size
) {
337 ac_llvm_add_target_dep_function_attr(main_function
,
338 "amdgpu-max-work-group-size",
342 /* These were copied from some LLVM test. */
343 LLVMAddTargetDependentFunctionAttr(main_function
,
344 "less-precise-fpmad",
346 LLVMAddTargetDependentFunctionAttr(main_function
,
349 LLVMAddTargetDependentFunctionAttr(main_function
,
352 LLVMAddTargetDependentFunctionAttr(main_function
,
355 LLVMAddTargetDependentFunctionAttr(main_function
,
356 "no-signed-zeros-fp-math",
359 return main_function
;
362 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
364 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
368 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
370 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
371 type
= LLVMGetElementType(type
);
373 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
374 return LLVMGetIntTypeWidth(type
);
376 if (type
== ctx
->f16
)
378 if (type
== ctx
->f32
)
380 if (type
== ctx
->f64
)
383 unreachable("Unhandled type kind in get_elem_bits");
386 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
387 LLVMValueRef param
, unsigned rshift
,
390 LLVMValueRef value
= param
;
392 value
= LLVMBuildLShr(ctx
->builder
, value
,
393 LLVMConstInt(ctx
->i32
, rshift
, false), "");
395 if (rshift
+ bitwidth
< 32) {
396 unsigned mask
= (1 << bitwidth
) - 1;
397 value
= LLVMBuildAnd(ctx
->builder
, value
,
398 LLVMConstInt(ctx
->i32
, mask
, false), "");
403 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
405 switch (ctx
->stage
) {
406 case MESA_SHADER_TESS_CTRL
:
407 return unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
408 case MESA_SHADER_TESS_EVAL
:
409 return ctx
->tes_rel_patch_id
;
412 unreachable("Illegal stage");
416 /* Tessellation shaders pass outputs to the next shader using LDS.
418 * LS outputs = TCS inputs
419 * TCS outputs = TES inputs
422 * - TCS inputs for patch 0
423 * - TCS inputs for patch 1
424 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
426 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
427 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
428 * - TCS outputs for patch 1
429 * - Per-patch TCS outputs for patch 1
430 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
431 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
434 * All three shaders VS(LS), TCS, TES share the same LDS space.
437 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
439 if (ctx
->stage
== MESA_SHADER_VERTEX
)
440 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
441 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
442 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
450 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
452 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
456 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
458 return LLVMBuildMul(ctx
->builder
,
459 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
460 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
464 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
466 return LLVMBuildMul(ctx
->builder
,
467 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
468 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
472 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
474 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
475 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
477 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
481 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
483 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
484 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
485 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
487 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
488 LLVMBuildMul(ctx
->builder
, patch_stride
,
494 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
496 LLVMValueRef patch0_patch_data_offset
=
497 get_tcs_out_patch0_patch_data_offset(ctx
);
498 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
499 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
501 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
502 LLVMBuildMul(ctx
->builder
, patch_stride
,
508 set_loc(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
,
509 uint32_t indirect_offset
)
511 ud_info
->sgpr_idx
= *sgpr_idx
;
512 ud_info
->num_sgprs
= num_sgprs
;
513 ud_info
->indirect
= indirect_offset
> 0;
514 ud_info
->indirect_offset
= indirect_offset
;
515 *sgpr_idx
+= num_sgprs
;
519 set_loc_shader(struct nir_to_llvm_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
522 struct ac_userdata_info
*ud_info
=
523 &ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
];
526 set_loc(ud_info
, sgpr_idx
, num_sgprs
, 0);
530 set_loc_desc(struct nir_to_llvm_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
531 uint32_t indirect_offset
)
533 struct ac_userdata_info
*ud_info
=
534 &ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[idx
];
537 set_loc(ud_info
, sgpr_idx
, 2, indirect_offset
);
540 struct user_sgpr_info
{
541 bool need_ring_offsets
;
543 bool indirect_all_descriptor_sets
;
546 static bool needs_view_index_sgpr(struct nir_to_llvm_context
*ctx
,
547 gl_shader_stage stage
)
550 case MESA_SHADER_VERTEX
:
551 if (ctx
->shader_info
->info
.needs_multiview_view_index
||
552 (!ctx
->options
->key
.vs
.as_es
&& !ctx
->options
->key
.vs
.as_ls
&& ctx
->options
->key
.has_multiview_view_index
))
555 case MESA_SHADER_TESS_EVAL
:
556 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
558 case MESA_SHADER_GEOMETRY
:
559 case MESA_SHADER_TESS_CTRL
:
560 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
568 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
569 gl_shader_stage stage
,
570 bool needs_view_index
,
571 struct user_sgpr_info
*user_sgpr_info
)
573 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
575 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
576 if (stage
== MESA_SHADER_GEOMETRY
||
577 stage
== MESA_SHADER_VERTEX
||
578 stage
== MESA_SHADER_TESS_CTRL
||
579 stage
== MESA_SHADER_TESS_EVAL
||
580 ctx
->is_gs_copy_shader
)
581 user_sgpr_info
->need_ring_offsets
= true;
583 if (stage
== MESA_SHADER_FRAGMENT
&&
584 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
585 user_sgpr_info
->need_ring_offsets
= true;
587 /* 2 user sgprs will nearly always be allocated for scratch/rings */
588 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
589 user_sgpr_info
->sgpr_count
+= 2;
592 /* FIXME: fix the number of user sgprs for merged shaders on GFX9 */
594 case MESA_SHADER_COMPUTE
:
595 if (ctx
->shader_info
->info
.cs
.uses_grid_size
)
596 user_sgpr_info
->sgpr_count
+= 3;
598 case MESA_SHADER_FRAGMENT
:
599 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
601 case MESA_SHADER_VERTEX
:
602 if (!ctx
->is_gs_copy_shader
) {
603 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
604 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
605 user_sgpr_info
->sgpr_count
+= 3;
607 user_sgpr_info
->sgpr_count
+= 2;
610 if (ctx
->options
->key
.vs
.as_ls
)
611 user_sgpr_info
->sgpr_count
++;
613 case MESA_SHADER_TESS_CTRL
:
614 user_sgpr_info
->sgpr_count
+= 4;
616 case MESA_SHADER_TESS_EVAL
:
617 user_sgpr_info
->sgpr_count
+= 1;
619 case MESA_SHADER_GEOMETRY
:
620 user_sgpr_info
->sgpr_count
+= 2;
626 if (needs_view_index
)
627 user_sgpr_info
->sgpr_count
++;
629 if (ctx
->shader_info
->info
.loads_push_constants
)
630 user_sgpr_info
->sgpr_count
+= 2;
632 uint32_t available_sgprs
= ctx
->options
->chip_class
>= GFX9
? 32 : 16;
633 uint32_t remaining_sgprs
= available_sgprs
- user_sgpr_info
->sgpr_count
;
635 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
636 user_sgpr_info
->sgpr_count
+= 2;
637 user_sgpr_info
->indirect_all_descriptor_sets
= true;
639 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
644 declare_global_input_sgprs(struct nir_to_llvm_context
*ctx
,
645 gl_shader_stage stage
,
646 bool has_previous_stage
,
647 gl_shader_stage previous_stage
,
648 const struct user_sgpr_info
*user_sgpr_info
,
649 struct arg_info
*args
,
650 LLVMValueRef
*desc_sets
)
652 LLVMTypeRef type
= const_array(ctx
->ac
.i8
, 1024 * 1024);
653 unsigned num_sets
= ctx
->options
->layout
?
654 ctx
->options
->layout
->num_sets
: 0;
655 unsigned stage_mask
= 1 << stage
;
657 if (has_previous_stage
)
658 stage_mask
|= 1 << previous_stage
;
660 /* 1 for each descriptor set */
661 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
662 for (unsigned i
= 0; i
< num_sets
; ++i
) {
663 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
664 add_array_arg(args
, type
,
665 &ctx
->descriptor_sets
[i
]);
669 add_array_arg(args
, const_array(type
, 32), desc_sets
);
672 if (ctx
->shader_info
->info
.loads_push_constants
) {
673 /* 1 for push constants and dynamic descriptors */
674 add_array_arg(args
, type
, &ctx
->push_constants
);
679 declare_vs_specific_input_sgprs(struct nir_to_llvm_context
*ctx
,
680 gl_shader_stage stage
,
681 bool has_previous_stage
,
682 gl_shader_stage previous_stage
,
683 struct arg_info
*args
)
685 if (!ctx
->is_gs_copy_shader
&&
686 (stage
== MESA_SHADER_VERTEX
||
687 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
688 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
689 add_arg(args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
690 &ctx
->vertex_buffers
);
692 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
);
693 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);
694 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
695 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
);
701 declare_vs_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
703 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
);
704 if (!ctx
->is_gs_copy_shader
) {
705 if (ctx
->options
->key
.vs
.as_ls
) {
706 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->rel_auto_id
);
707 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
709 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
710 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->vs_prim_id
);
712 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* unused */
717 declare_tes_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
719 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_u
);
720 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_v
);
721 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
);
722 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.tes_patch_id
);
726 set_global_input_locs(struct nir_to_llvm_context
*ctx
, gl_shader_stage stage
,
727 bool has_previous_stage
, gl_shader_stage previous_stage
,
728 const struct user_sgpr_info
*user_sgpr_info
,
729 LLVMValueRef desc_sets
, uint8_t *user_sgpr_idx
)
731 unsigned num_sets
= ctx
->options
->layout
?
732 ctx
->options
->layout
->num_sets
: 0;
733 unsigned stage_mask
= 1 << stage
;
735 if (has_previous_stage
)
736 stage_mask
|= 1 << previous_stage
;
738 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
739 for (unsigned i
= 0; i
< num_sets
; ++i
) {
740 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
741 set_loc_desc(ctx
, i
, user_sgpr_idx
, 0);
743 ctx
->descriptor_sets
[i
] = NULL
;
746 set_loc_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
,
749 for (unsigned i
= 0; i
< num_sets
; ++i
) {
750 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
751 set_loc_desc(ctx
, i
, user_sgpr_idx
, i
* 8);
752 ctx
->descriptor_sets
[i
] =
753 ac_build_load_to_sgpr(&ctx
->ac
,
755 LLVMConstInt(ctx
->ac
.i32
, i
, false));
758 ctx
->descriptor_sets
[i
] = NULL
;
760 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
763 if (ctx
->shader_info
->info
.loads_push_constants
) {
764 set_loc_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
769 set_vs_specific_input_locs(struct nir_to_llvm_context
*ctx
,
770 gl_shader_stage stage
, bool has_previous_stage
,
771 gl_shader_stage previous_stage
,
772 uint8_t *user_sgpr_idx
)
774 if (!ctx
->is_gs_copy_shader
&&
775 (stage
== MESA_SHADER_VERTEX
||
776 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
777 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
778 set_loc_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
,
783 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
786 set_loc_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
787 user_sgpr_idx
, vs_num
);
791 static void create_function(struct nir_to_llvm_context
*ctx
,
792 gl_shader_stage stage
,
793 bool has_previous_stage
,
794 gl_shader_stage previous_stage
)
796 uint8_t user_sgpr_idx
;
797 struct user_sgpr_info user_sgpr_info
;
798 struct arg_info args
= {};
799 LLVMValueRef desc_sets
;
800 bool needs_view_index
= needs_view_index_sgpr(ctx
, stage
);
801 allocate_user_sgprs(ctx
, stage
, needs_view_index
, &user_sgpr_info
);
803 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
804 add_arg(&args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
809 case MESA_SHADER_COMPUTE
:
810 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
811 previous_stage
, &user_sgpr_info
,
814 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
815 add_arg(&args
, ARG_SGPR
, ctx
->ac
.v3i32
,
816 &ctx
->num_work_groups
);
819 for (int i
= 0; i
< 3; i
++) {
820 ctx
->workgroup_ids
[i
] = NULL
;
821 if (ctx
->shader_info
->info
.cs
.uses_block_id
[i
]) {
822 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
823 &ctx
->workgroup_ids
[i
]);
827 if (ctx
->shader_info
->info
.cs
.uses_local_invocation_idx
)
828 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tg_size
);
829 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
,
830 &ctx
->local_invocation_ids
);
832 case MESA_SHADER_VERTEX
:
833 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
834 previous_stage
, &user_sgpr_info
,
836 declare_vs_specific_input_sgprs(ctx
, stage
, has_previous_stage
,
837 previous_stage
, &args
);
839 if (needs_view_index
)
840 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
841 if (ctx
->options
->key
.vs
.as_es
)
842 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
844 else if (ctx
->options
->key
.vs
.as_ls
)
845 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
846 &ctx
->ls_out_layout
);
848 declare_vs_input_vgprs(ctx
, &args
);
850 case MESA_SHADER_TESS_CTRL
:
851 if (has_previous_stage
) {
852 // First 6 system regs
853 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
854 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
855 &ctx
->merged_wave_info
);
856 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
857 &ctx
->tess_factor_offset
);
859 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
860 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
861 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
863 declare_global_input_sgprs(ctx
, stage
,
866 &user_sgpr_info
, &args
,
868 declare_vs_specific_input_sgprs(ctx
, stage
,
870 previous_stage
, &args
);
872 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
873 &ctx
->ls_out_layout
);
875 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
876 &ctx
->tcs_offchip_layout
);
877 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
878 &ctx
->tcs_out_offsets
);
879 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
880 &ctx
->tcs_out_layout
);
881 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
882 &ctx
->tcs_in_layout
);
883 if (needs_view_index
)
884 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
887 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
888 &ctx
->abi
.tcs_patch_id
);
889 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
890 &ctx
->abi
.tcs_rel_ids
);
892 declare_vs_input_vgprs(ctx
, &args
);
894 declare_global_input_sgprs(ctx
, stage
,
897 &user_sgpr_info
, &args
,
900 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
901 &ctx
->tcs_offchip_layout
);
902 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
903 &ctx
->tcs_out_offsets
);
904 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
905 &ctx
->tcs_out_layout
);
906 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
907 &ctx
->tcs_in_layout
);
908 if (needs_view_index
)
909 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
912 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
913 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
914 &ctx
->tess_factor_offset
);
915 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
916 &ctx
->abi
.tcs_patch_id
);
917 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
918 &ctx
->abi
.tcs_rel_ids
);
921 case MESA_SHADER_TESS_EVAL
:
922 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
923 previous_stage
, &user_sgpr_info
,
926 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
);
927 if (needs_view_index
)
928 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
930 if (ctx
->options
->key
.tes
.as_es
) {
931 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
932 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
933 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
936 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
937 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
939 declare_tes_input_vgprs(ctx
, &args
);
941 case MESA_SHADER_GEOMETRY
:
942 if (has_previous_stage
) {
943 // First 6 system regs
944 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
946 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
947 &ctx
->merged_wave_info
);
948 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
950 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
951 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
952 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
954 declare_global_input_sgprs(ctx
, stage
,
957 &user_sgpr_info
, &args
,
960 if (previous_stage
== MESA_SHADER_TESS_EVAL
) {
961 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
962 &ctx
->tcs_offchip_layout
);
964 declare_vs_specific_input_sgprs(ctx
, stage
,
970 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
971 &ctx
->gsvs_ring_stride
);
972 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
973 &ctx
->gsvs_num_entries
);
974 if (needs_view_index
)
975 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
978 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
979 &ctx
->gs_vtx_offset
[0]);
980 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
981 &ctx
->gs_vtx_offset
[2]);
982 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
983 &ctx
->abi
.gs_prim_id
);
984 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
985 &ctx
->abi
.gs_invocation_id
);
986 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
987 &ctx
->gs_vtx_offset
[4]);
989 if (previous_stage
== MESA_SHADER_VERTEX
) {
990 declare_vs_input_vgprs(ctx
, &args
);
992 declare_tes_input_vgprs(ctx
, &args
);
995 declare_global_input_sgprs(ctx
, stage
,
998 &user_sgpr_info
, &args
,
1001 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1002 &ctx
->gsvs_ring_stride
);
1003 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1004 &ctx
->gsvs_num_entries
);
1005 if (needs_view_index
)
1006 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1009 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
);
1010 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs_wave_id
);
1011 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1012 &ctx
->gs_vtx_offset
[0]);
1013 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1014 &ctx
->gs_vtx_offset
[1]);
1015 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1016 &ctx
->abi
.gs_prim_id
);
1017 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1018 &ctx
->gs_vtx_offset
[2]);
1019 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1020 &ctx
->gs_vtx_offset
[3]);
1021 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1022 &ctx
->gs_vtx_offset
[4]);
1023 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1024 &ctx
->gs_vtx_offset
[5]);
1025 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1026 &ctx
->abi
.gs_invocation_id
);
1029 case MESA_SHADER_FRAGMENT
:
1030 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
1031 previous_stage
, &user_sgpr_info
,
1034 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
1035 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1036 &ctx
->sample_pos_offset
);
1038 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->prim_mask
);
1039 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_sample
);
1040 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_center
);
1041 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
);
1042 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
1043 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_sample
);
1044 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_center
);
1045 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
);
1046 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
1047 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]);
1048 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]);
1049 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]);
1050 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]);
1051 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.front_face
);
1052 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
);
1053 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
);
1054 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* fixed pt */
1057 unreachable("Shader stage not implemented");
1060 ctx
->main_function
= create_llvm_function(
1061 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
1062 ctx
->max_workgroup_size
,
1063 ctx
->options
->unsafe_math
);
1064 set_llvm_calling_convention(ctx
->main_function
, stage
);
1067 ctx
->shader_info
->num_input_vgprs
= 0;
1068 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
1070 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
1072 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1073 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
1075 assign_arguments(ctx
->main_function
, &args
);
1079 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
1080 set_loc_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
,
1082 if (ctx
->options
->supports_spill
) {
1083 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
1084 LLVMPointerType(ctx
->ac
.i8
, CONST_ADDR_SPACE
),
1085 NULL
, 0, AC_FUNC_ATTR_READNONE
);
1086 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
1087 const_array(ctx
->ac
.v4i32
, 16), "");
1091 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
1092 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
1093 if (has_previous_stage
)
1096 set_global_input_locs(ctx
, stage
, has_previous_stage
, previous_stage
,
1097 &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
1100 case MESA_SHADER_COMPUTE
:
1101 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
1102 set_loc_shader(ctx
, AC_UD_CS_GRID_SIZE
,
1106 case MESA_SHADER_VERTEX
:
1107 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1108 previous_stage
, &user_sgpr_idx
);
1109 if (ctx
->view_index
)
1110 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1111 if (ctx
->options
->key
.vs
.as_ls
) {
1112 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1115 if (ctx
->options
->key
.vs
.as_ls
)
1116 ac_declare_lds_as_pointer(&ctx
->ac
);
1118 case MESA_SHADER_TESS_CTRL
:
1119 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1120 previous_stage
, &user_sgpr_idx
);
1121 if (has_previous_stage
)
1122 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1124 set_loc_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
1125 if (ctx
->view_index
)
1126 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1127 ac_declare_lds_as_pointer(&ctx
->ac
);
1129 case MESA_SHADER_TESS_EVAL
:
1130 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
1131 if (ctx
->view_index
)
1132 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1134 case MESA_SHADER_GEOMETRY
:
1135 if (has_previous_stage
) {
1136 if (previous_stage
== MESA_SHADER_VERTEX
)
1137 set_vs_specific_input_locs(ctx
, stage
,
1142 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
,
1145 set_loc_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
,
1147 if (ctx
->view_index
)
1148 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1149 if (has_previous_stage
)
1150 ac_declare_lds_as_pointer(&ctx
->ac
);
1152 case MESA_SHADER_FRAGMENT
:
1153 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
1154 set_loc_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
,
1159 unreachable("Shader stage not implemented");
1162 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
1165 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1166 LLVMValueRef value
, unsigned count
)
1168 unsigned num_components
= ac_get_llvm_num_components(value
);
1169 if (count
== num_components
)
1172 LLVMValueRef masks
[] = {
1173 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1174 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1177 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1180 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1181 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1185 build_store_values_extended(struct ac_llvm_context
*ac
,
1186 LLVMValueRef
*values
,
1187 unsigned value_count
,
1188 unsigned value_stride
,
1191 LLVMBuilderRef builder
= ac
->builder
;
1194 for (i
= 0; i
< value_count
; i
++) {
1195 LLVMValueRef ptr
= values
[i
* value_stride
];
1196 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1197 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1198 LLVMBuildStore(builder
, value
, ptr
);
1202 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1203 const nir_ssa_def
*def
)
1205 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1206 if (def
->num_components
> 1) {
1207 type
= LLVMVectorType(type
, def
->num_components
);
1212 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1215 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1216 return (LLVMValueRef
)entry
->data
;
1220 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1221 const struct nir_block
*b
)
1223 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1224 return (LLVMBasicBlockRef
)entry
->data
;
1227 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1229 unsigned num_components
)
1231 LLVMValueRef value
= get_src(ctx
, src
.src
);
1232 bool need_swizzle
= false;
1235 LLVMTypeRef type
= LLVMTypeOf(value
);
1236 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1237 ? LLVMGetVectorSize(type
)
1240 for (unsigned i
= 0; i
< num_components
; ++i
) {
1241 assert(src
.swizzle
[i
] < src_components
);
1242 if (src
.swizzle
[i
] != i
)
1243 need_swizzle
= true;
1246 if (need_swizzle
|| num_components
!= src_components
) {
1247 LLVMValueRef masks
[] = {
1248 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1249 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1250 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1251 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1253 if (src_components
> 1 && num_components
== 1) {
1254 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1256 } else if (src_components
== 1 && num_components
> 1) {
1257 LLVMValueRef values
[] = {value
, value
, value
, value
};
1258 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1260 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1261 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1265 assert(!src
.negate
);
1270 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1271 LLVMIntPredicate pred
, LLVMValueRef src0
,
1274 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1275 return LLVMBuildSelect(ctx
->builder
, result
,
1276 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1280 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1281 LLVMRealPredicate pred
, LLVMValueRef src0
,
1284 LLVMValueRef result
;
1285 src0
= ac_to_float(ctx
, src0
);
1286 src1
= ac_to_float(ctx
, src1
);
1287 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1288 return LLVMBuildSelect(ctx
->builder
, result
,
1289 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1293 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1295 LLVMTypeRef result_type
,
1299 LLVMValueRef params
[] = {
1300 ac_to_float(ctx
, src0
),
1303 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1304 get_elem_bits(ctx
, result_type
));
1305 assert(length
< sizeof(name
));
1306 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1309 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1311 LLVMTypeRef result_type
,
1312 LLVMValueRef src0
, LLVMValueRef src1
)
1315 LLVMValueRef params
[] = {
1316 ac_to_float(ctx
, src0
),
1317 ac_to_float(ctx
, src1
),
1320 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1321 get_elem_bits(ctx
, result_type
));
1322 assert(length
< sizeof(name
));
1323 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1326 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1328 LLVMTypeRef result_type
,
1329 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1332 LLVMValueRef params
[] = {
1333 ac_to_float(ctx
, src0
),
1334 ac_to_float(ctx
, src1
),
1335 ac_to_float(ctx
, src2
),
1338 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1339 get_elem_bits(ctx
, result_type
));
1340 assert(length
< sizeof(name
));
1341 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1344 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1345 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1347 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1349 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1352 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1353 LLVMIntPredicate pred
,
1354 LLVMValueRef src0
, LLVMValueRef src1
)
1356 return LLVMBuildSelect(ctx
->builder
,
1357 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1362 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1365 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1366 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1369 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1373 LLVMValueRef cmp
, val
, zero
, one
;
1376 if (bitsize
== 32) {
1386 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
1387 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1388 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
1389 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
1393 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1394 LLVMValueRef src0
, unsigned bitsize
)
1396 LLVMValueRef cmp
, val
, zero
, one
;
1399 if (bitsize
== 32) {
1409 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
1410 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1411 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
1412 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
1416 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1419 const char *intr
= "llvm.floor.f32";
1420 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1421 LLVMValueRef params
[] = {
1424 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1425 ctx
->f32
, params
, 1,
1426 AC_FUNC_ATTR_READNONE
);
1427 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1430 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1432 LLVMValueRef src0
, LLVMValueRef src1
)
1434 LLVMTypeRef ret_type
;
1435 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1437 LLVMValueRef params
[] = { src0
, src1
};
1438 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1441 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1442 params
, 2, AC_FUNC_ATTR_READNONE
);
1444 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1445 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1449 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1452 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1455 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1458 src0
= ac_to_float(ctx
, src0
);
1459 return LLVMBuildSExt(ctx
->builder
,
1460 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1464 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1468 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1473 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
1476 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1479 return LLVMBuildSExt(ctx
->builder
,
1480 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1484 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1487 LLVMValueRef result
;
1488 LLVMValueRef cond
= NULL
;
1490 src0
= ac_to_float(&ctx
->ac
, src0
);
1491 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->ac
.f16
, "");
1493 if (ctx
->options
->chip_class
>= VI
) {
1494 LLVMValueRef args
[2];
1495 /* Check if the result is a denormal - and flush to 0 if so. */
1497 args
[1] = LLVMConstInt(ctx
->ac
.i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1498 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->ac
.i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1501 /* need to convert back up to f32 */
1502 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->ac
.f32
, "");
1504 if (ctx
->options
->chip_class
>= VI
)
1505 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1508 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1509 * so compare the result and flush to 0 if it's smaller.
1511 LLVMValueRef temp
, cond2
;
1512 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1513 ctx
->ac
.f32
, result
);
1514 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1515 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, 0x38800000, false), ctx
->ac
.f32
, ""),
1517 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1518 temp
, ctx
->ac
.f32_0
, "");
1519 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1520 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1525 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1526 LLVMValueRef src0
, LLVMValueRef src1
)
1528 LLVMValueRef dst64
, result
;
1529 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1530 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1532 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1533 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1534 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1538 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1539 LLVMValueRef src0
, LLVMValueRef src1
)
1541 LLVMValueRef dst64
, result
;
1542 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1543 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1545 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1546 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1547 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1551 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1553 const LLVMValueRef srcs
[3])
1555 LLVMValueRef result
;
1556 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1558 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1559 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1563 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1564 LLVMValueRef src0
, LLVMValueRef src1
,
1565 LLVMValueRef src2
, LLVMValueRef src3
)
1567 LLVMValueRef bfi_args
[3], result
;
1569 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1570 LLVMBuildSub(ctx
->builder
,
1571 LLVMBuildShl(ctx
->builder
,
1576 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1579 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1582 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1583 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1585 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1586 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1587 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1589 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1593 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1596 LLVMValueRef comp
[2];
1598 src0
= ac_to_float(ctx
, src0
);
1599 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1600 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1602 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
1605 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1608 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1609 LLVMValueRef temps
[2], result
, val
;
1612 for (i
= 0; i
< 2; i
++) {
1613 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1614 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1615 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1616 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1619 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1621 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1626 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1632 LLVMValueRef result
;
1634 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1635 mask
= AC_TID_MASK_LEFT
;
1636 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1637 mask
= AC_TID_MASK_TOP
;
1639 mask
= AC_TID_MASK_TOP_LEFT
;
1641 /* for DDX we want to next X pixel, DDY next Y pixel. */
1642 if (op
== nir_op_fddx_fine
||
1643 op
== nir_op_fddx_coarse
||
1649 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1654 * this takes an I,J coordinate pair,
1655 * and works out the X and Y derivatives.
1656 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1658 static LLVMValueRef
emit_ddxy_interp(
1659 struct ac_nir_context
*ctx
,
1660 LLVMValueRef interp_ij
)
1662 LLVMValueRef result
[4], a
;
1665 for (i
= 0; i
< 2; i
++) {
1666 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1667 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1668 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1669 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1671 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1674 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1676 LLVMValueRef src
[4], result
= NULL
;
1677 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1678 unsigned src_components
;
1679 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1681 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1682 switch (instr
->op
) {
1688 case nir_op_pack_half_2x16
:
1691 case nir_op_unpack_half_2x16
:
1695 src_components
= num_components
;
1698 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1699 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1701 switch (instr
->op
) {
1707 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1708 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1711 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1714 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1717 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1720 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1721 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1722 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1725 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1726 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1727 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1730 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1733 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1736 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1739 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1742 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1743 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1744 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1745 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1746 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1747 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1748 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1751 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1752 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1753 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1756 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1759 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1762 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1765 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1766 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1767 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1770 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1771 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1772 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1775 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1776 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
1780 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1783 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1786 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1789 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1790 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1791 LLVMTypeOf(src
[0]), ""),
1795 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1796 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1797 LLVMTypeOf(src
[0]), ""),
1801 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1802 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1803 LLVMTypeOf(src
[0]), ""),
1807 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1810 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1813 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1816 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1819 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1822 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1825 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1828 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1831 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1834 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1837 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1838 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1841 result
= emit_iabs(&ctx
->ac
, src
[0]);
1844 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1847 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1850 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1853 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1856 result
= emit_isign(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1859 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1860 result
= emit_fsign(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1863 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1864 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1867 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1868 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1871 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1872 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1874 case nir_op_fround_even
:
1875 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1876 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1879 result
= emit_ffract(&ctx
->ac
, src
[0]);
1882 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1883 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1886 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1887 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1890 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1891 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1894 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1895 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1898 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1899 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1902 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1903 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1904 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
1908 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1909 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1912 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1913 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1914 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1915 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1916 ac_to_float_type(&ctx
->ac
, def_type
),
1920 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1921 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1922 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1923 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1924 ac_to_float_type(&ctx
->ac
, def_type
),
1928 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1929 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1931 case nir_op_ibitfield_extract
:
1932 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1934 case nir_op_ubitfield_extract
:
1935 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1937 case nir_op_bitfield_insert
:
1938 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1940 case nir_op_bitfield_reverse
:
1941 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1943 case nir_op_bit_count
:
1944 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1949 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1950 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1951 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1955 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1956 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1960 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1961 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1965 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1966 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1970 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1971 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1974 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1975 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1978 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1982 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1983 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1984 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1986 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1990 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1991 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1992 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1994 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1997 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1999 case nir_op_find_lsb
:
2000 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2001 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
2003 case nir_op_ufind_msb
:
2004 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2005 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
2007 case nir_op_ifind_msb
:
2008 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2009 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
2011 case nir_op_uadd_carry
:
2012 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2013 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2014 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
2016 case nir_op_usub_borrow
:
2017 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2018 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2019 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
2022 result
= emit_b2f(&ctx
->ac
, src
[0]);
2025 result
= emit_f2b(&ctx
->ac
, src
[0]);
2028 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
2031 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2032 result
= emit_i2b(&ctx
->ac
, src
[0]);
2034 case nir_op_fquantize2f16
:
2035 result
= emit_f2f16(ctx
->nctx
, src
[0]);
2037 case nir_op_umul_high
:
2038 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2039 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2040 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
2042 case nir_op_imul_high
:
2043 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2044 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2045 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
2047 case nir_op_pack_half_2x16
:
2048 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
2050 case nir_op_unpack_half_2x16
:
2051 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
2055 case nir_op_fddx_fine
:
2056 case nir_op_fddy_fine
:
2057 case nir_op_fddx_coarse
:
2058 case nir_op_fddy_coarse
:
2059 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
2062 case nir_op_unpack_64_2x32_split_x
: {
2063 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2064 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2067 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2072 case nir_op_unpack_64_2x32_split_y
: {
2073 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2074 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2077 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2082 case nir_op_pack_64_2x32_split
: {
2083 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
2084 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2085 src
[0], ctx
->ac
.i32_0
, "");
2086 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2087 src
[1], ctx
->ac
.i32_1
, "");
2088 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
2093 fprintf(stderr
, "Unknown NIR alu instr: ");
2094 nir_print_instr(&instr
->instr
, stderr
);
2095 fprintf(stderr
, "\n");
2100 assert(instr
->dest
.dest
.is_ssa
);
2101 result
= ac_to_integer(&ctx
->ac
, result
);
2102 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
2107 static void visit_load_const(struct ac_nir_context
*ctx
,
2108 const nir_load_const_instr
*instr
)
2110 LLVMValueRef values
[4], value
= NULL
;
2111 LLVMTypeRef element_type
=
2112 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
2114 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
2115 switch (instr
->def
.bit_size
) {
2117 values
[i
] = LLVMConstInt(element_type
,
2118 instr
->value
.u32
[i
], false);
2121 values
[i
] = LLVMConstInt(element_type
,
2122 instr
->value
.u64
[i
], false);
2126 "unsupported nir load_const bit_size: %d\n",
2127 instr
->def
.bit_size
);
2131 if (instr
->def
.num_components
> 1) {
2132 value
= LLVMConstVector(values
, instr
->def
.num_components
);
2136 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
2139 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
2142 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2143 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2144 LLVMPointerType(type
, addr_space
), "");
2148 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
2151 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2152 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
2155 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
2156 /* On VI, the descriptor contains the size in bytes,
2157 * but TXQ must return the size in elements.
2158 * The stride is always non-zero for resources using TXQ.
2160 LLVMValueRef stride
=
2161 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2163 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2164 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2165 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2166 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2168 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2174 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2177 static void build_int_type_name(
2179 char *buf
, unsigned bufsize
)
2181 assert(bufsize
>= 6);
2183 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2184 snprintf(buf
, bufsize
, "v%ui32",
2185 LLVMGetVectorSize(type
));
2190 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2191 struct ac_image_args
*args
,
2192 const nir_tex_instr
*instr
)
2194 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2195 LLVMValueRef coord
= args
->addr
;
2196 LLVMValueRef half_texel
[2];
2197 LLVMValueRef compare_cube_wa
= NULL
;
2198 LLVMValueRef result
;
2200 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2204 struct ac_image_args txq_args
= { 0 };
2206 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2207 txq_args
.opcode
= ac_image_get_resinfo
;
2208 txq_args
.dmask
= 0xf;
2209 txq_args
.addr
= ctx
->i32_0
;
2210 txq_args
.resource
= args
->resource
;
2211 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2213 for (c
= 0; c
< 2; c
++) {
2214 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2215 LLVMConstInt(ctx
->i32
, c
, false), "");
2216 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2217 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2218 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2219 LLVMConstReal(ctx
->f32
, -0.5), "");
2223 LLVMValueRef orig_coords
= args
->addr
;
2225 for (c
= 0; c
< 2; c
++) {
2227 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2228 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2229 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2230 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2231 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2232 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2237 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2238 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2239 * workaround by sampling using a scaled type and converting.
2240 * This is taken from amdgpu-pro shaders.
2242 /* NOTE this produces some ugly code compared to amdgpu-pro,
2243 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2244 * and then reads them back. -pro generates two selects,
2245 * one s_cmp for the descriptor rewriting
2246 * one v_cmp for the coordinate and result changes.
2248 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2249 LLVMValueRef tmp
, tmp2
;
2251 /* workaround 8/8/8/8 uint/sint cube gather bug */
2252 /* first detect it then change to a scaled read and f2i */
2253 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2256 /* extract the DATA_FORMAT */
2257 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2258 LLVMConstInt(ctx
->i32
, 6, false), false);
2260 /* is the DATA_FORMAT == 8_8_8_8 */
2261 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2263 if (stype
== GLSL_TYPE_UINT
)
2264 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2265 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2266 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2268 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2269 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2270 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2272 /* replace the NUM FORMAT in the descriptor */
2273 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2274 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2276 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2278 /* don't modify the coordinates for this case */
2279 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2282 result
= ac_build_image_opcode(ctx
, args
);
2284 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2285 LLVMValueRef tmp
, tmp2
;
2287 /* if the cube workaround is in place, f2i the result. */
2288 for (c
= 0; c
< 4; c
++) {
2289 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2290 if (stype
== GLSL_TYPE_UINT
)
2291 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2293 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2294 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2295 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2296 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2297 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2298 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2304 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2305 const nir_tex_instr
*instr
,
2307 struct ac_image_args
*args
)
2309 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2310 return ac_build_buffer_load_format(&ctx
->ac
,
2317 args
->opcode
= ac_image_sample
;
2318 args
->compare
= instr
->is_shadow
;
2320 switch (instr
->op
) {
2322 case nir_texop_txf_ms
:
2323 case nir_texop_samples_identical
:
2324 args
->opcode
= lod_is_zero
||
2325 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
2326 ac_image_load
: ac_image_load_mip
;
2327 args
->compare
= false;
2328 args
->offset
= false;
2335 args
->level_zero
= true;
2340 case nir_texop_query_levels
:
2341 args
->opcode
= ac_image_get_resinfo
;
2344 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2345 args
->level_zero
= true;
2351 args
->opcode
= ac_image_gather4
;
2352 args
->level_zero
= true;
2355 args
->opcode
= ac_image_get_lod
;
2356 args
->compare
= false;
2357 args
->offset
= false;
2363 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2364 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2365 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2366 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2369 return ac_build_image_opcode(&ctx
->ac
, args
);
2372 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2373 nir_intrinsic_instr
*instr
)
2375 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2376 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2377 unsigned binding
= nir_intrinsic_binding(instr
);
2378 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2379 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2380 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2381 unsigned base_offset
= layout
->binding
[binding
].offset
;
2382 LLVMValueRef offset
, stride
;
2384 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2385 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2386 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2387 layout
->binding
[binding
].dynamic_offset_offset
;
2388 desc_ptr
= ctx
->push_constants
;
2389 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2390 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2392 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2394 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2395 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2396 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2398 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2399 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->ac
.v4i32
);
2400 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2405 static LLVMValueRef
visit_vulkan_resource_reindex(struct nir_to_llvm_context
*ctx
,
2406 nir_intrinsic_instr
*instr
)
2408 LLVMValueRef ptr
= get_src(ctx
->nir
, instr
->src
[0]);
2409 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[1]);
2411 LLVMValueRef result
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2412 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2416 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2417 nir_intrinsic_instr
*instr
)
2419 LLVMValueRef ptr
, addr
;
2421 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2422 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2424 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2425 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2427 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2430 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2431 const nir_intrinsic_instr
*instr
)
2433 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2435 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
2437 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2438 nir_intrinsic_instr
*instr
)
2440 const char *store_name
;
2441 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2442 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2443 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2444 int components_32bit
= elem_size_mult
* instr
->num_components
;
2445 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2446 LLVMValueRef base_data
, base_offset
;
2447 LLVMValueRef params
[6];
2449 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2450 get_src(ctx
, instr
->src
[1]), true);
2451 params
[2] = ctx
->ac
.i32_0
; /* vindex */
2452 params
[4] = ctx
->ac
.i1false
; /* glc */
2453 params
[5] = ctx
->ac
.i1false
; /* slc */
2455 if (components_32bit
> 1)
2456 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2458 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2459 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2460 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2462 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2466 LLVMValueRef offset
;
2468 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2470 /* Due to an LLVM limitation, split 3-element writes
2471 * into a 2-element and a 1-element write. */
2473 writemask
|= 1 << (start
+ 2);
2477 start
*= elem_size_mult
;
2478 count
*= elem_size_mult
;
2481 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2486 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2488 } else if (count
== 2) {
2489 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2490 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2491 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(ctx
->ac
.v2f32
), tmp
,
2494 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2495 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2496 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2498 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2502 if (ac_get_llvm_num_components(base_data
) > 1)
2503 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2504 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2507 store_name
= "llvm.amdgcn.buffer.store.f32";
2510 offset
= base_offset
;
2512 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2516 ac_build_intrinsic(&ctx
->ac
, store_name
,
2517 ctx
->ac
.voidt
, params
, 6, 0);
2521 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2522 const nir_intrinsic_instr
*instr
)
2525 LLVMValueRef params
[6];
2528 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2529 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2531 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2532 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2533 get_src(ctx
, instr
->src
[0]),
2535 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
2536 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2537 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2539 switch (instr
->intrinsic
) {
2540 case nir_intrinsic_ssbo_atomic_add
:
2541 name
= "llvm.amdgcn.buffer.atomic.add";
2543 case nir_intrinsic_ssbo_atomic_imin
:
2544 name
= "llvm.amdgcn.buffer.atomic.smin";
2546 case nir_intrinsic_ssbo_atomic_umin
:
2547 name
= "llvm.amdgcn.buffer.atomic.umin";
2549 case nir_intrinsic_ssbo_atomic_imax
:
2550 name
= "llvm.amdgcn.buffer.atomic.smax";
2552 case nir_intrinsic_ssbo_atomic_umax
:
2553 name
= "llvm.amdgcn.buffer.atomic.umax";
2555 case nir_intrinsic_ssbo_atomic_and
:
2556 name
= "llvm.amdgcn.buffer.atomic.and";
2558 case nir_intrinsic_ssbo_atomic_or
:
2559 name
= "llvm.amdgcn.buffer.atomic.or";
2561 case nir_intrinsic_ssbo_atomic_xor
:
2562 name
= "llvm.amdgcn.buffer.atomic.xor";
2564 case nir_intrinsic_ssbo_atomic_exchange
:
2565 name
= "llvm.amdgcn.buffer.atomic.swap";
2567 case nir_intrinsic_ssbo_atomic_comp_swap
:
2568 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2574 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2577 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2578 const nir_intrinsic_instr
*instr
)
2580 LLVMValueRef results
[2];
2581 int load_components
;
2582 int num_components
= instr
->num_components
;
2583 if (instr
->dest
.ssa
.bit_size
== 64)
2584 num_components
*= 2;
2586 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2587 load_components
= MIN2(num_components
- i
, 4);
2588 const char *load_name
;
2589 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2590 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2591 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2593 if (load_components
== 3)
2594 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2595 else if (load_components
> 1)
2596 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2598 if (load_components
>= 3)
2599 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2600 else if (load_components
== 2)
2601 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2602 else if (load_components
== 1)
2603 load_name
= "llvm.amdgcn.buffer.load.f32";
2605 unreachable("unhandled number of components");
2607 LLVMValueRef params
[] = {
2608 ctx
->abi
->load_ssbo(ctx
->abi
,
2609 get_src(ctx
, instr
->src
[0]),
2617 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2621 LLVMValueRef ret
= results
[0];
2622 if (num_components
> 4 || num_components
== 3) {
2623 LLVMValueRef masks
[] = {
2624 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2625 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2626 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2627 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2630 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2631 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2632 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2635 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2636 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2639 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2640 const nir_intrinsic_instr
*instr
)
2643 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2644 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2645 int num_components
= instr
->num_components
;
2647 if (ctx
->abi
->load_ubo
)
2648 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2650 if (instr
->dest
.ssa
.bit_size
== 64)
2651 num_components
*= 2;
2653 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2654 NULL
, 0, false, false, true, true);
2655 ret
= trim_vector(&ctx
->ac
, ret
, num_components
);
2656 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2657 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2661 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2662 bool vs_in
, unsigned *vertex_index_out
,
2663 LLVMValueRef
*vertex_index_ref
,
2664 unsigned *const_out
, LLVMValueRef
*indir_out
)
2666 unsigned const_offset
= 0;
2667 nir_deref
*tail
= &deref
->deref
;
2668 LLVMValueRef offset
= NULL
;
2670 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2672 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2673 if (vertex_index_out
)
2674 *vertex_index_out
= deref_array
->base_offset
;
2676 if (vertex_index_ref
) {
2677 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2678 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2679 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2681 *vertex_index_ref
= vtx
;
2685 if (deref
->var
->data
.compact
) {
2686 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2687 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2688 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2689 /* We always lower indirect dereferences for "compact" array vars. */
2690 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2692 const_offset
= deref_array
->base_offset
;
2696 while (tail
->child
!= NULL
) {
2697 const struct glsl_type
*parent_type
= tail
->type
;
2700 if (tail
->deref_type
== nir_deref_type_array
) {
2701 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2702 LLVMValueRef index
, stride
, local_offset
;
2703 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2705 const_offset
+= size
* deref_array
->base_offset
;
2706 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2709 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2710 index
= get_src(ctx
, deref_array
->indirect
);
2711 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2712 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2715 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2717 offset
= local_offset
;
2718 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2719 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2721 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2722 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2723 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2726 unreachable("unsupported deref type");
2730 if (const_offset
&& offset
)
2731 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2732 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2735 *const_out
= const_offset
;
2736 *indir_out
= offset
;
2740 /* The offchip buffer layout for TCS->TES is
2742 * - attribute 0 of patch 0 vertex 0
2743 * - attribute 0 of patch 0 vertex 1
2744 * - attribute 0 of patch 0 vertex 2
2746 * - attribute 0 of patch 1 vertex 0
2747 * - attribute 0 of patch 1 vertex 1
2749 * - attribute 1 of patch 0 vertex 0
2750 * - attribute 1 of patch 0 vertex 1
2752 * - per patch attribute 0 of patch 0
2753 * - per patch attribute 0 of patch 1
2756 * Note that every attribute has 4 components.
2758 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2759 LLVMValueRef vertex_index
,
2760 LLVMValueRef param_index
)
2762 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2763 LLVMValueRef param_stride
, constant16
;
2764 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2766 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2767 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2768 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2771 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2773 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2774 vertices_per_patch
, "");
2776 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2779 param_stride
= total_vertices
;
2781 base_addr
= rel_patch_id
;
2782 param_stride
= num_patches
;
2785 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2786 LLVMBuildMul(ctx
->builder
, param_index
,
2787 param_stride
, ""), "");
2789 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2791 if (!vertex_index
) {
2792 LLVMValueRef patch_data_offset
=
2793 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2795 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2796 patch_data_offset
, "");
2801 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2803 unsigned const_index
,
2805 LLVMValueRef vertex_index
,
2806 LLVMValueRef indir_index
)
2808 LLVMValueRef param_index
;
2811 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2814 if (const_index
&& !is_compact
)
2815 param
+= const_index
;
2816 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2818 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2822 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2823 bool is_patch
, uint32_t param
)
2827 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2829 ctx
->tess_outputs_written
|= (1ull << param
);
2833 get_dw_address(struct nir_to_llvm_context
*ctx
,
2834 LLVMValueRef dw_addr
,
2836 unsigned const_index
,
2837 bool compact_const_index
,
2838 LLVMValueRef vertex_index
,
2839 LLVMValueRef stride
,
2840 LLVMValueRef indir_index
)
2845 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2846 LLVMBuildMul(ctx
->builder
,
2852 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2853 LLVMBuildMul(ctx
->builder
, indir_index
,
2854 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2855 else if (const_index
&& !compact_const_index
)
2856 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2857 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2859 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2860 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2862 if (const_index
&& compact_const_index
)
2863 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2864 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2869 load_tcs_varyings(struct ac_shader_abi
*abi
,
2870 LLVMValueRef vertex_index
,
2871 LLVMValueRef indir_index
,
2872 unsigned const_index
,
2874 unsigned driver_location
,
2876 unsigned num_components
,
2881 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2882 LLVMValueRef dw_addr
, stride
;
2883 LLVMValueRef value
[4], result
;
2884 unsigned param
= shader_io_get_unique_index(location
);
2887 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2888 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2891 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2892 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2894 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2899 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2902 for (unsigned i
= 0; i
< num_components
+ component
; i
++) {
2903 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2904 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2907 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
2912 store_tcs_output(struct ac_shader_abi
*abi
,
2913 LLVMValueRef vertex_index
,
2914 LLVMValueRef param_index
,
2915 unsigned const_index
,
2917 unsigned driver_location
,
2924 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2925 LLVMValueRef dw_addr
;
2926 LLVMValueRef stride
= NULL
;
2927 LLVMValueRef buf_addr
= NULL
;
2929 bool store_lds
= true;
2932 if (!(ctx
->tcs_patch_outputs_read
& (1U << (location
- VARYING_SLOT_PATCH0
))))
2935 if (!(ctx
->tcs_outputs_read
& (1ULL << location
)))
2939 param
= shader_io_get_unique_index(location
);
2940 if (location
== VARYING_SLOT_CLIP_DIST0
&&
2941 is_compact
&& const_index
> 3) {
2947 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2948 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2950 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2953 mark_tess_output(ctx
, is_patch
, param
);
2955 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2957 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2958 vertex_index
, param_index
);
2960 bool is_tess_factor
= false;
2961 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2962 location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2963 is_tess_factor
= true;
2965 unsigned base
= is_compact
? const_index
: 0;
2966 for (unsigned chan
= 0; chan
< 8; chan
++) {
2967 if (!(writemask
& (1 << chan
)))
2969 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
2971 if (store_lds
|| is_tess_factor
) {
2972 LLVMValueRef dw_addr_chan
=
2973 LLVMBuildAdd(ctx
->builder
, dw_addr
,
2974 LLVMConstInt(ctx
->ac
.i32
, chan
, false), "");
2975 ac_lds_store(&ctx
->ac
, dw_addr_chan
, value
);
2978 if (!is_tess_factor
&& writemask
!= 0xF)
2979 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2980 buf_addr
, ctx
->oc_lds
,
2981 4 * (base
+ chan
), 1, 0, true, false);
2984 if (writemask
== 0xF) {
2985 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2986 buf_addr
, ctx
->oc_lds
,
2987 (base
* 4), 1, 0, true, false);
2992 load_tes_input(struct ac_shader_abi
*abi
,
2993 LLVMValueRef vertex_index
,
2994 LLVMValueRef param_index
,
2995 unsigned const_index
,
2997 unsigned driver_location
,
2999 unsigned num_components
,
3004 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3005 LLVMValueRef buf_addr
;
3006 LLVMValueRef result
;
3007 unsigned param
= shader_io_get_unique_index(location
);
3009 if (location
== VARYING_SLOT_CLIP_DIST0
&& is_compact
&& const_index
> 3) {
3014 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
3015 is_compact
, vertex_index
, param_index
);
3017 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, component
* 4, false);
3018 buf_addr
= LLVMBuildAdd(ctx
->builder
, buf_addr
, comp_offset
, "");
3020 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, num_components
, NULL
,
3021 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
3022 result
= trim_vector(&ctx
->ac
, result
, num_components
);
3027 load_gs_input(struct ac_shader_abi
*abi
,
3029 unsigned driver_location
,
3031 unsigned num_components
,
3032 unsigned vertex_index
,
3033 unsigned const_index
,
3036 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3037 LLVMValueRef vtx_offset
;
3038 LLVMValueRef args
[9];
3039 unsigned param
, vtx_offset_param
;
3040 LLVMValueRef value
[4], result
;
3042 vtx_offset_param
= vertex_index
;
3043 assert(vtx_offset_param
< 6);
3044 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
3045 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3047 param
= shader_io_get_unique_index(location
);
3049 for (unsigned i
= component
; i
< num_components
+ component
; i
++) {
3050 if (ctx
->ac
.chip_class
>= GFX9
) {
3051 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
3052 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
3053 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
3054 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
3056 args
[0] = ctx
->esgs_ring
;
3057 args
[1] = vtx_offset
;
3058 args
[2] = LLVMConstInt(ctx
->ac
.i32
, (param
* 4 + i
+ const_index
) * 256, false);
3059 args
[3] = ctx
->ac
.i32_0
;
3060 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
3061 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
3062 args
[6] = ctx
->ac
.i32_1
; /* GLC */
3063 args
[7] = ctx
->ac
.i32_0
; /* SLC */
3064 args
[8] = ctx
->ac
.i32_0
; /* TFE */
3066 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
3067 ctx
->ac
.i32
, args
, 9,
3068 AC_FUNC_ATTR_READONLY
|
3069 AC_FUNC_ATTR_LEGACY
);
3072 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
3078 build_gep_for_deref(struct ac_nir_context
*ctx
,
3079 nir_deref_var
*deref
)
3081 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
3082 assert(entry
->data
);
3083 LLVMValueRef val
= entry
->data
;
3084 nir_deref
*tail
= deref
->deref
.child
;
3085 while (tail
!= NULL
) {
3086 LLVMValueRef offset
;
3087 switch (tail
->deref_type
) {
3088 case nir_deref_type_array
: {
3089 nir_deref_array
*array
= nir_deref_as_array(tail
);
3090 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
3091 if (array
->deref_array_type
==
3092 nir_deref_array_type_indirect
) {
3093 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
3100 case nir_deref_type_struct
: {
3101 nir_deref_struct
*deref_struct
=
3102 nir_deref_as_struct(tail
);
3103 offset
= LLVMConstInt(ctx
->ac
.i32
,
3104 deref_struct
->index
, 0);
3108 unreachable("bad deref type");
3110 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3116 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
3117 nir_intrinsic_instr
*instr
,
3120 LLVMValueRef result
;
3121 LLVMValueRef vertex_index
= NULL
;
3122 LLVMValueRef indir_index
= NULL
;
3123 unsigned const_index
= 0;
3124 unsigned location
= instr
->variables
[0]->var
->data
.location
;
3125 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3126 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3127 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3129 get_deref_offset(ctx
, instr
->variables
[0],
3130 false, NULL
, is_patch
? NULL
: &vertex_index
,
3131 &const_index
, &indir_index
);
3133 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, vertex_index
, indir_index
,
3134 const_index
, location
, driver_location
,
3135 instr
->variables
[0]->var
->data
.location_frac
,
3136 instr
->num_components
,
3137 is_patch
, is_compact
, load_inputs
);
3138 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3141 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3142 nir_intrinsic_instr
*instr
)
3144 LLVMValueRef values
[8];
3145 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3146 int ve
= instr
->dest
.ssa
.num_components
;
3147 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3148 LLVMValueRef indir_index
;
3150 unsigned const_index
;
3151 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
3152 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3153 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3154 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3155 &const_index
, &indir_index
);
3157 if (instr
->dest
.ssa
.bit_size
== 64)
3160 switch (instr
->variables
[0]->var
->data
.mode
) {
3161 case nir_var_shader_in
:
3162 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3163 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3164 return load_tess_varyings(ctx
, instr
, true);
3167 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3168 LLVMValueRef indir_index
;
3169 unsigned const_index
, vertex_index
;
3170 get_deref_offset(ctx
, instr
->variables
[0],
3171 false, &vertex_index
, NULL
,
3172 &const_index
, &indir_index
);
3173 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
3174 instr
->variables
[0]->var
->data
.driver_location
,
3175 instr
->variables
[0]->var
->data
.location_frac
, ve
,
3176 vertex_index
, const_index
,
3177 nir2llvmtype(ctx
, instr
->variables
[0]->var
->type
));
3180 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3182 unsigned count
= glsl_count_attribute_slots(
3183 instr
->variables
[0]->var
->type
,
3184 ctx
->stage
== MESA_SHADER_VERTEX
);
3186 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3187 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3188 stride
, false, true);
3190 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3194 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
3198 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3200 unsigned count
= glsl_count_attribute_slots(
3201 instr
->variables
[0]->var
->type
, false);
3203 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3204 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3205 stride
, true, true);
3207 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3211 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
3215 case nir_var_shared
: {
3216 LLVMValueRef address
= build_gep_for_deref(ctx
,
3217 instr
->variables
[0]);
3218 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3219 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3220 get_def_type(ctx
, &instr
->dest
.ssa
),
3223 case nir_var_shader_out
:
3224 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3225 return load_tess_varyings(ctx
, instr
, false);
3228 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3230 unsigned count
= glsl_count_attribute_slots(
3231 instr
->variables
[0]->var
->type
, false);
3233 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3234 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3235 stride
, true, true);
3237 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3241 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3242 ctx
->outputs
[idx
+ chan
+ const_index
* stride
],
3248 unreachable("unhandle variable mode");
3250 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3251 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3255 visit_store_var(struct ac_nir_context
*ctx
,
3256 nir_intrinsic_instr
*instr
)
3258 LLVMValueRef temp_ptr
, value
;
3259 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3260 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3261 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3262 int writemask
= instr
->const_index
[0] << comp
;
3263 LLVMValueRef indir_index
;
3264 unsigned const_index
;
3265 get_deref_offset(ctx
, instr
->variables
[0], false,
3266 NULL
, NULL
, &const_index
, &indir_index
);
3268 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3269 int old_writemask
= writemask
;
3271 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3272 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
3276 for (unsigned chan
= 0; chan
< 4; chan
++) {
3277 if (old_writemask
& (1 << chan
))
3278 writemask
|= 3u << (2 * chan
);
3282 switch (instr
->variables
[0]->var
->data
.mode
) {
3283 case nir_var_shader_out
:
3285 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3286 LLVMValueRef vertex_index
= NULL
;
3287 LLVMValueRef indir_index
= NULL
;
3288 unsigned const_index
= 0;
3289 const unsigned location
= instr
->variables
[0]->var
->data
.location
;
3290 const unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3291 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3292 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3293 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3295 get_deref_offset(ctx
, instr
->variables
[0],
3296 false, NULL
, is_patch
? NULL
: &vertex_index
,
3297 &const_index
, &indir_index
);
3299 ctx
->abi
->store_tcs_outputs(ctx
->abi
, vertex_index
, indir_index
,
3300 const_index
, location
, driver_location
,
3301 src
, comp
, is_patch
, is_compact
, writemask
);
3305 for (unsigned chan
= 0; chan
< 8; chan
++) {
3307 if (!(writemask
& (1 << chan
)))
3310 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3312 if (instr
->variables
[0]->var
->data
.compact
)
3315 unsigned count
= glsl_count_attribute_slots(
3316 instr
->variables
[0]->var
->type
, false);
3318 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3319 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3320 stride
, true, true);
3322 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3323 value
, indir_index
, "");
3324 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3325 count
, stride
, tmp_vec
);
3328 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3330 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3335 for (unsigned chan
= 0; chan
< 8; chan
++) {
3336 if (!(writemask
& (1 << chan
)))
3339 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3341 unsigned count
= glsl_count_attribute_slots(
3342 instr
->variables
[0]->var
->type
, false);
3344 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3345 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3348 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3349 value
, indir_index
, "");
3350 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3353 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3355 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3359 case nir_var_shared
: {
3360 int writemask
= instr
->const_index
[0];
3361 LLVMValueRef address
= build_gep_for_deref(ctx
,
3362 instr
->variables
[0]);
3363 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3364 unsigned components
=
3365 glsl_get_vector_elements(
3366 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3367 if (writemask
== (1 << components
) - 1) {
3368 val
= LLVMBuildBitCast(
3369 ctx
->ac
.builder
, val
,
3370 LLVMGetElementType(LLVMTypeOf(address
)), "");
3371 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3373 for (unsigned chan
= 0; chan
< 4; chan
++) {
3374 if (!(writemask
& (1 << chan
)))
3377 LLVMBuildStructGEP(ctx
->ac
.builder
,
3379 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
3381 src
= LLVMBuildBitCast(
3382 ctx
->ac
.builder
, src
,
3383 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3384 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3394 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3397 case GLSL_SAMPLER_DIM_BUF
:
3399 case GLSL_SAMPLER_DIM_1D
:
3400 return array
? 2 : 1;
3401 case GLSL_SAMPLER_DIM_2D
:
3402 return array
? 3 : 2;
3403 case GLSL_SAMPLER_DIM_MS
:
3404 return array
? 4 : 3;
3405 case GLSL_SAMPLER_DIM_3D
:
3406 case GLSL_SAMPLER_DIM_CUBE
:
3408 case GLSL_SAMPLER_DIM_RECT
:
3409 case GLSL_SAMPLER_DIM_SUBPASS
:
3411 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3421 /* Adjust the sample index according to FMASK.
3423 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3424 * which is the identity mapping. Each nibble says which physical sample
3425 * should be fetched to get that sample.
3427 * For example, 0x11111100 means there are only 2 samples stored and
3428 * the second sample covers 3/4 of the pixel. When reading samples 0
3429 * and 1, return physical sample 0 (determined by the first two 0s
3430 * in FMASK), otherwise return physical sample 1.
3432 * The sample index should be adjusted as follows:
3433 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3435 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3436 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3437 LLVMValueRef coord_z
,
3438 LLVMValueRef sample_index
,
3439 LLVMValueRef fmask_desc_ptr
)
3441 LLVMValueRef fmask_load_address
[4];
3444 fmask_load_address
[0] = coord_x
;
3445 fmask_load_address
[1] = coord_y
;
3447 fmask_load_address
[2] = coord_z
;
3448 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3451 struct ac_image_args args
= {0};
3453 args
.opcode
= ac_image_load
;
3454 args
.da
= coord_z
? true : false;
3455 args
.resource
= fmask_desc_ptr
;
3457 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3459 res
= ac_build_image_opcode(ctx
, &args
);
3461 res
= ac_to_integer(ctx
, res
);
3462 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3463 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3465 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3469 LLVMValueRef sample_index4
=
3470 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3471 LLVMValueRef shifted_fmask
=
3472 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3473 LLVMValueRef final_sample
=
3474 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3476 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3477 * resource descriptor is 0 (invalid),
3479 LLVMValueRef fmask_desc
=
3480 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3483 LLVMValueRef fmask_word1
=
3484 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3487 LLVMValueRef word1_is_nonzero
=
3488 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3489 fmask_word1
, ctx
->i32_0
, "");
3491 /* Replace the MSAA sample index. */
3493 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3494 final_sample
, sample_index
, "");
3495 return sample_index
;
3498 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3499 const nir_intrinsic_instr
*instr
)
3501 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3502 if(instr
->variables
[0]->deref
.child
)
3503 type
= instr
->variables
[0]->deref
.child
->type
;
3505 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3506 LLVMValueRef coords
[4];
3507 LLVMValueRef masks
[] = {
3508 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3509 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3512 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3515 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3516 bool is_array
= glsl_sampler_type_is_array(type
);
3517 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3518 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3519 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3520 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3521 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3522 count
= image_type_to_components_count(dim
, is_array
);
3525 LLVMValueRef fmask_load_address
[3];
3528 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3529 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3531 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3533 fmask_load_address
[2] = NULL
;
3535 for (chan
= 0; chan
< 2; ++chan
)
3536 fmask_load_address
[chan
] =
3537 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3538 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3539 ctx
->ac
.i32
, ""), "");
3540 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3542 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3543 fmask_load_address
[0],
3544 fmask_load_address
[1],
3545 fmask_load_address
[2],
3547 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3549 if (count
== 1 && !gfx9_1d
) {
3550 if (instr
->src
[0].ssa
->num_components
)
3551 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3558 for (chan
= 0; chan
< count
; ++chan
) {
3559 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3562 for (chan
= 0; chan
< 2; ++chan
)
3563 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3564 ctx
->ac
.i32
, ""), "");
3565 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3571 coords
[2] = coords
[1];
3572 coords
[1] = ctx
->ac
.i32_0
;
3574 coords
[1] = ctx
->ac
.i32_0
;
3579 coords
[count
] = sample_index
;
3584 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3587 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3592 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3593 const nir_intrinsic_instr
*instr
)
3595 LLVMValueRef params
[7];
3597 char intrinsic_name
[64];
3598 const nir_variable
*var
= instr
->variables
[0]->var
;
3599 const struct glsl_type
*type
= var
->type
;
3601 if(instr
->variables
[0]->deref
.child
)
3602 type
= instr
->variables
[0]->deref
.child
->type
;
3604 type
= glsl_without_array(type
);
3605 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3606 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3607 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3608 ctx
->ac
.i32_0
, ""); /* vindex */
3609 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3610 params
[3] = ctx
->ac
.i1false
; /* glc */
3611 params
[4] = ctx
->ac
.i1false
; /* slc */
3612 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3615 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3616 res
= ac_to_integer(&ctx
->ac
, res
);
3618 bool is_da
= glsl_sampler_type_is_array(type
) ||
3619 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3620 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3621 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3622 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3623 LLVMValueRef glc
= ctx
->ac
.i1false
;
3624 LLVMValueRef slc
= ctx
->ac
.i1false
;
3626 params
[0] = get_image_coords(ctx
, instr
);
3627 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3628 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3629 if (HAVE_LLVM
<= 0x0309) {
3630 params
[3] = ctx
->ac
.i1false
; /* r128 */
3635 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3642 ac_get_image_intr_name("llvm.amdgcn.image.load",
3643 ctx
->ac
.v4f32
, /* vdata */
3644 LLVMTypeOf(params
[0]), /* coords */
3645 LLVMTypeOf(params
[1]), /* rsrc */
3646 intrinsic_name
, sizeof(intrinsic_name
));
3648 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3649 params
, 7, AC_FUNC_ATTR_READONLY
);
3651 return ac_to_integer(&ctx
->ac
, res
);
3654 static void visit_image_store(struct ac_nir_context
*ctx
,
3655 nir_intrinsic_instr
*instr
)
3657 LLVMValueRef params
[8];
3658 char intrinsic_name
[64];
3659 const nir_variable
*var
= instr
->variables
[0]->var
;
3660 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3661 LLVMValueRef glc
= ctx
->ac
.i1false
;
3662 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3664 glc
= ctx
->ac
.i1true
;
3666 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3667 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3668 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3669 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3670 ctx
->ac
.i32_0
, ""); /* vindex */
3671 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3672 params
[4] = glc
; /* glc */
3673 params
[5] = ctx
->ac
.i1false
; /* slc */
3674 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3677 bool is_da
= glsl_sampler_type_is_array(type
) ||
3678 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3679 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3680 LLVMValueRef slc
= ctx
->ac
.i1false
;
3682 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3683 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3684 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3685 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3686 if (HAVE_LLVM
<= 0x0309) {
3687 params
[4] = ctx
->ac
.i1false
; /* r128 */
3692 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3699 ac_get_image_intr_name("llvm.amdgcn.image.store",
3700 LLVMTypeOf(params
[0]), /* vdata */
3701 LLVMTypeOf(params
[1]), /* coords */
3702 LLVMTypeOf(params
[2]), /* rsrc */
3703 intrinsic_name
, sizeof(intrinsic_name
));
3705 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3711 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3712 const nir_intrinsic_instr
*instr
)
3714 LLVMValueRef params
[7];
3715 int param_count
= 0;
3716 const nir_variable
*var
= instr
->variables
[0]->var
;
3718 const char *atomic_name
;
3719 char intrinsic_name
[41];
3720 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3721 MAYBE_UNUSED
int length
;
3723 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3725 switch (instr
->intrinsic
) {
3726 case nir_intrinsic_image_atomic_add
:
3727 atomic_name
= "add";
3729 case nir_intrinsic_image_atomic_min
:
3730 atomic_name
= is_unsigned
? "umin" : "smin";
3732 case nir_intrinsic_image_atomic_max
:
3733 atomic_name
= is_unsigned
? "umax" : "smax";
3735 case nir_intrinsic_image_atomic_and
:
3736 atomic_name
= "and";
3738 case nir_intrinsic_image_atomic_or
:
3741 case nir_intrinsic_image_atomic_xor
:
3742 atomic_name
= "xor";
3744 case nir_intrinsic_image_atomic_exchange
:
3745 atomic_name
= "swap";
3747 case nir_intrinsic_image_atomic_comp_swap
:
3748 atomic_name
= "cmpswap";
3754 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3755 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3756 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3758 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3759 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3761 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3762 ctx
->ac
.i32_0
, ""); /* vindex */
3763 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3764 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3766 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3767 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3769 char coords_type
[8];
3771 bool da
= glsl_sampler_type_is_array(type
) ||
3772 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3774 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3775 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3777 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3778 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3779 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3781 build_int_type_name(LLVMTypeOf(coords
),
3782 coords_type
, sizeof(coords_type
));
3784 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3785 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3788 assert(length
< sizeof(intrinsic_name
));
3789 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3792 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3793 const nir_intrinsic_instr
*instr
)
3796 const nir_variable
*var
= instr
->variables
[0]->var
;
3797 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3798 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3799 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3800 if(instr
->variables
[0]->deref
.child
)
3801 type
= instr
->variables
[0]->deref
.child
->type
;
3803 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3804 return get_buffer_size(ctx
,
3805 get_sampler_desc(ctx
, instr
->variables
[0],
3806 AC_DESC_BUFFER
, NULL
, true, false), true);
3808 struct ac_image_args args
= { 0 };
3812 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3813 args
.opcode
= ac_image_get_resinfo
;
3814 args
.addr
= ctx
->ac
.i32_0
;
3816 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3818 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3820 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3821 glsl_sampler_type_is_array(type
)) {
3822 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3823 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3824 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3825 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3827 if (ctx
->ac
.chip_class
>= GFX9
&&
3828 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3829 glsl_sampler_type_is_array(type
)) {
3830 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3831 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3838 #define NOOP_WAITCNT 0xf7f
3839 #define LGKM_CNT 0x07f
3840 #define VM_CNT 0xf70
3842 static void emit_membar(struct nir_to_llvm_context
*ctx
,
3843 const nir_intrinsic_instr
*instr
)
3845 unsigned waitcnt
= NOOP_WAITCNT
;
3847 switch (instr
->intrinsic
) {
3848 case nir_intrinsic_memory_barrier
:
3849 case nir_intrinsic_group_memory_barrier
:
3850 waitcnt
&= VM_CNT
& LGKM_CNT
;
3852 case nir_intrinsic_memory_barrier_atomic_counter
:
3853 case nir_intrinsic_memory_barrier_buffer
:
3854 case nir_intrinsic_memory_barrier_image
:
3857 case nir_intrinsic_memory_barrier_shared
:
3858 waitcnt
&= LGKM_CNT
;
3863 if (waitcnt
!= NOOP_WAITCNT
)
3864 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3867 static void emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3869 /* SI only (thanks to a hw bug workaround):
3870 * The real barrier instruction isn’t needed, because an entire patch
3871 * always fits into a single wave.
3873 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
3874 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
3877 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
3878 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3881 static void emit_discard(struct ac_nir_context
*ctx
,
3882 const nir_intrinsic_instr
*instr
)
3886 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3887 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3888 get_src(ctx
, instr
->src
[0]),
3891 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3892 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
3895 ac_build_kill_if_false(&ctx
->ac
, cond
);
3899 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3901 LLVMValueRef result
;
3902 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3903 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3904 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3906 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3909 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3910 const nir_intrinsic_instr
*instr
)
3912 LLVMValueRef ptr
, result
;
3913 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3914 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3916 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3917 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3918 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3920 LLVMAtomicOrderingSequentiallyConsistent
,
3921 LLVMAtomicOrderingSequentiallyConsistent
,
3924 LLVMAtomicRMWBinOp op
;
3925 switch (instr
->intrinsic
) {
3926 case nir_intrinsic_var_atomic_add
:
3927 op
= LLVMAtomicRMWBinOpAdd
;
3929 case nir_intrinsic_var_atomic_umin
:
3930 op
= LLVMAtomicRMWBinOpUMin
;
3932 case nir_intrinsic_var_atomic_umax
:
3933 op
= LLVMAtomicRMWBinOpUMax
;
3935 case nir_intrinsic_var_atomic_imin
:
3936 op
= LLVMAtomicRMWBinOpMin
;
3938 case nir_intrinsic_var_atomic_imax
:
3939 op
= LLVMAtomicRMWBinOpMax
;
3941 case nir_intrinsic_var_atomic_and
:
3942 op
= LLVMAtomicRMWBinOpAnd
;
3944 case nir_intrinsic_var_atomic_or
:
3945 op
= LLVMAtomicRMWBinOpOr
;
3947 case nir_intrinsic_var_atomic_xor
:
3948 op
= LLVMAtomicRMWBinOpXor
;
3950 case nir_intrinsic_var_atomic_exchange
:
3951 op
= LLVMAtomicRMWBinOpXchg
;
3957 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3958 LLVMAtomicOrderingSequentiallyConsistent
,
3964 #define INTERP_CENTER 0
3965 #define INTERP_CENTROID 1
3966 #define INTERP_SAMPLE 2
3968 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3969 enum glsl_interp_mode interp
, unsigned location
)
3972 case INTERP_MODE_FLAT
:
3975 case INTERP_MODE_SMOOTH
:
3976 case INTERP_MODE_NONE
:
3977 if (location
== INTERP_CENTER
)
3978 return ctx
->persp_center
;
3979 else if (location
== INTERP_CENTROID
)
3980 return ctx
->persp_centroid
;
3981 else if (location
== INTERP_SAMPLE
)
3982 return ctx
->persp_sample
;
3984 case INTERP_MODE_NOPERSPECTIVE
:
3985 if (location
== INTERP_CENTER
)
3986 return ctx
->linear_center
;
3987 else if (location
== INTERP_CENTROID
)
3988 return ctx
->linear_centroid
;
3989 else if (location
== INTERP_SAMPLE
)
3990 return ctx
->linear_sample
;
3996 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3997 LLVMValueRef sample_id
)
3999 LLVMValueRef result
;
4000 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
4002 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
4003 const_array(ctx
->ac
.v2f32
, 64), "");
4005 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
4006 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
4011 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
4013 LLVMValueRef values
[2];
4015 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
4016 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
4017 return ac_build_gather_values(&ctx
->ac
, values
, 2);
4020 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
4021 const nir_intrinsic_instr
*instr
)
4023 LLVMValueRef result
[4];
4024 LLVMValueRef interp_param
, attr_number
;
4027 LLVMValueRef src_c0
= NULL
;
4028 LLVMValueRef src_c1
= NULL
;
4029 LLVMValueRef src0
= NULL
;
4030 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
4031 switch (instr
->intrinsic
) {
4032 case nir_intrinsic_interp_var_at_centroid
:
4033 location
= INTERP_CENTROID
;
4035 case nir_intrinsic_interp_var_at_sample
:
4036 case nir_intrinsic_interp_var_at_offset
:
4037 location
= INTERP_CENTER
;
4038 src0
= get_src(ctx
->nir
, instr
->src
[0]);
4044 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
4045 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_0
, ""));
4046 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_1
, ""));
4047 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
4048 LLVMValueRef sample_position
;
4049 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
4051 /* fetch sample ID */
4052 sample_position
= load_sample_position(ctx
, src0
);
4054 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_0
, "");
4055 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
4056 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_1
, "");
4057 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
4059 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
4060 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
4062 if (location
== INTERP_CENTER
) {
4063 LLVMValueRef ij_out
[2];
4064 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
4067 * take the I then J parameters, and the DDX/Y for it, and
4068 * calculate the IJ inputs for the interpolator.
4069 * temp1 = ddx * offset/sample.x + I;
4070 * interp_param.I = ddy * offset/sample.y + temp1;
4071 * temp1 = ddx * offset/sample.x + J;
4072 * interp_param.J = ddy * offset/sample.y + temp1;
4074 for (unsigned i
= 0; i
< 2; i
++) {
4075 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
4076 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
4077 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
4078 ddxy_out
, ix_ll
, "");
4079 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
4080 ddxy_out
, iy_ll
, "");
4081 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
4082 interp_param
, ix_ll
, "");
4083 LLVMValueRef temp1
, temp2
;
4085 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
4088 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
4089 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
4091 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
4092 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
4094 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
4095 temp2
, ctx
->ac
.i32
, "");
4097 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
4101 for (chan
= 0; chan
< 4; chan
++) {
4102 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
4105 interp_param
= LLVMBuildBitCast(ctx
->builder
,
4106 interp_param
, ctx
->ac
.v2f32
, "");
4107 LLVMValueRef i
= LLVMBuildExtractElement(
4108 ctx
->builder
, interp_param
, ctx
->ac
.i32_0
, "");
4109 LLVMValueRef j
= LLVMBuildExtractElement(
4110 ctx
->builder
, interp_param
, ctx
->ac
.i32_1
, "");
4112 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4113 llvm_chan
, attr_number
,
4114 ctx
->prim_mask
, i
, j
);
4116 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4117 LLVMConstInt(ctx
->ac
.i32
, 2, false),
4118 llvm_chan
, attr_number
,
4122 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
4123 instr
->variables
[0]->var
->data
.location_frac
);
4127 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
4129 LLVMValueRef gs_next_vertex
;
4130 LLVMValueRef can_emit
;
4132 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4134 /* Write vertex attribute values to GSVS ring */
4135 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
4136 ctx
->gs_next_vertex
,
4139 /* If this thread has already emitted the declared maximum number of
4140 * vertices, kill it: excessive vertex emissions are not supposed to
4141 * have any effect, and GS threads have no externally observable
4142 * effects other than emitting vertices.
4144 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
4145 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
4146 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4148 /* loop num outputs */
4150 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4151 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
4156 if (!(ctx
->output_mask
& (1ull << i
)))
4159 if (i
== VARYING_SLOT_CLIP_DIST0
) {
4160 /* pack clip and cull into a single set of slots */
4161 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4165 for (unsigned j
= 0; j
< length
; j
++) {
4166 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
4168 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
4169 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
4170 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
4172 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
4174 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
4176 voffset
, ctx
->gs2vs_offset
, 0,
4182 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4184 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4186 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4190 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
4191 const nir_intrinsic_instr
*instr
)
4193 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4197 load_tess_coord(struct ac_shader_abi
*abi
, LLVMTypeRef type
,
4198 unsigned num_components
)
4200 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4202 LLVMValueRef coord
[4] = {
4209 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4210 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->ac
.f32_1
,
4211 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4213 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, num_components
);
4214 return LLVMBuildBitCast(ctx
->builder
, result
, type
, "");
4218 load_patch_vertices_in(struct ac_shader_abi
*abi
)
4220 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4221 return LLVMConstInt(ctx
->ac
.i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
4224 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4225 nir_intrinsic_instr
*instr
)
4227 LLVMValueRef result
= NULL
;
4229 switch (instr
->intrinsic
) {
4230 case nir_intrinsic_load_work_group_id
: {
4231 LLVMValueRef values
[3];
4233 for (int i
= 0; i
< 3; i
++) {
4234 values
[i
] = ctx
->nctx
->workgroup_ids
[i
] ?
4235 ctx
->nctx
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
4238 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
4241 case nir_intrinsic_load_base_vertex
: {
4242 result
= ctx
->abi
->base_vertex
;
4245 case nir_intrinsic_load_vertex_id_zero_base
: {
4246 result
= ctx
->abi
->vertex_id
;
4249 case nir_intrinsic_load_local_invocation_id
: {
4250 result
= ctx
->nctx
->local_invocation_ids
;
4253 case nir_intrinsic_load_base_instance
:
4254 result
= ctx
->abi
->start_instance
;
4256 case nir_intrinsic_load_draw_id
:
4257 result
= ctx
->abi
->draw_id
;
4259 case nir_intrinsic_load_view_index
:
4260 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4262 case nir_intrinsic_load_invocation_id
:
4263 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4264 result
= unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
4266 result
= ctx
->abi
->gs_invocation_id
;
4268 case nir_intrinsic_load_primitive_id
:
4269 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4270 result
= ctx
->abi
->gs_prim_id
;
4271 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4272 result
= ctx
->abi
->tcs_patch_id
;
4273 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4274 result
= ctx
->abi
->tes_patch_id
;
4276 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4278 case nir_intrinsic_load_sample_id
:
4279 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4281 case nir_intrinsic_load_sample_pos
:
4282 result
= load_sample_pos(ctx
);
4284 case nir_intrinsic_load_sample_mask_in
:
4285 result
= ctx
->abi
->sample_coverage
;
4287 case nir_intrinsic_load_frag_coord
: {
4288 LLVMValueRef values
[4] = {
4289 ctx
->abi
->frag_pos
[0],
4290 ctx
->abi
->frag_pos
[1],
4291 ctx
->abi
->frag_pos
[2],
4292 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4294 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4297 case nir_intrinsic_load_front_face
:
4298 result
= ctx
->abi
->front_face
;
4300 case nir_intrinsic_load_instance_id
:
4301 result
= ctx
->abi
->instance_id
;
4303 case nir_intrinsic_load_num_work_groups
:
4304 result
= ctx
->nctx
->num_work_groups
;
4306 case nir_intrinsic_load_local_invocation_index
:
4307 result
= visit_load_local_invocation_index(ctx
->nctx
);
4309 case nir_intrinsic_load_push_constant
:
4310 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4312 case nir_intrinsic_vulkan_resource_index
:
4313 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4315 case nir_intrinsic_vulkan_resource_reindex
:
4316 result
= visit_vulkan_resource_reindex(ctx
->nctx
, instr
);
4318 case nir_intrinsic_store_ssbo
:
4319 visit_store_ssbo(ctx
, instr
);
4321 case nir_intrinsic_load_ssbo
:
4322 result
= visit_load_buffer(ctx
, instr
);
4324 case nir_intrinsic_ssbo_atomic_add
:
4325 case nir_intrinsic_ssbo_atomic_imin
:
4326 case nir_intrinsic_ssbo_atomic_umin
:
4327 case nir_intrinsic_ssbo_atomic_imax
:
4328 case nir_intrinsic_ssbo_atomic_umax
:
4329 case nir_intrinsic_ssbo_atomic_and
:
4330 case nir_intrinsic_ssbo_atomic_or
:
4331 case nir_intrinsic_ssbo_atomic_xor
:
4332 case nir_intrinsic_ssbo_atomic_exchange
:
4333 case nir_intrinsic_ssbo_atomic_comp_swap
:
4334 result
= visit_atomic_ssbo(ctx
, instr
);
4336 case nir_intrinsic_load_ubo
:
4337 result
= visit_load_ubo_buffer(ctx
, instr
);
4339 case nir_intrinsic_get_buffer_size
:
4340 result
= visit_get_buffer_size(ctx
, instr
);
4342 case nir_intrinsic_load_var
:
4343 result
= visit_load_var(ctx
, instr
);
4345 case nir_intrinsic_store_var
:
4346 visit_store_var(ctx
, instr
);
4348 case nir_intrinsic_image_load
:
4349 result
= visit_image_load(ctx
, instr
);
4351 case nir_intrinsic_image_store
:
4352 visit_image_store(ctx
, instr
);
4354 case nir_intrinsic_image_atomic_add
:
4355 case nir_intrinsic_image_atomic_min
:
4356 case nir_intrinsic_image_atomic_max
:
4357 case nir_intrinsic_image_atomic_and
:
4358 case nir_intrinsic_image_atomic_or
:
4359 case nir_intrinsic_image_atomic_xor
:
4360 case nir_intrinsic_image_atomic_exchange
:
4361 case nir_intrinsic_image_atomic_comp_swap
:
4362 result
= visit_image_atomic(ctx
, instr
);
4364 case nir_intrinsic_image_size
:
4365 result
= visit_image_size(ctx
, instr
);
4367 case nir_intrinsic_discard
:
4368 case nir_intrinsic_discard_if
:
4369 emit_discard(ctx
, instr
);
4371 case nir_intrinsic_memory_barrier
:
4372 case nir_intrinsic_group_memory_barrier
:
4373 case nir_intrinsic_memory_barrier_atomic_counter
:
4374 case nir_intrinsic_memory_barrier_buffer
:
4375 case nir_intrinsic_memory_barrier_image
:
4376 case nir_intrinsic_memory_barrier_shared
:
4377 emit_membar(ctx
->nctx
, instr
);
4379 case nir_intrinsic_barrier
:
4380 emit_barrier(&ctx
->ac
, ctx
->stage
);
4382 case nir_intrinsic_var_atomic_add
:
4383 case nir_intrinsic_var_atomic_imin
:
4384 case nir_intrinsic_var_atomic_umin
:
4385 case nir_intrinsic_var_atomic_imax
:
4386 case nir_intrinsic_var_atomic_umax
:
4387 case nir_intrinsic_var_atomic_and
:
4388 case nir_intrinsic_var_atomic_or
:
4389 case nir_intrinsic_var_atomic_xor
:
4390 case nir_intrinsic_var_atomic_exchange
:
4391 case nir_intrinsic_var_atomic_comp_swap
:
4392 result
= visit_var_atomic(ctx
->nctx
, instr
);
4394 case nir_intrinsic_interp_var_at_centroid
:
4395 case nir_intrinsic_interp_var_at_sample
:
4396 case nir_intrinsic_interp_var_at_offset
:
4397 result
= visit_interp(ctx
->nctx
, instr
);
4399 case nir_intrinsic_emit_vertex
:
4400 assert(instr
->const_index
[0] == 0);
4401 ctx
->abi
->emit_vertex(ctx
->abi
, 0, ctx
->outputs
);
4403 case nir_intrinsic_end_primitive
:
4404 visit_end_primitive(ctx
->nctx
, instr
);
4406 case nir_intrinsic_load_tess_coord
: {
4407 LLVMTypeRef type
= ctx
->nctx
?
4408 get_def_type(ctx
->nctx
->nir
, &instr
->dest
.ssa
) :
4410 result
= ctx
->abi
->load_tess_coord(ctx
->abi
, type
, instr
->num_components
);
4413 case nir_intrinsic_load_tess_level_outer
:
4414 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
4416 case nir_intrinsic_load_tess_level_inner
:
4417 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
4419 case nir_intrinsic_load_patch_vertices_in
:
4420 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
4423 fprintf(stderr
, "Unknown intrinsic: ");
4424 nir_print_instr(&instr
->instr
, stderr
);
4425 fprintf(stderr
, "\n");
4429 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4433 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4434 LLVMValueRef buffer_ptr
, bool write
)
4436 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4438 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4439 ctx
->shader_info
->fs
.writes_memory
= true;
4441 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4444 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef buffer_ptr
)
4446 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4448 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4451 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4452 unsigned descriptor_set
,
4453 unsigned base_index
,
4454 unsigned constant_index
,
4456 enum ac_descriptor_type desc_type
,
4457 bool image
, bool write
)
4459 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4460 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4461 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4462 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4463 unsigned offset
= binding
->offset
;
4464 unsigned stride
= binding
->size
;
4466 LLVMBuilderRef builder
= ctx
->builder
;
4469 assert(base_index
< layout
->binding_count
);
4471 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4472 ctx
->shader_info
->fs
.writes_memory
= true;
4474 switch (desc_type
) {
4476 type
= ctx
->ac
.v8i32
;
4480 type
= ctx
->ac
.v8i32
;
4484 case AC_DESC_SAMPLER
:
4485 type
= ctx
->ac
.v4i32
;
4486 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4491 case AC_DESC_BUFFER
:
4492 type
= ctx
->ac
.v4i32
;
4496 unreachable("invalid desc_type\n");
4499 offset
+= constant_index
* stride
;
4501 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4502 (!index
|| binding
->immutable_samplers_equal
)) {
4503 if (binding
->immutable_samplers_equal
)
4506 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4508 LLVMValueRef constants
[] = {
4509 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4510 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4511 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4512 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4514 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4517 assert(stride
% type_size
== 0);
4520 index
= ctx
->ac
.i32_0
;
4522 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4524 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4525 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4527 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4530 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4531 const nir_deref_var
*deref
,
4532 enum ac_descriptor_type desc_type
,
4533 const nir_tex_instr
*tex_instr
,
4534 bool image
, bool write
)
4536 LLVMValueRef index
= NULL
;
4537 unsigned constant_index
= 0;
4538 unsigned descriptor_set
;
4539 unsigned base_index
;
4542 assert(tex_instr
&& !image
);
4544 base_index
= tex_instr
->sampler_index
;
4546 const nir_deref
*tail
= &deref
->deref
;
4547 while (tail
->child
) {
4548 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4549 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4554 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4556 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4557 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4559 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4560 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4565 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4568 constant_index
+= child
->base_offset
* array_size
;
4570 tail
= &child
->deref
;
4572 descriptor_set
= deref
->var
->data
.descriptor_set
;
4573 base_index
= deref
->var
->data
.binding
;
4576 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4579 constant_index
, index
,
4580 desc_type
, image
, write
);
4583 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4584 struct ac_image_args
*args
,
4585 const nir_tex_instr
*instr
,
4587 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4588 LLVMValueRef
*param
, unsigned count
,
4591 unsigned is_rect
= 0;
4592 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4594 if (op
== nir_texop_lod
)
4596 /* Pad to power of two vector */
4597 while (count
< util_next_power_of_two(count
))
4598 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4601 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4603 args
->addr
= param
[0];
4605 args
->resource
= res_ptr
;
4606 args
->sampler
= samp_ptr
;
4608 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4609 args
->addr
= param
[0];
4613 args
->dmask
= dmask
;
4614 args
->unorm
= is_rect
;
4618 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4621 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4622 * filtering manually. The driver sets img7 to a mask clearing
4623 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4624 * s_and_b32 samp0, samp0, img7
4627 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4629 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4630 LLVMValueRef res
, LLVMValueRef samp
)
4632 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4633 LLVMValueRef img7
, samp0
;
4635 if (ctx
->ac
.chip_class
>= VI
)
4638 img7
= LLVMBuildExtractElement(builder
, res
,
4639 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4640 samp0
= LLVMBuildExtractElement(builder
, samp
,
4641 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4642 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4643 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4644 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4647 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4648 nir_tex_instr
*instr
,
4649 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4650 LLVMValueRef
*fmask_ptr
)
4652 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4653 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4655 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4658 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4660 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4661 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4662 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4664 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4665 instr
->op
== nir_texop_samples_identical
))
4666 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4669 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4672 coord
= ac_to_float(ctx
, coord
);
4673 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4674 coord
= ac_to_integer(ctx
, coord
);
4678 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4680 LLVMValueRef result
= NULL
;
4681 struct ac_image_args args
= { 0 };
4682 unsigned dmask
= 0xf;
4683 LLVMValueRef address
[16];
4684 LLVMValueRef coords
[5];
4685 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4686 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4687 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4688 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4689 LLVMValueRef derivs
[6];
4690 unsigned chan
, count
= 0;
4691 unsigned const_src
= 0, num_deriv_comp
= 0;
4692 bool lod_is_zero
= false;
4694 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4696 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4697 switch (instr
->src
[i
].src_type
) {
4698 case nir_tex_src_coord
:
4699 coord
= get_src(ctx
, instr
->src
[i
].src
);
4701 case nir_tex_src_projector
:
4703 case nir_tex_src_comparator
:
4704 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4706 case nir_tex_src_offset
:
4707 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4710 case nir_tex_src_bias
:
4711 bias
= get_src(ctx
, instr
->src
[i
].src
);
4713 case nir_tex_src_lod
: {
4714 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4716 if (val
&& val
->i32
[0] == 0)
4718 lod
= get_src(ctx
, instr
->src
[i
].src
);
4721 case nir_tex_src_ms_index
:
4722 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4724 case nir_tex_src_ms_mcs
:
4726 case nir_tex_src_ddx
:
4727 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4728 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4730 case nir_tex_src_ddy
:
4731 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4733 case nir_tex_src_texture_offset
:
4734 case nir_tex_src_sampler_offset
:
4735 case nir_tex_src_plane
:
4741 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4742 result
= get_buffer_size(ctx
, res_ptr
, true);
4746 if (instr
->op
== nir_texop_texture_samples
) {
4747 LLVMValueRef res
, samples
, is_msaa
;
4748 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4749 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4750 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4751 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4752 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4753 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4754 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4755 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4756 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4758 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4759 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4760 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4761 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4762 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4764 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4771 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4772 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4774 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4775 LLVMValueRef offset
[3], pack
;
4776 for (chan
= 0; chan
< 3; ++chan
)
4777 offset
[chan
] = ctx
->ac
.i32_0
;
4780 for (chan
= 0; chan
< ac_get_llvm_num_components(offsets
); chan
++) {
4781 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4782 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4783 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4785 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4786 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4788 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4789 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4790 address
[count
++] = pack
;
4793 /* pack LOD bias value */
4794 if (instr
->op
== nir_texop_txb
&& bias
) {
4795 address
[count
++] = bias
;
4798 /* Pack depth comparison value */
4799 if (instr
->is_shadow
&& comparator
) {
4800 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4801 ac_llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4803 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
4804 * so the depth comparison value isn't clamped for Z16 and
4805 * Z24 anymore. Do it manually here.
4807 * It's unnecessary if the original texture format was
4808 * Z32_FLOAT, but we don't know that here.
4810 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
4811 z
= ac_build_clamp(&ctx
->ac
, z
);
4813 address
[count
++] = z
;
4816 /* pack derivatives */
4818 int num_src_deriv_channels
, num_dest_deriv_channels
;
4819 switch (instr
->sampler_dim
) {
4820 case GLSL_SAMPLER_DIM_3D
:
4821 case GLSL_SAMPLER_DIM_CUBE
:
4823 num_src_deriv_channels
= 3;
4824 num_dest_deriv_channels
= 3;
4826 case GLSL_SAMPLER_DIM_2D
:
4828 num_src_deriv_channels
= 2;
4829 num_dest_deriv_channels
= 2;
4832 case GLSL_SAMPLER_DIM_1D
:
4833 num_src_deriv_channels
= 1;
4834 if (ctx
->ac
.chip_class
>= GFX9
) {
4835 num_dest_deriv_channels
= 2;
4838 num_dest_deriv_channels
= 1;
4844 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4845 derivs
[i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4846 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4848 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4849 derivs
[i
] = ctx
->ac
.f32_0
;
4850 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4854 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4855 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4856 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4857 if (instr
->coord_components
== 3)
4858 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4859 ac_prepare_cube_coords(&ctx
->ac
,
4860 instr
->op
== nir_texop_txd
, instr
->is_array
,
4861 instr
->op
== nir_texop_lod
, coords
, derivs
);
4867 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4868 address
[count
++] = derivs
[i
];
4871 /* Pack texture coordinates */
4873 address
[count
++] = coords
[0];
4874 if (instr
->coord_components
> 1) {
4875 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4876 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4878 address
[count
++] = coords
[1];
4880 if (instr
->coord_components
> 2) {
4881 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4882 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4883 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4884 instr
->op
!= nir_texop_txf
) {
4885 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4887 address
[count
++] = coords
[2];
4890 if (ctx
->ac
.chip_class
>= GFX9
) {
4891 LLVMValueRef filler
;
4892 if (instr
->op
== nir_texop_txf
)
4893 filler
= ctx
->ac
.i32_0
;
4895 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4897 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4898 /* No nir_texop_lod, because it does not take a slice
4899 * even with array textures. */
4900 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
4901 address
[count
] = address
[count
- 1];
4902 address
[count
- 1] = filler
;
4905 address
[count
++] = filler
;
4911 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4912 instr
->op
== nir_texop_txf
)) {
4913 address
[count
++] = lod
;
4914 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4915 address
[count
++] = sample_index
;
4916 } else if(instr
->op
== nir_texop_txs
) {
4919 address
[count
++] = lod
;
4921 address
[count
++] = ctx
->ac
.i32_0
;
4924 for (chan
= 0; chan
< count
; chan
++) {
4925 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4926 address
[chan
], ctx
->ac
.i32
, "");
4929 if (instr
->op
== nir_texop_samples_identical
) {
4930 LLVMValueRef txf_address
[4];
4931 struct ac_image_args txf_args
= { 0 };
4932 unsigned txf_count
= count
;
4933 memcpy(txf_address
, address
, sizeof(txf_address
));
4935 if (!instr
->is_array
)
4936 txf_address
[2] = ctx
->ac
.i32_0
;
4937 txf_address
[3] = ctx
->ac
.i32_0
;
4939 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4941 txf_address
, txf_count
, 0xf);
4943 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4945 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4946 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4950 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4951 instr
->op
!= nir_texop_txs
) {
4952 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4953 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4956 instr
->is_array
? address
[2] : NULL
,
4957 address
[sample_chan
],
4961 if (offsets
&& instr
->op
== nir_texop_txf
) {
4962 nir_const_value
*const_offset
=
4963 nir_src_as_const_value(instr
->src
[const_src
].src
);
4964 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4965 assert(const_offset
);
4966 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4967 if (num_offsets
> 2)
4968 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4969 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4970 if (num_offsets
> 1)
4971 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4972 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4973 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4974 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4978 /* TODO TG4 support */
4979 if (instr
->op
== nir_texop_tg4
) {
4980 if (instr
->is_shadow
)
4983 dmask
= 1 << instr
->component
;
4985 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4986 res_ptr
, samp_ptr
, address
, count
, dmask
);
4988 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4990 if (instr
->op
== nir_texop_query_levels
)
4991 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4992 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4993 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4994 instr
->op
!= nir_texop_tg4
)
4995 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4996 else if (instr
->op
== nir_texop_txs
&&
4997 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4999 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
5000 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
5001 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
5002 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
5003 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
5004 } else if (ctx
->ac
.chip_class
>= GFX9
&&
5005 instr
->op
== nir_texop_txs
&&
5006 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
5008 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
5009 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
5010 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
5012 } else if (instr
->dest
.ssa
.num_components
!= 4)
5013 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
5017 assert(instr
->dest
.is_ssa
);
5018 result
= ac_to_integer(&ctx
->ac
, result
);
5019 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
5024 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
5026 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
5027 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
5029 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
5030 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
5033 static void visit_post_phi(struct ac_nir_context
*ctx
,
5034 nir_phi_instr
*instr
,
5035 LLVMValueRef llvm_phi
)
5037 nir_foreach_phi_src(src
, instr
) {
5038 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
5039 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
5041 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
5045 static void phi_post_pass(struct ac_nir_context
*ctx
)
5047 struct hash_entry
*entry
;
5048 hash_table_foreach(ctx
->phis
, entry
) {
5049 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
5050 (LLVMValueRef
)entry
->data
);
5055 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
5056 const nir_ssa_undef_instr
*instr
)
5058 unsigned num_components
= instr
->def
.num_components
;
5059 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
5062 if (num_components
== 1)
5063 undef
= LLVMGetUndef(type
);
5065 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
5067 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
5070 static void visit_jump(struct ac_nir_context
*ctx
,
5071 const nir_jump_instr
*instr
)
5073 switch (instr
->type
) {
5074 case nir_jump_break
:
5075 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
5076 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5078 case nir_jump_continue
:
5079 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5080 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5083 fprintf(stderr
, "Unknown NIR jump instr: ");
5084 nir_print_instr(&instr
->instr
, stderr
);
5085 fprintf(stderr
, "\n");
5090 static void visit_cf_list(struct ac_nir_context
*ctx
,
5091 struct exec_list
*list
);
5093 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5095 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
5096 nir_foreach_instr(instr
, block
)
5098 switch (instr
->type
) {
5099 case nir_instr_type_alu
:
5100 visit_alu(ctx
, nir_instr_as_alu(instr
));
5102 case nir_instr_type_load_const
:
5103 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5105 case nir_instr_type_intrinsic
:
5106 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5108 case nir_instr_type_tex
:
5109 visit_tex(ctx
, nir_instr_as_tex(instr
));
5111 case nir_instr_type_phi
:
5112 visit_phi(ctx
, nir_instr_as_phi(instr
));
5114 case nir_instr_type_ssa_undef
:
5115 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5117 case nir_instr_type_jump
:
5118 visit_jump(ctx
, nir_instr_as_jump(instr
));
5121 fprintf(stderr
, "Unknown NIR instr type: ");
5122 nir_print_instr(instr
, stderr
);
5123 fprintf(stderr
, "\n");
5128 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
5131 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5133 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5135 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5136 LLVMBasicBlockRef merge_block
=
5137 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5138 LLVMBasicBlockRef if_block
=
5139 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5140 LLVMBasicBlockRef else_block
= merge_block
;
5141 if (!exec_list_is_empty(&if_stmt
->else_list
))
5142 else_block
= LLVMAppendBasicBlockInContext(
5143 ctx
->ac
.context
, fn
, "");
5145 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
5147 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
5149 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
5150 visit_cf_list(ctx
, &if_stmt
->then_list
);
5151 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5152 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5154 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5155 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
5156 visit_cf_list(ctx
, &if_stmt
->else_list
);
5157 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5158 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5161 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
5164 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5166 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5167 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
5168 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
5170 ctx
->continue_block
=
5171 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5173 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5175 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5176 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
5177 visit_cf_list(ctx
, &loop
->body
);
5179 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5180 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5181 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
5183 ctx
->continue_block
= continue_parent
;
5184 ctx
->break_block
= break_parent
;
5187 static void visit_cf_list(struct ac_nir_context
*ctx
,
5188 struct exec_list
*list
)
5190 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5192 switch (node
->type
) {
5193 case nir_cf_node_block
:
5194 visit_block(ctx
, nir_cf_node_as_block(node
));
5197 case nir_cf_node_if
:
5198 visit_if(ctx
, nir_cf_node_as_if(node
));
5201 case nir_cf_node_loop
:
5202 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5212 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
5213 struct nir_variable
*variable
)
5215 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5216 LLVMValueRef t_offset
;
5217 LLVMValueRef t_list
;
5219 LLVMValueRef buffer_index
;
5220 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5221 int idx
= variable
->data
.location
;
5222 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5224 variable
->data
.driver_location
= idx
* 4;
5226 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
5227 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5228 ctx
->abi
.start_instance
, "");
5229 if (ctx
->options
->key
.vs
.as_ls
) {
5230 ctx
->shader_info
->vs
.vgpr_comp_cnt
=
5231 MAX2(2, ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5233 ctx
->shader_info
->vs
.vgpr_comp_cnt
=
5234 MAX2(1, ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5237 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5238 ctx
->abi
.base_vertex
, "");
5240 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5241 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5243 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5245 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5250 for (unsigned chan
= 0; chan
< 4; chan
++) {
5251 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5252 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5253 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5254 input
, llvm_chan
, ""));
5259 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5261 LLVMValueRef interp_param
,
5262 LLVMValueRef prim_mask
,
5263 LLVMValueRef result
[4])
5265 LLVMValueRef attr_number
;
5268 bool interp
= interp_param
!= NULL
;
5270 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5272 /* fs.constant returns the param from the middle vertex, so it's not
5273 * really useful for flat shading. It's meant to be used for custom
5274 * interpolation (but the intrinsic can't fetch from the other two
5277 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5278 * to do the right thing. The only reason we use fs.constant is that
5279 * fs.interp cannot be used on integers, because they can be equal
5283 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5286 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5288 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5292 for (chan
= 0; chan
< 4; chan
++) {
5293 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5296 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5301 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5302 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5311 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5312 struct nir_variable
*variable
)
5314 int idx
= variable
->data
.location
;
5315 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5316 LLVMValueRef interp
;
5318 variable
->data
.driver_location
= idx
* 4;
5319 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5321 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5322 unsigned interp_type
;
5323 if (variable
->data
.sample
) {
5324 interp_type
= INTERP_SAMPLE
;
5325 ctx
->shader_info
->info
.ps
.force_persample
= true;
5326 } else if (variable
->data
.centroid
)
5327 interp_type
= INTERP_CENTROID
;
5329 interp_type
= INTERP_CENTER
;
5331 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5335 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5336 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5341 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5342 struct nir_shader
*nir
) {
5343 nir_foreach_variable(variable
, &nir
->inputs
)
5344 handle_vs_input_decl(ctx
, variable
);
5348 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5349 struct nir_shader
*nir
)
5351 if (!ctx
->options
->key
.fs
.multisample
)
5354 bool uses_center
= false;
5355 bool uses_centroid
= false;
5356 nir_foreach_variable(variable
, &nir
->inputs
) {
5357 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5358 variable
->data
.sample
)
5361 if (variable
->data
.centroid
)
5362 uses_centroid
= true;
5367 if (uses_center
&& uses_centroid
) {
5368 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5369 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5370 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5375 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5376 struct nir_shader
*nir
)
5378 prepare_interp_optimize(ctx
, nir
);
5380 nir_foreach_variable(variable
, &nir
->inputs
)
5381 handle_fs_input_decl(ctx
, variable
);
5385 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5386 ctx
->shader_info
->info
.needs_multiview_view_index
)
5387 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5389 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5390 LLVMValueRef interp_param
;
5391 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5393 if (!(ctx
->input_mask
& (1ull << i
)))
5396 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5397 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5398 interp_param
= *inputs
;
5399 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5403 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5405 } else if (i
== VARYING_SLOT_POS
) {
5406 for(int i
= 0; i
< 3; ++i
)
5407 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5409 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5410 ctx
->abi
.frag_pos
[3]);
5413 ctx
->shader_info
->fs
.num_interp
= index
;
5414 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5415 ctx
->shader_info
->fs
.has_pcoord
= true;
5416 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5417 ctx
->shader_info
->fs
.prim_id_input
= true;
5418 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5419 ctx
->shader_info
->fs
.layer_input
= true;
5420 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5422 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5423 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5427 ac_build_alloca(struct ac_llvm_context
*ac
,
5431 LLVMBuilderRef builder
= ac
->builder
;
5432 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5433 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5434 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5435 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5436 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5440 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5442 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5445 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5446 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5448 LLVMDisposeBuilder(first_builder
);
5453 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5457 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5458 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5463 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5464 struct nir_variable
*variable
,
5465 struct nir_shader
*shader
,
5466 gl_shader_stage stage
)
5468 int idx
= variable
->data
.location
+ variable
->data
.index
;
5469 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5470 uint64_t mask_attribs
;
5472 variable
->data
.driver_location
= idx
* 4;
5474 /* tess ctrl has it's own load/store paths for outputs */
5475 if (stage
== MESA_SHADER_TESS_CTRL
)
5478 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5479 if (stage
== MESA_SHADER_VERTEX
||
5480 stage
== MESA_SHADER_TESS_EVAL
||
5481 stage
== MESA_SHADER_GEOMETRY
) {
5482 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5483 int length
= shader
->info
.clip_distance_array_size
+
5484 shader
->info
.cull_distance_array_size
;
5485 if (stage
== MESA_SHADER_VERTEX
) {
5486 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5487 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5489 if (stage
== MESA_SHADER_TESS_EVAL
) {
5490 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5491 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5498 mask_attribs
= 1ull << idx
;
5502 ctx
->output_mask
|= mask_attribs
;
5506 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5507 struct nir_shader
*nir
,
5508 struct nir_variable
*variable
)
5510 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5511 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5513 /* tess ctrl has it's own load/store paths for outputs */
5514 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5517 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5518 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5519 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5520 int idx
= variable
->data
.location
+ variable
->data
.index
;
5521 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5522 int length
= nir
->info
.clip_distance_array_size
+
5523 nir
->info
.cull_distance_array_size
;
5532 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5533 for (unsigned chan
= 0; chan
< 4; chan
++) {
5534 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5535 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5541 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5542 enum glsl_base_type type
)
5546 case GLSL_TYPE_UINT
:
5547 case GLSL_TYPE_BOOL
:
5548 case GLSL_TYPE_SUBROUTINE
:
5550 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5552 case GLSL_TYPE_INT64
:
5553 case GLSL_TYPE_UINT64
:
5555 case GLSL_TYPE_DOUBLE
:
5558 unreachable("unknown GLSL type");
5563 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5564 const struct glsl_type
*type
)
5566 if (glsl_type_is_scalar(type
)) {
5567 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5570 if (glsl_type_is_vector(type
)) {
5571 return LLVMVectorType(
5572 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5573 glsl_get_vector_elements(type
));
5576 if (glsl_type_is_matrix(type
)) {
5577 return LLVMArrayType(
5578 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5579 glsl_get_matrix_columns(type
));
5582 if (glsl_type_is_array(type
)) {
5583 return LLVMArrayType(
5584 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5585 glsl_get_length(type
));
5588 assert(glsl_type_is_struct(type
));
5590 LLVMTypeRef member_types
[glsl_get_length(type
)];
5592 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5594 glsl_to_llvm_type(ctx
,
5595 glsl_get_struct_field(type
, i
));
5598 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5599 glsl_get_length(type
), false);
5603 setup_locals(struct ac_nir_context
*ctx
,
5604 struct nir_function
*func
)
5607 ctx
->num_locals
= 0;
5608 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5609 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5610 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5611 variable
->data
.location_frac
= 0;
5612 ctx
->num_locals
+= attrib_count
;
5614 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5618 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5619 for (j
= 0; j
< 4; j
++) {
5620 ctx
->locals
[i
* 4 + j
] =
5621 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5627 setup_shared(struct ac_nir_context
*ctx
,
5628 struct nir_shader
*nir
)
5630 nir_foreach_variable(variable
, &nir
->shared
) {
5631 LLVMValueRef shared
=
5632 LLVMAddGlobalInAddressSpace(
5633 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5634 variable
->name
? variable
->name
: "",
5636 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5641 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5643 v
= ac_to_float(ctx
, v
);
5644 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5645 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5649 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5650 LLVMValueRef src0
, LLVMValueRef src1
)
5652 LLVMValueRef const16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
5653 LLVMValueRef comp
[2];
5655 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5656 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5657 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5658 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5661 /* Initialize arguments for the shader export intrinsic */
5663 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5664 LLVMValueRef
*values
,
5666 struct ac_export_args
*args
)
5668 /* Default is 0xf. Adjusted below depending on the format. */
5669 args
->enabled_channels
= 0xf;
5671 /* Specify whether the EXEC mask represents the valid mask */
5672 args
->valid_mask
= 0;
5674 /* Specify whether this is the last export */
5677 /* Specify the target we are exporting */
5678 args
->target
= target
;
5680 args
->compr
= false;
5681 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
5682 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
5683 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
5684 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5689 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5690 LLVMValueRef val
[4];
5691 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5692 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5693 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5694 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5696 switch(col_format
) {
5697 case V_028714_SPI_SHADER_ZERO
:
5698 args
->enabled_channels
= 0; /* writemask */
5699 args
->target
= V_008DFC_SQ_EXP_NULL
;
5702 case V_028714_SPI_SHADER_32_R
:
5703 args
->enabled_channels
= 1;
5704 args
->out
[0] = values
[0];
5707 case V_028714_SPI_SHADER_32_GR
:
5708 args
->enabled_channels
= 0x3;
5709 args
->out
[0] = values
[0];
5710 args
->out
[1] = values
[1];
5713 case V_028714_SPI_SHADER_32_AR
:
5714 args
->enabled_channels
= 0x9;
5715 args
->out
[0] = values
[0];
5716 args
->out
[3] = values
[3];
5719 case V_028714_SPI_SHADER_FP16_ABGR
:
5722 for (unsigned chan
= 0; chan
< 2; chan
++) {
5723 LLVMValueRef pack_args
[2] = {
5725 values
[2 * chan
+ 1]
5727 LLVMValueRef packed
;
5729 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5730 args
->out
[chan
] = packed
;
5734 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5735 for (unsigned chan
= 0; chan
< 4; chan
++) {
5736 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5737 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5738 LLVMConstReal(ctx
->ac
.f32
, 65535), "");
5739 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5740 LLVMConstReal(ctx
->ac
.f32
, 0.5), "");
5741 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5746 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5747 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5750 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5751 for (unsigned chan
= 0; chan
< 4; chan
++) {
5752 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5753 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5754 LLVMConstReal(ctx
->ac
.f32
, 32767), "");
5756 /* If positive, add 0.5, else add -0.5. */
5757 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5758 LLVMBuildSelect(ctx
->builder
,
5759 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5760 val
[chan
], ctx
->ac
.f32_0
, ""),
5761 LLVMConstReal(ctx
->ac
.f32
, 0.5),
5762 LLVMConstReal(ctx
->ac
.f32
, -0.5), ""), "");
5763 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->ac
.i32
, "");
5767 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5768 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5771 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5772 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5773 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5774 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->ac
.i32
, 3, 0);
5776 for (unsigned chan
= 0; chan
< 4; chan
++) {
5777 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5778 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5782 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5783 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5787 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5788 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5789 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5790 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5791 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5792 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->ac
.i32_1
;
5793 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->ac
.i32
, -2, 0);
5796 for (unsigned chan
= 0; chan
< 4; chan
++) {
5797 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5798 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5799 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5803 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5804 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5809 case V_028714_SPI_SHADER_32_ABGR
:
5810 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5814 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5816 for (unsigned i
= 0; i
< 4; ++i
)
5817 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5821 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5822 bool export_prim_id
,
5823 struct ac_vs_output_info
*outinfo
)
5825 uint32_t param_count
= 0;
5827 unsigned pos_idx
, num_pos_exports
= 0;
5828 struct ac_export_args args
, pos_args
[4] = {};
5829 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5832 if (ctx
->options
->key
.has_multiview_view_index
) {
5833 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5835 for(unsigned i
= 0; i
< 4; ++i
)
5836 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5837 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5840 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5841 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5844 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5845 sizeof(outinfo
->vs_output_param_offset
));
5847 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5848 LLVMValueRef slots
[8];
5851 if (outinfo
->cull_dist_mask
)
5852 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5854 i
= VARYING_SLOT_CLIP_DIST0
;
5855 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5856 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5857 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5859 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5860 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
5862 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5863 target
= V_008DFC_SQ_EXP_POS
+ 3;
5864 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5865 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5866 &args
, sizeof(args
));
5869 target
= V_008DFC_SQ_EXP_POS
+ 2;
5870 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5871 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5872 &args
, sizeof(args
));
5876 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
5877 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5878 for (unsigned j
= 0; j
< 4; j
++)
5879 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5880 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5882 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5884 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5885 outinfo
->writes_pointsize
= true;
5886 psize_value
= LLVMBuildLoad(ctx
->builder
,
5887 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5890 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5891 outinfo
->writes_layer
= true;
5892 layer_value
= LLVMBuildLoad(ctx
->builder
,
5893 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5896 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5897 outinfo
->writes_viewport_index
= true;
5898 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5899 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5902 if (outinfo
->writes_pointsize
||
5903 outinfo
->writes_layer
||
5904 outinfo
->writes_viewport_index
) {
5905 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5906 (outinfo
->writes_layer
== true ? 4 : 0));
5907 pos_args
[1].valid_mask
= 0;
5908 pos_args
[1].done
= 0;
5909 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5910 pos_args
[1].compr
= 0;
5911 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
5912 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
5913 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
5914 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
5916 if (outinfo
->writes_pointsize
== true)
5917 pos_args
[1].out
[0] = psize_value
;
5918 if (outinfo
->writes_layer
== true)
5919 pos_args
[1].out
[2] = layer_value
;
5920 if (outinfo
->writes_viewport_index
== true) {
5921 if (ctx
->options
->chip_class
>= GFX9
) {
5922 /* GFX9 has the layer in out.z[10:0] and the viewport
5923 * index in out.z[19:16].
5925 LLVMValueRef v
= viewport_index_value
;
5926 v
= ac_to_integer(&ctx
->ac
, v
);
5927 v
= LLVMBuildShl(ctx
->builder
, v
,
5928 LLVMConstInt(ctx
->ac
.i32
, 16, false),
5930 v
= LLVMBuildOr(ctx
->builder
, v
,
5931 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5933 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5934 pos_args
[1].enabled_channels
|= 1 << 2;
5936 pos_args
[1].out
[3] = viewport_index_value
;
5937 pos_args
[1].enabled_channels
|= 1 << 3;
5941 for (i
= 0; i
< 4; i
++) {
5942 if (pos_args
[i
].out
[0])
5947 for (i
= 0; i
< 4; i
++) {
5948 if (!pos_args
[i
].out
[0])
5951 /* Specify the target we are exporting */
5952 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5953 if (pos_idx
== num_pos_exports
)
5954 pos_args
[i
].done
= 1;
5955 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5958 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5959 LLVMValueRef values
[4];
5960 if (!(ctx
->output_mask
& (1ull << i
)))
5963 for (unsigned j
= 0; j
< 4; j
++)
5964 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5965 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5967 if (i
== VARYING_SLOT_LAYER
) {
5968 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5969 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5971 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5972 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5973 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5975 } else if (i
>= VARYING_SLOT_VAR0
) {
5976 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5977 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5978 outinfo
->vs_output_param_offset
[i
] = param_count
;
5983 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5985 if (target
>= V_008DFC_SQ_EXP_POS
&&
5986 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5987 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5988 &args
, sizeof(args
));
5990 ac_build_export(&ctx
->ac
, &args
);
5994 if (export_prim_id
) {
5995 LLVMValueRef values
[4];
5996 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5997 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
6000 values
[0] = ctx
->vs_prim_id
;
6001 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
6002 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
6003 for (unsigned j
= 1; j
< 4; j
++)
6004 values
[j
] = ctx
->ac
.f32_0
;
6005 si_llvm_init_export_args(ctx
, values
, target
, &args
);
6006 ac_build_export(&ctx
->ac
, &args
);
6007 outinfo
->export_prim_id
= true;
6010 outinfo
->pos_exports
= num_pos_exports
;
6011 outinfo
->param_exports
= param_count
;
6015 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
6016 struct ac_es_output_info
*outinfo
)
6019 uint64_t max_output_written
= 0;
6020 LLVMValueRef lds_base
= NULL
;
6022 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6026 if (!(ctx
->output_mask
& (1ull << i
)))
6029 if (i
== VARYING_SLOT_CLIP_DIST0
)
6030 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6032 param_index
= shader_io_get_unique_index(i
);
6034 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
6037 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
6039 if (ctx
->ac
.chip_class
>= GFX9
) {
6040 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
6041 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
6042 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6043 LLVMConstInt(ctx
->ac
.i32
, 24, false),
6044 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
6045 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
6046 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
6047 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
6048 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
6049 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
6052 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6053 LLVMValueRef dw_addr
;
6054 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
6058 if (!(ctx
->output_mask
& (1ull << i
)))
6061 if (i
== VARYING_SLOT_CLIP_DIST0
)
6062 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6064 param_index
= shader_io_get_unique_index(i
);
6067 dw_addr
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6068 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
6071 for (j
= 0; j
< length
; j
++) {
6072 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
6073 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
6075 if (ctx
->ac
.chip_class
>= GFX9
) {
6076 ac_lds_store(&ctx
->ac
, dw_addr
,
6077 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6078 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6080 ac_build_buffer_store_dword(&ctx
->ac
,
6083 NULL
, ctx
->es2gs_offset
,
6084 (4 * param_index
+ j
) * 4,
6092 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
6094 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
6095 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
6096 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
6097 vertex_dw_stride
, "");
6099 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6100 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
6103 if (!(ctx
->output_mask
& (1ull << i
)))
6106 if (i
== VARYING_SLOT_CLIP_DIST0
)
6107 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6108 int param
= shader_io_get_unique_index(i
);
6109 mark_tess_output(ctx
, false, param
);
6111 mark_tess_output(ctx
, false, param
+ 1);
6112 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
6113 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
6115 for (unsigned j
= 0; j
< length
; j
++) {
6116 ac_lds_store(&ctx
->ac
, dw_addr
,
6117 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6118 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6123 struct ac_build_if_state
6125 struct nir_to_llvm_context
*ctx
;
6126 LLVMValueRef condition
;
6127 LLVMBasicBlockRef entry_block
;
6128 LLVMBasicBlockRef true_block
;
6129 LLVMBasicBlockRef false_block
;
6130 LLVMBasicBlockRef merge_block
;
6133 static LLVMBasicBlockRef
6134 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
6136 LLVMBasicBlockRef current_block
;
6137 LLVMBasicBlockRef next_block
;
6138 LLVMBasicBlockRef new_block
;
6140 /* get current basic block */
6141 current_block
= LLVMGetInsertBlock(ctx
->builder
);
6143 /* chqeck if there's another block after this one */
6144 next_block
= LLVMGetNextBasicBlock(current_block
);
6146 /* insert the new block before the next block */
6147 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
6150 /* append new block after current block */
6151 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
6152 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
6158 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
6159 struct nir_to_llvm_context
*ctx
,
6160 LLVMValueRef condition
)
6162 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
6164 memset(ifthen
, 0, sizeof *ifthen
);
6166 ifthen
->condition
= condition
;
6167 ifthen
->entry_block
= block
;
6169 /* create endif/merge basic block for the phi functions */
6170 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
6172 /* create/insert true_block before merge_block */
6173 ifthen
->true_block
=
6174 LLVMInsertBasicBlockInContext(ctx
->context
,
6175 ifthen
->merge_block
,
6178 /* successive code goes into the true block */
6179 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
6183 * End a conditional.
6186 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
6188 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
6190 /* Insert branch to the merge block from current block */
6191 LLVMBuildBr(builder
, ifthen
->merge_block
);
6194 * Now patch in the various branch instructions.
6197 /* Insert the conditional branch instruction at the end of entry_block */
6198 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
6199 if (ifthen
->false_block
) {
6200 /* we have an else clause */
6201 LLVMBuildCondBr(builder
, ifthen
->condition
,
6202 ifthen
->true_block
, ifthen
->false_block
);
6205 /* no else clause */
6206 LLVMBuildCondBr(builder
, ifthen
->condition
,
6207 ifthen
->true_block
, ifthen
->merge_block
);
6210 /* Resume building code at end of the ifthen->merge_block */
6211 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
6215 write_tess_factors(struct nir_to_llvm_context
*ctx
)
6217 unsigned stride
, outer_comps
, inner_comps
;
6218 struct ac_build_if_state if_ctx
, inner_if_ctx
;
6219 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 8, 5);
6220 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
6221 unsigned tess_inner_index
, tess_outer_index
;
6222 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6223 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6225 emit_barrier(&ctx
->ac
, ctx
->stage
);
6227 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6247 ac_nir_build_if(&if_ctx
, ctx
,
6248 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6249 invocation_id
, ctx
->ac
.i32_0
, ""));
6251 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6252 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6254 mark_tess_output(ctx
, true, tess_inner_index
);
6255 mark_tess_output(ctx
, true, tess_outer_index
);
6256 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6257 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6258 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6259 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6260 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6262 for (i
= 0; i
< 4; i
++) {
6263 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6264 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6268 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6269 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6270 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6272 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6274 for (i
= 0; i
< outer_comps
; i
++) {
6276 ac_lds_load(&ctx
->ac
, lds_outer
);
6277 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6280 for (i
= 0; i
< inner_comps
; i
++) {
6281 inner
[i
] = out
[outer_comps
+i
] =
6282 ac_lds_load(&ctx
->ac
, lds_inner
);
6283 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6288 /* Convert the outputs to vectors for stores. */
6289 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6293 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6296 buffer
= ctx
->hs_ring_tess_factor
;
6297 tf_base
= ctx
->tess_factor_offset
;
6298 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6299 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6300 unsigned tf_offset
= 0;
6302 if (ctx
->options
->chip_class
<= VI
) {
6303 ac_nir_build_if(&inner_if_ctx
, ctx
,
6304 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6305 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6307 /* Store the dynamic HS control word. */
6308 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6309 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6310 1, ctx
->ac
.i32_0
, tf_base
,
6311 0, 1, 0, true, false);
6314 ac_nir_build_endif(&inner_if_ctx
);
6317 /* Store the tessellation factors. */
6318 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6319 MIN2(stride
, 4), byteoffset
, tf_base
,
6320 tf_offset
, 1, 0, true, false);
6322 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6323 stride
- 4, byteoffset
, tf_base
,
6324 16 + tf_offset
, 1, 0, true, false);
6326 //store to offchip for TES to read - only if TES reads them
6327 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6328 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6329 LLVMValueRef tf_inner_offset
;
6330 unsigned param_outer
, param_inner
;
6332 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6333 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6334 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6336 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6337 util_next_power_of_two(outer_comps
));
6339 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6340 outer_comps
, tf_outer_offset
,
6341 ctx
->oc_lds
, 0, 1, 0, true, false);
6343 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6344 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6345 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6347 inner_vec
= inner_comps
== 1 ? inner
[0] :
6348 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6349 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6350 inner_comps
, tf_inner_offset
,
6351 ctx
->oc_lds
, 0, 1, 0, true, false);
6354 ac_nir_build_endif(&if_ctx
);
6358 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6360 write_tess_factors(ctx
);
6364 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6365 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6366 struct ac_export_args
*args
)
6369 si_llvm_init_export_args(ctx
, color
, param
,
6373 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6374 args
->done
= 1; /* DONE bit */
6375 } else if (!args
->enabled_channels
)
6376 return false; /* unnecessary NULL export */
6382 radv_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6383 LLVMValueRef depth
, LLVMValueRef stencil
,
6384 LLVMValueRef samplemask
)
6386 struct ac_export_args args
;
6388 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
6390 ac_build_export(&ctx
->ac
, &args
);
6394 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6397 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6398 struct ac_export_args color_args
[8];
6400 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6401 LLVMValueRef values
[4];
6403 if (!(ctx
->output_mask
& (1ull << i
)))
6406 if (i
== FRAG_RESULT_DEPTH
) {
6407 ctx
->shader_info
->fs
.writes_z
= true;
6408 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6409 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6410 } else if (i
== FRAG_RESULT_STENCIL
) {
6411 ctx
->shader_info
->fs
.writes_stencil
= true;
6412 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6413 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6414 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6415 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6416 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6417 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6420 for (unsigned j
= 0; j
< 4; j
++)
6421 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6422 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6424 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6425 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6427 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6433 for (unsigned i
= 0; i
< index
; i
++)
6434 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6435 if (depth
|| stencil
|| samplemask
)
6436 radv_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6438 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6439 ac_build_export(&ctx
->ac
, &color_args
[0]);
6444 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6446 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6450 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6451 LLVMValueRef
*addrs
)
6453 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6455 switch (ctx
->stage
) {
6456 case MESA_SHADER_VERTEX
:
6457 if (ctx
->options
->key
.vs
.as_ls
)
6458 handle_ls_outputs_post(ctx
);
6459 else if (ctx
->options
->key
.vs
.as_es
)
6460 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6462 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6463 &ctx
->shader_info
->vs
.outinfo
);
6465 case MESA_SHADER_FRAGMENT
:
6466 handle_fs_outputs_post(ctx
);
6468 case MESA_SHADER_GEOMETRY
:
6469 emit_gs_epilogue(ctx
);
6471 case MESA_SHADER_TESS_CTRL
:
6472 handle_tcs_outputs_post(ctx
);
6474 case MESA_SHADER_TESS_EVAL
:
6475 if (ctx
->options
->key
.tes
.as_es
)
6476 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6478 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6479 &ctx
->shader_info
->tes
.outinfo
);
6486 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6488 LLVMPassManagerRef passmgr
;
6489 /* Create the pass manager */
6490 passmgr
= LLVMCreateFunctionPassManagerForModule(
6493 /* This pass should eliminate all the load and store instructions */
6494 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6496 /* Add some optimization passes */
6497 LLVMAddScalarReplAggregatesPass(passmgr
);
6498 LLVMAddLICMPass(passmgr
);
6499 LLVMAddAggressiveDCEPass(passmgr
);
6500 LLVMAddCFGSimplificationPass(passmgr
);
6501 LLVMAddInstructionCombiningPass(passmgr
);
6504 LLVMInitializeFunctionPassManager(passmgr
);
6505 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6506 LLVMFinalizeFunctionPassManager(passmgr
);
6508 LLVMDisposeBuilder(ctx
->builder
);
6509 LLVMDisposePassManager(passmgr
);
6513 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6515 struct ac_vs_output_info
*outinfo
;
6517 switch (ctx
->stage
) {
6518 case MESA_SHADER_FRAGMENT
:
6519 case MESA_SHADER_COMPUTE
:
6520 case MESA_SHADER_TESS_CTRL
:
6521 case MESA_SHADER_GEOMETRY
:
6523 case MESA_SHADER_VERTEX
:
6524 if (ctx
->options
->key
.vs
.as_ls
||
6525 ctx
->options
->key
.vs
.as_es
)
6527 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6529 case MESA_SHADER_TESS_EVAL
:
6530 if (ctx
->options
->key
.vs
.as_es
)
6532 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6535 unreachable("Unhandled shader type");
6538 ac_optimize_vs_outputs(&ctx
->ac
,
6540 outinfo
->vs_output_param_offset
,
6542 &outinfo
->param_exports
);
6546 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6548 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6549 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6550 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6553 if (ctx
->is_gs_copy_shader
) {
6554 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6556 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6558 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6559 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6561 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.v4i32
, "");
6563 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6564 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6565 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6566 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6569 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6570 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6571 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6572 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6577 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6578 const struct nir_shader
*nir
)
6580 switch (nir
->info
.stage
) {
6581 case MESA_SHADER_TESS_CTRL
:
6582 return chip_class
>= CIK
? 128 : 64;
6583 case MESA_SHADER_GEOMETRY
:
6584 return chip_class
>= GFX9
? 128 : 64;
6585 case MESA_SHADER_COMPUTE
:
6591 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6592 nir
->info
.cs
.local_size
[1] *
6593 nir
->info
.cs
.local_size
[2];
6594 return max_workgroup_size
;
6597 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6598 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6600 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6601 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6602 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6603 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6605 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6606 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6607 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_rel_ids
, ctx
->rel_auto_id
, "");
6608 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6611 static void prepare_gs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6613 for(int i
= 5; i
>= 0; --i
) {
6614 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6615 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6616 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6619 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6620 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6621 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6624 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6625 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6627 struct ac_nir_context ctx
= {};
6628 struct nir_function
*func
;
6637 ctx
.stage
= nir
->info
.stage
;
6639 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6641 nir_foreach_variable(variable
, &nir
->outputs
)
6642 handle_shader_output_decl(&ctx
, nir
, variable
);
6644 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6645 _mesa_key_pointer_equal
);
6646 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6647 _mesa_key_pointer_equal
);
6648 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6649 _mesa_key_pointer_equal
);
6651 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6653 setup_locals(&ctx
, func
);
6655 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6656 setup_shared(&ctx
, nir
);
6658 visit_cf_list(&ctx
, &func
->impl
->body
);
6659 phi_post_pass(&ctx
);
6661 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6665 ralloc_free(ctx
.defs
);
6666 ralloc_free(ctx
.phis
);
6667 ralloc_free(ctx
.vars
);
6674 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6675 struct nir_shader
*const *shaders
,
6677 struct ac_shader_variant_info
*shader_info
,
6678 const struct ac_nir_compiler_options
*options
)
6680 struct nir_to_llvm_context ctx
= {0};
6682 ctx
.options
= options
;
6683 ctx
.shader_info
= shader_info
;
6684 ctx
.context
= LLVMContextCreate();
6685 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6687 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
6689 ctx
.ac
.module
= ctx
.module
;
6690 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6692 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6693 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6694 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6695 LLVMDisposeTargetData(data_layout
);
6696 LLVMDisposeMessage(data_layout_str
);
6698 enum ac_float_mode float_mode
=
6699 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
6700 AC_FLOAT_MODE_DEFAULT
;
6702 ctx
.builder
= ac_create_builder(ctx
.context
, float_mode
);
6703 ctx
.ac
.builder
= ctx
.builder
;
6705 memset(shader_info
, 0, sizeof(*shader_info
));
6707 for(int i
= 0; i
< shader_count
; ++i
)
6708 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6710 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6711 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6712 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6713 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6715 ctx
.max_workgroup_size
= 0;
6716 for (int i
= 0; i
< shader_count
; ++i
) {
6717 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6718 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6722 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6723 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6725 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6726 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6727 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
6728 ctx
.abi
.load_ubo
= radv_load_ubo
;
6729 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6730 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6731 ctx
.abi
.clamp_shadow_reference
= false;
6733 if (shader_count
>= 2)
6734 ac_init_exec_full_mask(&ctx
.ac
);
6736 if (ctx
.ac
.chip_class
== GFX9
&&
6737 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6738 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6740 for(int i
= 0; i
< shader_count
; ++i
) {
6741 ctx
.stage
= shaders
[i
]->info
.stage
;
6742 ctx
.output_mask
= 0;
6743 ctx
.tess_outputs_written
= 0;
6744 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6745 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6747 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6748 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6749 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6750 ctx
.abi
.load_inputs
= load_gs_input
;
6751 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6752 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
6753 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
6754 ctx
.abi
.load_tess_varyings
= load_tcs_varyings
;
6755 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
6756 ctx
.abi
.store_tcs_outputs
= store_tcs_output
;
6757 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6758 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6759 ctx
.abi
.load_tess_varyings
= load_tes_input
;
6760 ctx
.abi
.load_tess_coord
= load_tess_coord
;
6761 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
6762 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6763 if (shader_info
->info
.vs
.needs_instance_id
) {
6764 if (ctx
.options
->key
.vs
.as_ls
) {
6765 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6766 MAX2(2, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6768 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6769 MAX2(1, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6772 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6773 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6777 emit_barrier(&ctx
.ac
, ctx
.stage
);
6779 ac_setup_rings(&ctx
);
6781 LLVMBasicBlockRef merge_block
;
6782 if (shader_count
>= 2) {
6783 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6784 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6785 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6787 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6788 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6789 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6790 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6791 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6792 thread_id
, count
, "");
6793 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6795 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6798 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6799 handle_fs_inputs(&ctx
, shaders
[i
]);
6800 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6801 handle_vs_inputs(&ctx
, shaders
[i
]);
6802 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6803 prepare_gs_input_vgprs(&ctx
);
6805 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6806 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6808 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6810 if (shader_count
>= 2) {
6811 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6812 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6815 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6816 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6817 shaders
[i
]->info
.cull_distance_array_size
> 4;
6818 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6819 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6820 shaders
[i
]->info
.gs
.vertices_out
;
6821 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6822 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6823 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6824 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6825 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6829 LLVMBuildRetVoid(ctx
.builder
);
6831 ac_llvm_finalize_module(&ctx
);
6833 if (shader_count
== 1)
6834 ac_nir_eliminate_const_vs_outputs(&ctx
);
6839 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6841 unsigned *retval
= (unsigned *)context
;
6842 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6843 char *description
= LLVMGetDiagInfoDescription(di
);
6845 if (severity
== LLVMDSError
) {
6847 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6851 LLVMDisposeMessage(description
);
6854 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6855 struct ac_shader_binary
*binary
,
6856 LLVMTargetMachineRef tm
)
6858 unsigned retval
= 0;
6860 LLVMContextRef llvm_ctx
;
6861 LLVMMemoryBufferRef out_buffer
;
6862 unsigned buffer_size
;
6863 const char *buffer_data
;
6866 /* Setup Diagnostic Handler*/
6867 llvm_ctx
= LLVMGetModuleContext(M
);
6869 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6873 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6876 /* Process Errors/Warnings */
6878 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6884 /* Extract Shader Code*/
6885 buffer_size
= LLVMGetBufferSize(out_buffer
);
6886 buffer_data
= LLVMGetBufferStart(out_buffer
);
6888 ac_elf_read(buffer_data
, buffer_size
, binary
);
6891 LLVMDisposeMemoryBuffer(out_buffer
);
6897 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6898 LLVMModuleRef llvm_module
,
6899 struct ac_shader_binary
*binary
,
6900 struct ac_shader_config
*config
,
6901 struct ac_shader_variant_info
*shader_info
,
6902 gl_shader_stage stage
,
6903 bool dump_shader
, bool supports_spill
)
6906 ac_dump_module(llvm_module
);
6908 memset(binary
, 0, sizeof(*binary
));
6909 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6911 fprintf(stderr
, "compile failed\n");
6915 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6917 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6919 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6920 LLVMDisposeModule(llvm_module
);
6921 LLVMContextDispose(ctx
);
6923 if (stage
== MESA_SHADER_FRAGMENT
) {
6924 shader_info
->num_input_vgprs
= 0;
6925 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6926 shader_info
->num_input_vgprs
+= 2;
6927 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6928 shader_info
->num_input_vgprs
+= 2;
6929 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6930 shader_info
->num_input_vgprs
+= 2;
6931 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6932 shader_info
->num_input_vgprs
+= 3;
6933 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6934 shader_info
->num_input_vgprs
+= 2;
6935 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6936 shader_info
->num_input_vgprs
+= 2;
6937 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6938 shader_info
->num_input_vgprs
+= 2;
6939 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6940 shader_info
->num_input_vgprs
+= 1;
6941 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6942 shader_info
->num_input_vgprs
+= 1;
6943 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6944 shader_info
->num_input_vgprs
+= 1;
6945 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6946 shader_info
->num_input_vgprs
+= 1;
6947 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6948 shader_info
->num_input_vgprs
+= 1;
6949 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6950 shader_info
->num_input_vgprs
+= 1;
6951 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6952 shader_info
->num_input_vgprs
+= 1;
6953 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6954 shader_info
->num_input_vgprs
+= 1;
6955 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6956 shader_info
->num_input_vgprs
+= 1;
6958 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6960 /* +3 for scratch wave offset and VCC */
6961 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6962 shader_info
->num_input_sgprs
+ 3);
6964 /* Enable 64-bit and 16-bit denormals, because there is no performance
6967 * If denormals are enabled, all floating-point output modifiers are
6970 * Don't enable denormals for 32-bit floats, because:
6971 * - Floating-point output modifiers would be ignored by the hw.
6972 * - Some opcodes don't support denormals, such as v_mad_f32. We would
6973 * have to stop using those.
6974 * - SI & CI would be very slow.
6976 config
->float_mode
|= V_00B028_FP_64_DENORMS
;
6980 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
6982 switch (nir
->info
.stage
) {
6983 case MESA_SHADER_COMPUTE
:
6984 for (int i
= 0; i
< 3; ++i
)
6985 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6987 case MESA_SHADER_FRAGMENT
:
6988 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6990 case MESA_SHADER_GEOMETRY
:
6991 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6992 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6993 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6994 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6996 case MESA_SHADER_TESS_EVAL
:
6997 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6998 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6999 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
7000 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
7001 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
7003 case MESA_SHADER_TESS_CTRL
:
7004 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
7006 case MESA_SHADER_VERTEX
:
7007 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
7008 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
7009 /* in LS mode we need at least 1, invocation id needs 2, handled elsewhere */
7010 if (options
->key
.vs
.as_ls
)
7011 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
7018 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
7019 struct ac_shader_binary
*binary
,
7020 struct ac_shader_config
*config
,
7021 struct ac_shader_variant_info
*shader_info
,
7022 struct nir_shader
*const *nir
,
7024 const struct ac_nir_compiler_options
*options
,
7028 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
7031 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
7032 for (int i
= 0; i
< nir_count
; ++i
)
7033 ac_fill_shader_info(shader_info
, nir
[i
], options
);
7035 /* Determine the ES type (VS or TES) for the GS on GFX9. */
7036 if (options
->chip_class
== GFX9
) {
7037 if (nir_count
== 2 &&
7038 nir
[1]->info
.stage
== MESA_SHADER_GEOMETRY
) {
7039 shader_info
->gs
.es_type
= nir
[0]->info
.stage
;
7045 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
7047 LLVMValueRef args
[9];
7048 args
[0] = ctx
->gsvs_ring
;
7049 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
7050 args
[3] = ctx
->ac
.i32_0
;
7051 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
7052 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
7053 args
[6] = ctx
->ac
.i32_1
; /* GLC */
7054 args
[7] = ctx
->ac
.i32_1
; /* SLC */
7055 args
[8] = ctx
->ac
.i32_0
; /* TFE */
7059 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
7063 if (!(ctx
->output_mask
& (1ull << i
)))
7066 if (i
== VARYING_SLOT_CLIP_DIST0
) {
7067 /* unpack clip and cull from a single set of slots */
7068 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
7073 for (unsigned j
= 0; j
< length
; j
++) {
7075 args
[2] = LLVMConstInt(ctx
->ac
.i32
,
7077 ctx
->gs_max_out_vertices
* 16 * 4, false);
7079 value
= ac_build_intrinsic(&ctx
->ac
,
7080 "llvm.SI.buffer.load.dword.i32.i32",
7081 ctx
->ac
.i32
, args
, 9,
7082 AC_FUNC_ATTR_READONLY
|
7083 AC_FUNC_ATTR_LEGACY
);
7085 LLVMBuildStore(ctx
->builder
,
7086 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
7090 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
7093 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
7094 struct nir_shader
*geom_shader
,
7095 struct ac_shader_binary
*binary
,
7096 struct ac_shader_config
*config
,
7097 struct ac_shader_variant_info
*shader_info
,
7098 const struct ac_nir_compiler_options
*options
,
7101 struct nir_to_llvm_context ctx
= {0};
7102 ctx
.context
= LLVMContextCreate();
7103 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
7104 ctx
.options
= options
;
7105 ctx
.shader_info
= shader_info
;
7107 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
7109 ctx
.ac
.module
= ctx
.module
;
7111 ctx
.is_gs_copy_shader
= true;
7112 LLVMSetTarget(ctx
.module
, "amdgcn--");
7114 enum ac_float_mode float_mode
=
7115 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
7116 AC_FLOAT_MODE_DEFAULT
;
7118 ctx
.builder
= ac_create_builder(ctx
.context
, float_mode
);
7119 ctx
.ac
.builder
= ctx
.builder
;
7120 ctx
.stage
= MESA_SHADER_VERTEX
;
7122 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
7124 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
7125 ac_setup_rings(&ctx
);
7127 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
7128 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
7130 struct ac_nir_context nir_ctx
= {};
7131 nir_ctx
.ac
= ctx
.ac
;
7132 nir_ctx
.abi
= &ctx
.abi
;
7134 nir_ctx
.nctx
= &ctx
;
7137 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
7138 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
7139 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
7142 ac_gs_copy_shader_emit(&ctx
);
7146 LLVMBuildRetVoid(ctx
.builder
);
7148 ac_llvm_finalize_module(&ctx
);
7150 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
7152 dump_shader
, options
->supports_spill
);