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,
44 #define CONST_ADDR_SPACE 2
45 #define LOCAL_ADDR_SPACE 3
47 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
48 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
50 struct nir_to_llvm_context
;
52 struct ac_nir_context
{
53 struct ac_llvm_context ac
;
54 struct ac_shader_abi
*abi
;
56 gl_shader_stage stage
;
58 struct hash_table
*defs
;
59 struct hash_table
*phis
;
60 struct hash_table
*vars
;
62 LLVMValueRef main_function
;
63 LLVMBasicBlockRef continue_block
;
64 LLVMBasicBlockRef break_block
;
66 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
71 struct nir_to_llvm_context
*nctx
; /* TODO get rid of this */
74 struct nir_to_llvm_context
{
75 struct ac_llvm_context ac
;
76 const struct ac_nir_compiler_options
*options
;
77 struct ac_shader_variant_info
*shader_info
;
78 struct ac_shader_abi abi
;
79 struct ac_nir_context
*nir
;
81 unsigned max_workgroup_size
;
82 LLVMContextRef context
;
84 LLVMBuilderRef builder
;
85 LLVMValueRef main_function
;
87 struct hash_table
*defs
;
88 struct hash_table
*phis
;
90 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
91 LLVMValueRef ring_offsets
;
92 LLVMValueRef push_constants
;
93 LLVMValueRef view_index
;
94 LLVMValueRef num_work_groups
;
95 LLVMValueRef workgroup_ids
;
96 LLVMValueRef local_invocation_ids
;
99 LLVMValueRef vertex_buffers
;
100 LLVMValueRef rel_auto_id
;
101 LLVMValueRef vs_prim_id
;
102 LLVMValueRef ls_out_layout
;
103 LLVMValueRef es2gs_offset
;
105 LLVMValueRef tcs_offchip_layout
;
106 LLVMValueRef tcs_out_offsets
;
107 LLVMValueRef tcs_out_layout
;
108 LLVMValueRef tcs_in_layout
;
110 LLVMValueRef tess_factor_offset
;
111 LLVMValueRef tcs_patch_id
;
112 LLVMValueRef tcs_rel_ids
;
113 LLVMValueRef tes_rel_patch_id
;
114 LLVMValueRef tes_patch_id
;
118 LLVMValueRef gsvs_ring_stride
;
119 LLVMValueRef gsvs_num_entries
;
120 LLVMValueRef gs2vs_offset
;
121 LLVMValueRef gs_wave_id
;
122 LLVMValueRef gs_vtx_offset
[6];
123 LLVMValueRef gs_prim_id
, gs_invocation_id
;
125 LLVMValueRef esgs_ring
;
126 LLVMValueRef gsvs_ring
;
127 LLVMValueRef hs_ring_tess_offchip
;
128 LLVMValueRef hs_ring_tess_factor
;
130 LLVMValueRef prim_mask
;
131 LLVMValueRef sample_pos_offset
;
132 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
133 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
152 LLVMValueRef i1false
;
153 LLVMValueRef i32zero
;
155 LLVMValueRef f32zero
;
157 LLVMValueRef v4f32empty
;
159 unsigned uniform_md_kind
;
160 LLVMValueRef empty_md
;
161 gl_shader_stage stage
;
164 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
167 uint64_t output_mask
;
168 uint8_t num_output_clips
;
169 uint8_t num_output_culls
;
171 bool is_gs_copy_shader
;
172 LLVMValueRef gs_next_vertex
;
173 unsigned gs_max_out_vertices
;
175 unsigned tes_primitive_mode
;
176 uint64_t tess_outputs_written
;
177 uint64_t tess_patch_outputs_written
;
180 static inline struct nir_to_llvm_context
*
181 nir_to_llvm_context_from_abi(struct ac_shader_abi
*abi
)
183 struct nir_to_llvm_context
*ctx
= NULL
;
184 return container_of(abi
, ctx
, abi
);
187 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
188 const nir_deref_var
*deref
,
189 enum ac_descriptor_type desc_type
,
190 bool image
, bool write
);
192 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
194 return (index
* 4) + chan
;
197 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
199 /* handle patch indices separate */
200 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
202 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
204 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
205 return 2 + (slot
- VARYING_SLOT_PATCH0
);
207 if (slot
== VARYING_SLOT_POS
)
209 if (slot
== VARYING_SLOT_PSIZ
)
211 if (slot
== VARYING_SLOT_CLIP_DIST0
)
213 /* 3 is reserved for clip dist as well */
214 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
215 return 4 + (slot
- VARYING_SLOT_VAR0
);
216 unreachable("illegal slot in get unique index\n");
219 static void set_llvm_calling_convention(LLVMValueRef func
,
220 gl_shader_stage stage
)
222 enum radeon_llvm_calling_convention calling_conv
;
225 case MESA_SHADER_VERTEX
:
226 case MESA_SHADER_TESS_CTRL
:
227 case MESA_SHADER_TESS_EVAL
:
228 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
230 case MESA_SHADER_GEOMETRY
:
231 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
233 case MESA_SHADER_FRAGMENT
:
234 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
236 case MESA_SHADER_COMPUTE
:
237 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
240 unreachable("Unhandle shader type");
243 LLVMSetFunctionCallConv(func
, calling_conv
);
248 LLVMTypeRef types
[MAX_ARGS
];
249 LLVMValueRef
*assign
[MAX_ARGS
];
250 unsigned array_params_mask
;
252 uint8_t user_sgpr_count
;
254 uint8_t num_user_sgprs_used
;
255 uint8_t num_sgprs_used
;
256 uint8_t num_vgprs_used
;
260 add_argument(struct arg_info
*info
,
261 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
263 assert(info
->count
< MAX_ARGS
);
264 info
->assign
[info
->count
] = param_ptr
;
265 info
->types
[info
->count
] = type
;
270 add_sgpr_argument(struct arg_info
*info
,
271 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
273 add_argument(info
, type
, param_ptr
);
274 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
279 add_user_sgpr_argument(struct arg_info
*info
,
281 LLVMValueRef
*param_ptr
)
283 add_sgpr_argument(info
, type
, param_ptr
);
284 info
->num_user_sgprs_used
+= ac_get_type_size(type
) / 4;
285 info
->user_sgpr_count
++;
289 add_vgpr_argument(struct arg_info
*info
,
291 LLVMValueRef
*param_ptr
)
293 add_argument(info
, type
, param_ptr
);
294 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
298 add_user_sgpr_array_argument(struct arg_info
*info
,
300 LLVMValueRef
*param_ptr
)
302 info
->array_params_mask
|= (1 << info
->count
);
303 add_user_sgpr_argument(info
, type
, param_ptr
);
306 static void assign_arguments(LLVMValueRef main_function
,
307 struct arg_info
*info
)
310 for (i
= 0; i
< info
->count
; i
++) {
312 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
317 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
318 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
319 unsigned num_return_elems
,
320 struct arg_info
*args
,
321 unsigned max_workgroup_size
,
324 LLVMTypeRef main_function_type
, ret_type
;
325 LLVMBasicBlockRef main_function_body
;
327 if (num_return_elems
)
328 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
329 num_return_elems
, true);
331 ret_type
= LLVMVoidTypeInContext(ctx
);
333 /* Setup the function */
335 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
336 LLVMValueRef main_function
=
337 LLVMAddFunction(module
, "main", main_function_type
);
339 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
340 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
342 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
343 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
344 if (args
->array_params_mask
& (1 << i
)) {
345 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
346 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
347 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
350 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
354 if (max_workgroup_size
) {
355 ac_llvm_add_target_dep_function_attr(main_function
,
356 "amdgpu-max-work-group-size",
360 /* These were copied from some LLVM test. */
361 LLVMAddTargetDependentFunctionAttr(main_function
,
362 "less-precise-fpmad",
364 LLVMAddTargetDependentFunctionAttr(main_function
,
367 LLVMAddTargetDependentFunctionAttr(main_function
,
370 LLVMAddTargetDependentFunctionAttr(main_function
,
374 return main_function
;
377 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
379 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
383 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
385 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
386 type
= LLVMGetElementType(type
);
388 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
389 return LLVMGetIntTypeWidth(type
);
391 if (type
== ctx
->f16
)
393 if (type
== ctx
->f32
)
395 if (type
== ctx
->f64
)
398 unreachable("Unhandled type kind in get_elem_bits");
401 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
402 LLVMValueRef param
, unsigned rshift
,
405 LLVMValueRef value
= param
;
407 value
= LLVMBuildLShr(ctx
->builder
, value
,
408 LLVMConstInt(ctx
->i32
, rshift
, false), "");
410 if (rshift
+ bitwidth
< 32) {
411 unsigned mask
= (1 << bitwidth
) - 1;
412 value
= LLVMBuildAnd(ctx
->builder
, value
,
413 LLVMConstInt(ctx
->i32
, mask
, false), "");
418 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
420 switch (ctx
->stage
) {
421 case MESA_SHADER_TESS_CTRL
:
422 return unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
423 case MESA_SHADER_TESS_EVAL
:
424 return ctx
->tes_rel_patch_id
;
427 unreachable("Illegal stage");
431 /* Tessellation shaders pass outputs to the next shader using LDS.
433 * LS outputs = TCS inputs
434 * TCS outputs = TES inputs
437 * - TCS inputs for patch 0
438 * - TCS inputs for patch 1
439 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
441 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
442 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
443 * - TCS outputs for patch 1
444 * - Per-patch TCS outputs for patch 1
445 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
446 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
449 * All three shaders VS(LS), TCS, TES share the same LDS space.
452 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
454 if (ctx
->stage
== MESA_SHADER_VERTEX
)
455 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
456 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
457 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
465 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
467 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
471 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
473 return LLVMBuildMul(ctx
->builder
,
474 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
475 LLVMConstInt(ctx
->i32
, 4, false), "");
479 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
481 return LLVMBuildMul(ctx
->builder
,
482 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
483 LLVMConstInt(ctx
->i32
, 4, false), "");
487 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
489 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
490 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
492 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
496 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
498 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
499 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
500 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
502 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
503 LLVMBuildMul(ctx
->builder
, patch_stride
,
509 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
511 LLVMValueRef patch0_patch_data_offset
=
512 get_tcs_out_patch0_patch_data_offset(ctx
);
513 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
514 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
516 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
517 LLVMBuildMul(ctx
->builder
, patch_stride
,
522 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
524 ud_info
->sgpr_idx
= *sgpr_idx
;
525 ud_info
->num_sgprs
= num_sgprs
;
526 ud_info
->indirect
= false;
527 ud_info
->indirect_offset
= 0;
528 *sgpr_idx
+= num_sgprs
;
531 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
532 int idx
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
534 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
538 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
539 uint32_t indirect_offset
)
541 ud_info
->sgpr_idx
= sgpr_idx
;
542 ud_info
->num_sgprs
= num_sgprs
;
543 ud_info
->indirect
= true;
544 ud_info
->indirect_offset
= indirect_offset
;
547 static void declare_tess_lds(struct nir_to_llvm_context
*ctx
)
549 unsigned lds_size
= ctx
->options
->chip_class
>= CIK
? 65536 : 32768;
550 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32zero
,
551 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
555 struct user_sgpr_info
{
556 bool need_ring_offsets
;
558 bool indirect_all_descriptor_sets
;
561 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
562 struct user_sgpr_info
*user_sgpr_info
)
564 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
566 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
567 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
568 ctx
->stage
== MESA_SHADER_VERTEX
||
569 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
570 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
571 ctx
->is_gs_copy_shader
)
572 user_sgpr_info
->need_ring_offsets
= true;
574 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
575 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
576 user_sgpr_info
->need_ring_offsets
= true;
578 /* 2 user sgprs will nearly always be allocated for scratch/rings */
579 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
580 user_sgpr_info
->sgpr_count
+= 2;
583 switch (ctx
->stage
) {
584 case MESA_SHADER_COMPUTE
:
585 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
587 case MESA_SHADER_FRAGMENT
:
588 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
590 case MESA_SHADER_VERTEX
:
591 if (!ctx
->is_gs_copy_shader
) {
592 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
593 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
594 user_sgpr_info
->sgpr_count
+= 3;
596 user_sgpr_info
->sgpr_count
+= 2;
599 if (ctx
->options
->key
.vs
.as_ls
)
600 user_sgpr_info
->sgpr_count
++;
602 case MESA_SHADER_TESS_CTRL
:
603 user_sgpr_info
->sgpr_count
+= 4;
605 case MESA_SHADER_TESS_EVAL
:
606 user_sgpr_info
->sgpr_count
+= 1;
608 case MESA_SHADER_GEOMETRY
:
609 user_sgpr_info
->sgpr_count
+= 2;
615 if (ctx
->shader_info
->info
.needs_push_constants
)
616 user_sgpr_info
->sgpr_count
+= 2;
618 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
619 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
620 user_sgpr_info
->sgpr_count
+= 2;
621 user_sgpr_info
->indirect_all_descriptor_sets
= true;
623 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
627 static void create_function(struct nir_to_llvm_context
*ctx
)
629 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
630 uint8_t user_sgpr_idx
;
631 struct user_sgpr_info user_sgpr_info
;
632 struct arg_info args
= {};
633 LLVMValueRef desc_sets
;
635 allocate_user_sgprs(ctx
, &user_sgpr_info
);
636 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
637 add_user_sgpr_argument(&args
, const_array(ctx
->v4i32
, 16), &ctx
->ring_offsets
); /* address of rings */
640 /* 1 for each descriptor set */
641 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
642 for (unsigned i
= 0; i
< num_sets
; ++i
) {
643 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
644 add_user_sgpr_array_argument(&args
, const_array(ctx
->i8
, 1024 * 1024), &ctx
->descriptor_sets
[i
]);
648 add_user_sgpr_array_argument(&args
, const_array(const_array(ctx
->i8
, 1024 * 1024), 32), &desc_sets
);
650 if (ctx
->shader_info
->info
.needs_push_constants
) {
651 /* 1 for push constants and dynamic descriptors */
652 add_user_sgpr_array_argument(&args
, const_array(ctx
->i8
, 1024 * 1024), &ctx
->push_constants
);
655 switch (ctx
->stage
) {
656 case MESA_SHADER_COMPUTE
:
657 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
658 add_user_sgpr_argument(&args
, LLVMVectorType(ctx
->i32
, ctx
->shader_info
->info
.cs
.grid_components_used
), &ctx
->num_work_groups
); /* grid size */
659 add_sgpr_argument(&args
, LLVMVectorType(ctx
->i32
, 3), &ctx
->workgroup_ids
);
660 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->tg_size
);
661 add_vgpr_argument(&args
, LLVMVectorType(ctx
->i32
, 3), &ctx
->local_invocation_ids
);
663 case MESA_SHADER_VERTEX
:
664 if (!ctx
->is_gs_copy_shader
) {
665 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
666 add_user_sgpr_argument(&args
, const_array(ctx
->v4i32
, 16), &ctx
->vertex_buffers
); /* vertex buffers */
667 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.base_vertex
); // base vertex
668 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.start_instance
);// start instance
669 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
670 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.draw_id
); // draw id
672 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
))
673 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
674 if (ctx
->options
->key
.vs
.as_es
)
675 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->es2gs_offset
); // es2gs offset
676 else if (ctx
->options
->key
.vs
.as_ls
)
677 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->ls_out_layout
); // ls out layout
678 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.vertex_id
); // vertex id
679 if (!ctx
->is_gs_copy_shader
) {
680 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->rel_auto_id
); // rel auto id
681 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->vs_prim_id
); // vs prim id
682 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.instance_id
); // instance id
685 case MESA_SHADER_TESS_CTRL
:
686 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
687 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
688 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_out_layout
); // tcs out layout
689 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_in_layout
); // tcs in layout
690 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
691 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
692 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // param oc lds
693 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->tess_factor_offset
); // tess factor offset
694 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_patch_id
); // patch id
695 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_rel_ids
); // rel ids;
697 case MESA_SHADER_TESS_EVAL
:
698 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
699 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
700 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
701 if (ctx
->options
->key
.tes
.as_es
) {
702 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // OC LDS
703 add_sgpr_argument(&args
, ctx
->i32
, NULL
); //
704 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->es2gs_offset
); // es2gs offset
706 add_sgpr_argument(&args
, ctx
->i32
, NULL
); //
707 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // OC LDS
709 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_u
); // tes_u
710 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_v
); // tes_v
711 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
712 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tes_patch_id
); // tes patch id
714 case MESA_SHADER_GEOMETRY
:
715 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
716 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
717 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
718 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->view_index
);
719 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->gs2vs_offset
); // gs2vs offset
720 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->gs_wave_id
); // wave id
721 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[0]); // vtx0
722 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[1]); // vtx1
723 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_prim_id
); // prim id
724 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[2]);
725 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[3]);
726 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[4]);
727 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[5]);
728 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_invocation_id
);
730 case MESA_SHADER_FRAGMENT
:
731 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
732 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->sample_pos_offset
); /* sample position offset */
733 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->prim_mask
); /* prim mask */
734 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_sample
); /* persp sample */
735 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_center
); /* persp center */
736 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_centroid
); /* persp centroid */
737 add_vgpr_argument(&args
, ctx
->v3i32
, NULL
); /* persp pull model */
738 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_sample
); /* linear sample */
739 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_center
); /* linear center */
740 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_centroid
); /* linear centroid */
741 add_vgpr_argument(&args
, ctx
->f32
, NULL
); /* line stipple tex */
742 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[0]); /* pos x float */
743 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[1]); /* pos y float */
744 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[2]); /* pos z float */
745 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->abi
.frag_pos
[3]); /* pos w float */
746 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.front_face
); /* front face */
747 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.ancillary
); /* ancillary */
748 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->abi
.sample_coverage
); /* sample coverage */
749 add_vgpr_argument(&args
, ctx
->i32
, NULL
); /* fixed pt */
752 unreachable("Shader stage not implemented");
755 ctx
->main_function
= create_llvm_function(
756 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
757 ctx
->max_workgroup_size
,
758 ctx
->options
->unsafe_math
);
759 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
762 ctx
->shader_info
->num_input_vgprs
= 0;
763 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
=
764 ctx
->options
->supports_spill
? 2 : 0;
766 ctx
->shader_info
->num_user_sgprs
+= args
.num_user_sgprs_used
;
767 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
769 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
770 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
772 assign_arguments(ctx
->main_function
, &args
);
776 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
777 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, &user_sgpr_idx
, 2);
778 if (ctx
->options
->supports_spill
) {
779 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
780 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
781 NULL
, 0, AC_FUNC_ATTR_READNONE
);
782 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
783 const_array(ctx
->v4i32
, 16), "");
787 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
788 for (unsigned i
= 0; i
< num_sets
; ++i
) {
789 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
790 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], &user_sgpr_idx
, 2);
792 ctx
->descriptor_sets
[i
] = NULL
;
795 uint32_t desc_sgpr_idx
= user_sgpr_idx
;
796 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, &user_sgpr_idx
, 2);
798 for (unsigned i
= 0; i
< num_sets
; ++i
) {
799 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
800 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
801 ctx
->descriptor_sets
[i
] = ac_build_indexed_load_const(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->i32
, i
, false));
804 ctx
->descriptor_sets
[i
] = NULL
;
806 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
809 if (ctx
->shader_info
->info
.needs_push_constants
) {
810 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, &user_sgpr_idx
, 2);
813 switch (ctx
->stage
) {
814 case MESA_SHADER_COMPUTE
:
815 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
816 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, &user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
819 case MESA_SHADER_VERTEX
:
820 if (!ctx
->is_gs_copy_shader
) {
821 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
822 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, &user_sgpr_idx
, 2);
825 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
828 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, &user_sgpr_idx
, vs_num
);
831 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
832 if (ctx
->options
->key
.vs
.as_ls
) {
833 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
835 if (ctx
->options
->key
.vs
.as_ls
)
836 declare_tess_lds(ctx
);
838 case MESA_SHADER_TESS_CTRL
:
839 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
841 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
842 declare_tess_lds(ctx
);
844 case MESA_SHADER_TESS_EVAL
:
845 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
847 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
849 case MESA_SHADER_GEOMETRY
:
850 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, &user_sgpr_idx
, 2);
852 set_userdata_location_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
854 case MESA_SHADER_FRAGMENT
:
855 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
856 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, &user_sgpr_idx
, 1);
860 unreachable("Shader stage not implemented");
864 static void setup_types(struct nir_to_llvm_context
*ctx
)
866 LLVMValueRef args
[4];
868 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
869 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
870 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
871 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
872 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
873 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
874 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
875 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
876 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
877 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
878 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
879 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
880 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
881 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
882 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
884 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
885 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
886 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
887 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
888 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
889 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
891 args
[0] = ctx
->f32zero
;
892 args
[1] = ctx
->f32zero
;
893 args
[2] = ctx
->f32zero
;
894 args
[3] = ctx
->f32one
;
895 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
897 ctx
->uniform_md_kind
=
898 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
899 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
901 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
904 static int get_llvm_num_components(LLVMValueRef value
)
906 LLVMTypeRef type
= LLVMTypeOf(value
);
907 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
908 ? LLVMGetVectorSize(type
)
910 return num_components
;
913 static LLVMValueRef
llvm_extract_elem(struct ac_llvm_context
*ac
,
917 int count
= get_llvm_num_components(value
);
919 assert(index
< count
);
923 return LLVMBuildExtractElement(ac
->builder
, value
,
924 LLVMConstInt(ac
->i32
, index
, false), "");
927 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
928 LLVMValueRef value
, unsigned count
)
930 unsigned num_components
= get_llvm_num_components(value
);
931 if (count
== num_components
)
934 LLVMValueRef masks
[] = {
935 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
936 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
939 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
942 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
943 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
947 build_store_values_extended(struct ac_llvm_context
*ac
,
948 LLVMValueRef
*values
,
949 unsigned value_count
,
950 unsigned value_stride
,
953 LLVMBuilderRef builder
= ac
->builder
;
956 for (i
= 0; i
< value_count
; i
++) {
957 LLVMValueRef ptr
= values
[i
* value_stride
];
958 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
959 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
960 LLVMBuildStore(builder
, value
, ptr
);
964 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
965 const nir_ssa_def
*def
)
967 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
968 if (def
->num_components
> 1) {
969 type
= LLVMVectorType(type
, def
->num_components
);
974 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
977 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
978 return (LLVMValueRef
)entry
->data
;
982 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
983 const struct nir_block
*b
)
985 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
986 return (LLVMBasicBlockRef
)entry
->data
;
989 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
991 unsigned num_components
)
993 LLVMValueRef value
= get_src(ctx
, src
.src
);
994 bool need_swizzle
= false;
997 LLVMTypeRef type
= LLVMTypeOf(value
);
998 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
999 ? LLVMGetVectorSize(type
)
1002 for (unsigned i
= 0; i
< num_components
; ++i
) {
1003 assert(src
.swizzle
[i
] < src_components
);
1004 if (src
.swizzle
[i
] != i
)
1005 need_swizzle
= true;
1008 if (need_swizzle
|| num_components
!= src_components
) {
1009 LLVMValueRef masks
[] = {
1010 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1011 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1012 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1013 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1015 if (src_components
> 1 && num_components
== 1) {
1016 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1018 } else if (src_components
== 1 && num_components
> 1) {
1019 LLVMValueRef values
[] = {value
, value
, value
, value
};
1020 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1022 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1023 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1027 assert(!src
.negate
);
1032 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1033 LLVMIntPredicate pred
, LLVMValueRef src0
,
1036 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1037 return LLVMBuildSelect(ctx
->builder
, result
,
1038 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1039 LLVMConstInt(ctx
->i32
, 0, false), "");
1042 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1043 LLVMRealPredicate pred
, LLVMValueRef src0
,
1046 LLVMValueRef result
;
1047 src0
= ac_to_float(ctx
, src0
);
1048 src1
= ac_to_float(ctx
, src1
);
1049 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1050 return LLVMBuildSelect(ctx
->builder
, result
,
1051 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1052 LLVMConstInt(ctx
->i32
, 0, false), "");
1055 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1057 LLVMTypeRef result_type
,
1061 LLVMValueRef params
[] = {
1062 ac_to_float(ctx
, src0
),
1065 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1066 get_elem_bits(ctx
, result_type
));
1067 assert(length
< sizeof(name
));
1068 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1071 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1073 LLVMTypeRef result_type
,
1074 LLVMValueRef src0
, LLVMValueRef src1
)
1077 LLVMValueRef params
[] = {
1078 ac_to_float(ctx
, src0
),
1079 ac_to_float(ctx
, src1
),
1082 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1083 get_elem_bits(ctx
, result_type
));
1084 assert(length
< sizeof(name
));
1085 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1088 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1090 LLVMTypeRef result_type
,
1091 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1094 LLVMValueRef params
[] = {
1095 ac_to_float(ctx
, src0
),
1096 ac_to_float(ctx
, src1
),
1097 ac_to_float(ctx
, src2
),
1100 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1101 get_elem_bits(ctx
, result_type
));
1102 assert(length
< sizeof(name
));
1103 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1106 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1107 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1109 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1111 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1114 static LLVMValueRef
emit_find_lsb(struct ac_llvm_context
*ctx
,
1117 LLVMValueRef params
[2] = {
1120 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1121 * add special code to check for x=0. The reason is that
1122 * the LLVM behavior for x=0 is different from what we
1125 * The hardware already implements the correct behavior.
1127 LLVMConstInt(ctx
->i1
, 1, false),
1130 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
1132 AC_FUNC_ATTR_READNONE
);
1134 /* TODO: We need an intrinsic to skip this conditional. */
1135 /* Check for zero: */
1136 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
1139 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
1142 static LLVMValueRef
emit_ifind_msb(struct ac_llvm_context
*ctx
,
1145 return ac_build_imsb(ctx
, src0
, ctx
->i32
);
1148 static LLVMValueRef
emit_ufind_msb(struct ac_llvm_context
*ctx
,
1151 return ac_build_umsb(ctx
, src0
, ctx
->i32
);
1154 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1155 LLVMIntPredicate pred
,
1156 LLVMValueRef src0
, LLVMValueRef src1
)
1158 return LLVMBuildSelect(ctx
->builder
,
1159 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1164 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1167 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1168 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1171 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1174 LLVMValueRef cmp
, val
;
1176 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32_0
, "");
1177 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32_1
, src0
, "");
1178 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32_0
, "");
1179 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1183 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1186 LLVMValueRef cmp
, val
;
1188 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32_0
, "");
1189 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32_1
, src0
, "");
1190 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32_0
, "");
1191 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1195 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1198 const char *intr
= "llvm.floor.f32";
1199 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1200 LLVMValueRef params
[] = {
1203 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1204 ctx
->f32
, params
, 1,
1205 AC_FUNC_ATTR_READNONE
);
1206 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1209 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1211 LLVMValueRef src0
, LLVMValueRef src1
)
1213 LLVMTypeRef ret_type
;
1214 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1216 LLVMValueRef params
[] = { src0
, src1
};
1217 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1220 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1221 params
, 2, AC_FUNC_ATTR_READNONE
);
1223 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1224 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1228 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1231 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1234 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1237 src0
= ac_to_float(ctx
, src0
);
1238 return LLVMBuildSExt(ctx
->builder
,
1239 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1243 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1246 return LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1249 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1252 return LLVMBuildSExt(ctx
->builder
,
1253 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1257 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1260 LLVMValueRef result
;
1261 LLVMValueRef cond
= NULL
;
1263 src0
= ac_to_float(&ctx
->ac
, src0
);
1264 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
1266 if (ctx
->options
->chip_class
>= VI
) {
1267 LLVMValueRef args
[2];
1268 /* Check if the result is a denormal - and flush to 0 if so. */
1270 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1271 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1274 /* need to convert back up to f32 */
1275 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1277 if (ctx
->options
->chip_class
>= VI
)
1278 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32zero
, result
, "");
1281 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1282 * so compare the result and flush to 0 if it's smaller.
1284 LLVMValueRef temp
, cond2
;
1285 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1287 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1288 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
1290 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1291 temp
, ctx
->f32zero
, "");
1292 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1293 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32zero
, result
, "");
1298 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1299 LLVMValueRef src0
, LLVMValueRef src1
)
1301 LLVMValueRef dst64
, result
;
1302 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1303 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1305 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1306 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1307 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1311 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1312 LLVMValueRef src0
, LLVMValueRef src1
)
1314 LLVMValueRef dst64
, result
;
1315 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1316 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1318 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1319 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1320 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1324 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1326 const LLVMValueRef srcs
[3])
1328 LLVMValueRef result
;
1329 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1331 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1332 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1336 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1337 LLVMValueRef src0
, LLVMValueRef src1
,
1338 LLVMValueRef src2
, LLVMValueRef src3
)
1340 LLVMValueRef bfi_args
[3], result
;
1342 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1343 LLVMBuildSub(ctx
->builder
,
1344 LLVMBuildShl(ctx
->builder
,
1349 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1352 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1355 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1356 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1358 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1359 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1360 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1362 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1366 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1369 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1371 LLVMValueRef comp
[2];
1373 src0
= ac_to_float(ctx
, src0
);
1374 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1375 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1376 for (i
= 0; i
< 2; i
++) {
1377 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1378 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1379 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1382 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1383 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1388 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1391 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1392 LLVMValueRef temps
[2], result
, val
;
1395 for (i
= 0; i
< 2; i
++) {
1396 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1397 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1398 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1399 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1402 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
1403 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(v2f32
), temps
[0],
1405 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1410 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1416 LLVMValueRef result
;
1418 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1419 mask
= AC_TID_MASK_LEFT
;
1420 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1421 mask
= AC_TID_MASK_TOP
;
1423 mask
= AC_TID_MASK_TOP_LEFT
;
1425 /* for DDX we want to next X pixel, DDY next Y pixel. */
1426 if (op
== nir_op_fddx_fine
||
1427 op
== nir_op_fddx_coarse
||
1433 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1438 * this takes an I,J coordinate pair,
1439 * and works out the X and Y derivatives.
1440 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1442 static LLVMValueRef
emit_ddxy_interp(
1443 struct ac_nir_context
*ctx
,
1444 LLVMValueRef interp_ij
)
1446 LLVMValueRef result
[4], a
;
1449 for (i
= 0; i
< 2; i
++) {
1450 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1451 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1452 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1453 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1455 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1458 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1460 LLVMValueRef src
[4], result
= NULL
;
1461 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1462 unsigned src_components
;
1463 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1465 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1466 switch (instr
->op
) {
1472 case nir_op_pack_half_2x16
:
1475 case nir_op_unpack_half_2x16
:
1479 src_components
= num_components
;
1482 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1483 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1485 switch (instr
->op
) {
1491 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1492 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1495 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1498 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1501 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1504 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1505 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1506 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1509 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1510 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1511 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1514 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1517 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1520 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1523 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1526 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1527 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1528 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1529 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1530 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1531 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1532 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1535 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1536 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1537 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1540 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1543 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1546 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1549 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1550 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1551 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1554 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1555 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1556 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1559 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1560 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, src
[0]);
1563 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1566 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1569 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1572 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1573 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1574 LLVMTypeOf(src
[0]), ""),
1578 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1579 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1580 LLVMTypeOf(src
[0]), ""),
1584 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1585 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1586 LLVMTypeOf(src
[0]), ""),
1590 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1593 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1596 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1599 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1602 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1605 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1608 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1611 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1614 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1617 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1620 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1621 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1624 result
= emit_iabs(&ctx
->ac
, src
[0]);
1627 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1630 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1633 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1636 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1639 result
= emit_isign(&ctx
->ac
, src
[0]);
1642 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1643 result
= emit_fsign(&ctx
->ac
, src
[0]);
1646 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1647 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1650 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1651 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1654 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1655 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1657 case nir_op_fround_even
:
1658 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1659 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1662 result
= emit_ffract(&ctx
->ac
, src
[0]);
1665 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1666 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1669 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1670 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1673 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1674 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1677 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1678 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1681 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1682 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1685 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1686 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1687 result
= ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, result
);
1690 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1691 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1694 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1695 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1696 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1697 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1698 ac_to_float_type(&ctx
->ac
, def_type
),
1702 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1703 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1704 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1705 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1706 ac_to_float_type(&ctx
->ac
, def_type
),
1710 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fma",
1711 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1713 case nir_op_ibitfield_extract
:
1714 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1716 case nir_op_ubitfield_extract
:
1717 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1719 case nir_op_bitfield_insert
:
1720 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1722 case nir_op_bitfield_reverse
:
1723 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1725 case nir_op_bit_count
:
1726 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1731 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1732 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1733 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1737 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1738 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1742 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1743 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1747 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1748 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1752 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1753 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1756 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1759 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1763 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1764 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1765 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1767 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1771 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1772 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1773 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1775 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1778 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1780 case nir_op_find_lsb
:
1781 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1782 result
= emit_find_lsb(&ctx
->ac
, src
[0]);
1784 case nir_op_ufind_msb
:
1785 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1786 result
= emit_ufind_msb(&ctx
->ac
, src
[0]);
1788 case nir_op_ifind_msb
:
1789 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1790 result
= emit_ifind_msb(&ctx
->ac
, src
[0]);
1792 case nir_op_uadd_carry
:
1793 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1794 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1795 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1797 case nir_op_usub_borrow
:
1798 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1799 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1800 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1803 result
= emit_b2f(&ctx
->ac
, src
[0]);
1806 result
= emit_f2b(&ctx
->ac
, src
[0]);
1809 result
= emit_b2i(&ctx
->ac
, src
[0]);
1812 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1813 result
= emit_i2b(&ctx
->ac
, src
[0]);
1815 case nir_op_fquantize2f16
:
1816 result
= emit_f2f16(ctx
->nctx
, src
[0]);
1818 case nir_op_umul_high
:
1819 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1820 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1821 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1823 case nir_op_imul_high
:
1824 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1825 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1826 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1828 case nir_op_pack_half_2x16
:
1829 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
1831 case nir_op_unpack_half_2x16
:
1832 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1836 case nir_op_fddx_fine
:
1837 case nir_op_fddy_fine
:
1838 case nir_op_fddx_coarse
:
1839 case nir_op_fddy_coarse
:
1840 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1843 case nir_op_unpack_64_2x32_split_x
: {
1844 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1845 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1846 LLVMVectorType(ctx
->ac
.i32
, 2),
1848 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1853 case nir_op_unpack_64_2x32_split_y
: {
1854 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
1855 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1856 LLVMVectorType(ctx
->ac
.i32
, 2),
1858 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1863 case nir_op_pack_64_2x32_split
: {
1864 LLVMValueRef tmp
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, 2));
1865 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1866 src
[0], ctx
->ac
.i32_0
, "");
1867 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1868 src
[1], ctx
->ac
.i32_1
, "");
1869 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1874 fprintf(stderr
, "Unknown NIR alu instr: ");
1875 nir_print_instr(&instr
->instr
, stderr
);
1876 fprintf(stderr
, "\n");
1881 assert(instr
->dest
.dest
.is_ssa
);
1882 result
= ac_to_integer(&ctx
->ac
, result
);
1883 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1888 static void visit_load_const(struct ac_nir_context
*ctx
,
1889 const nir_load_const_instr
*instr
)
1891 LLVMValueRef values
[4], value
= NULL
;
1892 LLVMTypeRef element_type
=
1893 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1895 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1896 switch (instr
->def
.bit_size
) {
1898 values
[i
] = LLVMConstInt(element_type
,
1899 instr
->value
.u32
[i
], false);
1902 values
[i
] = LLVMConstInt(element_type
,
1903 instr
->value
.u64
[i
], false);
1907 "unsupported nir load_const bit_size: %d\n",
1908 instr
->def
.bit_size
);
1912 if (instr
->def
.num_components
> 1) {
1913 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1917 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1920 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1923 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1924 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1925 LLVMPointerType(type
, addr_space
), "");
1929 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1932 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1933 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1936 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1937 /* On VI, the descriptor contains the size in bytes,
1938 * but TXQ must return the size in elements.
1939 * The stride is always non-zero for resources using TXQ.
1941 LLVMValueRef stride
=
1942 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1943 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1944 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1945 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1946 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1947 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1949 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1955 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1958 static void build_int_type_name(
1960 char *buf
, unsigned bufsize
)
1962 assert(bufsize
>= 6);
1964 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1965 snprintf(buf
, bufsize
, "v%ui32",
1966 LLVMGetVectorSize(type
));
1971 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
1972 struct ac_image_args
*args
,
1973 const nir_tex_instr
*instr
)
1975 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1976 LLVMValueRef coord
= args
->addr
;
1977 LLVMValueRef half_texel
[2];
1978 LLVMValueRef compare_cube_wa
= NULL
;
1979 LLVMValueRef result
;
1981 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1985 struct ac_image_args txq_args
= { 0 };
1987 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1988 txq_args
.opcode
= ac_image_get_resinfo
;
1989 txq_args
.dmask
= 0xf;
1990 txq_args
.addr
= ctx
->i32_0
;
1991 txq_args
.resource
= args
->resource
;
1992 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1994 for (c
= 0; c
< 2; c
++) {
1995 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1996 LLVMConstInt(ctx
->i32
, c
, false), "");
1997 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1998 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1999 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2000 LLVMConstReal(ctx
->f32
, -0.5), "");
2004 LLVMValueRef orig_coords
= args
->addr
;
2006 for (c
= 0; c
< 2; c
++) {
2008 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2009 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2010 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2011 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2012 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2013 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2018 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2019 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2020 * workaround by sampling using a scaled type and converting.
2021 * This is taken from amdgpu-pro shaders.
2023 /* NOTE this produces some ugly code compared to amdgpu-pro,
2024 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2025 * and then reads them back. -pro generates two selects,
2026 * one s_cmp for the descriptor rewriting
2027 * one v_cmp for the coordinate and result changes.
2029 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2030 LLVMValueRef tmp
, tmp2
;
2032 /* workaround 8/8/8/8 uint/sint cube gather bug */
2033 /* first detect it then change to a scaled read and f2i */
2034 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2037 /* extract the DATA_FORMAT */
2038 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2039 LLVMConstInt(ctx
->i32
, 6, false), false);
2041 /* is the DATA_FORMAT == 8_8_8_8 */
2042 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2044 if (stype
== GLSL_TYPE_UINT
)
2045 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2046 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2047 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2049 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2050 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2051 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2053 /* replace the NUM FORMAT in the descriptor */
2054 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2055 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2057 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2059 /* don't modify the coordinates for this case */
2060 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2063 result
= ac_build_image_opcode(ctx
, args
);
2065 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2066 LLVMValueRef tmp
, tmp2
;
2068 /* if the cube workaround is in place, f2i the result. */
2069 for (c
= 0; c
< 4; c
++) {
2070 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2071 if (stype
== GLSL_TYPE_UINT
)
2072 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2074 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2075 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2076 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2077 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2078 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2079 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2085 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2086 const nir_tex_instr
*instr
,
2088 struct ac_image_args
*args
)
2090 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2091 return ac_build_buffer_load_format(&ctx
->ac
,
2094 LLVMConstInt(ctx
->ac
.i32
, 0, false),
2098 args
->opcode
= ac_image_sample
;
2099 args
->compare
= instr
->is_shadow
;
2101 switch (instr
->op
) {
2103 case nir_texop_txf_ms
:
2104 case nir_texop_samples_identical
:
2105 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2106 args
->compare
= false;
2107 args
->offset
= false;
2114 args
->level_zero
= true;
2119 case nir_texop_query_levels
:
2120 args
->opcode
= ac_image_get_resinfo
;
2123 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2124 args
->level_zero
= true;
2130 args
->opcode
= ac_image_gather4
;
2131 args
->level_zero
= true;
2134 args
->opcode
= ac_image_get_lod
;
2135 args
->compare
= false;
2136 args
->offset
= false;
2142 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2143 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2144 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2145 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2148 return ac_build_image_opcode(&ctx
->ac
, args
);
2151 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2152 nir_intrinsic_instr
*instr
)
2154 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2155 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2156 unsigned binding
= nir_intrinsic_binding(instr
);
2157 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2158 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2159 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2160 unsigned base_offset
= layout
->binding
[binding
].offset
;
2161 LLVMValueRef offset
, stride
;
2163 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2164 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2165 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2166 layout
->binding
[binding
].dynamic_offset_offset
;
2167 desc_ptr
= ctx
->push_constants
;
2168 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2169 stride
= LLVMConstInt(ctx
->i32
, 16, false);
2171 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
2173 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
2174 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2175 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2177 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2178 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2179 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2181 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2184 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2185 nir_intrinsic_instr
*instr
)
2187 LLVMValueRef ptr
, addr
;
2189 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
2190 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2192 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2193 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2195 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2198 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2199 const nir_intrinsic_instr
*instr
)
2201 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2203 return get_buffer_size(ctx
, desc
, false);
2205 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2206 nir_intrinsic_instr
*instr
)
2208 const char *store_name
;
2209 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2210 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2211 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2212 int components_32bit
= elem_size_mult
* instr
->num_components
;
2213 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2214 LLVMValueRef base_data
, base_offset
;
2215 LLVMValueRef params
[6];
2216 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
2218 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2219 get_src(ctx
, instr
->src
[1]), true);
2220 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 0, false); /* vindex */
2221 params
[4] = i1false
; /* glc */
2222 params
[5] = i1false
; /* slc */
2224 if (components_32bit
> 1)
2225 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2227 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2228 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2229 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2231 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2235 LLVMValueRef offset
;
2237 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2239 /* Due to an LLVM limitation, split 3-element writes
2240 * into a 2-element and a 1-element write. */
2242 writemask
|= 1 << (start
+ 2);
2246 start
*= elem_size_mult
;
2247 count
*= elem_size_mult
;
2250 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2255 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2257 } else if (count
== 2) {
2258 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->ac
.f32
, 2);
2260 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2261 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2262 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, LLVMGetUndef(v2f32
), tmp
,
2265 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2266 base_data
, LLVMConstInt(ctx
->ac
.i32
, start
+ 1, false), "");
2267 data
= LLVMBuildInsertElement(ctx
->ac
.builder
, data
, tmp
,
2269 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2273 if (get_llvm_num_components(base_data
) > 1)
2274 data
= LLVMBuildExtractElement(ctx
->ac
.builder
, base_data
,
2275 LLVMConstInt(ctx
->ac
.i32
, start
, false), "");
2278 store_name
= "llvm.amdgcn.buffer.store.f32";
2281 offset
= base_offset
;
2283 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2287 ac_build_intrinsic(&ctx
->ac
, store_name
,
2288 ctx
->ac
.voidt
, params
, 6, 0);
2292 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2293 const nir_intrinsic_instr
*instr
)
2296 LLVMValueRef params
[6];
2299 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2300 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2302 params
[arg_count
++] = llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2303 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2304 get_src(ctx
, instr
->src
[0]),
2306 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i32
, 0, false); /* vindex */
2307 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2308 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2310 switch (instr
->intrinsic
) {
2311 case nir_intrinsic_ssbo_atomic_add
:
2312 name
= "llvm.amdgcn.buffer.atomic.add";
2314 case nir_intrinsic_ssbo_atomic_imin
:
2315 name
= "llvm.amdgcn.buffer.atomic.smin";
2317 case nir_intrinsic_ssbo_atomic_umin
:
2318 name
= "llvm.amdgcn.buffer.atomic.umin";
2320 case nir_intrinsic_ssbo_atomic_imax
:
2321 name
= "llvm.amdgcn.buffer.atomic.smax";
2323 case nir_intrinsic_ssbo_atomic_umax
:
2324 name
= "llvm.amdgcn.buffer.atomic.umax";
2326 case nir_intrinsic_ssbo_atomic_and
:
2327 name
= "llvm.amdgcn.buffer.atomic.and";
2329 case nir_intrinsic_ssbo_atomic_or
:
2330 name
= "llvm.amdgcn.buffer.atomic.or";
2332 case nir_intrinsic_ssbo_atomic_xor
:
2333 name
= "llvm.amdgcn.buffer.atomic.xor";
2335 case nir_intrinsic_ssbo_atomic_exchange
:
2336 name
= "llvm.amdgcn.buffer.atomic.swap";
2338 case nir_intrinsic_ssbo_atomic_comp_swap
:
2339 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2345 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2348 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2349 const nir_intrinsic_instr
*instr
)
2351 LLVMValueRef results
[2];
2352 int load_components
;
2353 int num_components
= instr
->num_components
;
2354 if (instr
->dest
.ssa
.bit_size
== 64)
2355 num_components
*= 2;
2357 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2358 load_components
= MIN2(num_components
- i
, 4);
2359 const char *load_name
;
2360 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2361 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2362 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2364 if (load_components
== 3)
2365 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2366 else if (load_components
> 1)
2367 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2369 if (load_components
>= 3)
2370 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2371 else if (load_components
== 2)
2372 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2373 else if (load_components
== 1)
2374 load_name
= "llvm.amdgcn.buffer.load.f32";
2376 unreachable("unhandled number of components");
2378 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
2379 LLVMValueRef params
[] = {
2380 ctx
->abi
->load_ssbo(ctx
->abi
,
2381 get_src(ctx
, instr
->src
[0]),
2383 LLVMConstInt(ctx
->ac
.i32
, 0, false),
2389 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2393 LLVMValueRef ret
= results
[0];
2394 if (num_components
> 4 || num_components
== 3) {
2395 LLVMValueRef masks
[] = {
2396 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2397 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2398 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2399 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2402 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2403 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2404 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2407 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2408 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2411 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2412 const nir_intrinsic_instr
*instr
)
2414 LLVMValueRef results
[8], ret
;
2415 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2416 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2417 int num_components
= instr
->num_components
;
2419 if (ctx
->abi
->load_ubo
)
2420 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2422 if (instr
->dest
.ssa
.bit_size
== 64)
2423 num_components
*= 2;
2425 for (unsigned i
= 0; i
< num_components
; ++i
) {
2426 LLVMValueRef params
[] = {
2428 LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, 4 * i
, 0),
2431 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const.v4i32", ctx
->ac
.f32
,
2433 AC_FUNC_ATTR_READNONE
|
2434 AC_FUNC_ATTR_LEGACY
);
2438 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2439 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2440 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2444 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2445 bool vs_in
, unsigned *vertex_index_out
,
2446 LLVMValueRef
*vertex_index_ref
,
2447 unsigned *const_out
, LLVMValueRef
*indir_out
)
2449 unsigned const_offset
= 0;
2450 nir_deref
*tail
= &deref
->deref
;
2451 LLVMValueRef offset
= NULL
;
2453 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2455 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2456 if (vertex_index_out
)
2457 *vertex_index_out
= deref_array
->base_offset
;
2459 if (vertex_index_ref
) {
2460 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2461 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2462 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2464 *vertex_index_ref
= vtx
;
2468 if (deref
->var
->data
.compact
) {
2469 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2470 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2471 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2472 /* We always lower indirect dereferences for "compact" array vars. */
2473 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2475 const_offset
= deref_array
->base_offset
;
2479 while (tail
->child
!= NULL
) {
2480 const struct glsl_type
*parent_type
= tail
->type
;
2483 if (tail
->deref_type
== nir_deref_type_array
) {
2484 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2485 LLVMValueRef index
, stride
, local_offset
;
2486 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2488 const_offset
+= size
* deref_array
->base_offset
;
2489 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2492 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2493 index
= get_src(ctx
, deref_array
->indirect
);
2494 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2495 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2498 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2500 offset
= local_offset
;
2501 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2502 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2504 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2505 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2506 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2509 unreachable("unsupported deref type");
2513 if (const_offset
&& offset
)
2514 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2515 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2518 *const_out
= const_offset
;
2519 *indir_out
= offset
;
2523 lds_load(struct nir_to_llvm_context
*ctx
,
2524 LLVMValueRef dw_addr
)
2527 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
2532 lds_store(struct nir_to_llvm_context
*ctx
,
2533 LLVMValueRef dw_addr
, LLVMValueRef value
)
2535 value
= LLVMBuildBitCast(ctx
->builder
, value
, ctx
->i32
, "");
2536 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
2540 /* The offchip buffer layout for TCS->TES is
2542 * - attribute 0 of patch 0 vertex 0
2543 * - attribute 0 of patch 0 vertex 1
2544 * - attribute 0 of patch 0 vertex 2
2546 * - attribute 0 of patch 1 vertex 0
2547 * - attribute 0 of patch 1 vertex 1
2549 * - attribute 1 of patch 0 vertex 0
2550 * - attribute 1 of patch 0 vertex 1
2552 * - per patch attribute 0 of patch 0
2553 * - per patch attribute 0 of patch 1
2556 * Note that every attribute has 4 components.
2558 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2559 LLVMValueRef vertex_index
,
2560 LLVMValueRef param_index
)
2562 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2563 LLVMValueRef param_stride
, constant16
;
2564 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2566 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2567 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2568 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2571 constant16
= LLVMConstInt(ctx
->i32
, 16, false);
2573 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2574 vertices_per_patch
, "");
2576 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2579 param_stride
= total_vertices
;
2581 base_addr
= rel_patch_id
;
2582 param_stride
= num_patches
;
2585 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2586 LLVMBuildMul(ctx
->builder
, param_index
,
2587 param_stride
, ""), "");
2589 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2591 if (!vertex_index
) {
2592 LLVMValueRef patch_data_offset
=
2593 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2595 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2596 patch_data_offset
, "");
2601 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2603 unsigned const_index
,
2605 LLVMValueRef vertex_index
,
2606 LLVMValueRef indir_index
)
2608 LLVMValueRef param_index
;
2611 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, param
, false),
2614 if (const_index
&& !is_compact
)
2615 param
+= const_index
;
2616 param_index
= LLVMConstInt(ctx
->i32
, param
, false);
2618 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2622 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2623 bool is_patch
, uint32_t param
)
2627 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2629 ctx
->tess_outputs_written
|= (1ull << param
);
2633 get_dw_address(struct nir_to_llvm_context
*ctx
,
2634 LLVMValueRef dw_addr
,
2636 unsigned const_index
,
2637 bool compact_const_index
,
2638 LLVMValueRef vertex_index
,
2639 LLVMValueRef stride
,
2640 LLVMValueRef indir_index
)
2645 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2646 LLVMBuildMul(ctx
->builder
,
2652 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2653 LLVMBuildMul(ctx
->builder
, indir_index
,
2654 LLVMConstInt(ctx
->i32
, 4, false), ""), "");
2655 else if (const_index
&& !compact_const_index
)
2656 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2657 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2659 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2660 LLVMConstInt(ctx
->i32
, param
* 4, false), "");
2662 if (const_index
&& compact_const_index
)
2663 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2664 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2669 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2670 nir_intrinsic_instr
*instr
)
2672 LLVMValueRef dw_addr
, stride
;
2673 unsigned const_index
;
2674 LLVMValueRef vertex_index
;
2675 LLVMValueRef indir_index
;
2677 LLVMValueRef value
[4], result
;
2678 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2679 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2680 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2681 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2682 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2683 &const_index
, &indir_index
);
2685 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2686 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2687 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2690 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2691 value
[i
] = lds_load(ctx
, dw_addr
);
2692 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2695 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2696 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2701 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2702 nir_intrinsic_instr
*instr
)
2704 LLVMValueRef dw_addr
;
2705 LLVMValueRef stride
= NULL
;
2706 LLVMValueRef value
[4], result
;
2707 LLVMValueRef vertex_index
= NULL
;
2708 LLVMValueRef indir_index
= NULL
;
2709 unsigned const_index
= 0;
2711 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2712 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2713 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2714 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2715 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2716 &const_index
, &indir_index
);
2718 if (!instr
->variables
[0]->var
->data
.patch
) {
2719 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2720 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2722 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2725 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2728 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2729 value
[i
] = lds_load(ctx
, dw_addr
);
2730 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2733 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2734 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2739 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2740 nir_intrinsic_instr
*instr
,
2744 LLVMValueRef dw_addr
;
2745 LLVMValueRef stride
= NULL
;
2746 LLVMValueRef buf_addr
= NULL
;
2747 LLVMValueRef vertex_index
= NULL
;
2748 LLVMValueRef indir_index
= NULL
;
2749 unsigned const_index
= 0;
2751 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2752 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2754 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2755 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2756 &const_index
, &indir_index
);
2758 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2759 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2760 is_compact
&& const_index
> 3) {
2765 if (!instr
->variables
[0]->var
->data
.patch
) {
2766 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2767 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2769 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2772 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2774 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2776 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2777 vertex_index
, indir_index
);
2779 unsigned base
= is_compact
? const_index
: 0;
2780 for (unsigned chan
= 0; chan
< 8; chan
++) {
2781 bool is_tess_factor
= false;
2782 if (!(writemask
& (1 << chan
)))
2784 LLVMValueRef value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
2786 lds_store(ctx
, dw_addr
, value
);
2788 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2789 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2790 is_tess_factor
= true;
2792 if (!is_tess_factor
&& writemask
!= 0xF)
2793 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2794 buf_addr
, ctx
->oc_lds
,
2795 4 * (base
+ chan
), 1, 0, true, false);
2797 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2801 if (writemask
== 0xF) {
2802 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2803 buf_addr
, ctx
->oc_lds
,
2804 (base
* 4), 1, 0, true, false);
2809 load_tes_input(struct nir_to_llvm_context
*ctx
,
2810 const nir_intrinsic_instr
*instr
)
2812 LLVMValueRef buf_addr
;
2813 LLVMValueRef result
;
2814 LLVMValueRef vertex_index
= NULL
;
2815 LLVMValueRef indir_index
= NULL
;
2816 unsigned const_index
= 0;
2818 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2819 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2821 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2822 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2823 &const_index
, &indir_index
);
2824 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2825 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2826 is_compact
&& const_index
> 3) {
2830 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2831 is_compact
, vertex_index
, indir_index
);
2833 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2834 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2835 result
= trim_vector(&ctx
->ac
, result
, instr
->num_components
);
2836 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
2841 load_gs_input(struct nir_to_llvm_context
*ctx
,
2842 nir_intrinsic_instr
*instr
)
2844 LLVMValueRef indir_index
, vtx_offset
;
2845 unsigned const_index
;
2846 LLVMValueRef args
[9];
2847 unsigned param
, vtx_offset_param
;
2848 LLVMValueRef value
[4], result
;
2849 unsigned vertex_index
;
2850 get_deref_offset(ctx
->nir
, instr
->variables
[0],
2851 false, &vertex_index
, NULL
,
2852 &const_index
, &indir_index
);
2853 vtx_offset_param
= vertex_index
;
2854 assert(vtx_offset_param
< 6);
2855 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2856 LLVMConstInt(ctx
->i32
, 4, false), "");
2858 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2859 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2861 args
[0] = ctx
->esgs_ring
;
2862 args
[1] = vtx_offset
;
2863 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2864 args
[3] = ctx
->i32zero
;
2865 args
[4] = ctx
->i32one
; /* OFFEN */
2866 args
[5] = ctx
->i32zero
; /* IDXEN */
2867 args
[6] = ctx
->i32one
; /* GLC */
2868 args
[7] = ctx
->i32zero
; /* SLC */
2869 args
[8] = ctx
->i32zero
; /* TFE */
2871 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2873 AC_FUNC_ATTR_READONLY
|
2874 AC_FUNC_ATTR_LEGACY
);
2876 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2882 build_gep_for_deref(struct ac_nir_context
*ctx
,
2883 nir_deref_var
*deref
)
2885 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
2886 assert(entry
->data
);
2887 LLVMValueRef val
= entry
->data
;
2888 nir_deref
*tail
= deref
->deref
.child
;
2889 while (tail
!= NULL
) {
2890 LLVMValueRef offset
;
2891 switch (tail
->deref_type
) {
2892 case nir_deref_type_array
: {
2893 nir_deref_array
*array
= nir_deref_as_array(tail
);
2894 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
2895 if (array
->deref_array_type
==
2896 nir_deref_array_type_indirect
) {
2897 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2904 case nir_deref_type_struct
: {
2905 nir_deref_struct
*deref_struct
=
2906 nir_deref_as_struct(tail
);
2907 offset
= LLVMConstInt(ctx
->ac
.i32
,
2908 deref_struct
->index
, 0);
2912 unreachable("bad deref type");
2914 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
2920 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2921 nir_intrinsic_instr
*instr
)
2923 LLVMValueRef values
[8];
2924 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2925 int ve
= instr
->dest
.ssa
.num_components
;
2926 LLVMValueRef indir_index
;
2928 unsigned const_index
;
2929 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2930 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2931 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
2932 &const_index
, &indir_index
);
2934 if (instr
->dest
.ssa
.bit_size
== 64)
2937 switch (instr
->variables
[0]->var
->data
.mode
) {
2938 case nir_var_shader_in
:
2939 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2940 return load_tcs_input(ctx
->nctx
, instr
);
2941 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
2942 return load_tes_input(ctx
->nctx
, instr
);
2943 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2944 return load_gs_input(ctx
->nctx
, instr
);
2946 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2948 unsigned count
= glsl_count_attribute_slots(
2949 instr
->variables
[0]->var
->type
,
2950 ctx
->stage
== MESA_SHADER_VERTEX
);
2952 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2953 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2956 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2960 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* 4];
2964 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2966 unsigned count
= glsl_count_attribute_slots(
2967 instr
->variables
[0]->var
->type
, false);
2969 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2970 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2973 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2977 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2981 case nir_var_shared
: {
2982 LLVMValueRef address
= build_gep_for_deref(ctx
,
2983 instr
->variables
[0]);
2984 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2985 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2986 get_def_type(ctx
, &instr
->dest
.ssa
),
2989 case nir_var_shader_out
:
2990 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2991 return load_tcs_output(ctx
->nctx
, instr
);
2992 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2994 unsigned count
= glsl_count_attribute_slots(
2995 instr
->variables
[0]->var
->type
, false);
2997 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2998 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3001 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3005 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3006 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
3012 unreachable("unhandle variable mode");
3014 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
3015 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3019 visit_store_var(struct ac_nir_context
*ctx
,
3020 nir_intrinsic_instr
*instr
)
3022 LLVMValueRef temp_ptr
, value
;
3023 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3024 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3025 int writemask
= instr
->const_index
[0];
3026 LLVMValueRef indir_index
;
3027 unsigned const_index
;
3028 get_deref_offset(ctx
, instr
->variables
[0], false,
3029 NULL
, NULL
, &const_index
, &indir_index
);
3031 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3032 int old_writemask
= writemask
;
3034 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3035 LLVMVectorType(ctx
->ac
.f32
, get_llvm_num_components(src
) * 2),
3039 for (unsigned chan
= 0; chan
< 4; chan
++) {
3040 if (old_writemask
& (1 << chan
))
3041 writemask
|= 3u << (2 * chan
);
3045 switch (instr
->variables
[0]->var
->data
.mode
) {
3046 case nir_var_shader_out
:
3048 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3049 store_tcs_output(ctx
->nctx
, instr
, src
, writemask
);
3053 for (unsigned chan
= 0; chan
< 8; chan
++) {
3055 if (!(writemask
& (1 << chan
)))
3058 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
3060 if (instr
->variables
[0]->var
->data
.compact
)
3063 unsigned count
= glsl_count_attribute_slots(
3064 instr
->variables
[0]->var
->type
, false);
3066 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3067 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3068 stride
, true, true);
3070 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3071 value
, indir_index
, "");
3072 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3073 count
, stride
, tmp_vec
);
3076 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3078 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3083 for (unsigned chan
= 0; chan
< 8; chan
++) {
3084 if (!(writemask
& (1 << chan
)))
3087 value
= llvm_extract_elem(&ctx
->ac
, src
, chan
);
3089 unsigned count
= glsl_count_attribute_slots(
3090 instr
->variables
[0]->var
->type
, false);
3092 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3093 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3096 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3097 value
, indir_index
, "");
3098 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3101 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3103 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3107 case nir_var_shared
: {
3108 int writemask
= instr
->const_index
[0];
3109 LLVMValueRef address
= build_gep_for_deref(ctx
,
3110 instr
->variables
[0]);
3111 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3112 unsigned components
=
3113 glsl_get_vector_elements(
3114 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3115 if (writemask
== (1 << components
) - 1) {
3116 val
= LLVMBuildBitCast(
3117 ctx
->ac
.builder
, val
,
3118 LLVMGetElementType(LLVMTypeOf(address
)), "");
3119 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3121 for (unsigned chan
= 0; chan
< 4; chan
++) {
3122 if (!(writemask
& (1 << chan
)))
3125 LLVMBuildStructGEP(ctx
->ac
.builder
,
3127 LLVMValueRef src
= llvm_extract_elem(&ctx
->ac
, val
,
3129 src
= LLVMBuildBitCast(
3130 ctx
->ac
.builder
, src
,
3131 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3132 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3142 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3145 case GLSL_SAMPLER_DIM_BUF
:
3147 case GLSL_SAMPLER_DIM_1D
:
3148 return array
? 2 : 1;
3149 case GLSL_SAMPLER_DIM_2D
:
3150 return array
? 3 : 2;
3151 case GLSL_SAMPLER_DIM_MS
:
3152 return array
? 4 : 3;
3153 case GLSL_SAMPLER_DIM_3D
:
3154 case GLSL_SAMPLER_DIM_CUBE
:
3156 case GLSL_SAMPLER_DIM_RECT
:
3157 case GLSL_SAMPLER_DIM_SUBPASS
:
3159 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3169 /* Adjust the sample index according to FMASK.
3171 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3172 * which is the identity mapping. Each nibble says which physical sample
3173 * should be fetched to get that sample.
3175 * For example, 0x11111100 means there are only 2 samples stored and
3176 * the second sample covers 3/4 of the pixel. When reading samples 0
3177 * and 1, return physical sample 0 (determined by the first two 0s
3178 * in FMASK), otherwise return physical sample 1.
3180 * The sample index should be adjusted as follows:
3181 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3183 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3184 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3185 LLVMValueRef coord_z
,
3186 LLVMValueRef sample_index
,
3187 LLVMValueRef fmask_desc_ptr
)
3189 LLVMValueRef fmask_load_address
[4];
3192 fmask_load_address
[0] = coord_x
;
3193 fmask_load_address
[1] = coord_y
;
3195 fmask_load_address
[2] = coord_z
;
3196 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3199 struct ac_image_args args
= {0};
3201 args
.opcode
= ac_image_load
;
3202 args
.da
= coord_z
? true : false;
3203 args
.resource
= fmask_desc_ptr
;
3205 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3207 res
= ac_build_image_opcode(ctx
, &args
);
3209 res
= ac_to_integer(ctx
, res
);
3210 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3211 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3213 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3217 LLVMValueRef sample_index4
=
3218 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3219 LLVMValueRef shifted_fmask
=
3220 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3221 LLVMValueRef final_sample
=
3222 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3224 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3225 * resource descriptor is 0 (invalid),
3227 LLVMValueRef fmask_desc
=
3228 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3231 LLVMValueRef fmask_word1
=
3232 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3235 LLVMValueRef word1_is_nonzero
=
3236 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3237 fmask_word1
, ctx
->i32_0
, "");
3239 /* Replace the MSAA sample index. */
3241 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3242 final_sample
, sample_index
, "");
3243 return sample_index
;
3246 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3247 const nir_intrinsic_instr
*instr
)
3249 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3250 if(instr
->variables
[0]->deref
.child
)
3251 type
= instr
->variables
[0]->deref
.child
->type
;
3253 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3254 LLVMValueRef coords
[4];
3255 LLVMValueRef masks
[] = {
3256 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3257 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3260 LLVMValueRef sample_index
= llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3263 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3264 bool is_array
= glsl_sampler_type_is_array(type
);
3265 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3266 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3267 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3268 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3269 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3270 count
= image_type_to_components_count(dim
, is_array
);
3273 LLVMValueRef fmask_load_address
[3];
3276 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3277 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3279 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3281 fmask_load_address
[2] = NULL
;
3283 for (chan
= 0; chan
< 2; ++chan
)
3284 fmask_load_address
[chan
] =
3285 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3286 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3287 ctx
->ac
.i32
, ""), "");
3288 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3290 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3291 fmask_load_address
[0],
3292 fmask_load_address
[1],
3293 fmask_load_address
[2],
3295 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, true, false));
3297 if (count
== 1 && !gfx9_1d
) {
3298 if (instr
->src
[0].ssa
->num_components
)
3299 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3306 for (chan
= 0; chan
< count
; ++chan
) {
3307 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3310 for (chan
= 0; chan
< 2; ++chan
)
3311 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3312 ctx
->ac
.i32
, ""), "");
3313 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3319 coords
[2] = coords
[1];
3320 coords
[1] = ctx
->ac
.i32_0
;
3322 coords
[1] = ctx
->ac
.i32_0
;
3327 coords
[count
] = sample_index
;
3332 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3335 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3340 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3341 const nir_intrinsic_instr
*instr
)
3343 LLVMValueRef params
[7];
3345 char intrinsic_name
[64];
3346 const nir_variable
*var
= instr
->variables
[0]->var
;
3347 const struct glsl_type
*type
= var
->type
;
3348 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
3349 LLVMValueRef i1true
= LLVMConstInt(ctx
->ac
.i1
, 1, false);
3351 if(instr
->variables
[0]->deref
.child
)
3352 type
= instr
->variables
[0]->deref
.child
->type
;
3354 type
= glsl_without_array(type
);
3355 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3356 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, true, false);
3357 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3358 ctx
->ac
.i32_0
, ""); /* vindex */
3359 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3360 params
[3] = i1false
; /* glc */
3361 params
[4] = i1false
; /* slc */
3362 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3365 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3366 res
= ac_to_integer(&ctx
->ac
, res
);
3368 bool is_da
= glsl_sampler_type_is_array(type
) ||
3369 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3370 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3371 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3372 LLVMValueRef da
= is_da
? i1true
: i1false
;
3373 LLVMValueRef glc
= i1false
;
3374 LLVMValueRef slc
= i1false
;
3376 params
[0] = get_image_coords(ctx
, instr
);
3377 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, true, false);
3378 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3379 if (HAVE_LLVM
<= 0x0309) {
3380 params
[3] = i1false
; /* r128 */
3385 LLVMValueRef lwe
= i1false
;
3392 ac_get_image_intr_name("llvm.amdgcn.image.load",
3393 ctx
->ac
.v4f32
, /* vdata */
3394 LLVMTypeOf(params
[0]), /* coords */
3395 LLVMTypeOf(params
[1]), /* rsrc */
3396 intrinsic_name
, sizeof(intrinsic_name
));
3398 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3399 params
, 7, AC_FUNC_ATTR_READONLY
);
3401 return ac_to_integer(&ctx
->ac
, res
);
3404 static void visit_image_store(struct ac_nir_context
*ctx
,
3405 nir_intrinsic_instr
*instr
)
3407 LLVMValueRef params
[8];
3408 char intrinsic_name
[64];
3409 const nir_variable
*var
= instr
->variables
[0]->var
;
3410 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3411 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
3412 LLVMValueRef i1true
= LLVMConstInt(ctx
->ac
.i1
, 1, false);
3413 LLVMValueRef glc
= i1false
;
3414 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3418 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3419 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3420 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, true, true);
3421 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3422 ctx
->ac
.i32_0
, ""); /* vindex */
3423 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3424 params
[4] = glc
; /* glc */
3425 params
[5] = i1false
; /* slc */
3426 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3429 bool is_da
= glsl_sampler_type_is_array(type
) ||
3430 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3431 LLVMValueRef da
= is_da
? i1true
: i1false
;
3432 LLVMValueRef slc
= i1false
;
3434 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3435 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3436 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, true, true);
3437 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3438 if (HAVE_LLVM
<= 0x0309) {
3439 params
[4] = i1false
; /* r128 */
3444 LLVMValueRef lwe
= i1false
;
3451 ac_get_image_intr_name("llvm.amdgcn.image.store",
3452 LLVMTypeOf(params
[0]), /* vdata */
3453 LLVMTypeOf(params
[1]), /* coords */
3454 LLVMTypeOf(params
[2]), /* rsrc */
3455 intrinsic_name
, sizeof(intrinsic_name
));
3457 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3463 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3464 const nir_intrinsic_instr
*instr
)
3466 LLVMValueRef params
[7];
3467 int param_count
= 0;
3468 const nir_variable
*var
= instr
->variables
[0]->var
;
3470 const char *atomic_name
;
3471 char intrinsic_name
[41];
3472 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3473 LLVMValueRef i1false
= LLVMConstInt(ctx
->ac
.i1
, 0, false);
3474 LLVMValueRef i1true
= LLVMConstInt(ctx
->ac
.i1
, 1, false);
3475 MAYBE_UNUSED
int length
;
3477 switch (instr
->intrinsic
) {
3478 case nir_intrinsic_image_atomic_add
:
3479 atomic_name
= "add";
3481 case nir_intrinsic_image_atomic_min
:
3482 atomic_name
= "smin";
3484 case nir_intrinsic_image_atomic_max
:
3485 atomic_name
= "smax";
3487 case nir_intrinsic_image_atomic_and
:
3488 atomic_name
= "and";
3490 case nir_intrinsic_image_atomic_or
:
3493 case nir_intrinsic_image_atomic_xor
:
3494 atomic_name
= "xor";
3496 case nir_intrinsic_image_atomic_exchange
:
3497 atomic_name
= "swap";
3499 case nir_intrinsic_image_atomic_comp_swap
:
3500 atomic_name
= "cmpswap";
3506 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3507 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3508 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3510 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3511 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3513 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3514 ctx
->ac
.i32_0
, ""); /* vindex */
3515 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3516 params
[param_count
++] = i1false
; /* slc */
3518 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3519 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3521 char coords_type
[8];
3523 bool da
= glsl_sampler_type_is_array(type
) ||
3524 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3526 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3527 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3529 params
[param_count
++] = i1false
; /* r128 */
3530 params
[param_count
++] = da
? i1true
: i1false
; /* da */
3531 params
[param_count
++] = i1false
; /* slc */
3533 build_int_type_name(LLVMTypeOf(coords
),
3534 coords_type
, sizeof(coords_type
));
3536 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3537 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3540 assert(length
< sizeof(intrinsic_name
));
3541 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3544 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3545 const nir_intrinsic_instr
*instr
)
3548 const nir_variable
*var
= instr
->variables
[0]->var
;
3549 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3550 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3551 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3552 if(instr
->variables
[0]->deref
.child
)
3553 type
= instr
->variables
[0]->deref
.child
->type
;
3555 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3556 return get_buffer_size(ctx
,
3557 get_sampler_desc(ctx
, instr
->variables
[0],
3558 AC_DESC_BUFFER
, true, false), true);
3560 struct ac_image_args args
= { 0 };
3564 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, true, false);
3565 args
.opcode
= ac_image_get_resinfo
;
3566 args
.addr
= ctx
->ac
.i32_0
;
3568 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3570 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3572 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3573 glsl_sampler_type_is_array(type
)) {
3574 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3575 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3576 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3577 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3579 if (ctx
->ac
.chip_class
>= GFX9
&&
3580 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3581 glsl_sampler_type_is_array(type
)) {
3582 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3583 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3590 #define NOOP_WAITCNT 0xf7f
3591 #define LGKM_CNT 0x07f
3592 #define VM_CNT 0xf70
3594 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3597 LLVMValueRef args
[1] = {
3598 LLVMConstInt(ctx
->i32
, simm16
, false),
3600 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3601 ctx
->voidt
, args
, 1, 0);
3604 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3606 /* SI only (thanks to a hw bug workaround):
3607 * The real barrier instruction isn’t needed, because an entire patch
3608 * always fits into a single wave.
3610 if (ctx
->options
->chip_class
== SI
&&
3611 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3612 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3615 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3616 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3619 static void emit_discard_if(struct ac_nir_context
*ctx
,
3620 const nir_intrinsic_instr
*instr
)
3624 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
3625 get_src(ctx
, instr
->src
[0]),
3628 cond
= LLVMBuildSelect(ctx
->ac
.builder
, cond
,
3629 LLVMConstReal(ctx
->ac
.f32
, -1.0f
),
3631 ac_build_kill(&ctx
->ac
, cond
);
3635 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3637 LLVMValueRef result
;
3638 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3639 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3640 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
3642 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3645 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3646 const nir_intrinsic_instr
*instr
)
3648 LLVMValueRef ptr
, result
;
3649 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3650 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3652 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3653 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3654 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3656 LLVMAtomicOrderingSequentiallyConsistent
,
3657 LLVMAtomicOrderingSequentiallyConsistent
,
3660 LLVMAtomicRMWBinOp op
;
3661 switch (instr
->intrinsic
) {
3662 case nir_intrinsic_var_atomic_add
:
3663 op
= LLVMAtomicRMWBinOpAdd
;
3665 case nir_intrinsic_var_atomic_umin
:
3666 op
= LLVMAtomicRMWBinOpUMin
;
3668 case nir_intrinsic_var_atomic_umax
:
3669 op
= LLVMAtomicRMWBinOpUMax
;
3671 case nir_intrinsic_var_atomic_imin
:
3672 op
= LLVMAtomicRMWBinOpMin
;
3674 case nir_intrinsic_var_atomic_imax
:
3675 op
= LLVMAtomicRMWBinOpMax
;
3677 case nir_intrinsic_var_atomic_and
:
3678 op
= LLVMAtomicRMWBinOpAnd
;
3680 case nir_intrinsic_var_atomic_or
:
3681 op
= LLVMAtomicRMWBinOpOr
;
3683 case nir_intrinsic_var_atomic_xor
:
3684 op
= LLVMAtomicRMWBinOpXor
;
3686 case nir_intrinsic_var_atomic_exchange
:
3687 op
= LLVMAtomicRMWBinOpXchg
;
3693 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3694 LLVMAtomicOrderingSequentiallyConsistent
,
3700 #define INTERP_CENTER 0
3701 #define INTERP_CENTROID 1
3702 #define INTERP_SAMPLE 2
3704 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3705 enum glsl_interp_mode interp
, unsigned location
)
3708 case INTERP_MODE_FLAT
:
3711 case INTERP_MODE_SMOOTH
:
3712 case INTERP_MODE_NONE
:
3713 if (location
== INTERP_CENTER
)
3714 return ctx
->persp_center
;
3715 else if (location
== INTERP_CENTROID
)
3716 return ctx
->persp_centroid
;
3717 else if (location
== INTERP_SAMPLE
)
3718 return ctx
->persp_sample
;
3720 case INTERP_MODE_NOPERSPECTIVE
:
3721 if (location
== INTERP_CENTER
)
3722 return ctx
->linear_center
;
3723 else if (location
== INTERP_CENTROID
)
3724 return ctx
->linear_centroid
;
3725 else if (location
== INTERP_SAMPLE
)
3726 return ctx
->linear_sample
;
3732 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3733 LLVMValueRef sample_id
)
3735 LLVMValueRef result
;
3736 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_PS_SAMPLE_POSITIONS
, false));
3738 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3739 const_array(ctx
->v2f32
, 64), "");
3741 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3742 result
= ac_build_indexed_load(&ctx
->ac
, ptr
, sample_id
, false);
3747 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3749 LLVMValueRef values
[2];
3751 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3752 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3753 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3756 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3757 const nir_intrinsic_instr
*instr
)
3759 LLVMValueRef result
[2];
3760 LLVMValueRef interp_param
, attr_number
;
3763 LLVMValueRef src_c0
= NULL
;
3764 LLVMValueRef src_c1
= NULL
;
3765 LLVMValueRef src0
= NULL
;
3766 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3767 switch (instr
->intrinsic
) {
3768 case nir_intrinsic_interp_var_at_centroid
:
3769 location
= INTERP_CENTROID
;
3771 case nir_intrinsic_interp_var_at_sample
:
3772 case nir_intrinsic_interp_var_at_offset
:
3773 location
= INTERP_CENTER
;
3774 src0
= get_src(ctx
->nir
, instr
->src
[0]);
3780 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3781 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3782 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3783 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3784 LLVMValueRef sample_position
;
3785 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3787 /* fetch sample ID */
3788 sample_position
= load_sample_position(ctx
, src0
);
3790 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3791 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3792 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3793 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3795 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3796 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3798 if (location
== INTERP_CENTER
) {
3799 LLVMValueRef ij_out
[2];
3800 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
3803 * take the I then J parameters, and the DDX/Y for it, and
3804 * calculate the IJ inputs for the interpolator.
3805 * temp1 = ddx * offset/sample.x + I;
3806 * interp_param.I = ddy * offset/sample.y + temp1;
3807 * temp1 = ddx * offset/sample.x + J;
3808 * interp_param.J = ddy * offset/sample.y + temp1;
3810 for (unsigned i
= 0; i
< 2; i
++) {
3811 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3812 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3813 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3814 ddxy_out
, ix_ll
, "");
3815 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3816 ddxy_out
, iy_ll
, "");
3817 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3818 interp_param
, ix_ll
, "");
3819 LLVMValueRef temp1
, temp2
;
3821 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3824 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3825 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3827 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3828 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3830 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3831 temp2
, ctx
->i32
, "");
3833 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3837 for (chan
= 0; chan
< 2; chan
++) {
3838 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3841 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3842 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3843 LLVMValueRef i
= LLVMBuildExtractElement(
3844 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3845 LLVMValueRef j
= LLVMBuildExtractElement(
3846 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3848 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3849 llvm_chan
, attr_number
,
3850 ctx
->prim_mask
, i
, j
);
3852 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3853 LLVMConstInt(ctx
->i32
, 2, false),
3854 llvm_chan
, attr_number
,
3858 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3862 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3863 const nir_intrinsic_instr
*instr
)
3865 LLVMValueRef gs_next_vertex
;
3866 LLVMValueRef can_emit
, kill
;
3869 assert(instr
->const_index
[0] == 0);
3870 /* Write vertex attribute values to GSVS ring */
3871 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3872 ctx
->gs_next_vertex
,
3875 /* If this thread has already emitted the declared maximum number of
3876 * vertices, kill it: excessive vertex emissions are not supposed to
3877 * have any effect, and GS threads have no externally observable
3878 * effects other than emitting vertices.
3880 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3881 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3883 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3884 LLVMConstReal(ctx
->f32
, 1.0f
),
3885 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3886 ac_build_kill(&ctx
->ac
, kill
);
3888 /* loop num outputs */
3890 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3891 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
3896 if (!(ctx
->output_mask
& (1ull << i
)))
3899 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3900 /* pack clip and cull into a single set of slots */
3901 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3905 for (unsigned j
= 0; j
< length
; j
++) {
3906 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3908 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3909 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3910 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3912 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3914 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3916 voffset
, ctx
->gs2vs_offset
, 0,
3922 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3924 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3926 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3930 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3931 const nir_intrinsic_instr
*instr
)
3933 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3937 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
3938 const nir_intrinsic_instr
*instr
)
3940 LLVMValueRef coord
[4] = {
3947 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
3948 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->f32one
,
3949 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
3951 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
3952 return LLVMBuildBitCast(ctx
->builder
, result
,
3953 get_def_type(ctx
->nir
, &instr
->dest
.ssa
), "");
3956 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3957 nir_intrinsic_instr
*instr
)
3959 LLVMValueRef result
= NULL
;
3961 switch (instr
->intrinsic
) {
3962 case nir_intrinsic_load_work_group_id
: {
3963 result
= ctx
->nctx
->workgroup_ids
;
3966 case nir_intrinsic_load_base_vertex
: {
3967 result
= ctx
->abi
->base_vertex
;
3970 case nir_intrinsic_load_vertex_id_zero_base
: {
3971 result
= ctx
->abi
->vertex_id
;
3974 case nir_intrinsic_load_local_invocation_id
: {
3975 result
= ctx
->nctx
->local_invocation_ids
;
3978 case nir_intrinsic_load_base_instance
:
3979 result
= ctx
->abi
->start_instance
;
3981 case nir_intrinsic_load_draw_id
:
3982 result
= ctx
->abi
->draw_id
;
3984 case nir_intrinsic_load_view_index
:
3985 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
3987 case nir_intrinsic_load_invocation_id
:
3988 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3989 result
= unpack_param(&ctx
->ac
, ctx
->nctx
->tcs_rel_ids
, 8, 5);
3991 result
= ctx
->nctx
->gs_invocation_id
;
3993 case nir_intrinsic_load_primitive_id
:
3994 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3995 ctx
->nctx
->shader_info
->gs
.uses_prim_id
= true;
3996 result
= ctx
->nctx
->gs_prim_id
;
3997 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3998 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
3999 result
= ctx
->nctx
->tcs_patch_id
;
4000 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4001 ctx
->nctx
->shader_info
->tcs
.uses_prim_id
= true;
4002 result
= ctx
->nctx
->tes_patch_id
;
4004 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4006 case nir_intrinsic_load_sample_id
:
4007 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4009 case nir_intrinsic_load_sample_pos
:
4010 result
= load_sample_pos(ctx
);
4012 case nir_intrinsic_load_sample_mask_in
:
4013 result
= ctx
->abi
->sample_coverage
;
4015 case nir_intrinsic_load_frag_coord
: {
4016 LLVMValueRef values
[4] = {
4017 ctx
->abi
->frag_pos
[0],
4018 ctx
->abi
->frag_pos
[1],
4019 ctx
->abi
->frag_pos
[2],
4020 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4022 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4025 case nir_intrinsic_load_front_face
:
4026 result
= ctx
->abi
->front_face
;
4028 case nir_intrinsic_load_instance_id
:
4029 result
= ctx
->abi
->instance_id
;
4031 case nir_intrinsic_load_num_work_groups
:
4032 result
= ctx
->nctx
->num_work_groups
;
4034 case nir_intrinsic_load_local_invocation_index
:
4035 result
= visit_load_local_invocation_index(ctx
->nctx
);
4037 case nir_intrinsic_load_push_constant
:
4038 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4040 case nir_intrinsic_vulkan_resource_index
:
4041 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4043 case nir_intrinsic_store_ssbo
:
4044 visit_store_ssbo(ctx
, instr
);
4046 case nir_intrinsic_load_ssbo
:
4047 result
= visit_load_buffer(ctx
, instr
);
4049 case nir_intrinsic_ssbo_atomic_add
:
4050 case nir_intrinsic_ssbo_atomic_imin
:
4051 case nir_intrinsic_ssbo_atomic_umin
:
4052 case nir_intrinsic_ssbo_atomic_imax
:
4053 case nir_intrinsic_ssbo_atomic_umax
:
4054 case nir_intrinsic_ssbo_atomic_and
:
4055 case nir_intrinsic_ssbo_atomic_or
:
4056 case nir_intrinsic_ssbo_atomic_xor
:
4057 case nir_intrinsic_ssbo_atomic_exchange
:
4058 case nir_intrinsic_ssbo_atomic_comp_swap
:
4059 result
= visit_atomic_ssbo(ctx
, instr
);
4061 case nir_intrinsic_load_ubo
:
4062 result
= visit_load_ubo_buffer(ctx
, instr
);
4064 case nir_intrinsic_get_buffer_size
:
4065 result
= visit_get_buffer_size(ctx
, instr
);
4067 case nir_intrinsic_load_var
:
4068 result
= visit_load_var(ctx
, instr
);
4070 case nir_intrinsic_store_var
:
4071 visit_store_var(ctx
, instr
);
4073 case nir_intrinsic_image_load
:
4074 result
= visit_image_load(ctx
, instr
);
4076 case nir_intrinsic_image_store
:
4077 visit_image_store(ctx
, instr
);
4079 case nir_intrinsic_image_atomic_add
:
4080 case nir_intrinsic_image_atomic_min
:
4081 case nir_intrinsic_image_atomic_max
:
4082 case nir_intrinsic_image_atomic_and
:
4083 case nir_intrinsic_image_atomic_or
:
4084 case nir_intrinsic_image_atomic_xor
:
4085 case nir_intrinsic_image_atomic_exchange
:
4086 case nir_intrinsic_image_atomic_comp_swap
:
4087 result
= visit_image_atomic(ctx
, instr
);
4089 case nir_intrinsic_image_size
:
4090 result
= visit_image_size(ctx
, instr
);
4092 case nir_intrinsic_discard
:
4093 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4094 LLVMVoidTypeInContext(ctx
->ac
.context
),
4095 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4097 case nir_intrinsic_discard_if
:
4098 emit_discard_if(ctx
, instr
);
4100 case nir_intrinsic_memory_barrier
:
4101 emit_waitcnt(ctx
->nctx
, VM_CNT
);
4103 case nir_intrinsic_barrier
:
4104 emit_barrier(ctx
->nctx
);
4106 case nir_intrinsic_var_atomic_add
:
4107 case nir_intrinsic_var_atomic_imin
:
4108 case nir_intrinsic_var_atomic_umin
:
4109 case nir_intrinsic_var_atomic_imax
:
4110 case nir_intrinsic_var_atomic_umax
:
4111 case nir_intrinsic_var_atomic_and
:
4112 case nir_intrinsic_var_atomic_or
:
4113 case nir_intrinsic_var_atomic_xor
:
4114 case nir_intrinsic_var_atomic_exchange
:
4115 case nir_intrinsic_var_atomic_comp_swap
:
4116 result
= visit_var_atomic(ctx
->nctx
, instr
);
4118 case nir_intrinsic_interp_var_at_centroid
:
4119 case nir_intrinsic_interp_var_at_sample
:
4120 case nir_intrinsic_interp_var_at_offset
:
4121 result
= visit_interp(ctx
->nctx
, instr
);
4123 case nir_intrinsic_emit_vertex
:
4124 visit_emit_vertex(ctx
->nctx
, instr
);
4126 case nir_intrinsic_end_primitive
:
4127 visit_end_primitive(ctx
->nctx
, instr
);
4129 case nir_intrinsic_load_tess_coord
:
4130 result
= visit_load_tess_coord(ctx
->nctx
, instr
);
4132 case nir_intrinsic_load_patch_vertices_in
:
4133 result
= LLVMConstInt(ctx
->ac
.i32
, ctx
->nctx
->options
->key
.tcs
.input_vertices
, false);
4136 fprintf(stderr
, "Unknown intrinsic: ");
4137 nir_print_instr(&instr
->instr
, stderr
);
4138 fprintf(stderr
, "\n");
4142 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4146 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4147 LLVMValueRef buffer
, bool write
)
4149 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4151 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4152 ctx
->shader_info
->fs
.writes_memory
= true;
4157 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4158 unsigned descriptor_set
,
4159 unsigned base_index
,
4160 unsigned constant_index
,
4162 enum ac_descriptor_type desc_type
,
4163 bool image
, bool write
)
4165 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4166 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4167 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4168 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4169 unsigned offset
= binding
->offset
;
4170 unsigned stride
= binding
->size
;
4172 LLVMBuilderRef builder
= ctx
->builder
;
4175 assert(base_index
< layout
->binding_count
);
4177 if (write
&& ctx
->stage
== MESA_SHADER_FRAGMENT
)
4178 ctx
->shader_info
->fs
.writes_memory
= true;
4180 switch (desc_type
) {
4190 case AC_DESC_SAMPLER
:
4192 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4197 case AC_DESC_BUFFER
:
4202 unreachable("invalid desc_type\n");
4205 offset
+= constant_index
* stride
;
4207 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4208 (!index
|| binding
->immutable_samplers_equal
)) {
4209 if (binding
->immutable_samplers_equal
)
4212 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4214 LLVMValueRef constants
[] = {
4215 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 0], 0),
4216 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 1], 0),
4217 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 2], 0),
4218 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 3], 0),
4220 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4223 assert(stride
% type_size
== 0);
4226 index
= ctx
->i32zero
;
4228 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
4230 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
4231 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4233 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4236 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4237 const nir_deref_var
*deref
,
4238 enum ac_descriptor_type desc_type
,
4239 bool image
, bool write
)
4241 LLVMValueRef index
= NULL
;
4242 unsigned constant_index
= 0;
4243 const nir_deref
*tail
= &deref
->deref
;
4245 while (tail
->child
) {
4246 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4247 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4252 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4254 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4255 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4257 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4258 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4263 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4266 constant_index
+= child
->base_offset
* array_size
;
4268 tail
= &child
->deref
;
4271 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4272 deref
->var
->data
.descriptor_set
,
4273 deref
->var
->data
.binding
,
4274 constant_index
, index
,
4275 desc_type
, image
, write
);
4278 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4279 struct ac_image_args
*args
,
4280 const nir_tex_instr
*instr
,
4282 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4283 LLVMValueRef
*param
, unsigned count
,
4286 unsigned is_rect
= 0;
4287 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4289 if (op
== nir_texop_lod
)
4291 /* Pad to power of two vector */
4292 while (count
< util_next_power_of_two(count
))
4293 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4296 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4298 args
->addr
= param
[0];
4300 args
->resource
= res_ptr
;
4301 args
->sampler
= samp_ptr
;
4303 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4304 args
->addr
= param
[0];
4308 args
->dmask
= dmask
;
4309 args
->unorm
= is_rect
;
4313 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4316 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4317 * filtering manually. The driver sets img7 to a mask clearing
4318 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4319 * s_and_b32 samp0, samp0, img7
4322 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4324 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4325 LLVMValueRef res
, LLVMValueRef samp
)
4327 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4328 LLVMValueRef img7
, samp0
;
4330 if (ctx
->ac
.chip_class
>= VI
)
4333 img7
= LLVMBuildExtractElement(builder
, res
,
4334 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4335 samp0
= LLVMBuildExtractElement(builder
, samp
,
4336 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4337 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4338 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4339 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4342 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4343 nir_tex_instr
*instr
,
4344 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4345 LLVMValueRef
*fmask_ptr
)
4347 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4348 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, false, false);
4350 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, false, false);
4353 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, false, false);
4355 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, false, false);
4356 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4357 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4359 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4360 instr
->op
== nir_texop_samples_identical
))
4361 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, false, false);
4364 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4367 coord
= ac_to_float(ctx
, coord
);
4368 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4369 coord
= ac_to_integer(ctx
, coord
);
4373 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4375 LLVMValueRef result
= NULL
;
4376 struct ac_image_args args
= { 0 };
4377 unsigned dmask
= 0xf;
4378 LLVMValueRef address
[16];
4379 LLVMValueRef coords
[5];
4380 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4381 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4382 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4383 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4384 LLVMValueRef derivs
[6];
4385 unsigned chan
, count
= 0;
4386 unsigned const_src
= 0, num_deriv_comp
= 0;
4387 bool lod_is_zero
= false;
4389 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4391 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4392 switch (instr
->src
[i
].src_type
) {
4393 case nir_tex_src_coord
:
4394 coord
= get_src(ctx
, instr
->src
[i
].src
);
4396 case nir_tex_src_projector
:
4398 case nir_tex_src_comparator
:
4399 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4401 case nir_tex_src_offset
:
4402 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4405 case nir_tex_src_bias
:
4406 bias
= get_src(ctx
, instr
->src
[i
].src
);
4408 case nir_tex_src_lod
: {
4409 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4411 if (val
&& val
->i32
[0] == 0)
4413 lod
= get_src(ctx
, instr
->src
[i
].src
);
4416 case nir_tex_src_ms_index
:
4417 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4419 case nir_tex_src_ms_mcs
:
4421 case nir_tex_src_ddx
:
4422 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4423 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4425 case nir_tex_src_ddy
:
4426 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4428 case nir_tex_src_texture_offset
:
4429 case nir_tex_src_sampler_offset
:
4430 case nir_tex_src_plane
:
4436 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4437 result
= get_buffer_size(ctx
, res_ptr
, true);
4441 if (instr
->op
== nir_texop_texture_samples
) {
4442 LLVMValueRef res
, samples
, is_msaa
;
4443 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4444 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4445 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4446 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4447 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4448 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4449 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4450 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4451 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4453 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4454 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4455 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4456 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4457 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4459 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4466 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4467 coords
[chan
] = llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4469 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4470 LLVMValueRef offset
[3], pack
;
4471 for (chan
= 0; chan
< 3; ++chan
)
4472 offset
[chan
] = ctx
->ac
.i32_0
;
4475 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4476 offset
[chan
] = llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4477 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4478 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4480 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4481 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4483 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4484 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4485 address
[count
++] = pack
;
4488 /* pack LOD bias value */
4489 if (instr
->op
== nir_texop_txb
&& bias
) {
4490 address
[count
++] = bias
;
4493 /* Pack depth comparison value */
4494 if (instr
->is_shadow
&& comparator
) {
4495 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4496 llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4498 /* TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4499 * so the depth comparison value isn't clamped for Z16 and
4500 * Z24 anymore. Do it manually here.
4502 * It's unnecessary if the original texture format was
4503 * Z32_FLOAT, but we don't know that here.
4505 if (ctx
->ac
.chip_class
== VI
)
4506 z
= ac_build_clamp(&ctx
->ac
, z
);
4508 address
[count
++] = z
;
4511 /* pack derivatives */
4513 int num_src_deriv_channels
, num_dest_deriv_channels
;
4514 switch (instr
->sampler_dim
) {
4515 case GLSL_SAMPLER_DIM_3D
:
4516 case GLSL_SAMPLER_DIM_CUBE
:
4518 num_src_deriv_channels
= 3;
4519 num_dest_deriv_channels
= 3;
4521 case GLSL_SAMPLER_DIM_2D
:
4523 num_src_deriv_channels
= 2;
4524 num_dest_deriv_channels
= 2;
4527 case GLSL_SAMPLER_DIM_1D
:
4528 num_src_deriv_channels
= 1;
4529 if (ctx
->ac
.chip_class
>= GFX9
) {
4530 num_dest_deriv_channels
= 2;
4533 num_dest_deriv_channels
= 1;
4539 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4540 derivs
[i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4541 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4543 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4544 derivs
[i
] = ctx
->ac
.f32_0
;
4545 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4549 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4550 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4551 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4552 if (instr
->coord_components
== 3)
4553 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4554 ac_prepare_cube_coords(&ctx
->ac
,
4555 instr
->op
== nir_texop_txd
, instr
->is_array
,
4556 instr
->op
== nir_texop_lod
, coords
, derivs
);
4562 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4563 address
[count
++] = derivs
[i
];
4566 /* Pack texture coordinates */
4568 address
[count
++] = coords
[0];
4569 if (instr
->coord_components
> 1) {
4570 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4571 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
4573 address
[count
++] = coords
[1];
4575 if (instr
->coord_components
> 2) {
4576 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4577 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4578 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4579 instr
->op
!= nir_texop_txf
) {
4580 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
4582 address
[count
++] = coords
[2];
4585 if (ctx
->ac
.chip_class
>= GFX9
) {
4586 LLVMValueRef filler
;
4587 if (instr
->op
== nir_texop_txf
)
4588 filler
= ctx
->ac
.i32_0
;
4590 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4592 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
4593 if (instr
->is_array
) {
4594 address
[count
] = address
[count
- 1];
4595 address
[count
- 1] = filler
;
4598 address
[count
++] = filler
;
4604 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4605 instr
->op
== nir_texop_txf
)) {
4606 address
[count
++] = lod
;
4607 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4608 address
[count
++] = sample_index
;
4609 } else if(instr
->op
== nir_texop_txs
) {
4612 address
[count
++] = lod
;
4614 address
[count
++] = ctx
->ac
.i32_0
;
4617 for (chan
= 0; chan
< count
; chan
++) {
4618 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
4619 address
[chan
], ctx
->ac
.i32
, "");
4622 if (instr
->op
== nir_texop_samples_identical
) {
4623 LLVMValueRef txf_address
[4];
4624 struct ac_image_args txf_args
= { 0 };
4625 unsigned txf_count
= count
;
4626 memcpy(txf_address
, address
, sizeof(txf_address
));
4628 if (!instr
->is_array
)
4629 txf_address
[2] = ctx
->ac
.i32_0
;
4630 txf_address
[3] = ctx
->ac
.i32_0
;
4632 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
4634 txf_address
, txf_count
, 0xf);
4636 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4638 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4639 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4643 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4644 instr
->op
!= nir_texop_txs
) {
4645 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4646 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
4649 instr
->is_array
? address
[2] : NULL
,
4650 address
[sample_chan
],
4654 if (offsets
&& instr
->op
== nir_texop_txf
) {
4655 nir_const_value
*const_offset
=
4656 nir_src_as_const_value(instr
->src
[const_src
].src
);
4657 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4658 assert(const_offset
);
4659 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4660 if (num_offsets
> 2)
4661 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
4662 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
4663 if (num_offsets
> 1)
4664 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
4665 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
4666 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
4667 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
4671 /* TODO TG4 support */
4672 if (instr
->op
== nir_texop_tg4
) {
4673 if (instr
->is_shadow
)
4676 dmask
= 1 << instr
->component
;
4678 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
4679 res_ptr
, samp_ptr
, address
, count
, dmask
);
4681 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4683 if (instr
->op
== nir_texop_query_levels
)
4684 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4685 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4686 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4687 instr
->op
!= nir_texop_tg4
)
4688 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4689 else if (instr
->op
== nir_texop_txs
&&
4690 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4692 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4693 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4694 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4695 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4696 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4697 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4698 instr
->op
== nir_texop_txs
&&
4699 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4701 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4702 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4703 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4705 } else if (instr
->dest
.ssa
.num_components
!= 4)
4706 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4710 assert(instr
->dest
.is_ssa
);
4711 result
= ac_to_integer(&ctx
->ac
, result
);
4712 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4717 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4719 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4720 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4722 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4723 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4726 static void visit_post_phi(struct ac_nir_context
*ctx
,
4727 nir_phi_instr
*instr
,
4728 LLVMValueRef llvm_phi
)
4730 nir_foreach_phi_src(src
, instr
) {
4731 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4732 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4734 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4738 static void phi_post_pass(struct ac_nir_context
*ctx
)
4740 struct hash_entry
*entry
;
4741 hash_table_foreach(ctx
->phis
, entry
) {
4742 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4743 (LLVMValueRef
)entry
->data
);
4748 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4749 const nir_ssa_undef_instr
*instr
)
4751 unsigned num_components
= instr
->def
.num_components
;
4754 if (num_components
== 1)
4755 undef
= LLVMGetUndef(ctx
->ac
.i32
);
4757 undef
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.i32
, num_components
));
4759 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4762 static void visit_jump(struct ac_nir_context
*ctx
,
4763 const nir_jump_instr
*instr
)
4765 switch (instr
->type
) {
4766 case nir_jump_break
:
4767 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
4768 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4770 case nir_jump_continue
:
4771 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4772 LLVMClearInsertionPosition(ctx
->ac
.builder
);
4775 fprintf(stderr
, "Unknown NIR jump instr: ");
4776 nir_print_instr(&instr
->instr
, stderr
);
4777 fprintf(stderr
, "\n");
4782 static void visit_cf_list(struct ac_nir_context
*ctx
,
4783 struct exec_list
*list
);
4785 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4787 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4788 nir_foreach_instr(instr
, block
)
4790 switch (instr
->type
) {
4791 case nir_instr_type_alu
:
4792 visit_alu(ctx
, nir_instr_as_alu(instr
));
4794 case nir_instr_type_load_const
:
4795 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4797 case nir_instr_type_intrinsic
:
4798 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4800 case nir_instr_type_tex
:
4801 visit_tex(ctx
, nir_instr_as_tex(instr
));
4803 case nir_instr_type_phi
:
4804 visit_phi(ctx
, nir_instr_as_phi(instr
));
4806 case nir_instr_type_ssa_undef
:
4807 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4809 case nir_instr_type_jump
:
4810 visit_jump(ctx
, nir_instr_as_jump(instr
));
4813 fprintf(stderr
, "Unknown NIR instr type: ");
4814 nir_print_instr(instr
, stderr
);
4815 fprintf(stderr
, "\n");
4820 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4823 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4825 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4827 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4828 LLVMBasicBlockRef merge_block
=
4829 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4830 LLVMBasicBlockRef if_block
=
4831 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4832 LLVMBasicBlockRef else_block
= merge_block
;
4833 if (!exec_list_is_empty(&if_stmt
->else_list
))
4834 else_block
= LLVMAppendBasicBlockInContext(
4835 ctx
->ac
.context
, fn
, "");
4837 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
4838 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4839 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
4841 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
4842 visit_cf_list(ctx
, &if_stmt
->then_list
);
4843 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4844 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4846 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4847 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
4848 visit_cf_list(ctx
, &if_stmt
->else_list
);
4849 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4850 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
4853 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
4856 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4858 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
4859 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4860 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4862 ctx
->continue_block
=
4863 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4865 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
4867 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4868 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
4869 visit_cf_list(ctx
, &loop
->body
);
4871 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
4872 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
4873 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
4875 ctx
->continue_block
= continue_parent
;
4876 ctx
->break_block
= break_parent
;
4879 static void visit_cf_list(struct ac_nir_context
*ctx
,
4880 struct exec_list
*list
)
4882 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4884 switch (node
->type
) {
4885 case nir_cf_node_block
:
4886 visit_block(ctx
, nir_cf_node_as_block(node
));
4889 case nir_cf_node_if
:
4890 visit_if(ctx
, nir_cf_node_as_if(node
));
4893 case nir_cf_node_loop
:
4894 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4904 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4905 struct nir_variable
*variable
)
4907 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4908 LLVMValueRef t_offset
;
4909 LLVMValueRef t_list
;
4911 LLVMValueRef buffer_index
;
4912 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4913 int idx
= variable
->data
.location
;
4914 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4916 variable
->data
.driver_location
= idx
* 4;
4918 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4919 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
4920 ctx
->abi
.start_instance
, "");
4921 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4922 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4924 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
4925 ctx
->abi
.base_vertex
, "");
4927 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4928 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4930 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4932 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
4934 LLVMConstInt(ctx
->i32
, 0, false),
4937 for (unsigned chan
= 0; chan
< 4; chan
++) {
4938 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4939 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4940 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
4941 input
, llvm_chan
, ""));
4946 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4948 LLVMValueRef interp_param
,
4949 LLVMValueRef prim_mask
,
4950 LLVMValueRef result
[4])
4952 LLVMValueRef attr_number
;
4955 bool interp
= interp_param
!= NULL
;
4957 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4959 /* fs.constant returns the param from the middle vertex, so it's not
4960 * really useful for flat shading. It's meant to be used for custom
4961 * interpolation (but the intrinsic can't fetch from the other two
4964 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4965 * to do the right thing. The only reason we use fs.constant is that
4966 * fs.interp cannot be used on integers, because they can be equal
4970 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4971 LLVMVectorType(ctx
->f32
, 2), "");
4973 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4975 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4979 for (chan
= 0; chan
< 4; chan
++) {
4980 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4983 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4988 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4989 LLVMConstInt(ctx
->i32
, 2, false),
4998 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4999 struct nir_variable
*variable
)
5001 int idx
= variable
->data
.location
;
5002 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5003 LLVMValueRef interp
;
5005 variable
->data
.driver_location
= idx
* 4;
5006 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5008 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5009 unsigned interp_type
;
5010 if (variable
->data
.sample
) {
5011 interp_type
= INTERP_SAMPLE
;
5012 ctx
->shader_info
->info
.ps
.force_persample
= true;
5013 } else if (variable
->data
.centroid
)
5014 interp_type
= INTERP_CENTROID
;
5016 interp_type
= INTERP_CENTER
;
5018 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5022 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5023 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5028 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5029 struct nir_shader
*nir
) {
5030 nir_foreach_variable(variable
, &nir
->inputs
)
5031 handle_vs_input_decl(ctx
, variable
);
5035 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5036 struct nir_shader
*nir
)
5038 if (!ctx
->options
->key
.fs
.multisample
)
5041 bool uses_center
= false;
5042 bool uses_centroid
= false;
5043 nir_foreach_variable(variable
, &nir
->inputs
) {
5044 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5045 variable
->data
.sample
)
5048 if (variable
->data
.centroid
)
5049 uses_centroid
= true;
5054 if (uses_center
&& uses_centroid
) {
5055 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5056 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5057 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5062 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5063 struct nir_shader
*nir
)
5065 prepare_interp_optimize(ctx
, nir
);
5067 nir_foreach_variable(variable
, &nir
->inputs
)
5068 handle_fs_input_decl(ctx
, variable
);
5072 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5073 ctx
->shader_info
->info
.needs_multiview_view_index
)
5074 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5076 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5077 LLVMValueRef interp_param
;
5078 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5080 if (!(ctx
->input_mask
& (1ull << i
)))
5083 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5084 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5085 interp_param
= *inputs
;
5086 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5090 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5092 } else if (i
== VARYING_SLOT_POS
) {
5093 for(int i
= 0; i
< 3; ++i
)
5094 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5096 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
,
5097 ctx
->abi
.frag_pos
[3]);
5100 ctx
->shader_info
->fs
.num_interp
= index
;
5101 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5102 ctx
->shader_info
->fs
.has_pcoord
= true;
5103 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5104 ctx
->shader_info
->fs
.prim_id_input
= true;
5105 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5106 ctx
->shader_info
->fs
.layer_input
= true;
5107 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5109 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5110 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5114 ac_build_alloca(struct ac_llvm_context
*ac
,
5118 LLVMBuilderRef builder
= ac
->builder
;
5119 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5120 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5121 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5122 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5123 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5127 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5129 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5132 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5133 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5135 LLVMDisposeBuilder(first_builder
);
5140 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5144 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5145 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5150 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5151 struct nir_variable
*variable
)
5153 int idx
= variable
->data
.location
+ variable
->data
.index
;
5154 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5155 uint64_t mask_attribs
;
5157 variable
->data
.driver_location
= idx
* 4;
5159 /* tess ctrl has it's own load/store paths for outputs */
5160 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5163 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5164 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5165 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5166 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5167 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5168 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5169 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
5170 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
5171 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
5173 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5174 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
5175 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
5182 mask_attribs
= 1ull << idx
;
5186 ctx
->output_mask
|= mask_attribs
;
5190 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5191 struct nir_shader
*nir
,
5192 struct nir_variable
*variable
)
5194 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5195 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5197 /* tess ctrl has it's own load/store paths for outputs */
5198 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5201 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5202 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5203 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5204 int idx
= variable
->data
.location
+ variable
->data
.index
;
5205 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5206 int length
= nir
->info
.clip_distance_array_size
+
5207 nir
->info
.cull_distance_array_size
;
5216 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5217 for (unsigned chan
= 0; chan
< 4; chan
++) {
5218 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5219 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5225 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5226 enum glsl_base_type type
)
5230 case GLSL_TYPE_UINT
:
5231 case GLSL_TYPE_BOOL
:
5232 case GLSL_TYPE_SUBROUTINE
:
5234 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5236 case GLSL_TYPE_INT64
:
5237 case GLSL_TYPE_UINT64
:
5239 case GLSL_TYPE_DOUBLE
:
5242 unreachable("unknown GLSL type");
5247 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5248 const struct glsl_type
*type
)
5250 if (glsl_type_is_scalar(type
)) {
5251 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5254 if (glsl_type_is_vector(type
)) {
5255 return LLVMVectorType(
5256 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5257 glsl_get_vector_elements(type
));
5260 if (glsl_type_is_matrix(type
)) {
5261 return LLVMArrayType(
5262 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5263 glsl_get_matrix_columns(type
));
5266 if (glsl_type_is_array(type
)) {
5267 return LLVMArrayType(
5268 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5269 glsl_get_length(type
));
5272 assert(glsl_type_is_struct(type
));
5274 LLVMTypeRef member_types
[glsl_get_length(type
)];
5276 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5278 glsl_to_llvm_type(ctx
,
5279 glsl_get_struct_field(type
, i
));
5282 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5283 glsl_get_length(type
), false);
5287 setup_locals(struct ac_nir_context
*ctx
,
5288 struct nir_function
*func
)
5291 ctx
->num_locals
= 0;
5292 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5293 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5294 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5295 ctx
->num_locals
+= attrib_count
;
5297 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5301 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5302 for (j
= 0; j
< 4; j
++) {
5303 ctx
->locals
[i
* 4 + j
] =
5304 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5310 setup_shared(struct ac_nir_context
*ctx
,
5311 struct nir_shader
*nir
)
5313 nir_foreach_variable(variable
, &nir
->shared
) {
5314 LLVMValueRef shared
=
5315 LLVMAddGlobalInAddressSpace(
5316 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5317 variable
->name
? variable
->name
: "",
5319 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5324 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5326 v
= ac_to_float(ctx
, v
);
5327 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5328 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5332 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5333 LLVMValueRef src0
, LLVMValueRef src1
)
5335 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
5336 LLVMValueRef comp
[2];
5338 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5339 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5340 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5341 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5344 /* Initialize arguments for the shader export intrinsic */
5346 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5347 LLVMValueRef
*values
,
5349 struct ac_export_args
*args
)
5351 /* Default is 0xf. Adjusted below depending on the format. */
5352 args
->enabled_channels
= 0xf;
5354 /* Specify whether the EXEC mask represents the valid mask */
5355 args
->valid_mask
= 0;
5357 /* Specify whether this is the last export */
5360 /* Specify the target we are exporting */
5361 args
->target
= target
;
5363 args
->compr
= false;
5364 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
5365 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
5366 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
5367 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
5372 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5373 LLVMValueRef val
[4];
5374 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5375 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5376 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5377 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5379 switch(col_format
) {
5380 case V_028714_SPI_SHADER_ZERO
:
5381 args
->enabled_channels
= 0; /* writemask */
5382 args
->target
= V_008DFC_SQ_EXP_NULL
;
5385 case V_028714_SPI_SHADER_32_R
:
5386 args
->enabled_channels
= 1;
5387 args
->out
[0] = values
[0];
5390 case V_028714_SPI_SHADER_32_GR
:
5391 args
->enabled_channels
= 0x3;
5392 args
->out
[0] = values
[0];
5393 args
->out
[1] = values
[1];
5396 case V_028714_SPI_SHADER_32_AR
:
5397 args
->enabled_channels
= 0x9;
5398 args
->out
[0] = values
[0];
5399 args
->out
[3] = values
[3];
5402 case V_028714_SPI_SHADER_FP16_ABGR
:
5405 for (unsigned chan
= 0; chan
< 2; chan
++) {
5406 LLVMValueRef pack_args
[2] = {
5408 values
[2 * chan
+ 1]
5410 LLVMValueRef packed
;
5412 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5413 args
->out
[chan
] = packed
;
5417 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5418 for (unsigned chan
= 0; chan
< 4; chan
++) {
5419 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5420 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5421 LLVMConstReal(ctx
->f32
, 65535), "");
5422 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5423 LLVMConstReal(ctx
->f32
, 0.5), "");
5424 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5429 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5430 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5433 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5434 for (unsigned chan
= 0; chan
< 4; chan
++) {
5435 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5436 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5437 LLVMConstReal(ctx
->f32
, 32767), "");
5439 /* If positive, add 0.5, else add -0.5. */
5440 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5441 LLVMBuildSelect(ctx
->builder
,
5442 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5443 val
[chan
], ctx
->f32zero
, ""),
5444 LLVMConstReal(ctx
->f32
, 0.5),
5445 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
5446 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
5450 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5451 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5454 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5455 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
5456 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5457 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
5459 for (unsigned chan
= 0; chan
< 4; chan
++) {
5460 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5461 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5465 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5466 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5470 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5471 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
5472 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5473 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
5474 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5475 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->i32one
;
5476 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
5479 for (unsigned chan
= 0; chan
< 4; chan
++) {
5480 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5481 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5482 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5486 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5487 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5492 case V_028714_SPI_SHADER_32_ABGR
:
5493 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5497 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5499 for (unsigned i
= 0; i
< 4; ++i
)
5500 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5504 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5505 bool export_prim_id
,
5506 struct ac_vs_output_info
*outinfo
)
5508 uint32_t param_count
= 0;
5510 unsigned pos_idx
, num_pos_exports
= 0;
5511 struct ac_export_args args
, pos_args
[4] = {};
5512 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5515 if (ctx
->options
->key
.has_multiview_view_index
) {
5516 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5518 for(unsigned i
= 0; i
< 4; ++i
)
5519 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5520 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5523 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5524 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5527 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5528 sizeof(outinfo
->vs_output_param_offset
));
5530 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5531 LLVMValueRef slots
[8];
5534 if (outinfo
->cull_dist_mask
)
5535 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5537 i
= VARYING_SLOT_CLIP_DIST0
;
5538 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5539 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5540 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5542 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5543 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5545 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5546 target
= V_008DFC_SQ_EXP_POS
+ 3;
5547 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5548 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5549 &args
, sizeof(args
));
5552 target
= V_008DFC_SQ_EXP_POS
+ 2;
5553 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5554 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5555 &args
, sizeof(args
));
5559 LLVMValueRef pos_values
[4] = {ctx
->f32zero
, ctx
->f32zero
, ctx
->f32zero
, ctx
->f32one
};
5560 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
5561 for (unsigned j
= 0; j
< 4; j
++)
5562 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
5563 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
5565 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
5567 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
5568 outinfo
->writes_pointsize
= true;
5569 psize_value
= LLVMBuildLoad(ctx
->builder
,
5570 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
5573 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
5574 outinfo
->writes_layer
= true;
5575 layer_value
= LLVMBuildLoad(ctx
->builder
,
5576 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
5579 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
5580 outinfo
->writes_viewport_index
= true;
5581 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
5582 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
5585 if (outinfo
->writes_pointsize
||
5586 outinfo
->writes_layer
||
5587 outinfo
->writes_viewport_index
) {
5588 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5589 (outinfo
->writes_layer
== true ? 4 : 0));
5590 pos_args
[1].valid_mask
= 0;
5591 pos_args
[1].done
= 0;
5592 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5593 pos_args
[1].compr
= 0;
5594 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
5595 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
5596 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
5597 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
5599 if (outinfo
->writes_pointsize
== true)
5600 pos_args
[1].out
[0] = psize_value
;
5601 if (outinfo
->writes_layer
== true)
5602 pos_args
[1].out
[2] = layer_value
;
5603 if (outinfo
->writes_viewport_index
== true) {
5604 if (ctx
->options
->chip_class
>= GFX9
) {
5605 /* GFX9 has the layer in out.z[10:0] and the viewport
5606 * index in out.z[19:16].
5608 LLVMValueRef v
= viewport_index_value
;
5609 v
= ac_to_integer(&ctx
->ac
, v
);
5610 v
= LLVMBuildShl(ctx
->builder
, v
,
5611 LLVMConstInt(ctx
->i32
, 16, false),
5613 v
= LLVMBuildOr(ctx
->builder
, v
,
5614 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
5616 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
5617 pos_args
[1].enabled_channels
|= 1 << 2;
5619 pos_args
[1].out
[3] = viewport_index_value
;
5620 pos_args
[1].enabled_channels
|= 1 << 3;
5624 for (i
= 0; i
< 4; i
++) {
5625 if (pos_args
[i
].out
[0])
5630 for (i
= 0; i
< 4; i
++) {
5631 if (!pos_args
[i
].out
[0])
5634 /* Specify the target we are exporting */
5635 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5636 if (pos_idx
== num_pos_exports
)
5637 pos_args
[i
].done
= 1;
5638 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5641 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5642 LLVMValueRef values
[4];
5643 if (!(ctx
->output_mask
& (1ull << i
)))
5646 for (unsigned j
= 0; j
< 4; j
++)
5647 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5648 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5650 if (i
== VARYING_SLOT_LAYER
) {
5651 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5652 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5654 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5655 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5656 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5658 } else if (i
>= VARYING_SLOT_VAR0
) {
5659 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5660 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5661 outinfo
->vs_output_param_offset
[i
] = param_count
;
5666 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5668 if (target
>= V_008DFC_SQ_EXP_POS
&&
5669 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5670 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5671 &args
, sizeof(args
));
5673 ac_build_export(&ctx
->ac
, &args
);
5677 if (export_prim_id
) {
5678 LLVMValueRef values
[4];
5679 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5680 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5683 values
[0] = ctx
->vs_prim_id
;
5684 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5685 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5686 for (unsigned j
= 1; j
< 4; j
++)
5687 values
[j
] = ctx
->f32zero
;
5688 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5689 ac_build_export(&ctx
->ac
, &args
);
5690 outinfo
->export_prim_id
= true;
5693 outinfo
->pos_exports
= num_pos_exports
;
5694 outinfo
->param_exports
= param_count
;
5698 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5699 struct ac_es_output_info
*outinfo
)
5702 uint64_t max_output_written
= 0;
5703 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5704 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5708 if (!(ctx
->output_mask
& (1ull << i
)))
5711 if (i
== VARYING_SLOT_CLIP_DIST0
)
5712 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5714 param_index
= shader_io_get_unique_index(i
);
5716 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5718 for (j
= 0; j
< length
; j
++) {
5719 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5720 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
5722 ac_build_buffer_store_dword(&ctx
->ac
,
5725 NULL
, ctx
->es2gs_offset
,
5726 (4 * param_index
+ j
) * 4,
5730 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5734 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5736 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5737 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
5738 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5739 vertex_dw_stride
, "");
5741 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5742 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
5745 if (!(ctx
->output_mask
& (1ull << i
)))
5748 if (i
== VARYING_SLOT_CLIP_DIST0
)
5749 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5750 int param
= shader_io_get_unique_index(i
);
5751 mark_tess_output(ctx
, false, param
);
5753 mark_tess_output(ctx
, false, param
+ 1);
5754 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5755 LLVMConstInt(ctx
->i32
, param
* 4, false),
5757 for (unsigned j
= 0; j
< length
; j
++) {
5758 lds_store(ctx
, dw_addr
,
5759 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5760 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->i32one
, "");
5765 struct ac_build_if_state
5767 struct nir_to_llvm_context
*ctx
;
5768 LLVMValueRef condition
;
5769 LLVMBasicBlockRef entry_block
;
5770 LLVMBasicBlockRef true_block
;
5771 LLVMBasicBlockRef false_block
;
5772 LLVMBasicBlockRef merge_block
;
5775 static LLVMBasicBlockRef
5776 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5778 LLVMBasicBlockRef current_block
;
5779 LLVMBasicBlockRef next_block
;
5780 LLVMBasicBlockRef new_block
;
5782 /* get current basic block */
5783 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5785 /* chqeck if there's another block after this one */
5786 next_block
= LLVMGetNextBasicBlock(current_block
);
5788 /* insert the new block before the next block */
5789 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5792 /* append new block after current block */
5793 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5794 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5800 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5801 struct nir_to_llvm_context
*ctx
,
5802 LLVMValueRef condition
)
5804 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5806 memset(ifthen
, 0, sizeof *ifthen
);
5808 ifthen
->condition
= condition
;
5809 ifthen
->entry_block
= block
;
5811 /* create endif/merge basic block for the phi functions */
5812 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5814 /* create/insert true_block before merge_block */
5815 ifthen
->true_block
=
5816 LLVMInsertBasicBlockInContext(ctx
->context
,
5817 ifthen
->merge_block
,
5820 /* successive code goes into the true block */
5821 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5825 * End a conditional.
5828 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5830 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5832 /* Insert branch to the merge block from current block */
5833 LLVMBuildBr(builder
, ifthen
->merge_block
);
5836 * Now patch in the various branch instructions.
5839 /* Insert the conditional branch instruction at the end of entry_block */
5840 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5841 if (ifthen
->false_block
) {
5842 /* we have an else clause */
5843 LLVMBuildCondBr(builder
, ifthen
->condition
,
5844 ifthen
->true_block
, ifthen
->false_block
);
5847 /* no else clause */
5848 LLVMBuildCondBr(builder
, ifthen
->condition
,
5849 ifthen
->true_block
, ifthen
->merge_block
);
5852 /* Resume building code at end of the ifthen->merge_block */
5853 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5857 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5859 unsigned stride
, outer_comps
, inner_comps
;
5860 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5861 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 8, 5);
5862 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->tcs_rel_ids
, 0, 8);
5863 unsigned tess_inner_index
, tess_outer_index
;
5864 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
5865 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
5869 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
5889 ac_nir_build_if(&if_ctx
, ctx
,
5890 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5891 invocation_id
, ctx
->i32zero
, ""));
5893 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5894 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5896 mark_tess_output(ctx
, true, tess_inner_index
);
5897 mark_tess_output(ctx
, true, tess_outer_index
);
5898 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
5899 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5900 LLVMConstInt(ctx
->i32
, tess_inner_index
* 4, false), "");
5901 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5902 LLVMConstInt(ctx
->i32
, tess_outer_index
* 4, false), "");
5904 for (i
= 0; i
< 4; i
++) {
5905 inner
[i
] = LLVMGetUndef(ctx
->i32
);
5906 outer
[i
] = LLVMGetUndef(ctx
->i32
);
5910 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
5911 outer
[0] = out
[1] = lds_load(ctx
, lds_outer
);
5912 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5913 LLVMConstInt(ctx
->i32
, 1, false), "");
5914 outer
[1] = out
[0] = lds_load(ctx
, lds_outer
);
5916 for (i
= 0; i
< outer_comps
; i
++) {
5918 lds_load(ctx
, lds_outer
);
5919 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5920 LLVMConstInt(ctx
->i32
, 1, false), "");
5922 for (i
= 0; i
< inner_comps
; i
++) {
5923 inner
[i
] = out
[outer_comps
+i
] =
5924 lds_load(ctx
, lds_inner
);
5925 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
5926 LLVMConstInt(ctx
->i32
, 1, false), "");
5930 /* Convert the outputs to vectors for stores. */
5931 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
5935 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
5938 buffer
= ctx
->hs_ring_tess_factor
;
5939 tf_base
= ctx
->tess_factor_offset
;
5940 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
5941 LLVMConstInt(ctx
->i32
, 4 * stride
, false), "");
5943 ac_nir_build_if(&inner_if_ctx
, ctx
,
5944 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5945 rel_patch_id
, ctx
->i32zero
, ""));
5947 /* Store the dynamic HS control word. */
5948 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
5949 LLVMConstInt(ctx
->i32
, 0x80000000, false),
5950 1, ctx
->i32zero
, tf_base
,
5951 0, 1, 0, true, false);
5952 ac_nir_build_endif(&inner_if_ctx
);
5954 /* Store the tessellation factors. */
5955 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
5956 MIN2(stride
, 4), byteoffset
, tf_base
,
5957 4, 1, 0, true, false);
5959 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
5960 stride
- 4, byteoffset
, tf_base
,
5961 20, 1, 0, true, false);
5963 //TODO store to offchip for TES to read - only if TES reads them
5965 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
5966 LLVMValueRef tf_inner_offset
;
5967 unsigned param_outer
, param_inner
;
5969 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5970 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5971 LLVMConstInt(ctx
->i32
, param_outer
, 0));
5973 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
5974 util_next_power_of_two(outer_comps
));
5976 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
5977 outer_comps
, tf_outer_offset
,
5978 ctx
->oc_lds
, 0, 1, 0, true, false);
5980 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5981 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5982 LLVMConstInt(ctx
->i32
, param_inner
, 0));
5984 inner_vec
= inner_comps
== 1 ? inner
[0] :
5985 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
5986 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
5987 inner_comps
, tf_inner_offset
,
5988 ctx
->oc_lds
, 0, 1, 0, true, false);
5991 ac_nir_build_endif(&if_ctx
);
5995 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
5997 write_tess_factors(ctx
);
6001 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6002 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6003 struct ac_export_args
*args
)
6006 si_llvm_init_export_args(ctx
, color
, param
,
6010 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6011 args
->done
= 1; /* DONE bit */
6012 } else if (!args
->enabled_channels
)
6013 return false; /* unnecessary NULL export */
6019 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6020 LLVMValueRef depth
, LLVMValueRef stencil
,
6021 LLVMValueRef samplemask
)
6023 struct ac_export_args args
;
6025 args
.enabled_channels
= 0;
6026 args
.valid_mask
= 1;
6028 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
6031 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
6032 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
6033 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
6034 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
6037 args
.out
[0] = depth
;
6038 args
.enabled_channels
|= 0x1;
6042 args
.out
[1] = stencil
;
6043 args
.enabled_channels
|= 0x2;
6047 args
.out
[2] = samplemask
;
6048 args
.enabled_channels
|= 0x4;
6051 /* SI (except OLAND and HAINAN) has a bug that it only looks
6052 * at the X writemask component. */
6053 if (ctx
->options
->chip_class
== SI
&&
6054 ctx
->options
->family
!= CHIP_OLAND
&&
6055 ctx
->options
->family
!= CHIP_HAINAN
)
6056 args
.enabled_channels
|= 0x1;
6058 ac_build_export(&ctx
->ac
, &args
);
6062 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6065 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6066 struct ac_export_args color_args
[8];
6068 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6069 LLVMValueRef values
[4];
6071 if (!(ctx
->output_mask
& (1ull << i
)))
6074 if (i
== FRAG_RESULT_DEPTH
) {
6075 ctx
->shader_info
->fs
.writes_z
= true;
6076 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6077 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6078 } else if (i
== FRAG_RESULT_STENCIL
) {
6079 ctx
->shader_info
->fs
.writes_stencil
= true;
6080 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6081 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6082 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6083 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6084 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6085 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6088 for (unsigned j
= 0; j
< 4; j
++)
6089 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6090 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6092 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6093 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6095 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6101 for (unsigned i
= 0; i
< index
; i
++)
6102 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6103 if (depth
|| stencil
|| samplemask
)
6104 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6106 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6107 ac_build_export(&ctx
->ac
, &color_args
[0]);
6110 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
6114 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6116 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6120 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6121 LLVMValueRef
*addrs
)
6123 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6125 switch (ctx
->stage
) {
6126 case MESA_SHADER_VERTEX
:
6127 if (ctx
->options
->key
.vs
.as_ls
)
6128 handle_ls_outputs_post(ctx
);
6129 else if (ctx
->options
->key
.vs
.as_es
)
6130 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6132 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6133 &ctx
->shader_info
->vs
.outinfo
);
6135 case MESA_SHADER_FRAGMENT
:
6136 handle_fs_outputs_post(ctx
);
6138 case MESA_SHADER_GEOMETRY
:
6139 emit_gs_epilogue(ctx
);
6141 case MESA_SHADER_TESS_CTRL
:
6142 handle_tcs_outputs_post(ctx
);
6144 case MESA_SHADER_TESS_EVAL
:
6145 if (ctx
->options
->key
.tes
.as_es
)
6146 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6148 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6149 &ctx
->shader_info
->tes
.outinfo
);
6156 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6158 LLVMPassManagerRef passmgr
;
6159 /* Create the pass manager */
6160 passmgr
= LLVMCreateFunctionPassManagerForModule(
6163 /* This pass should eliminate all the load and store instructions */
6164 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6166 /* Add some optimization passes */
6167 LLVMAddScalarReplAggregatesPass(passmgr
);
6168 LLVMAddLICMPass(passmgr
);
6169 LLVMAddAggressiveDCEPass(passmgr
);
6170 LLVMAddCFGSimplificationPass(passmgr
);
6171 LLVMAddInstructionCombiningPass(passmgr
);
6174 LLVMInitializeFunctionPassManager(passmgr
);
6175 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6176 LLVMFinalizeFunctionPassManager(passmgr
);
6178 LLVMDisposeBuilder(ctx
->builder
);
6179 LLVMDisposePassManager(passmgr
);
6183 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6185 struct ac_vs_output_info
*outinfo
;
6187 switch (ctx
->stage
) {
6188 case MESA_SHADER_FRAGMENT
:
6189 case MESA_SHADER_COMPUTE
:
6190 case MESA_SHADER_TESS_CTRL
:
6191 case MESA_SHADER_GEOMETRY
:
6193 case MESA_SHADER_VERTEX
:
6194 if (ctx
->options
->key
.vs
.as_ls
||
6195 ctx
->options
->key
.vs
.as_es
)
6197 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6199 case MESA_SHADER_TESS_EVAL
:
6200 if (ctx
->options
->key
.vs
.as_es
)
6202 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6205 unreachable("Unhandled shader type");
6208 ac_optimize_vs_outputs(&ctx
->ac
,
6210 outinfo
->vs_output_param_offset
,
6212 &outinfo
->param_exports
);
6216 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6218 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6219 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6220 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
6223 if (ctx
->is_gs_copy_shader
) {
6224 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
6226 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6228 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
6229 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
6231 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
6233 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
6234 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
6235 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6236 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
6239 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6240 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6241 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
6242 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
6247 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6248 const struct nir_shader
*nir
)
6250 switch (nir
->stage
) {
6251 case MESA_SHADER_TESS_CTRL
:
6252 return chip_class
>= CIK
? 128 : 64;
6253 case MESA_SHADER_GEOMETRY
:
6255 case MESA_SHADER_COMPUTE
:
6261 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6262 nir
->info
.cs
.local_size
[1] *
6263 nir
->info
.cs
.local_size
[2];
6264 return max_workgroup_size
;
6267 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6268 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6270 struct ac_nir_context ctx
= {};
6271 struct nir_function
*func
;
6280 ctx
.stage
= nir
->stage
;
6282 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6284 nir_foreach_variable(variable
, &nir
->outputs
)
6285 handle_shader_output_decl(&ctx
, nir
, variable
);
6287 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6288 _mesa_key_pointer_equal
);
6289 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6290 _mesa_key_pointer_equal
);
6291 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6292 _mesa_key_pointer_equal
);
6294 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6296 setup_locals(&ctx
, func
);
6298 if (nir
->stage
== MESA_SHADER_COMPUTE
)
6299 setup_shared(&ctx
, nir
);
6301 visit_cf_list(&ctx
, &func
->impl
->body
);
6302 phi_post_pass(&ctx
);
6304 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6308 ralloc_free(ctx
.defs
);
6309 ralloc_free(ctx
.phis
);
6310 ralloc_free(ctx
.vars
);
6317 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6318 struct nir_shader
*nir
,
6319 struct ac_shader_variant_info
*shader_info
,
6320 const struct ac_nir_compiler_options
*options
)
6322 struct nir_to_llvm_context ctx
= {0};
6324 ctx
.options
= options
;
6325 ctx
.shader_info
= shader_info
;
6326 ctx
.context
= LLVMContextCreate();
6327 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6329 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
);
6330 ctx
.ac
.module
= ctx
.module
;
6332 memset(shader_info
, 0, sizeof(*shader_info
));
6334 ac_nir_shader_info_pass(nir
, options
, &shader_info
->info
);
6336 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6338 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6339 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6340 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6341 LLVMDisposeTargetData(data_layout
);
6342 LLVMDisposeMessage(data_layout_str
);
6346 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6347 ctx
.ac
.builder
= ctx
.builder
;
6348 ctx
.stage
= nir
->stage
;
6349 ctx
.max_workgroup_size
= ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
, nir
);
6351 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6352 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6353 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6354 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6356 create_function(&ctx
);
6358 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
6359 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.i32
, "gs_next_vertex");
6361 ctx
.gs_max_out_vertices
= nir
->info
.gs
.vertices_out
;
6362 } else if (nir
->stage
== MESA_SHADER_TESS_EVAL
) {
6363 ctx
.tes_primitive_mode
= nir
->info
.tess
.primitive_mode
;
6364 } else if (nir
->stage
== MESA_SHADER_VERTEX
) {
6365 if (shader_info
->info
.vs
.needs_instance_id
) {
6366 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6367 MAX2(3, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6369 } else if (nir
->stage
== MESA_SHADER_FRAGMENT
) {
6370 shader_info
->fs
.can_discard
= nir
->info
.fs
.uses_discard
;
6373 ac_setup_rings(&ctx
);
6375 ctx
.num_output_clips
= nir
->info
.clip_distance_array_size
;
6376 ctx
.num_output_culls
= nir
->info
.cull_distance_array_size
;
6378 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
6379 handle_fs_inputs(&ctx
, nir
);
6380 else if(nir
->stage
== MESA_SHADER_VERTEX
)
6381 handle_vs_inputs(&ctx
, nir
);
6383 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6384 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6385 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6386 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6388 nir_foreach_variable(variable
, &nir
->outputs
)
6389 scan_shader_output_decl(&ctx
, variable
);
6391 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, nir
, &ctx
);
6393 LLVMBuildRetVoid(ctx
.builder
);
6395 ac_llvm_finalize_module(&ctx
);
6397 ac_nir_eliminate_const_vs_outputs(&ctx
);
6399 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
6400 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
6401 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6402 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6403 nir
->info
.gs
.vertices_out
;
6404 } else if (nir
->stage
== MESA_SHADER_TESS_CTRL
) {
6405 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6406 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6407 } else if (nir
->stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6408 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6414 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6416 unsigned *retval
= (unsigned *)context
;
6417 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6418 char *description
= LLVMGetDiagInfoDescription(di
);
6420 if (severity
== LLVMDSError
) {
6422 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6426 LLVMDisposeMessage(description
);
6429 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6430 struct ac_shader_binary
*binary
,
6431 LLVMTargetMachineRef tm
)
6433 unsigned retval
= 0;
6435 LLVMContextRef llvm_ctx
;
6436 LLVMMemoryBufferRef out_buffer
;
6437 unsigned buffer_size
;
6438 const char *buffer_data
;
6441 /* Setup Diagnostic Handler*/
6442 llvm_ctx
= LLVMGetModuleContext(M
);
6444 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6448 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6451 /* Process Errors/Warnings */
6453 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6459 /* Extract Shader Code*/
6460 buffer_size
= LLVMGetBufferSize(out_buffer
);
6461 buffer_data
= LLVMGetBufferStart(out_buffer
);
6463 ac_elf_read(buffer_data
, buffer_size
, binary
);
6466 LLVMDisposeMemoryBuffer(out_buffer
);
6472 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6473 LLVMModuleRef llvm_module
,
6474 struct ac_shader_binary
*binary
,
6475 struct ac_shader_config
*config
,
6476 struct ac_shader_variant_info
*shader_info
,
6477 gl_shader_stage stage
,
6478 bool dump_shader
, bool supports_spill
)
6481 ac_dump_module(llvm_module
);
6483 memset(binary
, 0, sizeof(*binary
));
6484 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6486 fprintf(stderr
, "compile failed\n");
6490 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6492 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6494 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6495 LLVMDisposeModule(llvm_module
);
6496 LLVMContextDispose(ctx
);
6498 if (stage
== MESA_SHADER_FRAGMENT
) {
6499 shader_info
->num_input_vgprs
= 0;
6500 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6501 shader_info
->num_input_vgprs
+= 2;
6502 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6503 shader_info
->num_input_vgprs
+= 2;
6504 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6505 shader_info
->num_input_vgprs
+= 2;
6506 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6507 shader_info
->num_input_vgprs
+= 3;
6508 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6509 shader_info
->num_input_vgprs
+= 2;
6510 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6511 shader_info
->num_input_vgprs
+= 2;
6512 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6513 shader_info
->num_input_vgprs
+= 2;
6514 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6515 shader_info
->num_input_vgprs
+= 1;
6516 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6517 shader_info
->num_input_vgprs
+= 1;
6518 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6519 shader_info
->num_input_vgprs
+= 1;
6520 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6521 shader_info
->num_input_vgprs
+= 1;
6522 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6523 shader_info
->num_input_vgprs
+= 1;
6524 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6525 shader_info
->num_input_vgprs
+= 1;
6526 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6527 shader_info
->num_input_vgprs
+= 1;
6528 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6529 shader_info
->num_input_vgprs
+= 1;
6530 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6531 shader_info
->num_input_vgprs
+= 1;
6533 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6535 /* +3 for scratch wave offset and VCC */
6536 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6537 shader_info
->num_input_sgprs
+ 3);
6540 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6541 struct ac_shader_binary
*binary
,
6542 struct ac_shader_config
*config
,
6543 struct ac_shader_variant_info
*shader_info
,
6544 struct nir_shader
*nir
,
6545 const struct ac_nir_compiler_options
*options
,
6549 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
6552 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
6553 switch (nir
->stage
) {
6554 case MESA_SHADER_COMPUTE
:
6555 for (int i
= 0; i
< 3; ++i
)
6556 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6558 case MESA_SHADER_FRAGMENT
:
6559 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6561 case MESA_SHADER_GEOMETRY
:
6562 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6563 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6564 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6565 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6567 case MESA_SHADER_TESS_EVAL
:
6568 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6569 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6570 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6571 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6572 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6574 case MESA_SHADER_TESS_CTRL
:
6575 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6577 case MESA_SHADER_VERTEX
:
6578 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6579 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6580 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6581 if (options
->key
.vs
.as_ls
)
6582 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6590 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6592 LLVMValueRef args
[9];
6593 args
[0] = ctx
->gsvs_ring
;
6594 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
6595 args
[3] = ctx
->i32zero
;
6596 args
[4] = ctx
->i32one
; /* OFFEN */
6597 args
[5] = ctx
->i32zero
; /* IDXEN */
6598 args
[6] = ctx
->i32one
; /* GLC */
6599 args
[7] = ctx
->i32one
; /* SLC */
6600 args
[8] = ctx
->i32zero
; /* TFE */
6604 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6608 if (!(ctx
->output_mask
& (1ull << i
)))
6611 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6612 /* unpack clip and cull from a single set of slots */
6613 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6618 for (unsigned j
= 0; j
< length
; j
++) {
6620 args
[2] = LLVMConstInt(ctx
->i32
,
6622 ctx
->gs_max_out_vertices
* 16 * 4, false);
6624 value
= ac_build_intrinsic(&ctx
->ac
,
6625 "llvm.SI.buffer.load.dword.i32.i32",
6627 AC_FUNC_ATTR_READONLY
|
6628 AC_FUNC_ATTR_LEGACY
);
6630 LLVMBuildStore(ctx
->builder
,
6631 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6635 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
6638 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6639 struct nir_shader
*geom_shader
,
6640 struct ac_shader_binary
*binary
,
6641 struct ac_shader_config
*config
,
6642 struct ac_shader_variant_info
*shader_info
,
6643 const struct ac_nir_compiler_options
*options
,
6646 struct nir_to_llvm_context ctx
= {0};
6647 ctx
.context
= LLVMContextCreate();
6648 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6649 ctx
.options
= options
;
6650 ctx
.shader_info
= shader_info
;
6652 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
);
6653 ctx
.ac
.module
= ctx
.module
;
6655 ctx
.is_gs_copy_shader
= true;
6656 LLVMSetTarget(ctx
.module
, "amdgcn--");
6659 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6660 ctx
.ac
.builder
= ctx
.builder
;
6661 ctx
.stage
= MESA_SHADER_VERTEX
;
6663 create_function(&ctx
);
6665 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6666 ac_setup_rings(&ctx
);
6668 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6669 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6671 struct ac_nir_context nir_ctx
= {};
6672 nir_ctx
.ac
= ctx
.ac
;
6673 nir_ctx
.abi
= &ctx
.abi
;
6675 nir_ctx
.nctx
= &ctx
;
6678 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
6679 scan_shader_output_decl(&ctx
, variable
);
6680 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
6683 ac_gs_copy_shader_emit(&ctx
);
6687 LLVMBuildRetVoid(ctx
.builder
);
6689 ac_llvm_finalize_module(&ctx
);
6691 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6693 dump_shader
, options
->supports_spill
);