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
,
356 return main_function
;
359 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
361 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
365 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
367 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
368 type
= LLVMGetElementType(type
);
370 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
371 return LLVMGetIntTypeWidth(type
);
373 if (type
== ctx
->f16
)
375 if (type
== ctx
->f32
)
377 if (type
== ctx
->f64
)
380 unreachable("Unhandled type kind in get_elem_bits");
383 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
384 LLVMValueRef param
, unsigned rshift
,
387 LLVMValueRef value
= param
;
389 value
= LLVMBuildLShr(ctx
->builder
, value
,
390 LLVMConstInt(ctx
->i32
, rshift
, false), "");
392 if (rshift
+ bitwidth
< 32) {
393 unsigned mask
= (1 << bitwidth
) - 1;
394 value
= LLVMBuildAnd(ctx
->builder
, value
,
395 LLVMConstInt(ctx
->i32
, mask
, false), "");
400 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
402 switch (ctx
->stage
) {
403 case MESA_SHADER_TESS_CTRL
:
404 return unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
405 case MESA_SHADER_TESS_EVAL
:
406 return ctx
->tes_rel_patch_id
;
409 unreachable("Illegal stage");
413 /* Tessellation shaders pass outputs to the next shader using LDS.
415 * LS outputs = TCS inputs
416 * TCS outputs = TES inputs
419 * - TCS inputs for patch 0
420 * - TCS inputs for patch 1
421 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
423 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
424 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
425 * - TCS outputs for patch 1
426 * - Per-patch TCS outputs for patch 1
427 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
428 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
431 * All three shaders VS(LS), TCS, TES share the same LDS space.
434 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
436 if (ctx
->stage
== MESA_SHADER_VERTEX
)
437 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
438 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
439 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
447 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
449 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
453 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
455 return LLVMBuildMul(ctx
->builder
,
456 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
457 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
461 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
463 return LLVMBuildMul(ctx
->builder
,
464 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
465 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
469 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
471 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
472 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
474 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
478 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
480 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
481 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
482 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
484 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
485 LLVMBuildMul(ctx
->builder
, patch_stride
,
491 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
493 LLVMValueRef patch0_patch_data_offset
=
494 get_tcs_out_patch0_patch_data_offset(ctx
);
495 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
496 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
498 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
499 LLVMBuildMul(ctx
->builder
, patch_stride
,
505 set_loc(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
,
506 uint32_t indirect_offset
)
508 ud_info
->sgpr_idx
= *sgpr_idx
;
509 ud_info
->num_sgprs
= num_sgprs
;
510 ud_info
->indirect
= indirect_offset
> 0;
511 ud_info
->indirect_offset
= indirect_offset
;
512 *sgpr_idx
+= num_sgprs
;
516 set_loc_shader(struct nir_to_llvm_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
519 struct ac_userdata_info
*ud_info
=
520 &ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
];
523 set_loc(ud_info
, sgpr_idx
, num_sgprs
, 0);
527 set_loc_desc(struct nir_to_llvm_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
528 uint32_t indirect_offset
)
530 struct ac_userdata_info
*ud_info
=
531 &ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[idx
];
534 set_loc(ud_info
, sgpr_idx
, 2, indirect_offset
);
537 struct user_sgpr_info
{
538 bool need_ring_offsets
;
540 bool indirect_all_descriptor_sets
;
543 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
544 gl_shader_stage stage
,
545 struct user_sgpr_info
*user_sgpr_info
)
547 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
549 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
550 if (stage
== MESA_SHADER_GEOMETRY
||
551 stage
== MESA_SHADER_VERTEX
||
552 stage
== MESA_SHADER_TESS_CTRL
||
553 stage
== MESA_SHADER_TESS_EVAL
||
554 ctx
->is_gs_copy_shader
)
555 user_sgpr_info
->need_ring_offsets
= true;
557 if (stage
== MESA_SHADER_FRAGMENT
&&
558 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
559 user_sgpr_info
->need_ring_offsets
= true;
561 /* 2 user sgprs will nearly always be allocated for scratch/rings */
562 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
563 user_sgpr_info
->sgpr_count
+= 2;
566 /* FIXME: fix the number of user sgprs for merged shaders on GFX9 */
568 case MESA_SHADER_COMPUTE
:
569 if (ctx
->shader_info
->info
.cs
.uses_grid_size
)
570 user_sgpr_info
->sgpr_count
+= 3;
572 case MESA_SHADER_FRAGMENT
:
573 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
575 case MESA_SHADER_VERTEX
:
576 if (!ctx
->is_gs_copy_shader
) {
577 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
578 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
579 user_sgpr_info
->sgpr_count
+= 3;
581 user_sgpr_info
->sgpr_count
+= 2;
584 if (ctx
->options
->key
.vs
.as_ls
)
585 user_sgpr_info
->sgpr_count
++;
587 case MESA_SHADER_TESS_CTRL
:
588 user_sgpr_info
->sgpr_count
+= 4;
590 case MESA_SHADER_TESS_EVAL
:
591 user_sgpr_info
->sgpr_count
+= 1;
593 case MESA_SHADER_GEOMETRY
:
594 user_sgpr_info
->sgpr_count
+= 2;
600 if (ctx
->shader_info
->info
.needs_push_constants
)
601 user_sgpr_info
->sgpr_count
+= 2;
603 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
604 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
605 user_sgpr_info
->sgpr_count
+= 2;
606 user_sgpr_info
->indirect_all_descriptor_sets
= true;
608 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
613 declare_global_input_sgprs(struct nir_to_llvm_context
*ctx
,
614 gl_shader_stage stage
,
615 bool has_previous_stage
,
616 gl_shader_stage previous_stage
,
617 const struct user_sgpr_info
*user_sgpr_info
,
618 struct arg_info
*args
,
619 LLVMValueRef
*desc_sets
)
621 LLVMTypeRef type
= const_array(ctx
->ac
.i8
, 1024 * 1024);
622 unsigned num_sets
= ctx
->options
->layout
?
623 ctx
->options
->layout
->num_sets
: 0;
624 unsigned stage_mask
= 1 << stage
;
626 if (has_previous_stage
)
627 stage_mask
|= 1 << previous_stage
;
629 /* 1 for each descriptor set */
630 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
631 for (unsigned i
= 0; i
< num_sets
; ++i
) {
632 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
633 add_array_arg(args
, type
,
634 &ctx
->descriptor_sets
[i
]);
638 add_array_arg(args
, const_array(type
, 32), desc_sets
);
641 if (ctx
->shader_info
->info
.needs_push_constants
) {
642 /* 1 for push constants and dynamic descriptors */
643 add_array_arg(args
, type
, &ctx
->push_constants
);
648 declare_vs_specific_input_sgprs(struct nir_to_llvm_context
*ctx
,
649 gl_shader_stage stage
,
650 bool has_previous_stage
,
651 gl_shader_stage previous_stage
,
652 struct arg_info
*args
)
654 if (!ctx
->is_gs_copy_shader
&&
655 (stage
== MESA_SHADER_VERTEX
||
656 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
657 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
658 add_arg(args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
659 &ctx
->vertex_buffers
);
661 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
);
662 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);
663 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
664 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
);
670 declare_vs_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
672 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
);
673 if (!ctx
->is_gs_copy_shader
) {
674 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->rel_auto_id
);
675 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->vs_prim_id
);
676 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
681 declare_tes_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
683 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_u
);
684 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_v
);
685 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
);
686 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.tes_patch_id
);
690 set_global_input_locs(struct nir_to_llvm_context
*ctx
, gl_shader_stage stage
,
691 bool has_previous_stage
, gl_shader_stage previous_stage
,
692 const struct user_sgpr_info
*user_sgpr_info
,
693 LLVMValueRef desc_sets
, uint8_t *user_sgpr_idx
)
695 unsigned num_sets
= ctx
->options
->layout
?
696 ctx
->options
->layout
->num_sets
: 0;
697 unsigned stage_mask
= 1 << stage
;
699 if (has_previous_stage
)
700 stage_mask
|= 1 << previous_stage
;
702 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
703 for (unsigned i
= 0; i
< num_sets
; ++i
) {
704 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
705 set_loc_desc(ctx
, i
, user_sgpr_idx
, 0);
707 ctx
->descriptor_sets
[i
] = NULL
;
710 set_loc_shader(ctx
, AC_UD_INDIRECT_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
, i
* 8);
716 ctx
->descriptor_sets
[i
] =
717 ac_build_load_to_sgpr(&ctx
->ac
,
719 LLVMConstInt(ctx
->ac
.i32
, i
, false));
722 ctx
->descriptor_sets
[i
] = NULL
;
724 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
727 if (ctx
->shader_info
->info
.needs_push_constants
) {
728 set_loc_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
733 set_vs_specific_input_locs(struct nir_to_llvm_context
*ctx
,
734 gl_shader_stage stage
, bool has_previous_stage
,
735 gl_shader_stage previous_stage
,
736 uint8_t *user_sgpr_idx
)
738 if (!ctx
->is_gs_copy_shader
&&
739 (stage
== MESA_SHADER_VERTEX
||
740 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
741 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
742 set_loc_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
,
747 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
750 set_loc_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
751 user_sgpr_idx
, vs_num
);
755 static void create_function(struct nir_to_llvm_context
*ctx
,
756 gl_shader_stage stage
,
757 bool has_previous_stage
,
758 gl_shader_stage previous_stage
)
760 uint8_t user_sgpr_idx
;
761 struct user_sgpr_info user_sgpr_info
;
762 struct arg_info args
= {};
763 LLVMValueRef desc_sets
;
765 allocate_user_sgprs(ctx
, stage
, &user_sgpr_info
);
767 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
768 add_arg(&args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
773 case MESA_SHADER_COMPUTE
:
774 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
775 previous_stage
, &user_sgpr_info
,
778 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
779 add_arg(&args
, ARG_SGPR
, ctx
->ac
.v3i32
,
780 &ctx
->num_work_groups
);
783 for (int i
= 0; i
< 3; i
++) {
784 ctx
->workgroup_ids
[i
] = NULL
;
785 if (ctx
->shader_info
->info
.cs
.uses_block_id
[i
]) {
786 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
787 &ctx
->workgroup_ids
[i
]);
791 if (ctx
->shader_info
->info
.cs
.uses_local_invocation_idx
)
792 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tg_size
);
793 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
,
794 &ctx
->local_invocation_ids
);
796 case MESA_SHADER_VERTEX
:
797 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
798 previous_stage
, &user_sgpr_info
,
800 declare_vs_specific_input_sgprs(ctx
, stage
, has_previous_stage
,
801 previous_stage
, &args
);
803 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
))
804 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
805 if (ctx
->options
->key
.vs
.as_es
)
806 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
808 else if (ctx
->options
->key
.vs
.as_ls
)
809 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
810 &ctx
->ls_out_layout
);
812 declare_vs_input_vgprs(ctx
, &args
);
814 case MESA_SHADER_TESS_CTRL
:
815 if (has_previous_stage
) {
816 // First 6 system regs
817 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
818 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
819 &ctx
->merged_wave_info
);
820 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
821 &ctx
->tess_factor_offset
);
823 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
824 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
825 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
827 declare_global_input_sgprs(ctx
, stage
,
830 &user_sgpr_info
, &args
,
832 declare_vs_specific_input_sgprs(ctx
, stage
,
834 previous_stage
, &args
);
836 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
837 &ctx
->ls_out_layout
);
839 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
840 &ctx
->tcs_offchip_layout
);
841 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
842 &ctx
->tcs_out_offsets
);
843 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
844 &ctx
->tcs_out_layout
);
845 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
846 &ctx
->tcs_in_layout
);
847 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
848 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
851 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
852 &ctx
->abi
.tcs_patch_id
);
853 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
854 &ctx
->abi
.tcs_rel_ids
);
856 declare_vs_input_vgprs(ctx
, &args
);
858 declare_global_input_sgprs(ctx
, stage
,
861 &user_sgpr_info
, &args
,
864 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
865 &ctx
->tcs_offchip_layout
);
866 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
867 &ctx
->tcs_out_offsets
);
868 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
869 &ctx
->tcs_out_layout
);
870 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
871 &ctx
->tcs_in_layout
);
872 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
873 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
876 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
877 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
878 &ctx
->tess_factor_offset
);
879 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
880 &ctx
->abi
.tcs_patch_id
);
881 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
882 &ctx
->abi
.tcs_rel_ids
);
885 case MESA_SHADER_TESS_EVAL
:
886 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
887 previous_stage
, &user_sgpr_info
,
890 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
);
891 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
892 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
894 if (ctx
->options
->key
.tes
.as_es
) {
895 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
896 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
897 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
900 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
901 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
903 declare_tes_input_vgprs(ctx
, &args
);
905 case MESA_SHADER_GEOMETRY
:
906 if (has_previous_stage
) {
907 // First 6 system regs
908 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
910 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
911 &ctx
->merged_wave_info
);
912 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
914 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
915 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
916 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
918 declare_global_input_sgprs(ctx
, stage
,
921 &user_sgpr_info
, &args
,
924 if (previous_stage
== MESA_SHADER_TESS_EVAL
) {
925 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
926 &ctx
->tcs_offchip_layout
);
928 declare_vs_specific_input_sgprs(ctx
, stage
,
934 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
935 &ctx
->gsvs_ring_stride
);
936 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
937 &ctx
->gsvs_num_entries
);
938 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
939 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
942 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
943 &ctx
->gs_vtx_offset
[0]);
944 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
945 &ctx
->gs_vtx_offset
[2]);
946 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
947 &ctx
->abi
.gs_prim_id
);
948 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
949 &ctx
->abi
.gs_invocation_id
);
950 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
951 &ctx
->gs_vtx_offset
[4]);
953 if (previous_stage
== MESA_SHADER_VERTEX
) {
954 declare_vs_input_vgprs(ctx
, &args
);
956 declare_tes_input_vgprs(ctx
, &args
);
959 declare_global_input_sgprs(ctx
, stage
,
962 &user_sgpr_info
, &args
,
965 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
966 &ctx
->gsvs_ring_stride
);
967 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
968 &ctx
->gsvs_num_entries
);
969 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
970 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
973 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
);
974 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs_wave_id
);
975 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
976 &ctx
->gs_vtx_offset
[0]);
977 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
978 &ctx
->gs_vtx_offset
[1]);
979 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
980 &ctx
->abi
.gs_prim_id
);
981 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
982 &ctx
->gs_vtx_offset
[2]);
983 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
984 &ctx
->gs_vtx_offset
[3]);
985 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
986 &ctx
->gs_vtx_offset
[4]);
987 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
988 &ctx
->gs_vtx_offset
[5]);
989 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
990 &ctx
->abi
.gs_invocation_id
);
993 case MESA_SHADER_FRAGMENT
:
994 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
995 previous_stage
, &user_sgpr_info
,
998 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
999 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1000 &ctx
->sample_pos_offset
);
1002 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->prim_mask
);
1003 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_sample
);
1004 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_center
);
1005 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
);
1006 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
1007 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_sample
);
1008 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_center
);
1009 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
);
1010 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
1011 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]);
1012 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]);
1013 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]);
1014 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]);
1015 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.front_face
);
1016 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
);
1017 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
);
1018 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* fixed pt */
1021 unreachable("Shader stage not implemented");
1024 ctx
->main_function
= create_llvm_function(
1025 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
1026 ctx
->max_workgroup_size
,
1027 ctx
->options
->unsafe_math
);
1028 set_llvm_calling_convention(ctx
->main_function
, stage
);
1031 ctx
->shader_info
->num_input_vgprs
= 0;
1032 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
1034 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
1036 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1037 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
1039 assign_arguments(ctx
->main_function
, &args
);
1043 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
1044 set_loc_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
,
1046 if (ctx
->options
->supports_spill
) {
1047 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
1048 LLVMPointerType(ctx
->ac
.i8
, CONST_ADDR_SPACE
),
1049 NULL
, 0, AC_FUNC_ATTR_READNONE
);
1050 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
1051 const_array(ctx
->ac
.v4i32
, 16), "");
1055 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
1056 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
1057 if (has_previous_stage
)
1060 set_global_input_locs(ctx
, stage
, has_previous_stage
, previous_stage
,
1061 &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
1064 case MESA_SHADER_COMPUTE
:
1065 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
1066 set_loc_shader(ctx
, AC_UD_CS_GRID_SIZE
,
1070 case MESA_SHADER_VERTEX
:
1071 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1072 previous_stage
, &user_sgpr_idx
);
1073 if (ctx
->view_index
)
1074 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1075 if (ctx
->options
->key
.vs
.as_ls
) {
1076 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1079 if (ctx
->options
->key
.vs
.as_ls
)
1080 ac_declare_lds_as_pointer(&ctx
->ac
);
1082 case MESA_SHADER_TESS_CTRL
:
1083 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1084 previous_stage
, &user_sgpr_idx
);
1085 if (has_previous_stage
)
1086 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1088 set_loc_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
1089 if (ctx
->view_index
)
1090 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1091 ac_declare_lds_as_pointer(&ctx
->ac
);
1093 case MESA_SHADER_TESS_EVAL
:
1094 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
1095 if (ctx
->view_index
)
1096 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1098 case MESA_SHADER_GEOMETRY
:
1099 if (has_previous_stage
) {
1100 if (previous_stage
== MESA_SHADER_VERTEX
)
1101 set_vs_specific_input_locs(ctx
, stage
,
1106 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
,
1109 set_loc_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
,
1111 if (ctx
->view_index
)
1112 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1113 if (has_previous_stage
)
1114 ac_declare_lds_as_pointer(&ctx
->ac
);
1116 case MESA_SHADER_FRAGMENT
:
1117 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
1118 set_loc_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
,
1123 unreachable("Shader stage not implemented");
1126 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
1129 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1130 LLVMValueRef value
, unsigned count
)
1132 unsigned num_components
= ac_get_llvm_num_components(value
);
1133 if (count
== num_components
)
1136 LLVMValueRef masks
[] = {
1137 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1138 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1141 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1144 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1145 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1149 build_store_values_extended(struct ac_llvm_context
*ac
,
1150 LLVMValueRef
*values
,
1151 unsigned value_count
,
1152 unsigned value_stride
,
1155 LLVMBuilderRef builder
= ac
->builder
;
1158 for (i
= 0; i
< value_count
; i
++) {
1159 LLVMValueRef ptr
= values
[i
* value_stride
];
1160 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1161 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1162 LLVMBuildStore(builder
, value
, ptr
);
1166 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1167 const nir_ssa_def
*def
)
1169 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1170 if (def
->num_components
> 1) {
1171 type
= LLVMVectorType(type
, def
->num_components
);
1176 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1179 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1180 return (LLVMValueRef
)entry
->data
;
1184 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1185 const struct nir_block
*b
)
1187 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1188 return (LLVMBasicBlockRef
)entry
->data
;
1191 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1193 unsigned num_components
)
1195 LLVMValueRef value
= get_src(ctx
, src
.src
);
1196 bool need_swizzle
= false;
1199 LLVMTypeRef type
= LLVMTypeOf(value
);
1200 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1201 ? LLVMGetVectorSize(type
)
1204 for (unsigned i
= 0; i
< num_components
; ++i
) {
1205 assert(src
.swizzle
[i
] < src_components
);
1206 if (src
.swizzle
[i
] != i
)
1207 need_swizzle
= true;
1210 if (need_swizzle
|| num_components
!= src_components
) {
1211 LLVMValueRef masks
[] = {
1212 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1213 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1214 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1215 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1217 if (src_components
> 1 && num_components
== 1) {
1218 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1220 } else if (src_components
== 1 && num_components
> 1) {
1221 LLVMValueRef values
[] = {value
, value
, value
, value
};
1222 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1224 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1225 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1229 assert(!src
.negate
);
1234 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1235 LLVMIntPredicate pred
, LLVMValueRef src0
,
1238 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1239 return LLVMBuildSelect(ctx
->builder
, result
,
1240 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1244 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1245 LLVMRealPredicate pred
, LLVMValueRef src0
,
1248 LLVMValueRef result
;
1249 src0
= ac_to_float(ctx
, src0
);
1250 src1
= ac_to_float(ctx
, src1
);
1251 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1252 return LLVMBuildSelect(ctx
->builder
, result
,
1253 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1257 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1259 LLVMTypeRef result_type
,
1263 LLVMValueRef params
[] = {
1264 ac_to_float(ctx
, src0
),
1267 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1268 get_elem_bits(ctx
, result_type
));
1269 assert(length
< sizeof(name
));
1270 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1273 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1275 LLVMTypeRef result_type
,
1276 LLVMValueRef src0
, LLVMValueRef src1
)
1279 LLVMValueRef params
[] = {
1280 ac_to_float(ctx
, src0
),
1281 ac_to_float(ctx
, src1
),
1284 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1285 get_elem_bits(ctx
, result_type
));
1286 assert(length
< sizeof(name
));
1287 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1290 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1292 LLVMTypeRef result_type
,
1293 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1296 LLVMValueRef params
[] = {
1297 ac_to_float(ctx
, src0
),
1298 ac_to_float(ctx
, src1
),
1299 ac_to_float(ctx
, src2
),
1302 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1303 get_elem_bits(ctx
, result_type
));
1304 assert(length
< sizeof(name
));
1305 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1308 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1309 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1311 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1313 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1316 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1317 LLVMIntPredicate pred
,
1318 LLVMValueRef src0
, LLVMValueRef src1
)
1320 return LLVMBuildSelect(ctx
->builder
,
1321 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1326 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1329 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1330 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1333 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1336 LLVMValueRef cmp
, val
;
1338 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32_0
, "");
1339 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32_1
, src0
, "");
1340 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32_0
, "");
1341 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1345 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1348 LLVMValueRef cmp
, val
;
1350 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32_0
, "");
1351 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32_1
, src0
, "");
1352 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32_0
, "");
1353 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1357 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1360 const char *intr
= "llvm.floor.f32";
1361 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1362 LLVMValueRef params
[] = {
1365 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1366 ctx
->f32
, params
, 1,
1367 AC_FUNC_ATTR_READNONE
);
1368 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1371 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1373 LLVMValueRef src0
, LLVMValueRef src1
)
1375 LLVMTypeRef ret_type
;
1376 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1378 LLVMValueRef params
[] = { src0
, src1
};
1379 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1382 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1383 params
, 2, AC_FUNC_ATTR_READNONE
);
1385 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1386 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1390 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1393 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1396 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1399 src0
= ac_to_float(ctx
, src0
);
1400 return LLVMBuildSExt(ctx
->builder
,
1401 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1405 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1408 return LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1411 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1414 return LLVMBuildSExt(ctx
->builder
,
1415 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1419 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1422 LLVMValueRef result
;
1423 LLVMValueRef cond
= NULL
;
1425 src0
= ac_to_float(&ctx
->ac
, src0
);
1426 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->ac
.f16
, "");
1428 if (ctx
->options
->chip_class
>= VI
) {
1429 LLVMValueRef args
[2];
1430 /* Check if the result is a denormal - and flush to 0 if so. */
1432 args
[1] = LLVMConstInt(ctx
->ac
.i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1433 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->ac
.i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1436 /* need to convert back up to f32 */
1437 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->ac
.f32
, "");
1439 if (ctx
->options
->chip_class
>= VI
)
1440 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1443 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1444 * so compare the result and flush to 0 if it's smaller.
1446 LLVMValueRef temp
, cond2
;
1447 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1448 ctx
->ac
.f32
, result
);
1449 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1450 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, 0x38800000, false), ctx
->ac
.f32
, ""),
1452 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1453 temp
, ctx
->ac
.f32_0
, "");
1454 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1455 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1460 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1461 LLVMValueRef src0
, LLVMValueRef src1
)
1463 LLVMValueRef dst64
, result
;
1464 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1465 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1467 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1468 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1469 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1473 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1474 LLVMValueRef src0
, LLVMValueRef src1
)
1476 LLVMValueRef dst64
, result
;
1477 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1478 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1480 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1481 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1482 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1486 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1488 const LLVMValueRef srcs
[3])
1490 LLVMValueRef result
;
1491 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1493 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1494 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1498 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1499 LLVMValueRef src0
, LLVMValueRef src1
,
1500 LLVMValueRef src2
, LLVMValueRef src3
)
1502 LLVMValueRef bfi_args
[3], result
;
1504 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1505 LLVMBuildSub(ctx
->builder
,
1506 LLVMBuildShl(ctx
->builder
,
1511 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1514 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1517 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1518 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1520 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1521 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1522 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1524 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1528 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1531 LLVMValueRef comp
[2];
1533 src0
= ac_to_float(ctx
, src0
);
1534 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1535 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1537 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
1540 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1543 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1544 LLVMValueRef temps
[2], result
, val
;
1547 for (i
= 0; i
< 2; i
++) {
1548 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1549 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1550 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1551 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1554 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1556 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1561 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1567 LLVMValueRef result
;
1569 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1570 mask
= AC_TID_MASK_LEFT
;
1571 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1572 mask
= AC_TID_MASK_TOP
;
1574 mask
= AC_TID_MASK_TOP_LEFT
;
1576 /* for DDX we want to next X pixel, DDY next Y pixel. */
1577 if (op
== nir_op_fddx_fine
||
1578 op
== nir_op_fddx_coarse
||
1584 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1589 * this takes an I,J coordinate pair,
1590 * and works out the X and Y derivatives.
1591 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1593 static LLVMValueRef
emit_ddxy_interp(
1594 struct ac_nir_context
*ctx
,
1595 LLVMValueRef interp_ij
)
1597 LLVMValueRef result
[4], a
;
1600 for (i
= 0; i
< 2; i
++) {
1601 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1602 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1603 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1604 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1606 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1609 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1611 LLVMValueRef src
[4], result
= NULL
;
1612 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1613 unsigned src_components
;
1614 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1616 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1617 switch (instr
->op
) {
1623 case nir_op_pack_half_2x16
:
1626 case nir_op_unpack_half_2x16
:
1630 src_components
= num_components
;
1633 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1634 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1636 switch (instr
->op
) {
1642 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1643 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1646 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1649 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1652 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1655 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1656 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1657 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1660 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1661 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1662 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1665 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1668 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1671 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1674 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1677 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1678 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1679 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1680 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1681 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1682 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1683 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1686 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1687 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1688 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1691 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1694 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1697 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1700 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1701 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1702 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1705 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1706 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1707 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1710 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1711 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, src
[0]);
1714 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1717 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1720 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1723 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1724 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1725 LLVMTypeOf(src
[0]), ""),
1729 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1730 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1731 LLVMTypeOf(src
[0]), ""),
1735 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1736 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1737 LLVMTypeOf(src
[0]), ""),
1741 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1744 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1747 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1750 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1753 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1756 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1759 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1762 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1765 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1768 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1771 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1772 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1775 result
= emit_iabs(&ctx
->ac
, src
[0]);
1778 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1781 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1784 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1787 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1790 result
= emit_isign(&ctx
->ac
, src
[0]);
1793 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1794 result
= emit_fsign(&ctx
->ac
, src
[0]);
1797 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1798 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1801 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1802 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1805 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1806 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1808 case nir_op_fround_even
:
1809 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1810 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1813 result
= emit_ffract(&ctx
->ac
, src
[0]);
1816 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1817 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1820 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1821 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1824 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1825 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1828 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1829 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1832 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1833 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1836 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1837 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1838 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, result
);
1841 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1842 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1845 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1846 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1847 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1848 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1849 ac_to_float_type(&ctx
->ac
, def_type
),
1853 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1854 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1855 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1856 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1857 ac_to_float_type(&ctx
->ac
, def_type
),
1861 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1862 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1864 case nir_op_ibitfield_extract
:
1865 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1867 case nir_op_ubitfield_extract
:
1868 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1870 case nir_op_bitfield_insert
:
1871 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1873 case nir_op_bitfield_reverse
:
1874 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1876 case nir_op_bit_count
:
1877 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1882 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1883 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1884 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1888 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1889 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1893 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1894 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1898 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1899 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1903 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1904 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1907 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1908 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1911 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1915 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1916 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1917 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1919 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1923 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1924 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1925 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1927 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1930 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1932 case nir_op_find_lsb
:
1933 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1934 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1936 case nir_op_ufind_msb
:
1937 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1938 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1940 case nir_op_ifind_msb
:
1941 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1942 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1944 case nir_op_uadd_carry
:
1945 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1946 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1947 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1949 case nir_op_usub_borrow
:
1950 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1951 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1952 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1955 result
= emit_b2f(&ctx
->ac
, src
[0]);
1958 result
= emit_f2b(&ctx
->ac
, src
[0]);
1961 result
= emit_b2i(&ctx
->ac
, src
[0]);
1964 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1965 result
= emit_i2b(&ctx
->ac
, src
[0]);
1967 case nir_op_fquantize2f16
:
1968 result
= emit_f2f16(ctx
->nctx
, src
[0]);
1970 case nir_op_umul_high
:
1971 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1972 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1973 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1975 case nir_op_imul_high
:
1976 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1977 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1978 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1980 case nir_op_pack_half_2x16
:
1981 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
1983 case nir_op_unpack_half_2x16
:
1984 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1988 case nir_op_fddx_fine
:
1989 case nir_op_fddy_fine
:
1990 case nir_op_fddx_coarse
:
1991 case nir_op_fddy_coarse
:
1992 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1995 case nir_op_unpack_64_2x32_split_x
: {
1996 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1997 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2000 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2005 case nir_op_unpack_64_2x32_split_y
: {
2006 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2007 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2010 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2015 case nir_op_pack_64_2x32_split
: {
2016 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
2017 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2018 src
[0], ctx
->ac
.i32_0
, "");
2019 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2020 src
[1], ctx
->ac
.i32_1
, "");
2021 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
2026 fprintf(stderr
, "Unknown NIR alu instr: ");
2027 nir_print_instr(&instr
->instr
, stderr
);
2028 fprintf(stderr
, "\n");
2033 assert(instr
->dest
.dest
.is_ssa
);
2034 result
= ac_to_integer(&ctx
->ac
, result
);
2035 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
2040 static void visit_load_const(struct ac_nir_context
*ctx
,
2041 const nir_load_const_instr
*instr
)
2043 LLVMValueRef values
[4], value
= NULL
;
2044 LLVMTypeRef element_type
=
2045 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
2047 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
2048 switch (instr
->def
.bit_size
) {
2050 values
[i
] = LLVMConstInt(element_type
,
2051 instr
->value
.u32
[i
], false);
2054 values
[i
] = LLVMConstInt(element_type
,
2055 instr
->value
.u64
[i
], false);
2059 "unsupported nir load_const bit_size: %d\n",
2060 instr
->def
.bit_size
);
2064 if (instr
->def
.num_components
> 1) {
2065 value
= LLVMConstVector(values
, instr
->def
.num_components
);
2069 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
2072 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
2075 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2076 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2077 LLVMPointerType(type
, addr_space
), "");
2081 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
2084 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2085 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
2088 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
2089 /* On VI, the descriptor contains the size in bytes,
2090 * but TXQ must return the size in elements.
2091 * The stride is always non-zero for resources using TXQ.
2093 LLVMValueRef stride
=
2094 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2096 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2097 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2098 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2099 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2101 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2107 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2110 static void build_int_type_name(
2112 char *buf
, unsigned bufsize
)
2114 assert(bufsize
>= 6);
2116 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2117 snprintf(buf
, bufsize
, "v%ui32",
2118 LLVMGetVectorSize(type
));
2123 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2124 struct ac_image_args
*args
,
2125 const nir_tex_instr
*instr
)
2127 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2128 LLVMValueRef coord
= args
->addr
;
2129 LLVMValueRef half_texel
[2];
2130 LLVMValueRef compare_cube_wa
= NULL
;
2131 LLVMValueRef result
;
2133 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2137 struct ac_image_args txq_args
= { 0 };
2139 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2140 txq_args
.opcode
= ac_image_get_resinfo
;
2141 txq_args
.dmask
= 0xf;
2142 txq_args
.addr
= ctx
->i32_0
;
2143 txq_args
.resource
= args
->resource
;
2144 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2146 for (c
= 0; c
< 2; c
++) {
2147 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2148 LLVMConstInt(ctx
->i32
, c
, false), "");
2149 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2150 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2151 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2152 LLVMConstReal(ctx
->f32
, -0.5), "");
2156 LLVMValueRef orig_coords
= args
->addr
;
2158 for (c
= 0; c
< 2; c
++) {
2160 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2161 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2162 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2163 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2164 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2165 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2170 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2171 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2172 * workaround by sampling using a scaled type and converting.
2173 * This is taken from amdgpu-pro shaders.
2175 /* NOTE this produces some ugly code compared to amdgpu-pro,
2176 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2177 * and then reads them back. -pro generates two selects,
2178 * one s_cmp for the descriptor rewriting
2179 * one v_cmp for the coordinate and result changes.
2181 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2182 LLVMValueRef tmp
, tmp2
;
2184 /* workaround 8/8/8/8 uint/sint cube gather bug */
2185 /* first detect it then change to a scaled read and f2i */
2186 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2189 /* extract the DATA_FORMAT */
2190 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2191 LLVMConstInt(ctx
->i32
, 6, false), false);
2193 /* is the DATA_FORMAT == 8_8_8_8 */
2194 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2196 if (stype
== GLSL_TYPE_UINT
)
2197 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2198 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2199 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2201 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2202 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2203 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2205 /* replace the NUM FORMAT in the descriptor */
2206 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2207 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2209 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2211 /* don't modify the coordinates for this case */
2212 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2215 result
= ac_build_image_opcode(ctx
, args
);
2217 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2218 LLVMValueRef tmp
, tmp2
;
2220 /* if the cube workaround is in place, f2i the result. */
2221 for (c
= 0; c
< 4; c
++) {
2222 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2223 if (stype
== GLSL_TYPE_UINT
)
2224 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2226 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2227 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2228 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2229 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2230 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2231 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2237 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2238 const nir_tex_instr
*instr
,
2240 struct ac_image_args
*args
)
2242 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2243 return ac_build_buffer_load_format(&ctx
->ac
,
2250 args
->opcode
= ac_image_sample
;
2251 args
->compare
= instr
->is_shadow
;
2253 switch (instr
->op
) {
2255 case nir_texop_txf_ms
:
2256 case nir_texop_samples_identical
:
2257 args
->opcode
= lod_is_zero
||
2258 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
2259 ac_image_load
: ac_image_load_mip
;
2260 args
->compare
= false;
2261 args
->offset
= false;
2268 args
->level_zero
= true;
2273 case nir_texop_query_levels
:
2274 args
->opcode
= ac_image_get_resinfo
;
2277 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2278 args
->level_zero
= true;
2284 args
->opcode
= ac_image_gather4
;
2285 args
->level_zero
= true;
2288 args
->opcode
= ac_image_get_lod
;
2289 args
->compare
= false;
2290 args
->offset
= false;
2296 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2297 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2298 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2299 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2302 return ac_build_image_opcode(&ctx
->ac
, args
);
2305 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2306 nir_intrinsic_instr
*instr
)
2308 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2309 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2310 unsigned binding
= nir_intrinsic_binding(instr
);
2311 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2312 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2313 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2314 unsigned base_offset
= layout
->binding
[binding
].offset
;
2315 LLVMValueRef offset
, stride
;
2317 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2318 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2319 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2320 layout
->binding
[binding
].dynamic_offset_offset
;
2321 desc_ptr
= ctx
->push_constants
;
2322 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2323 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2325 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2327 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2328 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2329 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2331 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2332 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->ac
.v4i32
);
2333 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2338 static LLVMValueRef
visit_vulkan_resource_reindex(struct nir_to_llvm_context
*ctx
,
2339 nir_intrinsic_instr
*instr
)
2341 LLVMValueRef ptr
= get_src(ctx
->nir
, instr
->src
[0]);
2342 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[1]);
2344 LLVMValueRef result
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2345 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2349 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2350 nir_intrinsic_instr
*instr
)
2352 LLVMValueRef ptr
, addr
;
2354 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2355 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2357 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2358 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2360 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2363 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2364 const nir_intrinsic_instr
*instr
)
2366 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
2368 return get_buffer_size(ctx
, LLVMBuildLoad(ctx
->ac
.builder
, ptr
, ""), false);
2370 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2371 nir_intrinsic_instr
*instr
)
2373 const char *store_name
;
2374 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2375 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2376 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2377 int components_32bit
= elem_size_mult
* instr
->num_components
;
2378 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2379 LLVMValueRef base_data
, base_offset
;
2380 LLVMValueRef params
[6];
2382 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2383 get_src(ctx
, instr
->src
[1]), true);
2384 params
[2] = ctx
->ac
.i32_0
; /* vindex */
2385 params
[4] = ctx
->ac
.i1false
; /* glc */
2386 params
[5] = ctx
->ac
.i1false
; /* slc */
2388 if (components_32bit
> 1)
2389 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2391 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2392 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2393 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2395 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2399 LLVMValueRef offset
;
2401 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2403 /* Due to an LLVM limitation, split 3-element writes
2404 * into a 2-element and a 1-element write. */
2406 writemask
|= 1 << (start
+ 2);
2410 start
*= elem_size_mult
;
2411 count
*= elem_size_mult
;
2414 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2419 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2421 } else if (count
== 2) {
2422 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2423 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2424 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(ctx
->ac
.v2f32
), tmp
,
2427 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2428 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2429 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2431 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2435 if (ac_get_llvm_num_components(base_data
) > 1)
2436 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2437 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2440 store_name
= "llvm.amdgcn.buffer.store.f32";
2443 offset
= base_offset
;
2445 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2449 ac_build_intrinsic(&ctx
->ac
, store_name
,
2450 ctx
->ac
.voidt
, params
, 6, 0);
2454 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2455 const nir_intrinsic_instr
*instr
)
2458 LLVMValueRef params
[6];
2461 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2462 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2464 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2465 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2466 get_src(ctx
, instr
->src
[0]),
2468 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
2469 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2470 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2472 switch (instr
->intrinsic
) {
2473 case nir_intrinsic_ssbo_atomic_add
:
2474 name
= "llvm.amdgcn.buffer.atomic.add";
2476 case nir_intrinsic_ssbo_atomic_imin
:
2477 name
= "llvm.amdgcn.buffer.atomic.smin";
2479 case nir_intrinsic_ssbo_atomic_umin
:
2480 name
= "llvm.amdgcn.buffer.atomic.umin";
2482 case nir_intrinsic_ssbo_atomic_imax
:
2483 name
= "llvm.amdgcn.buffer.atomic.smax";
2485 case nir_intrinsic_ssbo_atomic_umax
:
2486 name
= "llvm.amdgcn.buffer.atomic.umax";
2488 case nir_intrinsic_ssbo_atomic_and
:
2489 name
= "llvm.amdgcn.buffer.atomic.and";
2491 case nir_intrinsic_ssbo_atomic_or
:
2492 name
= "llvm.amdgcn.buffer.atomic.or";
2494 case nir_intrinsic_ssbo_atomic_xor
:
2495 name
= "llvm.amdgcn.buffer.atomic.xor";
2497 case nir_intrinsic_ssbo_atomic_exchange
:
2498 name
= "llvm.amdgcn.buffer.atomic.swap";
2500 case nir_intrinsic_ssbo_atomic_comp_swap
:
2501 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2507 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2510 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2511 const nir_intrinsic_instr
*instr
)
2513 LLVMValueRef results
[2];
2514 int load_components
;
2515 int num_components
= instr
->num_components
;
2516 if (instr
->dest
.ssa
.bit_size
== 64)
2517 num_components
*= 2;
2519 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2520 load_components
= MIN2(num_components
- i
, 4);
2521 const char *load_name
;
2522 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2523 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2524 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2526 if (load_components
== 3)
2527 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2528 else if (load_components
> 1)
2529 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2531 if (load_components
>= 3)
2532 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2533 else if (load_components
== 2)
2534 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2535 else if (load_components
== 1)
2536 load_name
= "llvm.amdgcn.buffer.load.f32";
2538 unreachable("unhandled number of components");
2540 LLVMValueRef params
[] = {
2541 ctx
->abi
->load_ssbo(ctx
->abi
,
2542 get_src(ctx
, instr
->src
[0]),
2550 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2555 LLVMValueRef ret
= results
[0];
2556 if (num_components
> 4 || num_components
== 3) {
2557 LLVMValueRef masks
[] = {
2558 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2559 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2560 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2561 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2564 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2565 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2566 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2569 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2570 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2573 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2574 const nir_intrinsic_instr
*instr
)
2576 LLVMValueRef results
[8], ret
;
2577 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2578 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2579 int num_components
= instr
->num_components
;
2581 if (ctx
->abi
->load_ubo
)
2582 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2584 if (instr
->dest
.ssa
.bit_size
== 64)
2585 num_components
*= 2;
2587 for (unsigned i
= 0; i
< num_components
; ++i
) {
2588 LLVMValueRef params
[] = {
2590 LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, 4 * i
, 0),
2593 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const.v4i32", ctx
->ac
.f32
,
2595 AC_FUNC_ATTR_READNONE
|
2596 AC_FUNC_ATTR_LEGACY
);
2600 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2601 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2602 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2606 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2607 bool vs_in
, unsigned *vertex_index_out
,
2608 LLVMValueRef
*vertex_index_ref
,
2609 unsigned *const_out
, LLVMValueRef
*indir_out
)
2611 unsigned const_offset
= 0;
2612 nir_deref
*tail
= &deref
->deref
;
2613 LLVMValueRef offset
= NULL
;
2615 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2617 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2618 if (vertex_index_out
)
2619 *vertex_index_out
= deref_array
->base_offset
;
2621 if (vertex_index_ref
) {
2622 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2623 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2624 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2626 *vertex_index_ref
= vtx
;
2630 if (deref
->var
->data
.compact
) {
2631 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2632 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2633 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2634 /* We always lower indirect dereferences for "compact" array vars. */
2635 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2637 const_offset
= deref_array
->base_offset
;
2641 while (tail
->child
!= NULL
) {
2642 const struct glsl_type
*parent_type
= tail
->type
;
2645 if (tail
->deref_type
== nir_deref_type_array
) {
2646 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2647 LLVMValueRef index
, stride
, local_offset
;
2648 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2650 const_offset
+= size
* deref_array
->base_offset
;
2651 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2654 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2655 index
= get_src(ctx
, deref_array
->indirect
);
2656 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2657 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2660 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2662 offset
= local_offset
;
2663 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2664 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2666 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2667 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2668 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2671 unreachable("unsupported deref type");
2675 if (const_offset
&& offset
)
2676 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2677 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2680 *const_out
= const_offset
;
2681 *indir_out
= offset
;
2685 /* The offchip buffer layout for TCS->TES is
2687 * - attribute 0 of patch 0 vertex 0
2688 * - attribute 0 of patch 0 vertex 1
2689 * - attribute 0 of patch 0 vertex 2
2691 * - attribute 0 of patch 1 vertex 0
2692 * - attribute 0 of patch 1 vertex 1
2694 * - attribute 1 of patch 0 vertex 0
2695 * - attribute 1 of patch 0 vertex 1
2697 * - per patch attribute 0 of patch 0
2698 * - per patch attribute 0 of patch 1
2701 * Note that every attribute has 4 components.
2703 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2704 LLVMValueRef vertex_index
,
2705 LLVMValueRef param_index
)
2707 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2708 LLVMValueRef param_stride
, constant16
;
2709 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2711 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2712 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2713 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2716 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2718 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2719 vertices_per_patch
, "");
2721 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2724 param_stride
= total_vertices
;
2726 base_addr
= rel_patch_id
;
2727 param_stride
= num_patches
;
2730 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2731 LLVMBuildMul(ctx
->builder
, param_index
,
2732 param_stride
, ""), "");
2734 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2736 if (!vertex_index
) {
2737 LLVMValueRef patch_data_offset
=
2738 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2740 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2741 patch_data_offset
, "");
2746 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2748 unsigned const_index
,
2750 LLVMValueRef vertex_index
,
2751 LLVMValueRef indir_index
)
2753 LLVMValueRef param_index
;
2756 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2759 if (const_index
&& !is_compact
)
2760 param
+= const_index
;
2761 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2763 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2767 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2768 bool is_patch
, uint32_t param
)
2772 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2774 ctx
->tess_outputs_written
|= (1ull << param
);
2778 get_dw_address(struct nir_to_llvm_context
*ctx
,
2779 LLVMValueRef dw_addr
,
2781 unsigned const_index
,
2782 bool compact_const_index
,
2783 LLVMValueRef vertex_index
,
2784 LLVMValueRef stride
,
2785 LLVMValueRef indir_index
)
2790 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2791 LLVMBuildMul(ctx
->builder
,
2797 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2798 LLVMBuildMul(ctx
->builder
, indir_index
,
2799 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2800 else if (const_index
&& !compact_const_index
)
2801 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2802 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2804 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2805 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2807 if (const_index
&& compact_const_index
)
2808 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2809 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2814 load_tcs_input(struct ac_shader_abi
*abi
,
2815 LLVMValueRef vertex_index
,
2816 LLVMValueRef indir_index
,
2817 unsigned const_index
,
2819 unsigned driver_location
,
2821 unsigned num_components
,
2825 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2826 LLVMValueRef dw_addr
, stride
;
2827 LLVMValueRef value
[4], result
;
2828 unsigned param
= shader_io_get_unique_index(location
);
2830 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2831 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2832 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2835 for (unsigned i
= 0; i
< num_components
+ component
; i
++) {
2836 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2837 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2840 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
2845 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2846 nir_intrinsic_instr
*instr
)
2848 LLVMValueRef dw_addr
;
2849 LLVMValueRef stride
= NULL
;
2850 LLVMValueRef value
[4], result
;
2851 LLVMValueRef vertex_index
= NULL
;
2852 LLVMValueRef indir_index
= NULL
;
2853 unsigned const_index
= 0;
2855 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2856 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2857 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2858 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2859 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2860 &const_index
, &indir_index
);
2862 if (!instr
->variables
[0]->var
->data
.patch
) {
2863 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2864 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2866 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2869 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2872 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2873 for (unsigned i
= comp
; i
< instr
->num_components
+ comp
; i
++) {
2874 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2875 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2878 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2879 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2884 store_tcs_output(struct ac_shader_abi
*abi
,
2885 LLVMValueRef vertex_index
,
2886 LLVMValueRef param_index
,
2887 unsigned const_index
,
2889 unsigned driver_location
,
2896 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2897 LLVMValueRef dw_addr
;
2898 LLVMValueRef stride
= NULL
;
2899 LLVMValueRef buf_addr
= NULL
;
2901 bool store_lds
= true;
2904 if (!(ctx
->tcs_patch_outputs_read
& (1U << (location
- VARYING_SLOT_PATCH0
))))
2907 if (!(ctx
->tcs_outputs_read
& (1ULL << location
)))
2911 param
= shader_io_get_unique_index(location
);
2912 if (location
== VARYING_SLOT_CLIP_DIST0
&&
2913 is_compact
&& const_index
> 3) {
2919 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2920 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2922 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2925 mark_tess_output(ctx
, is_patch
, param
);
2927 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2929 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2930 vertex_index
, param_index
);
2932 bool is_tess_factor
= false;
2933 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2934 location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2935 is_tess_factor
= true;
2937 unsigned base
= is_compact
? const_index
: 0;
2938 for (unsigned chan
= 0; chan
< 8; chan
++) {
2939 if (!(writemask
& (1 << chan
)))
2941 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
2943 if (store_lds
|| is_tess_factor
)
2944 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
2946 if (!is_tess_factor
&& writemask
!= 0xF)
2947 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2948 buf_addr
, ctx
->oc_lds
,
2949 4 * (base
+ chan
), 1, 0, true, false);
2951 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2955 if (writemask
== 0xF) {
2956 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2957 buf_addr
, ctx
->oc_lds
,
2958 (base
* 4), 1, 0, true, false);
2963 load_tes_input(struct ac_shader_abi
*abi
,
2964 LLVMValueRef vertex_index
,
2965 LLVMValueRef param_index
,
2966 unsigned const_index
,
2968 unsigned driver_location
,
2970 unsigned num_components
,
2974 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2975 LLVMValueRef buf_addr
;
2976 LLVMValueRef result
;
2977 unsigned param
= shader_io_get_unique_index(location
);
2979 if (location
== VARYING_SLOT_CLIP_DIST0
&& is_compact
&& const_index
> 3) {
2984 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2985 is_compact
, vertex_index
, param_index
);
2987 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, component
* 4, false);
2988 buf_addr
= LLVMBuildAdd(ctx
->builder
, buf_addr
, comp_offset
, "");
2990 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, num_components
, NULL
,
2991 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2992 result
= trim_vector(&ctx
->ac
, result
, num_components
);
2997 load_gs_input(struct ac_shader_abi
*abi
,
2999 unsigned driver_location
,
3001 unsigned num_components
,
3002 unsigned vertex_index
,
3003 unsigned const_index
,
3006 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3007 LLVMValueRef vtx_offset
;
3008 LLVMValueRef args
[9];
3009 unsigned param
, vtx_offset_param
;
3010 LLVMValueRef value
[4], result
;
3012 vtx_offset_param
= vertex_index
;
3013 assert(vtx_offset_param
< 6);
3014 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
3015 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3017 param
= shader_io_get_unique_index(location
);
3019 for (unsigned i
= component
; i
< num_components
+ component
; i
++) {
3020 if (ctx
->ac
.chip_class
>= GFX9
) {
3021 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
3022 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
3023 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
3024 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
3026 args
[0] = ctx
->esgs_ring
;
3027 args
[1] = vtx_offset
;
3028 args
[2] = LLVMConstInt(ctx
->ac
.i32
, (param
* 4 + i
+ const_index
) * 256, false);
3029 args
[3] = ctx
->ac
.i32_0
;
3030 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
3031 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
3032 args
[6] = ctx
->ac
.i32_1
; /* GLC */
3033 args
[7] = ctx
->ac
.i32_0
; /* SLC */
3034 args
[8] = ctx
->ac
.i32_0
; /* TFE */
3036 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
3037 ctx
->ac
.i32
, args
, 9,
3038 AC_FUNC_ATTR_READONLY
|
3039 AC_FUNC_ATTR_LEGACY
);
3042 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
3048 build_gep_for_deref(struct ac_nir_context
*ctx
,
3049 nir_deref_var
*deref
)
3051 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
3052 assert(entry
->data
);
3053 LLVMValueRef val
= entry
->data
;
3054 nir_deref
*tail
= deref
->deref
.child
;
3055 while (tail
!= NULL
) {
3056 LLVMValueRef offset
;
3057 switch (tail
->deref_type
) {
3058 case nir_deref_type_array
: {
3059 nir_deref_array
*array
= nir_deref_as_array(tail
);
3060 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
3061 if (array
->deref_array_type
==
3062 nir_deref_array_type_indirect
) {
3063 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
3070 case nir_deref_type_struct
: {
3071 nir_deref_struct
*deref_struct
=
3072 nir_deref_as_struct(tail
);
3073 offset
= LLVMConstInt(ctx
->ac
.i32
,
3074 deref_struct
->index
, 0);
3078 unreachable("bad deref type");
3080 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3086 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3087 nir_intrinsic_instr
*instr
)
3089 LLVMValueRef values
[8];
3090 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3091 int ve
= instr
->dest
.ssa
.num_components
;
3092 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3093 LLVMValueRef indir_index
;
3095 unsigned const_index
;
3096 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
3097 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3098 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3099 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3100 &const_index
, &indir_index
);
3102 if (instr
->dest
.ssa
.bit_size
== 64)
3105 switch (instr
->variables
[0]->var
->data
.mode
) {
3106 case nir_var_shader_in
:
3107 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3108 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3109 LLVMValueRef result
;
3110 LLVMValueRef vertex_index
= NULL
;
3111 LLVMValueRef indir_index
= NULL
;
3112 unsigned const_index
= 0;
3113 unsigned location
= instr
->variables
[0]->var
->data
.location
;
3114 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3115 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3116 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3118 get_deref_offset(ctx
, instr
->variables
[0],
3119 false, NULL
, is_patch
? NULL
: &vertex_index
,
3120 &const_index
, &indir_index
);
3122 result
= ctx
->abi
->load_tess_inputs(ctx
->abi
, vertex_index
, indir_index
,
3123 const_index
, location
, driver_location
,
3124 instr
->variables
[0]->var
->data
.location_frac
,
3125 instr
->num_components
,
3126 is_patch
, is_compact
);
3127 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3130 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3131 LLVMValueRef indir_index
;
3132 unsigned const_index
, vertex_index
;
3133 get_deref_offset(ctx
, instr
->variables
[0],
3134 false, &vertex_index
, NULL
,
3135 &const_index
, &indir_index
);
3136 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
3137 instr
->variables
[0]->var
->data
.driver_location
,
3138 instr
->variables
[0]->var
->data
.location_frac
, ve
,
3139 vertex_index
, const_index
,
3140 nir2llvmtype(ctx
, instr
->variables
[0]->var
->type
));
3143 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3145 unsigned count
= glsl_count_attribute_slots(
3146 instr
->variables
[0]->var
->type
,
3147 ctx
->stage
== MESA_SHADER_VERTEX
);
3149 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3150 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3151 stride
, false, true);
3153 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3157 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
3161 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3163 unsigned count
= glsl_count_attribute_slots(
3164 instr
->variables
[0]->var
->type
, false);
3166 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3167 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3168 stride
, true, true);
3170 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3174 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
3178 case nir_var_shared
: {
3179 LLVMValueRef address
= build_gep_for_deref(ctx
,
3180 instr
->variables
[0]);
3181 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3182 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3183 get_def_type(ctx
, &instr
->dest
.ssa
),
3186 case nir_var_shader_out
:
3187 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3188 return load_tcs_output(ctx
->nctx
, instr
);
3190 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3192 unsigned count
= glsl_count_attribute_slots(
3193 instr
->variables
[0]->var
->type
, false);
3195 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3196 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3197 stride
, true, true);
3199 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3203 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3204 ctx
->outputs
[idx
+ chan
+ const_index
* stride
],
3210 unreachable("unhandle variable mode");
3212 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3213 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3217 visit_store_var(struct ac_nir_context
*ctx
,
3218 nir_intrinsic_instr
*instr
)
3220 LLVMValueRef temp_ptr
, value
;
3221 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3222 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3223 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3224 int writemask
= instr
->const_index
[0] << comp
;
3225 LLVMValueRef indir_index
;
3226 unsigned const_index
;
3227 get_deref_offset(ctx
, instr
->variables
[0], false,
3228 NULL
, NULL
, &const_index
, &indir_index
);
3230 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3231 int old_writemask
= writemask
;
3233 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3234 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
3238 for (unsigned chan
= 0; chan
< 4; chan
++) {
3239 if (old_writemask
& (1 << chan
))
3240 writemask
|= 3u << (2 * chan
);
3244 switch (instr
->variables
[0]->var
->data
.mode
) {
3245 case nir_var_shader_out
:
3247 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3248 LLVMValueRef vertex_index
= NULL
;
3249 LLVMValueRef indir_index
= NULL
;
3250 unsigned const_index
= 0;
3251 const unsigned location
= instr
->variables
[0]->var
->data
.location
;
3252 const unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3253 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3254 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3255 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3257 get_deref_offset(ctx
, instr
->variables
[0],
3258 false, NULL
, is_patch
? NULL
: &vertex_index
,
3259 &const_index
, &indir_index
);
3261 ctx
->abi
->store_tcs_outputs(ctx
->abi
, vertex_index
, indir_index
,
3262 const_index
, location
, driver_location
,
3263 src
, comp
, is_patch
, is_compact
, writemask
);
3267 for (unsigned chan
= 0; chan
< 8; chan
++) {
3269 if (!(writemask
& (1 << chan
)))
3272 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3274 if (instr
->variables
[0]->var
->data
.compact
)
3277 unsigned count
= glsl_count_attribute_slots(
3278 instr
->variables
[0]->var
->type
, false);
3280 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3281 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3282 stride
, true, true);
3284 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3285 value
, indir_index
, "");
3286 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3287 count
, stride
, tmp_vec
);
3290 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3292 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3297 for (unsigned chan
= 0; chan
< 8; chan
++) {
3298 if (!(writemask
& (1 << chan
)))
3301 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3303 unsigned count
= glsl_count_attribute_slots(
3304 instr
->variables
[0]->var
->type
, false);
3306 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3307 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3310 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3311 value
, indir_index
, "");
3312 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3315 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3317 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3321 case nir_var_shared
: {
3322 int writemask
= instr
->const_index
[0];
3323 LLVMValueRef address
= build_gep_for_deref(ctx
,
3324 instr
->variables
[0]);
3325 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3326 unsigned components
=
3327 glsl_get_vector_elements(
3328 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3329 if (writemask
== (1 << components
) - 1) {
3330 val
= LLVMBuildBitCast(
3331 ctx
->ac
.builder
, val
,
3332 LLVMGetElementType(LLVMTypeOf(address
)), "");
3333 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3335 for (unsigned chan
= 0; chan
< 4; chan
++) {
3336 if (!(writemask
& (1 << chan
)))
3339 LLVMBuildStructGEP(ctx
->ac
.builder
,
3341 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
3343 src
= LLVMBuildBitCast(
3344 ctx
->ac
.builder
, src
,
3345 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3346 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3356 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3359 case GLSL_SAMPLER_DIM_BUF
:
3361 case GLSL_SAMPLER_DIM_1D
:
3362 return array
? 2 : 1;
3363 case GLSL_SAMPLER_DIM_2D
:
3364 return array
? 3 : 2;
3365 case GLSL_SAMPLER_DIM_MS
:
3366 return array
? 4 : 3;
3367 case GLSL_SAMPLER_DIM_3D
:
3368 case GLSL_SAMPLER_DIM_CUBE
:
3370 case GLSL_SAMPLER_DIM_RECT
:
3371 case GLSL_SAMPLER_DIM_SUBPASS
:
3373 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3383 /* Adjust the sample index according to FMASK.
3385 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3386 * which is the identity mapping. Each nibble says which physical sample
3387 * should be fetched to get that sample.
3389 * For example, 0x11111100 means there are only 2 samples stored and
3390 * the second sample covers 3/4 of the pixel. When reading samples 0
3391 * and 1, return physical sample 0 (determined by the first two 0s
3392 * in FMASK), otherwise return physical sample 1.
3394 * The sample index should be adjusted as follows:
3395 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3397 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3398 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3399 LLVMValueRef coord_z
,
3400 LLVMValueRef sample_index
,
3401 LLVMValueRef fmask_desc_ptr
)
3403 LLVMValueRef fmask_load_address
[4];
3406 fmask_load_address
[0] = coord_x
;
3407 fmask_load_address
[1] = coord_y
;
3409 fmask_load_address
[2] = coord_z
;
3410 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3413 struct ac_image_args args
= {0};
3415 args
.opcode
= ac_image_load
;
3416 args
.da
= coord_z
? true : false;
3417 args
.resource
= fmask_desc_ptr
;
3419 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3421 res
= ac_build_image_opcode(ctx
, &args
);
3423 res
= ac_to_integer(ctx
, res
);
3424 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3425 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3427 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3431 LLVMValueRef sample_index4
=
3432 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3433 LLVMValueRef shifted_fmask
=
3434 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3435 LLVMValueRef final_sample
=
3436 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3438 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3439 * resource descriptor is 0 (invalid),
3441 LLVMValueRef fmask_desc
=
3442 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3445 LLVMValueRef fmask_word1
=
3446 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3449 LLVMValueRef word1_is_nonzero
=
3450 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3451 fmask_word1
, ctx
->i32_0
, "");
3453 /* Replace the MSAA sample index. */
3455 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3456 final_sample
, sample_index
, "");
3457 return sample_index
;
3460 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3461 const nir_intrinsic_instr
*instr
)
3463 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3464 if(instr
->variables
[0]->deref
.child
)
3465 type
= instr
->variables
[0]->deref
.child
->type
;
3467 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3468 LLVMValueRef coords
[4];
3469 LLVMValueRef masks
[] = {
3470 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3471 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3474 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3477 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3478 bool is_array
= glsl_sampler_type_is_array(type
);
3479 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3480 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3481 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3482 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3483 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3484 count
= image_type_to_components_count(dim
, is_array
);
3487 LLVMValueRef fmask_load_address
[3];
3490 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3491 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3493 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3495 fmask_load_address
[2] = NULL
;
3497 for (chan
= 0; chan
< 2; ++chan
)
3498 fmask_load_address
[chan
] =
3499 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3500 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3501 ctx
->ac
.i32
, ""), "");
3502 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3504 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3505 fmask_load_address
[0],
3506 fmask_load_address
[1],
3507 fmask_load_address
[2],
3509 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3511 if (count
== 1 && !gfx9_1d
) {
3512 if (instr
->src
[0].ssa
->num_components
)
3513 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3520 for (chan
= 0; chan
< count
; ++chan
) {
3521 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3524 for (chan
= 0; chan
< 2; ++chan
)
3525 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3526 ctx
->ac
.i32
, ""), "");
3527 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3533 coords
[2] = coords
[1];
3534 coords
[1] = ctx
->ac
.i32_0
;
3536 coords
[1] = ctx
->ac
.i32_0
;
3541 coords
[count
] = sample_index
;
3546 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3549 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3554 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3555 const nir_intrinsic_instr
*instr
)
3557 LLVMValueRef params
[7];
3559 char intrinsic_name
[64];
3560 const nir_variable
*var
= instr
->variables
[0]->var
;
3561 const struct glsl_type
*type
= var
->type
;
3563 if(instr
->variables
[0]->deref
.child
)
3564 type
= instr
->variables
[0]->deref
.child
->type
;
3566 type
= glsl_without_array(type
);
3567 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3568 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3569 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3570 ctx
->ac
.i32_0
, ""); /* vindex */
3571 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3572 params
[3] = ctx
->ac
.i1false
; /* glc */
3573 params
[4] = ctx
->ac
.i1false
; /* slc */
3574 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3577 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3578 res
= ac_to_integer(&ctx
->ac
, res
);
3580 bool is_da
= glsl_sampler_type_is_array(type
) ||
3581 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3582 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3583 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3584 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3585 LLVMValueRef glc
= ctx
->ac
.i1false
;
3586 LLVMValueRef slc
= ctx
->ac
.i1false
;
3588 params
[0] = get_image_coords(ctx
, instr
);
3589 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3590 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3591 if (HAVE_LLVM
<= 0x0309) {
3592 params
[3] = ctx
->ac
.i1false
; /* r128 */
3597 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3604 ac_get_image_intr_name("llvm.amdgcn.image.load",
3605 ctx
->ac
.v4f32
, /* vdata */
3606 LLVMTypeOf(params
[0]), /* coords */
3607 LLVMTypeOf(params
[1]), /* rsrc */
3608 intrinsic_name
, sizeof(intrinsic_name
));
3610 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3611 params
, 7, AC_FUNC_ATTR_READONLY
);
3613 return ac_to_integer(&ctx
->ac
, res
);
3616 static void visit_image_store(struct ac_nir_context
*ctx
,
3617 nir_intrinsic_instr
*instr
)
3619 LLVMValueRef params
[8];
3620 char intrinsic_name
[64];
3621 const nir_variable
*var
= instr
->variables
[0]->var
;
3622 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3623 LLVMValueRef glc
= ctx
->ac
.i1false
;
3624 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3626 glc
= ctx
->ac
.i1true
;
3628 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3629 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3630 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3631 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3632 ctx
->ac
.i32_0
, ""); /* vindex */
3633 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3634 params
[4] = glc
; /* glc */
3635 params
[5] = ctx
->ac
.i1false
; /* slc */
3636 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3639 bool is_da
= glsl_sampler_type_is_array(type
) ||
3640 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3641 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3642 LLVMValueRef slc
= ctx
->ac
.i1false
;
3644 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3645 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3646 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3647 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3648 if (HAVE_LLVM
<= 0x0309) {
3649 params
[4] = ctx
->ac
.i1false
; /* r128 */
3654 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3661 ac_get_image_intr_name("llvm.amdgcn.image.store",
3662 LLVMTypeOf(params
[0]), /* vdata */
3663 LLVMTypeOf(params
[1]), /* coords */
3664 LLVMTypeOf(params
[2]), /* rsrc */
3665 intrinsic_name
, sizeof(intrinsic_name
));
3667 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3673 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3674 const nir_intrinsic_instr
*instr
)
3676 LLVMValueRef params
[7];
3677 int param_count
= 0;
3678 const nir_variable
*var
= instr
->variables
[0]->var
;
3680 const char *atomic_name
;
3681 char intrinsic_name
[41];
3682 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3683 MAYBE_UNUSED
int length
;
3685 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3687 switch (instr
->intrinsic
) {
3688 case nir_intrinsic_image_atomic_add
:
3689 atomic_name
= "add";
3691 case nir_intrinsic_image_atomic_min
:
3692 atomic_name
= is_unsigned
? "umin" : "smin";
3694 case nir_intrinsic_image_atomic_max
:
3695 atomic_name
= is_unsigned
? "umax" : "smax";
3697 case nir_intrinsic_image_atomic_and
:
3698 atomic_name
= "and";
3700 case nir_intrinsic_image_atomic_or
:
3703 case nir_intrinsic_image_atomic_xor
:
3704 atomic_name
= "xor";
3706 case nir_intrinsic_image_atomic_exchange
:
3707 atomic_name
= "swap";
3709 case nir_intrinsic_image_atomic_comp_swap
:
3710 atomic_name
= "cmpswap";
3716 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3717 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3718 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3720 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3721 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3723 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3724 ctx
->ac
.i32_0
, ""); /* vindex */
3725 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3726 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3728 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3729 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3731 char coords_type
[8];
3733 bool da
= glsl_sampler_type_is_array(type
) ||
3734 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3736 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3737 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3739 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3740 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3741 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3743 build_int_type_name(LLVMTypeOf(coords
),
3744 coords_type
, sizeof(coords_type
));
3746 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3747 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3750 assert(length
< sizeof(intrinsic_name
));
3751 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3754 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3755 const nir_intrinsic_instr
*instr
)
3758 const nir_variable
*var
= instr
->variables
[0]->var
;
3759 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3760 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3761 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3762 if(instr
->variables
[0]->deref
.child
)
3763 type
= instr
->variables
[0]->deref
.child
->type
;
3765 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3766 return get_buffer_size(ctx
,
3767 get_sampler_desc(ctx
, instr
->variables
[0],
3768 AC_DESC_BUFFER
, NULL
, true, false), true);
3770 struct ac_image_args args
= { 0 };
3774 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3775 args
.opcode
= ac_image_get_resinfo
;
3776 args
.addr
= ctx
->ac
.i32_0
;
3778 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3780 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3782 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3783 glsl_sampler_type_is_array(type
)) {
3784 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3785 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3786 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3787 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3789 if (ctx
->ac
.chip_class
>= GFX9
&&
3790 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3791 glsl_sampler_type_is_array(type
)) {
3792 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3793 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3800 #define NOOP_WAITCNT 0xf7f
3801 #define LGKM_CNT 0x07f
3802 #define VM_CNT 0xf70
3804 static void emit_membar(struct nir_to_llvm_context
*ctx
,
3805 const nir_intrinsic_instr
*instr
)
3807 unsigned waitcnt
= NOOP_WAITCNT
;
3809 switch (instr
->intrinsic
) {
3810 case nir_intrinsic_memory_barrier
:
3811 case nir_intrinsic_group_memory_barrier
:
3812 waitcnt
&= VM_CNT
& LGKM_CNT
;
3814 case nir_intrinsic_memory_barrier_atomic_counter
:
3815 case nir_intrinsic_memory_barrier_buffer
:
3816 case nir_intrinsic_memory_barrier_image
:
3819 case nir_intrinsic_memory_barrier_shared
:
3820 waitcnt
&= LGKM_CNT
;
3825 if (waitcnt
!= NOOP_WAITCNT
)
3826 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3829 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3831 /* SI only (thanks to a hw bug workaround):
3832 * The real barrier instruction isn’t needed, because an entire patch
3833 * always fits into a single wave.
3835 if (ctx
->options
->chip_class
== SI
&&
3836 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3837 ac_build_waitcnt(&ctx
->ac
, LGKM_CNT
& VM_CNT
);
3840 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3841 ctx
->ac
.voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3844 static void emit_discard_if(struct ac_nir_context
*ctx
,
3845 const nir_intrinsic_instr
*instr
)
3849 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3850 get_src(ctx
, instr
->src
[0]),
3852 ac_build_kill_if_false(&ctx
->ac
, cond
);
3856 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3858 LLVMValueRef result
;
3859 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3860 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3861 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3863 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3866 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3867 const nir_intrinsic_instr
*instr
)
3869 LLVMValueRef ptr
, result
;
3870 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3871 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3873 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3874 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3875 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3877 LLVMAtomicOrderingSequentiallyConsistent
,
3878 LLVMAtomicOrderingSequentiallyConsistent
,
3881 LLVMAtomicRMWBinOp op
;
3882 switch (instr
->intrinsic
) {
3883 case nir_intrinsic_var_atomic_add
:
3884 op
= LLVMAtomicRMWBinOpAdd
;
3886 case nir_intrinsic_var_atomic_umin
:
3887 op
= LLVMAtomicRMWBinOpUMin
;
3889 case nir_intrinsic_var_atomic_umax
:
3890 op
= LLVMAtomicRMWBinOpUMax
;
3892 case nir_intrinsic_var_atomic_imin
:
3893 op
= LLVMAtomicRMWBinOpMin
;
3895 case nir_intrinsic_var_atomic_imax
:
3896 op
= LLVMAtomicRMWBinOpMax
;
3898 case nir_intrinsic_var_atomic_and
:
3899 op
= LLVMAtomicRMWBinOpAnd
;
3901 case nir_intrinsic_var_atomic_or
:
3902 op
= LLVMAtomicRMWBinOpOr
;
3904 case nir_intrinsic_var_atomic_xor
:
3905 op
= LLVMAtomicRMWBinOpXor
;
3907 case nir_intrinsic_var_atomic_exchange
:
3908 op
= LLVMAtomicRMWBinOpXchg
;
3914 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3915 LLVMAtomicOrderingSequentiallyConsistent
,
3921 #define INTERP_CENTER 0
3922 #define INTERP_CENTROID 1
3923 #define INTERP_SAMPLE 2
3925 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3926 enum glsl_interp_mode interp
, unsigned location
)
3929 case INTERP_MODE_FLAT
:
3932 case INTERP_MODE_SMOOTH
:
3933 case INTERP_MODE_NONE
:
3934 if (location
== INTERP_CENTER
)
3935 return ctx
->persp_center
;
3936 else if (location
== INTERP_CENTROID
)
3937 return ctx
->persp_centroid
;
3938 else if (location
== INTERP_SAMPLE
)
3939 return ctx
->persp_sample
;
3941 case INTERP_MODE_NOPERSPECTIVE
:
3942 if (location
== INTERP_CENTER
)
3943 return ctx
->linear_center
;
3944 else if (location
== INTERP_CENTROID
)
3945 return ctx
->linear_centroid
;
3946 else if (location
== INTERP_SAMPLE
)
3947 return ctx
->linear_sample
;
3953 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3954 LLVMValueRef sample_id
)
3956 LLVMValueRef result
;
3957 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
3959 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3960 const_array(ctx
->ac
.v2f32
, 64), "");
3962 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3963 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
3968 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3970 LLVMValueRef values
[2];
3972 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3973 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3974 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3977 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3978 const nir_intrinsic_instr
*instr
)
3980 LLVMValueRef result
[4];
3981 LLVMValueRef interp_param
, attr_number
;
3984 LLVMValueRef src_c0
= NULL
;
3985 LLVMValueRef src_c1
= NULL
;
3986 LLVMValueRef src0
= NULL
;
3987 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3988 switch (instr
->intrinsic
) {
3989 case nir_intrinsic_interp_var_at_centroid
:
3990 location
= INTERP_CENTROID
;
3992 case nir_intrinsic_interp_var_at_sample
:
3993 case nir_intrinsic_interp_var_at_offset
:
3994 location
= INTERP_CENTER
;
3995 src0
= get_src(ctx
->nir
, instr
->src
[0]);
4001 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
4002 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_0
, ""));
4003 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_1
, ""));
4004 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
4005 LLVMValueRef sample_position
;
4006 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
4008 /* fetch sample ID */
4009 sample_position
= load_sample_position(ctx
, src0
);
4011 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_0
, "");
4012 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
4013 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_1
, "");
4014 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
4016 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
4017 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
4019 if (location
== INTERP_CENTER
) {
4020 LLVMValueRef ij_out
[2];
4021 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
4024 * take the I then J parameters, and the DDX/Y for it, and
4025 * calculate the IJ inputs for the interpolator.
4026 * temp1 = ddx * offset/sample.x + I;
4027 * interp_param.I = ddy * offset/sample.y + temp1;
4028 * temp1 = ddx * offset/sample.x + J;
4029 * interp_param.J = ddy * offset/sample.y + temp1;
4031 for (unsigned i
= 0; i
< 2; i
++) {
4032 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
4033 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
4034 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
4035 ddxy_out
, ix_ll
, "");
4036 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
4037 ddxy_out
, iy_ll
, "");
4038 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
4039 interp_param
, ix_ll
, "");
4040 LLVMValueRef temp1
, temp2
;
4042 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
4045 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
4046 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
4048 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
4049 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
4051 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
4052 temp2
, ctx
->ac
.i32
, "");
4054 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
4058 for (chan
= 0; chan
< 4; chan
++) {
4059 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
4062 interp_param
= LLVMBuildBitCast(ctx
->builder
,
4063 interp_param
, ctx
->ac
.v2f32
, "");
4064 LLVMValueRef i
= LLVMBuildExtractElement(
4065 ctx
->builder
, interp_param
, ctx
->ac
.i32_0
, "");
4066 LLVMValueRef j
= LLVMBuildExtractElement(
4067 ctx
->builder
, interp_param
, ctx
->ac
.i32_1
, "");
4069 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4070 llvm_chan
, attr_number
,
4071 ctx
->prim_mask
, i
, j
);
4073 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4074 LLVMConstInt(ctx
->ac
.i32
, 2, false),
4075 llvm_chan
, attr_number
,
4079 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
4080 instr
->variables
[0]->var
->data
.location_frac
);
4084 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
4086 LLVMValueRef gs_next_vertex
;
4087 LLVMValueRef can_emit
;
4089 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4091 /* Write vertex attribute values to GSVS ring */
4092 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
4093 ctx
->gs_next_vertex
,
4096 /* If this thread has already emitted the declared maximum number of
4097 * vertices, kill it: excessive vertex emissions are not supposed to
4098 * have any effect, and GS threads have no externally observable
4099 * effects other than emitting vertices.
4101 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
4102 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
4103 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4105 /* loop num outputs */
4107 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4108 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
4113 if (!(ctx
->output_mask
& (1ull << i
)))
4116 if (i
== VARYING_SLOT_CLIP_DIST0
) {
4117 /* pack clip and cull into a single set of slots */
4118 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4122 for (unsigned j
= 0; j
< length
; j
++) {
4123 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
4125 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
4126 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
4127 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
4129 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
4131 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
4133 voffset
, ctx
->gs2vs_offset
, 0,
4139 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4141 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4143 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4147 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
4148 const nir_intrinsic_instr
*instr
)
4150 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4154 load_tess_coord(struct ac_shader_abi
*abi
, LLVMTypeRef type
,
4155 unsigned num_components
)
4157 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4159 LLVMValueRef coord
[4] = {
4166 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4167 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->ac
.f32_1
,
4168 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4170 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, num_components
);
4171 return LLVMBuildBitCast(ctx
->builder
, result
, type
, "");
4174 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4175 nir_intrinsic_instr
*instr
)
4177 LLVMValueRef result
= NULL
;
4179 switch (instr
->intrinsic
) {
4180 case nir_intrinsic_load_work_group_id
: {
4181 LLVMValueRef values
[3];
4183 for (int i
= 0; i
< 3; i
++) {
4184 values
[i
] = ctx
->nctx
->workgroup_ids
[i
] ?
4185 ctx
->nctx
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
4188 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
4191 case nir_intrinsic_load_base_vertex
: {
4192 result
= ctx
->abi
->base_vertex
;
4195 case nir_intrinsic_load_vertex_id_zero_base
: {
4196 result
= ctx
->abi
->vertex_id
;
4199 case nir_intrinsic_load_local_invocation_id
: {
4200 result
= ctx
->nctx
->local_invocation_ids
;
4203 case nir_intrinsic_load_base_instance
:
4204 result
= ctx
->abi
->start_instance
;
4206 case nir_intrinsic_load_draw_id
:
4207 result
= ctx
->abi
->draw_id
;
4209 case nir_intrinsic_load_view_index
:
4210 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4212 case nir_intrinsic_load_invocation_id
:
4213 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4214 result
= unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
4216 result
= ctx
->abi
->gs_invocation_id
;
4218 case nir_intrinsic_load_primitive_id
:
4219 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4220 result
= ctx
->abi
->gs_prim_id
;
4221 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4222 result
= ctx
->abi
->tcs_patch_id
;
4223 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4224 result
= ctx
->abi
->tes_patch_id
;
4226 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4228 case nir_intrinsic_load_sample_id
:
4229 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4231 case nir_intrinsic_load_sample_pos
:
4232 result
= load_sample_pos(ctx
);
4234 case nir_intrinsic_load_sample_mask_in
:
4235 result
= ctx
->abi
->sample_coverage
;
4237 case nir_intrinsic_load_frag_coord
: {
4238 LLVMValueRef values
[4] = {
4239 ctx
->abi
->frag_pos
[0],
4240 ctx
->abi
->frag_pos
[1],
4241 ctx
->abi
->frag_pos
[2],
4242 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4244 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4247 case nir_intrinsic_load_front_face
:
4248 result
= ctx
->abi
->front_face
;
4250 case nir_intrinsic_load_instance_id
:
4251 result
= ctx
->abi
->instance_id
;
4253 case nir_intrinsic_load_num_work_groups
:
4254 result
= ctx
->nctx
->num_work_groups
;
4256 case nir_intrinsic_load_local_invocation_index
:
4257 result
= visit_load_local_invocation_index(ctx
->nctx
);
4259 case nir_intrinsic_load_push_constant
:
4260 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4262 case nir_intrinsic_vulkan_resource_index
:
4263 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4265 case nir_intrinsic_vulkan_resource_reindex
:
4266 result
= visit_vulkan_resource_reindex(ctx
->nctx
, instr
);
4268 case nir_intrinsic_store_ssbo
:
4269 visit_store_ssbo(ctx
, instr
);
4271 case nir_intrinsic_load_ssbo
:
4272 result
= visit_load_buffer(ctx
, instr
);
4274 case nir_intrinsic_ssbo_atomic_add
:
4275 case nir_intrinsic_ssbo_atomic_imin
:
4276 case nir_intrinsic_ssbo_atomic_umin
:
4277 case nir_intrinsic_ssbo_atomic_imax
:
4278 case nir_intrinsic_ssbo_atomic_umax
:
4279 case nir_intrinsic_ssbo_atomic_and
:
4280 case nir_intrinsic_ssbo_atomic_or
:
4281 case nir_intrinsic_ssbo_atomic_xor
:
4282 case nir_intrinsic_ssbo_atomic_exchange
:
4283 case nir_intrinsic_ssbo_atomic_comp_swap
:
4284 result
= visit_atomic_ssbo(ctx
, instr
);
4286 case nir_intrinsic_load_ubo
:
4287 result
= visit_load_ubo_buffer(ctx
, instr
);
4289 case nir_intrinsic_get_buffer_size
:
4290 result
= visit_get_buffer_size(ctx
, instr
);
4292 case nir_intrinsic_load_var
:
4293 result
= visit_load_var(ctx
, instr
);
4295 case nir_intrinsic_store_var
:
4296 visit_store_var(ctx
, instr
);
4298 case nir_intrinsic_image_load
:
4299 result
= visit_image_load(ctx
, instr
);
4301 case nir_intrinsic_image_store
:
4302 visit_image_store(ctx
, instr
);
4304 case nir_intrinsic_image_atomic_add
:
4305 case nir_intrinsic_image_atomic_min
:
4306 case nir_intrinsic_image_atomic_max
:
4307 case nir_intrinsic_image_atomic_and
:
4308 case nir_intrinsic_image_atomic_or
:
4309 case nir_intrinsic_image_atomic_xor
:
4310 case nir_intrinsic_image_atomic_exchange
:
4311 case nir_intrinsic_image_atomic_comp_swap
:
4312 result
= visit_image_atomic(ctx
, instr
);
4314 case nir_intrinsic_image_size
:
4315 result
= visit_image_size(ctx
, instr
);
4317 case nir_intrinsic_discard
:
4318 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4319 LLVMVoidTypeInContext(ctx
->ac
.context
),
4320 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4322 case nir_intrinsic_discard_if
:
4323 emit_discard_if(ctx
, instr
);
4325 case nir_intrinsic_memory_barrier
:
4326 case nir_intrinsic_group_memory_barrier
:
4327 case nir_intrinsic_memory_barrier_atomic_counter
:
4328 case nir_intrinsic_memory_barrier_buffer
:
4329 case nir_intrinsic_memory_barrier_image
:
4330 case nir_intrinsic_memory_barrier_shared
:
4331 emit_membar(ctx
->nctx
, instr
);
4333 case nir_intrinsic_barrier
:
4334 emit_barrier(ctx
->nctx
);
4336 case nir_intrinsic_var_atomic_add
:
4337 case nir_intrinsic_var_atomic_imin
:
4338 case nir_intrinsic_var_atomic_umin
:
4339 case nir_intrinsic_var_atomic_imax
:
4340 case nir_intrinsic_var_atomic_umax
:
4341 case nir_intrinsic_var_atomic_and
:
4342 case nir_intrinsic_var_atomic_or
:
4343 case nir_intrinsic_var_atomic_xor
:
4344 case nir_intrinsic_var_atomic_exchange
:
4345 case nir_intrinsic_var_atomic_comp_swap
:
4346 result
= visit_var_atomic(ctx
->nctx
, instr
);
4348 case nir_intrinsic_interp_var_at_centroid
:
4349 case nir_intrinsic_interp_var_at_sample
:
4350 case nir_intrinsic_interp_var_at_offset
:
4351 result
= visit_interp(ctx
->nctx
, instr
);
4353 case nir_intrinsic_emit_vertex
:
4354 assert(instr
->const_index
[0] == 0);
4355 ctx
->abi
->emit_vertex(ctx
->abi
, 0, ctx
->outputs
);
4357 case nir_intrinsic_end_primitive
:
4358 visit_end_primitive(ctx
->nctx
, instr
);
4360 case nir_intrinsic_load_tess_coord
: {
4361 LLVMTypeRef type
= ctx
->nctx
?
4362 get_def_type(ctx
->nctx
->nir
, &instr
->dest
.ssa
) :
4364 result
= ctx
->abi
->load_tess_coord(ctx
->abi
, type
, instr
->num_components
);
4367 case nir_intrinsic_load_patch_vertices_in
:
4368 result
= LLVMConstInt(ctx
->ac
.i32
, ctx
->nctx
->options
->key
.tcs
.input_vertices
, false);
4371 fprintf(stderr
, "Unknown intrinsic: ");
4372 nir_print_instr(&instr
->instr
, stderr
);
4373 fprintf(stderr
, "\n");
4377 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4381 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4382 LLVMValueRef buffer_ptr
, bool write
)
4384 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4386 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4387 ctx
->shader_info
->fs
.writes_memory
= true;
4389 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4392 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef buffer_ptr
)
4394 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4396 return LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4399 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4400 unsigned descriptor_set
,
4401 unsigned base_index
,
4402 unsigned constant_index
,
4404 enum ac_descriptor_type desc_type
,
4405 bool image
, bool write
)
4407 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4408 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4409 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4410 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4411 unsigned offset
= binding
->offset
;
4412 unsigned stride
= binding
->size
;
4414 LLVMBuilderRef builder
= ctx
->builder
;
4417 assert(base_index
< layout
->binding_count
);
4419 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4420 ctx
->shader_info
->fs
.writes_memory
= true;
4422 switch (desc_type
) {
4424 type
= ctx
->ac
.v8i32
;
4428 type
= ctx
->ac
.v8i32
;
4432 case AC_DESC_SAMPLER
:
4433 type
= ctx
->ac
.v4i32
;
4434 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4439 case AC_DESC_BUFFER
:
4440 type
= ctx
->ac
.v4i32
;
4444 unreachable("invalid desc_type\n");
4447 offset
+= constant_index
* stride
;
4449 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4450 (!index
|| binding
->immutable_samplers_equal
)) {
4451 if (binding
->immutable_samplers_equal
)
4454 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4456 LLVMValueRef constants
[] = {
4457 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4458 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4459 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4460 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4462 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4465 assert(stride
% type_size
== 0);
4468 index
= ctx
->ac
.i32_0
;
4470 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4472 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4473 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4475 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4478 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4479 const nir_deref_var
*deref
,
4480 enum ac_descriptor_type desc_type
,
4481 const nir_tex_instr
*tex_instr
,
4482 bool image
, bool write
)
4484 LLVMValueRef index
= NULL
;
4485 unsigned constant_index
= 0;
4486 unsigned descriptor_set
;
4487 unsigned base_index
;
4490 assert(tex_instr
&& !image
);
4492 base_index
= tex_instr
->sampler_index
;
4494 const nir_deref
*tail
= &deref
->deref
;
4495 while (tail
->child
) {
4496 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4497 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4502 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4504 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4505 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4507 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4508 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4513 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4516 constant_index
+= child
->base_offset
* array_size
;
4518 tail
= &child
->deref
;
4520 descriptor_set
= deref
->var
->data
.descriptor_set
;
4521 base_index
= deref
->var
->data
.binding
;
4524 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4527 constant_index
, index
,
4528 desc_type
, image
, write
);
4531 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4532 struct ac_image_args
*args
,
4533 const nir_tex_instr
*instr
,
4535 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4536 LLVMValueRef
*param
, unsigned count
,
4539 unsigned is_rect
= 0;
4540 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4542 if (op
== nir_texop_lod
)
4544 /* Pad to power of two vector */
4545 while (count
< util_next_power_of_two(count
))
4546 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4549 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4551 args
->addr
= param
[0];
4553 args
->resource
= res_ptr
;
4554 args
->sampler
= samp_ptr
;
4556 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4557 args
->addr
= param
[0];
4561 args
->dmask
= dmask
;
4562 args
->unorm
= is_rect
;
4566 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4569 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4570 * filtering manually. The driver sets img7 to a mask clearing
4571 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4572 * s_and_b32 samp0, samp0, img7
4575 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4577 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4578 LLVMValueRef res
, LLVMValueRef samp
)
4580 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4581 LLVMValueRef img7
, samp0
;
4583 if (ctx
->ac
.chip_class
>= VI
)
4586 img7
= LLVMBuildExtractElement(builder
, res
,
4587 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4588 samp0
= LLVMBuildExtractElement(builder
, samp
,
4589 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4590 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4591 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4592 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4595 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4596 nir_tex_instr
*instr
,
4597 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4598 LLVMValueRef
*fmask_ptr
)
4600 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4601 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4603 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4606 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4608 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4609 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4610 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4612 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4613 instr
->op
== nir_texop_samples_identical
))
4614 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4617 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4620 coord
= ac_to_float(ctx
, coord
);
4621 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4622 coord
= ac_to_integer(ctx
, coord
);
4626 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4628 LLVMValueRef result
= NULL
;
4629 struct ac_image_args args
= { 0 };
4630 unsigned dmask
= 0xf;
4631 LLVMValueRef address
[16];
4632 LLVMValueRef coords
[5];
4633 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4634 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4635 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4636 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4637 LLVMValueRef derivs
[6];
4638 unsigned chan
, count
= 0;
4639 unsigned const_src
= 0, num_deriv_comp
= 0;
4640 bool lod_is_zero
= false;
4642 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4644 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4645 switch (instr
->src
[i
].src_type
) {
4646 case nir_tex_src_coord
:
4647 coord
= get_src(ctx
, instr
->src
[i
].src
);
4649 case nir_tex_src_projector
:
4651 case nir_tex_src_comparator
:
4652 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4654 case nir_tex_src_offset
:
4655 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4658 case nir_tex_src_bias
:
4659 bias
= get_src(ctx
, instr
->src
[i
].src
);
4661 case nir_tex_src_lod
: {
4662 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4664 if (val
&& val
->i32
[0] == 0)
4666 lod
= get_src(ctx
, instr
->src
[i
].src
);
4669 case nir_tex_src_ms_index
:
4670 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4672 case nir_tex_src_ms_mcs
:
4674 case nir_tex_src_ddx
:
4675 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4676 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4678 case nir_tex_src_ddy
:
4679 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4681 case nir_tex_src_texture_offset
:
4682 case nir_tex_src_sampler_offset
:
4683 case nir_tex_src_plane
:
4689 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4690 result
= get_buffer_size(ctx
, res_ptr
, true);
4694 if (instr
->op
== nir_texop_texture_samples
) {
4695 LLVMValueRef res
, samples
, is_msaa
;
4696 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4697 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4698 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4699 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4700 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4701 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4702 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4703 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4704 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4706 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4707 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4708 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4709 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4710 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4712 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4719 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4720 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4722 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4723 LLVMValueRef offset
[3], pack
;
4724 for (chan
= 0; chan
< 3; ++chan
)
4725 offset
[chan
] = ctx
->ac
.i32_0
;
4728 for (chan
= 0; chan
< ac_get_llvm_num_components(offsets
); chan
++) {
4729 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4730 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4731 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4733 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4734 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4736 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4737 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4738 address
[count
++] = pack
;
4741 /* pack LOD bias value */
4742 if (instr
->op
== nir_texop_txb
&& bias
) {
4743 address
[count
++] = bias
;
4746 /* Pack depth comparison value */
4747 if (instr
->is_shadow
&& comparator
) {
4748 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4749 ac_llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4751 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
4752 * so the depth comparison value isn't clamped for Z16 and
4753 * Z24 anymore. Do it manually here.
4755 * It's unnecessary if the original texture format was
4756 * Z32_FLOAT, but we don't know that here.
4758 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
4759 z
= ac_build_clamp(&ctx
->ac
, z
);
4761 address
[count
++] = z
;
4764 /* pack derivatives */
4766 int num_src_deriv_channels
, num_dest_deriv_channels
;
4767 switch (instr
->sampler_dim
) {
4768 case GLSL_SAMPLER_DIM_3D
:
4769 case GLSL_SAMPLER_DIM_CUBE
:
4771 num_src_deriv_channels
= 3;
4772 num_dest_deriv_channels
= 3;
4774 case GLSL_SAMPLER_DIM_2D
:
4776 num_src_deriv_channels
= 2;
4777 num_dest_deriv_channels
= 2;
4780 case GLSL_SAMPLER_DIM_1D
:
4781 num_src_deriv_channels
= 1;
4782 if (ctx
->ac
.chip_class
>= GFX9
) {
4783 num_dest_deriv_channels
= 2;
4786 num_dest_deriv_channels
= 1;
4792 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4793 derivs
[i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4794 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4796 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4797 derivs
[i
] = ctx
->ac
.f32_0
;
4798 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4802 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4803 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4804 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4805 if (instr
->coord_components
== 3)
4806 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4807 ac_prepare_cube_coords(&ctx
->ac
,
4808 instr
->op
== nir_texop_txd
, instr
->is_array
,
4809 instr
->op
== nir_texop_lod
, coords
, derivs
);
4815 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4816 address
[count
++] = derivs
[i
];
4819 /* Pack texture coordinates */
4821 address
[count
++] = coords
[0];
4822 if (instr
->coord_components
> 1) {
4823 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4824 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4826 address
[count
++] = coords
[1];
4828 if (instr
->coord_components
> 2) {
4829 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4830 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4831 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4832 instr
->op
!= nir_texop_txf
) {
4833 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4835 address
[count
++] = coords
[2];
4838 if (ctx
->ac
.chip_class
>= GFX9
) {
4839 LLVMValueRef filler
;
4840 if (instr
->op
== nir_texop_txf
)
4841 filler
= ctx
->ac
.i32_0
;
4843 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4845 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4846 /* No nir_texop_lod, because it does not take a slice
4847 * even with array textures. */
4848 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
4849 address
[count
] = address
[count
- 1];
4850 address
[count
- 1] = filler
;
4853 address
[count
++] = filler
;
4859 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4860 instr
->op
== nir_texop_txf
)) {
4861 address
[count
++] = lod
;
4862 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4863 address
[count
++] = sample_index
;
4864 } else if(instr
->op
== nir_texop_txs
) {
4867 address
[count
++] = lod
;
4869 address
[count
++] = ctx
->ac
.i32_0
;
4872 for (chan
= 0; chan
< count
; chan
++) {
4873 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4874 address
[chan
], ctx
->ac
.i32
, "");
4877 if (instr
->op
== nir_texop_samples_identical
) {
4878 LLVMValueRef txf_address
[4];
4879 struct ac_image_args txf_args
= { 0 };
4880 unsigned txf_count
= count
;
4881 memcpy(txf_address
, address
, sizeof(txf_address
));
4883 if (!instr
->is_array
)
4884 txf_address
[2] = ctx
->ac
.i32_0
;
4885 txf_address
[3] = ctx
->ac
.i32_0
;
4887 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4889 txf_address
, txf_count
, 0xf);
4891 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4893 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4894 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4898 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4899 instr
->op
!= nir_texop_txs
) {
4900 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4901 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4904 instr
->is_array
? address
[2] : NULL
,
4905 address
[sample_chan
],
4909 if (offsets
&& instr
->op
== nir_texop_txf
) {
4910 nir_const_value
*const_offset
=
4911 nir_src_as_const_value(instr
->src
[const_src
].src
);
4912 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4913 assert(const_offset
);
4914 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4915 if (num_offsets
> 2)
4916 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4917 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4918 if (num_offsets
> 1)
4919 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4920 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4921 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4922 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4926 /* TODO TG4 support */
4927 if (instr
->op
== nir_texop_tg4
) {
4928 if (instr
->is_shadow
)
4931 dmask
= 1 << instr
->component
;
4933 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4934 res_ptr
, samp_ptr
, address
, count
, dmask
);
4936 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4938 if (instr
->op
== nir_texop_query_levels
)
4939 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4940 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4941 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4942 instr
->op
!= nir_texop_tg4
)
4943 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4944 else if (instr
->op
== nir_texop_txs
&&
4945 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4947 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4948 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4949 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4950 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4951 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4952 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4953 instr
->op
== nir_texop_txs
&&
4954 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4956 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4957 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4958 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4960 } else if (instr
->dest
.ssa
.num_components
!= 4)
4961 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4965 assert(instr
->dest
.is_ssa
);
4966 result
= ac_to_integer(&ctx
->ac
, result
);
4967 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4972 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4974 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4975 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4977 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4978 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4981 static void visit_post_phi(struct ac_nir_context
*ctx
,
4982 nir_phi_instr
*instr
,
4983 LLVMValueRef llvm_phi
)
4985 nir_foreach_phi_src(src
, instr
) {
4986 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4987 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4989 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4993 static void phi_post_pass(struct ac_nir_context
*ctx
)
4995 struct hash_entry
*entry
;
4996 hash_table_foreach(ctx
->phis
, entry
) {
4997 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4998 (LLVMValueRef
)entry
->data
);
5003 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
5004 const nir_ssa_undef_instr
*instr
)
5006 unsigned num_components
= instr
->def
.num_components
;
5009 if (num_components
== 1)
5010 undef
= LLVMGetUndef(ctx
->ac
.i32
);
5012 undef
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, num_components
));
5014 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
5017 static void visit_jump(struct ac_nir_context
*ctx
,
5018 const nir_jump_instr
*instr
)
5020 switch (instr
->type
) {
5021 case nir_jump_break
:
5022 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
5023 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5025 case nir_jump_continue
:
5026 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5027 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5030 fprintf(stderr
, "Unknown NIR jump instr: ");
5031 nir_print_instr(&instr
->instr
, stderr
);
5032 fprintf(stderr
, "\n");
5037 static void visit_cf_list(struct ac_nir_context
*ctx
,
5038 struct exec_list
*list
);
5040 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5042 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
5043 nir_foreach_instr(instr
, block
)
5045 switch (instr
->type
) {
5046 case nir_instr_type_alu
:
5047 visit_alu(ctx
, nir_instr_as_alu(instr
));
5049 case nir_instr_type_load_const
:
5050 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5052 case nir_instr_type_intrinsic
:
5053 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5055 case nir_instr_type_tex
:
5056 visit_tex(ctx
, nir_instr_as_tex(instr
));
5058 case nir_instr_type_phi
:
5059 visit_phi(ctx
, nir_instr_as_phi(instr
));
5061 case nir_instr_type_ssa_undef
:
5062 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5064 case nir_instr_type_jump
:
5065 visit_jump(ctx
, nir_instr_as_jump(instr
));
5068 fprintf(stderr
, "Unknown NIR instr type: ");
5069 nir_print_instr(instr
, stderr
);
5070 fprintf(stderr
, "\n");
5075 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
5078 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5080 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5082 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5083 LLVMBasicBlockRef merge_block
=
5084 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5085 LLVMBasicBlockRef if_block
=
5086 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5087 LLVMBasicBlockRef else_block
= merge_block
;
5088 if (!exec_list_is_empty(&if_stmt
->else_list
))
5089 else_block
= LLVMAppendBasicBlockInContext(
5090 ctx
->ac
.context
, fn
, "");
5092 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
5094 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
5096 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
5097 visit_cf_list(ctx
, &if_stmt
->then_list
);
5098 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5099 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5101 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5102 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
5103 visit_cf_list(ctx
, &if_stmt
->else_list
);
5104 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5105 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5108 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
5111 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5113 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5114 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
5115 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
5117 ctx
->continue_block
=
5118 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5120 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5122 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5123 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
5124 visit_cf_list(ctx
, &loop
->body
);
5126 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5127 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5128 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
5130 ctx
->continue_block
= continue_parent
;
5131 ctx
->break_block
= break_parent
;
5134 static void visit_cf_list(struct ac_nir_context
*ctx
,
5135 struct exec_list
*list
)
5137 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5139 switch (node
->type
) {
5140 case nir_cf_node_block
:
5141 visit_block(ctx
, nir_cf_node_as_block(node
));
5144 case nir_cf_node_if
:
5145 visit_if(ctx
, nir_cf_node_as_if(node
));
5148 case nir_cf_node_loop
:
5149 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5159 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
5160 struct nir_variable
*variable
)
5162 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5163 LLVMValueRef t_offset
;
5164 LLVMValueRef t_list
;
5166 LLVMValueRef buffer_index
;
5167 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5168 int idx
= variable
->data
.location
;
5169 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5171 variable
->data
.driver_location
= idx
* 4;
5173 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
5174 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5175 ctx
->abi
.start_instance
, "");
5176 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
5177 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5179 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5180 ctx
->abi
.base_vertex
, "");
5182 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5183 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5185 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5187 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5192 for (unsigned chan
= 0; chan
< 4; chan
++) {
5193 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5194 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5195 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5196 input
, llvm_chan
, ""));
5201 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5203 LLVMValueRef interp_param
,
5204 LLVMValueRef prim_mask
,
5205 LLVMValueRef result
[4])
5207 LLVMValueRef attr_number
;
5210 bool interp
= interp_param
!= NULL
;
5212 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5214 /* fs.constant returns the param from the middle vertex, so it's not
5215 * really useful for flat shading. It's meant to be used for custom
5216 * interpolation (but the intrinsic can't fetch from the other two
5219 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5220 * to do the right thing. The only reason we use fs.constant is that
5221 * fs.interp cannot be used on integers, because they can be equal
5225 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5228 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5230 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5234 for (chan
= 0; chan
< 4; chan
++) {
5235 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5238 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5243 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5244 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5253 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5254 struct nir_variable
*variable
)
5256 int idx
= variable
->data
.location
;
5257 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5258 LLVMValueRef interp
;
5260 variable
->data
.driver_location
= idx
* 4;
5261 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5263 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5264 unsigned interp_type
;
5265 if (variable
->data
.sample
) {
5266 interp_type
= INTERP_SAMPLE
;
5267 ctx
->shader_info
->info
.ps
.force_persample
= true;
5268 } else if (variable
->data
.centroid
)
5269 interp_type
= INTERP_CENTROID
;
5271 interp_type
= INTERP_CENTER
;
5273 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5277 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5278 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5283 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5284 struct nir_shader
*nir
) {
5285 nir_foreach_variable(variable
, &nir
->inputs
)
5286 handle_vs_input_decl(ctx
, variable
);
5290 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5291 struct nir_shader
*nir
)
5293 if (!ctx
->options
->key
.fs
.multisample
)
5296 bool uses_center
= false;
5297 bool uses_centroid
= false;
5298 nir_foreach_variable(variable
, &nir
->inputs
) {
5299 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5300 variable
->data
.sample
)
5303 if (variable
->data
.centroid
)
5304 uses_centroid
= true;
5309 if (uses_center
&& uses_centroid
) {
5310 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5311 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5312 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5317 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5318 struct nir_shader
*nir
)
5320 prepare_interp_optimize(ctx
, nir
);
5322 nir_foreach_variable(variable
, &nir
->inputs
)
5323 handle_fs_input_decl(ctx
, variable
);
5327 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5328 ctx
->shader_info
->info
.needs_multiview_view_index
)
5329 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5331 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5332 LLVMValueRef interp_param
;
5333 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5335 if (!(ctx
->input_mask
& (1ull << i
)))
5338 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5339 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5340 interp_param
= *inputs
;
5341 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5345 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5347 } else if (i
== VARYING_SLOT_POS
) {
5348 for(int i
= 0; i
< 3; ++i
)
5349 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5351 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5352 ctx
->abi
.frag_pos
[3]);
5355 ctx
->shader_info
->fs
.num_interp
= index
;
5356 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5357 ctx
->shader_info
->fs
.has_pcoord
= true;
5358 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5359 ctx
->shader_info
->fs
.prim_id_input
= true;
5360 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5361 ctx
->shader_info
->fs
.layer_input
= true;
5362 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5364 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5365 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5369 ac_build_alloca(struct ac_llvm_context
*ac
,
5373 LLVMBuilderRef builder
= ac
->builder
;
5374 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5375 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5376 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5377 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5378 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5382 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5384 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5387 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5388 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5390 LLVMDisposeBuilder(first_builder
);
5395 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5399 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5400 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5405 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5406 struct nir_variable
*variable
,
5407 struct nir_shader
*shader
,
5408 gl_shader_stage stage
)
5410 int idx
= variable
->data
.location
+ variable
->data
.index
;
5411 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5412 uint64_t mask_attribs
;
5414 variable
->data
.driver_location
= idx
* 4;
5416 /* tess ctrl has it's own load/store paths for outputs */
5417 if (stage
== MESA_SHADER_TESS_CTRL
)
5420 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5421 if (stage
== MESA_SHADER_VERTEX
||
5422 stage
== MESA_SHADER_TESS_EVAL
||
5423 stage
== MESA_SHADER_GEOMETRY
) {
5424 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5425 int length
= shader
->info
.clip_distance_array_size
+
5426 shader
->info
.cull_distance_array_size
;
5427 if (stage
== MESA_SHADER_VERTEX
) {
5428 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5429 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5431 if (stage
== MESA_SHADER_TESS_EVAL
) {
5432 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5433 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5440 mask_attribs
= 1ull << idx
;
5444 ctx
->output_mask
|= mask_attribs
;
5448 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5449 struct nir_shader
*nir
,
5450 struct nir_variable
*variable
)
5452 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5453 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5455 /* tess ctrl has it's own load/store paths for outputs */
5456 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5459 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5460 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5461 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5462 int idx
= variable
->data
.location
+ variable
->data
.index
;
5463 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5464 int length
= nir
->info
.clip_distance_array_size
+
5465 nir
->info
.cull_distance_array_size
;
5474 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5475 for (unsigned chan
= 0; chan
< 4; chan
++) {
5476 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5477 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5483 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5484 enum glsl_base_type type
)
5488 case GLSL_TYPE_UINT
:
5489 case GLSL_TYPE_BOOL
:
5490 case GLSL_TYPE_SUBROUTINE
:
5492 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5494 case GLSL_TYPE_INT64
:
5495 case GLSL_TYPE_UINT64
:
5497 case GLSL_TYPE_DOUBLE
:
5500 unreachable("unknown GLSL type");
5505 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5506 const struct glsl_type
*type
)
5508 if (glsl_type_is_scalar(type
)) {
5509 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5512 if (glsl_type_is_vector(type
)) {
5513 return LLVMVectorType(
5514 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5515 glsl_get_vector_elements(type
));
5518 if (glsl_type_is_matrix(type
)) {
5519 return LLVMArrayType(
5520 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5521 glsl_get_matrix_columns(type
));
5524 if (glsl_type_is_array(type
)) {
5525 return LLVMArrayType(
5526 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5527 glsl_get_length(type
));
5530 assert(glsl_type_is_struct(type
));
5532 LLVMTypeRef member_types
[glsl_get_length(type
)];
5534 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5536 glsl_to_llvm_type(ctx
,
5537 glsl_get_struct_field(type
, i
));
5540 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5541 glsl_get_length(type
), false);
5545 setup_locals(struct ac_nir_context
*ctx
,
5546 struct nir_function
*func
)
5549 ctx
->num_locals
= 0;
5550 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5551 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5552 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5553 ctx
->num_locals
+= attrib_count
;
5555 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5559 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5560 for (j
= 0; j
< 4; j
++) {
5561 ctx
->locals
[i
* 4 + j
] =
5562 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5568 setup_shared(struct ac_nir_context
*ctx
,
5569 struct nir_shader
*nir
)
5571 nir_foreach_variable(variable
, &nir
->shared
) {
5572 LLVMValueRef shared
=
5573 LLVMAddGlobalInAddressSpace(
5574 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5575 variable
->name
? variable
->name
: "",
5577 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5582 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5584 v
= ac_to_float(ctx
, v
);
5585 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5586 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5590 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5591 LLVMValueRef src0
, LLVMValueRef src1
)
5593 LLVMValueRef const16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
5594 LLVMValueRef comp
[2];
5596 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5597 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5598 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5599 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5602 /* Initialize arguments for the shader export intrinsic */
5604 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5605 LLVMValueRef
*values
,
5607 struct ac_export_args
*args
)
5609 /* Default is 0xf. Adjusted below depending on the format. */
5610 args
->enabled_channels
= 0xf;
5612 /* Specify whether the EXEC mask represents the valid mask */
5613 args
->valid_mask
= 0;
5615 /* Specify whether this is the last export */
5618 /* Specify the target we are exporting */
5619 args
->target
= target
;
5621 args
->compr
= false;
5622 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
5623 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
5624 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
5625 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5630 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5631 LLVMValueRef val
[4];
5632 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5633 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5634 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5635 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5637 switch(col_format
) {
5638 case V_028714_SPI_SHADER_ZERO
:
5639 args
->enabled_channels
= 0; /* writemask */
5640 args
->target
= V_008DFC_SQ_EXP_NULL
;
5643 case V_028714_SPI_SHADER_32_R
:
5644 args
->enabled_channels
= 1;
5645 args
->out
[0] = values
[0];
5648 case V_028714_SPI_SHADER_32_GR
:
5649 args
->enabled_channels
= 0x3;
5650 args
->out
[0] = values
[0];
5651 args
->out
[1] = values
[1];
5654 case V_028714_SPI_SHADER_32_AR
:
5655 args
->enabled_channels
= 0x9;
5656 args
->out
[0] = values
[0];
5657 args
->out
[3] = values
[3];
5660 case V_028714_SPI_SHADER_FP16_ABGR
:
5663 for (unsigned chan
= 0; chan
< 2; chan
++) {
5664 LLVMValueRef pack_args
[2] = {
5666 values
[2 * chan
+ 1]
5668 LLVMValueRef packed
;
5670 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5671 args
->out
[chan
] = packed
;
5675 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5676 for (unsigned chan
= 0; chan
< 4; chan
++) {
5677 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5678 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5679 LLVMConstReal(ctx
->ac
.f32
, 65535), "");
5680 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5681 LLVMConstReal(ctx
->ac
.f32
, 0.5), "");
5682 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5687 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5688 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5691 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5692 for (unsigned chan
= 0; chan
< 4; chan
++) {
5693 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5694 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5695 LLVMConstReal(ctx
->ac
.f32
, 32767), "");
5697 /* If positive, add 0.5, else add -0.5. */
5698 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5699 LLVMBuildSelect(ctx
->builder
,
5700 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5701 val
[chan
], ctx
->ac
.f32_0
, ""),
5702 LLVMConstReal(ctx
->ac
.f32
, 0.5),
5703 LLVMConstReal(ctx
->ac
.f32
, -0.5), ""), "");
5704 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->ac
.i32
, "");
5708 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5709 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5712 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5713 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5714 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5715 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->ac
.i32
, 3, 0);
5717 for (unsigned chan
= 0; chan
< 4; chan
++) {
5718 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5719 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5723 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5724 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5728 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5729 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5730 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5731 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5732 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5733 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->ac
.i32_1
;
5734 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->ac
.i32
, -2, 0);
5737 for (unsigned chan
= 0; chan
< 4; chan
++) {
5738 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5739 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5740 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5744 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5745 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5750 case V_028714_SPI_SHADER_32_ABGR
:
5751 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5755 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5757 for (unsigned i
= 0; i
< 4; ++i
)
5758 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5762 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5763 bool export_prim_id
,
5764 struct ac_vs_output_info
*outinfo
)
5766 uint32_t param_count
= 0;
5768 unsigned pos_idx
, num_pos_exports
= 0;
5769 struct ac_export_args args
, pos_args
[4] = {};
5770 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5773 if (ctx
->options
->key
.has_multiview_view_index
) {
5774 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5776 for(unsigned i
= 0; i
< 4; ++i
)
5777 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5778 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5781 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5782 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5785 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5786 sizeof(outinfo
->vs_output_param_offset
));
5788 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5789 LLVMValueRef slots
[8];
5792 if (outinfo
->cull_dist_mask
)
5793 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5795 i
= VARYING_SLOT_CLIP_DIST0
;
5796 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5797 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5798 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5800 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5801 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
5803 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5804 target
= V_008DFC_SQ_EXP_POS
+ 3;
5805 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5806 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5807 &args
, sizeof(args
));
5810 target
= V_008DFC_SQ_EXP_POS
+ 2;
5811 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5812 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5813 &args
, sizeof(args
));
5817 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
5818 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5819 for (unsigned j
= 0; j
< 4; j
++)
5820 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5821 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5823 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5825 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5826 outinfo
->writes_pointsize
= true;
5827 psize_value
= LLVMBuildLoad(ctx
->builder
,
5828 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5831 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5832 outinfo
->writes_layer
= true;
5833 layer_value
= LLVMBuildLoad(ctx
->builder
,
5834 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5837 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5838 outinfo
->writes_viewport_index
= true;
5839 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5840 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5843 if (outinfo
->writes_pointsize
||
5844 outinfo
->writes_layer
||
5845 outinfo
->writes_viewport_index
) {
5846 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5847 (outinfo
->writes_layer
== true ? 4 : 0));
5848 pos_args
[1].valid_mask
= 0;
5849 pos_args
[1].done
= 0;
5850 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5851 pos_args
[1].compr
= 0;
5852 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
5853 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
5854 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
5855 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
5857 if (outinfo
->writes_pointsize
== true)
5858 pos_args
[1].out
[0] = psize_value
;
5859 if (outinfo
->writes_layer
== true)
5860 pos_args
[1].out
[2] = layer_value
;
5861 if (outinfo
->writes_viewport_index
== true) {
5862 if (ctx
->options
->chip_class
>= GFX9
) {
5863 /* GFX9 has the layer in out.z[10:0] and the viewport
5864 * index in out.z[19:16].
5866 LLVMValueRef v
= viewport_index_value
;
5867 v
= ac_to_integer(&ctx
->ac
, v
);
5868 v
= LLVMBuildShl(ctx
->builder
, v
,
5869 LLVMConstInt(ctx
->ac
.i32
, 16, false),
5871 v
= LLVMBuildOr(ctx
->builder
, v
,
5872 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5874 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5875 pos_args
[1].enabled_channels
|= 1 << 2;
5877 pos_args
[1].out
[3] = viewport_index_value
;
5878 pos_args
[1].enabled_channels
|= 1 << 3;
5882 for (i
= 0; i
< 4; i
++) {
5883 if (pos_args
[i
].out
[0])
5888 for (i
= 0; i
< 4; i
++) {
5889 if (!pos_args
[i
].out
[0])
5892 /* Specify the target we are exporting */
5893 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5894 if (pos_idx
== num_pos_exports
)
5895 pos_args
[i
].done
= 1;
5896 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5899 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5900 LLVMValueRef values
[4];
5901 if (!(ctx
->output_mask
& (1ull << i
)))
5904 for (unsigned j
= 0; j
< 4; j
++)
5905 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5906 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5908 if (i
== VARYING_SLOT_LAYER
) {
5909 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5910 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5912 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5913 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5914 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5916 } else if (i
>= VARYING_SLOT_VAR0
) {
5917 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5918 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5919 outinfo
->vs_output_param_offset
[i
] = param_count
;
5924 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5926 if (target
>= V_008DFC_SQ_EXP_POS
&&
5927 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5928 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5929 &args
, sizeof(args
));
5931 ac_build_export(&ctx
->ac
, &args
);
5935 if (export_prim_id
) {
5936 LLVMValueRef values
[4];
5937 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5938 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5941 values
[0] = ctx
->vs_prim_id
;
5942 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5943 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5944 for (unsigned j
= 1; j
< 4; j
++)
5945 values
[j
] = ctx
->ac
.f32_0
;
5946 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5947 ac_build_export(&ctx
->ac
, &args
);
5948 outinfo
->export_prim_id
= true;
5951 outinfo
->pos_exports
= num_pos_exports
;
5952 outinfo
->param_exports
= param_count
;
5956 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5957 struct ac_es_output_info
*outinfo
)
5960 uint64_t max_output_written
= 0;
5961 LLVMValueRef lds_base
= NULL
;
5963 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5967 if (!(ctx
->output_mask
& (1ull << i
)))
5970 if (i
== VARYING_SLOT_CLIP_DIST0
)
5971 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5973 param_index
= shader_io_get_unique_index(i
);
5975 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5978 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5980 if (ctx
->ac
.chip_class
>= GFX9
) {
5981 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
5982 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
5983 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
5984 LLVMConstInt(ctx
->ac
.i32
, 24, false),
5985 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
5986 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
5987 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
5988 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
5989 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
5990 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
5993 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5994 LLVMValueRef dw_addr
;
5995 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
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
);
6008 dw_addr
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6009 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
6012 for (j
= 0; j
< length
; j
++) {
6013 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
6014 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
6016 if (ctx
->ac
.chip_class
>= GFX9
) {
6017 ac_lds_store(&ctx
->ac
, dw_addr
,
6018 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6019 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6021 ac_build_buffer_store_dword(&ctx
->ac
,
6024 NULL
, ctx
->es2gs_offset
,
6025 (4 * param_index
+ j
) * 4,
6033 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
6035 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
6036 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
6037 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
6038 vertex_dw_stride
, "");
6040 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6041 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
6044 if (!(ctx
->output_mask
& (1ull << i
)))
6047 if (i
== VARYING_SLOT_CLIP_DIST0
)
6048 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6049 int param
= shader_io_get_unique_index(i
);
6050 mark_tess_output(ctx
, false, param
);
6052 mark_tess_output(ctx
, false, param
+ 1);
6053 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
6054 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
6056 for (unsigned j
= 0; j
< length
; j
++) {
6057 ac_lds_store(&ctx
->ac
, dw_addr
,
6058 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6059 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6064 struct ac_build_if_state
6066 struct nir_to_llvm_context
*ctx
;
6067 LLVMValueRef condition
;
6068 LLVMBasicBlockRef entry_block
;
6069 LLVMBasicBlockRef true_block
;
6070 LLVMBasicBlockRef false_block
;
6071 LLVMBasicBlockRef merge_block
;
6074 static LLVMBasicBlockRef
6075 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
6077 LLVMBasicBlockRef current_block
;
6078 LLVMBasicBlockRef next_block
;
6079 LLVMBasicBlockRef new_block
;
6081 /* get current basic block */
6082 current_block
= LLVMGetInsertBlock(ctx
->builder
);
6084 /* chqeck if there's another block after this one */
6085 next_block
= LLVMGetNextBasicBlock(current_block
);
6087 /* insert the new block before the next block */
6088 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
6091 /* append new block after current block */
6092 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
6093 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
6099 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
6100 struct nir_to_llvm_context
*ctx
,
6101 LLVMValueRef condition
)
6103 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
6105 memset(ifthen
, 0, sizeof *ifthen
);
6107 ifthen
->condition
= condition
;
6108 ifthen
->entry_block
= block
;
6110 /* create endif/merge basic block for the phi functions */
6111 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
6113 /* create/insert true_block before merge_block */
6114 ifthen
->true_block
=
6115 LLVMInsertBasicBlockInContext(ctx
->context
,
6116 ifthen
->merge_block
,
6119 /* successive code goes into the true block */
6120 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
6124 * End a conditional.
6127 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
6129 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
6131 /* Insert branch to the merge block from current block */
6132 LLVMBuildBr(builder
, ifthen
->merge_block
);
6135 * Now patch in the various branch instructions.
6138 /* Insert the conditional branch instruction at the end of entry_block */
6139 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
6140 if (ifthen
->false_block
) {
6141 /* we have an else clause */
6142 LLVMBuildCondBr(builder
, ifthen
->condition
,
6143 ifthen
->true_block
, ifthen
->false_block
);
6146 /* no else clause */
6147 LLVMBuildCondBr(builder
, ifthen
->condition
,
6148 ifthen
->true_block
, ifthen
->merge_block
);
6151 /* Resume building code at end of the ifthen->merge_block */
6152 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
6156 write_tess_factors(struct nir_to_llvm_context
*ctx
)
6158 unsigned stride
, outer_comps
, inner_comps
;
6159 struct ac_build_if_state if_ctx
, inner_if_ctx
;
6160 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 8, 5);
6161 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
6162 unsigned tess_inner_index
, tess_outer_index
;
6163 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6164 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6168 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6188 ac_nir_build_if(&if_ctx
, ctx
,
6189 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6190 invocation_id
, ctx
->ac
.i32_0
, ""));
6192 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6193 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6195 mark_tess_output(ctx
, true, tess_inner_index
);
6196 mark_tess_output(ctx
, true, tess_outer_index
);
6197 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6198 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6199 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6200 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6201 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6203 for (i
= 0; i
< 4; i
++) {
6204 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6205 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6209 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6210 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6211 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6213 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6215 for (i
= 0; i
< outer_comps
; i
++) {
6217 ac_lds_load(&ctx
->ac
, lds_outer
);
6218 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6221 for (i
= 0; i
< inner_comps
; i
++) {
6222 inner
[i
] = out
[outer_comps
+i
] =
6223 ac_lds_load(&ctx
->ac
, lds_inner
);
6224 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6229 /* Convert the outputs to vectors for stores. */
6230 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6234 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6237 buffer
= ctx
->hs_ring_tess_factor
;
6238 tf_base
= ctx
->tess_factor_offset
;
6239 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6240 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6241 unsigned tf_offset
= 0;
6243 if (ctx
->options
->chip_class
<= VI
) {
6244 ac_nir_build_if(&inner_if_ctx
, ctx
,
6245 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6246 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6248 /* Store the dynamic HS control word. */
6249 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6250 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6251 1, ctx
->ac
.i32_0
, tf_base
,
6252 0, 1, 0, true, false);
6255 ac_nir_build_endif(&inner_if_ctx
);
6258 /* Store the tessellation factors. */
6259 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6260 MIN2(stride
, 4), byteoffset
, tf_base
,
6261 tf_offset
, 1, 0, true, false);
6263 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6264 stride
- 4, byteoffset
, tf_base
,
6265 16 + tf_offset
, 1, 0, true, false);
6267 //store to offchip for TES to read - only if TES reads them
6268 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6269 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6270 LLVMValueRef tf_inner_offset
;
6271 unsigned param_outer
, param_inner
;
6273 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6274 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6275 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6277 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6278 util_next_power_of_two(outer_comps
));
6280 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6281 outer_comps
, tf_outer_offset
,
6282 ctx
->oc_lds
, 0, 1, 0, true, false);
6284 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6285 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6286 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6288 inner_vec
= inner_comps
== 1 ? inner
[0] :
6289 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6290 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6291 inner_comps
, tf_inner_offset
,
6292 ctx
->oc_lds
, 0, 1, 0, true, false);
6295 ac_nir_build_endif(&if_ctx
);
6299 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6301 write_tess_factors(ctx
);
6305 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6306 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6307 struct ac_export_args
*args
)
6310 si_llvm_init_export_args(ctx
, color
, param
,
6314 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6315 args
->done
= 1; /* DONE bit */
6316 } else if (!args
->enabled_channels
)
6317 return false; /* unnecessary NULL export */
6323 radv_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6324 LLVMValueRef depth
, LLVMValueRef stencil
,
6325 LLVMValueRef samplemask
)
6327 struct ac_export_args args
;
6329 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
6331 ac_build_export(&ctx
->ac
, &args
);
6335 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6338 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6339 struct ac_export_args color_args
[8];
6341 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6342 LLVMValueRef values
[4];
6344 if (!(ctx
->output_mask
& (1ull << i
)))
6347 if (i
== FRAG_RESULT_DEPTH
) {
6348 ctx
->shader_info
->fs
.writes_z
= true;
6349 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6350 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6351 } else if (i
== FRAG_RESULT_STENCIL
) {
6352 ctx
->shader_info
->fs
.writes_stencil
= true;
6353 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6354 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6355 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6356 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6357 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6358 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6361 for (unsigned j
= 0; j
< 4; j
++)
6362 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6363 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6365 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6366 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6368 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6374 for (unsigned i
= 0; i
< index
; i
++)
6375 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6376 if (depth
|| stencil
|| samplemask
)
6377 radv_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6379 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6380 ac_build_export(&ctx
->ac
, &color_args
[0]);
6383 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
6387 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6389 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6393 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6394 LLVMValueRef
*addrs
)
6396 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6398 switch (ctx
->stage
) {
6399 case MESA_SHADER_VERTEX
:
6400 if (ctx
->options
->key
.vs
.as_ls
)
6401 handle_ls_outputs_post(ctx
);
6402 else if (ctx
->options
->key
.vs
.as_es
)
6403 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6405 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6406 &ctx
->shader_info
->vs
.outinfo
);
6408 case MESA_SHADER_FRAGMENT
:
6409 handle_fs_outputs_post(ctx
);
6411 case MESA_SHADER_GEOMETRY
:
6412 emit_gs_epilogue(ctx
);
6414 case MESA_SHADER_TESS_CTRL
:
6415 handle_tcs_outputs_post(ctx
);
6417 case MESA_SHADER_TESS_EVAL
:
6418 if (ctx
->options
->key
.tes
.as_es
)
6419 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6421 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6422 &ctx
->shader_info
->tes
.outinfo
);
6429 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6431 LLVMPassManagerRef passmgr
;
6432 /* Create the pass manager */
6433 passmgr
= LLVMCreateFunctionPassManagerForModule(
6436 /* This pass should eliminate all the load and store instructions */
6437 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6439 /* Add some optimization passes */
6440 LLVMAddScalarReplAggregatesPass(passmgr
);
6441 LLVMAddLICMPass(passmgr
);
6442 LLVMAddAggressiveDCEPass(passmgr
);
6443 LLVMAddCFGSimplificationPass(passmgr
);
6444 LLVMAddInstructionCombiningPass(passmgr
);
6447 LLVMInitializeFunctionPassManager(passmgr
);
6448 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6449 LLVMFinalizeFunctionPassManager(passmgr
);
6451 LLVMDisposeBuilder(ctx
->builder
);
6452 LLVMDisposePassManager(passmgr
);
6456 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6458 struct ac_vs_output_info
*outinfo
;
6460 switch (ctx
->stage
) {
6461 case MESA_SHADER_FRAGMENT
:
6462 case MESA_SHADER_COMPUTE
:
6463 case MESA_SHADER_TESS_CTRL
:
6464 case MESA_SHADER_GEOMETRY
:
6466 case MESA_SHADER_VERTEX
:
6467 if (ctx
->options
->key
.vs
.as_ls
||
6468 ctx
->options
->key
.vs
.as_es
)
6470 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6472 case MESA_SHADER_TESS_EVAL
:
6473 if (ctx
->options
->key
.vs
.as_es
)
6475 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6478 unreachable("Unhandled shader type");
6481 ac_optimize_vs_outputs(&ctx
->ac
,
6483 outinfo
->vs_output_param_offset
,
6485 &outinfo
->param_exports
);
6489 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6491 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6492 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6493 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6496 if (ctx
->is_gs_copy_shader
) {
6497 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6499 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6501 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6502 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6504 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.v4i32
, "");
6506 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6507 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6508 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6509 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6512 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6513 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6514 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6515 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6520 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6521 const struct nir_shader
*nir
)
6523 switch (nir
->info
.stage
) {
6524 case MESA_SHADER_TESS_CTRL
:
6525 return chip_class
>= CIK
? 128 : 64;
6526 case MESA_SHADER_GEOMETRY
:
6527 return chip_class
>= GFX9
? 128 : 64;
6528 case MESA_SHADER_COMPUTE
:
6534 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6535 nir
->info
.cs
.local_size
[1] *
6536 nir
->info
.cs
.local_size
[2];
6537 return max_workgroup_size
;
6540 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6541 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6543 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6544 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6545 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6546 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6548 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6549 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6550 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_rel_ids
, ctx
->rel_auto_id
, "");
6551 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6554 static void prepare_gs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6556 for(int i
= 5; i
>= 0; --i
) {
6557 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6558 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6559 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6562 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6563 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6564 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6567 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6568 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6570 struct ac_nir_context ctx
= {};
6571 struct nir_function
*func
;
6580 ctx
.stage
= nir
->info
.stage
;
6582 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6584 nir_foreach_variable(variable
, &nir
->outputs
)
6585 handle_shader_output_decl(&ctx
, nir
, variable
);
6587 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6588 _mesa_key_pointer_equal
);
6589 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6590 _mesa_key_pointer_equal
);
6591 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6592 _mesa_key_pointer_equal
);
6594 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6596 setup_locals(&ctx
, func
);
6598 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6599 setup_shared(&ctx
, nir
);
6601 visit_cf_list(&ctx
, &func
->impl
->body
);
6602 phi_post_pass(&ctx
);
6604 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6608 ralloc_free(ctx
.defs
);
6609 ralloc_free(ctx
.phis
);
6610 ralloc_free(ctx
.vars
);
6617 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6618 struct nir_shader
*const *shaders
,
6620 struct ac_shader_variant_info
*shader_info
,
6621 const struct ac_nir_compiler_options
*options
)
6623 struct nir_to_llvm_context ctx
= {0};
6625 ctx
.options
= options
;
6626 ctx
.shader_info
= shader_info
;
6627 ctx
.context
= LLVMContextCreate();
6628 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6630 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
6632 ctx
.ac
.module
= ctx
.module
;
6633 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6635 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6636 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6637 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6638 LLVMDisposeTargetData(data_layout
);
6639 LLVMDisposeMessage(data_layout_str
);
6641 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6642 ctx
.ac
.builder
= ctx
.builder
;
6644 memset(shader_info
, 0, sizeof(*shader_info
));
6646 for(int i
= 0; i
< shader_count
; ++i
)
6647 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6649 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6650 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6651 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6652 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6654 ctx
.max_workgroup_size
= 0;
6655 for (int i
= 0; i
< shader_count
; ++i
) {
6656 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6657 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6661 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6662 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6664 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6665 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6666 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
6667 ctx
.abi
.load_ubo
= radv_load_ubo
;
6668 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6669 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6670 ctx
.abi
.clamp_shadow_reference
= false;
6672 if (shader_count
>= 2)
6673 ac_init_exec_full_mask(&ctx
.ac
);
6675 if (ctx
.ac
.chip_class
== GFX9
&&
6676 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6677 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6679 for(int i
= 0; i
< shader_count
; ++i
) {
6680 ctx
.stage
= shaders
[i
]->info
.stage
;
6681 ctx
.output_mask
= 0;
6682 ctx
.tess_outputs_written
= 0;
6683 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6684 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6686 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6687 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6688 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6689 ctx
.abi
.load_inputs
= load_gs_input
;
6690 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6691 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
6692 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
6693 ctx
.abi
.load_tess_inputs
= load_tcs_input
;
6694 ctx
.abi
.store_tcs_outputs
= store_tcs_output
;
6695 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6696 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6697 ctx
.abi
.load_tess_inputs
= load_tes_input
;
6698 ctx
.abi
.load_tess_coord
= load_tess_coord
;
6699 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6700 if (shader_info
->info
.vs
.needs_instance_id
) {
6701 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6702 MAX2(3, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6704 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6705 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6711 ac_setup_rings(&ctx
);
6713 LLVMBasicBlockRef merge_block
;
6714 if (shader_count
>= 2) {
6715 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6716 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6717 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6719 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6720 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6721 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6722 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6723 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6724 thread_id
, count
, "");
6725 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6727 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6730 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6731 handle_fs_inputs(&ctx
, shaders
[i
]);
6732 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6733 handle_vs_inputs(&ctx
, shaders
[i
]);
6734 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6735 prepare_gs_input_vgprs(&ctx
);
6737 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6738 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6740 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6742 if (shader_count
>= 2) {
6743 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6744 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6747 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6748 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6749 shaders
[i
]->info
.cull_distance_array_size
> 4;
6750 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6751 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6752 shaders
[i
]->info
.gs
.vertices_out
;
6753 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6754 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6755 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6756 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6757 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6761 LLVMBuildRetVoid(ctx
.builder
);
6763 ac_llvm_finalize_module(&ctx
);
6765 if (shader_count
== 1)
6766 ac_nir_eliminate_const_vs_outputs(&ctx
);
6771 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6773 unsigned *retval
= (unsigned *)context
;
6774 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6775 char *description
= LLVMGetDiagInfoDescription(di
);
6777 if (severity
== LLVMDSError
) {
6779 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6783 LLVMDisposeMessage(description
);
6786 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6787 struct ac_shader_binary
*binary
,
6788 LLVMTargetMachineRef tm
)
6790 unsigned retval
= 0;
6792 LLVMContextRef llvm_ctx
;
6793 LLVMMemoryBufferRef out_buffer
;
6794 unsigned buffer_size
;
6795 const char *buffer_data
;
6798 /* Setup Diagnostic Handler*/
6799 llvm_ctx
= LLVMGetModuleContext(M
);
6801 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6805 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6808 /* Process Errors/Warnings */
6810 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6816 /* Extract Shader Code*/
6817 buffer_size
= LLVMGetBufferSize(out_buffer
);
6818 buffer_data
= LLVMGetBufferStart(out_buffer
);
6820 ac_elf_read(buffer_data
, buffer_size
, binary
);
6823 LLVMDisposeMemoryBuffer(out_buffer
);
6829 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6830 LLVMModuleRef llvm_module
,
6831 struct ac_shader_binary
*binary
,
6832 struct ac_shader_config
*config
,
6833 struct ac_shader_variant_info
*shader_info
,
6834 gl_shader_stage stage
,
6835 bool dump_shader
, bool supports_spill
)
6838 ac_dump_module(llvm_module
);
6840 memset(binary
, 0, sizeof(*binary
));
6841 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6843 fprintf(stderr
, "compile failed\n");
6847 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6849 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6851 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6852 LLVMDisposeModule(llvm_module
);
6853 LLVMContextDispose(ctx
);
6855 if (stage
== MESA_SHADER_FRAGMENT
) {
6856 shader_info
->num_input_vgprs
= 0;
6857 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6858 shader_info
->num_input_vgprs
+= 2;
6859 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6860 shader_info
->num_input_vgprs
+= 2;
6861 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6862 shader_info
->num_input_vgprs
+= 2;
6863 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6864 shader_info
->num_input_vgprs
+= 3;
6865 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6866 shader_info
->num_input_vgprs
+= 2;
6867 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6868 shader_info
->num_input_vgprs
+= 2;
6869 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6870 shader_info
->num_input_vgprs
+= 2;
6871 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6872 shader_info
->num_input_vgprs
+= 1;
6873 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6874 shader_info
->num_input_vgprs
+= 1;
6875 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6876 shader_info
->num_input_vgprs
+= 1;
6877 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6878 shader_info
->num_input_vgprs
+= 1;
6879 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6880 shader_info
->num_input_vgprs
+= 1;
6881 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6882 shader_info
->num_input_vgprs
+= 1;
6883 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6884 shader_info
->num_input_vgprs
+= 1;
6885 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6886 shader_info
->num_input_vgprs
+= 1;
6887 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6888 shader_info
->num_input_vgprs
+= 1;
6890 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6892 /* +3 for scratch wave offset and VCC */
6893 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6894 shader_info
->num_input_sgprs
+ 3);
6898 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
6900 switch (nir
->info
.stage
) {
6901 case MESA_SHADER_COMPUTE
:
6902 for (int i
= 0; i
< 3; ++i
)
6903 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6905 case MESA_SHADER_FRAGMENT
:
6906 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6908 case MESA_SHADER_GEOMETRY
:
6909 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6910 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6911 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6912 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6914 case MESA_SHADER_TESS_EVAL
:
6915 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6916 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6917 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6918 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6919 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6921 case MESA_SHADER_TESS_CTRL
:
6922 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6924 case MESA_SHADER_VERTEX
:
6925 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6926 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6927 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6928 if (options
->key
.vs
.as_ls
)
6929 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6936 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6937 struct ac_shader_binary
*binary
,
6938 struct ac_shader_config
*config
,
6939 struct ac_shader_variant_info
*shader_info
,
6940 struct nir_shader
*const *nir
,
6942 const struct ac_nir_compiler_options
*options
,
6946 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
6949 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
6950 for (int i
= 0; i
< nir_count
; ++i
)
6951 ac_fill_shader_info(shader_info
, nir
[i
], options
);
6955 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6957 LLVMValueRef args
[9];
6958 args
[0] = ctx
->gsvs_ring
;
6959 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
6960 args
[3] = ctx
->ac
.i32_0
;
6961 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
6962 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
6963 args
[6] = ctx
->ac
.i32_1
; /* GLC */
6964 args
[7] = ctx
->ac
.i32_1
; /* SLC */
6965 args
[8] = ctx
->ac
.i32_0
; /* TFE */
6969 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6973 if (!(ctx
->output_mask
& (1ull << i
)))
6976 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6977 /* unpack clip and cull from a single set of slots */
6978 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6983 for (unsigned j
= 0; j
< length
; j
++) {
6985 args
[2] = LLVMConstInt(ctx
->ac
.i32
,
6987 ctx
->gs_max_out_vertices
* 16 * 4, false);
6989 value
= ac_build_intrinsic(&ctx
->ac
,
6990 "llvm.SI.buffer.load.dword.i32.i32",
6991 ctx
->ac
.i32
, args
, 9,
6992 AC_FUNC_ATTR_READONLY
|
6993 AC_FUNC_ATTR_LEGACY
);
6995 LLVMBuildStore(ctx
->builder
,
6996 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
7000 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
7003 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
7004 struct nir_shader
*geom_shader
,
7005 struct ac_shader_binary
*binary
,
7006 struct ac_shader_config
*config
,
7007 struct ac_shader_variant_info
*shader_info
,
7008 const struct ac_nir_compiler_options
*options
,
7011 struct nir_to_llvm_context ctx
= {0};
7012 ctx
.context
= LLVMContextCreate();
7013 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
7014 ctx
.options
= options
;
7015 ctx
.shader_info
= shader_info
;
7017 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
7019 ctx
.ac
.module
= ctx
.module
;
7021 ctx
.is_gs_copy_shader
= true;
7022 LLVMSetTarget(ctx
.module
, "amdgcn--");
7024 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
7025 ctx
.ac
.builder
= ctx
.builder
;
7026 ctx
.stage
= MESA_SHADER_VERTEX
;
7028 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
7030 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
7031 ac_setup_rings(&ctx
);
7033 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
7034 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
7036 struct ac_nir_context nir_ctx
= {};
7037 nir_ctx
.ac
= ctx
.ac
;
7038 nir_ctx
.abi
= &ctx
.abi
;
7040 nir_ctx
.nctx
= &ctx
;
7043 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
7044 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
7045 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
7048 ac_gs_copy_shader_emit(&ctx
);
7052 LLVMBuildRetVoid(ctx
.builder
);
7054 ac_llvm_finalize_module(&ctx
);
7056 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
7058 dump_shader
, options
->supports_spill
);