2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "../vulkan/radv_descriptor_set.h"
31 #include "util/bitscan.h"
32 #include <llvm-c/Transforms/Scalar.h>
33 #include "ac_shader_abi.h"
34 #include "ac_shader_info.h"
35 #include "ac_exp_param.h"
37 enum radeon_llvm_calling_convention
{
38 RADEON_LLVM_AMDGPU_VS
= 87,
39 RADEON_LLVM_AMDGPU_GS
= 88,
40 RADEON_LLVM_AMDGPU_PS
= 89,
41 RADEON_LLVM_AMDGPU_CS
= 90,
42 RADEON_LLVM_AMDGPU_HS
= 93,
45 #define CONST_ADDR_SPACE 2
46 #define LOCAL_ADDR_SPACE 3
48 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
49 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
51 struct nir_to_llvm_context
;
53 struct ac_nir_context
{
54 struct ac_llvm_context ac
;
55 struct ac_shader_abi
*abi
;
57 gl_shader_stage stage
;
59 struct hash_table
*defs
;
60 struct hash_table
*phis
;
61 struct hash_table
*vars
;
63 LLVMValueRef main_function
;
64 LLVMBasicBlockRef continue_block
;
65 LLVMBasicBlockRef break_block
;
67 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
72 struct nir_to_llvm_context
*nctx
; /* TODO get rid of this */
75 struct nir_to_llvm_context
{
76 struct ac_llvm_context ac
;
77 const struct ac_nir_compiler_options
*options
;
78 struct ac_shader_variant_info
*shader_info
;
79 struct ac_shader_abi abi
;
80 struct ac_nir_context
*nir
;
82 unsigned max_workgroup_size
;
83 LLVMContextRef context
;
85 LLVMBuilderRef builder
;
86 LLVMValueRef main_function
;
88 struct hash_table
*defs
;
89 struct hash_table
*phis
;
91 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
92 LLVMValueRef ring_offsets
;
93 LLVMValueRef push_constants
;
94 LLVMValueRef view_index
;
95 LLVMValueRef num_work_groups
;
96 LLVMValueRef workgroup_ids
;
97 LLVMValueRef local_invocation_ids
;
100 LLVMValueRef vertex_buffers
;
101 LLVMValueRef rel_auto_id
;
102 LLVMValueRef vs_prim_id
;
103 LLVMValueRef ls_out_layout
;
104 LLVMValueRef es2gs_offset
;
106 LLVMValueRef tcs_offchip_layout
;
107 LLVMValueRef tcs_out_offsets
;
108 LLVMValueRef tcs_out_layout
;
109 LLVMValueRef tcs_in_layout
;
111 LLVMValueRef merged_wave_info
;
112 LLVMValueRef tess_factor_offset
;
113 LLVMValueRef tcs_patch_id
;
114 LLVMValueRef tcs_rel_ids
;
115 LLVMValueRef tes_rel_patch_id
;
116 LLVMValueRef tes_patch_id
;
120 LLVMValueRef gsvs_ring_stride
;
121 LLVMValueRef gsvs_num_entries
;
122 LLVMValueRef gs2vs_offset
;
123 LLVMValueRef gs_wave_id
;
124 LLVMValueRef gs_vtx_offset
[6];
126 LLVMValueRef esgs_ring
;
127 LLVMValueRef gsvs_ring
;
128 LLVMValueRef hs_ring_tess_offchip
;
129 LLVMValueRef hs_ring_tess_factor
;
131 LLVMValueRef prim_mask
;
132 LLVMValueRef sample_pos_offset
;
133 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
134 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
136 gl_shader_stage stage
;
138 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
141 uint64_t output_mask
;
142 uint8_t num_output_clips
;
143 uint8_t num_output_culls
;
145 bool is_gs_copy_shader
;
146 LLVMValueRef gs_next_vertex
;
147 unsigned gs_max_out_vertices
;
149 unsigned tes_primitive_mode
;
150 uint64_t tess_outputs_written
;
151 uint64_t tess_patch_outputs_written
;
153 uint32_t tcs_patch_outputs_read
;
154 uint64_t tcs_outputs_read
;
157 static inline struct nir_to_llvm_context
*
158 nir_to_llvm_context_from_abi(struct ac_shader_abi
*abi
)
160 struct nir_to_llvm_context
*ctx
= NULL
;
161 return container_of(abi
, ctx
, abi
);
164 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
165 const nir_deref_var
*deref
,
166 enum ac_descriptor_type desc_type
,
167 const nir_tex_instr
*instr
,
168 bool image
, bool write
);
170 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
172 return (index
* 4) + chan
;
175 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
177 /* handle patch indices separate */
178 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
180 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
182 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
183 return 2 + (slot
- VARYING_SLOT_PATCH0
);
185 if (slot
== VARYING_SLOT_POS
)
187 if (slot
== VARYING_SLOT_PSIZ
)
189 if (slot
== VARYING_SLOT_CLIP_DIST0
)
191 /* 3 is reserved for clip dist as well */
192 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
193 return 4 + (slot
- VARYING_SLOT_VAR0
);
194 unreachable("illegal slot in get unique index\n");
197 static void set_llvm_calling_convention(LLVMValueRef func
,
198 gl_shader_stage stage
)
200 enum radeon_llvm_calling_convention calling_conv
;
203 case MESA_SHADER_VERTEX
:
204 case MESA_SHADER_TESS_EVAL
:
205 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
207 case MESA_SHADER_GEOMETRY
:
208 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
210 case MESA_SHADER_TESS_CTRL
:
211 calling_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
: RADEON_LLVM_AMDGPU_VS
;
213 case MESA_SHADER_FRAGMENT
:
214 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
216 case MESA_SHADER_COMPUTE
:
217 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
220 unreachable("Unhandle shader type");
223 LLVMSetFunctionCallConv(func
, calling_conv
);
228 LLVMTypeRef types
[MAX_ARGS
];
229 LLVMValueRef
*assign
[MAX_ARGS
];
230 unsigned array_params_mask
;
232 uint8_t user_sgpr_count
;
234 uint8_t num_user_sgprs_used
;
235 uint8_t num_sgprs_used
;
236 uint8_t num_vgprs_used
;
240 add_argument(struct arg_info
*info
,
241 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
243 assert(info
->count
< MAX_ARGS
);
244 info
->assign
[info
->count
] = param_ptr
;
245 info
->types
[info
->count
] = type
;
250 add_sgpr_argument(struct arg_info
*info
,
251 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
253 add_argument(info
, type
, param_ptr
);
254 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
259 add_user_sgpr_argument(struct arg_info
*info
,
261 LLVMValueRef
*param_ptr
)
263 add_sgpr_argument(info
, type
, param_ptr
);
264 info
->num_user_sgprs_used
+= ac_get_type_size(type
) / 4;
265 info
->user_sgpr_count
++;
269 add_vgpr_argument(struct arg_info
*info
,
271 LLVMValueRef
*param_ptr
)
273 add_argument(info
, type
, param_ptr
);
274 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
278 add_user_sgpr_array_argument(struct arg_info
*info
,
280 LLVMValueRef
*param_ptr
)
282 info
->array_params_mask
|= (1 << info
->count
);
283 add_user_sgpr_argument(info
, type
, param_ptr
);
286 static void assign_arguments(LLVMValueRef main_function
,
287 struct arg_info
*info
)
290 for (i
= 0; i
< info
->count
; i
++) {
292 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
297 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
298 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
299 unsigned num_return_elems
,
300 struct arg_info
*args
,
301 unsigned max_workgroup_size
,
304 LLVMTypeRef main_function_type
, ret_type
;
305 LLVMBasicBlockRef main_function_body
;
307 if (num_return_elems
)
308 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
309 num_return_elems
, true);
311 ret_type
= LLVMVoidTypeInContext(ctx
);
313 /* Setup the function */
315 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
316 LLVMValueRef main_function
=
317 LLVMAddFunction(module
, "main", main_function_type
);
319 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
320 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
322 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
323 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
324 if (args
->array_params_mask
& (1 << i
)) {
325 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
326 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
327 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
330 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
334 if (max_workgroup_size
) {
335 ac_llvm_add_target_dep_function_attr(main_function
,
336 "amdgpu-max-work-group-size",
340 /* These were copied from some LLVM test. */
341 LLVMAddTargetDependentFunctionAttr(main_function
,
342 "less-precise-fpmad",
344 LLVMAddTargetDependentFunctionAttr(main_function
,
347 LLVMAddTargetDependentFunctionAttr(main_function
,
350 LLVMAddTargetDependentFunctionAttr(main_function
,
354 return main_function
;
357 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
359 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
363 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
365 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
366 type
= LLVMGetElementType(type
);
368 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
369 return LLVMGetIntTypeWidth(type
);
371 if (type
== ctx
->f16
)
373 if (type
== ctx
->f32
)
375 if (type
== ctx
->f64
)
378 unreachable("Unhandled type kind in get_elem_bits");
381 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
382 LLVMValueRef param
, unsigned rshift
,
385 LLVMValueRef value
= param
;
387 value
= LLVMBuildLShr(ctx
->builder
, value
,
388 LLVMConstInt(ctx
->i32
, rshift
, false), "");
390 if (rshift
+ bitwidth
< 32) {
391 unsigned mask
= (1 << bitwidth
) - 1;
392 value
= LLVMBuildAnd(ctx
->builder
, value
,
393 LLVMConstInt(ctx
->i32
, mask
, false), "");
398 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
400 switch (ctx
->stage
) {
401 case MESA_SHADER_TESS_CTRL
:
402 return unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
403 case MESA_SHADER_TESS_EVAL
:
404 return ctx
->tes_rel_patch_id
;
407 unreachable("Illegal stage");
411 /* Tessellation shaders pass outputs to the next shader using LDS.
413 * LS outputs = TCS inputs
414 * TCS outputs = TES inputs
417 * - TCS inputs for patch 0
418 * - TCS inputs for patch 1
419 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
421 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
422 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
423 * - TCS outputs for patch 1
424 * - Per-patch TCS outputs for patch 1
425 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
426 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
429 * All three shaders VS(LS), TCS, TES share the same LDS space.
432 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
434 if (ctx
->stage
== MESA_SHADER_VERTEX
)
435 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
436 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
437 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
445 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
447 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
451 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
453 return LLVMBuildMul(ctx
->builder
,
454 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
455 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
459 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
461 return LLVMBuildMul(ctx
->builder
,
462 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
463 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
467 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
469 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
470 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
472 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
476 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
478 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
479 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
480 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
482 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
483 LLVMBuildMul(ctx
->builder
, patch_stride
,
489 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
491 LLVMValueRef patch0_patch_data_offset
=
492 get_tcs_out_patch0_patch_data_offset(ctx
);
493 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
494 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
496 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
497 LLVMBuildMul(ctx
->builder
, patch_stride
,
502 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
504 ud_info
->sgpr_idx
= *sgpr_idx
;
505 ud_info
->num_sgprs
= num_sgprs
;
506 ud_info
->indirect
= false;
507 ud_info
->indirect_offset
= 0;
508 *sgpr_idx
+= num_sgprs
;
511 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
512 int idx
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
514 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
518 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
519 uint32_t indirect_offset
)
521 ud_info
->sgpr_idx
= sgpr_idx
;
522 ud_info
->num_sgprs
= num_sgprs
;
523 ud_info
->indirect
= true;
524 ud_info
->indirect_offset
= indirect_offset
;
527 struct user_sgpr_info
{
528 bool need_ring_offsets
;
530 bool indirect_all_descriptor_sets
;
533 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
534 struct user_sgpr_info
*user_sgpr_info
)
536 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
538 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
539 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
540 ctx
->stage
== MESA_SHADER_VERTEX
||
541 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
542 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
543 ctx
->is_gs_copy_shader
)
544 user_sgpr_info
->need_ring_offsets
= true;
546 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
547 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
548 user_sgpr_info
->need_ring_offsets
= true;
550 /* 2 user sgprs will nearly always be allocated for scratch/rings */
551 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
552 user_sgpr_info
->sgpr_count
+= 2;
555 switch (ctx
->stage
) {
556 case MESA_SHADER_COMPUTE
:
557 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
559 case MESA_SHADER_FRAGMENT
:
560 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
562 case MESA_SHADER_VERTEX
:
563 if (!ctx
->is_gs_copy_shader
) {
564 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
565 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
566 user_sgpr_info
->sgpr_count
+= 3;
568 user_sgpr_info
->sgpr_count
+= 2;
571 if (ctx
->options
->key
.vs
.as_ls
)
572 user_sgpr_info
->sgpr_count
++;
574 case MESA_SHADER_TESS_CTRL
:
575 user_sgpr_info
->sgpr_count
+= 4;
577 case MESA_SHADER_TESS_EVAL
:
578 user_sgpr_info
->sgpr_count
+= 1;
580 case MESA_SHADER_GEOMETRY
:
581 user_sgpr_info
->sgpr_count
+= 2;
587 if (ctx
->shader_info
->info
.needs_push_constants
)
588 user_sgpr_info
->sgpr_count
+= 2;
590 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
591 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
592 user_sgpr_info
->sgpr_count
+= 2;
593 user_sgpr_info
->indirect_all_descriptor_sets
= true;
595 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
600 radv_define_common_user_sgprs_phase1(struct nir_to_llvm_context
*ctx
,
601 gl_shader_stage stage
,
602 bool has_previous_stage
,
603 gl_shader_stage previous_stage
,
604 const struct user_sgpr_info
*user_sgpr_info
,
605 struct arg_info
*args
,
606 LLVMValueRef
*desc_sets
)
608 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
609 unsigned stage_mask
= 1 << stage
;
610 if (has_previous_stage
)
611 stage_mask
|= 1 << previous_stage
;
613 /* 1 for each descriptor set */
614 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
615 for (unsigned i
= 0; i
< num_sets
; ++i
) {
616 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
617 add_user_sgpr_array_argument(args
, const_array(ctx
->ac
.i8
, 1024 * 1024), &ctx
->descriptor_sets
[i
]);
621 add_user_sgpr_array_argument(args
, const_array(const_array(ctx
->ac
.i8
, 1024 * 1024), 32), desc_sets
);
623 if (ctx
->shader_info
->info
.needs_push_constants
) {
624 /* 1 for push constants and dynamic descriptors */
625 add_user_sgpr_array_argument(args
, const_array(ctx
->ac
.i8
, 1024 * 1024), &ctx
->push_constants
);
630 radv_define_common_user_sgprs_phase2(struct nir_to_llvm_context
*ctx
,
631 gl_shader_stage stage
,
632 bool has_previous_stage
,
633 gl_shader_stage previous_stage
,
634 const struct user_sgpr_info
*user_sgpr_info
,
635 LLVMValueRef desc_sets
,
636 uint8_t *user_sgpr_idx
)
638 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
639 unsigned stage_mask
= 1 << stage
;
640 if (has_previous_stage
)
641 stage_mask
|= 1 << previous_stage
;
643 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
644 for (unsigned i
= 0; i
< num_sets
; ++i
) {
645 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
646 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
648 ctx
->descriptor_sets
[i
] = NULL
;
651 uint32_t desc_sgpr_idx
= *user_sgpr_idx
;
652 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, user_sgpr_idx
, 2);
654 for (unsigned i
= 0; i
< num_sets
; ++i
) {
655 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
656 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
657 ctx
->descriptor_sets
[i
] = ac_build_load_to_sgpr(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->ac
.i32
, i
, false));
660 ctx
->descriptor_sets
[i
] = NULL
;
662 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
665 if (ctx
->shader_info
->info
.needs_push_constants
) {
666 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
671 radv_define_vs_user_sgprs_phase1(struct nir_to_llvm_context
*ctx
,
672 gl_shader_stage stage
,
673 bool has_previous_stage
,
674 gl_shader_stage previous_stage
,
675 struct arg_info
*args
)
677 if (!ctx
->is_gs_copy_shader
&& (stage
== MESA_SHADER_VERTEX
|| (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
678 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
679 add_user_sgpr_argument(args
, const_array(ctx
->ac
.v4i32
, 16), &ctx
->vertex_buffers
); /* vertex buffers */
680 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
); // base vertex
681 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);// start instance
682 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
683 add_user_sgpr_argument(args
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
); // draw id
688 radv_define_vs_user_sgprs_phase2(struct nir_to_llvm_context
*ctx
,
689 gl_shader_stage stage
,
690 bool has_previous_stage
,
691 gl_shader_stage previous_stage
,
692 uint8_t *user_sgpr_idx
)
694 if (!ctx
->is_gs_copy_shader
&& (stage
== MESA_SHADER_VERTEX
|| (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
695 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
696 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
699 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
702 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, vs_num
);
707 static void create_function(struct nir_to_llvm_context
*ctx
,
708 gl_shader_stage stage
,
709 bool has_previous_stage
,
710 gl_shader_stage previous_stage
)
712 uint8_t user_sgpr_idx
;
713 struct user_sgpr_info user_sgpr_info
;
714 struct arg_info args
= {};
715 LLVMValueRef desc_sets
;
717 allocate_user_sgprs(ctx
, &user_sgpr_info
);
719 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
720 add_user_sgpr_argument(&args
, const_array(ctx
->ac
.v4i32
, 16), &ctx
->ring_offsets
); /* address of rings */
724 case MESA_SHADER_COMPUTE
:
725 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
726 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
727 add_user_sgpr_argument(&args
, LLVMVectorType(ctx
->ac
.i32
, ctx
->shader_info
->info
.cs
.grid_components_used
), &ctx
->num_work_groups
); /* grid size */
728 add_sgpr_argument(&args
, ctx
->ac
.v3i32
, &ctx
->workgroup_ids
);
729 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tg_size
);
730 add_vgpr_argument(&args
, ctx
->ac
.v3i32
, &ctx
->local_invocation_ids
);
732 case MESA_SHADER_VERTEX
:
733 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
734 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
735 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
))
736 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
737 if (ctx
->options
->key
.vs
.as_es
)
738 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->es2gs_offset
); // es2gs offset
739 else if (ctx
->options
->key
.vs
.as_ls
)
740 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->ls_out_layout
); // ls out layout
741 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
742 if (!ctx
->is_gs_copy_shader
) {
743 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
744 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
745 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
748 case MESA_SHADER_TESS_CTRL
:
749 if (has_previous_stage
) {
750 // First 6 system regs
751 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
752 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->merged_wave_info
); // merged wave info
753 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tess_factor_offset
); // tess factor offset
755 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // scratch offset
756 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
757 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
759 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
760 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
761 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->ls_out_layout
); // ls out layout
763 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
764 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
765 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_layout
); // tcs out layout
766 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_in_layout
); // tcs in layout
767 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
768 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
770 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_patch_id
); // patch id
771 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_rel_ids
); // rel ids;
772 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
773 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
774 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
775 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
777 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
778 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
779 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
780 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_out_layout
); // tcs out layout
781 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_in_layout
); // tcs in layout
782 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
783 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
784 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
785 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tess_factor_offset
); // tess factor offset
786 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_patch_id
); // patch id
787 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_rel_ids
); // rel ids;
790 case MESA_SHADER_TESS_EVAL
:
791 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
792 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
793 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
794 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
795 if (ctx
->options
->key
.tes
.as_es
) {
796 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // OC LDS
797 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); //
798 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->es2gs_offset
); // es2gs offset
800 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); //
801 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // OC LDS
803 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->tes_u
); // tes_u
804 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->tes_v
); // tes_v
805 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
806 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_patch_id
); // tes patch id
808 case MESA_SHADER_GEOMETRY
:
809 if (has_previous_stage
) {
810 // First 6 system regs
811 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
); // tess factor offset
812 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->merged_wave_info
); // merged wave info
813 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->oc_lds
); // param oc lds
815 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // scratch offset
816 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
817 add_sgpr_argument(&args
, ctx
->ac
.i32
, NULL
); // unknown
819 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
820 if (previous_stage
== MESA_SHADER_TESS_EVAL
)
821 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
823 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
824 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
825 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
826 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
827 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
829 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[0]); // vtx01
830 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[2]); // vtx23
831 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.gs_prim_id
); // prim id
832 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.gs_invocation_id
);
833 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[4]);
835 if (previous_stage
== MESA_SHADER_VERTEX
) {
836 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
); // vertex id
837 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->rel_auto_id
); // rel auto id
838 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->vs_prim_id
); // vs prim id
839 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
); // instance id
841 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->tes_u
); // tes_u
842 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->tes_v
); // tes_v
843 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
844 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->tes_patch_id
); // tes patch id
847 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
848 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
849 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
850 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
851 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
852 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->view_index
);
853 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
); // gs2vs offset
854 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_wave_id
); // wave id
855 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[0]); // vtx0
856 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[1]); // vtx1
857 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.gs_prim_id
); // prim id
858 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[2]);
859 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[3]);
860 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[4]);
861 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->gs_vtx_offset
[5]);
862 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.gs_invocation_id
);
865 case MESA_SHADER_FRAGMENT
:
866 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
867 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
868 add_user_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->sample_pos_offset
); /* sample position offset */
869 add_sgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->prim_mask
); /* prim mask */
870 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->persp_sample
); /* persp sample */
871 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->persp_center
); /* persp center */
872 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
); /* persp centroid */
873 add_vgpr_argument(&args
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
874 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->linear_sample
); /* linear sample */
875 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->linear_center
); /* linear center */
876 add_vgpr_argument(&args
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
); /* linear centroid */
877 add_vgpr_argument(&args
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
878 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]); /* pos x float */
879 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]); /* pos y float */
880 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]); /* pos z float */
881 add_vgpr_argument(&args
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]); /* pos w float */
882 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.front_face
); /* front face */
883 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
); /* ancillary */
884 add_vgpr_argument(&args
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
); /* sample coverage */
885 add_vgpr_argument(&args
, ctx
->ac
.i32
, NULL
); /* fixed pt */
888 unreachable("Shader stage not implemented");
891 ctx
->main_function
= create_llvm_function(
892 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
893 ctx
->max_workgroup_size
,
894 ctx
->options
->unsafe_math
);
895 set_llvm_calling_convention(ctx
->main_function
, stage
);
898 ctx
->shader_info
->num_input_vgprs
= 0;
899 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
901 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
903 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
904 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
906 assign_arguments(ctx
->main_function
, &args
);
910 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
911 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, &user_sgpr_idx
, 2);
912 if (ctx
->options
->supports_spill
) {
913 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
914 LLVMPointerType(ctx
->ac
.i8
, CONST_ADDR_SPACE
),
915 NULL
, 0, AC_FUNC_ATTR_READNONE
);
916 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
917 const_array(ctx
->ac
.v4i32
, 16), "");
921 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
922 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
923 if (has_previous_stage
)
926 radv_define_common_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
929 case MESA_SHADER_COMPUTE
:
930 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
931 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, &user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
934 case MESA_SHADER_VERTEX
:
935 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
937 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
938 if (ctx
->options
->key
.vs
.as_ls
) {
939 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
941 if (ctx
->options
->key
.vs
.as_ls
)
942 ac_declare_lds_as_pointer(&ctx
->ac
);
944 case MESA_SHADER_TESS_CTRL
:
945 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
946 if (has_previous_stage
)
947 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
948 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
950 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
951 ac_declare_lds_as_pointer(&ctx
->ac
);
953 case MESA_SHADER_TESS_EVAL
:
954 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
956 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
958 case MESA_SHADER_GEOMETRY
:
959 if (has_previous_stage
) {
960 if (previous_stage
== MESA_SHADER_VERTEX
)
961 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
963 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
965 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, &user_sgpr_idx
, 2);
967 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
968 if (has_previous_stage
)
969 ac_declare_lds_as_pointer(&ctx
->ac
);
971 case MESA_SHADER_FRAGMENT
:
972 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
973 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, &user_sgpr_idx
, 1);
977 unreachable("Shader stage not implemented");
980 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
983 static int get_llvm_num_components(LLVMValueRef value
)
985 LLVMTypeRef type
= LLVMTypeOf(value
);
986 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
987 ? LLVMGetVectorSize(type
)
989 return num_components
;
992 static LLVMValueRef
llvm_extract_elem(struct ac_llvm_context
*ac
,
996 int count
= get_llvm_num_components(value
);
1001 return LLVMBuildExtractElement(ac
->builder
, value
,
1002 LLVMConstInt(ac
->i32
, index
, false), "");
1005 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1006 LLVMValueRef value
, unsigned count
)
1008 unsigned num_components
= get_llvm_num_components(value
);
1009 if (count
== num_components
)
1012 LLVMValueRef masks
[] = {
1013 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1014 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1017 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1020 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1021 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1025 build_store_values_extended(struct ac_llvm_context
*ac
,
1026 LLVMValueRef
*values
,
1027 unsigned value_count
,
1028 unsigned value_stride
,
1031 LLVMBuilderRef builder
= ac
->builder
;
1034 for (i
= 0; i
< value_count
; i
++) {
1035 LLVMValueRef ptr
= values
[i
* value_stride
];
1036 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1037 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1038 LLVMBuildStore(builder
, value
, ptr
);
1042 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1043 const nir_ssa_def
*def
)
1045 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1046 if (def
->num_components
> 1) {
1047 type
= LLVMVectorType(type
, def
->num_components
);
1052 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1055 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1056 return (LLVMValueRef
)entry
->data
;
1060 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1061 const struct nir_block
*b
)
1063 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1064 return (LLVMBasicBlockRef
)entry
->data
;
1067 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1069 unsigned num_components
)
1071 LLVMValueRef value
= get_src(ctx
, src
.src
);
1072 bool need_swizzle
= false;
1075 LLVMTypeRef type
= LLVMTypeOf(value
);
1076 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1077 ? LLVMGetVectorSize(type
)
1080 for (unsigned i
= 0; i
< num_components
; ++i
) {
1081 assert(src
.swizzle
[i
] < src_components
);
1082 if (src
.swizzle
[i
] != i
)
1083 need_swizzle
= true;
1086 if (need_swizzle
|| num_components
!= src_components
) {
1087 LLVMValueRef masks
[] = {
1088 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1089 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1090 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1091 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1093 if (src_components
> 1 && num_components
== 1) {
1094 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1096 } else if (src_components
== 1 && num_components
> 1) {
1097 LLVMValueRef values
[] = {value
, value
, value
, value
};
1098 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1100 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1101 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1105 assert(!src
.negate
);
1110 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1111 LLVMIntPredicate pred
, LLVMValueRef src0
,
1114 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1115 return LLVMBuildSelect(ctx
->builder
, result
,
1116 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1117 LLVMConstInt(ctx
->i32
, 0, false), "");
1120 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1121 LLVMRealPredicate pred
, LLVMValueRef src0
,
1124 LLVMValueRef result
;
1125 src0
= ac_to_float(ctx
, src0
);
1126 src1
= ac_to_float(ctx
, src1
);
1127 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1128 return LLVMBuildSelect(ctx
->builder
, result
,
1129 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1130 LLVMConstInt(ctx
->i32
, 0, false), "");
1133 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1135 LLVMTypeRef result_type
,
1139 LLVMValueRef params
[] = {
1140 ac_to_float(ctx
, src0
),
1143 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1144 get_elem_bits(ctx
, result_type
));
1145 assert(length
< sizeof(name
));
1146 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1149 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1151 LLVMTypeRef result_type
,
1152 LLVMValueRef src0
, LLVMValueRef src1
)
1155 LLVMValueRef params
[] = {
1156 ac_to_float(ctx
, src0
),
1157 ac_to_float(ctx
, src1
),
1160 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1161 get_elem_bits(ctx
, result_type
));
1162 assert(length
< sizeof(name
));
1163 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1166 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1168 LLVMTypeRef result_type
,
1169 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1172 LLVMValueRef params
[] = {
1173 ac_to_float(ctx
, src0
),
1174 ac_to_float(ctx
, src1
),
1175 ac_to_float(ctx
, src2
),
1178 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1179 get_elem_bits(ctx
, result_type
));
1180 assert(length
< sizeof(name
));
1181 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1184 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1185 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1187 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1189 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1192 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1193 LLVMIntPredicate pred
,
1194 LLVMValueRef src0
, LLVMValueRef src1
)
1196 return LLVMBuildSelect(ctx
->builder
,
1197 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1202 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1205 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1206 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1209 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1212 LLVMValueRef cmp
, val
;
1214 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32_0
, "");
1215 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32_1
, src0
, "");
1216 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32_0
, "");
1217 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1221 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1224 LLVMValueRef cmp
, val
;
1226 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32_0
, "");
1227 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32_1
, src0
, "");
1228 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32_0
, "");
1229 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1233 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1236 const char *intr
= "llvm.floor.f32";
1237 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1238 LLVMValueRef params
[] = {
1241 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1242 ctx
->f32
, params
, 1,
1243 AC_FUNC_ATTR_READNONE
);
1244 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1247 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1249 LLVMValueRef src0
, LLVMValueRef src1
)
1251 LLVMTypeRef ret_type
;
1252 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1254 LLVMValueRef params
[] = { src0
, src1
};
1255 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1258 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1259 params
, 2, AC_FUNC_ATTR_READNONE
);
1261 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1262 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1266 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1269 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1272 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1275 src0
= ac_to_float(ctx
, src0
);
1276 return LLVMBuildSExt(ctx
->builder
,
1277 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1281 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1284 return LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1287 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1290 return LLVMBuildSExt(ctx
->builder
,
1291 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1295 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1298 LLVMValueRef result
;
1299 LLVMValueRef cond
= NULL
;
1301 src0
= ac_to_float(&ctx
->ac
, src0
);
1302 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->ac
.f16
, "");
1304 if (ctx
->options
->chip_class
>= VI
) {
1305 LLVMValueRef args
[2];
1306 /* Check if the result is a denormal - and flush to 0 if so. */
1308 args
[1] = LLVMConstInt(ctx
->ac
.i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1309 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->ac
.i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1312 /* need to convert back up to f32 */
1313 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->ac
.f32
, "");
1315 if (ctx
->options
->chip_class
>= VI
)
1316 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1319 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1320 * so compare the result and flush to 0 if it's smaller.
1322 LLVMValueRef temp
, cond2
;
1323 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1324 ctx
->ac
.f32
, result
);
1325 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1326 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, 0x38800000, false), ctx
->ac
.f32
, ""),
1328 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1329 temp
, ctx
->ac
.f32_0
, "");
1330 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1331 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1336 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1337 LLVMValueRef src0
, LLVMValueRef src1
)
1339 LLVMValueRef dst64
, result
;
1340 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1341 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1343 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1344 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1345 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1349 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1350 LLVMValueRef src0
, LLVMValueRef src1
)
1352 LLVMValueRef dst64
, result
;
1353 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1354 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1356 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1357 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1358 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1362 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1364 const LLVMValueRef srcs
[3])
1366 LLVMValueRef result
;
1367 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1369 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1370 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1374 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1375 LLVMValueRef src0
, LLVMValueRef src1
,
1376 LLVMValueRef src2
, LLVMValueRef src3
)
1378 LLVMValueRef bfi_args
[3], result
;
1380 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1381 LLVMBuildSub(ctx
->builder
,
1382 LLVMBuildShl(ctx
->builder
,
1387 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1390 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1393 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1394 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1396 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1397 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1398 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1400 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1404 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1407 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1409 LLVMValueRef comp
[2];
1411 src0
= ac_to_float(ctx
, src0
);
1412 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1413 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1414 for (i
= 0; i
< 2; i
++) {
1415 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1416 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1417 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1420 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1421 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1426 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1429 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1430 LLVMValueRef temps
[2], result
, val
;
1433 for (i
= 0; i
< 2; i
++) {
1434 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1435 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1436 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1437 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1440 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1442 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1447 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1453 LLVMValueRef result
;
1455 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1456 mask
= AC_TID_MASK_LEFT
;
1457 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1458 mask
= AC_TID_MASK_TOP
;
1460 mask
= AC_TID_MASK_TOP_LEFT
;
1462 /* for DDX we want to next X pixel, DDY next Y pixel. */
1463 if (op
== nir_op_fddx_fine
||
1464 op
== nir_op_fddx_coarse
||
1470 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1475 * this takes an I,J coordinate pair,
1476 * and works out the X and Y derivatives.
1477 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1479 static LLVMValueRef
emit_ddxy_interp(
1480 struct ac_nir_context
*ctx
,
1481 LLVMValueRef interp_ij
)
1483 LLVMValueRef result
[4], a
;
1486 for (i
= 0; i
< 2; i
++) {
1487 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1488 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1489 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1490 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1492 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1495 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1497 LLVMValueRef src
[4], result
= NULL
;
1498 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1499 unsigned src_components
;
1500 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1502 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1503 switch (instr
->op
) {
1509 case nir_op_pack_half_2x16
:
1512 case nir_op_unpack_half_2x16
:
1516 src_components
= num_components
;
1519 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1520 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1522 switch (instr
->op
) {
1528 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1529 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1532 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1535 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1538 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1541 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1542 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1543 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1546 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1547 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1548 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1551 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1554 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1557 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1560 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1563 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1564 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1565 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1566 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1567 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1568 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1569 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1572 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1573 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1574 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1577 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1580 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1583 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1586 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1587 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1588 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1591 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1592 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1593 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1596 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1597 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, src
[0]);
1600 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1603 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1606 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1609 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1610 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1611 LLVMTypeOf(src
[0]), ""),
1615 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1616 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1617 LLVMTypeOf(src
[0]), ""),
1621 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1622 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1623 LLVMTypeOf(src
[0]), ""),
1627 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1630 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1633 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1636 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1639 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1642 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1645 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1648 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1651 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1654 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1657 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1658 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1661 result
= emit_iabs(&ctx
->ac
, src
[0]);
1664 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1667 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1670 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1673 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1676 result
= emit_isign(&ctx
->ac
, src
[0]);
1679 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1680 result
= emit_fsign(&ctx
->ac
, src
[0]);
1683 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1684 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1687 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1688 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1691 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1692 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1694 case nir_op_fround_even
:
1695 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1696 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1699 result
= emit_ffract(&ctx
->ac
, src
[0]);
1702 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1703 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1706 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1707 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1710 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1711 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1714 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1715 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1718 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1719 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1722 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1723 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1724 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, result
);
1727 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1728 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1731 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1732 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1733 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1734 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1735 ac_to_float_type(&ctx
->ac
, def_type
),
1739 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1740 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1741 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1742 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1743 ac_to_float_type(&ctx
->ac
, def_type
),
1747 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1748 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1750 case nir_op_ibitfield_extract
:
1751 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1753 case nir_op_ubitfield_extract
:
1754 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1756 case nir_op_bitfield_insert
:
1757 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1759 case nir_op_bitfield_reverse
:
1760 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1762 case nir_op_bit_count
:
1763 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1768 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1769 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1770 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1774 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1775 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1779 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1780 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1784 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1785 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1789 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1790 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1793 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1796 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1800 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1801 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1802 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1804 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1808 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1809 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1810 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1812 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1815 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1817 case nir_op_find_lsb
:
1818 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1819 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1821 case nir_op_ufind_msb
:
1822 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1823 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1825 case nir_op_ifind_msb
:
1826 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1827 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1829 case nir_op_uadd_carry
:
1830 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1831 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1832 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1834 case nir_op_usub_borrow
:
1835 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1836 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1837 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1840 result
= emit_b2f(&ctx
->ac
, src
[0]);
1843 result
= emit_f2b(&ctx
->ac
, src
[0]);
1846 result
= emit_b2i(&ctx
->ac
, src
[0]);
1849 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1850 result
= emit_i2b(&ctx
->ac
, src
[0]);
1852 case nir_op_fquantize2f16
:
1853 result
= emit_f2f16(ctx
->nctx
, src
[0]);
1855 case nir_op_umul_high
:
1856 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1857 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1858 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1860 case nir_op_imul_high
:
1861 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1862 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1863 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1865 case nir_op_pack_half_2x16
:
1866 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
1868 case nir_op_unpack_half_2x16
:
1869 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1873 case nir_op_fddx_fine
:
1874 case nir_op_fddy_fine
:
1875 case nir_op_fddx_coarse
:
1876 case nir_op_fddy_coarse
:
1877 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1880 case nir_op_unpack_64_2x32_split_x
: {
1881 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1882 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1885 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1890 case nir_op_unpack_64_2x32_split_y
: {
1891 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1892 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1895 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1900 case nir_op_pack_64_2x32_split
: {
1901 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1902 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1903 src
[0], ctx
->ac
.i32_0
, "");
1904 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1905 src
[1], ctx
->ac
.i32_1
, "");
1906 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1911 fprintf(stderr
, "Unknown NIR alu instr: ");
1912 nir_print_instr(&instr
->instr
, stderr
);
1913 fprintf(stderr
, "\n");
1918 assert(instr
->dest
.dest
.is_ssa
);
1919 result
= ac_to_integer(&ctx
->ac
, result
);
1920 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1925 static void visit_load_const(struct ac_nir_context
*ctx
,
1926 const nir_load_const_instr
*instr
)
1928 LLVMValueRef values
[4], value
= NULL
;
1929 LLVMTypeRef element_type
=
1930 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1932 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1933 switch (instr
->def
.bit_size
) {
1935 values
[i
] = LLVMConstInt(element_type
,
1936 instr
->value
.u32
[i
], false);
1939 values
[i
] = LLVMConstInt(element_type
,
1940 instr
->value
.u64
[i
], false);
1944 "unsupported nir load_const bit_size: %d\n",
1945 instr
->def
.bit_size
);
1949 if (instr
->def
.num_components
> 1) {
1950 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1954 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1957 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1960 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1961 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1962 LLVMPointerType(type
, addr_space
), "");
1966 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1969 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1970 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1973 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1974 /* On VI, the descriptor contains the size in bytes,
1975 * but TXQ must return the size in elements.
1976 * The stride is always non-zero for resources using TXQ.
1978 LLVMValueRef stride
=
1979 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1980 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1981 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1982 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1983 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1984 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1986 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1992 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1995 static void build_int_type_name(
1997 char *buf
, unsigned bufsize
)
1999 assert(bufsize
>= 6);
2001 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2002 snprintf(buf
, bufsize
, "v%ui32",
2003 LLVMGetVectorSize(type
));
2008 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2009 struct ac_image_args
*args
,
2010 const nir_tex_instr
*instr
)
2012 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2013 LLVMValueRef coord
= args
->addr
;
2014 LLVMValueRef half_texel
[2];
2015 LLVMValueRef compare_cube_wa
= NULL
;
2016 LLVMValueRef result
;
2018 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2022 struct ac_image_args txq_args
= { 0 };
2024 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2025 txq_args
.opcode
= ac_image_get_resinfo
;
2026 txq_args
.dmask
= 0xf;
2027 txq_args
.addr
= ctx
->i32_0
;
2028 txq_args
.resource
= args
->resource
;
2029 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2031 for (c
= 0; c
< 2; c
++) {
2032 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2033 LLVMConstInt(ctx
->i32
, c
, false), "");
2034 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2035 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2036 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2037 LLVMConstReal(ctx
->f32
, -0.5), "");
2041 LLVMValueRef orig_coords
= args
->addr
;
2043 for (c
= 0; c
< 2; c
++) {
2045 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2046 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2047 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2048 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2049 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2050 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2055 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2056 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2057 * workaround by sampling using a scaled type and converting.
2058 * This is taken from amdgpu-pro shaders.
2060 /* NOTE this produces some ugly code compared to amdgpu-pro,
2061 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2062 * and then reads them back. -pro generates two selects,
2063 * one s_cmp for the descriptor rewriting
2064 * one v_cmp for the coordinate and result changes.
2066 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2067 LLVMValueRef tmp
, tmp2
;
2069 /* workaround 8/8/8/8 uint/sint cube gather bug */
2070 /* first detect it then change to a scaled read and f2i */
2071 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2074 /* extract the DATA_FORMAT */
2075 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2076 LLVMConstInt(ctx
->i32
, 6, false), false);
2078 /* is the DATA_FORMAT == 8_8_8_8 */
2079 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2081 if (stype
== GLSL_TYPE_UINT
)
2082 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2083 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2084 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2086 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2087 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2088 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2090 /* replace the NUM FORMAT in the descriptor */
2091 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2092 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2094 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2096 /* don't modify the coordinates for this case */
2097 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2100 result
= ac_build_image_opcode(ctx
, args
);
2102 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2103 LLVMValueRef tmp
, tmp2
;
2105 /* if the cube workaround is in place, f2i the result. */
2106 for (c
= 0; c
< 4; c
++) {
2107 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2108 if (stype
== GLSL_TYPE_UINT
)
2109 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2111 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2112 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2113 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2114 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2115 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2116 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2122 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2123 const nir_tex_instr
*instr
,
2125 struct ac_image_args
*args
)
2127 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2128 return ac_build_buffer_load_format(&ctx
->ac
,
2131 LLVMConstInt(ctx
->ac
.i32
, 0, false),
2135 args
->opcode
= ac_image_sample
;
2136 args
->compare
= instr
->is_shadow
;
2138 switch (instr
->op
) {
2140 case nir_texop_txf_ms
:
2141 case nir_texop_samples_identical
:
2142 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2143 args
->compare
= false;
2144 args
->offset
= false;
2151 args
->level_zero
= true;
2156 case nir_texop_query_levels
:
2157 args
->opcode
= ac_image_get_resinfo
;
2160 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2161 args
->level_zero
= true;
2167 args
->opcode
= ac_image_gather4
;
2168 args
->level_zero
= true;
2171 args
->opcode
= ac_image_get_lod
;
2172 args
->compare
= false;
2173 args
->offset
= false;
2179 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2180 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2181 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2182 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2185 return ac_build_image_opcode(&ctx
->ac
, args
);
2188 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2189 nir_intrinsic_instr
*instr
)
2191 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2192 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2193 unsigned binding
= nir_intrinsic_binding(instr
);
2194 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2195 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2196 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2197 unsigned base_offset
= layout
->binding
[binding
].offset
;
2198 LLVMValueRef offset
, stride
;
2200 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2201 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2202 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2203 layout
->binding
[binding
].dynamic_offset_offset
;
2204 desc_ptr
= ctx
->push_constants
;
2205 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2206 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2208 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2210 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2211 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2212 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2214 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2215 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->ac
.v4i32
);
2216 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2218 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2221 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2222 nir_intrinsic_instr
*instr
)
2224 LLVMValueRef ptr
, addr
;
2226 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2227 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2229 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2230 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2232 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2235 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2236 const nir_intrinsic_instr
*instr
)
2238 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2240 return get_buffer_size(ctx
, desc
, false);
2242 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2243 nir_intrinsic_instr
*instr
)
2245 const char *store_name
;
2246 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2247 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2248 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2249 int components_32bit
= elem_size_mult
* instr
->num_components
;
2250 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2251 LLVMValueRef base_data
, base_offset
;
2252 LLVMValueRef params
[6];
2254 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2255 get_src(ctx
, instr
->src
[1]), true);
2256 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 0, false); /* vindex */
2257 params
[4] = ctx
->ac
.i1false
; /* glc */
2258 params
[5] = ctx
->ac
.i1false
; /* slc */
2260 if (components_32bit
> 1)
2261 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2263 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2264 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2265 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2267 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2271 LLVMValueRef offset
;
2273 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2275 /* Due to an LLVM limitation, split 3-element writes
2276 * into a 2-element and a 1-element write. */
2278 writemask
|= 1 << (start
+ 2);
2282 start
*= elem_size_mult
;
2283 count
*= elem_size_mult
;
2286 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2291 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2293 } else if (count
== 2) {
2294 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2295 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2296 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(ctx
->ac
.v2f32
), tmp
,
2299 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2300 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2301 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2303 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2307 if (get_llvm_num_components(base_data
) > 1)
2308 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2309 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2312 store_name
= "llvm.amdgcn.buffer.store.f32";
2315 offset
= base_offset
;
2317 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2321 ac_build_intrinsic(&ctx
->ac
, store_name
,
2322 ctx
->ac
.voidt
, params
, 6, 0);
2326 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2327 const nir_intrinsic_instr
*instr
)
2330 LLVMValueRef params
[6];
2333 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2334 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2336 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2337 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2338 get_src(ctx
, instr
->src
[0]),
2340 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i32
, 0, false); /* vindex */
2341 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2342 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2344 switch (instr
->intrinsic
) {
2345 case nir_intrinsic_ssbo_atomic_add
:
2346 name
= "llvm.amdgcn.buffer.atomic.add";
2348 case nir_intrinsic_ssbo_atomic_imin
:
2349 name
= "llvm.amdgcn.buffer.atomic.smin";
2351 case nir_intrinsic_ssbo_atomic_umin
:
2352 name
= "llvm.amdgcn.buffer.atomic.umin";
2354 case nir_intrinsic_ssbo_atomic_imax
:
2355 name
= "llvm.amdgcn.buffer.atomic.smax";
2357 case nir_intrinsic_ssbo_atomic_umax
:
2358 name
= "llvm.amdgcn.buffer.atomic.umax";
2360 case nir_intrinsic_ssbo_atomic_and
:
2361 name
= "llvm.amdgcn.buffer.atomic.and";
2363 case nir_intrinsic_ssbo_atomic_or
:
2364 name
= "llvm.amdgcn.buffer.atomic.or";
2366 case nir_intrinsic_ssbo_atomic_xor
:
2367 name
= "llvm.amdgcn.buffer.atomic.xor";
2369 case nir_intrinsic_ssbo_atomic_exchange
:
2370 name
= "llvm.amdgcn.buffer.atomic.swap";
2372 case nir_intrinsic_ssbo_atomic_comp_swap
:
2373 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2379 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2382 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2383 const nir_intrinsic_instr
*instr
)
2385 LLVMValueRef results
[2];
2386 int load_components
;
2387 int num_components
= instr
->num_components
;
2388 if (instr
->dest
.ssa
.bit_size
== 64)
2389 num_components
*= 2;
2391 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2392 load_components
= MIN2(num_components
- i
, 4);
2393 const char *load_name
;
2394 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2395 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2396 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2398 if (load_components
== 3)
2399 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2400 else if (load_components
> 1)
2401 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2403 if (load_components
>= 3)
2404 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2405 else if (load_components
== 2)
2406 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2407 else if (load_components
== 1)
2408 load_name
= "llvm.amdgcn.buffer.load.f32";
2410 unreachable("unhandled number of components");
2412 LLVMValueRef params
[] = {
2413 ctx
->abi
->load_ssbo(ctx
->abi
,
2414 get_src(ctx
, instr
->src
[0]),
2416 LLVMConstInt(ctx
->ac
.i32
, 0, false),
2422 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2427 LLVMValueRef ret
= results
[0];
2428 if (num_components
> 4 || num_components
== 3) {
2429 LLVMValueRef masks
[] = {
2430 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2431 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2432 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2433 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2436 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2437 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2438 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2441 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2442 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2445 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2446 const nir_intrinsic_instr
*instr
)
2448 LLVMValueRef results
[8], ret
;
2449 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2450 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2451 int num_components
= instr
->num_components
;
2453 if (ctx
->abi
->load_ubo
)
2454 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2456 if (instr
->dest
.ssa
.bit_size
== 64)
2457 num_components
*= 2;
2459 for (unsigned i
= 0; i
< num_components
; ++i
) {
2460 LLVMValueRef params
[] = {
2462 LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, 4 * i
, 0),
2465 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const.v4i32", ctx
->ac
.f32
,
2467 AC_FUNC_ATTR_READNONE
|
2468 AC_FUNC_ATTR_LEGACY
);
2472 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2473 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2474 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2478 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2479 bool vs_in
, unsigned *vertex_index_out
,
2480 LLVMValueRef
*vertex_index_ref
,
2481 unsigned *const_out
, LLVMValueRef
*indir_out
)
2483 unsigned const_offset
= 0;
2484 nir_deref
*tail
= &deref
->deref
;
2485 LLVMValueRef offset
= NULL
;
2487 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2489 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2490 if (vertex_index_out
)
2491 *vertex_index_out
= deref_array
->base_offset
;
2493 if (vertex_index_ref
) {
2494 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2495 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2496 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2498 *vertex_index_ref
= vtx
;
2502 if (deref
->var
->data
.compact
) {
2503 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2504 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2505 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2506 /* We always lower indirect dereferences for "compact" array vars. */
2507 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2509 const_offset
= deref_array
->base_offset
;
2513 while (tail
->child
!= NULL
) {
2514 const struct glsl_type
*parent_type
= tail
->type
;
2517 if (tail
->deref_type
== nir_deref_type_array
) {
2518 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2519 LLVMValueRef index
, stride
, local_offset
;
2520 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2522 const_offset
+= size
* deref_array
->base_offset
;
2523 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2526 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2527 index
= get_src(ctx
, deref_array
->indirect
);
2528 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2529 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2532 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2534 offset
= local_offset
;
2535 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2536 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2538 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2539 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2540 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2543 unreachable("unsupported deref type");
2547 if (const_offset
&& offset
)
2548 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2549 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2552 *const_out
= const_offset
;
2553 *indir_out
= offset
;
2557 /* The offchip buffer layout for TCS->TES is
2559 * - attribute 0 of patch 0 vertex 0
2560 * - attribute 0 of patch 0 vertex 1
2561 * - attribute 0 of patch 0 vertex 2
2563 * - attribute 0 of patch 1 vertex 0
2564 * - attribute 0 of patch 1 vertex 1
2566 * - attribute 1 of patch 0 vertex 0
2567 * - attribute 1 of patch 0 vertex 1
2569 * - per patch attribute 0 of patch 0
2570 * - per patch attribute 0 of patch 1
2573 * Note that every attribute has 4 components.
2575 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2576 LLVMValueRef vertex_index
,
2577 LLVMValueRef param_index
)
2579 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2580 LLVMValueRef param_stride
, constant16
;
2581 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2583 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2584 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2585 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2588 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2590 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2591 vertices_per_patch
, "");
2593 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2596 param_stride
= total_vertices
;
2598 base_addr
= rel_patch_id
;
2599 param_stride
= num_patches
;
2602 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2603 LLVMBuildMul(ctx
->builder
, param_index
,
2604 param_stride
, ""), "");
2606 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2608 if (!vertex_index
) {
2609 LLVMValueRef patch_data_offset
=
2610 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2612 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2613 patch_data_offset
, "");
2618 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2620 unsigned const_index
,
2622 LLVMValueRef vertex_index
,
2623 LLVMValueRef indir_index
)
2625 LLVMValueRef param_index
;
2628 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2631 if (const_index
&& !is_compact
)
2632 param
+= const_index
;
2633 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2635 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2639 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2640 bool is_patch
, uint32_t param
)
2644 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2646 ctx
->tess_outputs_written
|= (1ull << param
);
2650 get_dw_address(struct nir_to_llvm_context
*ctx
,
2651 LLVMValueRef dw_addr
,
2653 unsigned const_index
,
2654 bool compact_const_index
,
2655 LLVMValueRef vertex_index
,
2656 LLVMValueRef stride
,
2657 LLVMValueRef indir_index
)
2662 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2663 LLVMBuildMul(ctx
->builder
,
2669 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2670 LLVMBuildMul(ctx
->builder
, indir_index
,
2671 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2672 else if (const_index
&& !compact_const_index
)
2673 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2674 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2676 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2677 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2679 if (const_index
&& compact_const_index
)
2680 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2681 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2686 build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
2687 unsigned value_count
, unsigned component
)
2689 LLVMValueRef vec
= NULL
;
2691 if (value_count
== 1) {
2692 return values
[component
];
2693 } else if (!value_count
)
2694 unreachable("value_count is 0");
2696 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
2697 LLVMValueRef value
= values
[i
];
2700 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
2701 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
2702 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
2708 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2709 nir_intrinsic_instr
*instr
)
2711 LLVMValueRef dw_addr
, stride
;
2712 unsigned const_index
;
2713 LLVMValueRef vertex_index
;
2714 LLVMValueRef indir_index
;
2716 LLVMValueRef value
[4], result
;
2717 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2718 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2719 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2720 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2721 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2722 &const_index
, &indir_index
);
2724 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2725 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2726 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2729 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2730 for (unsigned i
= 0; i
< instr
->num_components
+ comp
; i
++) {
2731 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2732 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2735 result
= build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2736 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2741 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2742 nir_intrinsic_instr
*instr
)
2744 LLVMValueRef dw_addr
;
2745 LLVMValueRef stride
= NULL
;
2746 LLVMValueRef value
[4], result
;
2747 LLVMValueRef vertex_index
= NULL
;
2748 LLVMValueRef indir_index
= NULL
;
2749 unsigned const_index
= 0;
2751 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2752 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2753 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2754 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2755 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2756 &const_index
, &indir_index
);
2758 if (!instr
->variables
[0]->var
->data
.patch
) {
2759 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2760 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2762 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2765 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2768 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2769 for (unsigned i
= comp
; i
< instr
->num_components
+ comp
; i
++) {
2770 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2771 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2774 result
= build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2775 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2780 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2781 nir_intrinsic_instr
*instr
,
2785 LLVMValueRef dw_addr
;
2786 LLVMValueRef stride
= NULL
;
2787 LLVMValueRef buf_addr
= NULL
;
2788 LLVMValueRef vertex_index
= NULL
;
2789 LLVMValueRef indir_index
= NULL
;
2790 unsigned const_index
= 0;
2792 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2793 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2794 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2795 bool store_lds
= true;
2797 if (instr
->variables
[0]->var
->data
.patch
) {
2798 if (!(ctx
->tcs_patch_outputs_read
& (1U << instr
->variables
[0]->var
->data
.location
)))
2801 if (!(ctx
->tcs_outputs_read
& (1ULL << instr
->variables
[0]->var
->data
.location
)))
2804 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2805 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2806 &const_index
, &indir_index
);
2808 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2809 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2810 is_compact
&& const_index
> 3) {
2815 if (!instr
->variables
[0]->var
->data
.patch
) {
2816 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2817 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2819 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2822 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2824 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2826 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2827 vertex_index
, indir_index
);
2829 bool is_tess_factor
= false;
2830 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2831 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2832 is_tess_factor
= true;
2834 unsigned base
= is_compact
? const_index
: 0;
2835 for (unsigned chan
= 0; chan
< 8; chan
++) {
2836 if (!(writemask
& (1 << chan
)))
2838 LLVMValueRef value
= llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2840 if (store_lds
|| is_tess_factor
)
2841 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
2843 if (!is_tess_factor
&& writemask
!= 0xF)
2844 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2845 buf_addr
, ctx
->oc_lds
,
2846 4 * (base
+ chan
), 1, 0, true, false);
2848 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2852 if (writemask
== 0xF) {
2853 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2854 buf_addr
, ctx
->oc_lds
,
2855 (base
* 4), 1, 0, true, false);
2860 load_tes_input(struct nir_to_llvm_context
*ctx
,
2861 const nir_intrinsic_instr
*instr
)
2863 LLVMValueRef buf_addr
;
2864 LLVMValueRef result
;
2865 LLVMValueRef vertex_index
= NULL
;
2866 LLVMValueRef indir_index
= NULL
;
2867 unsigned const_index
= 0;
2869 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2870 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2872 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2873 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2874 &const_index
, &indir_index
);
2875 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2876 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2877 is_compact
&& const_index
> 3) {
2882 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2883 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2884 is_compact
, vertex_index
, indir_index
);
2886 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, comp
* 4, false);
2887 buf_addr
= LLVMBuildAdd(ctx
->builder
, buf_addr
, comp_offset
, "");
2889 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2890 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2891 result
= trim_vector(&ctx
->ac
, result
, instr
->num_components
);
2892 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2897 load_gs_input(struct nir_to_llvm_context
*ctx
,
2898 nir_intrinsic_instr
*instr
)
2900 LLVMValueRef indir_index
, vtx_offset
;
2901 unsigned const_index
;
2902 LLVMValueRef args
[9];
2903 unsigned param
, vtx_offset_param
;
2904 LLVMValueRef value
[4], result
;
2905 unsigned vertex_index
;
2906 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2907 false, &vertex_index
, NULL
,
2908 &const_index
, &indir_index
);
2909 vtx_offset_param
= vertex_index
;
2910 assert(vtx_offset_param
< 6);
2911 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2912 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
2914 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2916 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2917 for (unsigned i
= comp
; i
< instr
->num_components
+ comp
; i
++) {
2918 if (ctx
->ac
.chip_class
>= GFX9
) {
2919 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
2920 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
2921 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
2922 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2924 args
[0] = ctx
->esgs_ring
;
2925 args
[1] = vtx_offset
;
2926 args
[2] = LLVMConstInt(ctx
->ac
.i32
, (param
* 4 + i
+ const_index
) * 256, false);
2927 args
[3] = ctx
->ac
.i32_0
;
2928 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
2929 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
2930 args
[6] = ctx
->ac
.i32_1
; /* GLC */
2931 args
[7] = ctx
->ac
.i32_0
; /* SLC */
2932 args
[8] = ctx
->ac
.i32_0
; /* TFE */
2934 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2935 ctx
->ac
.i32
, args
, 9,
2936 AC_FUNC_ATTR_READONLY
|
2937 AC_FUNC_ATTR_LEGACY
);
2940 result
= build_varying_gather_values(&ctx
->ac
, value
, instr
->num_components
, comp
);
2946 build_gep_for_deref(struct ac_nir_context
*ctx
,
2947 nir_deref_var
*deref
)
2949 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
2950 assert(entry
->data
);
2951 LLVMValueRef val
= entry
->data
;
2952 nir_deref
*tail
= deref
->deref
.child
;
2953 while (tail
!= NULL
) {
2954 LLVMValueRef offset
;
2955 switch (tail
->deref_type
) {
2956 case nir_deref_type_array
: {
2957 nir_deref_array
*array
= nir_deref_as_array(tail
);
2958 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
2959 if (array
->deref_array_type
==
2960 nir_deref_array_type_indirect
) {
2961 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2968 case nir_deref_type_struct
: {
2969 nir_deref_struct
*deref_struct
=
2970 nir_deref_as_struct(tail
);
2971 offset
= LLVMConstInt(ctx
->ac
.i32
,
2972 deref_struct
->index
, 0);
2976 unreachable("bad deref type");
2978 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
2984 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2985 nir_intrinsic_instr
*instr
)
2987 LLVMValueRef values
[8];
2988 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2989 int ve
= instr
->dest
.ssa
.num_components
;
2990 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
2991 LLVMValueRef indir_index
;
2993 unsigned const_index
;
2994 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2995 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2996 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
2997 &const_index
, &indir_index
);
2999 if (instr
->dest
.ssa
.bit_size
== 64)
3002 switch (instr
->variables
[0]->var
->data
.mode
) {
3003 case nir_var_shader_in
:
3004 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3005 return load_tcs_input(ctx
->nctx
, instr
);
3006 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3007 return load_tes_input(ctx
->nctx
, instr
);
3008 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3009 return load_gs_input(ctx
->nctx
, instr
);
3012 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3014 unsigned count
= glsl_count_attribute_slots(
3015 instr
->variables
[0]->var
->type
,
3016 ctx
->stage
== MESA_SHADER_VERTEX
);
3018 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3019 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3022 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3026 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* 4];
3030 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3032 unsigned count
= glsl_count_attribute_slots(
3033 instr
->variables
[0]->var
->type
, false);
3035 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3036 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3039 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3043 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
3047 case nir_var_shared
: {
3048 LLVMValueRef address
= build_gep_for_deref(ctx
,
3049 instr
->variables
[0]);
3050 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3051 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3052 get_def_type(ctx
, &instr
->dest
.ssa
),
3055 case nir_var_shader_out
:
3056 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3057 return load_tcs_output(ctx
->nctx
, instr
);
3059 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3061 unsigned count
= glsl_count_attribute_slots(
3062 instr
->variables
[0]->var
->type
, false);
3064 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3065 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3068 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3072 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3073 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
3079 unreachable("unhandle variable mode");
3081 ret
= build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3082 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3086 visit_store_var(struct ac_nir_context
*ctx
,
3087 nir_intrinsic_instr
*instr
)
3089 LLVMValueRef temp_ptr
, value
;
3090 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3091 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3092 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3093 int writemask
= instr
->const_index
[0] << comp
;
3094 LLVMValueRef indir_index
;
3095 unsigned const_index
;
3096 get_deref_offset(ctx
, instr
->variables
[0], false,
3097 NULL
, NULL
, &const_index
, &indir_index
);
3099 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3100 int old_writemask
= writemask
;
3102 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3103 LLVMVectorType(ctx
->ac
.f32
, get_llvm_num_components(src
) * 2),
3107 for (unsigned chan
= 0; chan
< 4; chan
++) {
3108 if (old_writemask
& (1 << chan
))
3109 writemask
|= 3u << (2 * chan
);
3113 switch (instr
->variables
[0]->var
->data
.mode
) {
3114 case nir_var_shader_out
:
3116 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3117 store_tcs_output(ctx
->nctx
, instr
, src
, writemask
);
3121 for (unsigned chan
= 0; chan
< 8; chan
++) {
3123 if (!(writemask
& (1 << chan
)))
3126 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3128 if (instr
->variables
[0]->var
->data
.compact
)
3131 unsigned count
= glsl_count_attribute_slots(
3132 instr
->variables
[0]->var
->type
, false);
3134 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3135 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3136 stride
, true, true);
3138 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3139 value
, indir_index
, "");
3140 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3141 count
, stride
, tmp_vec
);
3144 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3146 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3151 for (unsigned chan
= 0; chan
< 8; chan
++) {
3152 if (!(writemask
& (1 << chan
)))
3155 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
3157 unsigned count
= glsl_count_attribute_slots(
3158 instr
->variables
[0]->var
->type
, false);
3160 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3161 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3164 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3165 value
, indir_index
, "");
3166 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3169 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3171 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3175 case nir_var_shared
: {
3176 int writemask
= instr
->const_index
[0];
3177 LLVMValueRef address
= build_gep_for_deref(ctx
,
3178 instr
->variables
[0]);
3179 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3180 unsigned components
=
3181 glsl_get_vector_elements(
3182 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3183 if (writemask
== (1 << components
) - 1) {
3184 val
= LLVMBuildBitCast(
3185 ctx
->ac
.builder
, val
,
3186 LLVMGetElementType(LLVMTypeOf(address
)), "");
3187 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3189 for (unsigned chan
= 0; chan
< 4; chan
++) {
3190 if (!(writemask
& (1 << chan
)))
3193 LLVMBuildStructGEP(ctx
->ac
.builder
,
3195 LLVMValueRef src
= llvm_extract_elem(&ctx
->ac
, val
,
3197 src
= LLVMBuildBitCast(
3198 ctx
->ac
.builder
, src
,
3199 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3200 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3210 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3213 case GLSL_SAMPLER_DIM_BUF
:
3215 case GLSL_SAMPLER_DIM_1D
:
3216 return array
? 2 : 1;
3217 case GLSL_SAMPLER_DIM_2D
:
3218 return array
? 3 : 2;
3219 case GLSL_SAMPLER_DIM_MS
:
3220 return array
? 4 : 3;
3221 case GLSL_SAMPLER_DIM_3D
:
3222 case GLSL_SAMPLER_DIM_CUBE
:
3224 case GLSL_SAMPLER_DIM_RECT
:
3225 case GLSL_SAMPLER_DIM_SUBPASS
:
3227 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3237 /* Adjust the sample index according to FMASK.
3239 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3240 * which is the identity mapping. Each nibble says which physical sample
3241 * should be fetched to get that sample.
3243 * For example, 0x11111100 means there are only 2 samples stored and
3244 * the second sample covers 3/4 of the pixel. When reading samples 0
3245 * and 1, return physical sample 0 (determined by the first two 0s
3246 * in FMASK), otherwise return physical sample 1.
3248 * The sample index should be adjusted as follows:
3249 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3251 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3252 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3253 LLVMValueRef coord_z
,
3254 LLVMValueRef sample_index
,
3255 LLVMValueRef fmask_desc_ptr
)
3257 LLVMValueRef fmask_load_address
[4];
3260 fmask_load_address
[0] = coord_x
;
3261 fmask_load_address
[1] = coord_y
;
3263 fmask_load_address
[2] = coord_z
;
3264 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3267 struct ac_image_args args
= {0};
3269 args
.opcode
= ac_image_load
;
3270 args
.da
= coord_z
? true : false;
3271 args
.resource
= fmask_desc_ptr
;
3273 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3275 res
= ac_build_image_opcode(ctx
, &args
);
3277 res
= ac_to_integer(ctx
, res
);
3278 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3279 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3281 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3285 LLVMValueRef sample_index4
=
3286 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3287 LLVMValueRef shifted_fmask
=
3288 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3289 LLVMValueRef final_sample
=
3290 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3292 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3293 * resource descriptor is 0 (invalid),
3295 LLVMValueRef fmask_desc
=
3296 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3299 LLVMValueRef fmask_word1
=
3300 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3303 LLVMValueRef word1_is_nonzero
=
3304 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3305 fmask_word1
, ctx
->i32_0
, "");
3307 /* Replace the MSAA sample index. */
3309 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3310 final_sample
, sample_index
, "");
3311 return sample_index
;
3314 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3315 const nir_intrinsic_instr
*instr
)
3317 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3318 if(instr
->variables
[0]->deref
.child
)
3319 type
= instr
->variables
[0]->deref
.child
->type
;
3321 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3322 LLVMValueRef coords
[4];
3323 LLVMValueRef masks
[] = {
3324 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3325 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3328 LLVMValueRef sample_index
= llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3331 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3332 bool is_array
= glsl_sampler_type_is_array(type
);
3333 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3334 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3335 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3336 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3337 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3338 count
= image_type_to_components_count(dim
, is_array
);
3341 LLVMValueRef fmask_load_address
[3];
3344 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3345 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3347 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3349 fmask_load_address
[2] = NULL
;
3351 for (chan
= 0; chan
< 2; ++chan
)
3352 fmask_load_address
[chan
] =
3353 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3354 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3355 ctx
->ac
.i32
, ""), "");
3356 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3358 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3359 fmask_load_address
[0],
3360 fmask_load_address
[1],
3361 fmask_load_address
[2],
3363 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3365 if (count
== 1 && !gfx9_1d
) {
3366 if (instr
->src
[0].ssa
->num_components
)
3367 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3374 for (chan
= 0; chan
< count
; ++chan
) {
3375 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3378 for (chan
= 0; chan
< 2; ++chan
)
3379 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3380 ctx
->ac
.i32
, ""), "");
3381 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3387 coords
[2] = coords
[1];
3388 coords
[1] = ctx
->ac
.i32_0
;
3390 coords
[1] = ctx
->ac
.i32_0
;
3395 coords
[count
] = sample_index
;
3400 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3403 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3408 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3409 const nir_intrinsic_instr
*instr
)
3411 LLVMValueRef params
[7];
3413 char intrinsic_name
[64];
3414 const nir_variable
*var
= instr
->variables
[0]->var
;
3415 const struct glsl_type
*type
= var
->type
;
3417 if(instr
->variables
[0]->deref
.child
)
3418 type
= instr
->variables
[0]->deref
.child
->type
;
3420 type
= glsl_without_array(type
);
3421 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3422 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3423 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3424 ctx
->ac
.i32_0
, ""); /* vindex */
3425 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3426 params
[3] = ctx
->ac
.i1false
; /* glc */
3427 params
[4] = ctx
->ac
.i1false
; /* slc */
3428 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3431 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3432 res
= ac_to_integer(&ctx
->ac
, res
);
3434 bool is_da
= glsl_sampler_type_is_array(type
) ||
3435 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3436 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3437 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3438 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3439 LLVMValueRef glc
= ctx
->ac
.i1false
;
3440 LLVMValueRef slc
= ctx
->ac
.i1false
;
3442 params
[0] = get_image_coords(ctx
, instr
);
3443 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3444 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3445 if (HAVE_LLVM
<= 0x0309) {
3446 params
[3] = ctx
->ac
.i1false
; /* r128 */
3451 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3458 ac_get_image_intr_name("llvm.amdgcn.image.load",
3459 ctx
->ac
.v4f32
, /* vdata */
3460 LLVMTypeOf(params
[0]), /* coords */
3461 LLVMTypeOf(params
[1]), /* rsrc */
3462 intrinsic_name
, sizeof(intrinsic_name
));
3464 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3465 params
, 7, AC_FUNC_ATTR_READONLY
);
3467 return ac_to_integer(&ctx
->ac
, res
);
3470 static void visit_image_store(struct ac_nir_context
*ctx
,
3471 nir_intrinsic_instr
*instr
)
3473 LLVMValueRef params
[8];
3474 char intrinsic_name
[64];
3475 const nir_variable
*var
= instr
->variables
[0]->var
;
3476 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3477 LLVMValueRef glc
= ctx
->ac
.i1false
;
3478 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3480 glc
= ctx
->ac
.i1true
;
3482 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3483 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3484 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3485 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3486 ctx
->ac
.i32_0
, ""); /* vindex */
3487 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3488 params
[4] = glc
; /* glc */
3489 params
[5] = ctx
->ac
.i1false
; /* slc */
3490 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3493 bool is_da
= glsl_sampler_type_is_array(type
) ||
3494 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3495 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3496 LLVMValueRef slc
= ctx
->ac
.i1false
;
3498 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3499 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3500 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3501 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3502 if (HAVE_LLVM
<= 0x0309) {
3503 params
[4] = ctx
->ac
.i1false
; /* r128 */
3508 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3515 ac_get_image_intr_name("llvm.amdgcn.image.store",
3516 LLVMTypeOf(params
[0]), /* vdata */
3517 LLVMTypeOf(params
[1]), /* coords */
3518 LLVMTypeOf(params
[2]), /* rsrc */
3519 intrinsic_name
, sizeof(intrinsic_name
));
3521 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3527 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3528 const nir_intrinsic_instr
*instr
)
3530 LLVMValueRef params
[7];
3531 int param_count
= 0;
3532 const nir_variable
*var
= instr
->variables
[0]->var
;
3534 const char *atomic_name
;
3535 char intrinsic_name
[41];
3536 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3537 MAYBE_UNUSED
int length
;
3539 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3541 switch (instr
->intrinsic
) {
3542 case nir_intrinsic_image_atomic_add
:
3543 atomic_name
= "add";
3545 case nir_intrinsic_image_atomic_min
:
3546 atomic_name
= is_unsigned
? "umin" : "smin";
3548 case nir_intrinsic_image_atomic_max
:
3549 atomic_name
= is_unsigned
? "umax" : "smax";
3551 case nir_intrinsic_image_atomic_and
:
3552 atomic_name
= "and";
3554 case nir_intrinsic_image_atomic_or
:
3557 case nir_intrinsic_image_atomic_xor
:
3558 atomic_name
= "xor";
3560 case nir_intrinsic_image_atomic_exchange
:
3561 atomic_name
= "swap";
3563 case nir_intrinsic_image_atomic_comp_swap
:
3564 atomic_name
= "cmpswap";
3570 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3571 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3572 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3574 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3575 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3577 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3578 ctx
->ac
.i32_0
, ""); /* vindex */
3579 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3580 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3582 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3583 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3585 char coords_type
[8];
3587 bool da
= glsl_sampler_type_is_array(type
) ||
3588 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3590 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3591 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3593 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3594 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3595 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3597 build_int_type_name(LLVMTypeOf(coords
),
3598 coords_type
, sizeof(coords_type
));
3600 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3601 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3604 assert(length
< sizeof(intrinsic_name
));
3605 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3608 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3609 const nir_intrinsic_instr
*instr
)
3612 const nir_variable
*var
= instr
->variables
[0]->var
;
3613 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3614 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3615 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3616 if(instr
->variables
[0]->deref
.child
)
3617 type
= instr
->variables
[0]->deref
.child
->type
;
3619 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3620 return get_buffer_size(ctx
,
3621 get_sampler_desc(ctx
, instr
->variables
[0],
3622 AC_DESC_BUFFER
, NULL
, true, false), true);
3624 struct ac_image_args args
= { 0 };
3628 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3629 args
.opcode
= ac_image_get_resinfo
;
3630 args
.addr
= ctx
->ac
.i32_0
;
3632 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3634 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3636 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3637 glsl_sampler_type_is_array(type
)) {
3638 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3639 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3640 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3641 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3643 if (ctx
->ac
.chip_class
>= GFX9
&&
3644 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3645 glsl_sampler_type_is_array(type
)) {
3646 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3647 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3654 #define NOOP_WAITCNT 0xf7f
3655 #define LGKM_CNT 0x07f
3656 #define VM_CNT 0xf70
3658 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3661 LLVMValueRef args
[1] = {
3662 LLVMConstInt(ctx
->ac
.i32
, simm16
, false),
3664 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3665 ctx
->ac
.voidt
, args
, 1, 0);
3668 static void emit_membar(struct nir_to_llvm_context
*ctx
,
3669 const nir_intrinsic_instr
*instr
)
3671 unsigned waitcnt
= NOOP_WAITCNT
;
3673 switch (instr
->intrinsic
) {
3674 case nir_intrinsic_memory_barrier
:
3675 case nir_intrinsic_group_memory_barrier
:
3676 waitcnt
&= VM_CNT
& LGKM_CNT
;
3678 case nir_intrinsic_memory_barrier_atomic_counter
:
3679 case nir_intrinsic_memory_barrier_buffer
:
3680 case nir_intrinsic_memory_barrier_image
:
3683 case nir_intrinsic_memory_barrier_shared
:
3684 waitcnt
&= LGKM_CNT
;
3689 if (waitcnt
!= NOOP_WAITCNT
)
3690 emit_waitcnt(ctx
, waitcnt
);
3693 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3695 /* SI only (thanks to a hw bug workaround):
3696 * The real barrier instruction isn’t needed, because an entire patch
3697 * always fits into a single wave.
3699 if (ctx
->options
->chip_class
== SI
&&
3700 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3701 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3704 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3705 ctx
->ac
.voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3708 static void emit_discard_if(struct ac_nir_context
*ctx
,
3709 const nir_intrinsic_instr
*instr
)
3713 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3714 get_src(ctx
, instr
->src
[0]),
3716 ac_build_kill_if_false(&ctx
->ac
, cond
);
3720 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3722 LLVMValueRef result
;
3723 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3724 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3725 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3727 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3730 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3731 const nir_intrinsic_instr
*instr
)
3733 LLVMValueRef ptr
, result
;
3734 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3735 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3737 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3738 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3739 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3741 LLVMAtomicOrderingSequentiallyConsistent
,
3742 LLVMAtomicOrderingSequentiallyConsistent
,
3745 LLVMAtomicRMWBinOp op
;
3746 switch (instr
->intrinsic
) {
3747 case nir_intrinsic_var_atomic_add
:
3748 op
= LLVMAtomicRMWBinOpAdd
;
3750 case nir_intrinsic_var_atomic_umin
:
3751 op
= LLVMAtomicRMWBinOpUMin
;
3753 case nir_intrinsic_var_atomic_umax
:
3754 op
= LLVMAtomicRMWBinOpUMax
;
3756 case nir_intrinsic_var_atomic_imin
:
3757 op
= LLVMAtomicRMWBinOpMin
;
3759 case nir_intrinsic_var_atomic_imax
:
3760 op
= LLVMAtomicRMWBinOpMax
;
3762 case nir_intrinsic_var_atomic_and
:
3763 op
= LLVMAtomicRMWBinOpAnd
;
3765 case nir_intrinsic_var_atomic_or
:
3766 op
= LLVMAtomicRMWBinOpOr
;
3768 case nir_intrinsic_var_atomic_xor
:
3769 op
= LLVMAtomicRMWBinOpXor
;
3771 case nir_intrinsic_var_atomic_exchange
:
3772 op
= LLVMAtomicRMWBinOpXchg
;
3778 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3779 LLVMAtomicOrderingSequentiallyConsistent
,
3785 #define INTERP_CENTER 0
3786 #define INTERP_CENTROID 1
3787 #define INTERP_SAMPLE 2
3789 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3790 enum glsl_interp_mode interp
, unsigned location
)
3793 case INTERP_MODE_FLAT
:
3796 case INTERP_MODE_SMOOTH
:
3797 case INTERP_MODE_NONE
:
3798 if (location
== INTERP_CENTER
)
3799 return ctx
->persp_center
;
3800 else if (location
== INTERP_CENTROID
)
3801 return ctx
->persp_centroid
;
3802 else if (location
== INTERP_SAMPLE
)
3803 return ctx
->persp_sample
;
3805 case INTERP_MODE_NOPERSPECTIVE
:
3806 if (location
== INTERP_CENTER
)
3807 return ctx
->linear_center
;
3808 else if (location
== INTERP_CENTROID
)
3809 return ctx
->linear_centroid
;
3810 else if (location
== INTERP_SAMPLE
)
3811 return ctx
->linear_sample
;
3817 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3818 LLVMValueRef sample_id
)
3820 LLVMValueRef result
;
3821 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
3823 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3824 const_array(ctx
->ac
.v2f32
, 64), "");
3826 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3827 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
3832 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3834 LLVMValueRef values
[2];
3836 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3837 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3838 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3841 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3842 const nir_intrinsic_instr
*instr
)
3844 LLVMValueRef result
[4];
3845 LLVMValueRef interp_param
, attr_number
;
3848 LLVMValueRef src_c0
= NULL
;
3849 LLVMValueRef src_c1
= NULL
;
3850 LLVMValueRef src0
= NULL
;
3851 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3852 switch (instr
->intrinsic
) {
3853 case nir_intrinsic_interp_var_at_centroid
:
3854 location
= INTERP_CENTROID
;
3856 case nir_intrinsic_interp_var_at_sample
:
3857 case nir_intrinsic_interp_var_at_offset
:
3858 location
= INTERP_CENTER
;
3859 src0
= get_src(ctx
->nir
, instr
->src
[0]);
3865 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3866 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_0
, ""));
3867 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_1
, ""));
3868 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3869 LLVMValueRef sample_position
;
3870 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3872 /* fetch sample ID */
3873 sample_position
= load_sample_position(ctx
, src0
);
3875 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_0
, "");
3876 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3877 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_1
, "");
3878 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3880 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3881 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
3883 if (location
== INTERP_CENTER
) {
3884 LLVMValueRef ij_out
[2];
3885 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
3888 * take the I then J parameters, and the DDX/Y for it, and
3889 * calculate the IJ inputs for the interpolator.
3890 * temp1 = ddx * offset/sample.x + I;
3891 * interp_param.I = ddy * offset/sample.y + temp1;
3892 * temp1 = ddx * offset/sample.x + J;
3893 * interp_param.J = ddy * offset/sample.y + temp1;
3895 for (unsigned i
= 0; i
< 2; i
++) {
3896 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3897 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3898 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3899 ddxy_out
, ix_ll
, "");
3900 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3901 ddxy_out
, iy_ll
, "");
3902 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3903 interp_param
, ix_ll
, "");
3904 LLVMValueRef temp1
, temp2
;
3906 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3909 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3910 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3912 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3913 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3915 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3916 temp2
, ctx
->ac
.i32
, "");
3918 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3922 for (chan
= 0; chan
< 4; chan
++) {
3923 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3926 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3927 interp_param
, ctx
->ac
.v2f32
, "");
3928 LLVMValueRef i
= LLVMBuildExtractElement(
3929 ctx
->builder
, interp_param
, ctx
->ac
.i32_0
, "");
3930 LLVMValueRef j
= LLVMBuildExtractElement(
3931 ctx
->builder
, interp_param
, ctx
->ac
.i32_1
, "");
3933 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3934 llvm_chan
, attr_number
,
3935 ctx
->prim_mask
, i
, j
);
3937 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3938 LLVMConstInt(ctx
->ac
.i32
, 2, false),
3939 llvm_chan
, attr_number
,
3943 return build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3944 instr
->variables
[0]->var
->data
.location_frac
);
3948 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
3950 LLVMValueRef gs_next_vertex
;
3951 LLVMValueRef can_emit
;
3953 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3955 /* Write vertex attribute values to GSVS ring */
3956 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3957 ctx
->gs_next_vertex
,
3960 /* If this thread has already emitted the declared maximum number of
3961 * vertices, kill it: excessive vertex emissions are not supposed to
3962 * have any effect, and GS threads have no externally observable
3963 * effects other than emitting vertices.
3965 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3966 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
3967 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
3969 /* loop num outputs */
3971 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3972 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
3977 if (!(ctx
->output_mask
& (1ull << i
)))
3980 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3981 /* pack clip and cull into a single set of slots */
3982 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3986 for (unsigned j
= 0; j
< length
; j
++) {
3987 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3989 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3990 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3991 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3993 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
3995 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3997 voffset
, ctx
->gs2vs_offset
, 0,
4003 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4005 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4007 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4011 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
4012 const nir_intrinsic_instr
*instr
)
4014 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4018 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
4019 const nir_intrinsic_instr
*instr
)
4021 LLVMValueRef coord
[4] = {
4028 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4029 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->ac
.f32_1
,
4030 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4032 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
4033 return LLVMBuildBitCast(ctx
->builder
, result
,
4034 get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
4037 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4038 nir_intrinsic_instr
*instr
)
4040 LLVMValueRef result
= NULL
;
4042 switch (instr
->intrinsic
) {
4043 case nir_intrinsic_load_work_group_id
: {
4044 result
= ctx
->nctx
->workgroup_ids
;
4047 case nir_intrinsic_load_base_vertex
: {
4048 result
= ctx
->abi
->base_vertex
;
4051 case nir_intrinsic_load_vertex_id_zero_base
: {
4052 result
= ctx
->abi
->vertex_id
;
4055 case nir_intrinsic_load_local_invocation_id
: {
4056 result
= ctx
->nctx
->local_invocation_ids
;
4059 case nir_intrinsic_load_base_instance
:
4060 result
= ctx
->abi
->start_instance
;
4062 case nir_intrinsic_load_draw_id
:
4063 result
= ctx
->abi
->draw_id
;
4065 case nir_intrinsic_load_view_index
:
4066 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4068 case nir_intrinsic_load_invocation_id
:
4069 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4070 result
= unpack_param(&ctx
->ac
, ctx
->nctx
->tcs_rel_ids
, 8, 5);
4072 result
= ctx
->abi
->gs_invocation_id
;
4074 case nir_intrinsic_load_primitive_id
:
4075 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4077 ctx
->nctx
->shader_info
->gs
.uses_prim_id
= true;
4078 result
= ctx
->abi
->gs_prim_id
;
4079 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4080 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4081 result
= ctx
->nctx
->tcs_patch_id
;
4082 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4083 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4084 result
= ctx
->nctx
->tes_patch_id
;
4086 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4088 case nir_intrinsic_load_sample_id
:
4089 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4091 case nir_intrinsic_load_sample_pos
:
4092 result
= load_sample_pos(ctx
);
4094 case nir_intrinsic_load_sample_mask_in
:
4095 result
= ctx
->abi
->sample_coverage
;
4097 case nir_intrinsic_load_frag_coord
: {
4098 LLVMValueRef values
[4] = {
4099 ctx
->abi
->frag_pos
[0],
4100 ctx
->abi
->frag_pos
[1],
4101 ctx
->abi
->frag_pos
[2],
4102 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4104 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4107 case nir_intrinsic_load_front_face
:
4108 result
= ctx
->abi
->front_face
;
4110 case nir_intrinsic_load_instance_id
:
4111 result
= ctx
->abi
->instance_id
;
4113 case nir_intrinsic_load_num_work_groups
:
4114 result
= ctx
->nctx
->num_work_groups
;
4116 case nir_intrinsic_load_local_invocation_index
:
4117 result
= visit_load_local_invocation_index(ctx
->nctx
);
4119 case nir_intrinsic_load_push_constant
:
4120 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4122 case nir_intrinsic_vulkan_resource_index
:
4123 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4125 case nir_intrinsic_store_ssbo
:
4126 visit_store_ssbo(ctx
, instr
);
4128 case nir_intrinsic_load_ssbo
:
4129 result
= visit_load_buffer(ctx
, instr
);
4131 case nir_intrinsic_ssbo_atomic_add
:
4132 case nir_intrinsic_ssbo_atomic_imin
:
4133 case nir_intrinsic_ssbo_atomic_umin
:
4134 case nir_intrinsic_ssbo_atomic_imax
:
4135 case nir_intrinsic_ssbo_atomic_umax
:
4136 case nir_intrinsic_ssbo_atomic_and
:
4137 case nir_intrinsic_ssbo_atomic_or
:
4138 case nir_intrinsic_ssbo_atomic_xor
:
4139 case nir_intrinsic_ssbo_atomic_exchange
:
4140 case nir_intrinsic_ssbo_atomic_comp_swap
:
4141 result
= visit_atomic_ssbo(ctx
, instr
);
4143 case nir_intrinsic_load_ubo
:
4144 result
= visit_load_ubo_buffer(ctx
, instr
);
4146 case nir_intrinsic_get_buffer_size
:
4147 result
= visit_get_buffer_size(ctx
, instr
);
4149 case nir_intrinsic_load_var
:
4150 result
= visit_load_var(ctx
, instr
);
4152 case nir_intrinsic_store_var
:
4153 visit_store_var(ctx
, instr
);
4155 case nir_intrinsic_image_load
:
4156 result
= visit_image_load(ctx
, instr
);
4158 case nir_intrinsic_image_store
:
4159 visit_image_store(ctx
, instr
);
4161 case nir_intrinsic_image_atomic_add
:
4162 case nir_intrinsic_image_atomic_min
:
4163 case nir_intrinsic_image_atomic_max
:
4164 case nir_intrinsic_image_atomic_and
:
4165 case nir_intrinsic_image_atomic_or
:
4166 case nir_intrinsic_image_atomic_xor
:
4167 case nir_intrinsic_image_atomic_exchange
:
4168 case nir_intrinsic_image_atomic_comp_swap
:
4169 result
= visit_image_atomic(ctx
, instr
);
4171 case nir_intrinsic_image_size
:
4172 result
= visit_image_size(ctx
, instr
);
4174 case nir_intrinsic_discard
:
4175 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4176 LLVMVoidTypeInContext(ctx
->ac
.context
),
4177 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4179 case nir_intrinsic_discard_if
:
4180 emit_discard_if(ctx
, instr
);
4182 case nir_intrinsic_memory_barrier
:
4183 case nir_intrinsic_group_memory_barrier
:
4184 case nir_intrinsic_memory_barrier_atomic_counter
:
4185 case nir_intrinsic_memory_barrier_buffer
:
4186 case nir_intrinsic_memory_barrier_image
:
4187 case nir_intrinsic_memory_barrier_shared
:
4188 emit_membar(ctx
->nctx
, instr
);
4190 case nir_intrinsic_barrier
:
4191 emit_barrier(ctx
->nctx
);
4193 case nir_intrinsic_var_atomic_add
:
4194 case nir_intrinsic_var_atomic_imin
:
4195 case nir_intrinsic_var_atomic_umin
:
4196 case nir_intrinsic_var_atomic_imax
:
4197 case nir_intrinsic_var_atomic_umax
:
4198 case nir_intrinsic_var_atomic_and
:
4199 case nir_intrinsic_var_atomic_or
:
4200 case nir_intrinsic_var_atomic_xor
:
4201 case nir_intrinsic_var_atomic_exchange
:
4202 case nir_intrinsic_var_atomic_comp_swap
:
4203 result
= visit_var_atomic(ctx
->nctx
, instr
);
4205 case nir_intrinsic_interp_var_at_centroid
:
4206 case nir_intrinsic_interp_var_at_sample
:
4207 case nir_intrinsic_interp_var_at_offset
:
4208 result
= visit_interp(ctx
->nctx
, instr
);
4210 case nir_intrinsic_emit_vertex
:
4211 assert(instr
->const_index
[0] == 0);
4212 ctx
->abi
->emit_vertex(ctx
->abi
, 0, ctx
->outputs
);
4214 case nir_intrinsic_end_primitive
:
4215 visit_end_primitive(ctx
->nctx
, instr
);
4217 case nir_intrinsic_load_tess_coord
:
4218 result
= visit_load_tess_coord(ctx
->nctx
, instr
);
4220 case nir_intrinsic_load_patch_vertices_in
:
4221 result
= LLVMConstInt(ctx
->ac
.i32
, ctx
->nctx
->options
->key
.tcs
.input_vertices
, false);
4224 fprintf(stderr
, "Unknown intrinsic: ");
4225 nir_print_instr(&instr
->instr
, stderr
);
4226 fprintf(stderr
, "\n");
4230 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4234 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4235 LLVMValueRef buffer
, bool write
)
4237 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4239 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4240 ctx
->shader_info
->fs
.writes_memory
= true;
4245 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4246 unsigned descriptor_set
,
4247 unsigned base_index
,
4248 unsigned constant_index
,
4250 enum ac_descriptor_type desc_type
,
4251 bool image
, bool write
)
4253 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4254 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4255 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4256 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4257 unsigned offset
= binding
->offset
;
4258 unsigned stride
= binding
->size
;
4260 LLVMBuilderRef builder
= ctx
->builder
;
4263 assert(base_index
< layout
->binding_count
);
4265 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4266 ctx
->shader_info
->fs
.writes_memory
= true;
4268 switch (desc_type
) {
4270 type
= ctx
->ac
.v8i32
;
4274 type
= ctx
->ac
.v8i32
;
4278 case AC_DESC_SAMPLER
:
4279 type
= ctx
->ac
.v4i32
;
4280 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4285 case AC_DESC_BUFFER
:
4286 type
= ctx
->ac
.v4i32
;
4290 unreachable("invalid desc_type\n");
4293 offset
+= constant_index
* stride
;
4295 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4296 (!index
|| binding
->immutable_samplers_equal
)) {
4297 if (binding
->immutable_samplers_equal
)
4300 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4302 LLVMValueRef constants
[] = {
4303 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4304 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4305 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4306 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4308 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4311 assert(stride
% type_size
== 0);
4314 index
= ctx
->ac
.i32_0
;
4316 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4318 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4319 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4321 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4324 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4325 const nir_deref_var
*deref
,
4326 enum ac_descriptor_type desc_type
,
4327 const nir_tex_instr
*tex_instr
,
4328 bool image
, bool write
)
4330 LLVMValueRef index
= NULL
;
4331 unsigned constant_index
= 0;
4332 unsigned descriptor_set
;
4333 unsigned base_index
;
4336 assert(tex_instr
&& !image
);
4338 base_index
= tex_instr
->sampler_index
;
4340 const nir_deref
*tail
= &deref
->deref
;
4341 while (tail
->child
) {
4342 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4343 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4348 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4350 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4351 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4353 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4354 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4359 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4362 constant_index
+= child
->base_offset
* array_size
;
4364 tail
= &child
->deref
;
4366 descriptor_set
= deref
->var
->data
.descriptor_set
;
4367 base_index
= deref
->var
->data
.binding
;
4370 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4373 constant_index
, index
,
4374 desc_type
, image
, write
);
4377 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4378 struct ac_image_args
*args
,
4379 const nir_tex_instr
*instr
,
4381 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4382 LLVMValueRef
*param
, unsigned count
,
4385 unsigned is_rect
= 0;
4386 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4388 if (op
== nir_texop_lod
)
4390 /* Pad to power of two vector */
4391 while (count
< util_next_power_of_two(count
))
4392 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4395 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4397 args
->addr
= param
[0];
4399 args
->resource
= res_ptr
;
4400 args
->sampler
= samp_ptr
;
4402 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4403 args
->addr
= param
[0];
4407 args
->dmask
= dmask
;
4408 args
->unorm
= is_rect
;
4412 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4415 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4416 * filtering manually. The driver sets img7 to a mask clearing
4417 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4418 * s_and_b32 samp0, samp0, img7
4421 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4423 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4424 LLVMValueRef res
, LLVMValueRef samp
)
4426 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4427 LLVMValueRef img7
, samp0
;
4429 if (ctx
->ac
.chip_class
>= VI
)
4432 img7
= LLVMBuildExtractElement(builder
, res
,
4433 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4434 samp0
= LLVMBuildExtractElement(builder
, samp
,
4435 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4436 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4437 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4438 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4441 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4442 nir_tex_instr
*instr
,
4443 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4444 LLVMValueRef
*fmask_ptr
)
4446 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4447 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4449 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4452 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4454 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4455 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4456 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4458 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4459 instr
->op
== nir_texop_samples_identical
))
4460 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4463 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4466 coord
= ac_to_float(ctx
, coord
);
4467 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4468 coord
= ac_to_integer(ctx
, coord
);
4472 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4474 LLVMValueRef result
= NULL
;
4475 struct ac_image_args args
= { 0 };
4476 unsigned dmask
= 0xf;
4477 LLVMValueRef address
[16];
4478 LLVMValueRef coords
[5];
4479 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4480 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4481 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4482 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4483 LLVMValueRef derivs
[6];
4484 unsigned chan
, count
= 0;
4485 unsigned const_src
= 0, num_deriv_comp
= 0;
4486 bool lod_is_zero
= false;
4488 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4490 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4491 switch (instr
->src
[i
].src_type
) {
4492 case nir_tex_src_coord
:
4493 coord
= get_src(ctx
, instr
->src
[i
].src
);
4495 case nir_tex_src_projector
:
4497 case nir_tex_src_comparator
:
4498 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4500 case nir_tex_src_offset
:
4501 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4504 case nir_tex_src_bias
:
4505 bias
= get_src(ctx
, instr
->src
[i
].src
);
4507 case nir_tex_src_lod
: {
4508 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4510 if (val
&& val
->i32
[0] == 0)
4512 lod
= get_src(ctx
, instr
->src
[i
].src
);
4515 case nir_tex_src_ms_index
:
4516 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4518 case nir_tex_src_ms_mcs
:
4520 case nir_tex_src_ddx
:
4521 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4522 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4524 case nir_tex_src_ddy
:
4525 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4527 case nir_tex_src_texture_offset
:
4528 case nir_tex_src_sampler_offset
:
4529 case nir_tex_src_plane
:
4535 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4536 result
= get_buffer_size(ctx
, res_ptr
, true);
4540 if (instr
->op
== nir_texop_texture_samples
) {
4541 LLVMValueRef res
, samples
, is_msaa
;
4542 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4543 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4544 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4545 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4546 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4547 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4548 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4549 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4550 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4552 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4553 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4554 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4555 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4556 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4558 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4565 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4566 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4568 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4569 LLVMValueRef offset
[3], pack
;
4570 for (chan
= 0; chan
< 3; ++chan
)
4571 offset
[chan
] = ctx
->ac
.i32_0
;
4574 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4575 offset
[chan
] = llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4576 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4577 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4579 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4580 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4582 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4583 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4584 address
[count
++] = pack
;
4587 /* pack LOD bias value */
4588 if (instr
->op
== nir_texop_txb
&& bias
) {
4589 address
[count
++] = bias
;
4592 /* Pack depth comparison value */
4593 if (instr
->is_shadow
&& comparator
) {
4594 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4595 llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4597 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
4598 * so the depth comparison value isn't clamped for Z16 and
4599 * Z24 anymore. Do it manually here.
4601 * It's unnecessary if the original texture format was
4602 * Z32_FLOAT, but we don't know that here.
4604 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
4605 z
= ac_build_clamp(&ctx
->ac
, z
);
4607 address
[count
++] = z
;
4610 /* pack derivatives */
4612 int num_src_deriv_channels
, num_dest_deriv_channels
;
4613 switch (instr
->sampler_dim
) {
4614 case GLSL_SAMPLER_DIM_3D
:
4615 case GLSL_SAMPLER_DIM_CUBE
:
4617 num_src_deriv_channels
= 3;
4618 num_dest_deriv_channels
= 3;
4620 case GLSL_SAMPLER_DIM_2D
:
4622 num_src_deriv_channels
= 2;
4623 num_dest_deriv_channels
= 2;
4626 case GLSL_SAMPLER_DIM_1D
:
4627 num_src_deriv_channels
= 1;
4628 if (ctx
->ac
.chip_class
>= GFX9
) {
4629 num_dest_deriv_channels
= 2;
4632 num_dest_deriv_channels
= 1;
4638 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4639 derivs
[i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4640 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4642 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4643 derivs
[i
] = ctx
->ac
.f32_0
;
4644 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4648 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4649 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4650 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4651 if (instr
->coord_components
== 3)
4652 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4653 ac_prepare_cube_coords(&ctx
->ac
,
4654 instr
->op
== nir_texop_txd
, instr
->is_array
,
4655 instr
->op
== nir_texop_lod
, coords
, derivs
);
4661 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4662 address
[count
++] = derivs
[i
];
4665 /* Pack texture coordinates */
4667 address
[count
++] = coords
[0];
4668 if (instr
->coord_components
> 1) {
4669 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4670 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4672 address
[count
++] = coords
[1];
4674 if (instr
->coord_components
> 2) {
4675 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4676 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4677 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4678 instr
->op
!= nir_texop_txf
) {
4679 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4681 address
[count
++] = coords
[2];
4684 if (ctx
->ac
.chip_class
>= GFX9
) {
4685 LLVMValueRef filler
;
4686 if (instr
->op
== nir_texop_txf
)
4687 filler
= ctx
->ac
.i32_0
;
4689 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4691 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4692 /* No nir_texop_lod, because it does not take a slice
4693 * even with array textures. */
4694 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
4695 address
[count
] = address
[count
- 1];
4696 address
[count
- 1] = filler
;
4699 address
[count
++] = filler
;
4705 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4706 instr
->op
== nir_texop_txf
)) {
4707 address
[count
++] = lod
;
4708 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4709 address
[count
++] = sample_index
;
4710 } else if(instr
->op
== nir_texop_txs
) {
4713 address
[count
++] = lod
;
4715 address
[count
++] = ctx
->ac
.i32_0
;
4718 for (chan
= 0; chan
< count
; chan
++) {
4719 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4720 address
[chan
], ctx
->ac
.i32
, "");
4723 if (instr
->op
== nir_texop_samples_identical
) {
4724 LLVMValueRef txf_address
[4];
4725 struct ac_image_args txf_args
= { 0 };
4726 unsigned txf_count
= count
;
4727 memcpy(txf_address
, address
, sizeof(txf_address
));
4729 if (!instr
->is_array
)
4730 txf_address
[2] = ctx
->ac
.i32_0
;
4731 txf_address
[3] = ctx
->ac
.i32_0
;
4733 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4735 txf_address
, txf_count
, 0xf);
4737 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4739 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4740 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4744 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4745 instr
->op
!= nir_texop_txs
) {
4746 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4747 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4750 instr
->is_array
? address
[2] : NULL
,
4751 address
[sample_chan
],
4755 if (offsets
&& instr
->op
== nir_texop_txf
) {
4756 nir_const_value
*const_offset
=
4757 nir_src_as_const_value(instr
->src
[const_src
].src
);
4758 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4759 assert(const_offset
);
4760 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4761 if (num_offsets
> 2)
4762 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4763 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4764 if (num_offsets
> 1)
4765 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4766 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4767 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4768 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4772 /* TODO TG4 support */
4773 if (instr
->op
== nir_texop_tg4
) {
4774 if (instr
->is_shadow
)
4777 dmask
= 1 << instr
->component
;
4779 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4780 res_ptr
, samp_ptr
, address
, count
, dmask
);
4782 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4784 if (instr
->op
== nir_texop_query_levels
)
4785 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4786 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4787 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4788 instr
->op
!= nir_texop_tg4
)
4789 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4790 else if (instr
->op
== nir_texop_txs
&&
4791 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4793 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4794 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4795 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4796 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4797 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4798 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4799 instr
->op
== nir_texop_txs
&&
4800 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4802 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4803 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4804 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4806 } else if (instr
->dest
.ssa
.num_components
!= 4)
4807 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4811 assert(instr
->dest
.is_ssa
);
4812 result
= ac_to_integer(&ctx
->ac
, result
);
4813 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4818 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4820 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4821 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4823 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4824 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4827 static void visit_post_phi(struct ac_nir_context
*ctx
,
4828 nir_phi_instr
*instr
,
4829 LLVMValueRef llvm_phi
)
4831 nir_foreach_phi_src(src
, instr
) {
4832 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4833 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4835 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4839 static void phi_post_pass(struct ac_nir_context
*ctx
)
4841 struct hash_entry
*entry
;
4842 hash_table_foreach(ctx
->phis
, entry
) {
4843 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4844 (LLVMValueRef
)entry
->data
);
4849 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4850 const nir_ssa_undef_instr
*instr
)
4852 unsigned num_components
= instr
->def
.num_components
;
4855 if (num_components
== 1)
4856 undef
= LLVMGetUndef(ctx
->ac
.i32
);
4858 undef
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, num_components
));
4860 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4863 static void visit_jump(struct ac_nir_context
*ctx
,
4864 const nir_jump_instr
*instr
)
4866 switch (instr
->type
) {
4867 case nir_jump_break
:
4868 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
4869 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4871 case nir_jump_continue
:
4872 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4873 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4876 fprintf(stderr
, "Unknown NIR jump instr: ");
4877 nir_print_instr(&instr
->instr
, stderr
);
4878 fprintf(stderr
, "\n");
4883 static void visit_cf_list(struct ac_nir_context
*ctx
,
4884 struct exec_list
*list
);
4886 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4888 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4889 nir_foreach_instr(instr
, block
)
4891 switch (instr
->type
) {
4892 case nir_instr_type_alu
:
4893 visit_alu(ctx
, nir_instr_as_alu(instr
));
4895 case nir_instr_type_load_const
:
4896 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4898 case nir_instr_type_intrinsic
:
4899 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4901 case nir_instr_type_tex
:
4902 visit_tex(ctx
, nir_instr_as_tex(instr
));
4904 case nir_instr_type_phi
:
4905 visit_phi(ctx
, nir_instr_as_phi(instr
));
4907 case nir_instr_type_ssa_undef
:
4908 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4910 case nir_instr_type_jump
:
4911 visit_jump(ctx
, nir_instr_as_jump(instr
));
4914 fprintf(stderr
, "Unknown NIR instr type: ");
4915 nir_print_instr(instr
, stderr
);
4916 fprintf(stderr
, "\n");
4921 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4924 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4926 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4928 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4929 LLVMBasicBlockRef merge_block
=
4930 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4931 LLVMBasicBlockRef if_block
=
4932 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4933 LLVMBasicBlockRef else_block
= merge_block
;
4934 if (!exec_list_is_empty(&if_stmt
->else_list
))
4935 else_block
= LLVMAppendBasicBlockInContext(
4936 ctx
->ac
.context
, fn
, "");
4938 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
4939 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4940 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
4942 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
4943 visit_cf_list(ctx
, &if_stmt
->then_list
);
4944 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4945 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4947 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4948 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
4949 visit_cf_list(ctx
, &if_stmt
->else_list
);
4950 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4951 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4954 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
4957 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4959 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4960 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4961 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4963 ctx
->continue_block
=
4964 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4966 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4968 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4969 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
4970 visit_cf_list(ctx
, &loop
->body
);
4972 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4973 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4974 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
4976 ctx
->continue_block
= continue_parent
;
4977 ctx
->break_block
= break_parent
;
4980 static void visit_cf_list(struct ac_nir_context
*ctx
,
4981 struct exec_list
*list
)
4983 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4985 switch (node
->type
) {
4986 case nir_cf_node_block
:
4987 visit_block(ctx
, nir_cf_node_as_block(node
));
4990 case nir_cf_node_if
:
4991 visit_if(ctx
, nir_cf_node_as_if(node
));
4994 case nir_cf_node_loop
:
4995 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5005 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
5006 struct nir_variable
*variable
)
5008 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5009 LLVMValueRef t_offset
;
5010 LLVMValueRef t_list
;
5012 LLVMValueRef buffer_index
;
5013 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5014 int idx
= variable
->data
.location
;
5015 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5017 variable
->data
.driver_location
= idx
* 4;
5019 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
5020 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5021 ctx
->abi
.start_instance
, "");
5022 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
5023 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5025 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5026 ctx
->abi
.base_vertex
, "");
5028 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5029 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5031 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5033 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5035 LLVMConstInt(ctx
->ac
.i32
, 0, false),
5038 for (unsigned chan
= 0; chan
< 4; chan
++) {
5039 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5040 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5041 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5042 input
, llvm_chan
, ""));
5047 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5049 LLVMValueRef interp_param
,
5050 LLVMValueRef prim_mask
,
5051 LLVMValueRef result
[4])
5053 LLVMValueRef attr_number
;
5056 bool interp
= interp_param
!= NULL
;
5058 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5060 /* fs.constant returns the param from the middle vertex, so it's not
5061 * really useful for flat shading. It's meant to be used for custom
5062 * interpolation (but the intrinsic can't fetch from the other two
5065 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5066 * to do the right thing. The only reason we use fs.constant is that
5067 * fs.interp cannot be used on integers, because they can be equal
5071 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5074 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5076 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5080 for (chan
= 0; chan
< 4; chan
++) {
5081 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5084 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5089 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5090 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5099 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5100 struct nir_variable
*variable
)
5102 int idx
= variable
->data
.location
;
5103 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5104 LLVMValueRef interp
;
5106 variable
->data
.driver_location
= idx
* 4;
5107 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5109 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5110 unsigned interp_type
;
5111 if (variable
->data
.sample
) {
5112 interp_type
= INTERP_SAMPLE
;
5113 ctx
->shader_info
->info
.ps
.force_persample
= true;
5114 } else if (variable
->data
.centroid
)
5115 interp_type
= INTERP_CENTROID
;
5117 interp_type
= INTERP_CENTER
;
5119 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5123 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5124 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5129 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5130 struct nir_shader
*nir
) {
5131 nir_foreach_variable(variable
, &nir
->inputs
)
5132 handle_vs_input_decl(ctx
, variable
);
5136 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5137 struct nir_shader
*nir
)
5139 if (!ctx
->options
->key
.fs
.multisample
)
5142 bool uses_center
= false;
5143 bool uses_centroid
= false;
5144 nir_foreach_variable(variable
, &nir
->inputs
) {
5145 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5146 variable
->data
.sample
)
5149 if (variable
->data
.centroid
)
5150 uses_centroid
= true;
5155 if (uses_center
&& uses_centroid
) {
5156 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5157 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5158 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5163 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5164 struct nir_shader
*nir
)
5166 prepare_interp_optimize(ctx
, nir
);
5168 nir_foreach_variable(variable
, &nir
->inputs
)
5169 handle_fs_input_decl(ctx
, variable
);
5173 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5174 ctx
->shader_info
->info
.needs_multiview_view_index
)
5175 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5177 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5178 LLVMValueRef interp_param
;
5179 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5181 if (!(ctx
->input_mask
& (1ull << i
)))
5184 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5185 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5186 interp_param
= *inputs
;
5187 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5191 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5193 } else if (i
== VARYING_SLOT_POS
) {
5194 for(int i
= 0; i
< 3; ++i
)
5195 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5197 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5198 ctx
->abi
.frag_pos
[3]);
5201 ctx
->shader_info
->fs
.num_interp
= index
;
5202 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5203 ctx
->shader_info
->fs
.has_pcoord
= true;
5204 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5205 ctx
->shader_info
->fs
.prim_id_input
= true;
5206 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5207 ctx
->shader_info
->fs
.layer_input
= true;
5208 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5210 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5211 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5215 ac_build_alloca(struct ac_llvm_context
*ac
,
5219 LLVMBuilderRef builder
= ac
->builder
;
5220 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5221 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5222 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5223 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5224 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5228 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5230 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5233 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5234 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5236 LLVMDisposeBuilder(first_builder
);
5241 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5245 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5246 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5251 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5252 struct nir_variable
*variable
,
5253 struct nir_shader
*shader
,
5254 gl_shader_stage stage
)
5256 int idx
= variable
->data
.location
+ variable
->data
.index
;
5257 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5258 uint64_t mask_attribs
;
5260 variable
->data
.driver_location
= idx
* 4;
5262 /* tess ctrl has it's own load/store paths for outputs */
5263 if (stage
== MESA_SHADER_TESS_CTRL
)
5266 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5267 if (stage
== MESA_SHADER_VERTEX
||
5268 stage
== MESA_SHADER_TESS_EVAL
||
5269 stage
== MESA_SHADER_GEOMETRY
) {
5270 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5271 int length
= shader
->info
.clip_distance_array_size
+
5272 shader
->info
.cull_distance_array_size
;
5273 if (stage
== MESA_SHADER_VERTEX
) {
5274 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5275 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5277 if (stage
== MESA_SHADER_TESS_EVAL
) {
5278 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5279 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5286 mask_attribs
= 1ull << idx
;
5290 ctx
->output_mask
|= mask_attribs
;
5294 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5295 struct nir_shader
*nir
,
5296 struct nir_variable
*variable
)
5298 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5299 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5301 /* tess ctrl has it's own load/store paths for outputs */
5302 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5305 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5306 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5307 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5308 int idx
= variable
->data
.location
+ variable
->data
.index
;
5309 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5310 int length
= nir
->info
.clip_distance_array_size
+
5311 nir
->info
.cull_distance_array_size
;
5320 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5321 for (unsigned chan
= 0; chan
< 4; chan
++) {
5322 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5323 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5329 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5330 enum glsl_base_type type
)
5334 case GLSL_TYPE_UINT
:
5335 case GLSL_TYPE_BOOL
:
5336 case GLSL_TYPE_SUBROUTINE
:
5338 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5340 case GLSL_TYPE_INT64
:
5341 case GLSL_TYPE_UINT64
:
5343 case GLSL_TYPE_DOUBLE
:
5346 unreachable("unknown GLSL type");
5351 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5352 const struct glsl_type
*type
)
5354 if (glsl_type_is_scalar(type
)) {
5355 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5358 if (glsl_type_is_vector(type
)) {
5359 return LLVMVectorType(
5360 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5361 glsl_get_vector_elements(type
));
5364 if (glsl_type_is_matrix(type
)) {
5365 return LLVMArrayType(
5366 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5367 glsl_get_matrix_columns(type
));
5370 if (glsl_type_is_array(type
)) {
5371 return LLVMArrayType(
5372 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5373 glsl_get_length(type
));
5376 assert(glsl_type_is_struct(type
));
5378 LLVMTypeRef member_types
[glsl_get_length(type
)];
5380 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5382 glsl_to_llvm_type(ctx
,
5383 glsl_get_struct_field(type
, i
));
5386 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5387 glsl_get_length(type
), false);
5391 setup_locals(struct ac_nir_context
*ctx
,
5392 struct nir_function
*func
)
5395 ctx
->num_locals
= 0;
5396 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5397 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5398 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5399 ctx
->num_locals
+= attrib_count
;
5401 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5405 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5406 for (j
= 0; j
< 4; j
++) {
5407 ctx
->locals
[i
* 4 + j
] =
5408 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5414 setup_shared(struct ac_nir_context
*ctx
,
5415 struct nir_shader
*nir
)
5417 nir_foreach_variable(variable
, &nir
->shared
) {
5418 LLVMValueRef shared
=
5419 LLVMAddGlobalInAddressSpace(
5420 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5421 variable
->name
? variable
->name
: "",
5423 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5428 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5430 v
= ac_to_float(ctx
, v
);
5431 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5432 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5436 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5437 LLVMValueRef src0
, LLVMValueRef src1
)
5439 LLVMValueRef const16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
5440 LLVMValueRef comp
[2];
5442 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5443 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5444 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5445 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5448 /* Initialize arguments for the shader export intrinsic */
5450 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5451 LLVMValueRef
*values
,
5453 struct ac_export_args
*args
)
5455 /* Default is 0xf. Adjusted below depending on the format. */
5456 args
->enabled_channels
= 0xf;
5458 /* Specify whether the EXEC mask represents the valid mask */
5459 args
->valid_mask
= 0;
5461 /* Specify whether this is the last export */
5464 /* Specify the target we are exporting */
5465 args
->target
= target
;
5467 args
->compr
= false;
5468 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
5469 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
5470 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
5471 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5476 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5477 LLVMValueRef val
[4];
5478 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5479 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5480 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5481 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5483 switch(col_format
) {
5484 case V_028714_SPI_SHADER_ZERO
:
5485 args
->enabled_channels
= 0; /* writemask */
5486 args
->target
= V_008DFC_SQ_EXP_NULL
;
5489 case V_028714_SPI_SHADER_32_R
:
5490 args
->enabled_channels
= 1;
5491 args
->out
[0] = values
[0];
5494 case V_028714_SPI_SHADER_32_GR
:
5495 args
->enabled_channels
= 0x3;
5496 args
->out
[0] = values
[0];
5497 args
->out
[1] = values
[1];
5500 case V_028714_SPI_SHADER_32_AR
:
5501 args
->enabled_channels
= 0x9;
5502 args
->out
[0] = values
[0];
5503 args
->out
[3] = values
[3];
5506 case V_028714_SPI_SHADER_FP16_ABGR
:
5509 for (unsigned chan
= 0; chan
< 2; chan
++) {
5510 LLVMValueRef pack_args
[2] = {
5512 values
[2 * chan
+ 1]
5514 LLVMValueRef packed
;
5516 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5517 args
->out
[chan
] = packed
;
5521 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5522 for (unsigned chan
= 0; chan
< 4; chan
++) {
5523 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5524 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5525 LLVMConstReal(ctx
->ac
.f32
, 65535), "");
5526 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5527 LLVMConstReal(ctx
->ac
.f32
, 0.5), "");
5528 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5533 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5534 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5537 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5538 for (unsigned chan
= 0; chan
< 4; chan
++) {
5539 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5540 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5541 LLVMConstReal(ctx
->ac
.f32
, 32767), "");
5543 /* If positive, add 0.5, else add -0.5. */
5544 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5545 LLVMBuildSelect(ctx
->builder
,
5546 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5547 val
[chan
], ctx
->ac
.f32_0
, ""),
5548 LLVMConstReal(ctx
->ac
.f32
, 0.5),
5549 LLVMConstReal(ctx
->ac
.f32
, -0.5), ""), "");
5550 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->ac
.i32
, "");
5554 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5555 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5558 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5559 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5560 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5561 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->ac
.i32
, 3, 0);
5563 for (unsigned chan
= 0; chan
< 4; chan
++) {
5564 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5565 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5569 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5570 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5574 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5575 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5576 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5577 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5578 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5579 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->ac
.i32_1
;
5580 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->ac
.i32
, -2, 0);
5583 for (unsigned chan
= 0; chan
< 4; chan
++) {
5584 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5585 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5586 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5590 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5591 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5596 case V_028714_SPI_SHADER_32_ABGR
:
5597 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5601 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5603 for (unsigned i
= 0; i
< 4; ++i
)
5604 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5608 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5609 bool export_prim_id
,
5610 struct ac_vs_output_info
*outinfo
)
5612 uint32_t param_count
= 0;
5614 unsigned pos_idx
, num_pos_exports
= 0;
5615 struct ac_export_args args
, pos_args
[4] = {};
5616 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5619 if (ctx
->options
->key
.has_multiview_view_index
) {
5620 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5622 for(unsigned i
= 0; i
< 4; ++i
)
5623 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5624 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5627 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5628 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5631 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5632 sizeof(outinfo
->vs_output_param_offset
));
5634 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5635 LLVMValueRef slots
[8];
5638 if (outinfo
->cull_dist_mask
)
5639 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5641 i
= VARYING_SLOT_CLIP_DIST0
;
5642 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5643 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5644 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5646 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5647 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
5649 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5650 target
= V_008DFC_SQ_EXP_POS
+ 3;
5651 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5652 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5653 &args
, sizeof(args
));
5656 target
= V_008DFC_SQ_EXP_POS
+ 2;
5657 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5658 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5659 &args
, sizeof(args
));
5663 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
5664 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5665 for (unsigned j
= 0; j
< 4; j
++)
5666 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5667 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5669 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5671 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5672 outinfo
->writes_pointsize
= true;
5673 psize_value
= LLVMBuildLoad(ctx
->builder
,
5674 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5677 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5678 outinfo
->writes_layer
= true;
5679 layer_value
= LLVMBuildLoad(ctx
->builder
,
5680 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5683 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5684 outinfo
->writes_viewport_index
= true;
5685 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5686 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5689 if (outinfo
->writes_pointsize
||
5690 outinfo
->writes_layer
||
5691 outinfo
->writes_viewport_index
) {
5692 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5693 (outinfo
->writes_layer
== true ? 4 : 0));
5694 pos_args
[1].valid_mask
= 0;
5695 pos_args
[1].done
= 0;
5696 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5697 pos_args
[1].compr
= 0;
5698 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
5699 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
5700 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
5701 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
5703 if (outinfo
->writes_pointsize
== true)
5704 pos_args
[1].out
[0] = psize_value
;
5705 if (outinfo
->writes_layer
== true)
5706 pos_args
[1].out
[2] = layer_value
;
5707 if (outinfo
->writes_viewport_index
== true) {
5708 if (ctx
->options
->chip_class
>= GFX9
) {
5709 /* GFX9 has the layer in out.z[10:0] and the viewport
5710 * index in out.z[19:16].
5712 LLVMValueRef v
= viewport_index_value
;
5713 v
= ac_to_integer(&ctx
->ac
, v
);
5714 v
= LLVMBuildShl(ctx
->builder
, v
,
5715 LLVMConstInt(ctx
->ac
.i32
, 16, false),
5717 v
= LLVMBuildOr(ctx
->builder
, v
,
5718 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5720 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5721 pos_args
[1].enabled_channels
|= 1 << 2;
5723 pos_args
[1].out
[3] = viewport_index_value
;
5724 pos_args
[1].enabled_channels
|= 1 << 3;
5728 for (i
= 0; i
< 4; i
++) {
5729 if (pos_args
[i
].out
[0])
5734 for (i
= 0; i
< 4; i
++) {
5735 if (!pos_args
[i
].out
[0])
5738 /* Specify the target we are exporting */
5739 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5740 if (pos_idx
== num_pos_exports
)
5741 pos_args
[i
].done
= 1;
5742 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5745 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5746 LLVMValueRef values
[4];
5747 if (!(ctx
->output_mask
& (1ull << i
)))
5750 for (unsigned j
= 0; j
< 4; j
++)
5751 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5752 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5754 if (i
== VARYING_SLOT_LAYER
) {
5755 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5756 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5758 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5759 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5760 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5762 } else if (i
>= VARYING_SLOT_VAR0
) {
5763 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5764 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5765 outinfo
->vs_output_param_offset
[i
] = param_count
;
5770 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5772 if (target
>= V_008DFC_SQ_EXP_POS
&&
5773 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5774 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5775 &args
, sizeof(args
));
5777 ac_build_export(&ctx
->ac
, &args
);
5781 if (export_prim_id
) {
5782 LLVMValueRef values
[4];
5783 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5784 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5787 values
[0] = ctx
->vs_prim_id
;
5788 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5789 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5790 for (unsigned j
= 1; j
< 4; j
++)
5791 values
[j
] = ctx
->ac
.f32_0
;
5792 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5793 ac_build_export(&ctx
->ac
, &args
);
5794 outinfo
->export_prim_id
= true;
5797 outinfo
->pos_exports
= num_pos_exports
;
5798 outinfo
->param_exports
= param_count
;
5802 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5803 struct ac_es_output_info
*outinfo
)
5806 uint64_t max_output_written
= 0;
5807 LLVMValueRef lds_base
= NULL
;
5809 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5813 if (!(ctx
->output_mask
& (1ull << i
)))
5816 if (i
== VARYING_SLOT_CLIP_DIST0
)
5817 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5819 param_index
= shader_io_get_unique_index(i
);
5821 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5824 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5826 if (ctx
->ac
.chip_class
>= GFX9
) {
5827 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
5828 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
5829 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
5830 LLVMConstInt(ctx
->ac
.i32
, 24, false),
5831 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
5832 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
5833 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
5834 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
5835 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
5836 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
5839 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5840 LLVMValueRef dw_addr
;
5841 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5845 if (!(ctx
->output_mask
& (1ull << i
)))
5848 if (i
== VARYING_SLOT_CLIP_DIST0
)
5849 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5851 param_index
= shader_io_get_unique_index(i
);
5854 dw_addr
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5855 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
5858 for (j
= 0; j
< length
; j
++) {
5859 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5860 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
5862 if (ctx
->ac
.chip_class
>= GFX9
) {
5863 ac_lds_store(&ctx
->ac
, dw_addr
,
5864 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5865 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
5867 ac_build_buffer_store_dword(&ctx
->ac
,
5870 NULL
, ctx
->es2gs_offset
,
5871 (4 * param_index
+ j
) * 4,
5879 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5881 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5882 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
5883 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5884 vertex_dw_stride
, "");
5886 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5887 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5890 if (!(ctx
->output_mask
& (1ull << i
)))
5893 if (i
== VARYING_SLOT_CLIP_DIST0
)
5894 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5895 int param
= shader_io_get_unique_index(i
);
5896 mark_tess_output(ctx
, false, param
);
5898 mark_tess_output(ctx
, false, param
+ 1);
5899 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5900 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
5902 for (unsigned j
= 0; j
< length
; j
++) {
5903 ac_lds_store(&ctx
->ac
, dw_addr
,
5904 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5905 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
5910 struct ac_build_if_state
5912 struct nir_to_llvm_context
*ctx
;
5913 LLVMValueRef condition
;
5914 LLVMBasicBlockRef entry_block
;
5915 LLVMBasicBlockRef true_block
;
5916 LLVMBasicBlockRef false_block
;
5917 LLVMBasicBlockRef merge_block
;
5920 static LLVMBasicBlockRef
5921 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5923 LLVMBasicBlockRef current_block
;
5924 LLVMBasicBlockRef next_block
;
5925 LLVMBasicBlockRef new_block
;
5927 /* get current basic block */
5928 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5930 /* chqeck if there's another block after this one */
5931 next_block
= LLVMGetNextBasicBlock(current_block
);
5933 /* insert the new block before the next block */
5934 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5937 /* append new block after current block */
5938 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5939 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5945 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5946 struct nir_to_llvm_context
*ctx
,
5947 LLVMValueRef condition
)
5949 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5951 memset(ifthen
, 0, sizeof *ifthen
);
5953 ifthen
->condition
= condition
;
5954 ifthen
->entry_block
= block
;
5956 /* create endif/merge basic block for the phi functions */
5957 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5959 /* create/insert true_block before merge_block */
5960 ifthen
->true_block
=
5961 LLVMInsertBasicBlockInContext(ctx
->context
,
5962 ifthen
->merge_block
,
5965 /* successive code goes into the true block */
5966 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5970 * End a conditional.
5973 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5975 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5977 /* Insert branch to the merge block from current block */
5978 LLVMBuildBr(builder
, ifthen
->merge_block
);
5981 * Now patch in the various branch instructions.
5984 /* Insert the conditional branch instruction at the end of entry_block */
5985 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5986 if (ifthen
->false_block
) {
5987 /* we have an else clause */
5988 LLVMBuildCondBr(builder
, ifthen
->condition
,
5989 ifthen
->true_block
, ifthen
->false_block
);
5992 /* no else clause */
5993 LLVMBuildCondBr(builder
, ifthen
->condition
,
5994 ifthen
->true_block
, ifthen
->merge_block
);
5997 /* Resume building code at end of the ifthen->merge_block */
5998 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
6002 write_tess_factors(struct nir_to_llvm_context
*ctx
)
6004 unsigned stride
, outer_comps
, inner_comps
;
6005 struct ac_build_if_state if_ctx
, inner_if_ctx
;
6006 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 8, 5);
6007 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
6008 unsigned tess_inner_index
, tess_outer_index
;
6009 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6010 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6014 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6034 ac_nir_build_if(&if_ctx
, ctx
,
6035 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6036 invocation_id
, ctx
->ac
.i32_0
, ""));
6038 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6039 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6041 mark_tess_output(ctx
, true, tess_inner_index
);
6042 mark_tess_output(ctx
, true, tess_outer_index
);
6043 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6044 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6045 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6046 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6047 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6049 for (i
= 0; i
< 4; i
++) {
6050 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6051 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6055 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6056 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6057 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6058 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
6059 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6061 for (i
= 0; i
< outer_comps
; i
++) {
6063 ac_lds_load(&ctx
->ac
, lds_outer
);
6064 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6065 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
6067 for (i
= 0; i
< inner_comps
; i
++) {
6068 inner
[i
] = out
[outer_comps
+i
] =
6069 ac_lds_load(&ctx
->ac
, lds_inner
);
6070 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6071 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
6075 /* Convert the outputs to vectors for stores. */
6076 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6080 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6083 buffer
= ctx
->hs_ring_tess_factor
;
6084 tf_base
= ctx
->tess_factor_offset
;
6085 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6086 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6087 unsigned tf_offset
= 0;
6089 if (ctx
->options
->chip_class
<= VI
) {
6090 ac_nir_build_if(&inner_if_ctx
, ctx
,
6091 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6092 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6094 /* Store the dynamic HS control word. */
6095 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6096 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6097 1, ctx
->ac
.i32_0
, tf_base
,
6098 0, 1, 0, true, false);
6101 ac_nir_build_endif(&inner_if_ctx
);
6104 /* Store the tessellation factors. */
6105 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6106 MIN2(stride
, 4), byteoffset
, tf_base
,
6107 tf_offset
, 1, 0, true, false);
6109 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6110 stride
- 4, byteoffset
, tf_base
,
6111 16 + tf_offset
, 1, 0, true, false);
6113 //store to offchip for TES to read - only if TES reads them
6114 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6115 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6116 LLVMValueRef tf_inner_offset
;
6117 unsigned param_outer
, param_inner
;
6119 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6120 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6121 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6123 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6124 util_next_power_of_two(outer_comps
));
6126 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6127 outer_comps
, tf_outer_offset
,
6128 ctx
->oc_lds
, 0, 1, 0, true, false);
6130 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6131 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6132 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6134 inner_vec
= inner_comps
== 1 ? inner
[0] :
6135 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6136 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6137 inner_comps
, tf_inner_offset
,
6138 ctx
->oc_lds
, 0, 1, 0, true, false);
6141 ac_nir_build_endif(&if_ctx
);
6145 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6147 write_tess_factors(ctx
);
6151 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6152 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6153 struct ac_export_args
*args
)
6156 si_llvm_init_export_args(ctx
, color
, param
,
6160 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6161 args
->done
= 1; /* DONE bit */
6162 } else if (!args
->enabled_channels
)
6163 return false; /* unnecessary NULL export */
6169 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6170 LLVMValueRef depth
, LLVMValueRef stencil
,
6171 LLVMValueRef samplemask
)
6173 struct ac_export_args args
;
6175 args
.enabled_channels
= 0;
6176 args
.valid_mask
= 1;
6178 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
6181 args
.out
[0] = LLVMGetUndef(ctx
->ac
.f32
); /* R, depth */
6182 args
.out
[1] = LLVMGetUndef(ctx
->ac
.f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
6183 args
.out
[2] = LLVMGetUndef(ctx
->ac
.f32
); /* B, sample mask */
6184 args
.out
[3] = LLVMGetUndef(ctx
->ac
.f32
); /* A, alpha to mask */
6187 args
.out
[0] = depth
;
6188 args
.enabled_channels
|= 0x1;
6192 args
.out
[1] = stencil
;
6193 args
.enabled_channels
|= 0x2;
6197 args
.out
[2] = samplemask
;
6198 args
.enabled_channels
|= 0x4;
6201 /* SI (except OLAND and HAINAN) has a bug that it only looks
6202 * at the X writemask component. */
6203 if (ctx
->options
->chip_class
== SI
&&
6204 ctx
->options
->family
!= CHIP_OLAND
&&
6205 ctx
->options
->family
!= CHIP_HAINAN
)
6206 args
.enabled_channels
|= 0x1;
6208 ac_build_export(&ctx
->ac
, &args
);
6212 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6215 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6216 struct ac_export_args color_args
[8];
6218 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6219 LLVMValueRef values
[4];
6221 if (!(ctx
->output_mask
& (1ull << i
)))
6224 if (i
== FRAG_RESULT_DEPTH
) {
6225 ctx
->shader_info
->fs
.writes_z
= true;
6226 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6227 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6228 } else if (i
== FRAG_RESULT_STENCIL
) {
6229 ctx
->shader_info
->fs
.writes_stencil
= true;
6230 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6231 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6232 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6233 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6234 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6235 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6238 for (unsigned j
= 0; j
< 4; j
++)
6239 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6240 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6242 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6243 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6245 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6251 for (unsigned i
= 0; i
< index
; i
++)
6252 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6253 if (depth
|| stencil
|| samplemask
)
6254 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6256 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6257 ac_build_export(&ctx
->ac
, &color_args
[0]);
6260 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
6264 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6266 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6270 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6271 LLVMValueRef
*addrs
)
6273 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6275 switch (ctx
->stage
) {
6276 case MESA_SHADER_VERTEX
:
6277 if (ctx
->options
->key
.vs
.as_ls
)
6278 handle_ls_outputs_post(ctx
);
6279 else if (ctx
->options
->key
.vs
.as_es
)
6280 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6282 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6283 &ctx
->shader_info
->vs
.outinfo
);
6285 case MESA_SHADER_FRAGMENT
:
6286 handle_fs_outputs_post(ctx
);
6288 case MESA_SHADER_GEOMETRY
:
6289 emit_gs_epilogue(ctx
);
6291 case MESA_SHADER_TESS_CTRL
:
6292 handle_tcs_outputs_post(ctx
);
6294 case MESA_SHADER_TESS_EVAL
:
6295 if (ctx
->options
->key
.tes
.as_es
)
6296 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6298 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6299 &ctx
->shader_info
->tes
.outinfo
);
6306 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6308 LLVMPassManagerRef passmgr
;
6309 /* Create the pass manager */
6310 passmgr
= LLVMCreateFunctionPassManagerForModule(
6313 /* This pass should eliminate all the load and store instructions */
6314 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6316 /* Add some optimization passes */
6317 LLVMAddScalarReplAggregatesPass(passmgr
);
6318 LLVMAddLICMPass(passmgr
);
6319 LLVMAddAggressiveDCEPass(passmgr
);
6320 LLVMAddCFGSimplificationPass(passmgr
);
6321 LLVMAddInstructionCombiningPass(passmgr
);
6324 LLVMInitializeFunctionPassManager(passmgr
);
6325 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6326 LLVMFinalizeFunctionPassManager(passmgr
);
6328 LLVMDisposeBuilder(ctx
->builder
);
6329 LLVMDisposePassManager(passmgr
);
6333 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6335 struct ac_vs_output_info
*outinfo
;
6337 switch (ctx
->stage
) {
6338 case MESA_SHADER_FRAGMENT
:
6339 case MESA_SHADER_COMPUTE
:
6340 case MESA_SHADER_TESS_CTRL
:
6341 case MESA_SHADER_GEOMETRY
:
6343 case MESA_SHADER_VERTEX
:
6344 if (ctx
->options
->key
.vs
.as_ls
||
6345 ctx
->options
->key
.vs
.as_es
)
6347 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6349 case MESA_SHADER_TESS_EVAL
:
6350 if (ctx
->options
->key
.vs
.as_es
)
6352 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6355 unreachable("Unhandled shader type");
6358 ac_optimize_vs_outputs(&ctx
->ac
,
6360 outinfo
->vs_output_param_offset
,
6362 &outinfo
->param_exports
);
6366 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6368 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6369 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6370 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6373 if (ctx
->is_gs_copy_shader
) {
6374 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6376 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6378 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6379 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6381 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.v4i32
, "");
6383 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6384 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6385 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6386 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6389 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6390 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6391 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6392 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6397 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6398 const struct nir_shader
*nir
)
6400 switch (nir
->info
.stage
) {
6401 case MESA_SHADER_TESS_CTRL
:
6402 return chip_class
>= CIK
? 128 : 64;
6403 case MESA_SHADER_GEOMETRY
:
6404 return chip_class
>= GFX9
? 128 : 64;
6405 case MESA_SHADER_COMPUTE
:
6411 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6412 nir
->info
.cs
.local_size
[1] *
6413 nir
->info
.cs
.local_size
[2];
6414 return max_workgroup_size
;
6417 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6418 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6420 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6421 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6422 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6423 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6424 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
6425 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6426 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6427 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_rel_ids
, ctx
->rel_auto_id
, "");
6428 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6431 static void prepare_gs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6433 for(int i
= 5; i
>= 0; --i
) {
6434 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6435 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6436 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6439 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6440 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6441 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6444 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6445 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6447 struct ac_nir_context ctx
= {};
6448 struct nir_function
*func
;
6457 ctx
.stage
= nir
->info
.stage
;
6459 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6461 nir_foreach_variable(variable
, &nir
->outputs
)
6462 handle_shader_output_decl(&ctx
, nir
, variable
);
6464 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6465 _mesa_key_pointer_equal
);
6466 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6467 _mesa_key_pointer_equal
);
6468 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6469 _mesa_key_pointer_equal
);
6471 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6473 setup_locals(&ctx
, func
);
6475 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6476 setup_shared(&ctx
, nir
);
6478 visit_cf_list(&ctx
, &func
->impl
->body
);
6479 phi_post_pass(&ctx
);
6481 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6485 ralloc_free(ctx
.defs
);
6486 ralloc_free(ctx
.phis
);
6487 ralloc_free(ctx
.vars
);
6494 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6495 struct nir_shader
*const *shaders
,
6497 struct ac_shader_variant_info
*shader_info
,
6498 const struct ac_nir_compiler_options
*options
)
6500 struct nir_to_llvm_context ctx
= {0};
6502 ctx
.options
= options
;
6503 ctx
.shader_info
= shader_info
;
6504 ctx
.context
= LLVMContextCreate();
6505 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6507 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
);
6508 ctx
.ac
.module
= ctx
.module
;
6509 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6511 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6512 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6513 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6514 LLVMDisposeTargetData(data_layout
);
6515 LLVMDisposeMessage(data_layout_str
);
6517 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6518 ctx
.ac
.builder
= ctx
.builder
;
6520 memset(shader_info
, 0, sizeof(*shader_info
));
6522 for(int i
= 0; i
< shader_count
; ++i
)
6523 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6525 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6526 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6527 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6528 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6530 ctx
.max_workgroup_size
= 0;
6531 for (int i
= 0; i
< shader_count
; ++i
) {
6532 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6533 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6537 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6538 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6540 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6541 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6542 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
6543 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6544 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6545 ctx
.abi
.clamp_shadow_reference
= false;
6547 if (shader_count
>= 2)
6548 ac_init_exec_full_mask(&ctx
.ac
);
6550 if (ctx
.ac
.chip_class
== GFX9
&&
6551 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6552 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6554 for(int i
= 0; i
< shader_count
; ++i
) {
6555 ctx
.stage
= shaders
[i
]->info
.stage
;
6556 ctx
.output_mask
= 0;
6557 ctx
.tess_outputs_written
= 0;
6558 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6559 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6561 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6562 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6564 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6565 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6566 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
6567 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
6568 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6569 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6570 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6571 if (shader_info
->info
.vs
.needs_instance_id
) {
6572 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6573 MAX2(3, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6575 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6576 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6582 ac_setup_rings(&ctx
);
6584 LLVMBasicBlockRef merge_block
;
6585 if (shader_count
>= 2) {
6586 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6587 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6588 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6590 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6591 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6592 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6593 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6594 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6595 thread_id
, count
, "");
6596 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6598 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6601 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6602 handle_fs_inputs(&ctx
, shaders
[i
]);
6603 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6604 handle_vs_inputs(&ctx
, shaders
[i
]);
6605 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6606 prepare_gs_input_vgprs(&ctx
);
6608 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6609 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6611 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6613 if (shader_count
>= 2) {
6614 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6615 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6618 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6619 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6620 shaders
[i
]->info
.cull_distance_array_size
> 4;
6621 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6622 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6623 shaders
[i
]->info
.gs
.vertices_out
;
6624 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6625 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6626 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6627 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6628 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6632 LLVMBuildRetVoid(ctx
.builder
);
6634 ac_llvm_finalize_module(&ctx
);
6636 if (shader_count
== 1)
6637 ac_nir_eliminate_const_vs_outputs(&ctx
);
6642 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6644 unsigned *retval
= (unsigned *)context
;
6645 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6646 char *description
= LLVMGetDiagInfoDescription(di
);
6648 if (severity
== LLVMDSError
) {
6650 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6654 LLVMDisposeMessage(description
);
6657 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6658 struct ac_shader_binary
*binary
,
6659 LLVMTargetMachineRef tm
)
6661 unsigned retval
= 0;
6663 LLVMContextRef llvm_ctx
;
6664 LLVMMemoryBufferRef out_buffer
;
6665 unsigned buffer_size
;
6666 const char *buffer_data
;
6669 /* Setup Diagnostic Handler*/
6670 llvm_ctx
= LLVMGetModuleContext(M
);
6672 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6676 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6679 /* Process Errors/Warnings */
6681 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6687 /* Extract Shader Code*/
6688 buffer_size
= LLVMGetBufferSize(out_buffer
);
6689 buffer_data
= LLVMGetBufferStart(out_buffer
);
6691 ac_elf_read(buffer_data
, buffer_size
, binary
);
6694 LLVMDisposeMemoryBuffer(out_buffer
);
6700 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6701 LLVMModuleRef llvm_module
,
6702 struct ac_shader_binary
*binary
,
6703 struct ac_shader_config
*config
,
6704 struct ac_shader_variant_info
*shader_info
,
6705 gl_shader_stage stage
,
6706 bool dump_shader
, bool supports_spill
)
6709 ac_dump_module(llvm_module
);
6711 memset(binary
, 0, sizeof(*binary
));
6712 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6714 fprintf(stderr
, "compile failed\n");
6718 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6720 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6722 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6723 LLVMDisposeModule(llvm_module
);
6724 LLVMContextDispose(ctx
);
6726 if (stage
== MESA_SHADER_FRAGMENT
) {
6727 shader_info
->num_input_vgprs
= 0;
6728 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6729 shader_info
->num_input_vgprs
+= 2;
6730 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6731 shader_info
->num_input_vgprs
+= 2;
6732 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6733 shader_info
->num_input_vgprs
+= 2;
6734 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6735 shader_info
->num_input_vgprs
+= 3;
6736 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6737 shader_info
->num_input_vgprs
+= 2;
6738 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6739 shader_info
->num_input_vgprs
+= 2;
6740 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6741 shader_info
->num_input_vgprs
+= 2;
6742 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6743 shader_info
->num_input_vgprs
+= 1;
6744 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6745 shader_info
->num_input_vgprs
+= 1;
6746 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6747 shader_info
->num_input_vgprs
+= 1;
6748 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6749 shader_info
->num_input_vgprs
+= 1;
6750 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6751 shader_info
->num_input_vgprs
+= 1;
6752 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6753 shader_info
->num_input_vgprs
+= 1;
6754 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6755 shader_info
->num_input_vgprs
+= 1;
6756 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6757 shader_info
->num_input_vgprs
+= 1;
6758 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6759 shader_info
->num_input_vgprs
+= 1;
6761 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6763 /* +3 for scratch wave offset and VCC */
6764 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6765 shader_info
->num_input_sgprs
+ 3);
6769 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
6771 switch (nir
->info
.stage
) {
6772 case MESA_SHADER_COMPUTE
:
6773 for (int i
= 0; i
< 3; ++i
)
6774 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6776 case MESA_SHADER_FRAGMENT
:
6777 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6779 case MESA_SHADER_GEOMETRY
:
6780 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6781 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6782 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6783 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6785 case MESA_SHADER_TESS_EVAL
:
6786 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6787 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6788 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6789 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6790 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6792 case MESA_SHADER_TESS_CTRL
:
6793 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6795 case MESA_SHADER_VERTEX
:
6796 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6797 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6798 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6799 if (options
->key
.vs
.as_ls
)
6800 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6807 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6808 struct ac_shader_binary
*binary
,
6809 struct ac_shader_config
*config
,
6810 struct ac_shader_variant_info
*shader_info
,
6811 struct nir_shader
*const *nir
,
6813 const struct ac_nir_compiler_options
*options
,
6817 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
6820 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
6821 for (int i
= 0; i
< nir_count
; ++i
)
6822 ac_fill_shader_info(shader_info
, nir
[i
], options
);
6826 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6828 LLVMValueRef args
[9];
6829 args
[0] = ctx
->gsvs_ring
;
6830 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
6831 args
[3] = ctx
->ac
.i32_0
;
6832 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
6833 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
6834 args
[6] = ctx
->ac
.i32_1
; /* GLC */
6835 args
[7] = ctx
->ac
.i32_1
; /* SLC */
6836 args
[8] = ctx
->ac
.i32_0
; /* TFE */
6840 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6844 if (!(ctx
->output_mask
& (1ull << i
)))
6847 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6848 /* unpack clip and cull from a single set of slots */
6849 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6854 for (unsigned j
= 0; j
< length
; j
++) {
6856 args
[2] = LLVMConstInt(ctx
->ac
.i32
,
6858 ctx
->gs_max_out_vertices
* 16 * 4, false);
6860 value
= ac_build_intrinsic(&ctx
->ac
,
6861 "llvm.SI.buffer.load.dword.i32.i32",
6862 ctx
->ac
.i32
, args
, 9,
6863 AC_FUNC_ATTR_READONLY
|
6864 AC_FUNC_ATTR_LEGACY
);
6866 LLVMBuildStore(ctx
->builder
,
6867 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6871 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
6874 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6875 struct nir_shader
*geom_shader
,
6876 struct ac_shader_binary
*binary
,
6877 struct ac_shader_config
*config
,
6878 struct ac_shader_variant_info
*shader_info
,
6879 const struct ac_nir_compiler_options
*options
,
6882 struct nir_to_llvm_context ctx
= {0};
6883 ctx
.context
= LLVMContextCreate();
6884 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6885 ctx
.options
= options
;
6886 ctx
.shader_info
= shader_info
;
6888 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
);
6889 ctx
.ac
.module
= ctx
.module
;
6891 ctx
.is_gs_copy_shader
= true;
6892 LLVMSetTarget(ctx
.module
, "amdgcn--");
6894 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6895 ctx
.ac
.builder
= ctx
.builder
;
6896 ctx
.stage
= MESA_SHADER_VERTEX
;
6898 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
6900 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6901 ac_setup_rings(&ctx
);
6903 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6904 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6906 struct ac_nir_context nir_ctx
= {};
6907 nir_ctx
.ac
= ctx
.ac
;
6908 nir_ctx
.abi
= &ctx
.abi
;
6910 nir_ctx
.nctx
= &ctx
;
6913 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
6914 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
6915 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
6918 ac_gs_copy_shader_emit(&ctx
);
6922 LLVMBuildRetVoid(ctx
.builder
);
6924 ac_llvm_finalize_module(&ctx
);
6926 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6928 dump_shader
, options
->supports_spill
);