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 void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
547 gl_shader_stage stage
,
548 struct user_sgpr_info
*user_sgpr_info
)
550 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
552 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
553 if (stage
== MESA_SHADER_GEOMETRY
||
554 stage
== MESA_SHADER_VERTEX
||
555 stage
== MESA_SHADER_TESS_CTRL
||
556 stage
== MESA_SHADER_TESS_EVAL
||
557 ctx
->is_gs_copy_shader
)
558 user_sgpr_info
->need_ring_offsets
= true;
560 if (stage
== MESA_SHADER_FRAGMENT
&&
561 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
562 user_sgpr_info
->need_ring_offsets
= true;
564 /* 2 user sgprs will nearly always be allocated for scratch/rings */
565 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
566 user_sgpr_info
->sgpr_count
+= 2;
569 /* FIXME: fix the number of user sgprs for merged shaders on GFX9 */
571 case MESA_SHADER_COMPUTE
:
572 if (ctx
->shader_info
->info
.cs
.uses_grid_size
)
573 user_sgpr_info
->sgpr_count
+= 3;
575 case MESA_SHADER_FRAGMENT
:
576 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
578 case MESA_SHADER_VERTEX
:
579 if (!ctx
->is_gs_copy_shader
) {
580 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
581 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
582 user_sgpr_info
->sgpr_count
+= 3;
584 user_sgpr_info
->sgpr_count
+= 2;
587 if (ctx
->options
->key
.vs
.as_ls
)
588 user_sgpr_info
->sgpr_count
++;
590 case MESA_SHADER_TESS_CTRL
:
591 user_sgpr_info
->sgpr_count
+= 4;
593 case MESA_SHADER_TESS_EVAL
:
594 user_sgpr_info
->sgpr_count
+= 1;
596 case MESA_SHADER_GEOMETRY
:
597 user_sgpr_info
->sgpr_count
+= 2;
603 if (ctx
->shader_info
->info
.loads_push_constants
)
604 user_sgpr_info
->sgpr_count
+= 2;
606 uint32_t available_sgprs
= ctx
->options
->chip_class
>= GFX9
? 32 : 16;
607 uint32_t remaining_sgprs
= available_sgprs
- user_sgpr_info
->sgpr_count
;
609 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
610 user_sgpr_info
->sgpr_count
+= 2;
611 user_sgpr_info
->indirect_all_descriptor_sets
= true;
613 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
618 declare_global_input_sgprs(struct nir_to_llvm_context
*ctx
,
619 gl_shader_stage stage
,
620 bool has_previous_stage
,
621 gl_shader_stage previous_stage
,
622 const struct user_sgpr_info
*user_sgpr_info
,
623 struct arg_info
*args
,
624 LLVMValueRef
*desc_sets
)
626 LLVMTypeRef type
= const_array(ctx
->ac
.i8
, 1024 * 1024);
627 unsigned num_sets
= ctx
->options
->layout
?
628 ctx
->options
->layout
->num_sets
: 0;
629 unsigned stage_mask
= 1 << stage
;
631 if (has_previous_stage
)
632 stage_mask
|= 1 << previous_stage
;
634 /* 1 for each descriptor set */
635 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
636 for (unsigned i
= 0; i
< num_sets
; ++i
) {
637 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
638 add_array_arg(args
, type
,
639 &ctx
->descriptor_sets
[i
]);
643 add_array_arg(args
, const_array(type
, 32), desc_sets
);
646 if (ctx
->shader_info
->info
.loads_push_constants
) {
647 /* 1 for push constants and dynamic descriptors */
648 add_array_arg(args
, type
, &ctx
->push_constants
);
653 declare_vs_specific_input_sgprs(struct nir_to_llvm_context
*ctx
,
654 gl_shader_stage stage
,
655 bool has_previous_stage
,
656 gl_shader_stage previous_stage
,
657 struct arg_info
*args
)
659 if (!ctx
->is_gs_copy_shader
&&
660 (stage
== MESA_SHADER_VERTEX
||
661 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
662 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
663 add_arg(args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
664 &ctx
->vertex_buffers
);
666 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
);
667 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);
668 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
669 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
);
675 declare_vs_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
677 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
);
678 if (!ctx
->is_gs_copy_shader
) {
679 if (ctx
->options
->key
.vs
.as_ls
) {
680 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->rel_auto_id
);
681 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
683 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
684 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->vs_prim_id
);
686 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* unused */
691 declare_tes_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
693 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_u
);
694 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_v
);
695 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
);
696 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.tes_patch_id
);
700 set_global_input_locs(struct nir_to_llvm_context
*ctx
, gl_shader_stage stage
,
701 bool has_previous_stage
, gl_shader_stage previous_stage
,
702 const struct user_sgpr_info
*user_sgpr_info
,
703 LLVMValueRef desc_sets
, uint8_t *user_sgpr_idx
)
705 unsigned num_sets
= ctx
->options
->layout
?
706 ctx
->options
->layout
->num_sets
: 0;
707 unsigned stage_mask
= 1 << stage
;
709 if (has_previous_stage
)
710 stage_mask
|= 1 << previous_stage
;
712 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
713 for (unsigned i
= 0; i
< num_sets
; ++i
) {
714 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
715 set_loc_desc(ctx
, i
, user_sgpr_idx
, 0);
717 ctx
->descriptor_sets
[i
] = NULL
;
720 set_loc_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
,
723 for (unsigned i
= 0; i
< num_sets
; ++i
) {
724 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
725 set_loc_desc(ctx
, i
, user_sgpr_idx
, i
* 8);
726 ctx
->descriptor_sets
[i
] =
727 ac_build_load_to_sgpr(&ctx
->ac
,
729 LLVMConstInt(ctx
->ac
.i32
, i
, false));
732 ctx
->descriptor_sets
[i
] = NULL
;
734 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
737 if (ctx
->shader_info
->info
.loads_push_constants
) {
738 set_loc_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
743 set_vs_specific_input_locs(struct nir_to_llvm_context
*ctx
,
744 gl_shader_stage stage
, bool has_previous_stage
,
745 gl_shader_stage previous_stage
,
746 uint8_t *user_sgpr_idx
)
748 if (!ctx
->is_gs_copy_shader
&&
749 (stage
== MESA_SHADER_VERTEX
||
750 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
751 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
752 set_loc_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
,
757 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
760 set_loc_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
761 user_sgpr_idx
, vs_num
);
765 static void create_function(struct nir_to_llvm_context
*ctx
,
766 gl_shader_stage stage
,
767 bool has_previous_stage
,
768 gl_shader_stage previous_stage
)
770 uint8_t user_sgpr_idx
;
771 struct user_sgpr_info user_sgpr_info
;
772 struct arg_info args
= {};
773 LLVMValueRef desc_sets
;
775 allocate_user_sgprs(ctx
, stage
, &user_sgpr_info
);
777 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
778 add_arg(&args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
783 case MESA_SHADER_COMPUTE
:
784 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
785 previous_stage
, &user_sgpr_info
,
788 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
789 add_arg(&args
, ARG_SGPR
, ctx
->ac
.v3i32
,
790 &ctx
->num_work_groups
);
793 for (int i
= 0; i
< 3; i
++) {
794 ctx
->workgroup_ids
[i
] = NULL
;
795 if (ctx
->shader_info
->info
.cs
.uses_block_id
[i
]) {
796 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
797 &ctx
->workgroup_ids
[i
]);
801 if (ctx
->shader_info
->info
.cs
.uses_local_invocation_idx
)
802 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tg_size
);
803 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
,
804 &ctx
->local_invocation_ids
);
806 case MESA_SHADER_VERTEX
:
807 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
808 previous_stage
, &user_sgpr_info
,
810 declare_vs_specific_input_sgprs(ctx
, stage
, has_previous_stage
,
811 previous_stage
, &args
);
813 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.vs
.as_es
&& !ctx
->options
->key
.vs
.as_ls
&& ctx
->options
->key
.has_multiview_view_index
))
814 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
815 if (ctx
->options
->key
.vs
.as_es
)
816 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
818 else if (ctx
->options
->key
.vs
.as_ls
)
819 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
820 &ctx
->ls_out_layout
);
822 declare_vs_input_vgprs(ctx
, &args
);
824 case MESA_SHADER_TESS_CTRL
:
825 if (has_previous_stage
) {
826 // First 6 system regs
827 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
828 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
829 &ctx
->merged_wave_info
);
830 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
831 &ctx
->tess_factor_offset
);
833 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
834 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
835 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
837 declare_global_input_sgprs(ctx
, stage
,
840 &user_sgpr_info
, &args
,
842 declare_vs_specific_input_sgprs(ctx
, stage
,
844 previous_stage
, &args
);
846 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
847 &ctx
->ls_out_layout
);
849 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
850 &ctx
->tcs_offchip_layout
);
851 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
852 &ctx
->tcs_out_offsets
);
853 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
854 &ctx
->tcs_out_layout
);
855 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
856 &ctx
->tcs_in_layout
);
857 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
858 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
861 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
862 &ctx
->abi
.tcs_patch_id
);
863 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
864 &ctx
->abi
.tcs_rel_ids
);
866 declare_vs_input_vgprs(ctx
, &args
);
868 declare_global_input_sgprs(ctx
, stage
,
871 &user_sgpr_info
, &args
,
874 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
875 &ctx
->tcs_offchip_layout
);
876 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
877 &ctx
->tcs_out_offsets
);
878 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
879 &ctx
->tcs_out_layout
);
880 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
881 &ctx
->tcs_in_layout
);
882 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
883 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
886 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
887 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
888 &ctx
->tess_factor_offset
);
889 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
890 &ctx
->abi
.tcs_patch_id
);
891 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
892 &ctx
->abi
.tcs_rel_ids
);
895 case MESA_SHADER_TESS_EVAL
:
896 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
897 previous_stage
, &user_sgpr_info
,
900 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
);
901 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
902 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
904 if (ctx
->options
->key
.tes
.as_es
) {
905 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
906 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
907 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
910 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
911 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
913 declare_tes_input_vgprs(ctx
, &args
);
915 case MESA_SHADER_GEOMETRY
:
916 if (has_previous_stage
) {
917 // First 6 system regs
918 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
920 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
921 &ctx
->merged_wave_info
);
922 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
924 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
925 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
926 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
928 declare_global_input_sgprs(ctx
, stage
,
931 &user_sgpr_info
, &args
,
934 if (previous_stage
== MESA_SHADER_TESS_EVAL
) {
935 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
936 &ctx
->tcs_offchip_layout
);
938 declare_vs_specific_input_sgprs(ctx
, stage
,
944 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
945 &ctx
->gsvs_ring_stride
);
946 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
947 &ctx
->gsvs_num_entries
);
948 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
949 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
952 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
953 &ctx
->gs_vtx_offset
[0]);
954 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
955 &ctx
->gs_vtx_offset
[2]);
956 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
957 &ctx
->abi
.gs_prim_id
);
958 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
959 &ctx
->abi
.gs_invocation_id
);
960 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
961 &ctx
->gs_vtx_offset
[4]);
963 if (previous_stage
== MESA_SHADER_VERTEX
) {
964 declare_vs_input_vgprs(ctx
, &args
);
966 declare_tes_input_vgprs(ctx
, &args
);
969 declare_global_input_sgprs(ctx
, stage
,
972 &user_sgpr_info
, &args
,
975 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
976 &ctx
->gsvs_ring_stride
);
977 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
978 &ctx
->gsvs_num_entries
);
979 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
980 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
983 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
);
984 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs_wave_id
);
985 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
986 &ctx
->gs_vtx_offset
[0]);
987 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
988 &ctx
->gs_vtx_offset
[1]);
989 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
990 &ctx
->abi
.gs_prim_id
);
991 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
992 &ctx
->gs_vtx_offset
[2]);
993 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
994 &ctx
->gs_vtx_offset
[3]);
995 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
996 &ctx
->gs_vtx_offset
[4]);
997 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
998 &ctx
->gs_vtx_offset
[5]);
999 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1000 &ctx
->abi
.gs_invocation_id
);
1003 case MESA_SHADER_FRAGMENT
:
1004 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
1005 previous_stage
, &user_sgpr_info
,
1008 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
1009 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1010 &ctx
->sample_pos_offset
);
1012 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->prim_mask
);
1013 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_sample
);
1014 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_center
);
1015 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
);
1016 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
1017 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_sample
);
1018 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_center
);
1019 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
);
1020 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
1021 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]);
1022 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]);
1023 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]);
1024 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]);
1025 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.front_face
);
1026 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
);
1027 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
);
1028 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* fixed pt */
1031 unreachable("Shader stage not implemented");
1034 ctx
->main_function
= create_llvm_function(
1035 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
1036 ctx
->max_workgroup_size
,
1037 ctx
->options
->unsafe_math
);
1038 set_llvm_calling_convention(ctx
->main_function
, stage
);
1041 ctx
->shader_info
->num_input_vgprs
= 0;
1042 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
1044 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
1046 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1047 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
1049 assign_arguments(ctx
->main_function
, &args
);
1053 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
1054 set_loc_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
,
1056 if (ctx
->options
->supports_spill
) {
1057 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
1058 LLVMPointerType(ctx
->ac
.i8
, CONST_ADDR_SPACE
),
1059 NULL
, 0, AC_FUNC_ATTR_READNONE
);
1060 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
1061 const_array(ctx
->ac
.v4i32
, 16), "");
1065 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
1066 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
1067 if (has_previous_stage
)
1070 set_global_input_locs(ctx
, stage
, has_previous_stage
, previous_stage
,
1071 &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
1074 case MESA_SHADER_COMPUTE
:
1075 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
1076 set_loc_shader(ctx
, AC_UD_CS_GRID_SIZE
,
1080 case MESA_SHADER_VERTEX
:
1081 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1082 previous_stage
, &user_sgpr_idx
);
1083 if (ctx
->view_index
)
1084 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1085 if (ctx
->options
->key
.vs
.as_ls
) {
1086 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1089 if (ctx
->options
->key
.vs
.as_ls
)
1090 ac_declare_lds_as_pointer(&ctx
->ac
);
1092 case MESA_SHADER_TESS_CTRL
:
1093 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1094 previous_stage
, &user_sgpr_idx
);
1095 if (has_previous_stage
)
1096 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1098 set_loc_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
1099 if (ctx
->view_index
)
1100 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1101 ac_declare_lds_as_pointer(&ctx
->ac
);
1103 case MESA_SHADER_TESS_EVAL
:
1104 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
1105 if (ctx
->view_index
)
1106 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1108 case MESA_SHADER_GEOMETRY
:
1109 if (has_previous_stage
) {
1110 if (previous_stage
== MESA_SHADER_VERTEX
)
1111 set_vs_specific_input_locs(ctx
, stage
,
1116 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
,
1119 set_loc_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
,
1121 if (ctx
->view_index
)
1122 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1123 if (has_previous_stage
)
1124 ac_declare_lds_as_pointer(&ctx
->ac
);
1126 case MESA_SHADER_FRAGMENT
:
1127 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
1128 set_loc_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
,
1133 unreachable("Shader stage not implemented");
1136 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
1139 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1140 LLVMValueRef value
, unsigned count
)
1142 unsigned num_components
= ac_get_llvm_num_components(value
);
1143 if (count
== num_components
)
1146 LLVMValueRef masks
[] = {
1147 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1148 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1151 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1154 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1155 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1159 build_store_values_extended(struct ac_llvm_context
*ac
,
1160 LLVMValueRef
*values
,
1161 unsigned value_count
,
1162 unsigned value_stride
,
1165 LLVMBuilderRef builder
= ac
->builder
;
1168 for (i
= 0; i
< value_count
; i
++) {
1169 LLVMValueRef ptr
= values
[i
* value_stride
];
1170 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1171 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1172 LLVMBuildStore(builder
, value
, ptr
);
1176 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1177 const nir_ssa_def
*def
)
1179 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1180 if (def
->num_components
> 1) {
1181 type
= LLVMVectorType(type
, def
->num_components
);
1186 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1189 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1190 return (LLVMValueRef
)entry
->data
;
1194 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1195 const struct nir_block
*b
)
1197 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1198 return (LLVMBasicBlockRef
)entry
->data
;
1201 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1203 unsigned num_components
)
1205 LLVMValueRef value
= get_src(ctx
, src
.src
);
1206 bool need_swizzle
= false;
1209 LLVMTypeRef type
= LLVMTypeOf(value
);
1210 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1211 ? LLVMGetVectorSize(type
)
1214 for (unsigned i
= 0; i
< num_components
; ++i
) {
1215 assert(src
.swizzle
[i
] < src_components
);
1216 if (src
.swizzle
[i
] != i
)
1217 need_swizzle
= true;
1220 if (need_swizzle
|| num_components
!= src_components
) {
1221 LLVMValueRef masks
[] = {
1222 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1223 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1224 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1225 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1227 if (src_components
> 1 && num_components
== 1) {
1228 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1230 } else if (src_components
== 1 && num_components
> 1) {
1231 LLVMValueRef values
[] = {value
, value
, value
, value
};
1232 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1234 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1235 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1239 assert(!src
.negate
);
1244 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1245 LLVMIntPredicate pred
, LLVMValueRef src0
,
1248 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1249 return LLVMBuildSelect(ctx
->builder
, result
,
1250 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1254 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1255 LLVMRealPredicate pred
, LLVMValueRef src0
,
1258 LLVMValueRef result
;
1259 src0
= ac_to_float(ctx
, src0
);
1260 src1
= ac_to_float(ctx
, src1
);
1261 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1262 return LLVMBuildSelect(ctx
->builder
, result
,
1263 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1267 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1269 LLVMTypeRef result_type
,
1273 LLVMValueRef params
[] = {
1274 ac_to_float(ctx
, src0
),
1277 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1278 get_elem_bits(ctx
, result_type
));
1279 assert(length
< sizeof(name
));
1280 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1283 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1285 LLVMTypeRef result_type
,
1286 LLVMValueRef src0
, LLVMValueRef src1
)
1289 LLVMValueRef params
[] = {
1290 ac_to_float(ctx
, src0
),
1291 ac_to_float(ctx
, src1
),
1294 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1295 get_elem_bits(ctx
, result_type
));
1296 assert(length
< sizeof(name
));
1297 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1300 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1302 LLVMTypeRef result_type
,
1303 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1306 LLVMValueRef params
[] = {
1307 ac_to_float(ctx
, src0
),
1308 ac_to_float(ctx
, src1
),
1309 ac_to_float(ctx
, src2
),
1312 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1313 get_elem_bits(ctx
, result_type
));
1314 assert(length
< sizeof(name
));
1315 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1318 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1319 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1321 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1323 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1326 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1327 LLVMIntPredicate pred
,
1328 LLVMValueRef src0
, LLVMValueRef src1
)
1330 return LLVMBuildSelect(ctx
->builder
,
1331 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1336 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1339 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1340 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1343 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1347 LLVMValueRef cmp
, val
, zero
, one
;
1350 if (bitsize
== 32) {
1360 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
1361 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1362 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
1363 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
1367 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1368 LLVMValueRef src0
, unsigned bitsize
)
1370 LLVMValueRef cmp
, val
, zero
, one
;
1373 if (bitsize
== 32) {
1383 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
1384 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1385 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
1386 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
1390 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1393 const char *intr
= "llvm.floor.f32";
1394 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1395 LLVMValueRef params
[] = {
1398 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1399 ctx
->f32
, params
, 1,
1400 AC_FUNC_ATTR_READNONE
);
1401 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1404 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1406 LLVMValueRef src0
, LLVMValueRef src1
)
1408 LLVMTypeRef ret_type
;
1409 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1411 LLVMValueRef params
[] = { src0
, src1
};
1412 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1415 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1416 params
, 2, AC_FUNC_ATTR_READNONE
);
1418 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1419 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1423 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1426 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1429 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1432 src0
= ac_to_float(ctx
, src0
);
1433 return LLVMBuildSExt(ctx
->builder
,
1434 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1438 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1442 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1447 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
1450 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1453 return LLVMBuildSExt(ctx
->builder
,
1454 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1458 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1461 LLVMValueRef result
;
1462 LLVMValueRef cond
= NULL
;
1464 src0
= ac_to_float(&ctx
->ac
, src0
);
1465 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->ac
.f16
, "");
1467 if (ctx
->options
->chip_class
>= VI
) {
1468 LLVMValueRef args
[2];
1469 /* Check if the result is a denormal - and flush to 0 if so. */
1471 args
[1] = LLVMConstInt(ctx
->ac
.i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1472 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->ac
.i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1475 /* need to convert back up to f32 */
1476 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->ac
.f32
, "");
1478 if (ctx
->options
->chip_class
>= VI
)
1479 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1482 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1483 * so compare the result and flush to 0 if it's smaller.
1485 LLVMValueRef temp
, cond2
;
1486 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1487 ctx
->ac
.f32
, result
);
1488 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1489 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, 0x38800000, false), ctx
->ac
.f32
, ""),
1491 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1492 temp
, ctx
->ac
.f32_0
, "");
1493 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1494 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1499 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1500 LLVMValueRef src0
, LLVMValueRef src1
)
1502 LLVMValueRef dst64
, result
;
1503 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1504 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1506 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1507 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1508 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1512 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1513 LLVMValueRef src0
, LLVMValueRef src1
)
1515 LLVMValueRef dst64
, result
;
1516 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1517 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1519 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1520 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1521 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1525 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1527 const LLVMValueRef srcs
[3])
1529 LLVMValueRef result
;
1530 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1532 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1533 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1537 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1538 LLVMValueRef src0
, LLVMValueRef src1
,
1539 LLVMValueRef src2
, LLVMValueRef src3
)
1541 LLVMValueRef bfi_args
[3], result
;
1543 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1544 LLVMBuildSub(ctx
->builder
,
1545 LLVMBuildShl(ctx
->builder
,
1550 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1553 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1556 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1557 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1559 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1560 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1561 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1563 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1567 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1570 LLVMValueRef comp
[2];
1572 src0
= ac_to_float(ctx
, src0
);
1573 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1574 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1576 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
1579 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1582 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1583 LLVMValueRef temps
[2], result
, val
;
1586 for (i
= 0; i
< 2; i
++) {
1587 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1588 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1589 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1590 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1593 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1595 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1600 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1606 LLVMValueRef result
;
1608 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1609 mask
= AC_TID_MASK_LEFT
;
1610 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1611 mask
= AC_TID_MASK_TOP
;
1613 mask
= AC_TID_MASK_TOP_LEFT
;
1615 /* for DDX we want to next X pixel, DDY next Y pixel. */
1616 if (op
== nir_op_fddx_fine
||
1617 op
== nir_op_fddx_coarse
||
1623 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1628 * this takes an I,J coordinate pair,
1629 * and works out the X and Y derivatives.
1630 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1632 static LLVMValueRef
emit_ddxy_interp(
1633 struct ac_nir_context
*ctx
,
1634 LLVMValueRef interp_ij
)
1636 LLVMValueRef result
[4], a
;
1639 for (i
= 0; i
< 2; i
++) {
1640 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1641 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1642 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1643 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1645 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1648 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1650 LLVMValueRef src
[4], result
= NULL
;
1651 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1652 unsigned src_components
;
1653 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1655 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1656 switch (instr
->op
) {
1662 case nir_op_pack_half_2x16
:
1665 case nir_op_unpack_half_2x16
:
1669 src_components
= num_components
;
1672 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1673 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1675 switch (instr
->op
) {
1681 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1682 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1685 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1688 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1691 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1694 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1695 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1696 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1699 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1700 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1701 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1704 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1707 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1710 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1713 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1716 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1717 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1718 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1719 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1720 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1721 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1722 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1725 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1726 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1727 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1730 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1733 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1736 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1739 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1740 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1741 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1744 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1745 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1746 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1749 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1750 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
1754 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1757 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1760 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1763 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1764 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1765 LLVMTypeOf(src
[0]), ""),
1769 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1770 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1771 LLVMTypeOf(src
[0]), ""),
1775 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1776 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1777 LLVMTypeOf(src
[0]), ""),
1781 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1784 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1787 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1790 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1793 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1796 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1799 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1802 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1805 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1808 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1811 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1812 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1815 result
= emit_iabs(&ctx
->ac
, src
[0]);
1818 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1821 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1824 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1827 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1830 result
= emit_isign(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1833 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1834 result
= emit_fsign(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1837 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1838 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1841 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1842 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1845 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1846 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1848 case nir_op_fround_even
:
1849 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1850 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1853 result
= emit_ffract(&ctx
->ac
, src
[0]);
1856 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1857 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1860 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1861 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1864 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1865 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1868 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1869 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1872 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1873 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1876 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1877 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1878 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
1882 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1883 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1886 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1887 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1888 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1889 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1890 ac_to_float_type(&ctx
->ac
, def_type
),
1894 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1895 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1896 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1897 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1898 ac_to_float_type(&ctx
->ac
, def_type
),
1902 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1903 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1905 case nir_op_ibitfield_extract
:
1906 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1908 case nir_op_ubitfield_extract
:
1909 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1911 case nir_op_bitfield_insert
:
1912 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1914 case nir_op_bitfield_reverse
:
1915 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1917 case nir_op_bit_count
:
1918 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1923 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1924 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1925 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1929 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1930 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1934 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1935 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1939 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1940 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1944 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1945 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1948 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1949 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1952 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1956 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1957 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1958 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1960 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1964 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1965 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1966 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1968 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1971 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1973 case nir_op_find_lsb
:
1974 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1975 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1977 case nir_op_ufind_msb
:
1978 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1979 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1981 case nir_op_ifind_msb
:
1982 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1983 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1985 case nir_op_uadd_carry
:
1986 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1987 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1988 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1990 case nir_op_usub_borrow
:
1991 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1992 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1993 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1996 result
= emit_b2f(&ctx
->ac
, src
[0]);
1999 result
= emit_f2b(&ctx
->ac
, src
[0]);
2002 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
2005 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2006 result
= emit_i2b(&ctx
->ac
, src
[0]);
2008 case nir_op_fquantize2f16
:
2009 result
= emit_f2f16(ctx
->nctx
, src
[0]);
2011 case nir_op_umul_high
:
2012 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2013 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2014 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
2016 case nir_op_imul_high
:
2017 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2018 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2019 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
2021 case nir_op_pack_half_2x16
:
2022 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
2024 case nir_op_unpack_half_2x16
:
2025 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
2029 case nir_op_fddx_fine
:
2030 case nir_op_fddy_fine
:
2031 case nir_op_fddx_coarse
:
2032 case nir_op_fddy_coarse
:
2033 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
2036 case nir_op_unpack_64_2x32_split_x
: {
2037 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2038 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2041 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2046 case nir_op_unpack_64_2x32_split_y
: {
2047 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2048 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2051 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2056 case nir_op_pack_64_2x32_split
: {
2057 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
2058 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2059 src
[0], ctx
->ac
.i32_0
, "");
2060 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2061 src
[1], ctx
->ac
.i32_1
, "");
2062 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
2067 fprintf(stderr
, "Unknown NIR alu instr: ");
2068 nir_print_instr(&instr
->instr
, stderr
);
2069 fprintf(stderr
, "\n");
2074 assert(instr
->dest
.dest
.is_ssa
);
2075 result
= ac_to_integer(&ctx
->ac
, result
);
2076 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
2081 static void visit_load_const(struct ac_nir_context
*ctx
,
2082 const nir_load_const_instr
*instr
)
2084 LLVMValueRef values
[4], value
= NULL
;
2085 LLVMTypeRef element_type
=
2086 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
2088 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
2089 switch (instr
->def
.bit_size
) {
2091 values
[i
] = LLVMConstInt(element_type
,
2092 instr
->value
.u32
[i
], false);
2095 values
[i
] = LLVMConstInt(element_type
,
2096 instr
->value
.u64
[i
], false);
2100 "unsupported nir load_const bit_size: %d\n",
2101 instr
->def
.bit_size
);
2105 if (instr
->def
.num_components
> 1) {
2106 value
= LLVMConstVector(values
, instr
->def
.num_components
);
2110 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
2113 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
2116 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2117 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2118 LLVMPointerType(type
, addr_space
), "");
2122 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
2125 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2126 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
2129 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
2130 /* On VI, the descriptor contains the size in bytes,
2131 * but TXQ must return the size in elements.
2132 * The stride is always non-zero for resources using TXQ.
2134 LLVMValueRef stride
=
2135 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2137 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2138 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2139 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2140 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2142 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2148 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2151 static void build_int_type_name(
2153 char *buf
, unsigned bufsize
)
2155 assert(bufsize
>= 6);
2157 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2158 snprintf(buf
, bufsize
, "v%ui32",
2159 LLVMGetVectorSize(type
));
2164 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2165 struct ac_image_args
*args
,
2166 const nir_tex_instr
*instr
)
2168 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2169 LLVMValueRef coord
= args
->addr
;
2170 LLVMValueRef half_texel
[2];
2171 LLVMValueRef compare_cube_wa
= NULL
;
2172 LLVMValueRef result
;
2174 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2178 struct ac_image_args txq_args
= { 0 };
2180 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2181 txq_args
.opcode
= ac_image_get_resinfo
;
2182 txq_args
.dmask
= 0xf;
2183 txq_args
.addr
= ctx
->i32_0
;
2184 txq_args
.resource
= args
->resource
;
2185 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2187 for (c
= 0; c
< 2; c
++) {
2188 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2189 LLVMConstInt(ctx
->i32
, c
, false), "");
2190 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2191 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2192 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2193 LLVMConstReal(ctx
->f32
, -0.5), "");
2197 LLVMValueRef orig_coords
= args
->addr
;
2199 for (c
= 0; c
< 2; c
++) {
2201 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2202 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2203 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2204 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2205 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2206 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2211 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2212 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2213 * workaround by sampling using a scaled type and converting.
2214 * This is taken from amdgpu-pro shaders.
2216 /* NOTE this produces some ugly code compared to amdgpu-pro,
2217 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2218 * and then reads them back. -pro generates two selects,
2219 * one s_cmp for the descriptor rewriting
2220 * one v_cmp for the coordinate and result changes.
2222 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2223 LLVMValueRef tmp
, tmp2
;
2225 /* workaround 8/8/8/8 uint/sint cube gather bug */
2226 /* first detect it then change to a scaled read and f2i */
2227 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2230 /* extract the DATA_FORMAT */
2231 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2232 LLVMConstInt(ctx
->i32
, 6, false), false);
2234 /* is the DATA_FORMAT == 8_8_8_8 */
2235 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2237 if (stype
== GLSL_TYPE_UINT
)
2238 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2239 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2240 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2242 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2243 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2244 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2246 /* replace the NUM FORMAT in the descriptor */
2247 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2248 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2250 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2252 /* don't modify the coordinates for this case */
2253 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2256 result
= ac_build_image_opcode(ctx
, args
);
2258 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2259 LLVMValueRef tmp
, tmp2
;
2261 /* if the cube workaround is in place, f2i the result. */
2262 for (c
= 0; c
< 4; c
++) {
2263 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2264 if (stype
== GLSL_TYPE_UINT
)
2265 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2267 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2268 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2269 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2270 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2271 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2272 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2278 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2279 const nir_tex_instr
*instr
,
2281 struct ac_image_args
*args
)
2283 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2284 return ac_build_buffer_load_format(&ctx
->ac
,
2291 args
->opcode
= ac_image_sample
;
2292 args
->compare
= instr
->is_shadow
;
2294 switch (instr
->op
) {
2296 case nir_texop_txf_ms
:
2297 case nir_texop_samples_identical
:
2298 args
->opcode
= lod_is_zero
||
2299 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
2300 ac_image_load
: ac_image_load_mip
;
2301 args
->compare
= false;
2302 args
->offset
= false;
2309 args
->level_zero
= true;
2314 case nir_texop_query_levels
:
2315 args
->opcode
= ac_image_get_resinfo
;
2318 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2319 args
->level_zero
= true;
2325 args
->opcode
= ac_image_gather4
;
2326 args
->level_zero
= true;
2329 args
->opcode
= ac_image_get_lod
;
2330 args
->compare
= false;
2331 args
->offset
= false;
2337 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2338 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2339 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2340 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2343 return ac_build_image_opcode(&ctx
->ac
, args
);
2346 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2347 nir_intrinsic_instr
*instr
)
2349 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2350 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2351 unsigned binding
= nir_intrinsic_binding(instr
);
2352 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2353 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2354 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2355 unsigned base_offset
= layout
->binding
[binding
].offset
;
2356 LLVMValueRef offset
, stride
;
2358 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2359 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2360 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2361 layout
->binding
[binding
].dynamic_offset_offset
;
2362 desc_ptr
= ctx
->push_constants
;
2363 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2364 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2366 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2368 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2369 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2370 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2372 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2373 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->ac
.v4i32
);
2374 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2379 static LLVMValueRef
visit_vulkan_resource_reindex(struct nir_to_llvm_context
*ctx
,
2380 nir_intrinsic_instr
*instr
)
2382 LLVMValueRef ptr
= get_src(ctx
->nir
, instr
->src
[0]);
2383 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[1]);
2385 LLVMValueRef result
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2386 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2390 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2391 nir_intrinsic_instr
*instr
)
2393 LLVMValueRef ptr
, addr
;
2395 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2396 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2398 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2399 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2401 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2404 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2405 const nir_intrinsic_instr
*instr
)
2407 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
2409 return get_buffer_size(ctx
, LLVMBuildLoad(ctx
->ac
.builder
, ptr
, ""), false);
2411 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2412 nir_intrinsic_instr
*instr
)
2414 const char *store_name
;
2415 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2416 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2417 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2418 int components_32bit
= elem_size_mult
* instr
->num_components
;
2419 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2420 LLVMValueRef base_data
, base_offset
;
2421 LLVMValueRef params
[6];
2423 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2424 get_src(ctx
, instr
->src
[1]), true);
2425 params
[2] = ctx
->ac
.i32_0
; /* vindex */
2426 params
[4] = ctx
->ac
.i1false
; /* glc */
2427 params
[5] = ctx
->ac
.i1false
; /* slc */
2429 if (components_32bit
> 1)
2430 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2432 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2433 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2434 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2436 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2440 LLVMValueRef offset
;
2442 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2444 /* Due to an LLVM limitation, split 3-element writes
2445 * into a 2-element and a 1-element write. */
2447 writemask
|= 1 << (start
+ 2);
2451 start
*= elem_size_mult
;
2452 count
*= elem_size_mult
;
2455 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2460 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2462 } else if (count
== 2) {
2463 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2464 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2465 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(ctx
->ac
.v2f32
), tmp
,
2468 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2469 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2470 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2472 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2476 if (ac_get_llvm_num_components(base_data
) > 1)
2477 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2478 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2481 store_name
= "llvm.amdgcn.buffer.store.f32";
2484 offset
= base_offset
;
2486 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2490 ac_build_intrinsic(&ctx
->ac
, store_name
,
2491 ctx
->ac
.voidt
, params
, 6, 0);
2495 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2496 const nir_intrinsic_instr
*instr
)
2499 LLVMValueRef params
[6];
2502 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2503 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2505 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2506 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2507 get_src(ctx
, instr
->src
[0]),
2509 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
2510 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2511 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2513 switch (instr
->intrinsic
) {
2514 case nir_intrinsic_ssbo_atomic_add
:
2515 name
= "llvm.amdgcn.buffer.atomic.add";
2517 case nir_intrinsic_ssbo_atomic_imin
:
2518 name
= "llvm.amdgcn.buffer.atomic.smin";
2520 case nir_intrinsic_ssbo_atomic_umin
:
2521 name
= "llvm.amdgcn.buffer.atomic.umin";
2523 case nir_intrinsic_ssbo_atomic_imax
:
2524 name
= "llvm.amdgcn.buffer.atomic.smax";
2526 case nir_intrinsic_ssbo_atomic_umax
:
2527 name
= "llvm.amdgcn.buffer.atomic.umax";
2529 case nir_intrinsic_ssbo_atomic_and
:
2530 name
= "llvm.amdgcn.buffer.atomic.and";
2532 case nir_intrinsic_ssbo_atomic_or
:
2533 name
= "llvm.amdgcn.buffer.atomic.or";
2535 case nir_intrinsic_ssbo_atomic_xor
:
2536 name
= "llvm.amdgcn.buffer.atomic.xor";
2538 case nir_intrinsic_ssbo_atomic_exchange
:
2539 name
= "llvm.amdgcn.buffer.atomic.swap";
2541 case nir_intrinsic_ssbo_atomic_comp_swap
:
2542 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2548 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2551 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2552 const nir_intrinsic_instr
*instr
)
2554 LLVMValueRef results
[2];
2555 int load_components
;
2556 int num_components
= instr
->num_components
;
2557 if (instr
->dest
.ssa
.bit_size
== 64)
2558 num_components
*= 2;
2560 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2561 load_components
= MIN2(num_components
- i
, 4);
2562 const char *load_name
;
2563 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2564 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2565 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2567 if (load_components
== 3)
2568 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2569 else if (load_components
> 1)
2570 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2572 if (load_components
>= 3)
2573 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2574 else if (load_components
== 2)
2575 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2576 else if (load_components
== 1)
2577 load_name
= "llvm.amdgcn.buffer.load.f32";
2579 unreachable("unhandled number of components");
2581 LLVMValueRef params
[] = {
2582 ctx
->abi
->load_ssbo(ctx
->abi
,
2583 get_src(ctx
, instr
->src
[0]),
2591 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2596 LLVMValueRef ret
= results
[0];
2597 if (num_components
> 4 || num_components
== 3) {
2598 LLVMValueRef masks
[] = {
2599 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2600 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2601 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2602 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2605 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2606 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2607 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2610 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2611 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2614 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2615 const nir_intrinsic_instr
*instr
)
2618 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2619 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2620 int num_components
= instr
->num_components
;
2622 if (ctx
->abi
->load_ubo
)
2623 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2625 if (instr
->dest
.ssa
.bit_size
== 64)
2626 num_components
*= 2;
2628 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2629 NULL
, 0, false, false, true, true);
2630 ret
= trim_vector(&ctx
->ac
, ret
, num_components
);
2631 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2632 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2636 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2637 bool vs_in
, unsigned *vertex_index_out
,
2638 LLVMValueRef
*vertex_index_ref
,
2639 unsigned *const_out
, LLVMValueRef
*indir_out
)
2641 unsigned const_offset
= 0;
2642 nir_deref
*tail
= &deref
->deref
;
2643 LLVMValueRef offset
= NULL
;
2645 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2647 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2648 if (vertex_index_out
)
2649 *vertex_index_out
= deref_array
->base_offset
;
2651 if (vertex_index_ref
) {
2652 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2653 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2654 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2656 *vertex_index_ref
= vtx
;
2660 if (deref
->var
->data
.compact
) {
2661 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2662 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2663 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2664 /* We always lower indirect dereferences for "compact" array vars. */
2665 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2667 const_offset
= deref_array
->base_offset
;
2671 while (tail
->child
!= NULL
) {
2672 const struct glsl_type
*parent_type
= tail
->type
;
2675 if (tail
->deref_type
== nir_deref_type_array
) {
2676 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2677 LLVMValueRef index
, stride
, local_offset
;
2678 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2680 const_offset
+= size
* deref_array
->base_offset
;
2681 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2684 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2685 index
= get_src(ctx
, deref_array
->indirect
);
2686 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2687 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2690 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2692 offset
= local_offset
;
2693 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2694 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2696 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2697 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2698 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2701 unreachable("unsupported deref type");
2705 if (const_offset
&& offset
)
2706 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2707 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2710 *const_out
= const_offset
;
2711 *indir_out
= offset
;
2715 /* The offchip buffer layout for TCS->TES is
2717 * - attribute 0 of patch 0 vertex 0
2718 * - attribute 0 of patch 0 vertex 1
2719 * - attribute 0 of patch 0 vertex 2
2721 * - attribute 0 of patch 1 vertex 0
2722 * - attribute 0 of patch 1 vertex 1
2724 * - attribute 1 of patch 0 vertex 0
2725 * - attribute 1 of patch 0 vertex 1
2727 * - per patch attribute 0 of patch 0
2728 * - per patch attribute 0 of patch 1
2731 * Note that every attribute has 4 components.
2733 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2734 LLVMValueRef vertex_index
,
2735 LLVMValueRef param_index
)
2737 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2738 LLVMValueRef param_stride
, constant16
;
2739 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2741 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2742 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2743 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2746 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2748 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2749 vertices_per_patch
, "");
2751 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2754 param_stride
= total_vertices
;
2756 base_addr
= rel_patch_id
;
2757 param_stride
= num_patches
;
2760 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2761 LLVMBuildMul(ctx
->builder
, param_index
,
2762 param_stride
, ""), "");
2764 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2766 if (!vertex_index
) {
2767 LLVMValueRef patch_data_offset
=
2768 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2770 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2771 patch_data_offset
, "");
2776 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2778 unsigned const_index
,
2780 LLVMValueRef vertex_index
,
2781 LLVMValueRef indir_index
)
2783 LLVMValueRef param_index
;
2786 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2789 if (const_index
&& !is_compact
)
2790 param
+= const_index
;
2791 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2793 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2797 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2798 bool is_patch
, uint32_t param
)
2802 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2804 ctx
->tess_outputs_written
|= (1ull << param
);
2808 get_dw_address(struct nir_to_llvm_context
*ctx
,
2809 LLVMValueRef dw_addr
,
2811 unsigned const_index
,
2812 bool compact_const_index
,
2813 LLVMValueRef vertex_index
,
2814 LLVMValueRef stride
,
2815 LLVMValueRef indir_index
)
2820 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2821 LLVMBuildMul(ctx
->builder
,
2827 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2828 LLVMBuildMul(ctx
->builder
, indir_index
,
2829 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2830 else if (const_index
&& !compact_const_index
)
2831 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2832 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2834 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2835 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2837 if (const_index
&& compact_const_index
)
2838 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2839 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2844 load_tcs_varyings(struct ac_shader_abi
*abi
,
2845 LLVMValueRef vertex_index
,
2846 LLVMValueRef indir_index
,
2847 unsigned const_index
,
2849 unsigned driver_location
,
2851 unsigned num_components
,
2856 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2857 LLVMValueRef dw_addr
, stride
;
2858 LLVMValueRef value
[4], result
;
2859 unsigned param
= shader_io_get_unique_index(location
);
2862 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2863 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2866 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2867 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2869 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2874 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2877 for (unsigned i
= 0; i
< num_components
+ component
; i
++) {
2878 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2879 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2882 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
2887 store_tcs_output(struct ac_shader_abi
*abi
,
2888 LLVMValueRef vertex_index
,
2889 LLVMValueRef param_index
,
2890 unsigned const_index
,
2892 unsigned driver_location
,
2899 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2900 LLVMValueRef dw_addr
;
2901 LLVMValueRef stride
= NULL
;
2902 LLVMValueRef buf_addr
= NULL
;
2904 bool store_lds
= true;
2907 if (!(ctx
->tcs_patch_outputs_read
& (1U << (location
- VARYING_SLOT_PATCH0
))))
2910 if (!(ctx
->tcs_outputs_read
& (1ULL << location
)))
2914 param
= shader_io_get_unique_index(location
);
2915 if (location
== VARYING_SLOT_CLIP_DIST0
&&
2916 is_compact
&& const_index
> 3) {
2922 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2923 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2925 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2928 mark_tess_output(ctx
, is_patch
, param
);
2930 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2932 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2933 vertex_index
, param_index
);
2935 bool is_tess_factor
= false;
2936 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2937 location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2938 is_tess_factor
= true;
2940 unsigned base
= is_compact
? const_index
: 0;
2941 for (unsigned chan
= 0; chan
< 8; chan
++) {
2942 if (!(writemask
& (1 << chan
)))
2944 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
2946 if (store_lds
|| is_tess_factor
)
2947 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
2949 if (!is_tess_factor
&& writemask
!= 0xF)
2950 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2951 buf_addr
, ctx
->oc_lds
,
2952 4 * (base
+ chan
), 1, 0, true, false);
2954 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2958 if (writemask
== 0xF) {
2959 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2960 buf_addr
, ctx
->oc_lds
,
2961 (base
* 4), 1, 0, true, false);
2966 load_tes_input(struct ac_shader_abi
*abi
,
2967 LLVMValueRef vertex_index
,
2968 LLVMValueRef param_index
,
2969 unsigned const_index
,
2971 unsigned driver_location
,
2973 unsigned num_components
,
2978 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2979 LLVMValueRef buf_addr
;
2980 LLVMValueRef result
;
2981 unsigned param
= shader_io_get_unique_index(location
);
2983 if (location
== VARYING_SLOT_CLIP_DIST0
&& is_compact
&& const_index
> 3) {
2988 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2989 is_compact
, vertex_index
, param_index
);
2991 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, component
* 4, false);
2992 buf_addr
= LLVMBuildAdd(ctx
->builder
, buf_addr
, comp_offset
, "");
2994 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, num_components
, NULL
,
2995 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2996 result
= trim_vector(&ctx
->ac
, result
, num_components
);
3001 load_gs_input(struct ac_shader_abi
*abi
,
3003 unsigned driver_location
,
3005 unsigned num_components
,
3006 unsigned vertex_index
,
3007 unsigned const_index
,
3010 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3011 LLVMValueRef vtx_offset
;
3012 LLVMValueRef args
[9];
3013 unsigned param
, vtx_offset_param
;
3014 LLVMValueRef value
[4], result
;
3016 vtx_offset_param
= vertex_index
;
3017 assert(vtx_offset_param
< 6);
3018 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
3019 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3021 param
= shader_io_get_unique_index(location
);
3023 for (unsigned i
= component
; i
< num_components
+ component
; i
++) {
3024 if (ctx
->ac
.chip_class
>= GFX9
) {
3025 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
3026 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
3027 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
3028 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
3030 args
[0] = ctx
->esgs_ring
;
3031 args
[1] = vtx_offset
;
3032 args
[2] = LLVMConstInt(ctx
->ac
.i32
, (param
* 4 + i
+ const_index
) * 256, false);
3033 args
[3] = ctx
->ac
.i32_0
;
3034 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
3035 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
3036 args
[6] = ctx
->ac
.i32_1
; /* GLC */
3037 args
[7] = ctx
->ac
.i32_0
; /* SLC */
3038 args
[8] = ctx
->ac
.i32_0
; /* TFE */
3040 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
3041 ctx
->ac
.i32
, args
, 9,
3042 AC_FUNC_ATTR_READONLY
|
3043 AC_FUNC_ATTR_LEGACY
);
3046 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
3052 build_gep_for_deref(struct ac_nir_context
*ctx
,
3053 nir_deref_var
*deref
)
3055 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
3056 assert(entry
->data
);
3057 LLVMValueRef val
= entry
->data
;
3058 nir_deref
*tail
= deref
->deref
.child
;
3059 while (tail
!= NULL
) {
3060 LLVMValueRef offset
;
3061 switch (tail
->deref_type
) {
3062 case nir_deref_type_array
: {
3063 nir_deref_array
*array
= nir_deref_as_array(tail
);
3064 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
3065 if (array
->deref_array_type
==
3066 nir_deref_array_type_indirect
) {
3067 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
3074 case nir_deref_type_struct
: {
3075 nir_deref_struct
*deref_struct
=
3076 nir_deref_as_struct(tail
);
3077 offset
= LLVMConstInt(ctx
->ac
.i32
,
3078 deref_struct
->index
, 0);
3082 unreachable("bad deref type");
3084 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3090 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
3091 nir_intrinsic_instr
*instr
,
3094 LLVMValueRef result
;
3095 LLVMValueRef vertex_index
= NULL
;
3096 LLVMValueRef indir_index
= NULL
;
3097 unsigned const_index
= 0;
3098 unsigned location
= instr
->variables
[0]->var
->data
.location
;
3099 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3100 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3101 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3103 get_deref_offset(ctx
, instr
->variables
[0],
3104 false, NULL
, is_patch
? NULL
: &vertex_index
,
3105 &const_index
, &indir_index
);
3107 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, vertex_index
, indir_index
,
3108 const_index
, location
, driver_location
,
3109 instr
->variables
[0]->var
->data
.location_frac
,
3110 instr
->num_components
,
3111 is_patch
, is_compact
, load_inputs
);
3112 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3115 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3116 nir_intrinsic_instr
*instr
)
3118 LLVMValueRef values
[8];
3119 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3120 int ve
= instr
->dest
.ssa
.num_components
;
3121 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3122 LLVMValueRef indir_index
;
3124 unsigned const_index
;
3125 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
3126 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3127 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3128 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3129 &const_index
, &indir_index
);
3131 if (instr
->dest
.ssa
.bit_size
== 64)
3134 switch (instr
->variables
[0]->var
->data
.mode
) {
3135 case nir_var_shader_in
:
3136 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3137 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3138 return load_tess_varyings(ctx
, instr
, true);
3141 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3142 LLVMValueRef indir_index
;
3143 unsigned const_index
, vertex_index
;
3144 get_deref_offset(ctx
, instr
->variables
[0],
3145 false, &vertex_index
, NULL
,
3146 &const_index
, &indir_index
);
3147 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
3148 instr
->variables
[0]->var
->data
.driver_location
,
3149 instr
->variables
[0]->var
->data
.location_frac
, ve
,
3150 vertex_index
, const_index
,
3151 nir2llvmtype(ctx
, instr
->variables
[0]->var
->type
));
3154 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3156 unsigned count
= glsl_count_attribute_slots(
3157 instr
->variables
[0]->var
->type
,
3158 ctx
->stage
== MESA_SHADER_VERTEX
);
3160 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3161 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3162 stride
, false, true);
3164 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3168 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
3172 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3174 unsigned count
= glsl_count_attribute_slots(
3175 instr
->variables
[0]->var
->type
, false);
3177 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3178 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3179 stride
, true, true);
3181 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3185 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
3189 case nir_var_shared
: {
3190 LLVMValueRef address
= build_gep_for_deref(ctx
,
3191 instr
->variables
[0]);
3192 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3193 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3194 get_def_type(ctx
, &instr
->dest
.ssa
),
3197 case nir_var_shader_out
:
3198 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3199 return load_tess_varyings(ctx
, instr
, false);
3202 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3204 unsigned count
= glsl_count_attribute_slots(
3205 instr
->variables
[0]->var
->type
, false);
3207 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3208 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3209 stride
, true, true);
3211 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3215 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3216 ctx
->outputs
[idx
+ chan
+ const_index
* stride
],
3222 unreachable("unhandle variable mode");
3224 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3225 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3229 visit_store_var(struct ac_nir_context
*ctx
,
3230 nir_intrinsic_instr
*instr
)
3232 LLVMValueRef temp_ptr
, value
;
3233 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3234 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3235 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3236 int writemask
= instr
->const_index
[0] << comp
;
3237 LLVMValueRef indir_index
;
3238 unsigned const_index
;
3239 get_deref_offset(ctx
, instr
->variables
[0], false,
3240 NULL
, NULL
, &const_index
, &indir_index
);
3242 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3243 int old_writemask
= writemask
;
3245 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3246 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
3250 for (unsigned chan
= 0; chan
< 4; chan
++) {
3251 if (old_writemask
& (1 << chan
))
3252 writemask
|= 3u << (2 * chan
);
3256 switch (instr
->variables
[0]->var
->data
.mode
) {
3257 case nir_var_shader_out
:
3259 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3260 LLVMValueRef vertex_index
= NULL
;
3261 LLVMValueRef indir_index
= NULL
;
3262 unsigned const_index
= 0;
3263 const unsigned location
= instr
->variables
[0]->var
->data
.location
;
3264 const unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3265 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3266 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3267 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3269 get_deref_offset(ctx
, instr
->variables
[0],
3270 false, NULL
, is_patch
? NULL
: &vertex_index
,
3271 &const_index
, &indir_index
);
3273 ctx
->abi
->store_tcs_outputs(ctx
->abi
, vertex_index
, indir_index
,
3274 const_index
, location
, driver_location
,
3275 src
, comp
, is_patch
, is_compact
, writemask
);
3279 for (unsigned chan
= 0; chan
< 8; chan
++) {
3281 if (!(writemask
& (1 << chan
)))
3284 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3286 if (instr
->variables
[0]->var
->data
.compact
)
3289 unsigned count
= glsl_count_attribute_slots(
3290 instr
->variables
[0]->var
->type
, false);
3292 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3293 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3294 stride
, true, true);
3296 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3297 value
, indir_index
, "");
3298 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3299 count
, stride
, tmp_vec
);
3302 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3304 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3309 for (unsigned chan
= 0; chan
< 8; chan
++) {
3310 if (!(writemask
& (1 << chan
)))
3313 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
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
->locals
+ idx
+ chan
, count
,
3322 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3323 value
, indir_index
, "");
3324 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3327 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3329 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3333 case nir_var_shared
: {
3334 int writemask
= instr
->const_index
[0];
3335 LLVMValueRef address
= build_gep_for_deref(ctx
,
3336 instr
->variables
[0]);
3337 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3338 unsigned components
=
3339 glsl_get_vector_elements(
3340 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3341 if (writemask
== (1 << components
) - 1) {
3342 val
= LLVMBuildBitCast(
3343 ctx
->ac
.builder
, val
,
3344 LLVMGetElementType(LLVMTypeOf(address
)), "");
3345 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3347 for (unsigned chan
= 0; chan
< 4; chan
++) {
3348 if (!(writemask
& (1 << chan
)))
3351 LLVMBuildStructGEP(ctx
->ac
.builder
,
3353 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
3355 src
= LLVMBuildBitCast(
3356 ctx
->ac
.builder
, src
,
3357 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3358 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3368 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3371 case GLSL_SAMPLER_DIM_BUF
:
3373 case GLSL_SAMPLER_DIM_1D
:
3374 return array
? 2 : 1;
3375 case GLSL_SAMPLER_DIM_2D
:
3376 return array
? 3 : 2;
3377 case GLSL_SAMPLER_DIM_MS
:
3378 return array
? 4 : 3;
3379 case GLSL_SAMPLER_DIM_3D
:
3380 case GLSL_SAMPLER_DIM_CUBE
:
3382 case GLSL_SAMPLER_DIM_RECT
:
3383 case GLSL_SAMPLER_DIM_SUBPASS
:
3385 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3395 /* Adjust the sample index according to FMASK.
3397 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3398 * which is the identity mapping. Each nibble says which physical sample
3399 * should be fetched to get that sample.
3401 * For example, 0x11111100 means there are only 2 samples stored and
3402 * the second sample covers 3/4 of the pixel. When reading samples 0
3403 * and 1, return physical sample 0 (determined by the first two 0s
3404 * in FMASK), otherwise return physical sample 1.
3406 * The sample index should be adjusted as follows:
3407 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3409 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3410 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3411 LLVMValueRef coord_z
,
3412 LLVMValueRef sample_index
,
3413 LLVMValueRef fmask_desc_ptr
)
3415 LLVMValueRef fmask_load_address
[4];
3418 fmask_load_address
[0] = coord_x
;
3419 fmask_load_address
[1] = coord_y
;
3421 fmask_load_address
[2] = coord_z
;
3422 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3425 struct ac_image_args args
= {0};
3427 args
.opcode
= ac_image_load
;
3428 args
.da
= coord_z
? true : false;
3429 args
.resource
= fmask_desc_ptr
;
3431 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3433 res
= ac_build_image_opcode(ctx
, &args
);
3435 res
= ac_to_integer(ctx
, res
);
3436 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3437 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3439 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3443 LLVMValueRef sample_index4
=
3444 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3445 LLVMValueRef shifted_fmask
=
3446 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3447 LLVMValueRef final_sample
=
3448 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3450 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3451 * resource descriptor is 0 (invalid),
3453 LLVMValueRef fmask_desc
=
3454 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3457 LLVMValueRef fmask_word1
=
3458 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3461 LLVMValueRef word1_is_nonzero
=
3462 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3463 fmask_word1
, ctx
->i32_0
, "");
3465 /* Replace the MSAA sample index. */
3467 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3468 final_sample
, sample_index
, "");
3469 return sample_index
;
3472 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3473 const nir_intrinsic_instr
*instr
)
3475 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3476 if(instr
->variables
[0]->deref
.child
)
3477 type
= instr
->variables
[0]->deref
.child
->type
;
3479 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3480 LLVMValueRef coords
[4];
3481 LLVMValueRef masks
[] = {
3482 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3483 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3486 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3489 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3490 bool is_array
= glsl_sampler_type_is_array(type
);
3491 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3492 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3493 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3494 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3495 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3496 count
= image_type_to_components_count(dim
, is_array
);
3499 LLVMValueRef fmask_load_address
[3];
3502 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3503 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3505 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3507 fmask_load_address
[2] = NULL
;
3509 for (chan
= 0; chan
< 2; ++chan
)
3510 fmask_load_address
[chan
] =
3511 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3512 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3513 ctx
->ac
.i32
, ""), "");
3514 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3516 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3517 fmask_load_address
[0],
3518 fmask_load_address
[1],
3519 fmask_load_address
[2],
3521 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3523 if (count
== 1 && !gfx9_1d
) {
3524 if (instr
->src
[0].ssa
->num_components
)
3525 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3532 for (chan
= 0; chan
< count
; ++chan
) {
3533 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3536 for (chan
= 0; chan
< 2; ++chan
)
3537 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3538 ctx
->ac
.i32
, ""), "");
3539 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3545 coords
[2] = coords
[1];
3546 coords
[1] = ctx
->ac
.i32_0
;
3548 coords
[1] = ctx
->ac
.i32_0
;
3553 coords
[count
] = sample_index
;
3558 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3561 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3566 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3567 const nir_intrinsic_instr
*instr
)
3569 LLVMValueRef params
[7];
3571 char intrinsic_name
[64];
3572 const nir_variable
*var
= instr
->variables
[0]->var
;
3573 const struct glsl_type
*type
= var
->type
;
3575 if(instr
->variables
[0]->deref
.child
)
3576 type
= instr
->variables
[0]->deref
.child
->type
;
3578 type
= glsl_without_array(type
);
3579 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3580 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3581 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3582 ctx
->ac
.i32_0
, ""); /* vindex */
3583 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3584 params
[3] = ctx
->ac
.i1false
; /* glc */
3585 params
[4] = ctx
->ac
.i1false
; /* slc */
3586 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3589 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3590 res
= ac_to_integer(&ctx
->ac
, res
);
3592 bool is_da
= glsl_sampler_type_is_array(type
) ||
3593 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3594 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3595 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3596 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3597 LLVMValueRef glc
= ctx
->ac
.i1false
;
3598 LLVMValueRef slc
= ctx
->ac
.i1false
;
3600 params
[0] = get_image_coords(ctx
, instr
);
3601 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3602 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3603 if (HAVE_LLVM
<= 0x0309) {
3604 params
[3] = ctx
->ac
.i1false
; /* r128 */
3609 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3616 ac_get_image_intr_name("llvm.amdgcn.image.load",
3617 ctx
->ac
.v4f32
, /* vdata */
3618 LLVMTypeOf(params
[0]), /* coords */
3619 LLVMTypeOf(params
[1]), /* rsrc */
3620 intrinsic_name
, sizeof(intrinsic_name
));
3622 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3623 params
, 7, AC_FUNC_ATTR_READONLY
);
3625 return ac_to_integer(&ctx
->ac
, res
);
3628 static void visit_image_store(struct ac_nir_context
*ctx
,
3629 nir_intrinsic_instr
*instr
)
3631 LLVMValueRef params
[8];
3632 char intrinsic_name
[64];
3633 const nir_variable
*var
= instr
->variables
[0]->var
;
3634 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3635 LLVMValueRef glc
= ctx
->ac
.i1false
;
3636 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3638 glc
= ctx
->ac
.i1true
;
3640 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3641 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3642 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3643 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3644 ctx
->ac
.i32_0
, ""); /* vindex */
3645 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3646 params
[4] = glc
; /* glc */
3647 params
[5] = ctx
->ac
.i1false
; /* slc */
3648 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3651 bool is_da
= glsl_sampler_type_is_array(type
) ||
3652 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3653 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3654 LLVMValueRef slc
= ctx
->ac
.i1false
;
3656 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3657 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3658 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3659 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3660 if (HAVE_LLVM
<= 0x0309) {
3661 params
[4] = ctx
->ac
.i1false
; /* r128 */
3666 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3673 ac_get_image_intr_name("llvm.amdgcn.image.store",
3674 LLVMTypeOf(params
[0]), /* vdata */
3675 LLVMTypeOf(params
[1]), /* coords */
3676 LLVMTypeOf(params
[2]), /* rsrc */
3677 intrinsic_name
, sizeof(intrinsic_name
));
3679 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3685 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3686 const nir_intrinsic_instr
*instr
)
3688 LLVMValueRef params
[7];
3689 int param_count
= 0;
3690 const nir_variable
*var
= instr
->variables
[0]->var
;
3692 const char *atomic_name
;
3693 char intrinsic_name
[41];
3694 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3695 MAYBE_UNUSED
int length
;
3697 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3699 switch (instr
->intrinsic
) {
3700 case nir_intrinsic_image_atomic_add
:
3701 atomic_name
= "add";
3703 case nir_intrinsic_image_atomic_min
:
3704 atomic_name
= is_unsigned
? "umin" : "smin";
3706 case nir_intrinsic_image_atomic_max
:
3707 atomic_name
= is_unsigned
? "umax" : "smax";
3709 case nir_intrinsic_image_atomic_and
:
3710 atomic_name
= "and";
3712 case nir_intrinsic_image_atomic_or
:
3715 case nir_intrinsic_image_atomic_xor
:
3716 atomic_name
= "xor";
3718 case nir_intrinsic_image_atomic_exchange
:
3719 atomic_name
= "swap";
3721 case nir_intrinsic_image_atomic_comp_swap
:
3722 atomic_name
= "cmpswap";
3728 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3729 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3730 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3732 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3733 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3735 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3736 ctx
->ac
.i32_0
, ""); /* vindex */
3737 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3738 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3740 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3741 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3743 char coords_type
[8];
3745 bool da
= glsl_sampler_type_is_array(type
) ||
3746 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3748 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3749 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3751 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3752 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3753 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3755 build_int_type_name(LLVMTypeOf(coords
),
3756 coords_type
, sizeof(coords_type
));
3758 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3759 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3762 assert(length
< sizeof(intrinsic_name
));
3763 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3766 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3767 const nir_intrinsic_instr
*instr
)
3770 const nir_variable
*var
= instr
->variables
[0]->var
;
3771 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3772 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3773 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3774 if(instr
->variables
[0]->deref
.child
)
3775 type
= instr
->variables
[0]->deref
.child
->type
;
3777 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3778 return get_buffer_size(ctx
,
3779 get_sampler_desc(ctx
, instr
->variables
[0],
3780 AC_DESC_BUFFER
, NULL
, true, false), true);
3782 struct ac_image_args args
= { 0 };
3786 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3787 args
.opcode
= ac_image_get_resinfo
;
3788 args
.addr
= ctx
->ac
.i32_0
;
3790 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3792 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3794 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3795 glsl_sampler_type_is_array(type
)) {
3796 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3797 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3798 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3799 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3801 if (ctx
->ac
.chip_class
>= GFX9
&&
3802 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3803 glsl_sampler_type_is_array(type
)) {
3804 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3805 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3812 #define NOOP_WAITCNT 0xf7f
3813 #define LGKM_CNT 0x07f
3814 #define VM_CNT 0xf70
3816 static void emit_membar(struct nir_to_llvm_context
*ctx
,
3817 const nir_intrinsic_instr
*instr
)
3819 unsigned waitcnt
= NOOP_WAITCNT
;
3821 switch (instr
->intrinsic
) {
3822 case nir_intrinsic_memory_barrier
:
3823 case nir_intrinsic_group_memory_barrier
:
3824 waitcnt
&= VM_CNT
& LGKM_CNT
;
3826 case nir_intrinsic_memory_barrier_atomic_counter
:
3827 case nir_intrinsic_memory_barrier_buffer
:
3828 case nir_intrinsic_memory_barrier_image
:
3831 case nir_intrinsic_memory_barrier_shared
:
3832 waitcnt
&= LGKM_CNT
;
3837 if (waitcnt
!= NOOP_WAITCNT
)
3838 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3841 static void emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3843 /* SI only (thanks to a hw bug workaround):
3844 * The real barrier instruction isn’t needed, because an entire patch
3845 * always fits into a single wave.
3847 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
3848 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
3851 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
3852 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3855 static void emit_discard(struct ac_nir_context
*ctx
,
3856 const nir_intrinsic_instr
*instr
)
3860 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3861 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3862 get_src(ctx
, instr
->src
[0]),
3865 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3866 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
3869 ac_build_kill_if_false(&ctx
->ac
, cond
);
3873 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3875 LLVMValueRef result
;
3876 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3877 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3878 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3880 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3883 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3884 const nir_intrinsic_instr
*instr
)
3886 LLVMValueRef ptr
, result
;
3887 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3888 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3890 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3891 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3892 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3894 LLVMAtomicOrderingSequentiallyConsistent
,
3895 LLVMAtomicOrderingSequentiallyConsistent
,
3898 LLVMAtomicRMWBinOp op
;
3899 switch (instr
->intrinsic
) {
3900 case nir_intrinsic_var_atomic_add
:
3901 op
= LLVMAtomicRMWBinOpAdd
;
3903 case nir_intrinsic_var_atomic_umin
:
3904 op
= LLVMAtomicRMWBinOpUMin
;
3906 case nir_intrinsic_var_atomic_umax
:
3907 op
= LLVMAtomicRMWBinOpUMax
;
3909 case nir_intrinsic_var_atomic_imin
:
3910 op
= LLVMAtomicRMWBinOpMin
;
3912 case nir_intrinsic_var_atomic_imax
:
3913 op
= LLVMAtomicRMWBinOpMax
;
3915 case nir_intrinsic_var_atomic_and
:
3916 op
= LLVMAtomicRMWBinOpAnd
;
3918 case nir_intrinsic_var_atomic_or
:
3919 op
= LLVMAtomicRMWBinOpOr
;
3921 case nir_intrinsic_var_atomic_xor
:
3922 op
= LLVMAtomicRMWBinOpXor
;
3924 case nir_intrinsic_var_atomic_exchange
:
3925 op
= LLVMAtomicRMWBinOpXchg
;
3931 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3932 LLVMAtomicOrderingSequentiallyConsistent
,
3938 #define INTERP_CENTER 0
3939 #define INTERP_CENTROID 1
3940 #define INTERP_SAMPLE 2
3942 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3943 enum glsl_interp_mode interp
, unsigned location
)
3946 case INTERP_MODE_FLAT
:
3949 case INTERP_MODE_SMOOTH
:
3950 case INTERP_MODE_NONE
:
3951 if (location
== INTERP_CENTER
)
3952 return ctx
->persp_center
;
3953 else if (location
== INTERP_CENTROID
)
3954 return ctx
->persp_centroid
;
3955 else if (location
== INTERP_SAMPLE
)
3956 return ctx
->persp_sample
;
3958 case INTERP_MODE_NOPERSPECTIVE
:
3959 if (location
== INTERP_CENTER
)
3960 return ctx
->linear_center
;
3961 else if (location
== INTERP_CENTROID
)
3962 return ctx
->linear_centroid
;
3963 else if (location
== INTERP_SAMPLE
)
3964 return ctx
->linear_sample
;
3970 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3971 LLVMValueRef sample_id
)
3973 LLVMValueRef result
;
3974 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
3976 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3977 const_array(ctx
->ac
.v2f32
, 64), "");
3979 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3980 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
3985 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3987 LLVMValueRef values
[2];
3989 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3990 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3991 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3994 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3995 const nir_intrinsic_instr
*instr
)
3997 LLVMValueRef result
[4];
3998 LLVMValueRef interp_param
, attr_number
;
4001 LLVMValueRef src_c0
= NULL
;
4002 LLVMValueRef src_c1
= NULL
;
4003 LLVMValueRef src0
= NULL
;
4004 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
4005 switch (instr
->intrinsic
) {
4006 case nir_intrinsic_interp_var_at_centroid
:
4007 location
= INTERP_CENTROID
;
4009 case nir_intrinsic_interp_var_at_sample
:
4010 case nir_intrinsic_interp_var_at_offset
:
4011 location
= INTERP_CENTER
;
4012 src0
= get_src(ctx
->nir
, instr
->src
[0]);
4018 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
4019 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_0
, ""));
4020 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_1
, ""));
4021 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
4022 LLVMValueRef sample_position
;
4023 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
4025 /* fetch sample ID */
4026 sample_position
= load_sample_position(ctx
, src0
);
4028 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_0
, "");
4029 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
4030 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_1
, "");
4031 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
4033 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
4034 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
4036 if (location
== INTERP_CENTER
) {
4037 LLVMValueRef ij_out
[2];
4038 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
4041 * take the I then J parameters, and the DDX/Y for it, and
4042 * calculate the IJ inputs for the interpolator.
4043 * temp1 = ddx * offset/sample.x + I;
4044 * interp_param.I = ddy * offset/sample.y + temp1;
4045 * temp1 = ddx * offset/sample.x + J;
4046 * interp_param.J = ddy * offset/sample.y + temp1;
4048 for (unsigned i
= 0; i
< 2; i
++) {
4049 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
4050 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
4051 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
4052 ddxy_out
, ix_ll
, "");
4053 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
4054 ddxy_out
, iy_ll
, "");
4055 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
4056 interp_param
, ix_ll
, "");
4057 LLVMValueRef temp1
, temp2
;
4059 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
4062 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
4063 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
4065 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
4066 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
4068 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
4069 temp2
, ctx
->ac
.i32
, "");
4071 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
4075 for (chan
= 0; chan
< 4; chan
++) {
4076 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
4079 interp_param
= LLVMBuildBitCast(ctx
->builder
,
4080 interp_param
, ctx
->ac
.v2f32
, "");
4081 LLVMValueRef i
= LLVMBuildExtractElement(
4082 ctx
->builder
, interp_param
, ctx
->ac
.i32_0
, "");
4083 LLVMValueRef j
= LLVMBuildExtractElement(
4084 ctx
->builder
, interp_param
, ctx
->ac
.i32_1
, "");
4086 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4087 llvm_chan
, attr_number
,
4088 ctx
->prim_mask
, i
, j
);
4090 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4091 LLVMConstInt(ctx
->ac
.i32
, 2, false),
4092 llvm_chan
, attr_number
,
4096 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
4097 instr
->variables
[0]->var
->data
.location_frac
);
4101 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
4103 LLVMValueRef gs_next_vertex
;
4104 LLVMValueRef can_emit
;
4106 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4108 /* Write vertex attribute values to GSVS ring */
4109 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
4110 ctx
->gs_next_vertex
,
4113 /* If this thread has already emitted the declared maximum number of
4114 * vertices, kill it: excessive vertex emissions are not supposed to
4115 * have any effect, and GS threads have no externally observable
4116 * effects other than emitting vertices.
4118 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
4119 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
4120 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4122 /* loop num outputs */
4124 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4125 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
4130 if (!(ctx
->output_mask
& (1ull << i
)))
4133 if (i
== VARYING_SLOT_CLIP_DIST0
) {
4134 /* pack clip and cull into a single set of slots */
4135 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4139 for (unsigned j
= 0; j
< length
; j
++) {
4140 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
4142 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
4143 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
4144 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
4146 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
4148 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
4150 voffset
, ctx
->gs2vs_offset
, 0,
4156 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4158 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4160 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4164 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
4165 const nir_intrinsic_instr
*instr
)
4167 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4171 load_tess_coord(struct ac_shader_abi
*abi
, LLVMTypeRef type
,
4172 unsigned num_components
)
4174 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4176 LLVMValueRef coord
[4] = {
4183 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4184 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->ac
.f32_1
,
4185 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4187 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, num_components
);
4188 return LLVMBuildBitCast(ctx
->builder
, result
, type
, "");
4192 load_patch_vertices_in(struct ac_shader_abi
*abi
)
4194 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4195 return LLVMConstInt(ctx
->ac
.i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
4198 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4199 nir_intrinsic_instr
*instr
)
4201 LLVMValueRef result
= NULL
;
4203 switch (instr
->intrinsic
) {
4204 case nir_intrinsic_load_work_group_id
: {
4205 LLVMValueRef values
[3];
4207 for (int i
= 0; i
< 3; i
++) {
4208 values
[i
] = ctx
->nctx
->workgroup_ids
[i
] ?
4209 ctx
->nctx
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
4212 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
4215 case nir_intrinsic_load_base_vertex
: {
4216 result
= ctx
->abi
->base_vertex
;
4219 case nir_intrinsic_load_vertex_id_zero_base
: {
4220 result
= ctx
->abi
->vertex_id
;
4223 case nir_intrinsic_load_local_invocation_id
: {
4224 result
= ctx
->nctx
->local_invocation_ids
;
4227 case nir_intrinsic_load_base_instance
:
4228 result
= ctx
->abi
->start_instance
;
4230 case nir_intrinsic_load_draw_id
:
4231 result
= ctx
->abi
->draw_id
;
4233 case nir_intrinsic_load_view_index
:
4234 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4236 case nir_intrinsic_load_invocation_id
:
4237 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4238 result
= unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
4240 result
= ctx
->abi
->gs_invocation_id
;
4242 case nir_intrinsic_load_primitive_id
:
4243 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4244 result
= ctx
->abi
->gs_prim_id
;
4245 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4246 result
= ctx
->abi
->tcs_patch_id
;
4247 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4248 result
= ctx
->abi
->tes_patch_id
;
4250 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4252 case nir_intrinsic_load_sample_id
:
4253 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4255 case nir_intrinsic_load_sample_pos
:
4256 result
= load_sample_pos(ctx
);
4258 case nir_intrinsic_load_sample_mask_in
:
4259 result
= ctx
->abi
->sample_coverage
;
4261 case nir_intrinsic_load_frag_coord
: {
4262 LLVMValueRef values
[4] = {
4263 ctx
->abi
->frag_pos
[0],
4264 ctx
->abi
->frag_pos
[1],
4265 ctx
->abi
->frag_pos
[2],
4266 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4268 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4271 case nir_intrinsic_load_front_face
:
4272 result
= ctx
->abi
->front_face
;
4274 case nir_intrinsic_load_instance_id
:
4275 result
= ctx
->abi
->instance_id
;
4277 case nir_intrinsic_load_num_work_groups
:
4278 result
= ctx
->nctx
->num_work_groups
;
4280 case nir_intrinsic_load_local_invocation_index
:
4281 result
= visit_load_local_invocation_index(ctx
->nctx
);
4283 case nir_intrinsic_load_push_constant
:
4284 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4286 case nir_intrinsic_vulkan_resource_index
:
4287 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4289 case nir_intrinsic_vulkan_resource_reindex
:
4290 result
= visit_vulkan_resource_reindex(ctx
->nctx
, instr
);
4292 case nir_intrinsic_store_ssbo
:
4293 visit_store_ssbo(ctx
, instr
);
4295 case nir_intrinsic_load_ssbo
:
4296 result
= visit_load_buffer(ctx
, instr
);
4298 case nir_intrinsic_ssbo_atomic_add
:
4299 case nir_intrinsic_ssbo_atomic_imin
:
4300 case nir_intrinsic_ssbo_atomic_umin
:
4301 case nir_intrinsic_ssbo_atomic_imax
:
4302 case nir_intrinsic_ssbo_atomic_umax
:
4303 case nir_intrinsic_ssbo_atomic_and
:
4304 case nir_intrinsic_ssbo_atomic_or
:
4305 case nir_intrinsic_ssbo_atomic_xor
:
4306 case nir_intrinsic_ssbo_atomic_exchange
:
4307 case nir_intrinsic_ssbo_atomic_comp_swap
:
4308 result
= visit_atomic_ssbo(ctx
, instr
);
4310 case nir_intrinsic_load_ubo
:
4311 result
= visit_load_ubo_buffer(ctx
, instr
);
4313 case nir_intrinsic_get_buffer_size
:
4314 result
= visit_get_buffer_size(ctx
, instr
);
4316 case nir_intrinsic_load_var
:
4317 result
= visit_load_var(ctx
, instr
);
4319 case nir_intrinsic_store_var
:
4320 visit_store_var(ctx
, instr
);
4322 case nir_intrinsic_image_load
:
4323 result
= visit_image_load(ctx
, instr
);
4325 case nir_intrinsic_image_store
:
4326 visit_image_store(ctx
, instr
);
4328 case nir_intrinsic_image_atomic_add
:
4329 case nir_intrinsic_image_atomic_min
:
4330 case nir_intrinsic_image_atomic_max
:
4331 case nir_intrinsic_image_atomic_and
:
4332 case nir_intrinsic_image_atomic_or
:
4333 case nir_intrinsic_image_atomic_xor
:
4334 case nir_intrinsic_image_atomic_exchange
:
4335 case nir_intrinsic_image_atomic_comp_swap
:
4336 result
= visit_image_atomic(ctx
, instr
);
4338 case nir_intrinsic_image_size
:
4339 result
= visit_image_size(ctx
, instr
);
4341 case nir_intrinsic_discard
:
4342 case nir_intrinsic_discard_if
:
4343 emit_discard(ctx
, instr
);
4345 case nir_intrinsic_memory_barrier
:
4346 case nir_intrinsic_group_memory_barrier
:
4347 case nir_intrinsic_memory_barrier_atomic_counter
:
4348 case nir_intrinsic_memory_barrier_buffer
:
4349 case nir_intrinsic_memory_barrier_image
:
4350 case nir_intrinsic_memory_barrier_shared
:
4351 emit_membar(ctx
->nctx
, instr
);
4353 case nir_intrinsic_barrier
:
4354 emit_barrier(&ctx
->ac
, ctx
->stage
);
4356 case nir_intrinsic_var_atomic_add
:
4357 case nir_intrinsic_var_atomic_imin
:
4358 case nir_intrinsic_var_atomic_umin
:
4359 case nir_intrinsic_var_atomic_imax
:
4360 case nir_intrinsic_var_atomic_umax
:
4361 case nir_intrinsic_var_atomic_and
:
4362 case nir_intrinsic_var_atomic_or
:
4363 case nir_intrinsic_var_atomic_xor
:
4364 case nir_intrinsic_var_atomic_exchange
:
4365 case nir_intrinsic_var_atomic_comp_swap
:
4366 result
= visit_var_atomic(ctx
->nctx
, instr
);
4368 case nir_intrinsic_interp_var_at_centroid
:
4369 case nir_intrinsic_interp_var_at_sample
:
4370 case nir_intrinsic_interp_var_at_offset
:
4371 result
= visit_interp(ctx
->nctx
, instr
);
4373 case nir_intrinsic_emit_vertex
:
4374 assert(instr
->const_index
[0] == 0);
4375 ctx
->abi
->emit_vertex(ctx
->abi
, 0, ctx
->outputs
);
4377 case nir_intrinsic_end_primitive
:
4378 visit_end_primitive(ctx
->nctx
, instr
);
4380 case nir_intrinsic_load_tess_coord
: {
4381 LLVMTypeRef type
= ctx
->nctx
?
4382 get_def_type(ctx
->nctx
->nir
, &instr
->dest
.ssa
) :
4384 result
= ctx
->abi
->load_tess_coord(ctx
->abi
, type
, instr
->num_components
);
4387 case nir_intrinsic_load_tess_level_outer
:
4388 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
4390 case nir_intrinsic_load_tess_level_inner
:
4391 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
4393 case nir_intrinsic_load_patch_vertices_in
:
4394 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
4397 fprintf(stderr
, "Unknown intrinsic: ");
4398 nir_print_instr(&instr
->instr
, stderr
);
4399 fprintf(stderr
, "\n");
4403 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4407 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4408 LLVMValueRef buffer_ptr
, bool write
)
4410 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4412 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4413 ctx
->shader_info
->fs
.writes_memory
= true;
4415 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4418 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef buffer_ptr
)
4420 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4422 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4425 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4426 unsigned descriptor_set
,
4427 unsigned base_index
,
4428 unsigned constant_index
,
4430 enum ac_descriptor_type desc_type
,
4431 bool image
, bool write
)
4433 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4434 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4435 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4436 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4437 unsigned offset
= binding
->offset
;
4438 unsigned stride
= binding
->size
;
4440 LLVMBuilderRef builder
= ctx
->builder
;
4443 assert(base_index
< layout
->binding_count
);
4445 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4446 ctx
->shader_info
->fs
.writes_memory
= true;
4448 switch (desc_type
) {
4450 type
= ctx
->ac
.v8i32
;
4454 type
= ctx
->ac
.v8i32
;
4458 case AC_DESC_SAMPLER
:
4459 type
= ctx
->ac
.v4i32
;
4460 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4465 case AC_DESC_BUFFER
:
4466 type
= ctx
->ac
.v4i32
;
4470 unreachable("invalid desc_type\n");
4473 offset
+= constant_index
* stride
;
4475 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4476 (!index
|| binding
->immutable_samplers_equal
)) {
4477 if (binding
->immutable_samplers_equal
)
4480 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4482 LLVMValueRef constants
[] = {
4483 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4484 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4485 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4486 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4488 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4491 assert(stride
% type_size
== 0);
4494 index
= ctx
->ac
.i32_0
;
4496 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4498 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4499 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4501 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4504 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4505 const nir_deref_var
*deref
,
4506 enum ac_descriptor_type desc_type
,
4507 const nir_tex_instr
*tex_instr
,
4508 bool image
, bool write
)
4510 LLVMValueRef index
= NULL
;
4511 unsigned constant_index
= 0;
4512 unsigned descriptor_set
;
4513 unsigned base_index
;
4516 assert(tex_instr
&& !image
);
4518 base_index
= tex_instr
->sampler_index
;
4520 const nir_deref
*tail
= &deref
->deref
;
4521 while (tail
->child
) {
4522 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4523 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4528 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4530 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4531 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4533 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4534 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4539 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4542 constant_index
+= child
->base_offset
* array_size
;
4544 tail
= &child
->deref
;
4546 descriptor_set
= deref
->var
->data
.descriptor_set
;
4547 base_index
= deref
->var
->data
.binding
;
4550 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4553 constant_index
, index
,
4554 desc_type
, image
, write
);
4557 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4558 struct ac_image_args
*args
,
4559 const nir_tex_instr
*instr
,
4561 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4562 LLVMValueRef
*param
, unsigned count
,
4565 unsigned is_rect
= 0;
4566 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4568 if (op
== nir_texop_lod
)
4570 /* Pad to power of two vector */
4571 while (count
< util_next_power_of_two(count
))
4572 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4575 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4577 args
->addr
= param
[0];
4579 args
->resource
= res_ptr
;
4580 args
->sampler
= samp_ptr
;
4582 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4583 args
->addr
= param
[0];
4587 args
->dmask
= dmask
;
4588 args
->unorm
= is_rect
;
4592 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4595 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4596 * filtering manually. The driver sets img7 to a mask clearing
4597 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4598 * s_and_b32 samp0, samp0, img7
4601 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4603 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4604 LLVMValueRef res
, LLVMValueRef samp
)
4606 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4607 LLVMValueRef img7
, samp0
;
4609 if (ctx
->ac
.chip_class
>= VI
)
4612 img7
= LLVMBuildExtractElement(builder
, res
,
4613 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4614 samp0
= LLVMBuildExtractElement(builder
, samp
,
4615 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4616 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4617 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4618 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4621 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4622 nir_tex_instr
*instr
,
4623 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4624 LLVMValueRef
*fmask_ptr
)
4626 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4627 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4629 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4632 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4634 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4635 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4636 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4638 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4639 instr
->op
== nir_texop_samples_identical
))
4640 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4643 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4646 coord
= ac_to_float(ctx
, coord
);
4647 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4648 coord
= ac_to_integer(ctx
, coord
);
4652 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4654 LLVMValueRef result
= NULL
;
4655 struct ac_image_args args
= { 0 };
4656 unsigned dmask
= 0xf;
4657 LLVMValueRef address
[16];
4658 LLVMValueRef coords
[5];
4659 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4660 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4661 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4662 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4663 LLVMValueRef derivs
[6];
4664 unsigned chan
, count
= 0;
4665 unsigned const_src
= 0, num_deriv_comp
= 0;
4666 bool lod_is_zero
= false;
4668 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4670 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4671 switch (instr
->src
[i
].src_type
) {
4672 case nir_tex_src_coord
:
4673 coord
= get_src(ctx
, instr
->src
[i
].src
);
4675 case nir_tex_src_projector
:
4677 case nir_tex_src_comparator
:
4678 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4680 case nir_tex_src_offset
:
4681 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4684 case nir_tex_src_bias
:
4685 bias
= get_src(ctx
, instr
->src
[i
].src
);
4687 case nir_tex_src_lod
: {
4688 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4690 if (val
&& val
->i32
[0] == 0)
4692 lod
= get_src(ctx
, instr
->src
[i
].src
);
4695 case nir_tex_src_ms_index
:
4696 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4698 case nir_tex_src_ms_mcs
:
4700 case nir_tex_src_ddx
:
4701 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4702 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4704 case nir_tex_src_ddy
:
4705 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4707 case nir_tex_src_texture_offset
:
4708 case nir_tex_src_sampler_offset
:
4709 case nir_tex_src_plane
:
4715 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4716 result
= get_buffer_size(ctx
, res_ptr
, true);
4720 if (instr
->op
== nir_texop_texture_samples
) {
4721 LLVMValueRef res
, samples
, is_msaa
;
4722 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4723 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4724 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4725 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4726 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4727 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4728 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4729 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4730 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4732 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4733 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4734 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4735 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4736 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4738 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4745 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4746 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4748 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4749 LLVMValueRef offset
[3], pack
;
4750 for (chan
= 0; chan
< 3; ++chan
)
4751 offset
[chan
] = ctx
->ac
.i32_0
;
4754 for (chan
= 0; chan
< ac_get_llvm_num_components(offsets
); chan
++) {
4755 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4756 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4757 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4759 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4760 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4762 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4763 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4764 address
[count
++] = pack
;
4767 /* pack LOD bias value */
4768 if (instr
->op
== nir_texop_txb
&& bias
) {
4769 address
[count
++] = bias
;
4772 /* Pack depth comparison value */
4773 if (instr
->is_shadow
&& comparator
) {
4774 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4775 ac_llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4777 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
4778 * so the depth comparison value isn't clamped for Z16 and
4779 * Z24 anymore. Do it manually here.
4781 * It's unnecessary if the original texture format was
4782 * Z32_FLOAT, but we don't know that here.
4784 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
4785 z
= ac_build_clamp(&ctx
->ac
, z
);
4787 address
[count
++] = z
;
4790 /* pack derivatives */
4792 int num_src_deriv_channels
, num_dest_deriv_channels
;
4793 switch (instr
->sampler_dim
) {
4794 case GLSL_SAMPLER_DIM_3D
:
4795 case GLSL_SAMPLER_DIM_CUBE
:
4797 num_src_deriv_channels
= 3;
4798 num_dest_deriv_channels
= 3;
4800 case GLSL_SAMPLER_DIM_2D
:
4802 num_src_deriv_channels
= 2;
4803 num_dest_deriv_channels
= 2;
4806 case GLSL_SAMPLER_DIM_1D
:
4807 num_src_deriv_channels
= 1;
4808 if (ctx
->ac
.chip_class
>= GFX9
) {
4809 num_dest_deriv_channels
= 2;
4812 num_dest_deriv_channels
= 1;
4818 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4819 derivs
[i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4820 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4822 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4823 derivs
[i
] = ctx
->ac
.f32_0
;
4824 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4828 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4829 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4830 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4831 if (instr
->coord_components
== 3)
4832 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4833 ac_prepare_cube_coords(&ctx
->ac
,
4834 instr
->op
== nir_texop_txd
, instr
->is_array
,
4835 instr
->op
== nir_texop_lod
, coords
, derivs
);
4841 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4842 address
[count
++] = derivs
[i
];
4845 /* Pack texture coordinates */
4847 address
[count
++] = coords
[0];
4848 if (instr
->coord_components
> 1) {
4849 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4850 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4852 address
[count
++] = coords
[1];
4854 if (instr
->coord_components
> 2) {
4855 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4856 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4857 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4858 instr
->op
!= nir_texop_txf
) {
4859 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4861 address
[count
++] = coords
[2];
4864 if (ctx
->ac
.chip_class
>= GFX9
) {
4865 LLVMValueRef filler
;
4866 if (instr
->op
== nir_texop_txf
)
4867 filler
= ctx
->ac
.i32_0
;
4869 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4871 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4872 /* No nir_texop_lod, because it does not take a slice
4873 * even with array textures. */
4874 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
4875 address
[count
] = address
[count
- 1];
4876 address
[count
- 1] = filler
;
4879 address
[count
++] = filler
;
4885 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4886 instr
->op
== nir_texop_txf
)) {
4887 address
[count
++] = lod
;
4888 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4889 address
[count
++] = sample_index
;
4890 } else if(instr
->op
== nir_texop_txs
) {
4893 address
[count
++] = lod
;
4895 address
[count
++] = ctx
->ac
.i32_0
;
4898 for (chan
= 0; chan
< count
; chan
++) {
4899 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4900 address
[chan
], ctx
->ac
.i32
, "");
4903 if (instr
->op
== nir_texop_samples_identical
) {
4904 LLVMValueRef txf_address
[4];
4905 struct ac_image_args txf_args
= { 0 };
4906 unsigned txf_count
= count
;
4907 memcpy(txf_address
, address
, sizeof(txf_address
));
4909 if (!instr
->is_array
)
4910 txf_address
[2] = ctx
->ac
.i32_0
;
4911 txf_address
[3] = ctx
->ac
.i32_0
;
4913 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4915 txf_address
, txf_count
, 0xf);
4917 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4919 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4920 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4924 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4925 instr
->op
!= nir_texop_txs
) {
4926 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4927 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4930 instr
->is_array
? address
[2] : NULL
,
4931 address
[sample_chan
],
4935 if (offsets
&& instr
->op
== nir_texop_txf
) {
4936 nir_const_value
*const_offset
=
4937 nir_src_as_const_value(instr
->src
[const_src
].src
);
4938 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4939 assert(const_offset
);
4940 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4941 if (num_offsets
> 2)
4942 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4943 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4944 if (num_offsets
> 1)
4945 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4946 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4947 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4948 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4952 /* TODO TG4 support */
4953 if (instr
->op
== nir_texop_tg4
) {
4954 if (instr
->is_shadow
)
4957 dmask
= 1 << instr
->component
;
4959 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4960 res_ptr
, samp_ptr
, address
, count
, dmask
);
4962 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4964 if (instr
->op
== nir_texop_query_levels
)
4965 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4966 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4967 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4968 instr
->op
!= nir_texop_tg4
)
4969 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4970 else if (instr
->op
== nir_texop_txs
&&
4971 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4973 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4974 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4975 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4976 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4977 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4978 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4979 instr
->op
== nir_texop_txs
&&
4980 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4982 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4983 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4984 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4986 } else if (instr
->dest
.ssa
.num_components
!= 4)
4987 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4991 assert(instr
->dest
.is_ssa
);
4992 result
= ac_to_integer(&ctx
->ac
, result
);
4993 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4998 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
5000 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
5001 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
5003 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
5004 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
5007 static void visit_post_phi(struct ac_nir_context
*ctx
,
5008 nir_phi_instr
*instr
,
5009 LLVMValueRef llvm_phi
)
5011 nir_foreach_phi_src(src
, instr
) {
5012 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
5013 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
5015 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
5019 static void phi_post_pass(struct ac_nir_context
*ctx
)
5021 struct hash_entry
*entry
;
5022 hash_table_foreach(ctx
->phis
, entry
) {
5023 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
5024 (LLVMValueRef
)entry
->data
);
5029 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
5030 const nir_ssa_undef_instr
*instr
)
5032 unsigned num_components
= instr
->def
.num_components
;
5035 if (num_components
== 1)
5036 undef
= LLVMGetUndef(ctx
->ac
.i32
);
5038 undef
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, num_components
));
5040 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
5043 static void visit_jump(struct ac_nir_context
*ctx
,
5044 const nir_jump_instr
*instr
)
5046 switch (instr
->type
) {
5047 case nir_jump_break
:
5048 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
5049 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5051 case nir_jump_continue
:
5052 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5053 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5056 fprintf(stderr
, "Unknown NIR jump instr: ");
5057 nir_print_instr(&instr
->instr
, stderr
);
5058 fprintf(stderr
, "\n");
5063 static void visit_cf_list(struct ac_nir_context
*ctx
,
5064 struct exec_list
*list
);
5066 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5068 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
5069 nir_foreach_instr(instr
, block
)
5071 switch (instr
->type
) {
5072 case nir_instr_type_alu
:
5073 visit_alu(ctx
, nir_instr_as_alu(instr
));
5075 case nir_instr_type_load_const
:
5076 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5078 case nir_instr_type_intrinsic
:
5079 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5081 case nir_instr_type_tex
:
5082 visit_tex(ctx
, nir_instr_as_tex(instr
));
5084 case nir_instr_type_phi
:
5085 visit_phi(ctx
, nir_instr_as_phi(instr
));
5087 case nir_instr_type_ssa_undef
:
5088 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5090 case nir_instr_type_jump
:
5091 visit_jump(ctx
, nir_instr_as_jump(instr
));
5094 fprintf(stderr
, "Unknown NIR instr type: ");
5095 nir_print_instr(instr
, stderr
);
5096 fprintf(stderr
, "\n");
5101 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
5104 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5106 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5108 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5109 LLVMBasicBlockRef merge_block
=
5110 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5111 LLVMBasicBlockRef if_block
=
5112 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5113 LLVMBasicBlockRef else_block
= merge_block
;
5114 if (!exec_list_is_empty(&if_stmt
->else_list
))
5115 else_block
= LLVMAppendBasicBlockInContext(
5116 ctx
->ac
.context
, fn
, "");
5118 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
5120 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
5122 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
5123 visit_cf_list(ctx
, &if_stmt
->then_list
);
5124 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5125 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5127 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5128 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
5129 visit_cf_list(ctx
, &if_stmt
->else_list
);
5130 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5131 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5134 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
5137 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5139 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5140 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
5141 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
5143 ctx
->continue_block
=
5144 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5146 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5148 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5149 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
5150 visit_cf_list(ctx
, &loop
->body
);
5152 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5153 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5154 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
5156 ctx
->continue_block
= continue_parent
;
5157 ctx
->break_block
= break_parent
;
5160 static void visit_cf_list(struct ac_nir_context
*ctx
,
5161 struct exec_list
*list
)
5163 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5165 switch (node
->type
) {
5166 case nir_cf_node_block
:
5167 visit_block(ctx
, nir_cf_node_as_block(node
));
5170 case nir_cf_node_if
:
5171 visit_if(ctx
, nir_cf_node_as_if(node
));
5174 case nir_cf_node_loop
:
5175 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5185 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
5186 struct nir_variable
*variable
)
5188 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5189 LLVMValueRef t_offset
;
5190 LLVMValueRef t_list
;
5192 LLVMValueRef buffer_index
;
5193 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5194 int idx
= variable
->data
.location
;
5195 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5197 variable
->data
.driver_location
= idx
* 4;
5199 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
5200 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5201 ctx
->abi
.start_instance
, "");
5202 if (ctx
->options
->key
.vs
.as_ls
) {
5203 ctx
->shader_info
->vs
.vgpr_comp_cnt
=
5204 MAX2(2, ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5206 ctx
->shader_info
->vs
.vgpr_comp_cnt
=
5207 MAX2(1, ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5210 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5211 ctx
->abi
.base_vertex
, "");
5213 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5214 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5216 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5218 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5223 for (unsigned chan
= 0; chan
< 4; chan
++) {
5224 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5225 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5226 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5227 input
, llvm_chan
, ""));
5232 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5234 LLVMValueRef interp_param
,
5235 LLVMValueRef prim_mask
,
5236 LLVMValueRef result
[4])
5238 LLVMValueRef attr_number
;
5241 bool interp
= interp_param
!= NULL
;
5243 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5245 /* fs.constant returns the param from the middle vertex, so it's not
5246 * really useful for flat shading. It's meant to be used for custom
5247 * interpolation (but the intrinsic can't fetch from the other two
5250 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5251 * to do the right thing. The only reason we use fs.constant is that
5252 * fs.interp cannot be used on integers, because they can be equal
5256 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5259 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5261 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5265 for (chan
= 0; chan
< 4; chan
++) {
5266 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5269 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5274 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5275 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5284 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5285 struct nir_variable
*variable
)
5287 int idx
= variable
->data
.location
;
5288 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5289 LLVMValueRef interp
;
5291 variable
->data
.driver_location
= idx
* 4;
5292 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5294 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5295 unsigned interp_type
;
5296 if (variable
->data
.sample
) {
5297 interp_type
= INTERP_SAMPLE
;
5298 ctx
->shader_info
->info
.ps
.force_persample
= true;
5299 } else if (variable
->data
.centroid
)
5300 interp_type
= INTERP_CENTROID
;
5302 interp_type
= INTERP_CENTER
;
5304 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5308 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5309 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5314 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5315 struct nir_shader
*nir
) {
5316 nir_foreach_variable(variable
, &nir
->inputs
)
5317 handle_vs_input_decl(ctx
, variable
);
5321 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5322 struct nir_shader
*nir
)
5324 if (!ctx
->options
->key
.fs
.multisample
)
5327 bool uses_center
= false;
5328 bool uses_centroid
= false;
5329 nir_foreach_variable(variable
, &nir
->inputs
) {
5330 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5331 variable
->data
.sample
)
5334 if (variable
->data
.centroid
)
5335 uses_centroid
= true;
5340 if (uses_center
&& uses_centroid
) {
5341 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5342 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5343 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5348 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5349 struct nir_shader
*nir
)
5351 prepare_interp_optimize(ctx
, nir
);
5353 nir_foreach_variable(variable
, &nir
->inputs
)
5354 handle_fs_input_decl(ctx
, variable
);
5358 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5359 ctx
->shader_info
->info
.needs_multiview_view_index
)
5360 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5362 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5363 LLVMValueRef interp_param
;
5364 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5366 if (!(ctx
->input_mask
& (1ull << i
)))
5369 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5370 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5371 interp_param
= *inputs
;
5372 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5376 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5378 } else if (i
== VARYING_SLOT_POS
) {
5379 for(int i
= 0; i
< 3; ++i
)
5380 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5382 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5383 ctx
->abi
.frag_pos
[3]);
5386 ctx
->shader_info
->fs
.num_interp
= index
;
5387 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5388 ctx
->shader_info
->fs
.has_pcoord
= true;
5389 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5390 ctx
->shader_info
->fs
.prim_id_input
= true;
5391 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5392 ctx
->shader_info
->fs
.layer_input
= true;
5393 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5395 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5396 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5400 ac_build_alloca(struct ac_llvm_context
*ac
,
5404 LLVMBuilderRef builder
= ac
->builder
;
5405 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5406 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5407 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5408 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5409 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5413 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5415 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5418 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5419 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5421 LLVMDisposeBuilder(first_builder
);
5426 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5430 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5431 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5436 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5437 struct nir_variable
*variable
,
5438 struct nir_shader
*shader
,
5439 gl_shader_stage stage
)
5441 int idx
= variable
->data
.location
+ variable
->data
.index
;
5442 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5443 uint64_t mask_attribs
;
5445 variable
->data
.driver_location
= idx
* 4;
5447 /* tess ctrl has it's own load/store paths for outputs */
5448 if (stage
== MESA_SHADER_TESS_CTRL
)
5451 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5452 if (stage
== MESA_SHADER_VERTEX
||
5453 stage
== MESA_SHADER_TESS_EVAL
||
5454 stage
== MESA_SHADER_GEOMETRY
) {
5455 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5456 int length
= shader
->info
.clip_distance_array_size
+
5457 shader
->info
.cull_distance_array_size
;
5458 if (stage
== MESA_SHADER_VERTEX
) {
5459 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5460 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5462 if (stage
== MESA_SHADER_TESS_EVAL
) {
5463 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5464 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5471 mask_attribs
= 1ull << idx
;
5475 ctx
->output_mask
|= mask_attribs
;
5479 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5480 struct nir_shader
*nir
,
5481 struct nir_variable
*variable
)
5483 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5484 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5486 /* tess ctrl has it's own load/store paths for outputs */
5487 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5490 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5491 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5492 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5493 int idx
= variable
->data
.location
+ variable
->data
.index
;
5494 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5495 int length
= nir
->info
.clip_distance_array_size
+
5496 nir
->info
.cull_distance_array_size
;
5505 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5506 for (unsigned chan
= 0; chan
< 4; chan
++) {
5507 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5508 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5514 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5515 enum glsl_base_type type
)
5519 case GLSL_TYPE_UINT
:
5520 case GLSL_TYPE_BOOL
:
5521 case GLSL_TYPE_SUBROUTINE
:
5523 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5525 case GLSL_TYPE_INT64
:
5526 case GLSL_TYPE_UINT64
:
5528 case GLSL_TYPE_DOUBLE
:
5531 unreachable("unknown GLSL type");
5536 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5537 const struct glsl_type
*type
)
5539 if (glsl_type_is_scalar(type
)) {
5540 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5543 if (glsl_type_is_vector(type
)) {
5544 return LLVMVectorType(
5545 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5546 glsl_get_vector_elements(type
));
5549 if (glsl_type_is_matrix(type
)) {
5550 return LLVMArrayType(
5551 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5552 glsl_get_matrix_columns(type
));
5555 if (glsl_type_is_array(type
)) {
5556 return LLVMArrayType(
5557 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5558 glsl_get_length(type
));
5561 assert(glsl_type_is_struct(type
));
5563 LLVMTypeRef member_types
[glsl_get_length(type
)];
5565 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5567 glsl_to_llvm_type(ctx
,
5568 glsl_get_struct_field(type
, i
));
5571 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5572 glsl_get_length(type
), false);
5576 setup_locals(struct ac_nir_context
*ctx
,
5577 struct nir_function
*func
)
5580 ctx
->num_locals
= 0;
5581 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5582 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5583 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5584 variable
->data
.location_frac
= 0;
5585 ctx
->num_locals
+= attrib_count
;
5587 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5591 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5592 for (j
= 0; j
< 4; j
++) {
5593 ctx
->locals
[i
* 4 + j
] =
5594 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5600 setup_shared(struct ac_nir_context
*ctx
,
5601 struct nir_shader
*nir
)
5603 nir_foreach_variable(variable
, &nir
->shared
) {
5604 LLVMValueRef shared
=
5605 LLVMAddGlobalInAddressSpace(
5606 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5607 variable
->name
? variable
->name
: "",
5609 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5614 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5616 v
= ac_to_float(ctx
, v
);
5617 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5618 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5622 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5623 LLVMValueRef src0
, LLVMValueRef src1
)
5625 LLVMValueRef const16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
5626 LLVMValueRef comp
[2];
5628 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5629 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5630 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5631 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5634 /* Initialize arguments for the shader export intrinsic */
5636 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5637 LLVMValueRef
*values
,
5639 struct ac_export_args
*args
)
5641 /* Default is 0xf. Adjusted below depending on the format. */
5642 args
->enabled_channels
= 0xf;
5644 /* Specify whether the EXEC mask represents the valid mask */
5645 args
->valid_mask
= 0;
5647 /* Specify whether this is the last export */
5650 /* Specify the target we are exporting */
5651 args
->target
= target
;
5653 args
->compr
= false;
5654 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
5655 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
5656 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
5657 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5662 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5663 LLVMValueRef val
[4];
5664 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5665 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5666 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5667 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5669 switch(col_format
) {
5670 case V_028714_SPI_SHADER_ZERO
:
5671 args
->enabled_channels
= 0; /* writemask */
5672 args
->target
= V_008DFC_SQ_EXP_NULL
;
5675 case V_028714_SPI_SHADER_32_R
:
5676 args
->enabled_channels
= 1;
5677 args
->out
[0] = values
[0];
5680 case V_028714_SPI_SHADER_32_GR
:
5681 args
->enabled_channels
= 0x3;
5682 args
->out
[0] = values
[0];
5683 args
->out
[1] = values
[1];
5686 case V_028714_SPI_SHADER_32_AR
:
5687 args
->enabled_channels
= 0x9;
5688 args
->out
[0] = values
[0];
5689 args
->out
[3] = values
[3];
5692 case V_028714_SPI_SHADER_FP16_ABGR
:
5695 for (unsigned chan
= 0; chan
< 2; chan
++) {
5696 LLVMValueRef pack_args
[2] = {
5698 values
[2 * chan
+ 1]
5700 LLVMValueRef packed
;
5702 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5703 args
->out
[chan
] = packed
;
5707 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5708 for (unsigned chan
= 0; chan
< 4; chan
++) {
5709 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5710 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5711 LLVMConstReal(ctx
->ac
.f32
, 65535), "");
5712 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5713 LLVMConstReal(ctx
->ac
.f32
, 0.5), "");
5714 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5719 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5720 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5723 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5724 for (unsigned chan
= 0; chan
< 4; chan
++) {
5725 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5726 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5727 LLVMConstReal(ctx
->ac
.f32
, 32767), "");
5729 /* If positive, add 0.5, else add -0.5. */
5730 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5731 LLVMBuildSelect(ctx
->builder
,
5732 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5733 val
[chan
], ctx
->ac
.f32_0
, ""),
5734 LLVMConstReal(ctx
->ac
.f32
, 0.5),
5735 LLVMConstReal(ctx
->ac
.f32
, -0.5), ""), "");
5736 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->ac
.i32
, "");
5740 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5741 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5744 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5745 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5746 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5747 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->ac
.i32
, 3, 0);
5749 for (unsigned chan
= 0; chan
< 4; chan
++) {
5750 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5751 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5755 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5756 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5760 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5761 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5762 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5763 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5764 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5765 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->ac
.i32_1
;
5766 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->ac
.i32
, -2, 0);
5769 for (unsigned chan
= 0; chan
< 4; chan
++) {
5770 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5771 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5772 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5776 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5777 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5782 case V_028714_SPI_SHADER_32_ABGR
:
5783 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5787 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5789 for (unsigned i
= 0; i
< 4; ++i
)
5790 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5794 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5795 bool export_prim_id
,
5796 struct ac_vs_output_info
*outinfo
)
5798 uint32_t param_count
= 0;
5800 unsigned pos_idx
, num_pos_exports
= 0;
5801 struct ac_export_args args
, pos_args
[4] = {};
5802 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5805 if (ctx
->options
->key
.has_multiview_view_index
) {
5806 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5808 for(unsigned i
= 0; i
< 4; ++i
)
5809 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5810 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5813 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5814 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5817 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5818 sizeof(outinfo
->vs_output_param_offset
));
5820 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5821 LLVMValueRef slots
[8];
5824 if (outinfo
->cull_dist_mask
)
5825 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5827 i
= VARYING_SLOT_CLIP_DIST0
;
5828 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5829 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5830 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5832 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5833 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
5835 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5836 target
= V_008DFC_SQ_EXP_POS
+ 3;
5837 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5838 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5839 &args
, sizeof(args
));
5842 target
= V_008DFC_SQ_EXP_POS
+ 2;
5843 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5844 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5845 &args
, sizeof(args
));
5849 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
5850 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5851 for (unsigned j
= 0; j
< 4; j
++)
5852 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5853 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5855 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5857 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5858 outinfo
->writes_pointsize
= true;
5859 psize_value
= LLVMBuildLoad(ctx
->builder
,
5860 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5863 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5864 outinfo
->writes_layer
= true;
5865 layer_value
= LLVMBuildLoad(ctx
->builder
,
5866 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5869 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5870 outinfo
->writes_viewport_index
= true;
5871 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5872 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5875 if (outinfo
->writes_pointsize
||
5876 outinfo
->writes_layer
||
5877 outinfo
->writes_viewport_index
) {
5878 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5879 (outinfo
->writes_layer
== true ? 4 : 0));
5880 pos_args
[1].valid_mask
= 0;
5881 pos_args
[1].done
= 0;
5882 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5883 pos_args
[1].compr
= 0;
5884 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
5885 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
5886 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
5887 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
5889 if (outinfo
->writes_pointsize
== true)
5890 pos_args
[1].out
[0] = psize_value
;
5891 if (outinfo
->writes_layer
== true)
5892 pos_args
[1].out
[2] = layer_value
;
5893 if (outinfo
->writes_viewport_index
== true) {
5894 if (ctx
->options
->chip_class
>= GFX9
) {
5895 /* GFX9 has the layer in out.z[10:0] and the viewport
5896 * index in out.z[19:16].
5898 LLVMValueRef v
= viewport_index_value
;
5899 v
= ac_to_integer(&ctx
->ac
, v
);
5900 v
= LLVMBuildShl(ctx
->builder
, v
,
5901 LLVMConstInt(ctx
->ac
.i32
, 16, false),
5903 v
= LLVMBuildOr(ctx
->builder
, v
,
5904 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5906 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5907 pos_args
[1].enabled_channels
|= 1 << 2;
5909 pos_args
[1].out
[3] = viewport_index_value
;
5910 pos_args
[1].enabled_channels
|= 1 << 3;
5914 for (i
= 0; i
< 4; i
++) {
5915 if (pos_args
[i
].out
[0])
5920 for (i
= 0; i
< 4; i
++) {
5921 if (!pos_args
[i
].out
[0])
5924 /* Specify the target we are exporting */
5925 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5926 if (pos_idx
== num_pos_exports
)
5927 pos_args
[i
].done
= 1;
5928 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5931 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5932 LLVMValueRef values
[4];
5933 if (!(ctx
->output_mask
& (1ull << i
)))
5936 for (unsigned j
= 0; j
< 4; j
++)
5937 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5938 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5940 if (i
== VARYING_SLOT_LAYER
) {
5941 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5942 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5944 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5945 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5946 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5948 } else if (i
>= VARYING_SLOT_VAR0
) {
5949 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5950 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5951 outinfo
->vs_output_param_offset
[i
] = param_count
;
5956 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5958 if (target
>= V_008DFC_SQ_EXP_POS
&&
5959 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5960 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5961 &args
, sizeof(args
));
5963 ac_build_export(&ctx
->ac
, &args
);
5967 if (export_prim_id
) {
5968 LLVMValueRef values
[4];
5969 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5970 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5973 values
[0] = ctx
->vs_prim_id
;
5974 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5975 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5976 for (unsigned j
= 1; j
< 4; j
++)
5977 values
[j
] = ctx
->ac
.f32_0
;
5978 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5979 ac_build_export(&ctx
->ac
, &args
);
5980 outinfo
->export_prim_id
= true;
5983 outinfo
->pos_exports
= num_pos_exports
;
5984 outinfo
->param_exports
= param_count
;
5988 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5989 struct ac_es_output_info
*outinfo
)
5992 uint64_t max_output_written
= 0;
5993 LLVMValueRef lds_base
= NULL
;
5995 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5999 if (!(ctx
->output_mask
& (1ull << i
)))
6002 if (i
== VARYING_SLOT_CLIP_DIST0
)
6003 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6005 param_index
= shader_io_get_unique_index(i
);
6007 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
6010 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
6012 if (ctx
->ac
.chip_class
>= GFX9
) {
6013 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
6014 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
6015 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6016 LLVMConstInt(ctx
->ac
.i32
, 24, false),
6017 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
6018 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
6019 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
6020 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
6021 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
6022 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
6025 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6026 LLVMValueRef dw_addr
;
6027 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
6031 if (!(ctx
->output_mask
& (1ull << i
)))
6034 if (i
== VARYING_SLOT_CLIP_DIST0
)
6035 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6037 param_index
= shader_io_get_unique_index(i
);
6040 dw_addr
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6041 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
6044 for (j
= 0; j
< length
; j
++) {
6045 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
6046 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
6048 if (ctx
->ac
.chip_class
>= GFX9
) {
6049 ac_lds_store(&ctx
->ac
, dw_addr
,
6050 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6051 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6053 ac_build_buffer_store_dword(&ctx
->ac
,
6056 NULL
, ctx
->es2gs_offset
,
6057 (4 * param_index
+ j
) * 4,
6065 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
6067 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
6068 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
6069 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
6070 vertex_dw_stride
, "");
6072 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6073 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
6076 if (!(ctx
->output_mask
& (1ull << i
)))
6079 if (i
== VARYING_SLOT_CLIP_DIST0
)
6080 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6081 int param
= shader_io_get_unique_index(i
);
6082 mark_tess_output(ctx
, false, param
);
6084 mark_tess_output(ctx
, false, param
+ 1);
6085 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
6086 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
6088 for (unsigned j
= 0; j
< length
; j
++) {
6089 ac_lds_store(&ctx
->ac
, dw_addr
,
6090 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6091 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6096 struct ac_build_if_state
6098 struct nir_to_llvm_context
*ctx
;
6099 LLVMValueRef condition
;
6100 LLVMBasicBlockRef entry_block
;
6101 LLVMBasicBlockRef true_block
;
6102 LLVMBasicBlockRef false_block
;
6103 LLVMBasicBlockRef merge_block
;
6106 static LLVMBasicBlockRef
6107 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
6109 LLVMBasicBlockRef current_block
;
6110 LLVMBasicBlockRef next_block
;
6111 LLVMBasicBlockRef new_block
;
6113 /* get current basic block */
6114 current_block
= LLVMGetInsertBlock(ctx
->builder
);
6116 /* chqeck if there's another block after this one */
6117 next_block
= LLVMGetNextBasicBlock(current_block
);
6119 /* insert the new block before the next block */
6120 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
6123 /* append new block after current block */
6124 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
6125 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
6131 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
6132 struct nir_to_llvm_context
*ctx
,
6133 LLVMValueRef condition
)
6135 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
6137 memset(ifthen
, 0, sizeof *ifthen
);
6139 ifthen
->condition
= condition
;
6140 ifthen
->entry_block
= block
;
6142 /* create endif/merge basic block for the phi functions */
6143 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
6145 /* create/insert true_block before merge_block */
6146 ifthen
->true_block
=
6147 LLVMInsertBasicBlockInContext(ctx
->context
,
6148 ifthen
->merge_block
,
6151 /* successive code goes into the true block */
6152 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
6156 * End a conditional.
6159 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
6161 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
6163 /* Insert branch to the merge block from current block */
6164 LLVMBuildBr(builder
, ifthen
->merge_block
);
6167 * Now patch in the various branch instructions.
6170 /* Insert the conditional branch instruction at the end of entry_block */
6171 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
6172 if (ifthen
->false_block
) {
6173 /* we have an else clause */
6174 LLVMBuildCondBr(builder
, ifthen
->condition
,
6175 ifthen
->true_block
, ifthen
->false_block
);
6178 /* no else clause */
6179 LLVMBuildCondBr(builder
, ifthen
->condition
,
6180 ifthen
->true_block
, ifthen
->merge_block
);
6183 /* Resume building code at end of the ifthen->merge_block */
6184 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
6188 write_tess_factors(struct nir_to_llvm_context
*ctx
)
6190 unsigned stride
, outer_comps
, inner_comps
;
6191 struct ac_build_if_state if_ctx
, inner_if_ctx
;
6192 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 8, 5);
6193 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
6194 unsigned tess_inner_index
, tess_outer_index
;
6195 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6196 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6198 emit_barrier(&ctx
->ac
, ctx
->stage
);
6200 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6220 ac_nir_build_if(&if_ctx
, ctx
,
6221 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6222 invocation_id
, ctx
->ac
.i32_0
, ""));
6224 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6225 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6227 mark_tess_output(ctx
, true, tess_inner_index
);
6228 mark_tess_output(ctx
, true, tess_outer_index
);
6229 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6230 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6231 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6232 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6233 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6235 for (i
= 0; i
< 4; i
++) {
6236 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6237 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6241 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6242 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6243 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6245 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6247 for (i
= 0; i
< outer_comps
; i
++) {
6249 ac_lds_load(&ctx
->ac
, lds_outer
);
6250 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6253 for (i
= 0; i
< inner_comps
; i
++) {
6254 inner
[i
] = out
[outer_comps
+i
] =
6255 ac_lds_load(&ctx
->ac
, lds_inner
);
6256 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6261 /* Convert the outputs to vectors for stores. */
6262 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6266 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6269 buffer
= ctx
->hs_ring_tess_factor
;
6270 tf_base
= ctx
->tess_factor_offset
;
6271 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6272 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6273 unsigned tf_offset
= 0;
6275 if (ctx
->options
->chip_class
<= VI
) {
6276 ac_nir_build_if(&inner_if_ctx
, ctx
,
6277 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6278 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6280 /* Store the dynamic HS control word. */
6281 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6282 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6283 1, ctx
->ac
.i32_0
, tf_base
,
6284 0, 1, 0, true, false);
6287 ac_nir_build_endif(&inner_if_ctx
);
6290 /* Store the tessellation factors. */
6291 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6292 MIN2(stride
, 4), byteoffset
, tf_base
,
6293 tf_offset
, 1, 0, true, false);
6295 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6296 stride
- 4, byteoffset
, tf_base
,
6297 16 + tf_offset
, 1, 0, true, false);
6299 //store to offchip for TES to read - only if TES reads them
6300 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6301 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6302 LLVMValueRef tf_inner_offset
;
6303 unsigned param_outer
, param_inner
;
6305 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6306 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6307 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6309 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6310 util_next_power_of_two(outer_comps
));
6312 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6313 outer_comps
, tf_outer_offset
,
6314 ctx
->oc_lds
, 0, 1, 0, true, false);
6316 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6317 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6318 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6320 inner_vec
= inner_comps
== 1 ? inner
[0] :
6321 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6322 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6323 inner_comps
, tf_inner_offset
,
6324 ctx
->oc_lds
, 0, 1, 0, true, false);
6327 ac_nir_build_endif(&if_ctx
);
6331 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6333 write_tess_factors(ctx
);
6337 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6338 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6339 struct ac_export_args
*args
)
6342 si_llvm_init_export_args(ctx
, color
, param
,
6346 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6347 args
->done
= 1; /* DONE bit */
6348 } else if (!args
->enabled_channels
)
6349 return false; /* unnecessary NULL export */
6355 radv_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6356 LLVMValueRef depth
, LLVMValueRef stencil
,
6357 LLVMValueRef samplemask
)
6359 struct ac_export_args args
;
6361 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
6363 ac_build_export(&ctx
->ac
, &args
);
6367 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6370 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6371 struct ac_export_args color_args
[8];
6373 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6374 LLVMValueRef values
[4];
6376 if (!(ctx
->output_mask
& (1ull << i
)))
6379 if (i
== FRAG_RESULT_DEPTH
) {
6380 ctx
->shader_info
->fs
.writes_z
= true;
6381 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6382 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6383 } else if (i
== FRAG_RESULT_STENCIL
) {
6384 ctx
->shader_info
->fs
.writes_stencil
= true;
6385 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6386 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6387 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6388 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6389 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6390 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6393 for (unsigned j
= 0; j
< 4; j
++)
6394 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6395 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6397 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6398 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6400 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6406 for (unsigned i
= 0; i
< index
; i
++)
6407 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6408 if (depth
|| stencil
|| samplemask
)
6409 radv_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6411 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6412 ac_build_export(&ctx
->ac
, &color_args
[0]);
6417 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6419 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6423 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6424 LLVMValueRef
*addrs
)
6426 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6428 switch (ctx
->stage
) {
6429 case MESA_SHADER_VERTEX
:
6430 if (ctx
->options
->key
.vs
.as_ls
)
6431 handle_ls_outputs_post(ctx
);
6432 else if (ctx
->options
->key
.vs
.as_es
)
6433 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6435 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6436 &ctx
->shader_info
->vs
.outinfo
);
6438 case MESA_SHADER_FRAGMENT
:
6439 handle_fs_outputs_post(ctx
);
6441 case MESA_SHADER_GEOMETRY
:
6442 emit_gs_epilogue(ctx
);
6444 case MESA_SHADER_TESS_CTRL
:
6445 handle_tcs_outputs_post(ctx
);
6447 case MESA_SHADER_TESS_EVAL
:
6448 if (ctx
->options
->key
.tes
.as_es
)
6449 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6451 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6452 &ctx
->shader_info
->tes
.outinfo
);
6459 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6461 LLVMPassManagerRef passmgr
;
6462 /* Create the pass manager */
6463 passmgr
= LLVMCreateFunctionPassManagerForModule(
6466 /* This pass should eliminate all the load and store instructions */
6467 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6469 /* Add some optimization passes */
6470 LLVMAddScalarReplAggregatesPass(passmgr
);
6471 LLVMAddLICMPass(passmgr
);
6472 LLVMAddAggressiveDCEPass(passmgr
);
6473 LLVMAddCFGSimplificationPass(passmgr
);
6474 LLVMAddInstructionCombiningPass(passmgr
);
6477 LLVMInitializeFunctionPassManager(passmgr
);
6478 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6479 LLVMFinalizeFunctionPassManager(passmgr
);
6481 LLVMDisposeBuilder(ctx
->builder
);
6482 LLVMDisposePassManager(passmgr
);
6486 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6488 struct ac_vs_output_info
*outinfo
;
6490 switch (ctx
->stage
) {
6491 case MESA_SHADER_FRAGMENT
:
6492 case MESA_SHADER_COMPUTE
:
6493 case MESA_SHADER_TESS_CTRL
:
6494 case MESA_SHADER_GEOMETRY
:
6496 case MESA_SHADER_VERTEX
:
6497 if (ctx
->options
->key
.vs
.as_ls
||
6498 ctx
->options
->key
.vs
.as_es
)
6500 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6502 case MESA_SHADER_TESS_EVAL
:
6503 if (ctx
->options
->key
.vs
.as_es
)
6505 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6508 unreachable("Unhandled shader type");
6511 ac_optimize_vs_outputs(&ctx
->ac
,
6513 outinfo
->vs_output_param_offset
,
6515 &outinfo
->param_exports
);
6519 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6521 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6522 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6523 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6526 if (ctx
->is_gs_copy_shader
) {
6527 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6529 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6531 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6532 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6534 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.v4i32
, "");
6536 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6537 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6538 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6539 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6542 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6543 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6544 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6545 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6550 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6551 const struct nir_shader
*nir
)
6553 switch (nir
->info
.stage
) {
6554 case MESA_SHADER_TESS_CTRL
:
6555 return chip_class
>= CIK
? 128 : 64;
6556 case MESA_SHADER_GEOMETRY
:
6557 return chip_class
>= GFX9
? 128 : 64;
6558 case MESA_SHADER_COMPUTE
:
6564 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6565 nir
->info
.cs
.local_size
[1] *
6566 nir
->info
.cs
.local_size
[2];
6567 return max_workgroup_size
;
6570 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6571 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6573 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6574 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6575 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6576 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6578 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6579 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6580 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_rel_ids
, ctx
->rel_auto_id
, "");
6581 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6584 static void prepare_gs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6586 for(int i
= 5; i
>= 0; --i
) {
6587 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6588 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6589 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6592 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6593 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6594 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6597 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6598 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6600 struct ac_nir_context ctx
= {};
6601 struct nir_function
*func
;
6610 ctx
.stage
= nir
->info
.stage
;
6612 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6614 nir_foreach_variable(variable
, &nir
->outputs
)
6615 handle_shader_output_decl(&ctx
, nir
, variable
);
6617 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6618 _mesa_key_pointer_equal
);
6619 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6620 _mesa_key_pointer_equal
);
6621 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6622 _mesa_key_pointer_equal
);
6624 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6626 setup_locals(&ctx
, func
);
6628 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6629 setup_shared(&ctx
, nir
);
6631 visit_cf_list(&ctx
, &func
->impl
->body
);
6632 phi_post_pass(&ctx
);
6634 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6638 ralloc_free(ctx
.defs
);
6639 ralloc_free(ctx
.phis
);
6640 ralloc_free(ctx
.vars
);
6647 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6648 struct nir_shader
*const *shaders
,
6650 struct ac_shader_variant_info
*shader_info
,
6651 const struct ac_nir_compiler_options
*options
)
6653 struct nir_to_llvm_context ctx
= {0};
6655 ctx
.options
= options
;
6656 ctx
.shader_info
= shader_info
;
6657 ctx
.context
= LLVMContextCreate();
6658 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6660 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
6662 ctx
.ac
.module
= ctx
.module
;
6663 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6665 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6666 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6667 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6668 LLVMDisposeTargetData(data_layout
);
6669 LLVMDisposeMessage(data_layout_str
);
6671 enum ac_float_mode float_mode
=
6672 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
6673 AC_FLOAT_MODE_DEFAULT
;
6675 ctx
.builder
= ac_create_builder(ctx
.context
, float_mode
);
6676 ctx
.ac
.builder
= ctx
.builder
;
6678 memset(shader_info
, 0, sizeof(*shader_info
));
6680 for(int i
= 0; i
< shader_count
; ++i
)
6681 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6683 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6684 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6685 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6686 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6688 ctx
.max_workgroup_size
= 0;
6689 for (int i
= 0; i
< shader_count
; ++i
) {
6690 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6691 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6695 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6696 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6698 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6699 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6700 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
6701 ctx
.abi
.load_ubo
= radv_load_ubo
;
6702 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6703 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6704 ctx
.abi
.clamp_shadow_reference
= false;
6706 if (shader_count
>= 2)
6707 ac_init_exec_full_mask(&ctx
.ac
);
6709 if (ctx
.ac
.chip_class
== GFX9
&&
6710 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6711 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6713 for(int i
= 0; i
< shader_count
; ++i
) {
6714 ctx
.stage
= shaders
[i
]->info
.stage
;
6715 ctx
.output_mask
= 0;
6716 ctx
.tess_outputs_written
= 0;
6717 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6718 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6720 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6721 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6722 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6723 ctx
.abi
.load_inputs
= load_gs_input
;
6724 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6725 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
6726 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
6727 ctx
.abi
.load_tess_varyings
= load_tcs_varyings
;
6728 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
6729 ctx
.abi
.store_tcs_outputs
= store_tcs_output
;
6730 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6731 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6732 ctx
.abi
.load_tess_varyings
= load_tes_input
;
6733 ctx
.abi
.load_tess_coord
= load_tess_coord
;
6734 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
6735 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6736 if (shader_info
->info
.vs
.needs_instance_id
) {
6737 if (ctx
.options
->key
.vs
.as_ls
) {
6738 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6739 MAX2(2, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6741 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6742 MAX2(1, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6745 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6746 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6750 emit_barrier(&ctx
.ac
, ctx
.stage
);
6752 ac_setup_rings(&ctx
);
6754 LLVMBasicBlockRef merge_block
;
6755 if (shader_count
>= 2) {
6756 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6757 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6758 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6760 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6761 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6762 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6763 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6764 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6765 thread_id
, count
, "");
6766 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6768 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6771 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6772 handle_fs_inputs(&ctx
, shaders
[i
]);
6773 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6774 handle_vs_inputs(&ctx
, shaders
[i
]);
6775 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6776 prepare_gs_input_vgprs(&ctx
);
6778 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6779 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6781 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6783 if (shader_count
>= 2) {
6784 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6785 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6788 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6789 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6790 shaders
[i
]->info
.cull_distance_array_size
> 4;
6791 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6792 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6793 shaders
[i
]->info
.gs
.vertices_out
;
6794 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6795 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6796 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6797 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6798 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6802 LLVMBuildRetVoid(ctx
.builder
);
6804 ac_llvm_finalize_module(&ctx
);
6806 if (shader_count
== 1)
6807 ac_nir_eliminate_const_vs_outputs(&ctx
);
6812 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6814 unsigned *retval
= (unsigned *)context
;
6815 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6816 char *description
= LLVMGetDiagInfoDescription(di
);
6818 if (severity
== LLVMDSError
) {
6820 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6824 LLVMDisposeMessage(description
);
6827 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6828 struct ac_shader_binary
*binary
,
6829 LLVMTargetMachineRef tm
)
6831 unsigned retval
= 0;
6833 LLVMContextRef llvm_ctx
;
6834 LLVMMemoryBufferRef out_buffer
;
6835 unsigned buffer_size
;
6836 const char *buffer_data
;
6839 /* Setup Diagnostic Handler*/
6840 llvm_ctx
= LLVMGetModuleContext(M
);
6842 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6846 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6849 /* Process Errors/Warnings */
6851 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6857 /* Extract Shader Code*/
6858 buffer_size
= LLVMGetBufferSize(out_buffer
);
6859 buffer_data
= LLVMGetBufferStart(out_buffer
);
6861 ac_elf_read(buffer_data
, buffer_size
, binary
);
6864 LLVMDisposeMemoryBuffer(out_buffer
);
6870 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6871 LLVMModuleRef llvm_module
,
6872 struct ac_shader_binary
*binary
,
6873 struct ac_shader_config
*config
,
6874 struct ac_shader_variant_info
*shader_info
,
6875 gl_shader_stage stage
,
6876 bool dump_shader
, bool supports_spill
)
6879 ac_dump_module(llvm_module
);
6881 memset(binary
, 0, sizeof(*binary
));
6882 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6884 fprintf(stderr
, "compile failed\n");
6888 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6890 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6892 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6893 LLVMDisposeModule(llvm_module
);
6894 LLVMContextDispose(ctx
);
6896 if (stage
== MESA_SHADER_FRAGMENT
) {
6897 shader_info
->num_input_vgprs
= 0;
6898 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6899 shader_info
->num_input_vgprs
+= 2;
6900 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6901 shader_info
->num_input_vgprs
+= 2;
6902 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6903 shader_info
->num_input_vgprs
+= 2;
6904 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6905 shader_info
->num_input_vgprs
+= 3;
6906 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6907 shader_info
->num_input_vgprs
+= 2;
6908 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6909 shader_info
->num_input_vgprs
+= 2;
6910 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6911 shader_info
->num_input_vgprs
+= 2;
6912 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6913 shader_info
->num_input_vgprs
+= 1;
6914 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6915 shader_info
->num_input_vgprs
+= 1;
6916 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6917 shader_info
->num_input_vgprs
+= 1;
6918 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6919 shader_info
->num_input_vgprs
+= 1;
6920 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6921 shader_info
->num_input_vgprs
+= 1;
6922 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6923 shader_info
->num_input_vgprs
+= 1;
6924 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6925 shader_info
->num_input_vgprs
+= 1;
6926 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6927 shader_info
->num_input_vgprs
+= 1;
6928 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6929 shader_info
->num_input_vgprs
+= 1;
6931 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6933 /* +3 for scratch wave offset and VCC */
6934 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6935 shader_info
->num_input_sgprs
+ 3);
6937 /* Enable 64-bit and 16-bit denormals, because there is no performance
6940 * If denormals are enabled, all floating-point output modifiers are
6943 * Don't enable denormals for 32-bit floats, because:
6944 * - Floating-point output modifiers would be ignored by the hw.
6945 * - Some opcodes don't support denormals, such as v_mad_f32. We would
6946 * have to stop using those.
6947 * - SI & CI would be very slow.
6949 config
->float_mode
|= V_00B028_FP_64_DENORMS
;
6953 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
6955 switch (nir
->info
.stage
) {
6956 case MESA_SHADER_COMPUTE
:
6957 for (int i
= 0; i
< 3; ++i
)
6958 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6960 case MESA_SHADER_FRAGMENT
:
6961 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6963 case MESA_SHADER_GEOMETRY
:
6964 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6965 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6966 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6967 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6969 case MESA_SHADER_TESS_EVAL
:
6970 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6971 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6972 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6973 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6974 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6976 case MESA_SHADER_TESS_CTRL
:
6977 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6979 case MESA_SHADER_VERTEX
:
6980 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6981 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6982 /* in LS mode we need at least 1, invocation id needs 2, handled elsewhere */
6983 if (options
->key
.vs
.as_ls
)
6984 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6991 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6992 struct ac_shader_binary
*binary
,
6993 struct ac_shader_config
*config
,
6994 struct ac_shader_variant_info
*shader_info
,
6995 struct nir_shader
*const *nir
,
6997 const struct ac_nir_compiler_options
*options
,
7001 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
7004 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
7005 for (int i
= 0; i
< nir_count
; ++i
)
7006 ac_fill_shader_info(shader_info
, nir
[i
], options
);
7008 /* Determine the ES type (VS or TES) for the GS on GFX9. */
7009 if (options
->chip_class
== GFX9
) {
7010 if (nir_count
== 2 &&
7011 nir
[1]->info
.stage
== MESA_SHADER_GEOMETRY
) {
7012 shader_info
->gs
.es_type
= nir
[0]->info
.stage
;
7018 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
7020 LLVMValueRef args
[9];
7021 args
[0] = ctx
->gsvs_ring
;
7022 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
7023 args
[3] = ctx
->ac
.i32_0
;
7024 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
7025 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
7026 args
[6] = ctx
->ac
.i32_1
; /* GLC */
7027 args
[7] = ctx
->ac
.i32_1
; /* SLC */
7028 args
[8] = ctx
->ac
.i32_0
; /* TFE */
7032 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
7036 if (!(ctx
->output_mask
& (1ull << i
)))
7039 if (i
== VARYING_SLOT_CLIP_DIST0
) {
7040 /* unpack clip and cull from a single set of slots */
7041 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
7046 for (unsigned j
= 0; j
< length
; j
++) {
7048 args
[2] = LLVMConstInt(ctx
->ac
.i32
,
7050 ctx
->gs_max_out_vertices
* 16 * 4, false);
7052 value
= ac_build_intrinsic(&ctx
->ac
,
7053 "llvm.SI.buffer.load.dword.i32.i32",
7054 ctx
->ac
.i32
, args
, 9,
7055 AC_FUNC_ATTR_READONLY
|
7056 AC_FUNC_ATTR_LEGACY
);
7058 LLVMBuildStore(ctx
->builder
,
7059 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
7063 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
7066 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
7067 struct nir_shader
*geom_shader
,
7068 struct ac_shader_binary
*binary
,
7069 struct ac_shader_config
*config
,
7070 struct ac_shader_variant_info
*shader_info
,
7071 const struct ac_nir_compiler_options
*options
,
7074 struct nir_to_llvm_context ctx
= {0};
7075 ctx
.context
= LLVMContextCreate();
7076 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
7077 ctx
.options
= options
;
7078 ctx
.shader_info
= shader_info
;
7080 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
7082 ctx
.ac
.module
= ctx
.module
;
7084 ctx
.is_gs_copy_shader
= true;
7085 LLVMSetTarget(ctx
.module
, "amdgcn--");
7087 enum ac_float_mode float_mode
=
7088 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
7089 AC_FLOAT_MODE_DEFAULT
;
7091 ctx
.builder
= ac_create_builder(ctx
.context
, float_mode
);
7092 ctx
.ac
.builder
= ctx
.builder
;
7093 ctx
.stage
= MESA_SHADER_VERTEX
;
7095 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
7097 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
7098 ac_setup_rings(&ctx
);
7100 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
7101 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
7103 struct ac_nir_context nir_ctx
= {};
7104 nir_ctx
.ac
= ctx
.ac
;
7105 nir_ctx
.abi
= &ctx
.abi
;
7107 nir_ctx
.nctx
= &ctx
;
7110 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
7111 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
7112 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
7115 ac_gs_copy_shader_emit(&ctx
);
7119 LLVMBuildRetVoid(ctx
.builder
);
7121 ac_llvm_finalize_module(&ctx
);
7123 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
7125 dump_shader
, options
->supports_spill
);