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];
125 LLVMValueRef gs_prim_id
, gs_invocation_id
;
127 LLVMValueRef esgs_ring
;
128 LLVMValueRef gsvs_ring
;
129 LLVMValueRef hs_ring_tess_offchip
;
130 LLVMValueRef hs_ring_tess_factor
;
132 LLVMValueRef prim_mask
;
133 LLVMValueRef sample_pos_offset
;
134 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
135 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
154 LLVMValueRef i1false
;
155 LLVMValueRef i32zero
;
157 LLVMValueRef f32zero
;
159 LLVMValueRef v4f32empty
;
161 unsigned uniform_md_kind
;
162 LLVMValueRef empty_md
;
163 gl_shader_stage stage
;
166 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
169 uint64_t output_mask
;
170 uint8_t num_output_clips
;
171 uint8_t num_output_culls
;
173 bool is_gs_copy_shader
;
174 LLVMValueRef gs_next_vertex
;
175 unsigned gs_max_out_vertices
;
177 unsigned tes_primitive_mode
;
178 uint64_t tess_outputs_written
;
179 uint64_t tess_patch_outputs_written
;
182 static inline struct nir_to_llvm_context
*
183 nir_to_llvm_context_from_abi(struct ac_shader_abi
*abi
)
185 struct nir_to_llvm_context
*ctx
= NULL
;
186 return container_of(abi
, ctx
, abi
);
189 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
190 const nir_deref_var
*deref
,
191 enum ac_descriptor_type desc_type
,
192 bool image
, bool write
);
194 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
196 return (index
* 4) + chan
;
199 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
201 /* handle patch indices separate */
202 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
204 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
206 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
207 return 2 + (slot
- VARYING_SLOT_PATCH0
);
209 if (slot
== VARYING_SLOT_POS
)
211 if (slot
== VARYING_SLOT_PSIZ
)
213 if (slot
== VARYING_SLOT_CLIP_DIST0
)
215 /* 3 is reserved for clip dist as well */
216 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
217 return 4 + (slot
- VARYING_SLOT_VAR0
);
218 unreachable("illegal slot in get unique index\n");
221 static void set_llvm_calling_convention(LLVMValueRef func
,
222 gl_shader_stage stage
)
224 enum radeon_llvm_calling_convention calling_conv
;
227 case MESA_SHADER_VERTEX
:
228 case MESA_SHADER_TESS_EVAL
:
229 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
231 case MESA_SHADER_GEOMETRY
:
232 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
234 case MESA_SHADER_TESS_CTRL
:
235 calling_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
: RADEON_LLVM_AMDGPU_VS
;
237 case MESA_SHADER_FRAGMENT
:
238 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
240 case MESA_SHADER_COMPUTE
:
241 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
244 unreachable("Unhandle shader type");
247 LLVMSetFunctionCallConv(func
, calling_conv
);
252 LLVMTypeRef types
[MAX_ARGS
];
253 LLVMValueRef
*assign
[MAX_ARGS
];
254 unsigned array_params_mask
;
256 uint8_t user_sgpr_count
;
258 uint8_t num_user_sgprs_used
;
259 uint8_t num_sgprs_used
;
260 uint8_t num_vgprs_used
;
264 add_argument(struct arg_info
*info
,
265 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
267 assert(info
->count
< MAX_ARGS
);
268 info
->assign
[info
->count
] = param_ptr
;
269 info
->types
[info
->count
] = type
;
274 add_sgpr_argument(struct arg_info
*info
,
275 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
277 add_argument(info
, type
, param_ptr
);
278 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
283 add_user_sgpr_argument(struct arg_info
*info
,
285 LLVMValueRef
*param_ptr
)
287 add_sgpr_argument(info
, type
, param_ptr
);
288 info
->num_user_sgprs_used
+= ac_get_type_size(type
) / 4;
289 info
->user_sgpr_count
++;
293 add_vgpr_argument(struct arg_info
*info
,
295 LLVMValueRef
*param_ptr
)
297 add_argument(info
, type
, param_ptr
);
298 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
302 add_user_sgpr_array_argument(struct arg_info
*info
,
304 LLVMValueRef
*param_ptr
)
306 info
->array_params_mask
|= (1 << info
->count
);
307 add_user_sgpr_argument(info
, type
, param_ptr
);
310 static void assign_arguments(LLVMValueRef main_function
,
311 struct arg_info
*info
)
314 for (i
= 0; i
< info
->count
; i
++) {
316 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
321 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
322 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
323 unsigned num_return_elems
,
324 struct arg_info
*args
,
325 unsigned max_workgroup_size
,
328 LLVMTypeRef main_function_type
, ret_type
;
329 LLVMBasicBlockRef main_function_body
;
331 if (num_return_elems
)
332 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
333 num_return_elems
, true);
335 ret_type
= LLVMVoidTypeInContext(ctx
);
337 /* Setup the function */
339 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
340 LLVMValueRef main_function
=
341 LLVMAddFunction(module
, "main", main_function_type
);
343 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
344 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
346 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
347 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
348 if (args
->array_params_mask
& (1 << i
)) {
349 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
350 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
351 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
354 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
358 if (max_workgroup_size
) {
359 ac_llvm_add_target_dep_function_attr(main_function
,
360 "amdgpu-max-work-group-size",
364 /* These were copied from some LLVM test. */
365 LLVMAddTargetDependentFunctionAttr(main_function
,
366 "less-precise-fpmad",
368 LLVMAddTargetDependentFunctionAttr(main_function
,
371 LLVMAddTargetDependentFunctionAttr(main_function
,
374 LLVMAddTargetDependentFunctionAttr(main_function
,
378 return main_function
;
381 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
383 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
387 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
389 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
390 type
= LLVMGetElementType(type
);
392 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
393 return LLVMGetIntTypeWidth(type
);
395 if (type
== ctx
->f16
)
397 if (type
== ctx
->f32
)
399 if (type
== ctx
->f64
)
402 unreachable("Unhandled type kind in get_elem_bits");
405 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
406 LLVMValueRef param
, unsigned rshift
,
409 LLVMValueRef value
= param
;
411 value
= LLVMBuildLShr(ctx
->builder
, value
,
412 LLVMConstInt(ctx
->i32
, rshift
, false), "");
414 if (rshift
+ bitwidth
< 32) {
415 unsigned mask
= (1 << bitwidth
) - 1;
416 value
= LLVMBuildAnd(ctx
->builder
, value
,
417 LLVMConstInt(ctx
->i32
, mask
, false), "");
422 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
424 switch (ctx
->stage
) {
425 case MESA_SHADER_TESS_CTRL
:
426 return unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
427 case MESA_SHADER_TESS_EVAL
:
428 return ctx
->tes_rel_patch_id
;
431 unreachable("Illegal stage");
435 /* Tessellation shaders pass outputs to the next shader using LDS.
437 * LS outputs = TCS inputs
438 * TCS outputs = TES inputs
441 * - TCS inputs for patch 0
442 * - TCS inputs for patch 1
443 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
445 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
446 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
447 * - TCS outputs for patch 1
448 * - Per-patch TCS outputs for patch 1
449 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
450 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
453 * All three shaders VS(LS), TCS, TES share the same LDS space.
456 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
458 if (ctx
->stage
== MESA_SHADER_VERTEX
)
459 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
460 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
461 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
469 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
471 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
475 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
477 return LLVMBuildMul(ctx
->builder
,
478 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
479 LLVMConstInt(ctx
->i32
, 4, false), "");
483 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
485 return LLVMBuildMul(ctx
->builder
,
486 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
487 LLVMConstInt(ctx
->i32
, 4, false), "");
491 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
493 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
494 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
496 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
500 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
502 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
503 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
504 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
506 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
507 LLVMBuildMul(ctx
->builder
, patch_stride
,
513 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
515 LLVMValueRef patch0_patch_data_offset
=
516 get_tcs_out_patch0_patch_data_offset(ctx
);
517 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
518 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
520 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
521 LLVMBuildMul(ctx
->builder
, patch_stride
,
526 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
528 ud_info
->sgpr_idx
= *sgpr_idx
;
529 ud_info
->num_sgprs
= num_sgprs
;
530 ud_info
->indirect
= false;
531 ud_info
->indirect_offset
= 0;
532 *sgpr_idx
+= num_sgprs
;
535 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
536 int idx
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
538 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
542 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
543 uint32_t indirect_offset
)
545 ud_info
->sgpr_idx
= sgpr_idx
;
546 ud_info
->num_sgprs
= num_sgprs
;
547 ud_info
->indirect
= true;
548 ud_info
->indirect_offset
= indirect_offset
;
551 static void declare_tess_lds(struct nir_to_llvm_context
*ctx
)
553 unsigned lds_size
= ctx
->options
->chip_class
>= CIK
? 65536 : 32768;
554 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32zero
,
555 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
559 struct user_sgpr_info
{
560 bool need_ring_offsets
;
562 bool indirect_all_descriptor_sets
;
565 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
566 struct user_sgpr_info
*user_sgpr_info
)
568 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
570 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
571 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
572 ctx
->stage
== MESA_SHADER_VERTEX
||
573 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
574 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
575 ctx
->is_gs_copy_shader
)
576 user_sgpr_info
->need_ring_offsets
= true;
578 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
579 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
580 user_sgpr_info
->need_ring_offsets
= true;
582 /* 2 user sgprs will nearly always be allocated for scratch/rings */
583 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
584 user_sgpr_info
->sgpr_count
+= 2;
587 switch (ctx
->stage
) {
588 case MESA_SHADER_COMPUTE
:
589 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
591 case MESA_SHADER_FRAGMENT
:
592 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
594 case MESA_SHADER_VERTEX
:
595 if (!ctx
->is_gs_copy_shader
) {
596 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
597 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
598 user_sgpr_info
->sgpr_count
+= 3;
600 user_sgpr_info
->sgpr_count
+= 2;
603 if (ctx
->options
->key
.vs
.as_ls
)
604 user_sgpr_info
->sgpr_count
++;
606 case MESA_SHADER_TESS_CTRL
:
607 user_sgpr_info
->sgpr_count
+= 4;
609 case MESA_SHADER_TESS_EVAL
:
610 user_sgpr_info
->sgpr_count
+= 1;
612 case MESA_SHADER_GEOMETRY
:
613 user_sgpr_info
->sgpr_count
+= 2;
619 if (ctx
->shader_info
->info
.needs_push_constants
)
620 user_sgpr_info
->sgpr_count
+= 2;
622 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
623 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
624 user_sgpr_info
->sgpr_count
+= 2;
625 user_sgpr_info
->indirect_all_descriptor_sets
= true;
627 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
632 radv_define_common_user_sgprs_phase1(struct nir_to_llvm_context
*ctx
,
633 gl_shader_stage stage
,
634 bool has_previous_stage
,
635 gl_shader_stage previous_stage
,
636 const struct user_sgpr_info
*user_sgpr_info
,
637 struct arg_info
*args
,
638 LLVMValueRef
*desc_sets
)
640 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
641 unsigned stage_mask
= 1 << stage
;
642 if (has_previous_stage
)
643 stage_mask
|= 1 << previous_stage
;
645 /* 1 for each descriptor set */
646 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
647 for (unsigned i
= 0; i
< num_sets
; ++i
) {
648 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
649 add_user_sgpr_array_argument(args
, const_array(ctx
->i8
, 1024 * 1024), &ctx
->descriptor_sets
[i
]);
653 add_user_sgpr_array_argument(args
, const_array(const_array(ctx
->i8
, 1024 * 1024), 32), desc_sets
);
655 if (ctx
->shader_info
->info
.needs_push_constants
) {
656 /* 1 for push constants and dynamic descriptors */
657 add_user_sgpr_array_argument(args
, const_array(ctx
->i8
, 1024 * 1024), &ctx
->push_constants
);
662 radv_define_common_user_sgprs_phase2(struct nir_to_llvm_context
*ctx
,
663 gl_shader_stage stage
,
664 bool has_previous_stage
,
665 gl_shader_stage previous_stage
,
666 const struct user_sgpr_info
*user_sgpr_info
,
667 LLVMValueRef desc_sets
,
668 uint8_t *user_sgpr_idx
)
670 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
671 unsigned stage_mask
= 1 << stage
;
672 if (has_previous_stage
)
673 stage_mask
|= 1 << previous_stage
;
675 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
676 for (unsigned i
= 0; i
< num_sets
; ++i
) {
677 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
678 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
680 ctx
->descriptor_sets
[i
] = NULL
;
683 uint32_t desc_sgpr_idx
= *user_sgpr_idx
;
684 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, user_sgpr_idx
, 2);
686 for (unsigned i
= 0; i
< num_sets
; ++i
) {
687 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
688 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
689 ctx
->descriptor_sets
[i
] = ac_build_load_to_sgpr(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->i32
, i
, false));
692 ctx
->descriptor_sets
[i
] = NULL
;
694 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
697 if (ctx
->shader_info
->info
.needs_push_constants
) {
698 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
703 radv_define_vs_user_sgprs_phase1(struct nir_to_llvm_context
*ctx
,
704 gl_shader_stage stage
,
705 bool has_previous_stage
,
706 gl_shader_stage previous_stage
,
707 struct arg_info
*args
)
709 if (!ctx
->is_gs_copy_shader
&& (stage
== MESA_SHADER_VERTEX
|| (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
710 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
711 add_user_sgpr_argument(args
, const_array(ctx
->v4i32
, 16), &ctx
->vertex_buffers
); /* vertex buffers */
712 add_user_sgpr_argument(args
, ctx
->i32
, &ctx
->abi
.base_vertex
); // base vertex
713 add_user_sgpr_argument(args
, ctx
->i32
, &ctx
->abi
.start_instance
);// start instance
714 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
715 add_user_sgpr_argument(args
, ctx
->i32
, &ctx
->abi
.draw_id
); // draw id
720 radv_define_vs_user_sgprs_phase2(struct nir_to_llvm_context
*ctx
,
721 gl_shader_stage stage
,
722 bool has_previous_stage
,
723 gl_shader_stage previous_stage
,
724 uint8_t *user_sgpr_idx
)
726 if (!ctx
->is_gs_copy_shader
&& (stage
== MESA_SHADER_VERTEX
|| (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
727 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
728 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
731 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
734 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, vs_num
);
739 static void create_function(struct nir_to_llvm_context
*ctx
,
740 gl_shader_stage stage
,
741 bool has_previous_stage
,
742 gl_shader_stage previous_stage
)
744 uint8_t user_sgpr_idx
;
745 struct user_sgpr_info user_sgpr_info
;
746 struct arg_info args
= {};
747 LLVMValueRef desc_sets
;
749 allocate_user_sgprs(ctx
, &user_sgpr_info
);
751 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
752 add_user_sgpr_argument(&args
, const_array(ctx
->v4i32
, 16), &ctx
->ring_offsets
); /* address of rings */
756 case MESA_SHADER_COMPUTE
:
757 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
758 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
759 add_user_sgpr_argument(&args
, LLVMVectorType(ctx
->i32
, ctx
->shader_info
->info
.cs
.grid_components_used
), &ctx
->num_work_groups
); /* grid size */
760 add_sgpr_argument(&args
, LLVMVectorType(ctx
->i32
, 3), &ctx
->workgroup_ids
);
761 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->tg_size
);
762 add_vgpr_argument(&args
, LLVMVectorType(ctx
->i32
, 3), &ctx
->local_invocation_ids
);
764 case MESA_SHADER_VERTEX
:
765 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
766 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
767 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
))
768 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
769 if (ctx
->options
->key
.vs
.as_es
)
770 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->es2gs_offset
); // es2gs offset
771 else if (ctx
->options
->key
.vs
.as_ls
)
772 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->ls_out_layout
); // ls out layout
773 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.vertex_id
); // vertex id
774 if (!ctx
->is_gs_copy_shader
) {
775 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->rel_auto_id
); // rel auto id
776 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->vs_prim_id
); // vs prim id
777 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.instance_id
); // instance id
780 case MESA_SHADER_TESS_CTRL
:
781 if (has_previous_stage
) {
782 // First 6 system regs
783 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // param oc lds
784 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->merged_wave_info
); // merged wave info
785 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->tess_factor_offset
); // tess factor offset
787 add_sgpr_argument(&args
, ctx
->i32
, NULL
); // scratch offset
788 add_sgpr_argument(&args
, ctx
->i32
, NULL
); // unknown
789 add_sgpr_argument(&args
, ctx
->i32
, NULL
); // unknown
791 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
792 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
793 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->ls_out_layout
); // ls out layout
795 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
796 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
797 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_out_layout
); // tcs out layout
798 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_in_layout
); // tcs in layout
799 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
800 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
802 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_patch_id
); // patch id
803 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_rel_ids
); // rel ids;
804 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.vertex_id
); // vertex id
805 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->rel_auto_id
); // rel auto id
806 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->vs_prim_id
); // vs prim id
807 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.instance_id
); // instance id
809 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
810 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
811 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
812 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_out_layout
); // tcs out layout
813 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_in_layout
); // tcs in layout
814 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
815 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
816 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // param oc lds
817 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->tess_factor_offset
); // tess factor offset
818 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_patch_id
); // patch id
819 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_rel_ids
); // rel ids;
822 case MESA_SHADER_TESS_EVAL
:
823 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
824 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
825 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
826 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
827 if (ctx
->options
->key
.tes
.as_es
) {
828 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // OC LDS
829 add_sgpr_argument(&args
, ctx
->i32
, NULL
); //
830 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->es2gs_offset
); // es2gs offset
832 add_sgpr_argument(&args
, ctx
->i32
, NULL
); //
833 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // OC LDS
835 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_u
); // tes_u
836 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_v
); // tes_v
837 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
838 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tes_patch_id
); // tes patch id
840 case MESA_SHADER_GEOMETRY
:
841 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
842 radv_define_vs_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &args
);
843 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
844 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
845 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
846 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
847 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->gs2vs_offset
); // gs2vs offset
848 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->gs_wave_id
); // wave id
849 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[0]); // vtx0
850 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[1]); // vtx1
851 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_prim_id
); // prim id
852 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[2]);
853 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[3]);
854 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[4]);
855 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[5]);
856 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_invocation_id
);
858 case MESA_SHADER_FRAGMENT
:
859 radv_define_common_user_sgprs_phase1(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, &args
, &desc_sets
);
860 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
861 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->sample_pos_offset
); /* sample position offset */
862 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->prim_mask
); /* prim mask */
863 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_sample
); /* persp sample */
864 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_center
); /* persp center */
865 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_centroid
); /* persp centroid */
866 add_vgpr_argument(&args
, ctx
->v3i32
, NULL
); /* persp pull model */
867 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_sample
); /* linear sample */
868 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_center
); /* linear center */
869 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_centroid
); /* linear centroid */
870 add_vgpr_argument(&args
, ctx
->f32
, NULL
); /* line stipple tex */
871 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[0]); /* pos x float */
872 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[1]); /* pos y float */
873 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[2]); /* pos z float */
874 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[3]); /* pos w float */
875 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.front_face
); /* front face */
876 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.ancillary
); /* ancillary */
877 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.sample_coverage
); /* sample coverage */
878 add_vgpr_argument(&args
, ctx
->i32
, NULL
); /* fixed pt */
881 unreachable("Shader stage not implemented");
884 ctx
->main_function
= create_llvm_function(
885 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
886 ctx
->max_workgroup_size
,
887 ctx
->options
->unsafe_math
);
888 set_llvm_calling_convention(ctx
->main_function
, stage
);
891 ctx
->shader_info
->num_input_vgprs
= 0;
892 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
894 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
896 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
897 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
899 assign_arguments(ctx
->main_function
, &args
);
903 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
904 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, &user_sgpr_idx
, 2);
905 if (ctx
->options
->supports_spill
) {
906 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
907 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
908 NULL
, 0, AC_FUNC_ATTR_READNONE
);
909 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
910 const_array(ctx
->v4i32
, 16), "");
914 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
915 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
916 if (has_previous_stage
)
919 radv_define_common_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
922 case MESA_SHADER_COMPUTE
:
923 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
924 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, &user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
927 case MESA_SHADER_VERTEX
:
928 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
930 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
931 if (ctx
->options
->key
.vs
.as_ls
) {
932 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
934 if (ctx
->options
->key
.vs
.as_ls
)
935 declare_tess_lds(ctx
);
937 case MESA_SHADER_TESS_CTRL
:
938 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
939 if (has_previous_stage
)
940 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
941 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
943 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
944 declare_tess_lds(ctx
);
946 case MESA_SHADER_TESS_EVAL
:
947 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
949 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
951 case MESA_SHADER_GEOMETRY
:
952 radv_define_vs_user_sgprs_phase2(ctx
, stage
, has_previous_stage
, previous_stage
, &user_sgpr_idx
);
953 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, &user_sgpr_idx
, 2);
955 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
957 case MESA_SHADER_FRAGMENT
:
958 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
959 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, &user_sgpr_idx
, 1);
963 unreachable("Shader stage not implemented");
966 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
969 static void setup_types(struct nir_to_llvm_context
*ctx
)
971 LLVMValueRef args
[4];
973 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
974 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
975 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
976 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
977 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
978 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
979 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
980 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
981 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
982 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
983 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
984 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
985 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
986 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
987 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
989 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
990 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
991 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
992 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
993 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
994 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
996 args
[0] = ctx
->f32zero
;
997 args
[1] = ctx
->f32zero
;
998 args
[2] = ctx
->f32zero
;
999 args
[3] = ctx
->f32one
;
1000 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
1002 ctx
->uniform_md_kind
=
1003 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
1004 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
1006 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
1009 static int get_llvm_num_components(LLVMValueRef value
)
1011 LLVMTypeRef type
= LLVMTypeOf(value
);
1012 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1013 ? LLVMGetVectorSize(type
)
1015 return num_components
;
1018 static LLVMValueRef
llvm_extract_elem(struct ac_llvm_context
*ac
,
1022 int count
= get_llvm_num_components(value
);
1024 assert(index
< count
);
1028 return LLVMBuildExtractElement(ac
->builder
, value
,
1029 LLVMConstInt(ac
->i32
, index
, false), "");
1032 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1033 LLVMValueRef value
, unsigned count
)
1035 unsigned num_components
= get_llvm_num_components(value
);
1036 if (count
== num_components
)
1039 LLVMValueRef masks
[] = {
1040 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1041 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1044 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1047 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1048 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1052 build_store_values_extended(struct ac_llvm_context
*ac
,
1053 LLVMValueRef
*values
,
1054 unsigned value_count
,
1055 unsigned value_stride
,
1058 LLVMBuilderRef builder
= ac
->builder
;
1061 for (i
= 0; i
< value_count
; i
++) {
1062 LLVMValueRef ptr
= values
[i
* value_stride
];
1063 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1064 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1065 LLVMBuildStore(builder
, value
, ptr
);
1069 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1070 const nir_ssa_def
*def
)
1072 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1073 if (def
->num_components
> 1) {
1074 type
= LLVMVectorType(type
, def
->num_components
);
1079 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1082 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1083 return (LLVMValueRef
)entry
->data
;
1087 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1088 const struct nir_block
*b
)
1090 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1091 return (LLVMBasicBlockRef
)entry
->data
;
1094 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1096 unsigned num_components
)
1098 LLVMValueRef value
= get_src(ctx
, src
.src
);
1099 bool need_swizzle
= false;
1102 LLVMTypeRef type
= LLVMTypeOf(value
);
1103 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1104 ? LLVMGetVectorSize(type
)
1107 for (unsigned i
= 0; i
< num_components
; ++i
) {
1108 assert(src
.swizzle
[i
] < src_components
);
1109 if (src
.swizzle
[i
] != i
)
1110 need_swizzle
= true;
1113 if (need_swizzle
|| num_components
!= src_components
) {
1114 LLVMValueRef masks
[] = {
1115 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1116 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1117 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1118 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1120 if (src_components
> 1 && num_components
== 1) {
1121 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1123 } else if (src_components
== 1 && num_components
> 1) {
1124 LLVMValueRef values
[] = {value
, value
, value
, value
};
1125 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1127 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1128 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1132 assert(!src
.negate
);
1137 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1138 LLVMIntPredicate pred
, LLVMValueRef src0
,
1141 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1142 return LLVMBuildSelect(ctx
->builder
, result
,
1143 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1144 LLVMConstInt(ctx
->i32
, 0, false), "");
1147 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1148 LLVMRealPredicate pred
, LLVMValueRef src0
,
1151 LLVMValueRef result
;
1152 src0
= ac_to_float(ctx
, src0
);
1153 src1
= ac_to_float(ctx
, src1
);
1154 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1155 return LLVMBuildSelect(ctx
->builder
, result
,
1156 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1157 LLVMConstInt(ctx
->i32
, 0, false), "");
1160 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1162 LLVMTypeRef result_type
,
1166 LLVMValueRef params
[] = {
1167 ac_to_float(ctx
, src0
),
1170 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1171 get_elem_bits(ctx
, result_type
));
1172 assert(length
< sizeof(name
));
1173 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1176 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1178 LLVMTypeRef result_type
,
1179 LLVMValueRef src0
, LLVMValueRef src1
)
1182 LLVMValueRef params
[] = {
1183 ac_to_float(ctx
, src0
),
1184 ac_to_float(ctx
, src1
),
1187 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1188 get_elem_bits(ctx
, result_type
));
1189 assert(length
< sizeof(name
));
1190 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1193 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1195 LLVMTypeRef result_type
,
1196 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1199 LLVMValueRef params
[] = {
1200 ac_to_float(ctx
, src0
),
1201 ac_to_float(ctx
, src1
),
1202 ac_to_float(ctx
, src2
),
1205 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1206 get_elem_bits(ctx
, result_type
));
1207 assert(length
< sizeof(name
));
1208 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1211 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1212 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1214 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1216 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1219 static LLVMValueRef
emit_find_lsb(struct ac_llvm_context
*ctx
,
1222 LLVMValueRef params
[2] = {
1225 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1226 * add special code to check for x=0. The reason is that
1227 * the LLVM behavior for x=0 is different from what we
1230 * The hardware already implements the correct behavior.
1232 LLVMConstInt(ctx
->i1
, 1, false),
1235 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
1237 AC_FUNC_ATTR_READNONE
);
1239 /* TODO: We need an intrinsic to skip this conditional. */
1240 /* Check for zero: */
1241 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
1244 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
1247 static LLVMValueRef
emit_ifind_msb(struct ac_llvm_context
*ctx
,
1250 return ac_build_imsb(ctx
, src0
, ctx
->i32
);
1253 static LLVMValueRef
emit_ufind_msb(struct ac_llvm_context
*ctx
,
1256 return ac_build_umsb(ctx
, src0
, ctx
->i32
);
1259 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1260 LLVMIntPredicate pred
,
1261 LLVMValueRef src0
, LLVMValueRef src1
)
1263 return LLVMBuildSelect(ctx
->builder
,
1264 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1269 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1272 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1273 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1276 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1279 LLVMValueRef cmp
, val
;
1281 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32_0
, "");
1282 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32_1
, src0
, "");
1283 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32_0
, "");
1284 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1288 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1291 LLVMValueRef cmp
, val
;
1293 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32_0
, "");
1294 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32_1
, src0
, "");
1295 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32_0
, "");
1296 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1300 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1303 const char *intr
= "llvm.floor.f32";
1304 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1305 LLVMValueRef params
[] = {
1308 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1309 ctx
->f32
, params
, 1,
1310 AC_FUNC_ATTR_READNONE
);
1311 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1314 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1316 LLVMValueRef src0
, LLVMValueRef src1
)
1318 LLVMTypeRef ret_type
;
1319 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1321 LLVMValueRef params
[] = { src0
, src1
};
1322 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1325 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1326 params
, 2, AC_FUNC_ATTR_READNONE
);
1328 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1329 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1333 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1336 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1339 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1342 src0
= ac_to_float(ctx
, src0
);
1343 return LLVMBuildSExt(ctx
->builder
,
1344 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1348 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1351 return LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1354 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1357 return LLVMBuildSExt(ctx
->builder
,
1358 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1362 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1365 LLVMValueRef result
;
1366 LLVMValueRef cond
= NULL
;
1368 src0
= ac_to_float(&ctx
->ac
, src0
);
1369 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
1371 if (ctx
->options
->chip_class
>= VI
) {
1372 LLVMValueRef args
[2];
1373 /* Check if the result is a denormal - and flush to 0 if so. */
1375 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1376 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1379 /* need to convert back up to f32 */
1380 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1382 if (ctx
->options
->chip_class
>= VI
)
1383 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32zero
, result
, "");
1386 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1387 * so compare the result and flush to 0 if it's smaller.
1389 LLVMValueRef temp
, cond2
;
1390 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1392 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1393 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
1395 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1396 temp
, ctx
->f32zero
, "");
1397 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1398 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32zero
, result
, "");
1403 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1404 LLVMValueRef src0
, LLVMValueRef src1
)
1406 LLVMValueRef dst64
, result
;
1407 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1408 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1410 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1411 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1412 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1416 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1417 LLVMValueRef src0
, LLVMValueRef src1
)
1419 LLVMValueRef dst64
, result
;
1420 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1421 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1423 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1424 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1425 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1429 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1431 const LLVMValueRef srcs
[3])
1433 LLVMValueRef result
;
1434 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1436 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1437 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1441 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1442 LLVMValueRef src0
, LLVMValueRef src1
,
1443 LLVMValueRef src2
, LLVMValueRef src3
)
1445 LLVMValueRef bfi_args
[3], result
;
1447 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1448 LLVMBuildSub(ctx
->builder
,
1449 LLVMBuildShl(ctx
->builder
,
1454 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1457 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1460 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1461 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1463 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1464 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1465 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1467 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1471 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1474 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1476 LLVMValueRef comp
[2];
1478 src0
= ac_to_float(ctx
, src0
);
1479 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1480 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1481 for (i
= 0; i
< 2; i
++) {
1482 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1483 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1484 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1487 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1488 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1493 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1496 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1497 LLVMValueRef temps
[2], result
, val
;
1500 for (i
= 0; i
< 2; i
++) {
1501 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1502 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1503 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1504 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1507 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
1508 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(v2f32
), temps
[0],
1510 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1515 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1521 LLVMValueRef result
;
1523 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1524 mask
= AC_TID_MASK_LEFT
;
1525 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1526 mask
= AC_TID_MASK_TOP
;
1528 mask
= AC_TID_MASK_TOP_LEFT
;
1530 /* for DDX we want to next X pixel, DDY next Y pixel. */
1531 if (op
== nir_op_fddx_fine
||
1532 op
== nir_op_fddx_coarse
||
1538 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1543 * this takes an I,J coordinate pair,
1544 * and works out the X and Y derivatives.
1545 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1547 static LLVMValueRef
emit_ddxy_interp(
1548 struct ac_nir_context
*ctx
,
1549 LLVMValueRef interp_ij
)
1551 LLVMValueRef result
[4], a
;
1554 for (i
= 0; i
< 2; i
++) {
1555 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1556 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1557 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1558 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1560 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1563 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1565 LLVMValueRef src
[4], result
= NULL
;
1566 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1567 unsigned src_components
;
1568 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1570 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1571 switch (instr
->op
) {
1577 case nir_op_pack_half_2x16
:
1580 case nir_op_unpack_half_2x16
:
1584 src_components
= num_components
;
1587 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1588 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1590 switch (instr
->op
) {
1596 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1597 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1600 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1603 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1606 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1609 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1610 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1611 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1614 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1615 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1616 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1619 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1622 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1625 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1628 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1631 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1632 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1633 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1634 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1635 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1636 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1637 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1640 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1641 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1642 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1645 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1648 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1651 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1654 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1655 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1656 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1659 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1660 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1661 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1664 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1665 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, src
[0]);
1668 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1671 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1674 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1677 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1678 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1679 LLVMTypeOf(src
[0]), ""),
1683 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1684 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1685 LLVMTypeOf(src
[0]), ""),
1689 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1690 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1691 LLVMTypeOf(src
[0]), ""),
1695 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1698 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1701 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1704 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1707 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1710 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1713 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1716 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1719 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1722 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1725 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1726 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1729 result
= emit_iabs(&ctx
->ac
, src
[0]);
1732 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1735 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1738 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1741 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1744 result
= emit_isign(&ctx
->ac
, src
[0]);
1747 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1748 result
= emit_fsign(&ctx
->ac
, src
[0]);
1751 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1752 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1755 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1756 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1759 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1760 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1762 case nir_op_fround_even
:
1763 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1764 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1767 result
= emit_ffract(&ctx
->ac
, src
[0]);
1770 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1771 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1774 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1775 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1778 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1779 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1782 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1783 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1786 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1787 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1790 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1791 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1792 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, result
);
1795 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1796 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1799 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1800 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1801 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1802 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1803 ac_to_float_type(&ctx
->ac
, def_type
),
1807 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1808 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1809 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1810 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1811 ac_to_float_type(&ctx
->ac
, def_type
),
1815 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1816 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1818 case nir_op_ibitfield_extract
:
1819 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1821 case nir_op_ubitfield_extract
:
1822 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1824 case nir_op_bitfield_insert
:
1825 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1827 case nir_op_bitfield_reverse
:
1828 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1830 case nir_op_bit_count
:
1831 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1836 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1837 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1838 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1842 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1843 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1847 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1848 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1852 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1853 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1857 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1858 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1861 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1864 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1868 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1869 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1870 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1872 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1876 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1877 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1878 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1880 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1883 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1885 case nir_op_find_lsb
:
1886 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1887 result
= emit_find_lsb(&ctx
->ac
, src
[0]);
1889 case nir_op_ufind_msb
:
1890 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1891 result
= emit_ufind_msb(&ctx
->ac
, src
[0]);
1893 case nir_op_ifind_msb
:
1894 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1895 result
= emit_ifind_msb(&ctx
->ac
, src
[0]);
1897 case nir_op_uadd_carry
:
1898 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1899 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1900 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1902 case nir_op_usub_borrow
:
1903 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1904 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1905 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1908 result
= emit_b2f(&ctx
->ac
, src
[0]);
1911 result
= emit_f2b(&ctx
->ac
, src
[0]);
1914 result
= emit_b2i(&ctx
->ac
, src
[0]);
1917 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1918 result
= emit_i2b(&ctx
->ac
, src
[0]);
1920 case nir_op_fquantize2f16
:
1921 result
= emit_f2f16(ctx
->nctx
, src
[0]);
1923 case nir_op_umul_high
:
1924 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1925 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1926 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1928 case nir_op_imul_high
:
1929 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1930 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1931 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1933 case nir_op_pack_half_2x16
:
1934 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
1936 case nir_op_unpack_half_2x16
:
1937 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1941 case nir_op_fddx_fine
:
1942 case nir_op_fddy_fine
:
1943 case nir_op_fddx_coarse
:
1944 case nir_op_fddy_coarse
:
1945 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1948 case nir_op_unpack_64_2x32_split_x
: {
1949 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1950 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1951 LLVMVectorType(ctx
->ac
.i32
, 2),
1953 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1958 case nir_op_unpack_64_2x32_split_y
: {
1959 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1960 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1961 LLVMVectorType(ctx
->ac
.i32
, 2),
1963 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1968 case nir_op_pack_64_2x32_split
: {
1969 LLVMValueRef tmp
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, 2));
1970 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1971 src
[0], ctx
->ac
.i32_0
, "");
1972 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1973 src
[1], ctx
->ac
.i32_1
, "");
1974 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1979 fprintf(stderr
, "Unknown NIR alu instr: ");
1980 nir_print_instr(&instr
->instr
, stderr
);
1981 fprintf(stderr
, "\n");
1986 assert(instr
->dest
.dest
.is_ssa
);
1987 result
= ac_to_integer(&ctx
->ac
, result
);
1988 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1993 static void visit_load_const(struct ac_nir_context
*ctx
,
1994 const nir_load_const_instr
*instr
)
1996 LLVMValueRef values
[4], value
= NULL
;
1997 LLVMTypeRef element_type
=
1998 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
2000 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
2001 switch (instr
->def
.bit_size
) {
2003 values
[i
] = LLVMConstInt(element_type
,
2004 instr
->value
.u32
[i
], false);
2007 values
[i
] = LLVMConstInt(element_type
,
2008 instr
->value
.u64
[i
], false);
2012 "unsupported nir load_const bit_size: %d\n",
2013 instr
->def
.bit_size
);
2017 if (instr
->def
.num_components
> 1) {
2018 value
= LLVMConstVector(values
, instr
->def
.num_components
);
2022 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
2025 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
2028 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2029 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2030 LLVMPointerType(type
, addr_space
), "");
2034 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
2037 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2038 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
2041 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
2042 /* On VI, the descriptor contains the size in bytes,
2043 * but TXQ must return the size in elements.
2044 * The stride is always non-zero for resources using TXQ.
2046 LLVMValueRef stride
=
2047 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2048 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
2049 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2050 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2051 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2052 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2054 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2060 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2063 static void build_int_type_name(
2065 char *buf
, unsigned bufsize
)
2067 assert(bufsize
>= 6);
2069 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2070 snprintf(buf
, bufsize
, "v%ui32",
2071 LLVMGetVectorSize(type
));
2076 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2077 struct ac_image_args
*args
,
2078 const nir_tex_instr
*instr
)
2080 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2081 LLVMValueRef coord
= args
->addr
;
2082 LLVMValueRef half_texel
[2];
2083 LLVMValueRef compare_cube_wa
= NULL
;
2084 LLVMValueRef result
;
2086 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2090 struct ac_image_args txq_args
= { 0 };
2092 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2093 txq_args
.opcode
= ac_image_get_resinfo
;
2094 txq_args
.dmask
= 0xf;
2095 txq_args
.addr
= ctx
->i32_0
;
2096 txq_args
.resource
= args
->resource
;
2097 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2099 for (c
= 0; c
< 2; c
++) {
2100 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2101 LLVMConstInt(ctx
->i32
, c
, false), "");
2102 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2103 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2104 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2105 LLVMConstReal(ctx
->f32
, -0.5), "");
2109 LLVMValueRef orig_coords
= args
->addr
;
2111 for (c
= 0; c
< 2; c
++) {
2113 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2114 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2115 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2116 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2117 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2118 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2123 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2124 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2125 * workaround by sampling using a scaled type and converting.
2126 * This is taken from amdgpu-pro shaders.
2128 /* NOTE this produces some ugly code compared to amdgpu-pro,
2129 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2130 * and then reads them back. -pro generates two selects,
2131 * one s_cmp for the descriptor rewriting
2132 * one v_cmp for the coordinate and result changes.
2134 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2135 LLVMValueRef tmp
, tmp2
;
2137 /* workaround 8/8/8/8 uint/sint cube gather bug */
2138 /* first detect it then change to a scaled read and f2i */
2139 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2142 /* extract the DATA_FORMAT */
2143 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2144 LLVMConstInt(ctx
->i32
, 6, false), false);
2146 /* is the DATA_FORMAT == 8_8_8_8 */
2147 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2149 if (stype
== GLSL_TYPE_UINT
)
2150 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2151 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2152 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2154 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2155 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2156 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2158 /* replace the NUM FORMAT in the descriptor */
2159 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2160 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2162 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2164 /* don't modify the coordinates for this case */
2165 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2168 result
= ac_build_image_opcode(ctx
, args
);
2170 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2171 LLVMValueRef tmp
, tmp2
;
2173 /* if the cube workaround is in place, f2i the result. */
2174 for (c
= 0; c
< 4; c
++) {
2175 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2176 if (stype
== GLSL_TYPE_UINT
)
2177 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2179 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2180 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2181 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2182 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2183 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2184 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2190 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2191 const nir_tex_instr
*instr
,
2193 struct ac_image_args
*args
)
2195 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2196 return ac_build_buffer_load_format(&ctx
->ac
,
2199 LLVMConstInt(ctx
->ac
.i32
, 0, false),
2203 args
->opcode
= ac_image_sample
;
2204 args
->compare
= instr
->is_shadow
;
2206 switch (instr
->op
) {
2208 case nir_texop_txf_ms
:
2209 case nir_texop_samples_identical
:
2210 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2211 args
->compare
= false;
2212 args
->offset
= false;
2219 args
->level_zero
= true;
2224 case nir_texop_query_levels
:
2225 args
->opcode
= ac_image_get_resinfo
;
2228 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2229 args
->level_zero
= true;
2235 args
->opcode
= ac_image_gather4
;
2236 args
->level_zero
= true;
2239 args
->opcode
= ac_image_get_lod
;
2240 args
->compare
= false;
2241 args
->offset
= false;
2247 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2248 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2249 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2250 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2253 return ac_build_image_opcode(&ctx
->ac
, args
);
2256 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2257 nir_intrinsic_instr
*instr
)
2259 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2260 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2261 unsigned binding
= nir_intrinsic_binding(instr
);
2262 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2263 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2264 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2265 unsigned base_offset
= layout
->binding
[binding
].offset
;
2266 LLVMValueRef offset
, stride
;
2268 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2269 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2270 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2271 layout
->binding
[binding
].dynamic_offset_offset
;
2272 desc_ptr
= ctx
->push_constants
;
2273 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2274 stride
= LLVMConstInt(ctx
->i32
, 16, false);
2276 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
2278 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
2279 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2280 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2282 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2283 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2284 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2286 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2289 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2290 nir_intrinsic_instr
*instr
)
2292 LLVMValueRef ptr
, addr
;
2294 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
2295 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2297 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2298 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2300 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2303 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2304 const nir_intrinsic_instr
*instr
)
2306 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2308 return get_buffer_size(ctx
, desc
, false);
2310 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2311 nir_intrinsic_instr
*instr
)
2313 const char *store_name
;
2314 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2315 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2316 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2317 int components_32bit
= elem_size_mult
* instr
->num_components
;
2318 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2319 LLVMValueRef base_data
, base_offset
;
2320 LLVMValueRef params
[6];
2321 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
2323 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2324 get_src(ctx
, instr
->src
[1]), true);
2325 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 0, false); /* vindex */
2326 params
[4] = i1false
; /* glc */
2327 params
[5] = i1false
; /* slc */
2329 if (components_32bit
> 1)
2330 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2332 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2333 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2334 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2336 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2340 LLVMValueRef offset
;
2342 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2344 /* Due to an LLVM limitation, split 3-element writes
2345 * into a 2-element and a 1-element write. */
2347 writemask
|= 1 << (start
+ 2);
2351 start
*= elem_size_mult
;
2352 count
*= elem_size_mult
;
2355 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2360 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2362 } else if (count
== 2) {
2363 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->ac
.f32
, 2);
2365 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2366 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2367 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(v2f32
), tmp
,
2370 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2371 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2372 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2374 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2378 if (get_llvm_num_components(base_data
) > 1)
2379 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2380 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2383 store_name
= "llvm.amdgcn.buffer.store.f32";
2386 offset
= base_offset
;
2388 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2392 ac_build_intrinsic(&ctx
->ac
, store_name
,
2393 ctx
->ac
.voidt
, params
, 6, 0);
2397 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2398 const nir_intrinsic_instr
*instr
)
2401 LLVMValueRef params
[6];
2404 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2405 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2407 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2408 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2409 get_src(ctx
, instr
->src
[0]),
2411 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i32
, 0, false); /* vindex */
2412 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2413 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2415 switch (instr
->intrinsic
) {
2416 case nir_intrinsic_ssbo_atomic_add
:
2417 name
= "llvm.amdgcn.buffer.atomic.add";
2419 case nir_intrinsic_ssbo_atomic_imin
:
2420 name
= "llvm.amdgcn.buffer.atomic.smin";
2422 case nir_intrinsic_ssbo_atomic_umin
:
2423 name
= "llvm.amdgcn.buffer.atomic.umin";
2425 case nir_intrinsic_ssbo_atomic_imax
:
2426 name
= "llvm.amdgcn.buffer.atomic.smax";
2428 case nir_intrinsic_ssbo_atomic_umax
:
2429 name
= "llvm.amdgcn.buffer.atomic.umax";
2431 case nir_intrinsic_ssbo_atomic_and
:
2432 name
= "llvm.amdgcn.buffer.atomic.and";
2434 case nir_intrinsic_ssbo_atomic_or
:
2435 name
= "llvm.amdgcn.buffer.atomic.or";
2437 case nir_intrinsic_ssbo_atomic_xor
:
2438 name
= "llvm.amdgcn.buffer.atomic.xor";
2440 case nir_intrinsic_ssbo_atomic_exchange
:
2441 name
= "llvm.amdgcn.buffer.atomic.swap";
2443 case nir_intrinsic_ssbo_atomic_comp_swap
:
2444 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2450 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2453 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2454 const nir_intrinsic_instr
*instr
)
2456 LLVMValueRef results
[2];
2457 int load_components
;
2458 int num_components
= instr
->num_components
;
2459 if (instr
->dest
.ssa
.bit_size
== 64)
2460 num_components
*= 2;
2462 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2463 load_components
= MIN2(num_components
- i
, 4);
2464 const char *load_name
;
2465 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2466 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2467 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2469 if (load_components
== 3)
2470 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2471 else if (load_components
> 1)
2472 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2474 if (load_components
>= 3)
2475 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2476 else if (load_components
== 2)
2477 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2478 else if (load_components
== 1)
2479 load_name
= "llvm.amdgcn.buffer.load.f32";
2481 unreachable("unhandled number of components");
2483 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
2484 LLVMValueRef params
[] = {
2485 ctx
->abi
->load_ssbo(ctx
->abi
,
2486 get_src(ctx
, instr
->src
[0]),
2488 LLVMConstInt(ctx
->ac
.i32
, 0, false),
2494 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2498 LLVMValueRef ret
= results
[0];
2499 if (num_components
> 4 || num_components
== 3) {
2500 LLVMValueRef masks
[] = {
2501 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2502 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2503 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2504 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2507 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2508 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2509 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2512 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2513 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2516 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2517 const nir_intrinsic_instr
*instr
)
2519 LLVMValueRef results
[8], ret
;
2520 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2521 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2522 int num_components
= instr
->num_components
;
2524 if (ctx
->abi
->load_ubo
)
2525 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2527 if (instr
->dest
.ssa
.bit_size
== 64)
2528 num_components
*= 2;
2530 for (unsigned i
= 0; i
< num_components
; ++i
) {
2531 LLVMValueRef params
[] = {
2533 LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, 4 * i
, 0),
2536 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const.v4i32", ctx
->ac
.f32
,
2538 AC_FUNC_ATTR_READNONE
|
2539 AC_FUNC_ATTR_LEGACY
);
2543 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2544 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2545 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2549 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2550 bool vs_in
, unsigned *vertex_index_out
,
2551 LLVMValueRef
*vertex_index_ref
,
2552 unsigned *const_out
, LLVMValueRef
*indir_out
)
2554 unsigned const_offset
= 0;
2555 nir_deref
*tail
= &deref
->deref
;
2556 LLVMValueRef offset
= NULL
;
2558 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2560 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2561 if (vertex_index_out
)
2562 *vertex_index_out
= deref_array
->base_offset
;
2564 if (vertex_index_ref
) {
2565 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2566 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2567 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2569 *vertex_index_ref
= vtx
;
2573 if (deref
->var
->data
.compact
) {
2574 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2575 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2576 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2577 /* We always lower indirect dereferences for "compact" array vars. */
2578 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2580 const_offset
= deref_array
->base_offset
;
2584 while (tail
->child
!= NULL
) {
2585 const struct glsl_type
*parent_type
= tail
->type
;
2588 if (tail
->deref_type
== nir_deref_type_array
) {
2589 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2590 LLVMValueRef index
, stride
, local_offset
;
2591 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2593 const_offset
+= size
* deref_array
->base_offset
;
2594 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2597 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2598 index
= get_src(ctx
, deref_array
->indirect
);
2599 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2600 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2603 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2605 offset
= local_offset
;
2606 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2607 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2609 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2610 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2611 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2614 unreachable("unsupported deref type");
2618 if (const_offset
&& offset
)
2619 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2620 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2623 *const_out
= const_offset
;
2624 *indir_out
= offset
;
2628 lds_load(struct nir_to_llvm_context
*ctx
,
2629 LLVMValueRef dw_addr
)
2632 value
= ac_build_load(&ctx
->ac
, ctx
->lds
, dw_addr
);
2637 lds_store(struct nir_to_llvm_context
*ctx
,
2638 LLVMValueRef dw_addr
, LLVMValueRef value
)
2640 value
= LLVMBuildBitCast(ctx
->builder
, value
, ctx
->i32
, "");
2641 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
2645 /* The offchip buffer layout for TCS->TES is
2647 * - attribute 0 of patch 0 vertex 0
2648 * - attribute 0 of patch 0 vertex 1
2649 * - attribute 0 of patch 0 vertex 2
2651 * - attribute 0 of patch 1 vertex 0
2652 * - attribute 0 of patch 1 vertex 1
2654 * - attribute 1 of patch 0 vertex 0
2655 * - attribute 1 of patch 0 vertex 1
2657 * - per patch attribute 0 of patch 0
2658 * - per patch attribute 0 of patch 1
2661 * Note that every attribute has 4 components.
2663 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2664 LLVMValueRef vertex_index
,
2665 LLVMValueRef param_index
)
2667 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2668 LLVMValueRef param_stride
, constant16
;
2669 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2671 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2672 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2673 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2676 constant16
= LLVMConstInt(ctx
->i32
, 16, false);
2678 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2679 vertices_per_patch
, "");
2681 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2684 param_stride
= total_vertices
;
2686 base_addr
= rel_patch_id
;
2687 param_stride
= num_patches
;
2690 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2691 LLVMBuildMul(ctx
->builder
, param_index
,
2692 param_stride
, ""), "");
2694 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2696 if (!vertex_index
) {
2697 LLVMValueRef patch_data_offset
=
2698 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2700 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2701 patch_data_offset
, "");
2706 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2708 unsigned const_index
,
2710 LLVMValueRef vertex_index
,
2711 LLVMValueRef indir_index
)
2713 LLVMValueRef param_index
;
2716 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, param
, false),
2719 if (const_index
&& !is_compact
)
2720 param
+= const_index
;
2721 param_index
= LLVMConstInt(ctx
->i32
, param
, false);
2723 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2727 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2728 bool is_patch
, uint32_t param
)
2732 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2734 ctx
->tess_outputs_written
|= (1ull << param
);
2738 get_dw_address(struct nir_to_llvm_context
*ctx
,
2739 LLVMValueRef dw_addr
,
2741 unsigned const_index
,
2742 bool compact_const_index
,
2743 LLVMValueRef vertex_index
,
2744 LLVMValueRef stride
,
2745 LLVMValueRef indir_index
)
2750 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2751 LLVMBuildMul(ctx
->builder
,
2757 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2758 LLVMBuildMul(ctx
->builder
, indir_index
,
2759 LLVMConstInt(ctx
->i32
, 4, false), ""), "");
2760 else if (const_index
&& !compact_const_index
)
2761 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2762 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2764 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2765 LLVMConstInt(ctx
->i32
, param
* 4, false), "");
2767 if (const_index
&& compact_const_index
)
2768 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2769 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2774 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2775 nir_intrinsic_instr
*instr
)
2777 LLVMValueRef dw_addr
, stride
;
2778 unsigned const_index
;
2779 LLVMValueRef vertex_index
;
2780 LLVMValueRef indir_index
;
2782 LLVMValueRef value
[4], result
;
2783 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2784 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2785 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2786 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2787 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2788 &const_index
, &indir_index
);
2790 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2791 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2792 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2795 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2796 value
[i
] = lds_load(ctx
, dw_addr
);
2797 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2800 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2801 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2806 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2807 nir_intrinsic_instr
*instr
)
2809 LLVMValueRef dw_addr
;
2810 LLVMValueRef stride
= NULL
;
2811 LLVMValueRef value
[4], result
;
2812 LLVMValueRef vertex_index
= NULL
;
2813 LLVMValueRef indir_index
= NULL
;
2814 unsigned const_index
= 0;
2816 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2817 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2818 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2819 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2820 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2821 &const_index
, &indir_index
);
2823 if (!instr
->variables
[0]->var
->data
.patch
) {
2824 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2825 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2827 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2830 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2833 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2834 value
[i
] = lds_load(ctx
, dw_addr
);
2835 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2838 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2839 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2844 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2845 nir_intrinsic_instr
*instr
,
2849 LLVMValueRef dw_addr
;
2850 LLVMValueRef stride
= NULL
;
2851 LLVMValueRef buf_addr
= NULL
;
2852 LLVMValueRef vertex_index
= NULL
;
2853 LLVMValueRef indir_index
= NULL
;
2854 unsigned const_index
= 0;
2856 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2857 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2859 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2860 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2861 &const_index
, &indir_index
);
2863 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2864 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2865 is_compact
&& const_index
> 3) {
2870 if (!instr
->variables
[0]->var
->data
.patch
) {
2871 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2872 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2874 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2877 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2879 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2881 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2882 vertex_index
, indir_index
);
2884 unsigned base
= is_compact
? const_index
: 0;
2885 for (unsigned chan
= 0; chan
< 8; chan
++) {
2886 bool is_tess_factor
= false;
2887 if (!(writemask
& (1 << chan
)))
2889 LLVMValueRef value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
2891 lds_store(ctx
, dw_addr
, value
);
2893 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2894 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2895 is_tess_factor
= true;
2897 if (!is_tess_factor
&& writemask
!= 0xF)
2898 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2899 buf_addr
, ctx
->oc_lds
,
2900 4 * (base
+ chan
), 1, 0, true, false);
2902 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2906 if (writemask
== 0xF) {
2907 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2908 buf_addr
, ctx
->oc_lds
,
2909 (base
* 4), 1, 0, true, false);
2914 load_tes_input(struct nir_to_llvm_context
*ctx
,
2915 const nir_intrinsic_instr
*instr
)
2917 LLVMValueRef buf_addr
;
2918 LLVMValueRef result
;
2919 LLVMValueRef vertex_index
= NULL
;
2920 LLVMValueRef indir_index
= NULL
;
2921 unsigned const_index
= 0;
2923 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2924 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2926 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2927 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2928 &const_index
, &indir_index
);
2929 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2930 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2931 is_compact
&& const_index
> 3) {
2935 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2936 is_compact
, vertex_index
, indir_index
);
2938 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2939 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2940 result
= trim_vector(&ctx
->ac
, result
, instr
->num_components
);
2941 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2946 load_gs_input(struct nir_to_llvm_context
*ctx
,
2947 nir_intrinsic_instr
*instr
)
2949 LLVMValueRef indir_index
, vtx_offset
;
2950 unsigned const_index
;
2951 LLVMValueRef args
[9];
2952 unsigned param
, vtx_offset_param
;
2953 LLVMValueRef value
[4], result
;
2954 unsigned vertex_index
;
2955 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2956 false, &vertex_index
, NULL
,
2957 &const_index
, &indir_index
);
2958 vtx_offset_param
= vertex_index
;
2959 assert(vtx_offset_param
< 6);
2960 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2961 LLVMConstInt(ctx
->i32
, 4, false), "");
2963 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2964 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2966 args
[0] = ctx
->esgs_ring
;
2967 args
[1] = vtx_offset
;
2968 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2969 args
[3] = ctx
->i32zero
;
2970 args
[4] = ctx
->i32one
; /* OFFEN */
2971 args
[5] = ctx
->i32zero
; /* IDXEN */
2972 args
[6] = ctx
->i32one
; /* GLC */
2973 args
[7] = ctx
->i32zero
; /* SLC */
2974 args
[8] = ctx
->i32zero
; /* TFE */
2976 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2978 AC_FUNC_ATTR_READONLY
|
2979 AC_FUNC_ATTR_LEGACY
);
2981 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2987 build_gep_for_deref(struct ac_nir_context
*ctx
,
2988 nir_deref_var
*deref
)
2990 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
2991 assert(entry
->data
);
2992 LLVMValueRef val
= entry
->data
;
2993 nir_deref
*tail
= deref
->deref
.child
;
2994 while (tail
!= NULL
) {
2995 LLVMValueRef offset
;
2996 switch (tail
->deref_type
) {
2997 case nir_deref_type_array
: {
2998 nir_deref_array
*array
= nir_deref_as_array(tail
);
2999 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
3000 if (array
->deref_array_type
==
3001 nir_deref_array_type_indirect
) {
3002 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
3009 case nir_deref_type_struct
: {
3010 nir_deref_struct
*deref_struct
=
3011 nir_deref_as_struct(tail
);
3012 offset
= LLVMConstInt(ctx
->ac
.i32
,
3013 deref_struct
->index
, 0);
3017 unreachable("bad deref type");
3019 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3025 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3026 nir_intrinsic_instr
*instr
)
3028 LLVMValueRef values
[8];
3029 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3030 int ve
= instr
->dest
.ssa
.num_components
;
3031 LLVMValueRef indir_index
;
3033 unsigned const_index
;
3034 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3035 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3036 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3037 &const_index
, &indir_index
);
3039 if (instr
->dest
.ssa
.bit_size
== 64)
3042 switch (instr
->variables
[0]->var
->data
.mode
) {
3043 case nir_var_shader_in
:
3044 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3045 return load_tcs_input(ctx
->nctx
, instr
);
3046 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3047 return load_tes_input(ctx
->nctx
, instr
);
3048 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3049 return load_gs_input(ctx
->nctx
, instr
);
3051 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3053 unsigned count
= glsl_count_attribute_slots(
3054 instr
->variables
[0]->var
->type
,
3055 ctx
->stage
== MESA_SHADER_VERTEX
);
3057 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3058 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3061 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3065 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* 4];
3069 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3071 unsigned count
= glsl_count_attribute_slots(
3072 instr
->variables
[0]->var
->type
, false);
3074 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3075 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3078 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3082 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
3086 case nir_var_shared
: {
3087 LLVMValueRef address
= build_gep_for_deref(ctx
,
3088 instr
->variables
[0]);
3089 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3090 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3091 get_def_type(ctx
, &instr
->dest
.ssa
),
3094 case nir_var_shader_out
:
3095 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3096 return load_tcs_output(ctx
->nctx
, instr
);
3097 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3099 unsigned count
= glsl_count_attribute_slots(
3100 instr
->variables
[0]->var
->type
, false);
3102 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3103 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3106 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3110 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3111 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
3117 unreachable("unhandle variable mode");
3119 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
3120 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3124 visit_store_var(struct ac_nir_context
*ctx
,
3125 nir_intrinsic_instr
*instr
)
3127 LLVMValueRef temp_ptr
, value
;
3128 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3129 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3130 int writemask
= instr
->const_index
[0];
3131 LLVMValueRef indir_index
;
3132 unsigned const_index
;
3133 get_deref_offset(ctx
, instr
->variables
[0], false,
3134 NULL
, NULL
, &const_index
, &indir_index
);
3136 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3137 int old_writemask
= writemask
;
3139 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3140 LLVMVectorType(ctx
->ac
.f32
, get_llvm_num_components(src
) * 2),
3144 for (unsigned chan
= 0; chan
< 4; chan
++) {
3145 if (old_writemask
& (1 << chan
))
3146 writemask
|= 3u << (2 * chan
);
3150 switch (instr
->variables
[0]->var
->data
.mode
) {
3151 case nir_var_shader_out
:
3153 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3154 store_tcs_output(ctx
->nctx
, instr
, src
, writemask
);
3158 for (unsigned chan
= 0; chan
< 8; chan
++) {
3160 if (!(writemask
& (1 << chan
)))
3163 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
3165 if (instr
->variables
[0]->var
->data
.compact
)
3168 unsigned count
= glsl_count_attribute_slots(
3169 instr
->variables
[0]->var
->type
, false);
3171 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3172 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3173 stride
, true, true);
3175 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3176 value
, indir_index
, "");
3177 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3178 count
, stride
, tmp_vec
);
3181 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3183 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3188 for (unsigned chan
= 0; chan
< 8; chan
++) {
3189 if (!(writemask
& (1 << chan
)))
3192 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
3194 unsigned count
= glsl_count_attribute_slots(
3195 instr
->variables
[0]->var
->type
, false);
3197 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3198 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3201 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3202 value
, indir_index
, "");
3203 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3206 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3208 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3212 case nir_var_shared
: {
3213 int writemask
= instr
->const_index
[0];
3214 LLVMValueRef address
= build_gep_for_deref(ctx
,
3215 instr
->variables
[0]);
3216 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3217 unsigned components
=
3218 glsl_get_vector_elements(
3219 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3220 if (writemask
== (1 << components
) - 1) {
3221 val
= LLVMBuildBitCast(
3222 ctx
->ac
.builder
, val
,
3223 LLVMGetElementType(LLVMTypeOf(address
)), "");
3224 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3226 for (unsigned chan
= 0; chan
< 4; chan
++) {
3227 if (!(writemask
& (1 << chan
)))
3230 LLVMBuildStructGEP(ctx
->ac
.builder
,
3232 LLVMValueRef src
= llvm_extract_elem(&ctx
->ac
, val
,
3234 src
= LLVMBuildBitCast(
3235 ctx
->ac
.builder
, src
,
3236 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3237 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3247 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3250 case GLSL_SAMPLER_DIM_BUF
:
3252 case GLSL_SAMPLER_DIM_1D
:
3253 return array
? 2 : 1;
3254 case GLSL_SAMPLER_DIM_2D
:
3255 return array
? 3 : 2;
3256 case GLSL_SAMPLER_DIM_MS
:
3257 return array
? 4 : 3;
3258 case GLSL_SAMPLER_DIM_3D
:
3259 case GLSL_SAMPLER_DIM_CUBE
:
3261 case GLSL_SAMPLER_DIM_RECT
:
3262 case GLSL_SAMPLER_DIM_SUBPASS
:
3264 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3274 /* Adjust the sample index according to FMASK.
3276 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3277 * which is the identity mapping. Each nibble says which physical sample
3278 * should be fetched to get that sample.
3280 * For example, 0x11111100 means there are only 2 samples stored and
3281 * the second sample covers 3/4 of the pixel. When reading samples 0
3282 * and 1, return physical sample 0 (determined by the first two 0s
3283 * in FMASK), otherwise return physical sample 1.
3285 * The sample index should be adjusted as follows:
3286 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3288 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3289 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3290 LLVMValueRef coord_z
,
3291 LLVMValueRef sample_index
,
3292 LLVMValueRef fmask_desc_ptr
)
3294 LLVMValueRef fmask_load_address
[4];
3297 fmask_load_address
[0] = coord_x
;
3298 fmask_load_address
[1] = coord_y
;
3300 fmask_load_address
[2] = coord_z
;
3301 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3304 struct ac_image_args args
= {0};
3306 args
.opcode
= ac_image_load
;
3307 args
.da
= coord_z
? true : false;
3308 args
.resource
= fmask_desc_ptr
;
3310 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3312 res
= ac_build_image_opcode(ctx
, &args
);
3314 res
= ac_to_integer(ctx
, res
);
3315 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3316 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3318 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3322 LLVMValueRef sample_index4
=
3323 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3324 LLVMValueRef shifted_fmask
=
3325 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3326 LLVMValueRef final_sample
=
3327 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3329 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3330 * resource descriptor is 0 (invalid),
3332 LLVMValueRef fmask_desc
=
3333 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3336 LLVMValueRef fmask_word1
=
3337 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3340 LLVMValueRef word1_is_nonzero
=
3341 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3342 fmask_word1
, ctx
->i32_0
, "");
3344 /* Replace the MSAA sample index. */
3346 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3347 final_sample
, sample_index
, "");
3348 return sample_index
;
3351 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3352 const nir_intrinsic_instr
*instr
)
3354 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3355 if(instr
->variables
[0]->deref
.child
)
3356 type
= instr
->variables
[0]->deref
.child
->type
;
3358 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3359 LLVMValueRef coords
[4];
3360 LLVMValueRef masks
[] = {
3361 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3362 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3365 LLVMValueRef sample_index
= llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3368 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3369 bool is_array
= glsl_sampler_type_is_array(type
);
3370 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3371 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3372 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3373 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3374 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3375 count
= image_type_to_components_count(dim
, is_array
);
3378 LLVMValueRef fmask_load_address
[3];
3381 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3382 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3384 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3386 fmask_load_address
[2] = NULL
;
3388 for (chan
= 0; chan
< 2; ++chan
)
3389 fmask_load_address
[chan
] =
3390 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3391 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3392 ctx
->ac
.i32
, ""), "");
3393 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3395 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3396 fmask_load_address
[0],
3397 fmask_load_address
[1],
3398 fmask_load_address
[2],
3400 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, true, false));
3402 if (count
== 1 && !gfx9_1d
) {
3403 if (instr
->src
[0].ssa
->num_components
)
3404 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3411 for (chan
= 0; chan
< count
; ++chan
) {
3412 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3415 for (chan
= 0; chan
< 2; ++chan
)
3416 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3417 ctx
->ac
.i32
, ""), "");
3418 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3424 coords
[2] = coords
[1];
3425 coords
[1] = ctx
->ac
.i32_0
;
3427 coords
[1] = ctx
->ac
.i32_0
;
3432 coords
[count
] = sample_index
;
3437 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3440 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3445 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3446 const nir_intrinsic_instr
*instr
)
3448 LLVMValueRef params
[7];
3450 char intrinsic_name
[64];
3451 const nir_variable
*var
= instr
->variables
[0]->var
;
3452 const struct glsl_type
*type
= var
->type
;
3453 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
3454 LLVMValueRef i1true
= LLVMConstInt(ctx
->ac
.i1
, 1, false);
3456 if(instr
->variables
[0]->deref
.child
)
3457 type
= instr
->variables
[0]->deref
.child
->type
;
3459 type
= glsl_without_array(type
);
3460 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3461 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, true, false);
3462 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3463 ctx
->ac
.i32_0
, ""); /* vindex */
3464 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3465 params
[3] = i1false
; /* glc */
3466 params
[4] = i1false
; /* slc */
3467 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3470 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3471 res
= ac_to_integer(&ctx
->ac
, res
);
3473 bool is_da
= glsl_sampler_type_is_array(type
) ||
3474 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3475 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3476 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3477 LLVMValueRef da
= is_da
? i1true
: i1false
;
3478 LLVMValueRef glc
= i1false
;
3479 LLVMValueRef slc
= i1false
;
3481 params
[0] = get_image_coords(ctx
, instr
);
3482 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, true, false);
3483 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3484 if (HAVE_LLVM
<= 0x0309) {
3485 params
[3] = i1false
; /* r128 */
3490 LLVMValueRef lwe
= i1false
;
3497 ac_get_image_intr_name("llvm.amdgcn.image.load",
3498 ctx
->ac
.v4f32
, /* vdata */
3499 LLVMTypeOf(params
[0]), /* coords */
3500 LLVMTypeOf(params
[1]), /* rsrc */
3501 intrinsic_name
, sizeof(intrinsic_name
));
3503 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3504 params
, 7, AC_FUNC_ATTR_READONLY
);
3506 return ac_to_integer(&ctx
->ac
, res
);
3509 static void visit_image_store(struct ac_nir_context
*ctx
,
3510 nir_intrinsic_instr
*instr
)
3512 LLVMValueRef params
[8];
3513 char intrinsic_name
[64];
3514 const nir_variable
*var
= instr
->variables
[0]->var
;
3515 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3516 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
3517 LLVMValueRef i1true
= LLVMConstInt(ctx
->ac
.i1
, 1, false);
3518 LLVMValueRef glc
= i1false
;
3519 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3523 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3524 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3525 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, true, true);
3526 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3527 ctx
->ac
.i32_0
, ""); /* vindex */
3528 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3529 params
[4] = glc
; /* glc */
3530 params
[5] = i1false
; /* slc */
3531 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3534 bool is_da
= glsl_sampler_type_is_array(type
) ||
3535 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3536 LLVMValueRef da
= is_da
? i1true
: i1false
;
3537 LLVMValueRef slc
= i1false
;
3539 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3540 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3541 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, true, true);
3542 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3543 if (HAVE_LLVM
<= 0x0309) {
3544 params
[4] = i1false
; /* r128 */
3549 LLVMValueRef lwe
= i1false
;
3556 ac_get_image_intr_name("llvm.amdgcn.image.store",
3557 LLVMTypeOf(params
[0]), /* vdata */
3558 LLVMTypeOf(params
[1]), /* coords */
3559 LLVMTypeOf(params
[2]), /* rsrc */
3560 intrinsic_name
, sizeof(intrinsic_name
));
3562 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3568 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3569 const nir_intrinsic_instr
*instr
)
3571 LLVMValueRef params
[7];
3572 int param_count
= 0;
3573 const nir_variable
*var
= instr
->variables
[0]->var
;
3575 const char *atomic_name
;
3576 char intrinsic_name
[41];
3577 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3578 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
3579 LLVMValueRef i1true
= LLVMConstInt(ctx
->ac
.i1
, 1, false);
3580 MAYBE_UNUSED
int length
;
3582 switch (instr
->intrinsic
) {
3583 case nir_intrinsic_image_atomic_add
:
3584 atomic_name
= "add";
3586 case nir_intrinsic_image_atomic_min
:
3587 atomic_name
= "smin";
3589 case nir_intrinsic_image_atomic_max
:
3590 atomic_name
= "smax";
3592 case nir_intrinsic_image_atomic_and
:
3593 atomic_name
= "and";
3595 case nir_intrinsic_image_atomic_or
:
3598 case nir_intrinsic_image_atomic_xor
:
3599 atomic_name
= "xor";
3601 case nir_intrinsic_image_atomic_exchange
:
3602 atomic_name
= "swap";
3604 case nir_intrinsic_image_atomic_comp_swap
:
3605 atomic_name
= "cmpswap";
3611 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3612 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3613 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3615 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3616 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3618 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3619 ctx
->ac
.i32_0
, ""); /* vindex */
3620 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3621 params
[param_count
++] = i1false
; /* slc */
3623 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3624 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3626 char coords_type
[8];
3628 bool da
= glsl_sampler_type_is_array(type
) ||
3629 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3631 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3632 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3634 params
[param_count
++] = i1false
; /* r128 */
3635 params
[param_count
++] = da
? i1true
: i1false
; /* da */
3636 params
[param_count
++] = i1false
; /* slc */
3638 build_int_type_name(LLVMTypeOf(coords
),
3639 coords_type
, sizeof(coords_type
));
3641 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3642 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3645 assert(length
< sizeof(intrinsic_name
));
3646 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3649 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3650 const nir_intrinsic_instr
*instr
)
3653 const nir_variable
*var
= instr
->variables
[0]->var
;
3654 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3655 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3656 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3657 if(instr
->variables
[0]->deref
.child
)
3658 type
= instr
->variables
[0]->deref
.child
->type
;
3660 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3661 return get_buffer_size(ctx
,
3662 get_sampler_desc(ctx
, instr
->variables
[0],
3663 AC_DESC_BUFFER
, true, false), true);
3665 struct ac_image_args args
= { 0 };
3669 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, true, false);
3670 args
.opcode
= ac_image_get_resinfo
;
3671 args
.addr
= ctx
->ac
.i32_0
;
3673 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3675 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3677 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3678 glsl_sampler_type_is_array(type
)) {
3679 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3680 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3681 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3682 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3684 if (ctx
->ac
.chip_class
>= GFX9
&&
3685 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3686 glsl_sampler_type_is_array(type
)) {
3687 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3688 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3695 #define NOOP_WAITCNT 0xf7f
3696 #define LGKM_CNT 0x07f
3697 #define VM_CNT 0xf70
3699 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3702 LLVMValueRef args
[1] = {
3703 LLVMConstInt(ctx
->i32
, simm16
, false),
3705 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3706 ctx
->voidt
, args
, 1, 0);
3709 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3711 /* SI only (thanks to a hw bug workaround):
3712 * The real barrier instruction isn’t needed, because an entire patch
3713 * always fits into a single wave.
3715 if (ctx
->options
->chip_class
== SI
&&
3716 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3717 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3720 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3721 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3724 static void emit_discard_if(struct ac_nir_context
*ctx
,
3725 const nir_intrinsic_instr
*instr
)
3729 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
3730 get_src(ctx
, instr
->src
[0]),
3733 cond
= LLVMBuildSelect(ctx
->ac
.builder
, cond
,
3734 LLVMConstReal(ctx
->ac
.f32
, -1.0f
),
3736 ac_build_kill(&ctx
->ac
, cond
);
3740 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3742 LLVMValueRef result
;
3743 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3744 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3745 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
3747 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3750 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3751 const nir_intrinsic_instr
*instr
)
3753 LLVMValueRef ptr
, result
;
3754 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3755 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3757 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3758 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3759 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3761 LLVMAtomicOrderingSequentiallyConsistent
,
3762 LLVMAtomicOrderingSequentiallyConsistent
,
3765 LLVMAtomicRMWBinOp op
;
3766 switch (instr
->intrinsic
) {
3767 case nir_intrinsic_var_atomic_add
:
3768 op
= LLVMAtomicRMWBinOpAdd
;
3770 case nir_intrinsic_var_atomic_umin
:
3771 op
= LLVMAtomicRMWBinOpUMin
;
3773 case nir_intrinsic_var_atomic_umax
:
3774 op
= LLVMAtomicRMWBinOpUMax
;
3776 case nir_intrinsic_var_atomic_imin
:
3777 op
= LLVMAtomicRMWBinOpMin
;
3779 case nir_intrinsic_var_atomic_imax
:
3780 op
= LLVMAtomicRMWBinOpMax
;
3782 case nir_intrinsic_var_atomic_and
:
3783 op
= LLVMAtomicRMWBinOpAnd
;
3785 case nir_intrinsic_var_atomic_or
:
3786 op
= LLVMAtomicRMWBinOpOr
;
3788 case nir_intrinsic_var_atomic_xor
:
3789 op
= LLVMAtomicRMWBinOpXor
;
3791 case nir_intrinsic_var_atomic_exchange
:
3792 op
= LLVMAtomicRMWBinOpXchg
;
3798 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3799 LLVMAtomicOrderingSequentiallyConsistent
,
3805 #define INTERP_CENTER 0
3806 #define INTERP_CENTROID 1
3807 #define INTERP_SAMPLE 2
3809 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3810 enum glsl_interp_mode interp
, unsigned location
)
3813 case INTERP_MODE_FLAT
:
3816 case INTERP_MODE_SMOOTH
:
3817 case INTERP_MODE_NONE
:
3818 if (location
== INTERP_CENTER
)
3819 return ctx
->persp_center
;
3820 else if (location
== INTERP_CENTROID
)
3821 return ctx
->persp_centroid
;
3822 else if (location
== INTERP_SAMPLE
)
3823 return ctx
->persp_sample
;
3825 case INTERP_MODE_NOPERSPECTIVE
:
3826 if (location
== INTERP_CENTER
)
3827 return ctx
->linear_center
;
3828 else if (location
== INTERP_CENTROID
)
3829 return ctx
->linear_centroid
;
3830 else if (location
== INTERP_SAMPLE
)
3831 return ctx
->linear_sample
;
3837 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3838 LLVMValueRef sample_id
)
3840 LLVMValueRef result
;
3841 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_PS_SAMPLE_POSITIONS
, false));
3843 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3844 const_array(ctx
->v2f32
, 64), "");
3846 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3847 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
3852 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3854 LLVMValueRef values
[2];
3856 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3857 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3858 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3861 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3862 const nir_intrinsic_instr
*instr
)
3864 LLVMValueRef result
[2];
3865 LLVMValueRef interp_param
, attr_number
;
3868 LLVMValueRef src_c0
= NULL
;
3869 LLVMValueRef src_c1
= NULL
;
3870 LLVMValueRef src0
= NULL
;
3871 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3872 switch (instr
->intrinsic
) {
3873 case nir_intrinsic_interp_var_at_centroid
:
3874 location
= INTERP_CENTROID
;
3876 case nir_intrinsic_interp_var_at_sample
:
3877 case nir_intrinsic_interp_var_at_offset
:
3878 location
= INTERP_CENTER
;
3879 src0
= get_src(ctx
->nir
, instr
->src
[0]);
3885 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3886 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3887 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3888 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3889 LLVMValueRef sample_position
;
3890 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3892 /* fetch sample ID */
3893 sample_position
= load_sample_position(ctx
, src0
);
3895 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3896 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3897 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3898 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3900 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3901 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3903 if (location
== INTERP_CENTER
) {
3904 LLVMValueRef ij_out
[2];
3905 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
3908 * take the I then J parameters, and the DDX/Y for it, and
3909 * calculate the IJ inputs for the interpolator.
3910 * temp1 = ddx * offset/sample.x + I;
3911 * interp_param.I = ddy * offset/sample.y + temp1;
3912 * temp1 = ddx * offset/sample.x + J;
3913 * interp_param.J = ddy * offset/sample.y + temp1;
3915 for (unsigned i
= 0; i
< 2; i
++) {
3916 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3917 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3918 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3919 ddxy_out
, ix_ll
, "");
3920 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3921 ddxy_out
, iy_ll
, "");
3922 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3923 interp_param
, ix_ll
, "");
3924 LLVMValueRef temp1
, temp2
;
3926 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3929 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3930 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3932 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3933 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3935 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3936 temp2
, ctx
->i32
, "");
3938 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3942 for (chan
= 0; chan
< 2; chan
++) {
3943 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3946 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3947 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3948 LLVMValueRef i
= LLVMBuildExtractElement(
3949 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3950 LLVMValueRef j
= LLVMBuildExtractElement(
3951 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3953 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3954 llvm_chan
, attr_number
,
3955 ctx
->prim_mask
, i
, j
);
3957 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3958 LLVMConstInt(ctx
->i32
, 2, false),
3959 llvm_chan
, attr_number
,
3963 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3967 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3968 const nir_intrinsic_instr
*instr
)
3970 LLVMValueRef gs_next_vertex
;
3971 LLVMValueRef can_emit
, kill
;
3974 assert(instr
->const_index
[0] == 0);
3975 /* Write vertex attribute values to GSVS ring */
3976 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3977 ctx
->gs_next_vertex
,
3980 /* If this thread has already emitted the declared maximum number of
3981 * vertices, kill it: excessive vertex emissions are not supposed to
3982 * have any effect, and GS threads have no externally observable
3983 * effects other than emitting vertices.
3985 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3986 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3988 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3989 LLVMConstReal(ctx
->f32
, 1.0f
),
3990 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3991 ac_build_kill(&ctx
->ac
, kill
);
3993 /* loop num outputs */
3995 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3996 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
4001 if (!(ctx
->output_mask
& (1ull << i
)))
4004 if (i
== VARYING_SLOT_CLIP_DIST0
) {
4005 /* pack clip and cull into a single set of slots */
4006 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4010 for (unsigned j
= 0; j
< length
; j
++) {
4011 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
4013 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
4014 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
4015 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
4017 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
4019 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
4021 voffset
, ctx
->gs2vs_offset
, 0,
4027 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4029 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4031 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4035 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
4036 const nir_intrinsic_instr
*instr
)
4038 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4042 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
4043 const nir_intrinsic_instr
*instr
)
4045 LLVMValueRef coord
[4] = {
4052 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4053 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->f32one
,
4054 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4056 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
4057 return LLVMBuildBitCast(ctx
->builder
, result
,
4058 get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
4061 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4062 nir_intrinsic_instr
*instr
)
4064 LLVMValueRef result
= NULL
;
4066 switch (instr
->intrinsic
) {
4067 case nir_intrinsic_load_work_group_id
: {
4068 result
= ctx
->nctx
->workgroup_ids
;
4071 case nir_intrinsic_load_base_vertex
: {
4072 result
= ctx
->abi
->base_vertex
;
4075 case nir_intrinsic_load_vertex_id_zero_base
: {
4076 result
= ctx
->abi
->vertex_id
;
4079 case nir_intrinsic_load_local_invocation_id
: {
4080 result
= ctx
->nctx
->local_invocation_ids
;
4083 case nir_intrinsic_load_base_instance
:
4084 result
= ctx
->abi
->start_instance
;
4086 case nir_intrinsic_load_draw_id
:
4087 result
= ctx
->abi
->draw_id
;
4089 case nir_intrinsic_load_view_index
:
4090 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4092 case nir_intrinsic_load_invocation_id
:
4093 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4094 result
= unpack_param(&ctx
->ac
, ctx
->nctx
->tcs_rel_ids
, 8, 5);
4096 result
= ctx
->nctx
->gs_invocation_id
;
4098 case nir_intrinsic_load_primitive_id
:
4099 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4100 ctx
->nctx
->shader_info
->gs
.uses_prim_id
= true;
4101 result
= ctx
->nctx
->gs_prim_id
;
4102 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4103 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4104 result
= ctx
->nctx
->tcs_patch_id
;
4105 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4106 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4107 result
= ctx
->nctx
->tes_patch_id
;
4109 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4111 case nir_intrinsic_load_sample_id
:
4112 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4114 case nir_intrinsic_load_sample_pos
:
4115 result
= load_sample_pos(ctx
);
4117 case nir_intrinsic_load_sample_mask_in
:
4118 result
= ctx
->abi
->sample_coverage
;
4120 case nir_intrinsic_load_frag_coord
: {
4121 LLVMValueRef values
[4] = {
4122 ctx
->abi
->frag_pos
[0],
4123 ctx
->abi
->frag_pos
[1],
4124 ctx
->abi
->frag_pos
[2],
4125 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4127 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4130 case nir_intrinsic_load_front_face
:
4131 result
= ctx
->abi
->front_face
;
4133 case nir_intrinsic_load_instance_id
:
4134 result
= ctx
->abi
->instance_id
;
4136 case nir_intrinsic_load_num_work_groups
:
4137 result
= ctx
->nctx
->num_work_groups
;
4139 case nir_intrinsic_load_local_invocation_index
:
4140 result
= visit_load_local_invocation_index(ctx
->nctx
);
4142 case nir_intrinsic_load_push_constant
:
4143 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4145 case nir_intrinsic_vulkan_resource_index
:
4146 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4148 case nir_intrinsic_store_ssbo
:
4149 visit_store_ssbo(ctx
, instr
);
4151 case nir_intrinsic_load_ssbo
:
4152 result
= visit_load_buffer(ctx
, instr
);
4154 case nir_intrinsic_ssbo_atomic_add
:
4155 case nir_intrinsic_ssbo_atomic_imin
:
4156 case nir_intrinsic_ssbo_atomic_umin
:
4157 case nir_intrinsic_ssbo_atomic_imax
:
4158 case nir_intrinsic_ssbo_atomic_umax
:
4159 case nir_intrinsic_ssbo_atomic_and
:
4160 case nir_intrinsic_ssbo_atomic_or
:
4161 case nir_intrinsic_ssbo_atomic_xor
:
4162 case nir_intrinsic_ssbo_atomic_exchange
:
4163 case nir_intrinsic_ssbo_atomic_comp_swap
:
4164 result
= visit_atomic_ssbo(ctx
, instr
);
4166 case nir_intrinsic_load_ubo
:
4167 result
= visit_load_ubo_buffer(ctx
, instr
);
4169 case nir_intrinsic_get_buffer_size
:
4170 result
= visit_get_buffer_size(ctx
, instr
);
4172 case nir_intrinsic_load_var
:
4173 result
= visit_load_var(ctx
, instr
);
4175 case nir_intrinsic_store_var
:
4176 visit_store_var(ctx
, instr
);
4178 case nir_intrinsic_image_load
:
4179 result
= visit_image_load(ctx
, instr
);
4181 case nir_intrinsic_image_store
:
4182 visit_image_store(ctx
, instr
);
4184 case nir_intrinsic_image_atomic_add
:
4185 case nir_intrinsic_image_atomic_min
:
4186 case nir_intrinsic_image_atomic_max
:
4187 case nir_intrinsic_image_atomic_and
:
4188 case nir_intrinsic_image_atomic_or
:
4189 case nir_intrinsic_image_atomic_xor
:
4190 case nir_intrinsic_image_atomic_exchange
:
4191 case nir_intrinsic_image_atomic_comp_swap
:
4192 result
= visit_image_atomic(ctx
, instr
);
4194 case nir_intrinsic_image_size
:
4195 result
= visit_image_size(ctx
, instr
);
4197 case nir_intrinsic_discard
:
4198 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4199 LLVMVoidTypeInContext(ctx
->ac
.context
),
4200 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4202 case nir_intrinsic_discard_if
:
4203 emit_discard_if(ctx
, instr
);
4205 case nir_intrinsic_memory_barrier
:
4206 emit_waitcnt(ctx
->nctx
, VM_CNT
);
4208 case nir_intrinsic_barrier
:
4209 emit_barrier(ctx
->nctx
);
4211 case nir_intrinsic_var_atomic_add
:
4212 case nir_intrinsic_var_atomic_imin
:
4213 case nir_intrinsic_var_atomic_umin
:
4214 case nir_intrinsic_var_atomic_imax
:
4215 case nir_intrinsic_var_atomic_umax
:
4216 case nir_intrinsic_var_atomic_and
:
4217 case nir_intrinsic_var_atomic_or
:
4218 case nir_intrinsic_var_atomic_xor
:
4219 case nir_intrinsic_var_atomic_exchange
:
4220 case nir_intrinsic_var_atomic_comp_swap
:
4221 result
= visit_var_atomic(ctx
->nctx
, instr
);
4223 case nir_intrinsic_interp_var_at_centroid
:
4224 case nir_intrinsic_interp_var_at_sample
:
4225 case nir_intrinsic_interp_var_at_offset
:
4226 result
= visit_interp(ctx
->nctx
, instr
);
4228 case nir_intrinsic_emit_vertex
:
4229 visit_emit_vertex(ctx
->nctx
, instr
);
4231 case nir_intrinsic_end_primitive
:
4232 visit_end_primitive(ctx
->nctx
, instr
);
4234 case nir_intrinsic_load_tess_coord
:
4235 result
= visit_load_tess_coord(ctx
->nctx
, instr
);
4237 case nir_intrinsic_load_patch_vertices_in
:
4238 result
= LLVMConstInt(ctx
->ac
.i32
, ctx
->nctx
->options
->key
.tcs
.input_vertices
, false);
4241 fprintf(stderr
, "Unknown intrinsic: ");
4242 nir_print_instr(&instr
->instr
, stderr
);
4243 fprintf(stderr
, "\n");
4247 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4251 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4252 LLVMValueRef buffer
, bool write
)
4254 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4256 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4257 ctx
->shader_info
->fs
.writes_memory
= true;
4262 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4263 unsigned descriptor_set
,
4264 unsigned base_index
,
4265 unsigned constant_index
,
4267 enum ac_descriptor_type desc_type
,
4268 bool image
, bool write
)
4270 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4271 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4272 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4273 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4274 unsigned offset
= binding
->offset
;
4275 unsigned stride
= binding
->size
;
4277 LLVMBuilderRef builder
= ctx
->builder
;
4280 assert(base_index
< layout
->binding_count
);
4282 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4283 ctx
->shader_info
->fs
.writes_memory
= true;
4285 switch (desc_type
) {
4295 case AC_DESC_SAMPLER
:
4297 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4302 case AC_DESC_BUFFER
:
4307 unreachable("invalid desc_type\n");
4310 offset
+= constant_index
* stride
;
4312 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4313 (!index
|| binding
->immutable_samplers_equal
)) {
4314 if (binding
->immutable_samplers_equal
)
4317 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4319 LLVMValueRef constants
[] = {
4320 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 0], 0),
4321 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 1], 0),
4322 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 2], 0),
4323 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 3], 0),
4325 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4328 assert(stride
% type_size
== 0);
4331 index
= ctx
->i32zero
;
4333 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
4335 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
4336 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4338 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4341 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4342 const nir_deref_var
*deref
,
4343 enum ac_descriptor_type desc_type
,
4344 bool image
, bool write
)
4346 LLVMValueRef index
= NULL
;
4347 unsigned constant_index
= 0;
4348 const nir_deref
*tail
= &deref
->deref
;
4350 while (tail
->child
) {
4351 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4352 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4357 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4359 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4360 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4362 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4363 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4368 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4371 constant_index
+= child
->base_offset
* array_size
;
4373 tail
= &child
->deref
;
4376 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4377 deref
->var
->data
.descriptor_set
,
4378 deref
->var
->data
.binding
,
4379 constant_index
, index
,
4380 desc_type
, image
, write
);
4383 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4384 struct ac_image_args
*args
,
4385 const nir_tex_instr
*instr
,
4387 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4388 LLVMValueRef
*param
, unsigned count
,
4391 unsigned is_rect
= 0;
4392 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4394 if (op
== nir_texop_lod
)
4396 /* Pad to power of two vector */
4397 while (count
< util_next_power_of_two(count
))
4398 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4401 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4403 args
->addr
= param
[0];
4405 args
->resource
= res_ptr
;
4406 args
->sampler
= samp_ptr
;
4408 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4409 args
->addr
= param
[0];
4413 args
->dmask
= dmask
;
4414 args
->unorm
= is_rect
;
4418 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4421 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4422 * filtering manually. The driver sets img7 to a mask clearing
4423 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4424 * s_and_b32 samp0, samp0, img7
4427 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4429 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4430 LLVMValueRef res
, LLVMValueRef samp
)
4432 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4433 LLVMValueRef img7
, samp0
;
4435 if (ctx
->ac
.chip_class
>= VI
)
4438 img7
= LLVMBuildExtractElement(builder
, res
,
4439 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4440 samp0
= LLVMBuildExtractElement(builder
, samp
,
4441 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4442 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4443 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4444 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4447 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4448 nir_tex_instr
*instr
,
4449 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4450 LLVMValueRef
*fmask_ptr
)
4452 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4453 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, false, false);
4455 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, false, false);
4458 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, false, false);
4460 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, false, false);
4461 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4462 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4464 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4465 instr
->op
== nir_texop_samples_identical
))
4466 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, false, false);
4469 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4472 coord
= ac_to_float(ctx
, coord
);
4473 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4474 coord
= ac_to_integer(ctx
, coord
);
4478 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4480 LLVMValueRef result
= NULL
;
4481 struct ac_image_args args
= { 0 };
4482 unsigned dmask
= 0xf;
4483 LLVMValueRef address
[16];
4484 LLVMValueRef coords
[5];
4485 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4486 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4487 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4488 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4489 LLVMValueRef derivs
[6];
4490 unsigned chan
, count
= 0;
4491 unsigned const_src
= 0, num_deriv_comp
= 0;
4492 bool lod_is_zero
= false;
4494 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4496 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4497 switch (instr
->src
[i
].src_type
) {
4498 case nir_tex_src_coord
:
4499 coord
= get_src(ctx
, instr
->src
[i
].src
);
4501 case nir_tex_src_projector
:
4503 case nir_tex_src_comparator
:
4504 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4506 case nir_tex_src_offset
:
4507 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4510 case nir_tex_src_bias
:
4511 bias
= get_src(ctx
, instr
->src
[i
].src
);
4513 case nir_tex_src_lod
: {
4514 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4516 if (val
&& val
->i32
[0] == 0)
4518 lod
= get_src(ctx
, instr
->src
[i
].src
);
4521 case nir_tex_src_ms_index
:
4522 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4524 case nir_tex_src_ms_mcs
:
4526 case nir_tex_src_ddx
:
4527 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4528 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4530 case nir_tex_src_ddy
:
4531 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4533 case nir_tex_src_texture_offset
:
4534 case nir_tex_src_sampler_offset
:
4535 case nir_tex_src_plane
:
4541 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4542 result
= get_buffer_size(ctx
, res_ptr
, true);
4546 if (instr
->op
== nir_texop_texture_samples
) {
4547 LLVMValueRef res
, samples
, is_msaa
;
4548 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4549 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4550 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4551 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4552 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4553 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4554 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4555 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4556 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4558 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4559 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4560 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4561 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4562 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4564 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4571 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4572 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4574 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4575 LLVMValueRef offset
[3], pack
;
4576 for (chan
= 0; chan
< 3; ++chan
)
4577 offset
[chan
] = ctx
->ac
.i32_0
;
4580 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4581 offset
[chan
] = llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4582 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4583 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4585 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4586 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4588 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4589 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4590 address
[count
++] = pack
;
4593 /* pack LOD bias value */
4594 if (instr
->op
== nir_texop_txb
&& bias
) {
4595 address
[count
++] = bias
;
4598 /* Pack depth comparison value */
4599 if (instr
->is_shadow
&& comparator
) {
4600 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4601 llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4603 /* TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4604 * so the depth comparison value isn't clamped for Z16 and
4605 * Z24 anymore. Do it manually here.
4607 * It's unnecessary if the original texture format was
4608 * Z32_FLOAT, but we don't know that here.
4610 if (ctx
->ac
.chip_class
== VI
)
4611 z
= ac_build_clamp(&ctx
->ac
, z
);
4613 address
[count
++] = z
;
4616 /* pack derivatives */
4618 int num_src_deriv_channels
, num_dest_deriv_channels
;
4619 switch (instr
->sampler_dim
) {
4620 case GLSL_SAMPLER_DIM_3D
:
4621 case GLSL_SAMPLER_DIM_CUBE
:
4623 num_src_deriv_channels
= 3;
4624 num_dest_deriv_channels
= 3;
4626 case GLSL_SAMPLER_DIM_2D
:
4628 num_src_deriv_channels
= 2;
4629 num_dest_deriv_channels
= 2;
4632 case GLSL_SAMPLER_DIM_1D
:
4633 num_src_deriv_channels
= 1;
4634 if (ctx
->ac
.chip_class
>= GFX9
) {
4635 num_dest_deriv_channels
= 2;
4638 num_dest_deriv_channels
= 1;
4644 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4645 derivs
[i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4646 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4648 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4649 derivs
[i
] = ctx
->ac
.f32_0
;
4650 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4654 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4655 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4656 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4657 if (instr
->coord_components
== 3)
4658 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4659 ac_prepare_cube_coords(&ctx
->ac
,
4660 instr
->op
== nir_texop_txd
, instr
->is_array
,
4661 instr
->op
== nir_texop_lod
, coords
, derivs
);
4667 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4668 address
[count
++] = derivs
[i
];
4671 /* Pack texture coordinates */
4673 address
[count
++] = coords
[0];
4674 if (instr
->coord_components
> 1) {
4675 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4676 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4678 address
[count
++] = coords
[1];
4680 if (instr
->coord_components
> 2) {
4681 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4682 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4683 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4684 instr
->op
!= nir_texop_txf
) {
4685 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4687 address
[count
++] = coords
[2];
4690 if (ctx
->ac
.chip_class
>= GFX9
) {
4691 LLVMValueRef filler
;
4692 if (instr
->op
== nir_texop_txf
)
4693 filler
= ctx
->ac
.i32_0
;
4695 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4697 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4698 if (instr
->is_array
) {
4699 address
[count
] = address
[count
- 1];
4700 address
[count
- 1] = filler
;
4703 address
[count
++] = filler
;
4709 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4710 instr
->op
== nir_texop_txf
)) {
4711 address
[count
++] = lod
;
4712 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4713 address
[count
++] = sample_index
;
4714 } else if(instr
->op
== nir_texop_txs
) {
4717 address
[count
++] = lod
;
4719 address
[count
++] = ctx
->ac
.i32_0
;
4722 for (chan
= 0; chan
< count
; chan
++) {
4723 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4724 address
[chan
], ctx
->ac
.i32
, "");
4727 if (instr
->op
== nir_texop_samples_identical
) {
4728 LLVMValueRef txf_address
[4];
4729 struct ac_image_args txf_args
= { 0 };
4730 unsigned txf_count
= count
;
4731 memcpy(txf_address
, address
, sizeof(txf_address
));
4733 if (!instr
->is_array
)
4734 txf_address
[2] = ctx
->ac
.i32_0
;
4735 txf_address
[3] = ctx
->ac
.i32_0
;
4737 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4739 txf_address
, txf_count
, 0xf);
4741 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4743 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4744 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4748 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4749 instr
->op
!= nir_texop_txs
) {
4750 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4751 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4754 instr
->is_array
? address
[2] : NULL
,
4755 address
[sample_chan
],
4759 if (offsets
&& instr
->op
== nir_texop_txf
) {
4760 nir_const_value
*const_offset
=
4761 nir_src_as_const_value(instr
->src
[const_src
].src
);
4762 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4763 assert(const_offset
);
4764 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4765 if (num_offsets
> 2)
4766 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4767 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4768 if (num_offsets
> 1)
4769 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4770 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4771 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4772 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4776 /* TODO TG4 support */
4777 if (instr
->op
== nir_texop_tg4
) {
4778 if (instr
->is_shadow
)
4781 dmask
= 1 << instr
->component
;
4783 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4784 res_ptr
, samp_ptr
, address
, count
, dmask
);
4786 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4788 if (instr
->op
== nir_texop_query_levels
)
4789 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4790 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4791 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4792 instr
->op
!= nir_texop_tg4
)
4793 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4794 else if (instr
->op
== nir_texop_txs
&&
4795 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4797 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4798 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4799 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4800 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4801 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4802 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4803 instr
->op
== nir_texop_txs
&&
4804 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4806 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4807 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4808 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4810 } else if (instr
->dest
.ssa
.num_components
!= 4)
4811 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4815 assert(instr
->dest
.is_ssa
);
4816 result
= ac_to_integer(&ctx
->ac
, result
);
4817 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4822 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4824 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4825 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4827 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4828 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4831 static void visit_post_phi(struct ac_nir_context
*ctx
,
4832 nir_phi_instr
*instr
,
4833 LLVMValueRef llvm_phi
)
4835 nir_foreach_phi_src(src
, instr
) {
4836 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4837 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4839 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4843 static void phi_post_pass(struct ac_nir_context
*ctx
)
4845 struct hash_entry
*entry
;
4846 hash_table_foreach(ctx
->phis
, entry
) {
4847 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4848 (LLVMValueRef
)entry
->data
);
4853 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4854 const nir_ssa_undef_instr
*instr
)
4856 unsigned num_components
= instr
->def
.num_components
;
4859 if (num_components
== 1)
4860 undef
= LLVMGetUndef(ctx
->ac
.i32
);
4862 undef
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, num_components
));
4864 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4867 static void visit_jump(struct ac_nir_context
*ctx
,
4868 const nir_jump_instr
*instr
)
4870 switch (instr
->type
) {
4871 case nir_jump_break
:
4872 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
4873 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4875 case nir_jump_continue
:
4876 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4877 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4880 fprintf(stderr
, "Unknown NIR jump instr: ");
4881 nir_print_instr(&instr
->instr
, stderr
);
4882 fprintf(stderr
, "\n");
4887 static void visit_cf_list(struct ac_nir_context
*ctx
,
4888 struct exec_list
*list
);
4890 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4892 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4893 nir_foreach_instr(instr
, block
)
4895 switch (instr
->type
) {
4896 case nir_instr_type_alu
:
4897 visit_alu(ctx
, nir_instr_as_alu(instr
));
4899 case nir_instr_type_load_const
:
4900 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4902 case nir_instr_type_intrinsic
:
4903 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4905 case nir_instr_type_tex
:
4906 visit_tex(ctx
, nir_instr_as_tex(instr
));
4908 case nir_instr_type_phi
:
4909 visit_phi(ctx
, nir_instr_as_phi(instr
));
4911 case nir_instr_type_ssa_undef
:
4912 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4914 case nir_instr_type_jump
:
4915 visit_jump(ctx
, nir_instr_as_jump(instr
));
4918 fprintf(stderr
, "Unknown NIR instr type: ");
4919 nir_print_instr(instr
, stderr
);
4920 fprintf(stderr
, "\n");
4925 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4928 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4930 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4932 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4933 LLVMBasicBlockRef merge_block
=
4934 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4935 LLVMBasicBlockRef if_block
=
4936 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4937 LLVMBasicBlockRef else_block
= merge_block
;
4938 if (!exec_list_is_empty(&if_stmt
->else_list
))
4939 else_block
= LLVMAppendBasicBlockInContext(
4940 ctx
->ac
.context
, fn
, "");
4942 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
4943 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4944 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
4946 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
4947 visit_cf_list(ctx
, &if_stmt
->then_list
);
4948 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4949 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4951 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4952 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
4953 visit_cf_list(ctx
, &if_stmt
->else_list
);
4954 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4955 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4958 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
4961 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4963 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4964 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4965 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4967 ctx
->continue_block
=
4968 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4970 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4972 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4973 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
4974 visit_cf_list(ctx
, &loop
->body
);
4976 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4977 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4978 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
4980 ctx
->continue_block
= continue_parent
;
4981 ctx
->break_block
= break_parent
;
4984 static void visit_cf_list(struct ac_nir_context
*ctx
,
4985 struct exec_list
*list
)
4987 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4989 switch (node
->type
) {
4990 case nir_cf_node_block
:
4991 visit_block(ctx
, nir_cf_node_as_block(node
));
4994 case nir_cf_node_if
:
4995 visit_if(ctx
, nir_cf_node_as_if(node
));
4998 case nir_cf_node_loop
:
4999 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5009 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
5010 struct nir_variable
*variable
)
5012 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5013 LLVMValueRef t_offset
;
5014 LLVMValueRef t_list
;
5016 LLVMValueRef buffer_index
;
5017 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5018 int idx
= variable
->data
.location
;
5019 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5021 variable
->data
.driver_location
= idx
* 4;
5023 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
5024 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5025 ctx
->abi
.start_instance
, "");
5026 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
5027 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5029 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5030 ctx
->abi
.base_vertex
, "");
5032 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5033 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
5035 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5037 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5039 LLVMConstInt(ctx
->i32
, 0, false),
5042 for (unsigned chan
= 0; chan
< 4; chan
++) {
5043 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
5044 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5045 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5046 input
, llvm_chan
, ""));
5051 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5053 LLVMValueRef interp_param
,
5054 LLVMValueRef prim_mask
,
5055 LLVMValueRef result
[4])
5057 LLVMValueRef attr_number
;
5060 bool interp
= interp_param
!= NULL
;
5062 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
5064 /* fs.constant returns the param from the middle vertex, so it's not
5065 * really useful for flat shading. It's meant to be used for custom
5066 * interpolation (but the intrinsic can't fetch from the other two
5069 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5070 * to do the right thing. The only reason we use fs.constant is that
5071 * fs.interp cannot be used on integers, because they can be equal
5075 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5076 LLVMVectorType(ctx
->f32
, 2), "");
5078 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5080 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5084 for (chan
= 0; chan
< 4; chan
++) {
5085 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
5088 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5093 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5094 LLVMConstInt(ctx
->i32
, 2, false),
5103 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5104 struct nir_variable
*variable
)
5106 int idx
= variable
->data
.location
;
5107 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5108 LLVMValueRef interp
;
5110 variable
->data
.driver_location
= idx
* 4;
5111 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5113 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5114 unsigned interp_type
;
5115 if (variable
->data
.sample
) {
5116 interp_type
= INTERP_SAMPLE
;
5117 ctx
->shader_info
->info
.ps
.force_persample
= true;
5118 } else if (variable
->data
.centroid
)
5119 interp_type
= INTERP_CENTROID
;
5121 interp_type
= INTERP_CENTER
;
5123 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5127 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5128 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5133 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5134 struct nir_shader
*nir
) {
5135 nir_foreach_variable(variable
, &nir
->inputs
)
5136 handle_vs_input_decl(ctx
, variable
);
5140 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5141 struct nir_shader
*nir
)
5143 if (!ctx
->options
->key
.fs
.multisample
)
5146 bool uses_center
= false;
5147 bool uses_centroid
= false;
5148 nir_foreach_variable(variable
, &nir
->inputs
) {
5149 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5150 variable
->data
.sample
)
5153 if (variable
->data
.centroid
)
5154 uses_centroid
= true;
5159 if (uses_center
&& uses_centroid
) {
5160 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5161 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5162 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5167 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5168 struct nir_shader
*nir
)
5170 prepare_interp_optimize(ctx
, nir
);
5172 nir_foreach_variable(variable
, &nir
->inputs
)
5173 handle_fs_input_decl(ctx
, variable
);
5177 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5178 ctx
->shader_info
->info
.needs_multiview_view_index
)
5179 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5181 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5182 LLVMValueRef interp_param
;
5183 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5185 if (!(ctx
->input_mask
& (1ull << i
)))
5188 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5189 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5190 interp_param
= *inputs
;
5191 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5195 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5197 } else if (i
== VARYING_SLOT_POS
) {
5198 for(int i
= 0; i
< 3; ++i
)
5199 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5201 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
,
5202 ctx
->abi
.frag_pos
[3]);
5205 ctx
->shader_info
->fs
.num_interp
= index
;
5206 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5207 ctx
->shader_info
->fs
.has_pcoord
= true;
5208 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5209 ctx
->shader_info
->fs
.prim_id_input
= true;
5210 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5211 ctx
->shader_info
->fs
.layer_input
= true;
5212 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5214 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5215 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5219 ac_build_alloca(struct ac_llvm_context
*ac
,
5223 LLVMBuilderRef builder
= ac
->builder
;
5224 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5225 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5226 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5227 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5228 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5232 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5234 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5237 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5238 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5240 LLVMDisposeBuilder(first_builder
);
5245 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5249 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5250 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5255 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5256 struct nir_variable
*variable
,
5257 struct nir_shader
*shader
,
5258 gl_shader_stage stage
)
5260 int idx
= variable
->data
.location
+ variable
->data
.index
;
5261 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5262 uint64_t mask_attribs
;
5264 variable
->data
.driver_location
= idx
* 4;
5266 /* tess ctrl has it's own load/store paths for outputs */
5267 if (stage
== MESA_SHADER_TESS_CTRL
)
5270 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5271 if (stage
== MESA_SHADER_VERTEX
||
5272 stage
== MESA_SHADER_TESS_EVAL
||
5273 stage
== MESA_SHADER_GEOMETRY
) {
5274 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5275 int length
= shader
->info
.clip_distance_array_size
+
5276 shader
->info
.cull_distance_array_size
;
5277 if (stage
== MESA_SHADER_VERTEX
) {
5278 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5279 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5281 if (stage
== MESA_SHADER_TESS_EVAL
) {
5282 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5283 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5290 mask_attribs
= 1ull << idx
;
5294 ctx
->output_mask
|= mask_attribs
;
5298 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5299 struct nir_shader
*nir
,
5300 struct nir_variable
*variable
)
5302 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5303 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5305 /* tess ctrl has it's own load/store paths for outputs */
5306 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5309 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5310 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5311 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5312 int idx
= variable
->data
.location
+ variable
->data
.index
;
5313 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5314 int length
= nir
->info
.clip_distance_array_size
+
5315 nir
->info
.cull_distance_array_size
;
5324 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5325 for (unsigned chan
= 0; chan
< 4; chan
++) {
5326 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5327 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5333 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5334 enum glsl_base_type type
)
5338 case GLSL_TYPE_UINT
:
5339 case GLSL_TYPE_BOOL
:
5340 case GLSL_TYPE_SUBROUTINE
:
5342 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5344 case GLSL_TYPE_INT64
:
5345 case GLSL_TYPE_UINT64
:
5347 case GLSL_TYPE_DOUBLE
:
5350 unreachable("unknown GLSL type");
5355 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5356 const struct glsl_type
*type
)
5358 if (glsl_type_is_scalar(type
)) {
5359 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5362 if (glsl_type_is_vector(type
)) {
5363 return LLVMVectorType(
5364 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5365 glsl_get_vector_elements(type
));
5368 if (glsl_type_is_matrix(type
)) {
5369 return LLVMArrayType(
5370 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5371 glsl_get_matrix_columns(type
));
5374 if (glsl_type_is_array(type
)) {
5375 return LLVMArrayType(
5376 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5377 glsl_get_length(type
));
5380 assert(glsl_type_is_struct(type
));
5382 LLVMTypeRef member_types
[glsl_get_length(type
)];
5384 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5386 glsl_to_llvm_type(ctx
,
5387 glsl_get_struct_field(type
, i
));
5390 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5391 glsl_get_length(type
), false);
5395 setup_locals(struct ac_nir_context
*ctx
,
5396 struct nir_function
*func
)
5399 ctx
->num_locals
= 0;
5400 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5401 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5402 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5403 ctx
->num_locals
+= attrib_count
;
5405 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5409 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5410 for (j
= 0; j
< 4; j
++) {
5411 ctx
->locals
[i
* 4 + j
] =
5412 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5418 setup_shared(struct ac_nir_context
*ctx
,
5419 struct nir_shader
*nir
)
5421 nir_foreach_variable(variable
, &nir
->shared
) {
5422 LLVMValueRef shared
=
5423 LLVMAddGlobalInAddressSpace(
5424 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5425 variable
->name
? variable
->name
: "",
5427 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5432 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5434 v
= ac_to_float(ctx
, v
);
5435 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5436 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5440 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5441 LLVMValueRef src0
, LLVMValueRef src1
)
5443 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
5444 LLVMValueRef comp
[2];
5446 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5447 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5448 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5449 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5452 /* Initialize arguments for the shader export intrinsic */
5454 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5455 LLVMValueRef
*values
,
5457 struct ac_export_args
*args
)
5459 /* Default is 0xf. Adjusted below depending on the format. */
5460 args
->enabled_channels
= 0xf;
5462 /* Specify whether the EXEC mask represents the valid mask */
5463 args
->valid_mask
= 0;
5465 /* Specify whether this is the last export */
5468 /* Specify the target we are exporting */
5469 args
->target
= target
;
5471 args
->compr
= false;
5472 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
5473 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
5474 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
5475 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
5480 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5481 LLVMValueRef val
[4];
5482 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5483 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5484 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5485 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5487 switch(col_format
) {
5488 case V_028714_SPI_SHADER_ZERO
:
5489 args
->enabled_channels
= 0; /* writemask */
5490 args
->target
= V_008DFC_SQ_EXP_NULL
;
5493 case V_028714_SPI_SHADER_32_R
:
5494 args
->enabled_channels
= 1;
5495 args
->out
[0] = values
[0];
5498 case V_028714_SPI_SHADER_32_GR
:
5499 args
->enabled_channels
= 0x3;
5500 args
->out
[0] = values
[0];
5501 args
->out
[1] = values
[1];
5504 case V_028714_SPI_SHADER_32_AR
:
5505 args
->enabled_channels
= 0x9;
5506 args
->out
[0] = values
[0];
5507 args
->out
[3] = values
[3];
5510 case V_028714_SPI_SHADER_FP16_ABGR
:
5513 for (unsigned chan
= 0; chan
< 2; chan
++) {
5514 LLVMValueRef pack_args
[2] = {
5516 values
[2 * chan
+ 1]
5518 LLVMValueRef packed
;
5520 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5521 args
->out
[chan
] = packed
;
5525 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5526 for (unsigned chan
= 0; chan
< 4; chan
++) {
5527 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5528 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5529 LLVMConstReal(ctx
->f32
, 65535), "");
5530 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5531 LLVMConstReal(ctx
->f32
, 0.5), "");
5532 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5537 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5538 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5541 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5542 for (unsigned chan
= 0; chan
< 4; chan
++) {
5543 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5544 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5545 LLVMConstReal(ctx
->f32
, 32767), "");
5547 /* If positive, add 0.5, else add -0.5. */
5548 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5549 LLVMBuildSelect(ctx
->builder
,
5550 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5551 val
[chan
], ctx
->f32zero
, ""),
5552 LLVMConstReal(ctx
->f32
, 0.5),
5553 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
5554 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
5558 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5559 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5562 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5563 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
5564 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5565 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
5567 for (unsigned chan
= 0; chan
< 4; chan
++) {
5568 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5569 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5573 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5574 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5578 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5579 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
5580 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5581 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
5582 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5583 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->i32one
;
5584 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
5587 for (unsigned chan
= 0; chan
< 4; chan
++) {
5588 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5589 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5590 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5594 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5595 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5600 case V_028714_SPI_SHADER_32_ABGR
:
5601 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5605 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5607 for (unsigned i
= 0; i
< 4; ++i
)
5608 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5612 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5613 bool export_prim_id
,
5614 struct ac_vs_output_info
*outinfo
)
5616 uint32_t param_count
= 0;
5618 unsigned pos_idx
, num_pos_exports
= 0;
5619 struct ac_export_args args
, pos_args
[4] = {};
5620 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5623 if (ctx
->options
->key
.has_multiview_view_index
) {
5624 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5626 for(unsigned i
= 0; i
< 4; ++i
)
5627 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5628 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5631 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5632 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5635 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5636 sizeof(outinfo
->vs_output_param_offset
));
5638 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5639 LLVMValueRef slots
[8];
5642 if (outinfo
->cull_dist_mask
)
5643 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5645 i
= VARYING_SLOT_CLIP_DIST0
;
5646 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5647 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5648 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5650 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5651 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5653 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5654 target
= V_008DFC_SQ_EXP_POS
+ 3;
5655 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5656 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5657 &args
, sizeof(args
));
5660 target
= V_008DFC_SQ_EXP_POS
+ 2;
5661 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5662 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5663 &args
, sizeof(args
));
5667 LLVMValueRef pos_values
[4] = {ctx
->f32zero
, ctx
->f32zero
, ctx
->f32zero
, ctx
->f32one
};
5668 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5669 for (unsigned j
= 0; j
< 4; j
++)
5670 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5671 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5673 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5675 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5676 outinfo
->writes_pointsize
= true;
5677 psize_value
= LLVMBuildLoad(ctx
->builder
,
5678 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5681 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5682 outinfo
->writes_layer
= true;
5683 layer_value
= LLVMBuildLoad(ctx
->builder
,
5684 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5687 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5688 outinfo
->writes_viewport_index
= true;
5689 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5690 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5693 if (outinfo
->writes_pointsize
||
5694 outinfo
->writes_layer
||
5695 outinfo
->writes_viewport_index
) {
5696 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5697 (outinfo
->writes_layer
== true ? 4 : 0));
5698 pos_args
[1].valid_mask
= 0;
5699 pos_args
[1].done
= 0;
5700 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5701 pos_args
[1].compr
= 0;
5702 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
5703 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
5704 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
5705 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
5707 if (outinfo
->writes_pointsize
== true)
5708 pos_args
[1].out
[0] = psize_value
;
5709 if (outinfo
->writes_layer
== true)
5710 pos_args
[1].out
[2] = layer_value
;
5711 if (outinfo
->writes_viewport_index
== true) {
5712 if (ctx
->options
->chip_class
>= GFX9
) {
5713 /* GFX9 has the layer in out.z[10:0] and the viewport
5714 * index in out.z[19:16].
5716 LLVMValueRef v
= viewport_index_value
;
5717 v
= ac_to_integer(&ctx
->ac
, v
);
5718 v
= LLVMBuildShl(ctx
->builder
, v
,
5719 LLVMConstInt(ctx
->i32
, 16, false),
5721 v
= LLVMBuildOr(ctx
->builder
, v
,
5722 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5724 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5725 pos_args
[1].enabled_channels
|= 1 << 2;
5727 pos_args
[1].out
[3] = viewport_index_value
;
5728 pos_args
[1].enabled_channels
|= 1 << 3;
5732 for (i
= 0; i
< 4; i
++) {
5733 if (pos_args
[i
].out
[0])
5738 for (i
= 0; i
< 4; i
++) {
5739 if (!pos_args
[i
].out
[0])
5742 /* Specify the target we are exporting */
5743 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5744 if (pos_idx
== num_pos_exports
)
5745 pos_args
[i
].done
= 1;
5746 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5749 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5750 LLVMValueRef values
[4];
5751 if (!(ctx
->output_mask
& (1ull << i
)))
5754 for (unsigned j
= 0; j
< 4; j
++)
5755 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5756 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5758 if (i
== VARYING_SLOT_LAYER
) {
5759 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5760 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5762 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5763 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5764 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5766 } else if (i
>= VARYING_SLOT_VAR0
) {
5767 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5768 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5769 outinfo
->vs_output_param_offset
[i
] = param_count
;
5774 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5776 if (target
>= V_008DFC_SQ_EXP_POS
&&
5777 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5778 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5779 &args
, sizeof(args
));
5781 ac_build_export(&ctx
->ac
, &args
);
5785 if (export_prim_id
) {
5786 LLVMValueRef values
[4];
5787 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5788 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5791 values
[0] = ctx
->vs_prim_id
;
5792 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5793 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5794 for (unsigned j
= 1; j
< 4; j
++)
5795 values
[j
] = ctx
->f32zero
;
5796 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5797 ac_build_export(&ctx
->ac
, &args
);
5798 outinfo
->export_prim_id
= true;
5801 outinfo
->pos_exports
= num_pos_exports
;
5802 outinfo
->param_exports
= param_count
;
5806 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5807 struct ac_es_output_info
*outinfo
)
5810 uint64_t max_output_written
= 0;
5811 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5812 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5816 if (!(ctx
->output_mask
& (1ull << i
)))
5819 if (i
== VARYING_SLOT_CLIP_DIST0
)
5820 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5822 param_index
= shader_io_get_unique_index(i
);
5824 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5826 for (j
= 0; j
< length
; j
++) {
5827 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5828 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
5830 ac_build_buffer_store_dword(&ctx
->ac
,
5833 NULL
, ctx
->es2gs_offset
,
5834 (4 * param_index
+ j
) * 4,
5838 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5842 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5844 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5845 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
5846 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5847 vertex_dw_stride
, "");
5849 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5850 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5853 if (!(ctx
->output_mask
& (1ull << i
)))
5856 if (i
== VARYING_SLOT_CLIP_DIST0
)
5857 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5858 int param
= shader_io_get_unique_index(i
);
5859 mark_tess_output(ctx
, false, param
);
5861 mark_tess_output(ctx
, false, param
+ 1);
5862 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5863 LLVMConstInt(ctx
->i32
, param
* 4, false),
5865 for (unsigned j
= 0; j
< length
; j
++) {
5866 lds_store(ctx
, dw_addr
,
5867 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5868 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->i32one
, "");
5873 struct ac_build_if_state
5875 struct nir_to_llvm_context
*ctx
;
5876 LLVMValueRef condition
;
5877 LLVMBasicBlockRef entry_block
;
5878 LLVMBasicBlockRef true_block
;
5879 LLVMBasicBlockRef false_block
;
5880 LLVMBasicBlockRef merge_block
;
5883 static LLVMBasicBlockRef
5884 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5886 LLVMBasicBlockRef current_block
;
5887 LLVMBasicBlockRef next_block
;
5888 LLVMBasicBlockRef new_block
;
5890 /* get current basic block */
5891 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5893 /* chqeck if there's another block after this one */
5894 next_block
= LLVMGetNextBasicBlock(current_block
);
5896 /* insert the new block before the next block */
5897 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5900 /* append new block after current block */
5901 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5902 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5908 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5909 struct nir_to_llvm_context
*ctx
,
5910 LLVMValueRef condition
)
5912 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5914 memset(ifthen
, 0, sizeof *ifthen
);
5916 ifthen
->condition
= condition
;
5917 ifthen
->entry_block
= block
;
5919 /* create endif/merge basic block for the phi functions */
5920 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5922 /* create/insert true_block before merge_block */
5923 ifthen
->true_block
=
5924 LLVMInsertBasicBlockInContext(ctx
->context
,
5925 ifthen
->merge_block
,
5928 /* successive code goes into the true block */
5929 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5933 * End a conditional.
5936 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5938 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5940 /* Insert branch to the merge block from current block */
5941 LLVMBuildBr(builder
, ifthen
->merge_block
);
5944 * Now patch in the various branch instructions.
5947 /* Insert the conditional branch instruction at the end of entry_block */
5948 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5949 if (ifthen
->false_block
) {
5950 /* we have an else clause */
5951 LLVMBuildCondBr(builder
, ifthen
->condition
,
5952 ifthen
->true_block
, ifthen
->false_block
);
5955 /* no else clause */
5956 LLVMBuildCondBr(builder
, ifthen
->condition
,
5957 ifthen
->true_block
, ifthen
->merge_block
);
5960 /* Resume building code at end of the ifthen->merge_block */
5961 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5965 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5967 unsigned stride
, outer_comps
, inner_comps
;
5968 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5969 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 8, 5);
5970 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
5971 unsigned tess_inner_index
, tess_outer_index
;
5972 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
5973 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
5977 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
5997 ac_nir_build_if(&if_ctx
, ctx
,
5998 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5999 invocation_id
, ctx
->i32zero
, ""));
6001 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6002 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6004 mark_tess_output(ctx
, true, tess_inner_index
);
6005 mark_tess_output(ctx
, true, tess_outer_index
);
6006 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6007 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6008 LLVMConstInt(ctx
->i32
, tess_inner_index
* 4, false), "");
6009 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6010 LLVMConstInt(ctx
->i32
, tess_outer_index
* 4, false), "");
6012 for (i
= 0; i
< 4; i
++) {
6013 inner
[i
] = LLVMGetUndef(ctx
->i32
);
6014 outer
[i
] = LLVMGetUndef(ctx
->i32
);
6018 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6019 outer
[0] = out
[1] = lds_load(ctx
, lds_outer
);
6020 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6021 LLVMConstInt(ctx
->i32
, 1, false), "");
6022 outer
[1] = out
[0] = lds_load(ctx
, lds_outer
);
6024 for (i
= 0; i
< outer_comps
; i
++) {
6026 lds_load(ctx
, lds_outer
);
6027 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6028 LLVMConstInt(ctx
->i32
, 1, false), "");
6030 for (i
= 0; i
< inner_comps
; i
++) {
6031 inner
[i
] = out
[outer_comps
+i
] =
6032 lds_load(ctx
, lds_inner
);
6033 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6034 LLVMConstInt(ctx
->i32
, 1, false), "");
6038 /* Convert the outputs to vectors for stores. */
6039 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6043 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6046 buffer
= ctx
->hs_ring_tess_factor
;
6047 tf_base
= ctx
->tess_factor_offset
;
6048 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6049 LLVMConstInt(ctx
->i32
, 4 * stride
, false), "");
6050 unsigned tf_offset
= 0;
6052 if (ctx
->options
->chip_class
<= VI
) {
6053 ac_nir_build_if(&inner_if_ctx
, ctx
,
6054 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6055 rel_patch_id
, ctx
->i32zero
, ""));
6057 /* Store the dynamic HS control word. */
6058 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6059 LLVMConstInt(ctx
->i32
, 0x80000000, false),
6060 1, ctx
->i32zero
, tf_base
,
6061 0, 1, 0, true, false);
6064 ac_nir_build_endif(&inner_if_ctx
);
6067 /* Store the tessellation factors. */
6068 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6069 MIN2(stride
, 4), byteoffset
, tf_base
,
6070 tf_offset
, 1, 0, true, false);
6072 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6073 stride
- 4, byteoffset
, tf_base
,
6074 16 + tf_offset
, 1, 0, true, false);
6076 //TODO store to offchip for TES to read - only if TES reads them
6078 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6079 LLVMValueRef tf_inner_offset
;
6080 unsigned param_outer
, param_inner
;
6082 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6083 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6084 LLVMConstInt(ctx
->i32
, param_outer
, 0));
6086 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6087 util_next_power_of_two(outer_comps
));
6089 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6090 outer_comps
, tf_outer_offset
,
6091 ctx
->oc_lds
, 0, 1, 0, true, false);
6093 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6094 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6095 LLVMConstInt(ctx
->i32
, param_inner
, 0));
6097 inner_vec
= inner_comps
== 1 ? inner
[0] :
6098 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6099 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6100 inner_comps
, tf_inner_offset
,
6101 ctx
->oc_lds
, 0, 1, 0, true, false);
6104 ac_nir_build_endif(&if_ctx
);
6108 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6110 write_tess_factors(ctx
);
6114 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6115 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6116 struct ac_export_args
*args
)
6119 si_llvm_init_export_args(ctx
, color
, param
,
6123 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6124 args
->done
= 1; /* DONE bit */
6125 } else if (!args
->enabled_channels
)
6126 return false; /* unnecessary NULL export */
6132 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6133 LLVMValueRef depth
, LLVMValueRef stencil
,
6134 LLVMValueRef samplemask
)
6136 struct ac_export_args args
;
6138 args
.enabled_channels
= 0;
6139 args
.valid_mask
= 1;
6141 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
6144 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
6145 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
6146 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
6147 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
6150 args
.out
[0] = depth
;
6151 args
.enabled_channels
|= 0x1;
6155 args
.out
[1] = stencil
;
6156 args
.enabled_channels
|= 0x2;
6160 args
.out
[2] = samplemask
;
6161 args
.enabled_channels
|= 0x4;
6164 /* SI (except OLAND and HAINAN) has a bug that it only looks
6165 * at the X writemask component. */
6166 if (ctx
->options
->chip_class
== SI
&&
6167 ctx
->options
->family
!= CHIP_OLAND
&&
6168 ctx
->options
->family
!= CHIP_HAINAN
)
6169 args
.enabled_channels
|= 0x1;
6171 ac_build_export(&ctx
->ac
, &args
);
6175 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6178 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6179 struct ac_export_args color_args
[8];
6181 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6182 LLVMValueRef values
[4];
6184 if (!(ctx
->output_mask
& (1ull << i
)))
6187 if (i
== FRAG_RESULT_DEPTH
) {
6188 ctx
->shader_info
->fs
.writes_z
= true;
6189 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6190 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6191 } else if (i
== FRAG_RESULT_STENCIL
) {
6192 ctx
->shader_info
->fs
.writes_stencil
= true;
6193 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6194 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6195 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6196 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6197 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6198 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6201 for (unsigned j
= 0; j
< 4; j
++)
6202 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6203 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6205 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6206 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6208 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6214 for (unsigned i
= 0; i
< index
; i
++)
6215 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6216 if (depth
|| stencil
|| samplemask
)
6217 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6219 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6220 ac_build_export(&ctx
->ac
, &color_args
[0]);
6223 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
6227 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6229 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6233 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6234 LLVMValueRef
*addrs
)
6236 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6238 switch (ctx
->stage
) {
6239 case MESA_SHADER_VERTEX
:
6240 if (ctx
->options
->key
.vs
.as_ls
)
6241 handle_ls_outputs_post(ctx
);
6242 else if (ctx
->options
->key
.vs
.as_es
)
6243 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6245 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6246 &ctx
->shader_info
->vs
.outinfo
);
6248 case MESA_SHADER_FRAGMENT
:
6249 handle_fs_outputs_post(ctx
);
6251 case MESA_SHADER_GEOMETRY
:
6252 emit_gs_epilogue(ctx
);
6254 case MESA_SHADER_TESS_CTRL
:
6255 handle_tcs_outputs_post(ctx
);
6257 case MESA_SHADER_TESS_EVAL
:
6258 if (ctx
->options
->key
.tes
.as_es
)
6259 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6261 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6262 &ctx
->shader_info
->tes
.outinfo
);
6269 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6271 LLVMPassManagerRef passmgr
;
6272 /* Create the pass manager */
6273 passmgr
= LLVMCreateFunctionPassManagerForModule(
6276 /* This pass should eliminate all the load and store instructions */
6277 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6279 /* Add some optimization passes */
6280 LLVMAddScalarReplAggregatesPass(passmgr
);
6281 LLVMAddLICMPass(passmgr
);
6282 LLVMAddAggressiveDCEPass(passmgr
);
6283 LLVMAddCFGSimplificationPass(passmgr
);
6284 LLVMAddInstructionCombiningPass(passmgr
);
6287 LLVMInitializeFunctionPassManager(passmgr
);
6288 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6289 LLVMFinalizeFunctionPassManager(passmgr
);
6291 LLVMDisposeBuilder(ctx
->builder
);
6292 LLVMDisposePassManager(passmgr
);
6296 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6298 struct ac_vs_output_info
*outinfo
;
6300 switch (ctx
->stage
) {
6301 case MESA_SHADER_FRAGMENT
:
6302 case MESA_SHADER_COMPUTE
:
6303 case MESA_SHADER_TESS_CTRL
:
6304 case MESA_SHADER_GEOMETRY
:
6306 case MESA_SHADER_VERTEX
:
6307 if (ctx
->options
->key
.vs
.as_ls
||
6308 ctx
->options
->key
.vs
.as_es
)
6310 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6312 case MESA_SHADER_TESS_EVAL
:
6313 if (ctx
->options
->key
.vs
.as_es
)
6315 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6318 unreachable("Unhandled shader type");
6321 ac_optimize_vs_outputs(&ctx
->ac
,
6323 outinfo
->vs_output_param_offset
,
6325 &outinfo
->param_exports
);
6329 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6331 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6332 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6333 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
6336 if (ctx
->is_gs_copy_shader
) {
6337 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
6339 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6341 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
6342 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
6344 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
6346 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
6347 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
6348 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6349 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
6352 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6353 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6354 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
6355 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
6360 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6361 const struct nir_shader
*nir
)
6363 switch (nir
->stage
) {
6364 case MESA_SHADER_TESS_CTRL
:
6365 return chip_class
>= CIK
? 128 : 64;
6366 case MESA_SHADER_GEOMETRY
:
6368 case MESA_SHADER_COMPUTE
:
6374 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6375 nir
->info
.cs
.local_size
[1] *
6376 nir
->info
.cs
.local_size
[2];
6377 return max_workgroup_size
;
6380 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6381 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6383 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6384 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6385 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6386 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6387 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
6388 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6389 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6390 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_rel_ids
, ctx
->rel_auto_id
, "");
6391 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6394 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6395 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6397 struct ac_nir_context ctx
= {};
6398 struct nir_function
*func
;
6407 ctx
.stage
= nir
->stage
;
6409 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6411 nir_foreach_variable(variable
, &nir
->outputs
)
6412 handle_shader_output_decl(&ctx
, nir
, variable
);
6414 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6415 _mesa_key_pointer_equal
);
6416 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6417 _mesa_key_pointer_equal
);
6418 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6419 _mesa_key_pointer_equal
);
6421 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6423 setup_locals(&ctx
, func
);
6425 if (nir
->stage
== MESA_SHADER_COMPUTE
)
6426 setup_shared(&ctx
, nir
);
6428 visit_cf_list(&ctx
, &func
->impl
->body
);
6429 phi_post_pass(&ctx
);
6431 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6435 ralloc_free(ctx
.defs
);
6436 ralloc_free(ctx
.phis
);
6437 ralloc_free(ctx
.vars
);
6444 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6445 struct nir_shader
*const *shaders
,
6447 struct ac_shader_variant_info
*shader_info
,
6448 const struct ac_nir_compiler_options
*options
)
6450 struct nir_to_llvm_context ctx
= {0};
6452 ctx
.options
= options
;
6453 ctx
.shader_info
= shader_info
;
6454 ctx
.context
= LLVMContextCreate();
6455 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6457 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
);
6458 ctx
.ac
.module
= ctx
.module
;
6459 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6461 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6462 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6463 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6464 LLVMDisposeTargetData(data_layout
);
6465 LLVMDisposeMessage(data_layout_str
);
6468 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6469 ctx
.ac
.builder
= ctx
.builder
;
6471 memset(shader_info
, 0, sizeof(*shader_info
));
6473 for(int i
= 0; i
< shader_count
; ++i
)
6474 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6476 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6477 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6478 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6479 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6481 ctx
.max_workgroup_size
= ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
, shaders
[0]);
6483 create_function(&ctx
, shaders
[shader_count
- 1]->stage
, shader_count
>= 2,
6484 shader_count
>= 2 ? shaders
[shader_count
- 2]->stage
: MESA_SHADER_VERTEX
);
6486 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6487 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6488 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6489 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6491 if (ctx
.ac
.chip_class
== GFX9
&&
6492 shaders
[shader_count
- 1]->stage
== MESA_SHADER_TESS_CTRL
)
6493 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6495 for(int i
= 0; i
< shader_count
; ++i
) {
6496 ctx
.stage
= shaders
[i
]->stage
;
6497 ctx
.output_mask
= 0;
6498 ctx
.tess_outputs_written
= 0;
6499 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6500 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6502 if (shaders
[i
]->stage
== MESA_SHADER_GEOMETRY
) {
6503 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.i32
, "gs_next_vertex");
6505 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6506 } else if (shaders
[i
]->stage
== MESA_SHADER_TESS_EVAL
) {
6507 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6508 } else if (shaders
[i
]->stage
== MESA_SHADER_VERTEX
) {
6509 if (shader_info
->info
.vs
.needs_instance_id
) {
6510 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6511 MAX2(3, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6513 } else if (shaders
[i
]->stage
== MESA_SHADER_FRAGMENT
) {
6514 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6520 ac_setup_rings(&ctx
);
6522 LLVMBasicBlockRef merge_block
;
6523 if (shader_count
>= 2) {
6524 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6525 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6526 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6528 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6529 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6530 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6531 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6532 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6533 thread_id
, count
, "");
6534 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6536 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6539 if (shaders
[i
]->stage
== MESA_SHADER_FRAGMENT
)
6540 handle_fs_inputs(&ctx
, shaders
[i
]);
6541 else if(shaders
[i
]->stage
== MESA_SHADER_VERTEX
)
6542 handle_vs_inputs(&ctx
, shaders
[i
]);
6544 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6545 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->stage
);
6547 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6549 if (shader_count
>= 2) {
6550 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6551 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6554 if (shaders
[i
]->stage
== MESA_SHADER_GEOMETRY
) {
6555 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6556 shaders
[i
]->info
.cull_distance_array_size
> 4;
6557 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6558 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6559 shaders
[i
]->info
.gs
.vertices_out
;
6560 } else if (shaders
[i
]->stage
== MESA_SHADER_TESS_CTRL
) {
6561 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6562 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6563 } else if (shaders
[i
]->stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6564 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6568 LLVMBuildRetVoid(ctx
.builder
);
6570 ac_llvm_finalize_module(&ctx
);
6572 if (shader_count
== 1)
6573 ac_nir_eliminate_const_vs_outputs(&ctx
);
6578 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6580 unsigned *retval
= (unsigned *)context
;
6581 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6582 char *description
= LLVMGetDiagInfoDescription(di
);
6584 if (severity
== LLVMDSError
) {
6586 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6590 LLVMDisposeMessage(description
);
6593 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6594 struct ac_shader_binary
*binary
,
6595 LLVMTargetMachineRef tm
)
6597 unsigned retval
= 0;
6599 LLVMContextRef llvm_ctx
;
6600 LLVMMemoryBufferRef out_buffer
;
6601 unsigned buffer_size
;
6602 const char *buffer_data
;
6605 /* Setup Diagnostic Handler*/
6606 llvm_ctx
= LLVMGetModuleContext(M
);
6608 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6612 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6615 /* Process Errors/Warnings */
6617 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6623 /* Extract Shader Code*/
6624 buffer_size
= LLVMGetBufferSize(out_buffer
);
6625 buffer_data
= LLVMGetBufferStart(out_buffer
);
6627 ac_elf_read(buffer_data
, buffer_size
, binary
);
6630 LLVMDisposeMemoryBuffer(out_buffer
);
6636 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6637 LLVMModuleRef llvm_module
,
6638 struct ac_shader_binary
*binary
,
6639 struct ac_shader_config
*config
,
6640 struct ac_shader_variant_info
*shader_info
,
6641 gl_shader_stage stage
,
6642 bool dump_shader
, bool supports_spill
)
6645 ac_dump_module(llvm_module
);
6647 memset(binary
, 0, sizeof(*binary
));
6648 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6650 fprintf(stderr
, "compile failed\n");
6654 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6656 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6658 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6659 LLVMDisposeModule(llvm_module
);
6660 LLVMContextDispose(ctx
);
6662 if (stage
== MESA_SHADER_FRAGMENT
) {
6663 shader_info
->num_input_vgprs
= 0;
6664 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6665 shader_info
->num_input_vgprs
+= 2;
6666 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6667 shader_info
->num_input_vgprs
+= 2;
6668 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6669 shader_info
->num_input_vgprs
+= 2;
6670 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6671 shader_info
->num_input_vgprs
+= 3;
6672 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6673 shader_info
->num_input_vgprs
+= 2;
6674 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6675 shader_info
->num_input_vgprs
+= 2;
6676 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6677 shader_info
->num_input_vgprs
+= 2;
6678 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6679 shader_info
->num_input_vgprs
+= 1;
6680 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6681 shader_info
->num_input_vgprs
+= 1;
6682 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6683 shader_info
->num_input_vgprs
+= 1;
6684 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6685 shader_info
->num_input_vgprs
+= 1;
6686 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6687 shader_info
->num_input_vgprs
+= 1;
6688 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6689 shader_info
->num_input_vgprs
+= 1;
6690 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6691 shader_info
->num_input_vgprs
+= 1;
6692 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6693 shader_info
->num_input_vgprs
+= 1;
6694 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6695 shader_info
->num_input_vgprs
+= 1;
6697 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6699 /* +3 for scratch wave offset and VCC */
6700 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6701 shader_info
->num_input_sgprs
+ 3);
6705 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
6707 switch (nir
->stage
) {
6708 case MESA_SHADER_COMPUTE
:
6709 for (int i
= 0; i
< 3; ++i
)
6710 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6712 case MESA_SHADER_FRAGMENT
:
6713 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6715 case MESA_SHADER_GEOMETRY
:
6716 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6717 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6718 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6719 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6721 case MESA_SHADER_TESS_EVAL
:
6722 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6723 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6724 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6725 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6726 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6728 case MESA_SHADER_TESS_CTRL
:
6729 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6731 case MESA_SHADER_VERTEX
:
6732 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6733 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6734 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6735 if (options
->key
.vs
.as_ls
)
6736 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6743 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6744 struct ac_shader_binary
*binary
,
6745 struct ac_shader_config
*config
,
6746 struct ac_shader_variant_info
*shader_info
,
6747 struct nir_shader
*const *nir
,
6749 const struct ac_nir_compiler_options
*options
,
6753 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
6756 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->stage
, dump_shader
, options
->supports_spill
);
6757 for (int i
= 0; i
< nir_count
; ++i
)
6758 ac_fill_shader_info(shader_info
, nir
[i
], options
);
6762 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6764 LLVMValueRef args
[9];
6765 args
[0] = ctx
->gsvs_ring
;
6766 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
6767 args
[3] = ctx
->i32zero
;
6768 args
[4] = ctx
->i32one
; /* OFFEN */
6769 args
[5] = ctx
->i32zero
; /* IDXEN */
6770 args
[6] = ctx
->i32one
; /* GLC */
6771 args
[7] = ctx
->i32one
; /* SLC */
6772 args
[8] = ctx
->i32zero
; /* TFE */
6776 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6780 if (!(ctx
->output_mask
& (1ull << i
)))
6783 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6784 /* unpack clip and cull from a single set of slots */
6785 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6790 for (unsigned j
= 0; j
< length
; j
++) {
6792 args
[2] = LLVMConstInt(ctx
->i32
,
6794 ctx
->gs_max_out_vertices
* 16 * 4, false);
6796 value
= ac_build_intrinsic(&ctx
->ac
,
6797 "llvm.SI.buffer.load.dword.i32.i32",
6799 AC_FUNC_ATTR_READONLY
|
6800 AC_FUNC_ATTR_LEGACY
);
6802 LLVMBuildStore(ctx
->builder
,
6803 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6807 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
6810 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6811 struct nir_shader
*geom_shader
,
6812 struct ac_shader_binary
*binary
,
6813 struct ac_shader_config
*config
,
6814 struct ac_shader_variant_info
*shader_info
,
6815 const struct ac_nir_compiler_options
*options
,
6818 struct nir_to_llvm_context ctx
= {0};
6819 ctx
.context
= LLVMContextCreate();
6820 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6821 ctx
.options
= options
;
6822 ctx
.shader_info
= shader_info
;
6824 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
);
6825 ctx
.ac
.module
= ctx
.module
;
6827 ctx
.is_gs_copy_shader
= true;
6828 LLVMSetTarget(ctx
.module
, "amdgcn--");
6831 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6832 ctx
.ac
.builder
= ctx
.builder
;
6833 ctx
.stage
= MESA_SHADER_VERTEX
;
6835 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
6837 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6838 ac_setup_rings(&ctx
);
6840 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6841 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6843 struct ac_nir_context nir_ctx
= {};
6844 nir_ctx
.ac
= ctx
.ac
;
6845 nir_ctx
.abi
= &ctx
.abi
;
6847 nir_ctx
.nctx
= &ctx
;
6850 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
6851 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
6852 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
6855 ac_gs_copy_shader_emit(&ctx
);
6859 LLVMBuildRetVoid(ctx
.builder
);
6861 ac_llvm_finalize_module(&ctx
);
6863 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6865 dump_shader
, options
->supports_spill
);