2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "../vulkan/radv_descriptor_set.h"
31 #include "util/bitscan.h"
32 #include <llvm-c/Transforms/Scalar.h>
33 #include "ac_shader_abi.h"
34 #include "ac_shader_info.h"
35 #include "ac_shader_util.h"
36 #include "ac_exp_param.h"
38 enum radeon_llvm_calling_convention
{
39 RADEON_LLVM_AMDGPU_VS
= 87,
40 RADEON_LLVM_AMDGPU_GS
= 88,
41 RADEON_LLVM_AMDGPU_PS
= 89,
42 RADEON_LLVM_AMDGPU_CS
= 90,
43 RADEON_LLVM_AMDGPU_HS
= 93,
46 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
47 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
49 struct nir_to_llvm_context
;
51 struct ac_nir_context
{
52 struct ac_llvm_context ac
;
53 struct ac_shader_abi
*abi
;
55 gl_shader_stage stage
;
57 struct hash_table
*defs
;
58 struct hash_table
*phis
;
59 struct hash_table
*vars
;
61 LLVMValueRef main_function
;
62 LLVMBasicBlockRef continue_block
;
63 LLVMBasicBlockRef break_block
;
65 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
70 struct nir_to_llvm_context
*nctx
; /* TODO get rid of this */
73 struct nir_to_llvm_context
{
74 struct ac_llvm_context ac
;
75 const struct ac_nir_compiler_options
*options
;
76 struct ac_shader_variant_info
*shader_info
;
77 struct ac_shader_abi abi
;
78 struct ac_nir_context
*nir
;
80 unsigned max_workgroup_size
;
81 LLVMContextRef context
;
83 LLVMBuilderRef builder
;
84 LLVMValueRef main_function
;
86 struct hash_table
*defs
;
87 struct hash_table
*phis
;
89 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
90 LLVMValueRef ring_offsets
;
91 LLVMValueRef push_constants
;
92 LLVMValueRef view_index
;
93 LLVMValueRef num_work_groups
;
94 LLVMValueRef workgroup_ids
[3];
95 LLVMValueRef local_invocation_ids
;
98 LLVMValueRef vertex_buffers
;
99 LLVMValueRef rel_auto_id
;
100 LLVMValueRef vs_prim_id
;
101 LLVMValueRef ls_out_layout
;
102 LLVMValueRef es2gs_offset
;
104 LLVMValueRef tcs_offchip_layout
;
105 LLVMValueRef tcs_out_offsets
;
106 LLVMValueRef tcs_out_layout
;
107 LLVMValueRef tcs_in_layout
;
109 LLVMValueRef merged_wave_info
;
110 LLVMValueRef tess_factor_offset
;
111 LLVMValueRef tes_rel_patch_id
;
115 LLVMValueRef gsvs_ring_stride
;
116 LLVMValueRef gsvs_num_entries
;
117 LLVMValueRef gs2vs_offset
;
118 LLVMValueRef gs_wave_id
;
119 LLVMValueRef gs_vtx_offset
[6];
121 LLVMValueRef esgs_ring
;
122 LLVMValueRef gsvs_ring
;
123 LLVMValueRef hs_ring_tess_offchip
;
124 LLVMValueRef hs_ring_tess_factor
;
126 LLVMValueRef prim_mask
;
127 LLVMValueRef sample_pos_offset
;
128 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
129 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
131 gl_shader_stage stage
;
133 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
136 uint64_t output_mask
;
137 uint8_t num_output_clips
;
138 uint8_t num_output_culls
;
140 bool is_gs_copy_shader
;
141 LLVMValueRef gs_next_vertex
;
142 unsigned gs_max_out_vertices
;
144 unsigned tes_primitive_mode
;
145 uint64_t tess_outputs_written
;
146 uint64_t tess_patch_outputs_written
;
148 uint32_t tcs_patch_outputs_read
;
149 uint64_t tcs_outputs_read
;
152 static inline struct nir_to_llvm_context
*
153 nir_to_llvm_context_from_abi(struct ac_shader_abi
*abi
)
155 struct nir_to_llvm_context
*ctx
= NULL
;
156 return container_of(abi
, ctx
, abi
);
160 nir2llvmtype(struct ac_nir_context
*ctx
,
161 const struct glsl_type
*type
)
163 switch (glsl_get_base_type(glsl_without_array(type
))) {
167 case GLSL_TYPE_UINT64
:
168 case GLSL_TYPE_INT64
:
170 case GLSL_TYPE_DOUBLE
:
172 case GLSL_TYPE_FLOAT
:
175 assert(!"Unsupported type in nir2llvmtype()");
181 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
182 const nir_deref_var
*deref
,
183 enum ac_descriptor_type desc_type
,
184 const nir_tex_instr
*instr
,
185 bool image
, bool write
);
187 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
189 return (index
* 4) + chan
;
192 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
194 /* handle patch indices separate */
195 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
197 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
199 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
200 return 2 + (slot
- VARYING_SLOT_PATCH0
);
202 if (slot
== VARYING_SLOT_POS
)
204 if (slot
== VARYING_SLOT_PSIZ
)
206 if (slot
== VARYING_SLOT_CLIP_DIST0
)
208 /* 3 is reserved for clip dist as well */
209 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
210 return 4 + (slot
- VARYING_SLOT_VAR0
);
211 unreachable("illegal slot in get unique index\n");
214 static void set_llvm_calling_convention(LLVMValueRef func
,
215 gl_shader_stage stage
)
217 enum radeon_llvm_calling_convention calling_conv
;
220 case MESA_SHADER_VERTEX
:
221 case MESA_SHADER_TESS_EVAL
:
222 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
224 case MESA_SHADER_GEOMETRY
:
225 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
227 case MESA_SHADER_TESS_CTRL
:
228 calling_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
: RADEON_LLVM_AMDGPU_VS
;
230 case MESA_SHADER_FRAGMENT
:
231 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
233 case MESA_SHADER_COMPUTE
:
234 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
237 unreachable("Unhandle shader type");
240 LLVMSetFunctionCallConv(func
, calling_conv
);
245 LLVMTypeRef types
[MAX_ARGS
];
246 LLVMValueRef
*assign
[MAX_ARGS
];
247 unsigned array_params_mask
;
250 uint8_t num_sgprs_used
;
251 uint8_t num_vgprs_used
;
254 enum ac_arg_regfile
{
260 add_arg(struct arg_info
*info
, enum ac_arg_regfile regfile
, LLVMTypeRef type
,
261 LLVMValueRef
*param_ptr
)
263 assert(info
->count
< MAX_ARGS
);
265 info
->assign
[info
->count
] = param_ptr
;
266 info
->types
[info
->count
] = type
;
269 if (regfile
== ARG_SGPR
) {
270 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
273 assert(regfile
== ARG_VGPR
);
274 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
279 add_array_arg(struct arg_info
*info
, LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
281 info
->array_params_mask
|= (1 << info
->count
);
282 add_arg(info
, ARG_SGPR
, type
, param_ptr
);
285 static void assign_arguments(LLVMValueRef main_function
,
286 struct arg_info
*info
)
289 for (i
= 0; i
< info
->count
; i
++) {
291 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
296 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
297 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
298 unsigned num_return_elems
,
299 struct arg_info
*args
,
300 unsigned max_workgroup_size
,
303 LLVMTypeRef main_function_type
, ret_type
;
304 LLVMBasicBlockRef main_function_body
;
306 if (num_return_elems
)
307 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
308 num_return_elems
, true);
310 ret_type
= LLVMVoidTypeInContext(ctx
);
312 /* Setup the function */
314 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
315 LLVMValueRef main_function
=
316 LLVMAddFunction(module
, "main", main_function_type
);
318 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
319 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
321 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
322 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
323 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
325 if (args
->array_params_mask
& (1 << i
)) {
326 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
327 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_NOALIAS
);
328 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
332 if (max_workgroup_size
) {
333 ac_llvm_add_target_dep_function_attr(main_function
,
334 "amdgpu-max-work-group-size",
338 /* These were copied from some LLVM test. */
339 LLVMAddTargetDependentFunctionAttr(main_function
,
340 "less-precise-fpmad",
342 LLVMAddTargetDependentFunctionAttr(main_function
,
345 LLVMAddTargetDependentFunctionAttr(main_function
,
348 LLVMAddTargetDependentFunctionAttr(main_function
,
351 LLVMAddTargetDependentFunctionAttr(main_function
,
352 "no-signed-zeros-fp-math",
355 return main_function
;
358 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
360 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
361 AC_CONST_ADDR_SPACE
);
364 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
366 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
367 type
= LLVMGetElementType(type
);
369 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
370 return LLVMGetIntTypeWidth(type
);
372 if (type
== ctx
->f16
)
374 if (type
== ctx
->f32
)
376 if (type
== ctx
->f64
)
379 unreachable("Unhandled type kind in get_elem_bits");
382 static LLVMValueRef
unpack_param(struct ac_llvm_context
*ctx
,
383 LLVMValueRef param
, unsigned rshift
,
386 LLVMValueRef value
= param
;
388 value
= LLVMBuildLShr(ctx
->builder
, value
,
389 LLVMConstInt(ctx
->i32
, rshift
, false), "");
391 if (rshift
+ bitwidth
< 32) {
392 unsigned mask
= (1 << bitwidth
) - 1;
393 value
= LLVMBuildAnd(ctx
->builder
, value
,
394 LLVMConstInt(ctx
->i32
, mask
, false), "");
399 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
401 switch (ctx
->stage
) {
402 case MESA_SHADER_TESS_CTRL
:
403 return unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
404 case MESA_SHADER_TESS_EVAL
:
405 return ctx
->tes_rel_patch_id
;
408 unreachable("Illegal stage");
412 /* Tessellation shaders pass outputs to the next shader using LDS.
414 * LS outputs = TCS inputs
415 * TCS outputs = TES inputs
418 * - TCS inputs for patch 0
419 * - TCS inputs for patch 1
420 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
422 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
423 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
424 * - TCS outputs for patch 1
425 * - Per-patch TCS outputs for patch 1
426 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
427 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
430 * All three shaders VS(LS), TCS, TES share the same LDS space.
433 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
435 if (ctx
->stage
== MESA_SHADER_VERTEX
)
436 return unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 0, 13);
437 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
438 return unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 0, 13);
446 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
448 return unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 0, 13);
452 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
454 return LLVMBuildMul(ctx
->builder
,
455 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 0, 16),
456 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
460 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
462 return LLVMBuildMul(ctx
->builder
,
463 unpack_param(&ctx
->ac
, ctx
->tcs_out_offsets
, 16, 16),
464 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
468 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
470 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
471 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
473 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
477 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
479 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
480 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
481 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
483 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
484 LLVMBuildMul(ctx
->builder
, patch_stride
,
490 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
492 LLVMValueRef patch0_patch_data_offset
=
493 get_tcs_out_patch0_patch_data_offset(ctx
);
494 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
495 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
497 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
498 LLVMBuildMul(ctx
->builder
, patch_stride
,
504 set_loc(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
,
505 uint32_t indirect_offset
)
507 ud_info
->sgpr_idx
= *sgpr_idx
;
508 ud_info
->num_sgprs
= num_sgprs
;
509 ud_info
->indirect
= indirect_offset
> 0;
510 ud_info
->indirect_offset
= indirect_offset
;
511 *sgpr_idx
+= num_sgprs
;
515 set_loc_shader(struct nir_to_llvm_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
518 struct ac_userdata_info
*ud_info
=
519 &ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
];
522 set_loc(ud_info
, sgpr_idx
, num_sgprs
, 0);
526 set_loc_desc(struct nir_to_llvm_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
527 uint32_t indirect_offset
)
529 struct ac_userdata_info
*ud_info
=
530 &ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[idx
];
533 set_loc(ud_info
, sgpr_idx
, 2, indirect_offset
);
536 struct user_sgpr_info
{
537 bool need_ring_offsets
;
539 bool indirect_all_descriptor_sets
;
542 static bool needs_view_index_sgpr(struct nir_to_llvm_context
*ctx
,
543 gl_shader_stage stage
)
546 case MESA_SHADER_VERTEX
:
547 if (ctx
->shader_info
->info
.needs_multiview_view_index
||
548 (!ctx
->options
->key
.vs
.as_es
&& !ctx
->options
->key
.vs
.as_ls
&& ctx
->options
->key
.has_multiview_view_index
))
551 case MESA_SHADER_TESS_EVAL
:
552 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
555 case MESA_SHADER_GEOMETRY
:
556 case MESA_SHADER_TESS_CTRL
:
557 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
566 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
567 gl_shader_stage stage
,
568 bool needs_view_index
,
569 struct user_sgpr_info
*user_sgpr_info
)
571 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
573 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
574 if (stage
== MESA_SHADER_GEOMETRY
||
575 stage
== MESA_SHADER_VERTEX
||
576 stage
== MESA_SHADER_TESS_CTRL
||
577 stage
== MESA_SHADER_TESS_EVAL
||
578 ctx
->is_gs_copy_shader
)
579 user_sgpr_info
->need_ring_offsets
= true;
581 if (stage
== MESA_SHADER_FRAGMENT
&&
582 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
583 user_sgpr_info
->need_ring_offsets
= true;
585 /* 2 user sgprs will nearly always be allocated for scratch/rings */
586 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
587 user_sgpr_info
->sgpr_count
+= 2;
590 /* FIXME: fix the number of user sgprs for merged shaders on GFX9 */
592 case MESA_SHADER_COMPUTE
:
593 if (ctx
->shader_info
->info
.cs
.uses_grid_size
)
594 user_sgpr_info
->sgpr_count
+= 3;
596 case MESA_SHADER_FRAGMENT
:
597 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
599 case MESA_SHADER_VERTEX
:
600 if (!ctx
->is_gs_copy_shader
) {
601 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
602 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
603 user_sgpr_info
->sgpr_count
+= 3;
605 user_sgpr_info
->sgpr_count
+= 2;
608 if (ctx
->options
->key
.vs
.as_ls
)
609 user_sgpr_info
->sgpr_count
++;
611 case MESA_SHADER_TESS_CTRL
:
612 user_sgpr_info
->sgpr_count
+= 4;
614 case MESA_SHADER_TESS_EVAL
:
615 user_sgpr_info
->sgpr_count
+= 1;
617 case MESA_SHADER_GEOMETRY
:
618 user_sgpr_info
->sgpr_count
+= 2;
624 if (needs_view_index
)
625 user_sgpr_info
->sgpr_count
++;
627 if (ctx
->shader_info
->info
.loads_push_constants
)
628 user_sgpr_info
->sgpr_count
+= 2;
630 uint32_t available_sgprs
= ctx
->options
->chip_class
>= GFX9
? 32 : 16;
631 uint32_t remaining_sgprs
= available_sgprs
- user_sgpr_info
->sgpr_count
;
633 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
634 user_sgpr_info
->sgpr_count
+= 2;
635 user_sgpr_info
->indirect_all_descriptor_sets
= true;
637 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
642 declare_global_input_sgprs(struct nir_to_llvm_context
*ctx
,
643 gl_shader_stage stage
,
644 bool has_previous_stage
,
645 gl_shader_stage previous_stage
,
646 const struct user_sgpr_info
*user_sgpr_info
,
647 struct arg_info
*args
,
648 LLVMValueRef
*desc_sets
)
650 LLVMTypeRef type
= const_array(ctx
->ac
.i8
, 1024 * 1024);
651 unsigned num_sets
= ctx
->options
->layout
?
652 ctx
->options
->layout
->num_sets
: 0;
653 unsigned stage_mask
= 1 << stage
;
655 if (has_previous_stage
)
656 stage_mask
|= 1 << previous_stage
;
658 /* 1 for each descriptor set */
659 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
660 for (unsigned i
= 0; i
< num_sets
; ++i
) {
661 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
662 add_array_arg(args
, type
,
663 &ctx
->descriptor_sets
[i
]);
667 add_array_arg(args
, const_array(type
, 32), desc_sets
);
670 if (ctx
->shader_info
->info
.loads_push_constants
) {
671 /* 1 for push constants and dynamic descriptors */
672 add_array_arg(args
, type
, &ctx
->push_constants
);
677 declare_vs_specific_input_sgprs(struct nir_to_llvm_context
*ctx
,
678 gl_shader_stage stage
,
679 bool has_previous_stage
,
680 gl_shader_stage previous_stage
,
681 struct arg_info
*args
)
683 if (!ctx
->is_gs_copy_shader
&&
684 (stage
== MESA_SHADER_VERTEX
||
685 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
686 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
687 add_arg(args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
688 &ctx
->vertex_buffers
);
690 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
);
691 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);
692 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
693 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
);
699 declare_vs_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
701 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
);
702 if (!ctx
->is_gs_copy_shader
) {
703 if (ctx
->options
->key
.vs
.as_ls
) {
704 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->rel_auto_id
);
705 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
707 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
708 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->vs_prim_id
);
710 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* unused */
715 declare_tes_input_vgprs(struct nir_to_llvm_context
*ctx
, struct arg_info
*args
)
717 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_u
);
718 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_v
);
719 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
);
720 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.tes_patch_id
);
724 set_global_input_locs(struct nir_to_llvm_context
*ctx
, gl_shader_stage stage
,
725 bool has_previous_stage
, gl_shader_stage previous_stage
,
726 const struct user_sgpr_info
*user_sgpr_info
,
727 LLVMValueRef desc_sets
, uint8_t *user_sgpr_idx
)
729 unsigned num_sets
= ctx
->options
->layout
?
730 ctx
->options
->layout
->num_sets
: 0;
731 unsigned stage_mask
= 1 << stage
;
733 if (has_previous_stage
)
734 stage_mask
|= 1 << previous_stage
;
736 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
737 for (unsigned i
= 0; i
< num_sets
; ++i
) {
738 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
739 set_loc_desc(ctx
, i
, user_sgpr_idx
, 0);
741 ctx
->descriptor_sets
[i
] = NULL
;
744 set_loc_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
,
747 for (unsigned i
= 0; i
< num_sets
; ++i
) {
748 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& stage_mask
) {
749 set_loc_desc(ctx
, i
, user_sgpr_idx
, i
* 8);
750 ctx
->descriptor_sets
[i
] =
751 ac_build_load_to_sgpr(&ctx
->ac
,
753 LLVMConstInt(ctx
->ac
.i32
, i
, false));
756 ctx
->descriptor_sets
[i
] = NULL
;
758 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
761 if (ctx
->shader_info
->info
.loads_push_constants
) {
762 set_loc_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
767 set_vs_specific_input_locs(struct nir_to_llvm_context
*ctx
,
768 gl_shader_stage stage
, bool has_previous_stage
,
769 gl_shader_stage previous_stage
,
770 uint8_t *user_sgpr_idx
)
772 if (!ctx
->is_gs_copy_shader
&&
773 (stage
== MESA_SHADER_VERTEX
||
774 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
775 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
776 set_loc_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
,
781 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
784 set_loc_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
785 user_sgpr_idx
, vs_num
);
789 static void create_function(struct nir_to_llvm_context
*ctx
,
790 gl_shader_stage stage
,
791 bool has_previous_stage
,
792 gl_shader_stage previous_stage
)
794 uint8_t user_sgpr_idx
;
795 struct user_sgpr_info user_sgpr_info
;
796 struct arg_info args
= {};
797 LLVMValueRef desc_sets
;
798 bool needs_view_index
= needs_view_index_sgpr(ctx
, stage
);
799 allocate_user_sgprs(ctx
, stage
, needs_view_index
, &user_sgpr_info
);
801 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
802 add_arg(&args
, ARG_SGPR
, const_array(ctx
->ac
.v4i32
, 16),
807 case MESA_SHADER_COMPUTE
:
808 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
809 previous_stage
, &user_sgpr_info
,
812 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
813 add_arg(&args
, ARG_SGPR
, ctx
->ac
.v3i32
,
814 &ctx
->num_work_groups
);
817 for (int i
= 0; i
< 3; i
++) {
818 ctx
->workgroup_ids
[i
] = NULL
;
819 if (ctx
->shader_info
->info
.cs
.uses_block_id
[i
]) {
820 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
821 &ctx
->workgroup_ids
[i
]);
825 if (ctx
->shader_info
->info
.cs
.uses_local_invocation_idx
)
826 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tg_size
);
827 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
,
828 &ctx
->local_invocation_ids
);
830 case MESA_SHADER_VERTEX
:
831 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
832 previous_stage
, &user_sgpr_info
,
834 declare_vs_specific_input_sgprs(ctx
, stage
, has_previous_stage
,
835 previous_stage
, &args
);
837 if (needs_view_index
)
838 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
839 if (ctx
->options
->key
.vs
.as_es
)
840 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
842 else if (ctx
->options
->key
.vs
.as_ls
)
843 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
844 &ctx
->ls_out_layout
);
846 declare_vs_input_vgprs(ctx
, &args
);
848 case MESA_SHADER_TESS_CTRL
:
849 if (has_previous_stage
) {
850 // First 6 system regs
851 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
852 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
853 &ctx
->merged_wave_info
);
854 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
855 &ctx
->tess_factor_offset
);
857 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
858 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
859 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
861 declare_global_input_sgprs(ctx
, stage
,
864 &user_sgpr_info
, &args
,
866 declare_vs_specific_input_sgprs(ctx
, stage
,
868 previous_stage
, &args
);
870 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
871 &ctx
->ls_out_layout
);
873 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
874 &ctx
->tcs_offchip_layout
);
875 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
876 &ctx
->tcs_out_offsets
);
877 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
878 &ctx
->tcs_out_layout
);
879 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
880 &ctx
->tcs_in_layout
);
881 if (needs_view_index
)
882 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
885 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
886 &ctx
->abi
.tcs_patch_id
);
887 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
888 &ctx
->abi
.tcs_rel_ids
);
890 declare_vs_input_vgprs(ctx
, &args
);
892 declare_global_input_sgprs(ctx
, stage
,
895 &user_sgpr_info
, &args
,
898 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
899 &ctx
->tcs_offchip_layout
);
900 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
901 &ctx
->tcs_out_offsets
);
902 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
903 &ctx
->tcs_out_layout
);
904 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
905 &ctx
->tcs_in_layout
);
906 if (needs_view_index
)
907 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
910 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
911 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
912 &ctx
->tess_factor_offset
);
913 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
914 &ctx
->abi
.tcs_patch_id
);
915 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
916 &ctx
->abi
.tcs_rel_ids
);
919 case MESA_SHADER_TESS_EVAL
:
920 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
921 previous_stage
, &user_sgpr_info
,
924 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->tcs_offchip_layout
);
925 if (needs_view_index
)
926 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->view_index
);
928 if (ctx
->options
->key
.tes
.as_es
) {
929 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
930 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
931 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
934 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
935 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
937 declare_tes_input_vgprs(ctx
, &args
);
939 case MESA_SHADER_GEOMETRY
:
940 if (has_previous_stage
) {
941 // First 6 system regs
942 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
944 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
945 &ctx
->merged_wave_info
);
946 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
948 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
949 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
950 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
952 declare_global_input_sgprs(ctx
, stage
,
955 &user_sgpr_info
, &args
,
958 if (previous_stage
== MESA_SHADER_TESS_EVAL
) {
959 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
960 &ctx
->tcs_offchip_layout
);
962 declare_vs_specific_input_sgprs(ctx
, stage
,
968 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
969 &ctx
->gsvs_ring_stride
);
970 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
971 &ctx
->gsvs_num_entries
);
972 if (needs_view_index
)
973 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
976 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
977 &ctx
->gs_vtx_offset
[0]);
978 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
979 &ctx
->gs_vtx_offset
[2]);
980 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
981 &ctx
->abi
.gs_prim_id
);
982 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
983 &ctx
->abi
.gs_invocation_id
);
984 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
985 &ctx
->gs_vtx_offset
[4]);
987 if (previous_stage
== MESA_SHADER_VERTEX
) {
988 declare_vs_input_vgprs(ctx
, &args
);
990 declare_tes_input_vgprs(ctx
, &args
);
993 declare_global_input_sgprs(ctx
, stage
,
996 &user_sgpr_info
, &args
,
999 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1000 &ctx
->gsvs_ring_stride
);
1001 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1002 &ctx
->gsvs_num_entries
);
1003 if (needs_view_index
)
1004 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1007 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
);
1008 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs_wave_id
);
1009 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1010 &ctx
->gs_vtx_offset
[0]);
1011 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1012 &ctx
->gs_vtx_offset
[1]);
1013 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1014 &ctx
->abi
.gs_prim_id
);
1015 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1016 &ctx
->gs_vtx_offset
[2]);
1017 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1018 &ctx
->gs_vtx_offset
[3]);
1019 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1020 &ctx
->gs_vtx_offset
[4]);
1021 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1022 &ctx
->gs_vtx_offset
[5]);
1023 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1024 &ctx
->abi
.gs_invocation_id
);
1027 case MESA_SHADER_FRAGMENT
:
1028 declare_global_input_sgprs(ctx
, stage
, has_previous_stage
,
1029 previous_stage
, &user_sgpr_info
,
1032 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
1033 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1034 &ctx
->sample_pos_offset
);
1036 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->prim_mask
);
1037 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_sample
);
1038 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_center
);
1039 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
);
1040 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
1041 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_sample
);
1042 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_center
);
1043 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
);
1044 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
1045 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]);
1046 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]);
1047 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]);
1048 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]);
1049 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.front_face
);
1050 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
);
1051 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
);
1052 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* fixed pt */
1055 unreachable("Shader stage not implemented");
1058 ctx
->main_function
= create_llvm_function(
1059 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
1060 ctx
->max_workgroup_size
,
1061 ctx
->options
->unsafe_math
);
1062 set_llvm_calling_convention(ctx
->main_function
, stage
);
1065 ctx
->shader_info
->num_input_vgprs
= 0;
1066 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
1068 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
1070 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1071 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
1073 assign_arguments(ctx
->main_function
, &args
);
1077 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
1078 set_loc_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
,
1080 if (ctx
->options
->supports_spill
) {
1081 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
1082 LLVMPointerType(ctx
->ac
.i8
, AC_CONST_ADDR_SPACE
),
1083 NULL
, 0, AC_FUNC_ATTR_READNONE
);
1084 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
1085 const_array(ctx
->ac
.v4i32
, 16), "");
1089 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
1090 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
1091 if (has_previous_stage
)
1094 set_global_input_locs(ctx
, stage
, has_previous_stage
, previous_stage
,
1095 &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
1098 case MESA_SHADER_COMPUTE
:
1099 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
1100 set_loc_shader(ctx
, AC_UD_CS_GRID_SIZE
,
1104 case MESA_SHADER_VERTEX
:
1105 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1106 previous_stage
, &user_sgpr_idx
);
1107 if (ctx
->view_index
)
1108 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1109 if (ctx
->options
->key
.vs
.as_ls
) {
1110 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1113 if (ctx
->options
->key
.vs
.as_ls
)
1114 ac_declare_lds_as_pointer(&ctx
->ac
);
1116 case MESA_SHADER_TESS_CTRL
:
1117 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1118 previous_stage
, &user_sgpr_idx
);
1119 if (has_previous_stage
)
1120 set_loc_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
,
1122 set_loc_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
1123 if (ctx
->view_index
)
1124 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1125 ac_declare_lds_as_pointer(&ctx
->ac
);
1127 case MESA_SHADER_TESS_EVAL
:
1128 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
1129 if (ctx
->view_index
)
1130 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1132 case MESA_SHADER_GEOMETRY
:
1133 if (has_previous_stage
) {
1134 if (previous_stage
== MESA_SHADER_VERTEX
)
1135 set_vs_specific_input_locs(ctx
, stage
,
1140 set_loc_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
,
1143 set_loc_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
,
1145 if (ctx
->view_index
)
1146 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1147 if (has_previous_stage
)
1148 ac_declare_lds_as_pointer(&ctx
->ac
);
1150 case MESA_SHADER_FRAGMENT
:
1151 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
1152 set_loc_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
,
1157 unreachable("Shader stage not implemented");
1160 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
1163 static LLVMValueRef
trim_vector(struct ac_llvm_context
*ctx
,
1164 LLVMValueRef value
, unsigned count
)
1166 unsigned num_components
= ac_get_llvm_num_components(value
);
1167 if (count
== num_components
)
1170 LLVMValueRef masks
[] = {
1171 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1172 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1175 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1178 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1179 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1183 build_store_values_extended(struct ac_llvm_context
*ac
,
1184 LLVMValueRef
*values
,
1185 unsigned value_count
,
1186 unsigned value_stride
,
1189 LLVMBuilderRef builder
= ac
->builder
;
1192 for (i
= 0; i
< value_count
; i
++) {
1193 LLVMValueRef ptr
= values
[i
* value_stride
];
1194 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
1195 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1196 LLVMBuildStore(builder
, value
, ptr
);
1200 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
1201 const nir_ssa_def
*def
)
1203 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
1204 if (def
->num_components
> 1) {
1205 type
= LLVMVectorType(type
, def
->num_components
);
1210 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
1213 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
1214 return (LLVMValueRef
)entry
->data
;
1218 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
1219 const struct nir_block
*b
)
1221 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
1222 return (LLVMBasicBlockRef
)entry
->data
;
1225 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
1227 unsigned num_components
)
1229 LLVMValueRef value
= get_src(ctx
, src
.src
);
1230 bool need_swizzle
= false;
1233 LLVMTypeRef type
= LLVMTypeOf(value
);
1234 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1235 ? LLVMGetVectorSize(type
)
1238 for (unsigned i
= 0; i
< num_components
; ++i
) {
1239 assert(src
.swizzle
[i
] < src_components
);
1240 if (src
.swizzle
[i
] != i
)
1241 need_swizzle
= true;
1244 if (need_swizzle
|| num_components
!= src_components
) {
1245 LLVMValueRef masks
[] = {
1246 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
1247 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
1248 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
1249 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
1251 if (src_components
> 1 && num_components
== 1) {
1252 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1254 } else if (src_components
== 1 && num_components
> 1) {
1255 LLVMValueRef values
[] = {value
, value
, value
, value
};
1256 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1258 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1259 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
1263 assert(!src
.negate
);
1268 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
1269 LLVMIntPredicate pred
, LLVMValueRef src0
,
1272 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1273 return LLVMBuildSelect(ctx
->builder
, result
,
1274 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1278 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
1279 LLVMRealPredicate pred
, LLVMValueRef src0
,
1282 LLVMValueRef result
;
1283 src0
= ac_to_float(ctx
, src0
);
1284 src1
= ac_to_float(ctx
, src1
);
1285 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1286 return LLVMBuildSelect(ctx
->builder
, result
,
1287 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1291 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
1293 LLVMTypeRef result_type
,
1297 LLVMValueRef params
[] = {
1298 ac_to_float(ctx
, src0
),
1301 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1302 get_elem_bits(ctx
, result_type
));
1303 assert(length
< sizeof(name
));
1304 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1307 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
1309 LLVMTypeRef result_type
,
1310 LLVMValueRef src0
, LLVMValueRef src1
)
1313 LLVMValueRef params
[] = {
1314 ac_to_float(ctx
, src0
),
1315 ac_to_float(ctx
, src1
),
1318 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1319 get_elem_bits(ctx
, result_type
));
1320 assert(length
< sizeof(name
));
1321 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1324 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
1326 LLVMTypeRef result_type
,
1327 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1330 LLVMValueRef params
[] = {
1331 ac_to_float(ctx
, src0
),
1332 ac_to_float(ctx
, src1
),
1333 ac_to_float(ctx
, src2
),
1336 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
1337 get_elem_bits(ctx
, result_type
));
1338 assert(length
< sizeof(name
));
1339 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1342 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
1343 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1345 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1347 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1350 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
1351 LLVMIntPredicate pred
,
1352 LLVMValueRef src0
, LLVMValueRef src1
)
1354 return LLVMBuildSelect(ctx
->builder
,
1355 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1360 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
1363 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1364 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1367 static LLVMValueRef
emit_fsign(struct ac_llvm_context
*ctx
,
1371 LLVMValueRef cmp
, val
, zero
, one
;
1374 if (bitsize
== 32) {
1384 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
1385 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1386 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
1387 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
1391 static LLVMValueRef
emit_isign(struct ac_llvm_context
*ctx
,
1392 LLVMValueRef src0
, unsigned bitsize
)
1394 LLVMValueRef cmp
, val
, zero
, one
;
1397 if (bitsize
== 32) {
1407 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
1408 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1409 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
1410 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
1414 static LLVMValueRef
emit_ffract(struct ac_llvm_context
*ctx
,
1417 const char *intr
= "llvm.floor.f32";
1418 LLVMValueRef fsrc0
= ac_to_float(ctx
, src0
);
1419 LLVMValueRef params
[] = {
1422 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
,
1423 ctx
->f32
, params
, 1,
1424 AC_FUNC_ATTR_READNONE
);
1425 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1428 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
1430 LLVMValueRef src0
, LLVMValueRef src1
)
1432 LLVMTypeRef ret_type
;
1433 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1435 LLVMValueRef params
[] = { src0
, src1
};
1436 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1439 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
1440 params
, 2, AC_FUNC_ATTR_READNONE
);
1442 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1443 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1447 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
1450 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1453 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1456 src0
= ac_to_float(ctx
, src0
);
1457 return LLVMBuildSExt(ctx
->builder
,
1458 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1462 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1466 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1471 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
1474 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1477 return LLVMBuildSExt(ctx
->builder
,
1478 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1482 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1485 LLVMValueRef result
;
1486 LLVMValueRef cond
= NULL
;
1488 src0
= ac_to_float(&ctx
->ac
, src0
);
1489 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->ac
.f16
, "");
1491 if (ctx
->options
->chip_class
>= VI
) {
1492 LLVMValueRef args
[2];
1493 /* Check if the result is a denormal - and flush to 0 if so. */
1495 args
[1] = LLVMConstInt(ctx
->ac
.i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1496 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->ac
.i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1499 /* need to convert back up to f32 */
1500 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->ac
.f32
, "");
1502 if (ctx
->options
->chip_class
>= VI
)
1503 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1506 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
1507 * so compare the result and flush to 0 if it's smaller.
1509 LLVMValueRef temp
, cond2
;
1510 temp
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1511 ctx
->ac
.f32
, result
);
1512 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
1513 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, 0x38800000, false), ctx
->ac
.f32
, ""),
1515 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
1516 temp
, ctx
->ac
.f32_0
, "");
1517 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
1518 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->ac
.f32_0
, result
, "");
1523 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
1524 LLVMValueRef src0
, LLVMValueRef src1
)
1526 LLVMValueRef dst64
, result
;
1527 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1528 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1530 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1531 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1532 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1536 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
1537 LLVMValueRef src0
, LLVMValueRef src1
)
1539 LLVMValueRef dst64
, result
;
1540 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1541 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1543 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1544 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1545 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1549 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
1551 const LLVMValueRef srcs
[3])
1553 LLVMValueRef result
;
1554 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1556 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1557 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1561 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
1562 LLVMValueRef src0
, LLVMValueRef src1
,
1563 LLVMValueRef src2
, LLVMValueRef src3
)
1565 LLVMValueRef bfi_args
[3], result
;
1567 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1568 LLVMBuildSub(ctx
->builder
,
1569 LLVMBuildShl(ctx
->builder
,
1574 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1577 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1580 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1581 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1583 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1584 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1585 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1587 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1591 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
1594 LLVMValueRef comp
[2];
1596 src0
= ac_to_float(ctx
, src0
);
1597 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
1598 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
1600 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
1603 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
1606 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1607 LLVMValueRef temps
[2], result
, val
;
1610 for (i
= 0; i
< 2; i
++) {
1611 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1612 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1613 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1614 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1617 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1619 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1624 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
1630 LLVMValueRef result
;
1632 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1633 mask
= AC_TID_MASK_LEFT
;
1634 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1635 mask
= AC_TID_MASK_TOP
;
1637 mask
= AC_TID_MASK_TOP_LEFT
;
1639 /* for DDX we want to next X pixel, DDY next Y pixel. */
1640 if (op
== nir_op_fddx_fine
||
1641 op
== nir_op_fddx_coarse
||
1647 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
1652 * this takes an I,J coordinate pair,
1653 * and works out the X and Y derivatives.
1654 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1656 static LLVMValueRef
emit_ddxy_interp(
1657 struct ac_nir_context
*ctx
,
1658 LLVMValueRef interp_ij
)
1660 LLVMValueRef result
[4], a
;
1663 for (i
= 0; i
< 2; i
++) {
1664 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
1665 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
1666 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1667 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1669 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1672 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
1674 LLVMValueRef src
[4], result
= NULL
;
1675 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1676 unsigned src_components
;
1677 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1679 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1680 switch (instr
->op
) {
1686 case nir_op_pack_half_2x16
:
1689 case nir_op_unpack_half_2x16
:
1693 src_components
= num_components
;
1696 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1697 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1699 switch (instr
->op
) {
1705 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1706 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
1709 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
1712 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
1715 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1718 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1719 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1720 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
1723 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1724 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1725 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1728 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
1731 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1734 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1737 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
1740 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1741 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1742 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1743 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1744 ac_to_float_type(&ctx
->ac
, def_type
), result
);
1745 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
1746 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
1749 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1750 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1751 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1754 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
1757 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1760 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
1763 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1764 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1765 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
1768 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1769 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1770 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1773 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1774 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
1778 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
1781 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
1784 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
1787 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
1788 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1789 LLVMTypeOf(src
[0]), ""),
1793 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
1794 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1795 LLVMTypeOf(src
[0]), ""),
1799 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
1800 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
1801 LLVMTypeOf(src
[0]), ""),
1805 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1808 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
1811 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
1814 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
1817 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1820 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
1823 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUEQ
, src
[0], src
[1]);
1826 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
1829 result
= emit_float_cmp(&ctx
->ac
, LLVMRealULT
, src
[0], src
[1]);
1832 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUGE
, src
[0], src
[1]);
1835 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
1836 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1839 result
= emit_iabs(&ctx
->ac
, src
[0]);
1842 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1845 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1848 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1851 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1854 result
= emit_isign(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1857 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1858 result
= emit_fsign(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1861 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
1862 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1865 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
1866 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1869 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
1870 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1872 case nir_op_fround_even
:
1873 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
1874 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
1877 result
= emit_ffract(&ctx
->ac
, src
[0]);
1880 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
1881 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1884 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
1885 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1888 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1889 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1892 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
1893 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1896 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
1897 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1900 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
1901 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
1902 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
1906 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
1907 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1910 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1911 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1912 if (ctx
->ac
.chip_class
< GFX9
&&
1913 instr
->dest
.dest
.ssa
.bit_size
== 32) {
1914 /* Only pre-GFX9 chips do not flush denorms. */
1915 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1916 ac_to_float_type(&ctx
->ac
, def_type
),
1921 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1922 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1923 if (ctx
->ac
.chip_class
< GFX9
&&
1924 instr
->dest
.dest
.ssa
.bit_size
== 32) {
1925 /* Only pre-GFX9 chips do not flush denorms. */
1926 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
1927 ac_to_float_type(&ctx
->ac
, def_type
),
1932 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
1933 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1935 case nir_op_ibitfield_extract
:
1936 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
1938 case nir_op_ubitfield_extract
:
1939 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
1941 case nir_op_bitfield_insert
:
1942 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
1944 case nir_op_bitfield_reverse
:
1945 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1947 case nir_op_bit_count
:
1948 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1953 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1954 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
1955 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1959 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1960 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
1964 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1965 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
1969 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1970 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1974 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1975 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1978 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1979 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1982 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1986 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1987 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1988 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1990 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1994 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1995 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1996 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1998 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
2001 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
2003 case nir_op_find_lsb
:
2004 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2005 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
2007 case nir_op_ufind_msb
:
2008 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2009 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
2011 case nir_op_ifind_msb
:
2012 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2013 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
2015 case nir_op_uadd_carry
:
2016 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2017 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2018 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
2020 case nir_op_usub_borrow
:
2021 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2022 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2023 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
2026 result
= emit_b2f(&ctx
->ac
, src
[0]);
2029 result
= emit_f2b(&ctx
->ac
, src
[0]);
2032 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
2035 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2036 result
= emit_i2b(&ctx
->ac
, src
[0]);
2038 case nir_op_fquantize2f16
:
2039 result
= emit_f2f16(ctx
->nctx
, src
[0]);
2041 case nir_op_umul_high
:
2042 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2043 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2044 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
2046 case nir_op_imul_high
:
2047 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
2048 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
2049 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
2051 case nir_op_pack_half_2x16
:
2052 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
2054 case nir_op_unpack_half_2x16
:
2055 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
2059 case nir_op_fddx_fine
:
2060 case nir_op_fddy_fine
:
2061 case nir_op_fddx_coarse
:
2062 case nir_op_fddy_coarse
:
2063 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
2066 case nir_op_unpack_64_2x32_split_x
: {
2067 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2068 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2071 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2076 case nir_op_unpack_64_2x32_split_y
: {
2077 assert(instr
->src
[0].src
.ssa
->num_components
== 1);
2078 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
2081 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
2086 case nir_op_pack_64_2x32_split
: {
2087 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
2088 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2089 src
[0], ctx
->ac
.i32_0
, "");
2090 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
2091 src
[1], ctx
->ac
.i32_1
, "");
2092 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
2097 fprintf(stderr
, "Unknown NIR alu instr: ");
2098 nir_print_instr(&instr
->instr
, stderr
);
2099 fprintf(stderr
, "\n");
2104 assert(instr
->dest
.dest
.is_ssa
);
2105 result
= ac_to_integer(&ctx
->ac
, result
);
2106 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
2111 static void visit_load_const(struct ac_nir_context
*ctx
,
2112 const nir_load_const_instr
*instr
)
2114 LLVMValueRef values
[4], value
= NULL
;
2115 LLVMTypeRef element_type
=
2116 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
2118 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
2119 switch (instr
->def
.bit_size
) {
2121 values
[i
] = LLVMConstInt(element_type
,
2122 instr
->value
.u32
[i
], false);
2125 values
[i
] = LLVMConstInt(element_type
,
2126 instr
->value
.u64
[i
], false);
2130 "unsupported nir load_const bit_size: %d\n",
2131 instr
->def
.bit_size
);
2135 if (instr
->def
.num_components
> 1) {
2136 value
= LLVMConstVector(values
, instr
->def
.num_components
);
2140 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
2143 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
2146 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2147 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2148 LLVMPointerType(type
, addr_space
), "");
2152 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
2155 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2156 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
2159 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
2160 /* On VI, the descriptor contains the size in bytes,
2161 * but TXQ must return the size in elements.
2162 * The stride is always non-zero for resources using TXQ.
2164 LLVMValueRef stride
=
2165 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
2167 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
2168 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
2169 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
2170 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
2172 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
2178 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2181 static void build_int_type_name(
2183 char *buf
, unsigned bufsize
)
2185 assert(bufsize
>= 6);
2187 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2188 snprintf(buf
, bufsize
, "v%ui32",
2189 LLVMGetVectorSize(type
));
2194 static LLVMValueRef
radv_lower_gather4_integer(struct ac_llvm_context
*ctx
,
2195 struct ac_image_args
*args
,
2196 const nir_tex_instr
*instr
)
2198 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2199 LLVMValueRef coord
= args
->addr
;
2200 LLVMValueRef half_texel
[2];
2201 LLVMValueRef compare_cube_wa
= NULL
;
2202 LLVMValueRef result
;
2204 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
2208 struct ac_image_args txq_args
= { 0 };
2210 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
2211 txq_args
.opcode
= ac_image_get_resinfo
;
2212 txq_args
.dmask
= 0xf;
2213 txq_args
.addr
= ctx
->i32_0
;
2214 txq_args
.resource
= args
->resource
;
2215 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
2217 for (c
= 0; c
< 2; c
++) {
2218 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2219 LLVMConstInt(ctx
->i32
, c
, false), "");
2220 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2221 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
2222 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2223 LLVMConstReal(ctx
->f32
, -0.5), "");
2227 LLVMValueRef orig_coords
= args
->addr
;
2229 for (c
= 0; c
< 2; c
++) {
2231 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2232 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2233 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2234 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2235 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2236 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2241 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2242 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2243 * workaround by sampling using a scaled type and converting.
2244 * This is taken from amdgpu-pro shaders.
2246 /* NOTE this produces some ugly code compared to amdgpu-pro,
2247 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2248 * and then reads them back. -pro generates two selects,
2249 * one s_cmp for the descriptor rewriting
2250 * one v_cmp for the coordinate and result changes.
2252 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2253 LLVMValueRef tmp
, tmp2
;
2255 /* workaround 8/8/8/8 uint/sint cube gather bug */
2256 /* first detect it then change to a scaled read and f2i */
2257 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
2260 /* extract the DATA_FORMAT */
2261 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2262 LLVMConstInt(ctx
->i32
, 6, false), false);
2264 /* is the DATA_FORMAT == 8_8_8_8 */
2265 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2267 if (stype
== GLSL_TYPE_UINT
)
2268 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2269 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2270 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2272 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2273 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2274 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2276 /* replace the NUM FORMAT in the descriptor */
2277 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2278 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2280 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
2282 /* don't modify the coordinates for this case */
2283 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2286 result
= ac_build_image_opcode(ctx
, args
);
2288 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2289 LLVMValueRef tmp
, tmp2
;
2291 /* if the cube workaround is in place, f2i the result. */
2292 for (c
= 0; c
< 4; c
++) {
2293 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2294 if (stype
== GLSL_TYPE_UINT
)
2295 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2297 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2298 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2299 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2300 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2301 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2302 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2308 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
2309 const nir_tex_instr
*instr
,
2311 struct ac_image_args
*args
)
2313 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2314 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2316 return ac_build_buffer_load_format(&ctx
->ac
,
2320 util_last_bit(mask
),
2324 args
->opcode
= ac_image_sample
;
2325 args
->compare
= instr
->is_shadow
;
2327 switch (instr
->op
) {
2329 case nir_texop_txf_ms
:
2330 case nir_texop_samples_identical
:
2331 args
->opcode
= lod_is_zero
||
2332 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
2333 ac_image_load
: ac_image_load_mip
;
2334 args
->compare
= false;
2335 args
->offset
= false;
2342 args
->level_zero
= true;
2347 case nir_texop_query_levels
:
2348 args
->opcode
= ac_image_get_resinfo
;
2351 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2352 args
->level_zero
= true;
2358 args
->opcode
= ac_image_gather4
;
2359 args
->level_zero
= true;
2362 args
->opcode
= ac_image_get_lod
;
2363 args
->compare
= false;
2364 args
->offset
= false;
2370 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
2371 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2372 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2373 return radv_lower_gather4_integer(&ctx
->ac
, args
, instr
);
2376 return ac_build_image_opcode(&ctx
->ac
, args
);
2379 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2380 nir_intrinsic_instr
*instr
)
2382 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[0]);
2383 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2384 unsigned binding
= nir_intrinsic_binding(instr
);
2385 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2386 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2387 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2388 unsigned base_offset
= layout
->binding
[binding
].offset
;
2389 LLVMValueRef offset
, stride
;
2391 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2392 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2393 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2394 layout
->binding
[binding
].dynamic_offset_offset
;
2395 desc_ptr
= ctx
->push_constants
;
2396 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2397 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2399 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
2401 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
2402 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2403 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2405 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2406 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->ac
.v4i32
);
2407 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2412 static LLVMValueRef
visit_vulkan_resource_reindex(struct nir_to_llvm_context
*ctx
,
2413 nir_intrinsic_instr
*instr
)
2415 LLVMValueRef ptr
= get_src(ctx
->nir
, instr
->src
[0]);
2416 LLVMValueRef index
= get_src(ctx
->nir
, instr
->src
[1]);
2418 LLVMValueRef result
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2419 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
2423 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2424 nir_intrinsic_instr
*instr
)
2426 LLVMValueRef ptr
, addr
;
2428 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
2429 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
->nir
, instr
->src
[0]), "");
2431 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2432 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
->nir
, &instr
->dest
.ssa
));
2434 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2437 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
2438 const nir_intrinsic_instr
*instr
)
2440 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2442 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
2445 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2447 uint32_t new_mask
= 0;
2448 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2449 if (mask
& (1u << i
))
2450 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2454 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
2455 unsigned start
, unsigned count
)
2457 LLVMTypeRef type
= LLVMTypeOf(src
);
2459 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
2465 unsigned src_elements
= LLVMGetVectorSize(type
);
2466 assert(start
< src_elements
);
2467 assert(start
+ count
<= src_elements
);
2469 if (start
== 0 && count
== src_elements
)
2473 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
2476 LLVMValueRef indices
[8];
2477 for (unsigned i
= 0; i
< count
; ++i
)
2478 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
2480 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
2481 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
2484 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
2485 nir_intrinsic_instr
*instr
)
2487 const char *store_name
;
2488 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2489 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2490 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2491 int components_32bit
= elem_size_mult
* instr
->num_components
;
2492 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2493 LLVMValueRef base_data
, base_offset
;
2494 LLVMValueRef params
[6];
2496 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
2497 get_src(ctx
, instr
->src
[1]), true);
2498 params
[2] = ctx
->ac
.i32_0
; /* vindex */
2499 params
[4] = ctx
->ac
.i1false
; /* glc */
2500 params
[5] = ctx
->ac
.i1false
; /* slc */
2502 if (components_32bit
> 1)
2503 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
2505 writemask
= widen_mask(writemask
, elem_size_mult
);
2507 base_data
= ac_to_float(&ctx
->ac
, src_data
);
2508 base_data
= trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
2509 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
2511 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2515 LLVMValueRef offset
;
2517 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2519 /* Due to an LLVM limitation, split 3-element writes
2520 * into a 2-element and a 1-element write. */
2522 writemask
|= 1 << (start
+ 2);
2527 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2532 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2533 } else if (count
== 2) {
2534 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2538 store_name
= "llvm.amdgcn.buffer.store.f32";
2540 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
2542 offset
= base_offset
;
2544 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
2548 ac_build_intrinsic(&ctx
->ac
, store_name
,
2549 ctx
->ac
.voidt
, params
, 6, 0);
2553 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
2554 const nir_intrinsic_instr
*instr
)
2557 LLVMValueRef params
[6];
2560 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2561 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
2563 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2564 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
2565 get_src(ctx
, instr
->src
[0]),
2567 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
2568 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2569 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
2571 switch (instr
->intrinsic
) {
2572 case nir_intrinsic_ssbo_atomic_add
:
2573 name
= "llvm.amdgcn.buffer.atomic.add";
2575 case nir_intrinsic_ssbo_atomic_imin
:
2576 name
= "llvm.amdgcn.buffer.atomic.smin";
2578 case nir_intrinsic_ssbo_atomic_umin
:
2579 name
= "llvm.amdgcn.buffer.atomic.umin";
2581 case nir_intrinsic_ssbo_atomic_imax
:
2582 name
= "llvm.amdgcn.buffer.atomic.smax";
2584 case nir_intrinsic_ssbo_atomic_umax
:
2585 name
= "llvm.amdgcn.buffer.atomic.umax";
2587 case nir_intrinsic_ssbo_atomic_and
:
2588 name
= "llvm.amdgcn.buffer.atomic.and";
2590 case nir_intrinsic_ssbo_atomic_or
:
2591 name
= "llvm.amdgcn.buffer.atomic.or";
2593 case nir_intrinsic_ssbo_atomic_xor
:
2594 name
= "llvm.amdgcn.buffer.atomic.xor";
2596 case nir_intrinsic_ssbo_atomic_exchange
:
2597 name
= "llvm.amdgcn.buffer.atomic.swap";
2599 case nir_intrinsic_ssbo_atomic_comp_swap
:
2600 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2606 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
2609 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2610 const nir_intrinsic_instr
*instr
)
2612 LLVMValueRef results
[2];
2613 int load_components
;
2614 int num_components
= instr
->num_components
;
2615 if (instr
->dest
.ssa
.bit_size
== 64)
2616 num_components
*= 2;
2618 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2619 load_components
= MIN2(num_components
- i
, 4);
2620 const char *load_name
;
2621 LLVMTypeRef data_type
= ctx
->ac
.f32
;
2622 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
2623 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2625 if (load_components
== 3)
2626 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
2627 else if (load_components
> 1)
2628 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
2630 if (load_components
>= 3)
2631 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2632 else if (load_components
== 2)
2633 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2634 else if (load_components
== 1)
2635 load_name
= "llvm.amdgcn.buffer.load.f32";
2637 unreachable("unhandled number of components");
2639 LLVMValueRef params
[] = {
2640 ctx
->abi
->load_ssbo(ctx
->abi
,
2641 get_src(ctx
, instr
->src
[0]),
2649 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2653 LLVMValueRef ret
= results
[0];
2654 if (num_components
> 4 || num_components
== 3) {
2655 LLVMValueRef masks
[] = {
2656 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2657 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2658 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
2659 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
2662 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2663 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
2664 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2667 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2668 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2671 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2672 const nir_intrinsic_instr
*instr
)
2675 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2676 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2677 int num_components
= instr
->num_components
;
2679 if (ctx
->abi
->load_ubo
)
2680 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2682 if (instr
->dest
.ssa
.bit_size
== 64)
2683 num_components
*= 2;
2685 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2686 NULL
, 0, false, false, true, true);
2687 ret
= trim_vector(&ctx
->ac
, ret
, num_components
);
2688 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2689 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2693 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
2694 bool vs_in
, unsigned *vertex_index_out
,
2695 LLVMValueRef
*vertex_index_ref
,
2696 unsigned *const_out
, LLVMValueRef
*indir_out
)
2698 unsigned const_offset
= 0;
2699 nir_deref
*tail
= &deref
->deref
;
2700 LLVMValueRef offset
= NULL
;
2702 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2704 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2705 if (vertex_index_out
)
2706 *vertex_index_out
= deref_array
->base_offset
;
2708 if (vertex_index_ref
) {
2709 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
2710 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2711 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2713 *vertex_index_ref
= vtx
;
2717 if (deref
->var
->data
.compact
) {
2718 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2719 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2720 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2721 /* We always lower indirect dereferences for "compact" array vars. */
2722 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2724 const_offset
= deref_array
->base_offset
;
2728 while (tail
->child
!= NULL
) {
2729 const struct glsl_type
*parent_type
= tail
->type
;
2732 if (tail
->deref_type
== nir_deref_type_array
) {
2733 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2734 LLVMValueRef index
, stride
, local_offset
;
2735 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2737 const_offset
+= size
* deref_array
->base_offset
;
2738 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2741 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2742 index
= get_src(ctx
, deref_array
->indirect
);
2743 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
2744 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
2747 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
2749 offset
= local_offset
;
2750 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2751 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2753 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2754 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2755 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2758 unreachable("unsupported deref type");
2762 if (const_offset
&& offset
)
2763 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2764 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2767 *const_out
= const_offset
;
2768 *indir_out
= offset
;
2772 /* The offchip buffer layout for TCS->TES is
2774 * - attribute 0 of patch 0 vertex 0
2775 * - attribute 0 of patch 0 vertex 1
2776 * - attribute 0 of patch 0 vertex 2
2778 * - attribute 0 of patch 1 vertex 0
2779 * - attribute 0 of patch 1 vertex 1
2781 * - attribute 1 of patch 0 vertex 0
2782 * - attribute 1 of patch 0 vertex 1
2784 * - per patch attribute 0 of patch 0
2785 * - per patch attribute 0 of patch 1
2788 * Note that every attribute has 4 components.
2790 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2791 LLVMValueRef vertex_index
,
2792 LLVMValueRef param_index
)
2794 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2795 LLVMValueRef param_stride
, constant16
;
2796 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2798 vertices_per_patch
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 9, 6);
2799 num_patches
= unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 0, 9);
2800 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2803 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
2805 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2806 vertices_per_patch
, "");
2808 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2811 param_stride
= total_vertices
;
2813 base_addr
= rel_patch_id
;
2814 param_stride
= num_patches
;
2817 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2818 LLVMBuildMul(ctx
->builder
, param_index
,
2819 param_stride
, ""), "");
2821 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2823 if (!vertex_index
) {
2824 LLVMValueRef patch_data_offset
=
2825 unpack_param(&ctx
->ac
, ctx
->tcs_offchip_layout
, 16, 16);
2827 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2828 patch_data_offset
, "");
2833 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2835 unsigned const_index
,
2837 LLVMValueRef vertex_index
,
2838 LLVMValueRef indir_index
)
2840 LLVMValueRef param_index
;
2843 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
2846 if (const_index
&& !is_compact
)
2847 param
+= const_index
;
2848 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
2850 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2854 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2855 bool is_patch
, uint32_t param
)
2859 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2861 ctx
->tess_outputs_written
|= (1ull << param
);
2865 get_dw_address(struct nir_to_llvm_context
*ctx
,
2866 LLVMValueRef dw_addr
,
2868 unsigned const_index
,
2869 bool compact_const_index
,
2870 LLVMValueRef vertex_index
,
2871 LLVMValueRef stride
,
2872 LLVMValueRef indir_index
)
2877 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2878 LLVMBuildMul(ctx
->builder
,
2884 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2885 LLVMBuildMul(ctx
->builder
, indir_index
,
2886 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
2887 else if (const_index
&& !compact_const_index
)
2888 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2889 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2891 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2892 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
2894 if (const_index
&& compact_const_index
)
2895 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2896 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
2901 load_tcs_varyings(struct ac_shader_abi
*abi
,
2902 LLVMValueRef vertex_index
,
2903 LLVMValueRef indir_index
,
2904 unsigned const_index
,
2906 unsigned driver_location
,
2908 unsigned num_components
,
2913 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2914 LLVMValueRef dw_addr
, stride
;
2915 LLVMValueRef value
[4], result
;
2916 unsigned param
= shader_io_get_unique_index(location
);
2919 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_in_layout
, 13, 8);
2920 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2923 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2924 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2926 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2931 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2934 for (unsigned i
= 0; i
< num_components
+ component
; i
++) {
2935 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
2936 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2939 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
2944 store_tcs_output(struct ac_shader_abi
*abi
,
2945 LLVMValueRef vertex_index
,
2946 LLVMValueRef param_index
,
2947 unsigned const_index
,
2949 unsigned driver_location
,
2956 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
2957 LLVMValueRef dw_addr
;
2958 LLVMValueRef stride
= NULL
;
2959 LLVMValueRef buf_addr
= NULL
;
2961 bool store_lds
= true;
2964 if (!(ctx
->tcs_patch_outputs_read
& (1U << (location
- VARYING_SLOT_PATCH0
))))
2967 if (!(ctx
->tcs_outputs_read
& (1ULL << location
)))
2971 param
= shader_io_get_unique_index(location
);
2972 if (location
== VARYING_SLOT_CLIP_DIST0
&&
2973 is_compact
&& const_index
> 3) {
2979 stride
= unpack_param(&ctx
->ac
, ctx
->tcs_out_layout
, 13, 8);
2980 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2982 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2985 mark_tess_output(ctx
, is_patch
, param
);
2987 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2989 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2990 vertex_index
, param_index
);
2992 bool is_tess_factor
= false;
2993 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2994 location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2995 is_tess_factor
= true;
2997 unsigned base
= is_compact
? const_index
: 0;
2998 for (unsigned chan
= 0; chan
< 8; chan
++) {
2999 if (!(writemask
& (1 << chan
)))
3001 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
3003 if (store_lds
|| is_tess_factor
) {
3004 LLVMValueRef dw_addr_chan
=
3005 LLVMBuildAdd(ctx
->builder
, dw_addr
,
3006 LLVMConstInt(ctx
->ac
.i32
, chan
, false), "");
3007 ac_lds_store(&ctx
->ac
, dw_addr_chan
, value
);
3010 if (!is_tess_factor
&& writemask
!= 0xF)
3011 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
3012 buf_addr
, ctx
->oc_lds
,
3013 4 * (base
+ chan
), 1, 0, true, false);
3016 if (writemask
== 0xF) {
3017 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
3018 buf_addr
, ctx
->oc_lds
,
3019 (base
* 4), 1, 0, true, false);
3024 load_tes_input(struct ac_shader_abi
*abi
,
3025 LLVMValueRef vertex_index
,
3026 LLVMValueRef param_index
,
3027 unsigned const_index
,
3029 unsigned driver_location
,
3031 unsigned num_components
,
3036 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3037 LLVMValueRef buf_addr
;
3038 LLVMValueRef result
;
3039 unsigned param
= shader_io_get_unique_index(location
);
3041 if (location
== VARYING_SLOT_CLIP_DIST0
&& is_compact
&& const_index
> 3) {
3046 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
3047 is_compact
, vertex_index
, param_index
);
3049 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, component
* 4, false);
3050 buf_addr
= LLVMBuildAdd(ctx
->builder
, buf_addr
, comp_offset
, "");
3052 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, num_components
, NULL
,
3053 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
3054 result
= trim_vector(&ctx
->ac
, result
, num_components
);
3059 load_gs_input(struct ac_shader_abi
*abi
,
3061 unsigned driver_location
,
3063 unsigned num_components
,
3064 unsigned vertex_index
,
3065 unsigned const_index
,
3068 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
3069 LLVMValueRef vtx_offset
;
3070 LLVMValueRef args
[9];
3071 unsigned param
, vtx_offset_param
;
3072 LLVMValueRef value
[4], result
;
3074 vtx_offset_param
= vertex_index
;
3075 assert(vtx_offset_param
< 6);
3076 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
3077 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3079 param
= shader_io_get_unique_index(location
);
3081 for (unsigned i
= component
; i
< num_components
+ component
; i
++) {
3082 if (ctx
->ac
.chip_class
>= GFX9
) {
3083 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
3084 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
3085 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
3086 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
3088 args
[0] = ctx
->esgs_ring
;
3089 args
[1] = vtx_offset
;
3090 args
[2] = LLVMConstInt(ctx
->ac
.i32
, (param
* 4 + i
+ const_index
) * 256, false);
3091 args
[3] = ctx
->ac
.i32_0
;
3092 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
3093 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
3094 args
[6] = ctx
->ac
.i32_1
; /* GLC */
3095 args
[7] = ctx
->ac
.i32_0
; /* SLC */
3096 args
[8] = ctx
->ac
.i32_0
; /* TFE */
3098 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
3099 ctx
->ac
.i32
, args
, 9,
3100 AC_FUNC_ATTR_READONLY
|
3101 AC_FUNC_ATTR_LEGACY
);
3104 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
3110 build_gep_for_deref(struct ac_nir_context
*ctx
,
3111 nir_deref_var
*deref
)
3113 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
3114 assert(entry
->data
);
3115 LLVMValueRef val
= entry
->data
;
3116 nir_deref
*tail
= deref
->deref
.child
;
3117 while (tail
!= NULL
) {
3118 LLVMValueRef offset
;
3119 switch (tail
->deref_type
) {
3120 case nir_deref_type_array
: {
3121 nir_deref_array
*array
= nir_deref_as_array(tail
);
3122 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
3123 if (array
->deref_array_type
==
3124 nir_deref_array_type_indirect
) {
3125 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
3132 case nir_deref_type_struct
: {
3133 nir_deref_struct
*deref_struct
=
3134 nir_deref_as_struct(tail
);
3135 offset
= LLVMConstInt(ctx
->ac
.i32
,
3136 deref_struct
->index
, 0);
3140 unreachable("bad deref type");
3142 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
3148 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
3149 nir_intrinsic_instr
*instr
,
3152 LLVMValueRef result
;
3153 LLVMValueRef vertex_index
= NULL
;
3154 LLVMValueRef indir_index
= NULL
;
3155 unsigned const_index
= 0;
3156 unsigned location
= instr
->variables
[0]->var
->data
.location
;
3157 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3158 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3159 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3161 get_deref_offset(ctx
, instr
->variables
[0],
3162 false, NULL
, is_patch
? NULL
: &vertex_index
,
3163 &const_index
, &indir_index
);
3165 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, vertex_index
, indir_index
,
3166 const_index
, location
, driver_location
,
3167 instr
->variables
[0]->var
->data
.location_frac
,
3168 instr
->num_components
,
3169 is_patch
, is_compact
, load_inputs
);
3170 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3173 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
3174 nir_intrinsic_instr
*instr
)
3176 LLVMValueRef values
[8];
3177 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3178 int ve
= instr
->dest
.ssa
.num_components
;
3179 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3180 LLVMValueRef indir_index
;
3182 unsigned const_index
;
3183 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
3184 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
3185 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
3186 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
3187 &const_index
, &indir_index
);
3189 if (instr
->dest
.ssa
.bit_size
== 64)
3192 switch (instr
->variables
[0]->var
->data
.mode
) {
3193 case nir_var_shader_in
:
3194 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3195 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3196 return load_tess_varyings(ctx
, instr
, true);
3199 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3200 LLVMValueRef indir_index
;
3201 unsigned const_index
, vertex_index
;
3202 get_deref_offset(ctx
, instr
->variables
[0],
3203 false, &vertex_index
, NULL
,
3204 &const_index
, &indir_index
);
3205 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
3206 instr
->variables
[0]->var
->data
.driver_location
,
3207 instr
->variables
[0]->var
->data
.location_frac
, ve
,
3208 vertex_index
, const_index
,
3209 nir2llvmtype(ctx
, instr
->variables
[0]->var
->type
));
3212 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3214 unsigned count
= glsl_count_attribute_slots(
3215 instr
->variables
[0]->var
->type
,
3216 ctx
->stage
== MESA_SHADER_VERTEX
);
3218 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3219 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
3220 stride
, false, true);
3222 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3226 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
3230 for (unsigned chan
= 0; chan
< ve
; chan
++) {
3232 unsigned count
= glsl_count_attribute_slots(
3233 instr
->variables
[0]->var
->type
, false);
3235 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3236 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3237 stride
, true, true);
3239 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3243 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
3247 case nir_var_shared
: {
3248 LLVMValueRef address
= build_gep_for_deref(ctx
,
3249 instr
->variables
[0]);
3250 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
3251 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
3252 get_def_type(ctx
, &instr
->dest
.ssa
),
3255 case nir_var_shader_out
:
3256 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3257 return load_tess_varyings(ctx
, instr
, false);
3260 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
3262 unsigned count
= glsl_count_attribute_slots(
3263 instr
->variables
[0]->var
->type
, false);
3265 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3266 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3267 stride
, true, true);
3269 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
3273 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3274 ctx
->outputs
[idx
+ chan
+ const_index
* stride
],
3280 unreachable("unhandle variable mode");
3282 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
3283 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3287 visit_store_var(struct ac_nir_context
*ctx
,
3288 nir_intrinsic_instr
*instr
)
3290 LLVMValueRef temp_ptr
, value
;
3291 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3292 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3293 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3294 int writemask
= instr
->const_index
[0] << comp
;
3295 LLVMValueRef indir_index
;
3296 unsigned const_index
;
3297 get_deref_offset(ctx
, instr
->variables
[0], false,
3298 NULL
, NULL
, &const_index
, &indir_index
);
3300 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
3302 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
3303 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
3306 writemask
= widen_mask(writemask
, 2);
3309 switch (instr
->variables
[0]->var
->data
.mode
) {
3310 case nir_var_shader_out
:
3312 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3313 LLVMValueRef vertex_index
= NULL
;
3314 LLVMValueRef indir_index
= NULL
;
3315 unsigned const_index
= 0;
3316 const unsigned location
= instr
->variables
[0]->var
->data
.location
;
3317 const unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
3318 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
3319 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
3320 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
3322 get_deref_offset(ctx
, instr
->variables
[0],
3323 false, NULL
, is_patch
? NULL
: &vertex_index
,
3324 &const_index
, &indir_index
);
3326 ctx
->abi
->store_tcs_outputs(ctx
->abi
, vertex_index
, indir_index
,
3327 const_index
, location
, driver_location
,
3328 src
, comp
, is_patch
, is_compact
, writemask
);
3332 for (unsigned chan
= 0; chan
< 8; chan
++) {
3334 if (!(writemask
& (1 << chan
)))
3337 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
3339 if (instr
->variables
[0]->var
->data
.compact
)
3342 unsigned count
= glsl_count_attribute_slots(
3343 instr
->variables
[0]->var
->type
, false);
3345 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3346 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3347 stride
, true, true);
3349 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3350 value
, indir_index
, "");
3351 build_store_values_extended(&ctx
->ac
, ctx
->outputs
+ idx
+ chan
,
3352 count
, stride
, tmp_vec
);
3355 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3357 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3362 for (unsigned chan
= 0; chan
< 8; chan
++) {
3363 if (!(writemask
& (1 << chan
)))
3366 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3368 unsigned count
= glsl_count_attribute_slots(
3369 instr
->variables
[0]->var
->type
, false);
3371 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3372 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3375 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
3376 value
, indir_index
, "");
3377 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
3380 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3382 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
3386 case nir_var_shared
: {
3387 int writemask
= instr
->const_index
[0];
3388 LLVMValueRef address
= build_gep_for_deref(ctx
,
3389 instr
->variables
[0]);
3390 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
3391 unsigned components
=
3392 glsl_get_vector_elements(
3393 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
3394 if (writemask
== (1 << components
) - 1) {
3395 val
= LLVMBuildBitCast(
3396 ctx
->ac
.builder
, val
,
3397 LLVMGetElementType(LLVMTypeOf(address
)), "");
3398 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
3400 for (unsigned chan
= 0; chan
< 4; chan
++) {
3401 if (!(writemask
& (1 << chan
)))
3404 LLVMBuildStructGEP(ctx
->ac
.builder
,
3406 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
3408 src
= LLVMBuildBitCast(
3409 ctx
->ac
.builder
, src
,
3410 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
3411 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
3421 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3424 case GLSL_SAMPLER_DIM_BUF
:
3426 case GLSL_SAMPLER_DIM_1D
:
3427 return array
? 2 : 1;
3428 case GLSL_SAMPLER_DIM_2D
:
3429 return array
? 3 : 2;
3430 case GLSL_SAMPLER_DIM_MS
:
3431 return array
? 4 : 3;
3432 case GLSL_SAMPLER_DIM_3D
:
3433 case GLSL_SAMPLER_DIM_CUBE
:
3435 case GLSL_SAMPLER_DIM_RECT
:
3436 case GLSL_SAMPLER_DIM_SUBPASS
:
3438 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3448 /* Adjust the sample index according to FMASK.
3450 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3451 * which is the identity mapping. Each nibble says which physical sample
3452 * should be fetched to get that sample.
3454 * For example, 0x11111100 means there are only 2 samples stored and
3455 * the second sample covers 3/4 of the pixel. When reading samples 0
3456 * and 1, return physical sample 0 (determined by the first two 0s
3457 * in FMASK), otherwise return physical sample 1.
3459 * The sample index should be adjusted as follows:
3460 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3462 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
3463 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3464 LLVMValueRef coord_z
,
3465 LLVMValueRef sample_index
,
3466 LLVMValueRef fmask_desc_ptr
)
3468 LLVMValueRef fmask_load_address
[4];
3471 fmask_load_address
[0] = coord_x
;
3472 fmask_load_address
[1] = coord_y
;
3474 fmask_load_address
[2] = coord_z
;
3475 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3478 struct ac_image_args args
= {0};
3480 args
.opcode
= ac_image_load
;
3481 args
.da
= coord_z
? true : false;
3482 args
.resource
= fmask_desc_ptr
;
3484 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
3486 res
= ac_build_image_opcode(ctx
, &args
);
3488 res
= ac_to_integer(ctx
, res
);
3489 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3490 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3492 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3496 LLVMValueRef sample_index4
=
3497 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3498 LLVMValueRef shifted_fmask
=
3499 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3500 LLVMValueRef final_sample
=
3501 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3503 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3504 * resource descriptor is 0 (invalid),
3506 LLVMValueRef fmask_desc
=
3507 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3510 LLVMValueRef fmask_word1
=
3511 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3514 LLVMValueRef word1_is_nonzero
=
3515 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3516 fmask_word1
, ctx
->i32_0
, "");
3518 /* Replace the MSAA sample index. */
3520 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3521 final_sample
, sample_index
, "");
3522 return sample_index
;
3525 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
3526 const nir_intrinsic_instr
*instr
)
3528 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
3530 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3531 LLVMValueRef coords
[4];
3532 LLVMValueRef masks
[] = {
3533 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
3534 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
3537 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
3540 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3541 bool is_array
= glsl_sampler_type_is_array(type
);
3542 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3543 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3544 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3545 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3546 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3547 count
= image_type_to_components_count(dim
, is_array
);
3550 LLVMValueRef fmask_load_address
[3];
3553 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3554 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
3556 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
3558 fmask_load_address
[2] = NULL
;
3560 for (chan
= 0; chan
< 2; ++chan
)
3561 fmask_load_address
[chan
] =
3562 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
3563 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3564 ctx
->ac
.i32
, ""), "");
3565 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3567 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
3568 fmask_load_address
[0],
3569 fmask_load_address
[1],
3570 fmask_load_address
[2],
3572 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
3574 if (count
== 1 && !gfx9_1d
) {
3575 if (instr
->src
[0].ssa
->num_components
)
3576 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
3583 for (chan
= 0; chan
< count
; ++chan
) {
3584 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
3587 for (chan
= 0; chan
< 2; ++chan
)
3588 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
3589 ctx
->ac
.i32
, ""), "");
3590 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
3596 coords
[2] = coords
[1];
3597 coords
[1] = ctx
->ac
.i32_0
;
3599 coords
[1] = ctx
->ac
.i32_0
;
3604 coords
[count
] = sample_index
;
3609 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
3612 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3617 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
3618 const nir_intrinsic_instr
*instr
)
3620 LLVMValueRef params
[7];
3622 char intrinsic_name
[64];
3623 const nir_variable
*var
= instr
->variables
[0]->var
;
3624 const struct glsl_type
*type
= var
->type
;
3626 if(instr
->variables
[0]->deref
.child
)
3627 type
= instr
->variables
[0]->deref
.child
->type
;
3629 type
= glsl_without_array(type
);
3630 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3631 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
3632 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3633 ctx
->ac
.i32_0
, ""); /* vindex */
3634 params
[2] = ctx
->ac
.i32_0
; /* voffset */
3635 params
[3] = ctx
->ac
.i1false
; /* glc */
3636 params
[4] = ctx
->ac
.i1false
; /* slc */
3637 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->ac
.v4f32
,
3640 res
= trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
3641 res
= ac_to_integer(&ctx
->ac
, res
);
3643 bool is_da
= glsl_sampler_type_is_array(type
) ||
3644 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3645 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_3D
||
3646 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
||
3647 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS_MS
;
3648 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3649 LLVMValueRef glc
= ctx
->ac
.i1false
;
3650 LLVMValueRef slc
= ctx
->ac
.i1false
;
3652 params
[0] = get_image_coords(ctx
, instr
);
3653 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3654 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3655 if (HAVE_LLVM
<= 0x0309) {
3656 params
[3] = ctx
->ac
.i1false
; /* r128 */
3661 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3668 ac_get_image_intr_name("llvm.amdgcn.image.load",
3669 ctx
->ac
.v4f32
, /* vdata */
3670 LLVMTypeOf(params
[0]), /* coords */
3671 LLVMTypeOf(params
[1]), /* rsrc */
3672 intrinsic_name
, sizeof(intrinsic_name
));
3674 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
3675 params
, 7, AC_FUNC_ATTR_READONLY
);
3677 return ac_to_integer(&ctx
->ac
, res
);
3680 static void visit_image_store(struct ac_nir_context
*ctx
,
3681 nir_intrinsic_instr
*instr
)
3683 LLVMValueRef params
[8];
3684 char intrinsic_name
[64];
3685 const nir_variable
*var
= instr
->variables
[0]->var
;
3686 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3687 LLVMValueRef glc
= ctx
->ac
.i1false
;
3688 bool force_glc
= ctx
->ac
.chip_class
== SI
;
3690 glc
= ctx
->ac
.i1true
;
3692 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3693 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3694 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
3695 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3696 ctx
->ac
.i32_0
, ""); /* vindex */
3697 params
[3] = ctx
->ac
.i32_0
; /* voffset */
3698 params
[4] = glc
; /* glc */
3699 params
[5] = ctx
->ac
.i1false
; /* slc */
3700 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
3703 bool is_da
= glsl_sampler_type_is_array(type
) ||
3704 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
||
3705 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_3D
;
3706 LLVMValueRef da
= is_da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
3707 LLVMValueRef slc
= ctx
->ac
.i1false
;
3709 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3710 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3711 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
3712 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
3713 if (HAVE_LLVM
<= 0x0309) {
3714 params
[4] = ctx
->ac
.i1false
; /* r128 */
3719 LLVMValueRef lwe
= ctx
->ac
.i1false
;
3726 ac_get_image_intr_name("llvm.amdgcn.image.store",
3727 LLVMTypeOf(params
[0]), /* vdata */
3728 LLVMTypeOf(params
[1]), /* coords */
3729 LLVMTypeOf(params
[2]), /* rsrc */
3730 intrinsic_name
, sizeof(intrinsic_name
));
3732 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
3738 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
3739 const nir_intrinsic_instr
*instr
)
3741 LLVMValueRef params
[7];
3742 int param_count
= 0;
3743 const nir_variable
*var
= instr
->variables
[0]->var
;
3745 const char *atomic_name
;
3746 char intrinsic_name
[41];
3747 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3748 MAYBE_UNUSED
int length
;
3750 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
3752 switch (instr
->intrinsic
) {
3753 case nir_intrinsic_image_atomic_add
:
3754 atomic_name
= "add";
3756 case nir_intrinsic_image_atomic_min
:
3757 atomic_name
= is_unsigned
? "umin" : "smin";
3759 case nir_intrinsic_image_atomic_max
:
3760 atomic_name
= is_unsigned
? "umax" : "smax";
3762 case nir_intrinsic_image_atomic_and
:
3763 atomic_name
= "and";
3765 case nir_intrinsic_image_atomic_or
:
3768 case nir_intrinsic_image_atomic_xor
:
3769 atomic_name
= "xor";
3771 case nir_intrinsic_image_atomic_exchange
:
3772 atomic_name
= "swap";
3774 case nir_intrinsic_image_atomic_comp_swap
:
3775 atomic_name
= "cmpswap";
3781 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3782 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3783 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3785 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3786 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
3788 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
3789 ctx
->ac
.i32_0
, ""); /* vindex */
3790 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3791 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3793 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3794 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3796 char coords_type
[8];
3798 bool da
= glsl_sampler_type_is_array(type
) ||
3799 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3801 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3802 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
3804 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
3805 params
[param_count
++] = da
? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
3806 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3808 build_int_type_name(LLVMTypeOf(coords
),
3809 coords_type
, sizeof(coords_type
));
3811 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3812 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
3815 assert(length
< sizeof(intrinsic_name
));
3816 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
3819 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3820 const nir_intrinsic_instr
*instr
)
3823 const nir_variable
*var
= instr
->variables
[0]->var
;
3824 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3825 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3826 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
||
3827 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_3D
;
3828 if(instr
->variables
[0]->deref
.child
)
3829 type
= instr
->variables
[0]->deref
.child
->type
;
3831 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3832 return get_buffer_size(ctx
,
3833 get_sampler_desc(ctx
, instr
->variables
[0],
3834 AC_DESC_BUFFER
, NULL
, true, false), true);
3836 struct ac_image_args args
= { 0 };
3840 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
3841 args
.opcode
= ac_image_get_resinfo
;
3842 args
.addr
= ctx
->ac
.i32_0
;
3844 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3846 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3848 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3849 glsl_sampler_type_is_array(type
)) {
3850 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3851 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3852 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3853 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3855 if (ctx
->ac
.chip_class
>= GFX9
&&
3856 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
3857 glsl_sampler_type_is_array(type
)) {
3858 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3859 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3866 #define NOOP_WAITCNT 0xf7f
3867 #define LGKM_CNT 0x07f
3868 #define VM_CNT 0xf70
3870 static void emit_membar(struct nir_to_llvm_context
*ctx
,
3871 const nir_intrinsic_instr
*instr
)
3873 unsigned waitcnt
= NOOP_WAITCNT
;
3875 switch (instr
->intrinsic
) {
3876 case nir_intrinsic_memory_barrier
:
3877 case nir_intrinsic_group_memory_barrier
:
3878 waitcnt
&= VM_CNT
& LGKM_CNT
;
3880 case nir_intrinsic_memory_barrier_atomic_counter
:
3881 case nir_intrinsic_memory_barrier_buffer
:
3882 case nir_intrinsic_memory_barrier_image
:
3885 case nir_intrinsic_memory_barrier_shared
:
3886 waitcnt
&= LGKM_CNT
;
3891 if (waitcnt
!= NOOP_WAITCNT
)
3892 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3895 static void emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3897 /* SI only (thanks to a hw bug workaround):
3898 * The real barrier instruction isn’t needed, because an entire patch
3899 * always fits into a single wave.
3901 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
3902 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
3905 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
3906 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3909 static void emit_discard(struct ac_nir_context
*ctx
,
3910 const nir_intrinsic_instr
*instr
)
3914 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3915 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3916 get_src(ctx
, instr
->src
[0]),
3919 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3920 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
3923 ac_build_kill_if_false(&ctx
->ac
, cond
);
3927 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
3929 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
3930 "llvm.amdgcn.ps.live",
3931 ctx
->ac
.i1
, NULL
, 0,
3932 AC_FUNC_ATTR_READNONE
);
3933 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
3934 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
3938 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3940 LLVMValueRef result
;
3941 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3942 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3943 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3945 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3948 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3949 const nir_intrinsic_instr
*instr
)
3951 LLVMValueRef ptr
, result
;
3952 LLVMValueRef src
= get_src(ctx
->nir
, instr
->src
[0]);
3953 ptr
= build_gep_for_deref(ctx
->nir
, instr
->variables
[0]);
3955 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3956 LLVMValueRef src1
= get_src(ctx
->nir
, instr
->src
[1]);
3957 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3959 LLVMAtomicOrderingSequentiallyConsistent
,
3960 LLVMAtomicOrderingSequentiallyConsistent
,
3963 LLVMAtomicRMWBinOp op
;
3964 switch (instr
->intrinsic
) {
3965 case nir_intrinsic_var_atomic_add
:
3966 op
= LLVMAtomicRMWBinOpAdd
;
3968 case nir_intrinsic_var_atomic_umin
:
3969 op
= LLVMAtomicRMWBinOpUMin
;
3971 case nir_intrinsic_var_atomic_umax
:
3972 op
= LLVMAtomicRMWBinOpUMax
;
3974 case nir_intrinsic_var_atomic_imin
:
3975 op
= LLVMAtomicRMWBinOpMin
;
3977 case nir_intrinsic_var_atomic_imax
:
3978 op
= LLVMAtomicRMWBinOpMax
;
3980 case nir_intrinsic_var_atomic_and
:
3981 op
= LLVMAtomicRMWBinOpAnd
;
3983 case nir_intrinsic_var_atomic_or
:
3984 op
= LLVMAtomicRMWBinOpOr
;
3986 case nir_intrinsic_var_atomic_xor
:
3987 op
= LLVMAtomicRMWBinOpXor
;
3989 case nir_intrinsic_var_atomic_exchange
:
3990 op
= LLVMAtomicRMWBinOpXchg
;
3996 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3997 LLVMAtomicOrderingSequentiallyConsistent
,
4003 #define INTERP_CENTER 0
4004 #define INTERP_CENTROID 1
4005 #define INTERP_SAMPLE 2
4007 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
4008 enum glsl_interp_mode interp
, unsigned location
)
4011 case INTERP_MODE_FLAT
:
4014 case INTERP_MODE_SMOOTH
:
4015 case INTERP_MODE_NONE
:
4016 if (location
== INTERP_CENTER
)
4017 return ctx
->persp_center
;
4018 else if (location
== INTERP_CENTROID
)
4019 return ctx
->persp_centroid
;
4020 else if (location
== INTERP_SAMPLE
)
4021 return ctx
->persp_sample
;
4023 case INTERP_MODE_NOPERSPECTIVE
:
4024 if (location
== INTERP_CENTER
)
4025 return ctx
->linear_center
;
4026 else if (location
== INTERP_CENTROID
)
4027 return ctx
->linear_centroid
;
4028 else if (location
== INTERP_SAMPLE
)
4029 return ctx
->linear_sample
;
4035 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
4036 LLVMValueRef sample_id
)
4038 LLVMValueRef result
;
4039 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false));
4041 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
4042 const_array(ctx
->ac
.v2f32
, 64), "");
4044 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
4045 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
4050 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
4052 LLVMValueRef values
[2];
4054 values
[0] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
4055 values
[1] = emit_ffract(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
4056 return ac_build_gather_values(&ctx
->ac
, values
, 2);
4059 static LLVMValueRef
load_sample_mask_in(struct ac_nir_context
*ctx
)
4061 uint8_t log2_ps_iter_samples
= ctx
->nctx
->shader_info
->info
.ps
.force_persample
? ctx
->nctx
->options
->key
.fs
.log2_num_samples
: ctx
->nctx
->options
->key
.fs
.log2_ps_iter_samples
;
4063 /* The bit pattern matches that used by fixed function fragment
4065 static const uint16_t ps_iter_masks
[] = {
4066 0xffff, /* not used */
4072 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
4074 uint32_t ps_iter_mask
= ps_iter_masks
[log2_ps_iter_samples
];
4076 LLVMValueRef result
, sample_id
;
4077 sample_id
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4078 sample_id
= LLVMBuildShl(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, ps_iter_mask
, false), sample_id
, "");
4079 result
= LLVMBuildAnd(ctx
->ac
.builder
, sample_id
, ctx
->abi
->sample_coverage
, "");
4083 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
4084 const nir_intrinsic_instr
*instr
)
4086 LLVMValueRef result
[4];
4087 LLVMValueRef interp_param
, attr_number
;
4090 LLVMValueRef src_c0
= NULL
;
4091 LLVMValueRef src_c1
= NULL
;
4092 LLVMValueRef src0
= NULL
;
4093 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
4094 switch (instr
->intrinsic
) {
4095 case nir_intrinsic_interp_var_at_centroid
:
4096 location
= INTERP_CENTROID
;
4098 case nir_intrinsic_interp_var_at_sample
:
4099 case nir_intrinsic_interp_var_at_offset
:
4100 location
= INTERP_CENTER
;
4101 src0
= get_src(ctx
->nir
, instr
->src
[0]);
4107 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
4108 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_0
, ""));
4109 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->ac
.i32_1
, ""));
4110 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
4111 LLVMValueRef sample_position
;
4112 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
4114 /* fetch sample ID */
4115 sample_position
= load_sample_position(ctx
, src0
);
4117 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_0
, "");
4118 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
4119 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->ac
.i32_1
, "");
4120 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
4122 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
4123 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
4125 if (location
== INTERP_CENTER
) {
4126 LLVMValueRef ij_out
[2];
4127 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
->nir
, interp_param
);
4130 * take the I then J parameters, and the DDX/Y for it, and
4131 * calculate the IJ inputs for the interpolator.
4132 * temp1 = ddx * offset/sample.x + I;
4133 * interp_param.I = ddy * offset/sample.y + temp1;
4134 * temp1 = ddx * offset/sample.x + J;
4135 * interp_param.J = ddy * offset/sample.y + temp1;
4137 for (unsigned i
= 0; i
< 2; i
++) {
4138 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
4139 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
4140 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
4141 ddxy_out
, ix_ll
, "");
4142 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
4143 ddxy_out
, iy_ll
, "");
4144 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
4145 interp_param
, ix_ll
, "");
4146 LLVMValueRef temp1
, temp2
;
4148 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
4151 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
4152 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
4154 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
4155 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
4157 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
4158 temp2
, ctx
->ac
.i32
, "");
4160 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
4164 for (chan
= 0; chan
< 4; chan
++) {
4165 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
4168 interp_param
= LLVMBuildBitCast(ctx
->builder
,
4169 interp_param
, ctx
->ac
.v2f32
, "");
4170 LLVMValueRef i
= LLVMBuildExtractElement(
4171 ctx
->builder
, interp_param
, ctx
->ac
.i32_0
, "");
4172 LLVMValueRef j
= LLVMBuildExtractElement(
4173 ctx
->builder
, interp_param
, ctx
->ac
.i32_1
, "");
4175 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4176 llvm_chan
, attr_number
,
4177 ctx
->prim_mask
, i
, j
);
4179 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4180 LLVMConstInt(ctx
->ac
.i32
, 2, false),
4181 llvm_chan
, attr_number
,
4185 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
4186 instr
->variables
[0]->var
->data
.location_frac
);
4190 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
4192 LLVMValueRef gs_next_vertex
;
4193 LLVMValueRef can_emit
;
4195 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4197 assert(stream
== 0);
4199 /* Write vertex attribute values to GSVS ring */
4200 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
4201 ctx
->gs_next_vertex
,
4204 /* If this thread has already emitted the declared maximum number of
4205 * vertices, kill it: excessive vertex emissions are not supposed to
4206 * have any effect, and GS threads have no externally observable
4207 * effects other than emitting vertices.
4209 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
4210 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
4211 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4213 /* loop num outputs */
4215 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4216 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
4221 if (!(ctx
->output_mask
& (1ull << i
)))
4224 if (i
== VARYING_SLOT_CLIP_DIST0
) {
4225 /* pack clip and cull into a single set of slots */
4226 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4230 for (unsigned j
= 0; j
< length
; j
++) {
4231 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
4233 LLVMValueRef voffset
= LLVMConstInt(ctx
->ac
.i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
4234 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
4235 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
4237 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
4239 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
4241 voffset
, ctx
->gs2vs_offset
, 0,
4247 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
4249 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
4251 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
4255 visit_end_primitive(struct ac_shader_abi
*abi
, unsigned stream
)
4257 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4258 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8), ctx
->gs_wave_id
);
4262 load_tess_coord(struct ac_shader_abi
*abi
, LLVMTypeRef type
,
4263 unsigned num_components
)
4265 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4267 LLVMValueRef coord
[4] = {
4274 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
4275 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->ac
.f32_1
,
4276 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
4278 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, num_components
);
4279 return LLVMBuildBitCast(ctx
->builder
, result
, type
, "");
4283 load_patch_vertices_in(struct ac_shader_abi
*abi
)
4285 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4286 return LLVMConstInt(ctx
->ac
.i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
4289 static void visit_intrinsic(struct ac_nir_context
*ctx
,
4290 nir_intrinsic_instr
*instr
)
4292 LLVMValueRef result
= NULL
;
4294 switch (instr
->intrinsic
) {
4295 case nir_intrinsic_ballot
:
4296 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4298 case nir_intrinsic_read_invocation
:
4299 case nir_intrinsic_read_first_invocation
: {
4300 LLVMValueRef args
[2];
4303 args
[0] = get_src(ctx
, instr
->src
[0]);
4306 const char *intr_name
;
4307 if (instr
->intrinsic
== nir_intrinsic_read_invocation
) {
4309 intr_name
= "llvm.amdgcn.readlane";
4312 args
[1] = get_src(ctx
, instr
->src
[1]);
4315 intr_name
= "llvm.amdgcn.readfirstlane";
4318 /* We currently have no other way to prevent LLVM from lifting the icmp
4319 * calls to a dominating basic block.
4321 ac_build_optimization_barrier(&ctx
->ac
, &args
[0]);
4323 result
= ac_build_intrinsic(&ctx
->ac
, intr_name
,
4324 ctx
->ac
.i32
, args
, num_args
,
4325 AC_FUNC_ATTR_READNONE
|
4326 AC_FUNC_ATTR_CONVERGENT
);
4329 case nir_intrinsic_load_subgroup_invocation
:
4330 result
= ac_get_thread_id(&ctx
->ac
);
4332 case nir_intrinsic_load_work_group_id
: {
4333 LLVMValueRef values
[3];
4335 for (int i
= 0; i
< 3; i
++) {
4336 values
[i
] = ctx
->nctx
->workgroup_ids
[i
] ?
4337 ctx
->nctx
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
4340 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
4343 case nir_intrinsic_load_base_vertex
: {
4344 result
= ctx
->abi
->base_vertex
;
4347 case nir_intrinsic_load_vertex_id_zero_base
: {
4348 result
= ctx
->abi
->vertex_id
;
4351 case nir_intrinsic_load_local_invocation_id
: {
4352 result
= ctx
->nctx
->local_invocation_ids
;
4355 case nir_intrinsic_load_base_instance
:
4356 result
= ctx
->abi
->start_instance
;
4358 case nir_intrinsic_load_draw_id
:
4359 result
= ctx
->abi
->draw_id
;
4361 case nir_intrinsic_load_view_index
:
4362 result
= ctx
->nctx
->view_index
? ctx
->nctx
->view_index
: ctx
->ac
.i32_0
;
4364 case nir_intrinsic_load_invocation_id
:
4365 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4366 result
= unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
4368 result
= ctx
->abi
->gs_invocation_id
;
4370 case nir_intrinsic_load_primitive_id
:
4371 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4372 result
= ctx
->abi
->gs_prim_id
;
4373 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
4374 result
= ctx
->abi
->tcs_patch_id
;
4375 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4376 result
= ctx
->abi
->tes_patch_id
;
4378 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
4380 case nir_intrinsic_load_sample_id
:
4381 result
= unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
4383 case nir_intrinsic_load_sample_pos
:
4384 result
= load_sample_pos(ctx
);
4386 case nir_intrinsic_load_sample_mask_in
:
4388 result
= load_sample_mask_in(ctx
);
4390 result
= ctx
->abi
->sample_coverage
;
4392 case nir_intrinsic_load_frag_coord
: {
4393 LLVMValueRef values
[4] = {
4394 ctx
->abi
->frag_pos
[0],
4395 ctx
->abi
->frag_pos
[1],
4396 ctx
->abi
->frag_pos
[2],
4397 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
4399 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
4402 case nir_intrinsic_load_front_face
:
4403 result
= ctx
->abi
->front_face
;
4405 case nir_intrinsic_load_helper_invocation
:
4406 result
= visit_load_helper_invocation(ctx
);
4408 case nir_intrinsic_load_instance_id
:
4409 result
= ctx
->abi
->instance_id
;
4411 case nir_intrinsic_load_num_work_groups
:
4412 result
= ctx
->nctx
->num_work_groups
;
4414 case nir_intrinsic_load_local_invocation_index
:
4415 result
= visit_load_local_invocation_index(ctx
->nctx
);
4417 case nir_intrinsic_load_push_constant
:
4418 result
= visit_load_push_constant(ctx
->nctx
, instr
);
4420 case nir_intrinsic_vulkan_resource_index
:
4421 result
= visit_vulkan_resource_index(ctx
->nctx
, instr
);
4423 case nir_intrinsic_vulkan_resource_reindex
:
4424 result
= visit_vulkan_resource_reindex(ctx
->nctx
, instr
);
4426 case nir_intrinsic_store_ssbo
:
4427 visit_store_ssbo(ctx
, instr
);
4429 case nir_intrinsic_load_ssbo
:
4430 result
= visit_load_buffer(ctx
, instr
);
4432 case nir_intrinsic_ssbo_atomic_add
:
4433 case nir_intrinsic_ssbo_atomic_imin
:
4434 case nir_intrinsic_ssbo_atomic_umin
:
4435 case nir_intrinsic_ssbo_atomic_imax
:
4436 case nir_intrinsic_ssbo_atomic_umax
:
4437 case nir_intrinsic_ssbo_atomic_and
:
4438 case nir_intrinsic_ssbo_atomic_or
:
4439 case nir_intrinsic_ssbo_atomic_xor
:
4440 case nir_intrinsic_ssbo_atomic_exchange
:
4441 case nir_intrinsic_ssbo_atomic_comp_swap
:
4442 result
= visit_atomic_ssbo(ctx
, instr
);
4444 case nir_intrinsic_load_ubo
:
4445 result
= visit_load_ubo_buffer(ctx
, instr
);
4447 case nir_intrinsic_get_buffer_size
:
4448 result
= visit_get_buffer_size(ctx
, instr
);
4450 case nir_intrinsic_load_var
:
4451 result
= visit_load_var(ctx
, instr
);
4453 case nir_intrinsic_store_var
:
4454 visit_store_var(ctx
, instr
);
4456 case nir_intrinsic_image_load
:
4457 result
= visit_image_load(ctx
, instr
);
4459 case nir_intrinsic_image_store
:
4460 visit_image_store(ctx
, instr
);
4462 case nir_intrinsic_image_atomic_add
:
4463 case nir_intrinsic_image_atomic_min
:
4464 case nir_intrinsic_image_atomic_max
:
4465 case nir_intrinsic_image_atomic_and
:
4466 case nir_intrinsic_image_atomic_or
:
4467 case nir_intrinsic_image_atomic_xor
:
4468 case nir_intrinsic_image_atomic_exchange
:
4469 case nir_intrinsic_image_atomic_comp_swap
:
4470 result
= visit_image_atomic(ctx
, instr
);
4472 case nir_intrinsic_image_size
:
4473 result
= visit_image_size(ctx
, instr
);
4475 case nir_intrinsic_discard
:
4476 case nir_intrinsic_discard_if
:
4477 emit_discard(ctx
, instr
);
4479 case nir_intrinsic_memory_barrier
:
4480 case nir_intrinsic_group_memory_barrier
:
4481 case nir_intrinsic_memory_barrier_atomic_counter
:
4482 case nir_intrinsic_memory_barrier_buffer
:
4483 case nir_intrinsic_memory_barrier_image
:
4484 case nir_intrinsic_memory_barrier_shared
:
4485 emit_membar(ctx
->nctx
, instr
);
4487 case nir_intrinsic_barrier
:
4488 emit_barrier(&ctx
->ac
, ctx
->stage
);
4490 case nir_intrinsic_var_atomic_add
:
4491 case nir_intrinsic_var_atomic_imin
:
4492 case nir_intrinsic_var_atomic_umin
:
4493 case nir_intrinsic_var_atomic_imax
:
4494 case nir_intrinsic_var_atomic_umax
:
4495 case nir_intrinsic_var_atomic_and
:
4496 case nir_intrinsic_var_atomic_or
:
4497 case nir_intrinsic_var_atomic_xor
:
4498 case nir_intrinsic_var_atomic_exchange
:
4499 case nir_intrinsic_var_atomic_comp_swap
:
4500 result
= visit_var_atomic(ctx
->nctx
, instr
);
4502 case nir_intrinsic_interp_var_at_centroid
:
4503 case nir_intrinsic_interp_var_at_sample
:
4504 case nir_intrinsic_interp_var_at_offset
:
4505 result
= visit_interp(ctx
->nctx
, instr
);
4507 case nir_intrinsic_emit_vertex
:
4508 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->outputs
);
4510 case nir_intrinsic_end_primitive
:
4511 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
4513 case nir_intrinsic_load_tess_coord
: {
4514 LLVMTypeRef type
= ctx
->nctx
?
4515 get_def_type(ctx
->nctx
->nir
, &instr
->dest
.ssa
) :
4517 result
= ctx
->abi
->load_tess_coord(ctx
->abi
, type
, instr
->num_components
);
4520 case nir_intrinsic_load_tess_level_outer
:
4521 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
4523 case nir_intrinsic_load_tess_level_inner
:
4524 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
4526 case nir_intrinsic_load_patch_vertices_in
:
4527 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
4529 case nir_intrinsic_vote_all
: {
4530 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4531 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4534 case nir_intrinsic_vote_any
: {
4535 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4536 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4539 case nir_intrinsic_vote_eq
: {
4540 LLVMValueRef tmp
= ac_build_vote_eq(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4541 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4545 fprintf(stderr
, "Unknown intrinsic: ");
4546 nir_print_instr(&instr
->instr
, stderr
);
4547 fprintf(stderr
, "\n");
4551 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4555 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
4556 LLVMValueRef buffer_ptr
, bool write
)
4558 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4559 LLVMValueRef result
;
4561 LLVMSetMetadata(buffer_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
4563 result
= LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4564 LLVMSetMetadata(result
, ctx
->ac
.invariant_load_md_kind
, ctx
->ac
.empty_md
);
4569 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef buffer_ptr
)
4571 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4572 LLVMValueRef result
;
4574 LLVMSetMetadata(buffer_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
4576 result
= LLVMBuildLoad(ctx
->builder
, buffer_ptr
, "");
4577 LLVMSetMetadata(result
, ctx
->ac
.invariant_load_md_kind
, ctx
->ac
.empty_md
);
4582 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
4583 unsigned descriptor_set
,
4584 unsigned base_index
,
4585 unsigned constant_index
,
4587 enum ac_descriptor_type desc_type
,
4588 bool image
, bool write
)
4590 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
4591 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
4592 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
4593 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
4594 unsigned offset
= binding
->offset
;
4595 unsigned stride
= binding
->size
;
4597 LLVMBuilderRef builder
= ctx
->builder
;
4600 assert(base_index
< layout
->binding_count
);
4602 switch (desc_type
) {
4604 type
= ctx
->ac
.v8i32
;
4608 type
= ctx
->ac
.v8i32
;
4612 case AC_DESC_SAMPLER
:
4613 type
= ctx
->ac
.v4i32
;
4614 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4619 case AC_DESC_BUFFER
:
4620 type
= ctx
->ac
.v4i32
;
4624 unreachable("invalid desc_type\n");
4627 offset
+= constant_index
* stride
;
4629 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4630 (!index
|| binding
->immutable_samplers_equal
)) {
4631 if (binding
->immutable_samplers_equal
)
4634 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4636 LLVMValueRef constants
[] = {
4637 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
4638 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
4639 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
4640 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
4642 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4645 assert(stride
% type_size
== 0);
4648 index
= ctx
->ac
.i32_0
;
4650 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
4652 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4653 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4655 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
4658 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4659 const nir_deref_var
*deref
,
4660 enum ac_descriptor_type desc_type
,
4661 const nir_tex_instr
*tex_instr
,
4662 bool image
, bool write
)
4664 LLVMValueRef index
= NULL
;
4665 unsigned constant_index
= 0;
4666 unsigned descriptor_set
;
4667 unsigned base_index
;
4670 assert(tex_instr
&& !image
);
4672 base_index
= tex_instr
->sampler_index
;
4674 const nir_deref
*tail
= &deref
->deref
;
4675 while (tail
->child
) {
4676 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
4677 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
4682 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4684 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4685 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
4687 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4688 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4693 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4696 constant_index
+= child
->base_offset
* array_size
;
4698 tail
= &child
->deref
;
4700 descriptor_set
= deref
->var
->data
.descriptor_set
;
4701 base_index
= deref
->var
->data
.binding
;
4704 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4707 constant_index
, index
,
4708 desc_type
, image
, write
);
4711 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
4712 struct ac_image_args
*args
,
4713 const nir_tex_instr
*instr
,
4715 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4716 LLVMValueRef
*param
, unsigned count
,
4719 unsigned is_rect
= 0;
4720 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4722 if (op
== nir_texop_lod
)
4724 /* Pad to power of two vector */
4725 while (count
< util_next_power_of_two(count
))
4726 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4729 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
4731 args
->addr
= param
[0];
4733 args
->resource
= res_ptr
;
4734 args
->sampler
= samp_ptr
;
4736 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4737 args
->addr
= param
[0];
4741 args
->dmask
= dmask
;
4742 args
->unorm
= is_rect
;
4746 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4749 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4750 * filtering manually. The driver sets img7 to a mask clearing
4751 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4752 * s_and_b32 samp0, samp0, img7
4755 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4757 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4758 LLVMValueRef res
, LLVMValueRef samp
)
4760 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4761 LLVMValueRef img7
, samp0
;
4763 if (ctx
->ac
.chip_class
>= VI
)
4766 img7
= LLVMBuildExtractElement(builder
, res
,
4767 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4768 samp0
= LLVMBuildExtractElement(builder
, samp
,
4769 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4770 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4771 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4772 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4775 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4776 nir_tex_instr
*instr
,
4777 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4778 LLVMValueRef
*fmask_ptr
)
4780 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4781 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
4783 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
4786 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
4788 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
4789 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4790 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4792 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4793 instr
->op
== nir_texop_samples_identical
))
4794 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
4797 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4800 coord
= ac_to_float(ctx
, coord
);
4801 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4802 coord
= ac_to_integer(ctx
, coord
);
4806 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4808 LLVMValueRef result
= NULL
;
4809 struct ac_image_args args
= { 0 };
4810 unsigned dmask
= 0xf;
4811 LLVMValueRef address
[16];
4812 LLVMValueRef coords
[5];
4813 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4814 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4815 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4816 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4817 LLVMValueRef derivs
[6];
4818 unsigned chan
, count
= 0;
4819 unsigned const_src
= 0, num_deriv_comp
= 0;
4820 bool lod_is_zero
= false;
4822 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4824 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4825 switch (instr
->src
[i
].src_type
) {
4826 case nir_tex_src_coord
:
4827 coord
= get_src(ctx
, instr
->src
[i
].src
);
4829 case nir_tex_src_projector
:
4831 case nir_tex_src_comparator
:
4832 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4834 case nir_tex_src_offset
:
4835 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4838 case nir_tex_src_bias
:
4839 bias
= get_src(ctx
, instr
->src
[i
].src
);
4841 case nir_tex_src_lod
: {
4842 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4844 if (val
&& val
->i32
[0] == 0)
4846 lod
= get_src(ctx
, instr
->src
[i
].src
);
4849 case nir_tex_src_ms_index
:
4850 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4852 case nir_tex_src_ms_mcs
:
4854 case nir_tex_src_ddx
:
4855 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4856 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4858 case nir_tex_src_ddy
:
4859 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4861 case nir_tex_src_texture_offset
:
4862 case nir_tex_src_sampler_offset
:
4863 case nir_tex_src_plane
:
4869 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4870 result
= get_buffer_size(ctx
, res_ptr
, true);
4874 if (instr
->op
== nir_texop_texture_samples
) {
4875 LLVMValueRef res
, samples
, is_msaa
;
4876 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
4877 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4878 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4879 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4880 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4881 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4882 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4883 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4884 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4886 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4887 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4888 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4889 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4890 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4892 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4899 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4900 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4902 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4903 LLVMValueRef offset
[3], pack
;
4904 for (chan
= 0; chan
< 3; ++chan
)
4905 offset
[chan
] = ctx
->ac
.i32_0
;
4908 for (chan
= 0; chan
< ac_get_llvm_num_components(offsets
); chan
++) {
4909 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
4910 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4911 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4913 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4914 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4916 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4917 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4918 address
[count
++] = pack
;
4921 /* pack LOD bias value */
4922 if (instr
->op
== nir_texop_txb
&& bias
) {
4923 address
[count
++] = bias
;
4926 /* Pack depth comparison value */
4927 if (instr
->is_shadow
&& comparator
) {
4928 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
4929 ac_llvm_extract_elem(&ctx
->ac
, comparator
, 0));
4931 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
4932 * so the depth comparison value isn't clamped for Z16 and
4933 * Z24 anymore. Do it manually here.
4935 * It's unnecessary if the original texture format was
4936 * Z32_FLOAT, but we don't know that here.
4938 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
4939 z
= ac_build_clamp(&ctx
->ac
, z
);
4941 address
[count
++] = z
;
4944 /* pack derivatives */
4946 int num_src_deriv_channels
, num_dest_deriv_channels
;
4947 switch (instr
->sampler_dim
) {
4948 case GLSL_SAMPLER_DIM_3D
:
4949 case GLSL_SAMPLER_DIM_CUBE
:
4951 num_src_deriv_channels
= 3;
4952 num_dest_deriv_channels
= 3;
4954 case GLSL_SAMPLER_DIM_2D
:
4956 num_src_deriv_channels
= 2;
4957 num_dest_deriv_channels
= 2;
4960 case GLSL_SAMPLER_DIM_1D
:
4961 num_src_deriv_channels
= 1;
4962 if (ctx
->ac
.chip_class
>= GFX9
) {
4963 num_dest_deriv_channels
= 2;
4966 num_dest_deriv_channels
= 1;
4972 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4973 derivs
[i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4974 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4976 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4977 derivs
[i
] = ctx
->ac
.f32_0
;
4978 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4982 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4983 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4984 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
4985 if (instr
->coord_components
== 3)
4986 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4987 ac_prepare_cube_coords(&ctx
->ac
,
4988 instr
->op
== nir_texop_txd
, instr
->is_array
,
4989 instr
->op
== nir_texop_lod
, coords
, derivs
);
4995 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4996 address
[count
++] = derivs
[i
];
4999 /* Pack texture coordinates */
5001 address
[count
++] = coords
[0];
5002 if (instr
->coord_components
> 1) {
5003 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
5004 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
5006 address
[count
++] = coords
[1];
5008 if (instr
->coord_components
> 2) {
5009 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
5010 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
5011 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
5012 instr
->op
!= nir_texop_txf
) {
5013 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
5015 address
[count
++] = coords
[2];
5018 if (ctx
->ac
.chip_class
>= GFX9
) {
5019 LLVMValueRef filler
;
5020 if (instr
->op
== nir_texop_txf
)
5021 filler
= ctx
->ac
.i32_0
;
5023 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
5025 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
5026 /* No nir_texop_lod, because it does not take a slice
5027 * even with array textures. */
5028 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
5029 address
[count
] = address
[count
- 1];
5030 address
[count
- 1] = filler
;
5033 address
[count
++] = filler
;
5039 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
5040 instr
->op
== nir_texop_txf
)) {
5041 address
[count
++] = lod
;
5042 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
5043 address
[count
++] = sample_index
;
5044 } else if(instr
->op
== nir_texop_txs
) {
5047 address
[count
++] = lod
;
5049 address
[count
++] = ctx
->ac
.i32_0
;
5052 for (chan
= 0; chan
< count
; chan
++) {
5053 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
5054 address
[chan
], ctx
->ac
.i32
, "");
5057 if (instr
->op
== nir_texop_samples_identical
) {
5058 LLVMValueRef txf_address
[4];
5059 struct ac_image_args txf_args
= { 0 };
5060 unsigned txf_count
= count
;
5061 memcpy(txf_address
, address
, sizeof(txf_address
));
5063 if (!instr
->is_array
)
5064 txf_address
[2] = ctx
->ac
.i32_0
;
5065 txf_address
[3] = ctx
->ac
.i32_0
;
5067 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
5069 txf_address
, txf_count
, 0xf);
5071 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
5073 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
5074 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
5078 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
5079 instr
->op
!= nir_texop_txs
) {
5080 unsigned sample_chan
= instr
->is_array
? 3 : 2;
5081 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
5084 instr
->is_array
? address
[2] : NULL
,
5085 address
[sample_chan
],
5089 if (offsets
&& instr
->op
== nir_texop_txf
) {
5090 nir_const_value
*const_offset
=
5091 nir_src_as_const_value(instr
->src
[const_src
].src
);
5092 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
5093 assert(const_offset
);
5094 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
5095 if (num_offsets
> 2)
5096 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
5097 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
5098 if (num_offsets
> 1)
5099 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
5100 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
5101 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
5102 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
5106 /* TODO TG4 support */
5107 if (instr
->op
== nir_texop_tg4
) {
5108 if (instr
->is_shadow
)
5111 dmask
= 1 << instr
->component
;
5113 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
5114 res_ptr
, samp_ptr
, address
, count
, dmask
);
5116 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
5118 if (instr
->op
== nir_texop_query_levels
)
5119 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
5120 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
5121 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
5122 instr
->op
!= nir_texop_tg4
)
5123 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
5124 else if (instr
->op
== nir_texop_txs
&&
5125 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
5127 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
5128 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
5129 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
5130 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
5131 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
5132 } else if (ctx
->ac
.chip_class
>= GFX9
&&
5133 instr
->op
== nir_texop_txs
&&
5134 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
5136 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
5137 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
5138 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
5140 } else if (instr
->dest
.ssa
.num_components
!= 4)
5141 result
= trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
5145 assert(instr
->dest
.is_ssa
);
5146 result
= ac_to_integer(&ctx
->ac
, result
);
5147 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
5152 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
5154 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
5155 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
5157 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
5158 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
5161 static void visit_post_phi(struct ac_nir_context
*ctx
,
5162 nir_phi_instr
*instr
,
5163 LLVMValueRef llvm_phi
)
5165 nir_foreach_phi_src(src
, instr
) {
5166 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
5167 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
5169 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
5173 static void phi_post_pass(struct ac_nir_context
*ctx
)
5175 struct hash_entry
*entry
;
5176 hash_table_foreach(ctx
->phis
, entry
) {
5177 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
5178 (LLVMValueRef
)entry
->data
);
5183 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
5184 const nir_ssa_undef_instr
*instr
)
5186 unsigned num_components
= instr
->def
.num_components
;
5187 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
5190 if (num_components
== 1)
5191 undef
= LLVMGetUndef(type
);
5193 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
5195 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
5198 static void visit_jump(struct ac_nir_context
*ctx
,
5199 const nir_jump_instr
*instr
)
5201 switch (instr
->type
) {
5202 case nir_jump_break
:
5203 LLVMBuildBr(ctx
->ac
.builder
, ctx
->break_block
);
5204 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5206 case nir_jump_continue
:
5207 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5208 LLVMClearInsertionPosition(ctx
->ac
.builder
);
5211 fprintf(stderr
, "Unknown NIR jump instr: ");
5212 nir_print_instr(&instr
->instr
, stderr
);
5213 fprintf(stderr
, "\n");
5218 static void visit_cf_list(struct ac_nir_context
*ctx
,
5219 struct exec_list
*list
);
5221 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5223 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
5224 nir_foreach_instr(instr
, block
)
5226 switch (instr
->type
) {
5227 case nir_instr_type_alu
:
5228 visit_alu(ctx
, nir_instr_as_alu(instr
));
5230 case nir_instr_type_load_const
:
5231 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5233 case nir_instr_type_intrinsic
:
5234 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5236 case nir_instr_type_tex
:
5237 visit_tex(ctx
, nir_instr_as_tex(instr
));
5239 case nir_instr_type_phi
:
5240 visit_phi(ctx
, nir_instr_as_phi(instr
));
5242 case nir_instr_type_ssa_undef
:
5243 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5245 case nir_instr_type_jump
:
5246 visit_jump(ctx
, nir_instr_as_jump(instr
));
5249 fprintf(stderr
, "Unknown NIR instr type: ");
5250 nir_print_instr(instr
, stderr
);
5251 fprintf(stderr
, "\n");
5256 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
5259 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5261 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5263 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5264 LLVMBasicBlockRef merge_block
=
5265 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5266 LLVMBasicBlockRef if_block
=
5267 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5268 LLVMBasicBlockRef else_block
= merge_block
;
5269 if (!exec_list_is_empty(&if_stmt
->else_list
))
5270 else_block
= LLVMAppendBasicBlockInContext(
5271 ctx
->ac
.context
, fn
, "");
5273 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, value
,
5275 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, else_block
);
5277 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
5278 visit_cf_list(ctx
, &if_stmt
->then_list
);
5279 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5280 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5282 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5283 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, else_block
);
5284 visit_cf_list(ctx
, &if_stmt
->else_list
);
5285 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5286 LLVMBuildBr(ctx
->ac
.builder
, merge_block
);
5289 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, merge_block
);
5292 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5294 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->ac
.builder
));
5295 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
5296 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
5298 ctx
->continue_block
=
5299 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5301 LLVMAppendBasicBlockInContext(ctx
->ac
.context
, fn
, "");
5303 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5304 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->continue_block
);
5305 visit_cf_list(ctx
, &loop
->body
);
5307 if (LLVMGetInsertBlock(ctx
->ac
.builder
))
5308 LLVMBuildBr(ctx
->ac
.builder
, ctx
->continue_block
);
5309 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ctx
->break_block
);
5311 ctx
->continue_block
= continue_parent
;
5312 ctx
->break_block
= break_parent
;
5315 static void visit_cf_list(struct ac_nir_context
*ctx
,
5316 struct exec_list
*list
)
5318 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5320 switch (node
->type
) {
5321 case nir_cf_node_block
:
5322 visit_block(ctx
, nir_cf_node_as_block(node
));
5325 case nir_cf_node_if
:
5326 visit_if(ctx
, nir_cf_node_as_if(node
));
5329 case nir_cf_node_loop
:
5330 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5340 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
5341 struct nir_variable
*variable
)
5343 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
5344 LLVMValueRef t_offset
;
5345 LLVMValueRef t_list
;
5347 LLVMValueRef buffer_index
;
5348 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
5349 int idx
= variable
->data
.location
;
5350 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
5352 variable
->data
.driver_location
= idx
* 4;
5354 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
5355 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << (index
+ i
))) {
5356 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.instance_id
,
5357 ctx
->abi
.start_instance
, "");
5358 if (ctx
->options
->key
.vs
.as_ls
) {
5359 ctx
->shader_info
->vs
.vgpr_comp_cnt
=
5360 MAX2(2, ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5362 ctx
->shader_info
->vs
.vgpr_comp_cnt
=
5363 MAX2(1, ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5366 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->abi
.vertex_id
,
5367 ctx
->abi
.base_vertex
, "");
5368 t_offset
= LLVMConstInt(ctx
->ac
.i32
, index
+ i
, false);
5370 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
5372 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
5377 for (unsigned chan
= 0; chan
< 4; chan
++) {
5378 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5379 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
5380 ac_to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
5381 input
, llvm_chan
, ""));
5386 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
5388 LLVMValueRef interp_param
,
5389 LLVMValueRef prim_mask
,
5390 LLVMValueRef result
[4])
5392 LLVMValueRef attr_number
;
5395 bool interp
= interp_param
!= NULL
;
5397 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
5399 /* fs.constant returns the param from the middle vertex, so it's not
5400 * really useful for flat shading. It's meant to be used for custom
5401 * interpolation (but the intrinsic can't fetch from the other two
5404 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
5405 * to do the right thing. The only reason we use fs.constant is that
5406 * fs.interp cannot be used on integers, because they can be equal
5410 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
5413 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5415 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
5419 for (chan
= 0; chan
< 4; chan
++) {
5420 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
5423 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5428 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5429 LLVMConstInt(ctx
->ac
.i32
, 2, false),
5438 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
5439 struct nir_variable
*variable
)
5441 int idx
= variable
->data
.location
;
5442 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5443 LLVMValueRef interp
;
5445 variable
->data
.driver_location
= idx
* 4;
5446 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
5448 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
5449 unsigned interp_type
;
5450 if (variable
->data
.sample
) {
5451 interp_type
= INTERP_SAMPLE
;
5452 ctx
->shader_info
->info
.ps
.force_persample
= true;
5453 } else if (variable
->data
.centroid
)
5454 interp_type
= INTERP_CENTROID
;
5456 interp_type
= INTERP_CENTER
;
5458 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
5462 for (unsigned i
= 0; i
< attrib_count
; ++i
)
5463 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
5468 handle_vs_inputs(struct nir_to_llvm_context
*ctx
,
5469 struct nir_shader
*nir
) {
5470 nir_foreach_variable(variable
, &nir
->inputs
)
5471 handle_vs_input_decl(ctx
, variable
);
5475 prepare_interp_optimize(struct nir_to_llvm_context
*ctx
,
5476 struct nir_shader
*nir
)
5478 if (!ctx
->options
->key
.fs
.multisample
)
5481 bool uses_center
= false;
5482 bool uses_centroid
= false;
5483 nir_foreach_variable(variable
, &nir
->inputs
) {
5484 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
5485 variable
->data
.sample
)
5488 if (variable
->data
.centroid
)
5489 uses_centroid
= true;
5494 if (uses_center
&& uses_centroid
) {
5495 LLVMValueRef sel
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, ctx
->prim_mask
, ctx
->ac
.i32_0
, "");
5496 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
5497 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
5502 handle_fs_inputs(struct nir_to_llvm_context
*ctx
,
5503 struct nir_shader
*nir
)
5505 prepare_interp_optimize(ctx
, nir
);
5507 nir_foreach_variable(variable
, &nir
->inputs
)
5508 handle_fs_input_decl(ctx
, variable
);
5512 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
5513 ctx
->shader_info
->info
.needs_multiview_view_index
)
5514 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
5516 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
5517 LLVMValueRef interp_param
;
5518 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
5520 if (!(ctx
->input_mask
& (1ull << i
)))
5523 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
5524 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
5525 interp_param
= *inputs
;
5526 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
5530 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
5532 } else if (i
== VARYING_SLOT_POS
) {
5533 for(int i
= 0; i
< 3; ++i
)
5534 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
5536 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
5537 ctx
->abi
.frag_pos
[3]);
5540 ctx
->shader_info
->fs
.num_interp
= index
;
5541 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
5542 ctx
->shader_info
->fs
.has_pcoord
= true;
5543 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
5544 ctx
->shader_info
->fs
.prim_id_input
= true;
5545 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
5546 ctx
->shader_info
->fs
.layer_input
= true;
5547 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
5549 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
5550 ctx
->view_index
= ctx
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5554 ac_build_alloca(struct ac_llvm_context
*ac
,
5558 LLVMBuilderRef builder
= ac
->builder
;
5559 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
5560 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5561 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
5562 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
5563 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
5567 LLVMPositionBuilderBefore(first_builder
, first_instr
);
5569 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
5572 res
= LLVMBuildAlloca(first_builder
, type
, name
);
5573 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
5575 LLVMDisposeBuilder(first_builder
);
5580 static LLVMValueRef
si_build_alloca_undef(struct ac_llvm_context
*ac
,
5584 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
5585 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
5590 scan_shader_output_decl(struct nir_to_llvm_context
*ctx
,
5591 struct nir_variable
*variable
,
5592 struct nir_shader
*shader
,
5593 gl_shader_stage stage
)
5595 int idx
= variable
->data
.location
+ variable
->data
.index
;
5596 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5597 uint64_t mask_attribs
;
5599 variable
->data
.driver_location
= idx
* 4;
5601 /* tess ctrl has it's own load/store paths for outputs */
5602 if (stage
== MESA_SHADER_TESS_CTRL
)
5605 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
5606 if (stage
== MESA_SHADER_VERTEX
||
5607 stage
== MESA_SHADER_TESS_EVAL
||
5608 stage
== MESA_SHADER_GEOMETRY
) {
5609 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5610 int length
= shader
->info
.clip_distance_array_size
+
5611 shader
->info
.cull_distance_array_size
;
5612 if (stage
== MESA_SHADER_VERTEX
) {
5613 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5614 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5616 if (stage
== MESA_SHADER_TESS_EVAL
) {
5617 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
5618 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
5625 mask_attribs
= 1ull << idx
;
5629 ctx
->output_mask
|= mask_attribs
;
5633 handle_shader_output_decl(struct ac_nir_context
*ctx
,
5634 struct nir_shader
*nir
,
5635 struct nir_variable
*variable
)
5637 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5638 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5640 /* tess ctrl has it's own load/store paths for outputs */
5641 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
5644 if (ctx
->stage
== MESA_SHADER_VERTEX
||
5645 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
5646 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5647 int idx
= variable
->data
.location
+ variable
->data
.index
;
5648 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5649 int length
= nir
->info
.clip_distance_array_size
+
5650 nir
->info
.cull_distance_array_size
;
5659 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5660 for (unsigned chan
= 0; chan
< 4; chan
++) {
5661 ctx
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5662 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5668 glsl_base_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5669 enum glsl_base_type type
)
5673 case GLSL_TYPE_UINT
:
5674 case GLSL_TYPE_BOOL
:
5675 case GLSL_TYPE_SUBROUTINE
:
5677 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
5679 case GLSL_TYPE_INT64
:
5680 case GLSL_TYPE_UINT64
:
5682 case GLSL_TYPE_DOUBLE
:
5685 unreachable("unknown GLSL type");
5690 glsl_to_llvm_type(struct nir_to_llvm_context
*ctx
,
5691 const struct glsl_type
*type
)
5693 if (glsl_type_is_scalar(type
)) {
5694 return glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
));
5697 if (glsl_type_is_vector(type
)) {
5698 return LLVMVectorType(
5699 glsl_base_to_llvm_type(ctx
, glsl_get_base_type(type
)),
5700 glsl_get_vector_elements(type
));
5703 if (glsl_type_is_matrix(type
)) {
5704 return LLVMArrayType(
5705 glsl_to_llvm_type(ctx
, glsl_get_column_type(type
)),
5706 glsl_get_matrix_columns(type
));
5709 if (glsl_type_is_array(type
)) {
5710 return LLVMArrayType(
5711 glsl_to_llvm_type(ctx
, glsl_get_array_element(type
)),
5712 glsl_get_length(type
));
5715 assert(glsl_type_is_struct(type
));
5717 LLVMTypeRef member_types
[glsl_get_length(type
)];
5719 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5721 glsl_to_llvm_type(ctx
,
5722 glsl_get_struct_field(type
, i
));
5725 return LLVMStructTypeInContext(ctx
->context
, member_types
,
5726 glsl_get_length(type
), false);
5730 setup_locals(struct ac_nir_context
*ctx
,
5731 struct nir_function
*func
)
5734 ctx
->num_locals
= 0;
5735 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5736 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5737 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5738 variable
->data
.location_frac
= 0;
5739 ctx
->num_locals
+= attrib_count
;
5741 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5745 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5746 for (j
= 0; j
< 4; j
++) {
5747 ctx
->locals
[i
* 4 + j
] =
5748 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5754 setup_shared(struct ac_nir_context
*ctx
,
5755 struct nir_shader
*nir
)
5757 nir_foreach_variable(variable
, &nir
->shared
) {
5758 LLVMValueRef shared
=
5759 LLVMAddGlobalInAddressSpace(
5760 ctx
->ac
.module
, glsl_to_llvm_type(ctx
->nctx
, variable
->type
),
5761 variable
->name
? variable
->name
: "",
5762 AC_LOCAL_ADDR_SPACE
);
5763 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
5768 emit_float_saturate(struct ac_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
5770 v
= ac_to_float(ctx
, v
);
5771 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
5772 return emit_intrin_2f_param(ctx
, "llvm.minnum", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5776 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5777 LLVMValueRef src0
, LLVMValueRef src1
)
5779 LLVMValueRef const16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
5780 LLVMValueRef comp
[2];
5782 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5783 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "");
5784 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5785 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5788 /* Initialize arguments for the shader export intrinsic */
5790 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5791 LLVMValueRef
*values
,
5793 struct ac_export_args
*args
)
5795 /* Default is 0xf. Adjusted below depending on the format. */
5796 args
->enabled_channels
= 0xf;
5798 /* Specify whether the EXEC mask represents the valid mask */
5799 args
->valid_mask
= 0;
5801 /* Specify whether this is the last export */
5804 /* Specify the target we are exporting */
5805 args
->target
= target
;
5807 args
->compr
= false;
5808 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
5809 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
5810 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
5811 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
5816 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5817 LLVMValueRef val
[4];
5818 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5819 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5820 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5821 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
5823 switch(col_format
) {
5824 case V_028714_SPI_SHADER_ZERO
:
5825 args
->enabled_channels
= 0; /* writemask */
5826 args
->target
= V_008DFC_SQ_EXP_NULL
;
5829 case V_028714_SPI_SHADER_32_R
:
5830 args
->enabled_channels
= 1;
5831 args
->out
[0] = values
[0];
5834 case V_028714_SPI_SHADER_32_GR
:
5835 args
->enabled_channels
= 0x3;
5836 args
->out
[0] = values
[0];
5837 args
->out
[1] = values
[1];
5840 case V_028714_SPI_SHADER_32_AR
:
5841 args
->enabled_channels
= 0x9;
5842 args
->out
[0] = values
[0];
5843 args
->out
[3] = values
[3];
5846 case V_028714_SPI_SHADER_FP16_ABGR
:
5849 for (unsigned chan
= 0; chan
< 2; chan
++) {
5850 LLVMValueRef pack_args
[2] = {
5852 values
[2 * chan
+ 1]
5854 LLVMValueRef packed
;
5856 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5857 args
->out
[chan
] = packed
;
5861 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5862 for (unsigned chan
= 0; chan
< 4; chan
++) {
5863 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5864 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5865 LLVMConstReal(ctx
->ac
.f32
, 65535), "");
5866 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5867 LLVMConstReal(ctx
->ac
.f32
, 0.5), "");
5868 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5873 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5874 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5877 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5878 for (unsigned chan
= 0; chan
< 4; chan
++) {
5879 val
[chan
] = emit_float_saturate(&ctx
->ac
, values
[chan
], -1, 1);
5880 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5881 LLVMConstReal(ctx
->ac
.f32
, 32767), "");
5883 /* If positive, add 0.5, else add -0.5. */
5884 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5885 LLVMBuildSelect(ctx
->builder
,
5886 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5887 val
[chan
], ctx
->ac
.f32_0
, ""),
5888 LLVMConstReal(ctx
->ac
.f32
, 0.5),
5889 LLVMConstReal(ctx
->ac
.f32
, -0.5), ""), "");
5890 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->ac
.i32
, "");
5894 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5895 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5898 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5899 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5900 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
5901 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: LLVMConstInt(ctx
->ac
.i32
, 3, 0);
5903 for (unsigned chan
= 0; chan
< 4; chan
++) {
5904 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5905 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntULT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5909 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5910 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5914 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5915 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5916 is_int8
? 127 : is_int10
? 511 : 32767, 0);
5917 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->ac
.i32
,
5918 is_int8
? -128 : is_int10
? -512 : -32768, 0);
5919 LLVMValueRef max_alpha
= !is_int10
? max_rgb
: ctx
->ac
.i32_1
;
5920 LLVMValueRef min_alpha
= !is_int10
? min_rgb
: LLVMConstInt(ctx
->ac
.i32
, -2, 0);
5923 for (unsigned chan
= 0; chan
< 4; chan
++) {
5924 val
[chan
] = ac_to_integer(&ctx
->ac
, values
[chan
]);
5925 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
5926 val
[chan
] = emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
5930 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5931 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5936 case V_028714_SPI_SHADER_32_ABGR
:
5937 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5941 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5943 for (unsigned i
= 0; i
< 4; ++i
)
5944 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
5948 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5949 bool export_prim_id
,
5950 struct ac_vs_output_info
*outinfo
)
5952 uint32_t param_count
= 0;
5954 unsigned pos_idx
, num_pos_exports
= 0;
5955 struct ac_export_args args
, pos_args
[4] = {};
5956 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5959 if (ctx
->options
->key
.has_multiview_view_index
) {
5960 LLVMValueRef
* tmp_out
= &ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
5962 for(unsigned i
= 0; i
< 4; ++i
)
5963 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
5964 si_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
5967 LLVMBuildStore(ctx
->builder
, ac_to_float(&ctx
->ac
, ctx
->view_index
), *tmp_out
);
5968 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
5971 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5972 sizeof(outinfo
->vs_output_param_offset
));
5974 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5975 LLVMValueRef slots
[8];
5978 if (outinfo
->cull_dist_mask
)
5979 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5981 i
= VARYING_SLOT_CLIP_DIST0
;
5982 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5983 slots
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5984 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5986 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5987 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
5989 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5990 target
= V_008DFC_SQ_EXP_POS
+ 3;
5991 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5992 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5993 &args
, sizeof(args
));
5996 target
= V_008DFC_SQ_EXP_POS
+ 2;
5997 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5998 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5999 &args
, sizeof(args
));
6003 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
6004 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
6005 for (unsigned j
= 0; j
< 4; j
++)
6006 pos_values
[j
] = LLVMBuildLoad(ctx
->builder
,
6007 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_POS
, j
)], "");
6009 si_llvm_init_export_args(ctx
, pos_values
, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
6011 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
6012 outinfo
->writes_pointsize
= true;
6013 psize_value
= LLVMBuildLoad(ctx
->builder
,
6014 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ
, 0)], "");
6017 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
6018 outinfo
->writes_layer
= true;
6019 layer_value
= LLVMBuildLoad(ctx
->builder
,
6020 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)], "");
6023 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
6024 outinfo
->writes_viewport_index
= true;
6025 viewport_index_value
= LLVMBuildLoad(ctx
->builder
,
6026 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT
, 0)], "");
6029 if (outinfo
->writes_pointsize
||
6030 outinfo
->writes_layer
||
6031 outinfo
->writes_viewport_index
) {
6032 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
6033 (outinfo
->writes_layer
== true ? 4 : 0));
6034 pos_args
[1].valid_mask
= 0;
6035 pos_args
[1].done
= 0;
6036 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
6037 pos_args
[1].compr
= 0;
6038 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
6039 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
6040 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
6041 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
6043 if (outinfo
->writes_pointsize
== true)
6044 pos_args
[1].out
[0] = psize_value
;
6045 if (outinfo
->writes_layer
== true)
6046 pos_args
[1].out
[2] = layer_value
;
6047 if (outinfo
->writes_viewport_index
== true) {
6048 if (ctx
->options
->chip_class
>= GFX9
) {
6049 /* GFX9 has the layer in out.z[10:0] and the viewport
6050 * index in out.z[19:16].
6052 LLVMValueRef v
= viewport_index_value
;
6053 v
= ac_to_integer(&ctx
->ac
, v
);
6054 v
= LLVMBuildShl(ctx
->builder
, v
,
6055 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6057 v
= LLVMBuildOr(ctx
->builder
, v
,
6058 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
6060 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
6061 pos_args
[1].enabled_channels
|= 1 << 2;
6063 pos_args
[1].out
[3] = viewport_index_value
;
6064 pos_args
[1].enabled_channels
|= 1 << 3;
6068 for (i
= 0; i
< 4; i
++) {
6069 if (pos_args
[i
].out
[0])
6074 for (i
= 0; i
< 4; i
++) {
6075 if (!pos_args
[i
].out
[0])
6078 /* Specify the target we are exporting */
6079 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
6080 if (pos_idx
== num_pos_exports
)
6081 pos_args
[i
].done
= 1;
6082 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
6085 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6086 LLVMValueRef values
[4];
6087 if (!(ctx
->output_mask
& (1ull << i
)))
6090 for (unsigned j
= 0; j
< 4; j
++)
6091 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6092 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6094 if (i
== VARYING_SLOT_LAYER
) {
6095 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
6096 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
6098 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
6099 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
6100 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
6102 } else if (i
>= VARYING_SLOT_VAR0
) {
6103 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
6104 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
6105 outinfo
->vs_output_param_offset
[i
] = param_count
;
6110 si_llvm_init_export_args(ctx
, values
, target
, &args
);
6112 if (target
>= V_008DFC_SQ_EXP_POS
&&
6113 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
6114 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
6115 &args
, sizeof(args
));
6117 ac_build_export(&ctx
->ac
, &args
);
6121 if (export_prim_id
) {
6122 LLVMValueRef values
[4];
6123 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
6124 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
6127 values
[0] = ctx
->vs_prim_id
;
6128 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
6129 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
6130 for (unsigned j
= 1; j
< 4; j
++)
6131 values
[j
] = ctx
->ac
.f32_0
;
6132 si_llvm_init_export_args(ctx
, values
, target
, &args
);
6133 ac_build_export(&ctx
->ac
, &args
);
6134 outinfo
->export_prim_id
= true;
6137 outinfo
->pos_exports
= num_pos_exports
;
6138 outinfo
->param_exports
= param_count
;
6142 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
6143 struct ac_es_output_info
*outinfo
)
6146 uint64_t max_output_written
= 0;
6147 LLVMValueRef lds_base
= NULL
;
6149 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6153 if (!(ctx
->output_mask
& (1ull << i
)))
6156 if (i
== VARYING_SLOT_CLIP_DIST0
)
6157 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6159 param_index
= shader_io_get_unique_index(i
);
6161 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
6164 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
6166 if (ctx
->ac
.chip_class
>= GFX9
) {
6167 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
6168 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
6169 LLVMValueRef wave_idx
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6170 LLVMConstInt(ctx
->ac
.i32
, 24, false),
6171 LLVMConstInt(ctx
->ac
.i32
, 4, false), false);
6172 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
6173 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
6174 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
6175 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
6176 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
6179 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6180 LLVMValueRef dw_addr
;
6181 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
6185 if (!(ctx
->output_mask
& (1ull << i
)))
6188 if (i
== VARYING_SLOT_CLIP_DIST0
)
6189 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6191 param_index
= shader_io_get_unique_index(i
);
6194 dw_addr
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6195 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
6198 for (j
= 0; j
< length
; j
++) {
6199 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
6200 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->ac
.i32
, "");
6202 if (ctx
->ac
.chip_class
>= GFX9
) {
6203 ac_lds_store(&ctx
->ac
, dw_addr
,
6204 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6205 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6207 ac_build_buffer_store_dword(&ctx
->ac
,
6210 NULL
, ctx
->es2gs_offset
,
6211 (4 * param_index
+ j
) * 4,
6219 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
6221 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
6222 LLVMValueRef vertex_dw_stride
= unpack_param(&ctx
->ac
, ctx
->ls_out_layout
, 13, 8);
6223 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
6224 vertex_dw_stride
, "");
6226 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6227 LLVMValueRef
*out_ptr
= &ctx
->nir
->outputs
[i
* 4];
6230 if (!(ctx
->output_mask
& (1ull << i
)))
6233 if (i
== VARYING_SLOT_CLIP_DIST0
)
6234 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6235 int param
= shader_io_get_unique_index(i
);
6236 mark_tess_output(ctx
, false, param
);
6238 mark_tess_output(ctx
, false, param
+ 1);
6239 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
6240 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
6242 for (unsigned j
= 0; j
< length
; j
++) {
6243 ac_lds_store(&ctx
->ac
, dw_addr
,
6244 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
6245 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->ac
.i32_1
, "");
6250 struct ac_build_if_state
6252 struct nir_to_llvm_context
*ctx
;
6253 LLVMValueRef condition
;
6254 LLVMBasicBlockRef entry_block
;
6255 LLVMBasicBlockRef true_block
;
6256 LLVMBasicBlockRef false_block
;
6257 LLVMBasicBlockRef merge_block
;
6260 static LLVMBasicBlockRef
6261 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
6263 LLVMBasicBlockRef current_block
;
6264 LLVMBasicBlockRef next_block
;
6265 LLVMBasicBlockRef new_block
;
6267 /* get current basic block */
6268 current_block
= LLVMGetInsertBlock(ctx
->builder
);
6270 /* chqeck if there's another block after this one */
6271 next_block
= LLVMGetNextBasicBlock(current_block
);
6273 /* insert the new block before the next block */
6274 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
6277 /* append new block after current block */
6278 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
6279 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
6285 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
6286 struct nir_to_llvm_context
*ctx
,
6287 LLVMValueRef condition
)
6289 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
6291 memset(ifthen
, 0, sizeof *ifthen
);
6293 ifthen
->condition
= condition
;
6294 ifthen
->entry_block
= block
;
6296 /* create endif/merge basic block for the phi functions */
6297 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
6299 /* create/insert true_block before merge_block */
6300 ifthen
->true_block
=
6301 LLVMInsertBasicBlockInContext(ctx
->context
,
6302 ifthen
->merge_block
,
6305 /* successive code goes into the true block */
6306 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
6310 * End a conditional.
6313 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
6315 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
6317 /* Insert branch to the merge block from current block */
6318 LLVMBuildBr(builder
, ifthen
->merge_block
);
6321 * Now patch in the various branch instructions.
6324 /* Insert the conditional branch instruction at the end of entry_block */
6325 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
6326 if (ifthen
->false_block
) {
6327 /* we have an else clause */
6328 LLVMBuildCondBr(builder
, ifthen
->condition
,
6329 ifthen
->true_block
, ifthen
->false_block
);
6332 /* no else clause */
6333 LLVMBuildCondBr(builder
, ifthen
->condition
,
6334 ifthen
->true_block
, ifthen
->merge_block
);
6337 /* Resume building code at end of the ifthen->merge_block */
6338 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
6342 write_tess_factors(struct nir_to_llvm_context
*ctx
)
6344 unsigned stride
, outer_comps
, inner_comps
;
6345 struct ac_build_if_state if_ctx
, inner_if_ctx
;
6346 LLVMValueRef invocation_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 8, 5);
6347 LLVMValueRef rel_patch_id
= unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
6348 unsigned tess_inner_index
, tess_outer_index
;
6349 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
6350 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
6352 emit_barrier(&ctx
->ac
, ctx
->stage
);
6354 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
6374 ac_nir_build_if(&if_ctx
, ctx
,
6375 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6376 invocation_id
, ctx
->ac
.i32_0
, ""));
6378 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6379 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6381 mark_tess_output(ctx
, true, tess_inner_index
);
6382 mark_tess_output(ctx
, true, tess_outer_index
);
6383 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
6384 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6385 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
6386 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
6387 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
6389 for (i
= 0; i
< 4; i
++) {
6390 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6391 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
6395 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
6396 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
6397 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6399 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
6401 for (i
= 0; i
< outer_comps
; i
++) {
6403 ac_lds_load(&ctx
->ac
, lds_outer
);
6404 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
6407 for (i
= 0; i
< inner_comps
; i
++) {
6408 inner
[i
] = out
[outer_comps
+i
] =
6409 ac_lds_load(&ctx
->ac
, lds_inner
);
6410 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
6415 /* Convert the outputs to vectors for stores. */
6416 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
6420 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
6423 buffer
= ctx
->hs_ring_tess_factor
;
6424 tf_base
= ctx
->tess_factor_offset
;
6425 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
6426 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
6427 unsigned tf_offset
= 0;
6429 if (ctx
->options
->chip_class
<= VI
) {
6430 ac_nir_build_if(&inner_if_ctx
, ctx
,
6431 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
6432 rel_patch_id
, ctx
->ac
.i32_0
, ""));
6434 /* Store the dynamic HS control word. */
6435 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
6436 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
6437 1, ctx
->ac
.i32_0
, tf_base
,
6438 0, 1, 0, true, false);
6441 ac_nir_build_endif(&inner_if_ctx
);
6444 /* Store the tessellation factors. */
6445 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
6446 MIN2(stride
, 4), byteoffset
, tf_base
,
6447 tf_offset
, 1, 0, true, false);
6449 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
6450 stride
- 4, byteoffset
, tf_base
,
6451 16 + tf_offset
, 1, 0, true, false);
6453 //store to offchip for TES to read - only if TES reads them
6454 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
6455 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
6456 LLVMValueRef tf_inner_offset
;
6457 unsigned param_outer
, param_inner
;
6459 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
6460 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6461 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
6463 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
6464 util_next_power_of_two(outer_comps
));
6466 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
6467 outer_comps
, tf_outer_offset
,
6468 ctx
->oc_lds
, 0, 1, 0, true, false);
6470 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
6471 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
6472 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
6474 inner_vec
= inner_comps
== 1 ? inner
[0] :
6475 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
6476 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
6477 inner_comps
, tf_inner_offset
,
6478 ctx
->oc_lds
, 0, 1, 0, true, false);
6481 ac_nir_build_endif(&if_ctx
);
6485 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
6487 write_tess_factors(ctx
);
6491 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
6492 LLVMValueRef
*color
, unsigned param
, bool is_last
,
6493 struct ac_export_args
*args
)
6496 si_llvm_init_export_args(ctx
, color
, param
,
6500 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
6501 args
->done
= 1; /* DONE bit */
6502 } else if (!args
->enabled_channels
)
6503 return false; /* unnecessary NULL export */
6509 radv_export_mrt_z(struct nir_to_llvm_context
*ctx
,
6510 LLVMValueRef depth
, LLVMValueRef stencil
,
6511 LLVMValueRef samplemask
)
6513 struct ac_export_args args
;
6515 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
6517 ac_build_export(&ctx
->ac
, &args
);
6521 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
6524 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6525 struct ac_export_args color_args
[8];
6527 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6528 LLVMValueRef values
[4];
6530 if (!(ctx
->output_mask
& (1ull << i
)))
6533 if (i
== FRAG_RESULT_DEPTH
) {
6534 ctx
->shader_info
->fs
.writes_z
= true;
6535 depth
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6536 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6537 } else if (i
== FRAG_RESULT_STENCIL
) {
6538 ctx
->shader_info
->fs
.writes_stencil
= true;
6539 stencil
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6540 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6541 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
6542 ctx
->shader_info
->fs
.writes_sample_mask
= true;
6543 samplemask
= ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6544 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
6547 for (unsigned j
= 0; j
< 4; j
++)
6548 values
[j
] = ac_to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
6549 ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
6551 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
6552 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
6554 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
6560 for (unsigned i
= 0; i
< index
; i
++)
6561 ac_build_export(&ctx
->ac
, &color_args
[i
]);
6562 if (depth
|| stencil
|| samplemask
)
6563 radv_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
6565 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
6566 ac_build_export(&ctx
->ac
, &color_args
[0]);
6571 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
6573 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
6577 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
6578 LLVMValueRef
*addrs
)
6580 struct nir_to_llvm_context
*ctx
= nir_to_llvm_context_from_abi(abi
);
6582 switch (ctx
->stage
) {
6583 case MESA_SHADER_VERTEX
:
6584 if (ctx
->options
->key
.vs
.as_ls
)
6585 handle_ls_outputs_post(ctx
);
6586 else if (ctx
->options
->key
.vs
.as_es
)
6587 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
6589 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
6590 &ctx
->shader_info
->vs
.outinfo
);
6592 case MESA_SHADER_FRAGMENT
:
6593 handle_fs_outputs_post(ctx
);
6595 case MESA_SHADER_GEOMETRY
:
6596 emit_gs_epilogue(ctx
);
6598 case MESA_SHADER_TESS_CTRL
:
6599 handle_tcs_outputs_post(ctx
);
6601 case MESA_SHADER_TESS_EVAL
:
6602 if (ctx
->options
->key
.tes
.as_es
)
6603 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
6605 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
6606 &ctx
->shader_info
->tes
.outinfo
);
6613 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
6615 LLVMPassManagerRef passmgr
;
6616 /* Create the pass manager */
6617 passmgr
= LLVMCreateFunctionPassManagerForModule(
6620 /* This pass should eliminate all the load and store instructions */
6621 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
6623 /* Add some optimization passes */
6624 LLVMAddScalarReplAggregatesPass(passmgr
);
6625 LLVMAddLICMPass(passmgr
);
6626 LLVMAddAggressiveDCEPass(passmgr
);
6627 LLVMAddCFGSimplificationPass(passmgr
);
6628 LLVMAddInstructionCombiningPass(passmgr
);
6631 LLVMInitializeFunctionPassManager(passmgr
);
6632 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
6633 LLVMFinalizeFunctionPassManager(passmgr
);
6635 LLVMDisposeBuilder(ctx
->builder
);
6636 LLVMDisposePassManager(passmgr
);
6640 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
6642 struct ac_vs_output_info
*outinfo
;
6644 switch (ctx
->stage
) {
6645 case MESA_SHADER_FRAGMENT
:
6646 case MESA_SHADER_COMPUTE
:
6647 case MESA_SHADER_TESS_CTRL
:
6648 case MESA_SHADER_GEOMETRY
:
6650 case MESA_SHADER_VERTEX
:
6651 if (ctx
->options
->key
.vs
.as_ls
||
6652 ctx
->options
->key
.vs
.as_es
)
6654 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
6656 case MESA_SHADER_TESS_EVAL
:
6657 if (ctx
->options
->key
.vs
.as_es
)
6659 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
6662 unreachable("Unhandled shader type");
6665 ac_optimize_vs_outputs(&ctx
->ac
,
6667 outinfo
->vs_output_param_offset
,
6669 &outinfo
->param_exports
);
6673 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
6675 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
6676 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
6677 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_VS
, false));
6680 if (ctx
->is_gs_copy_shader
) {
6681 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_VS
, false));
6683 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
6685 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_ESGS_GS
, false));
6686 ctx
->gsvs_ring
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_GSVS_GS
, false));
6688 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.v4i32
, "");
6690 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
6691 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->ac
.i32_1
, "");
6692 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
6693 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->ac
.i32_1
, "");
6696 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
6697 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
6698 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
6699 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
6704 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
6705 const struct nir_shader
*nir
)
6707 switch (nir
->info
.stage
) {
6708 case MESA_SHADER_TESS_CTRL
:
6709 return chip_class
>= CIK
? 128 : 64;
6710 case MESA_SHADER_GEOMETRY
:
6711 return chip_class
>= GFX9
? 128 : 64;
6712 case MESA_SHADER_COMPUTE
:
6718 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
6719 nir
->info
.cs
.local_size
[1] *
6720 nir
->info
.cs
.local_size
[2];
6721 return max_workgroup_size
;
6724 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
6725 static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6727 LLVMValueRef count
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6728 LLVMConstInt(ctx
->ac
.i32
, 8, false),
6729 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6730 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
6732 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
6733 ctx
->vs_prim_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.vertex_id
, ctx
->vs_prim_id
, "");
6734 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_rel_ids
, ctx
->rel_auto_id
, "");
6735 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_patch_id
, ctx
->abi
.vertex_id
, "");
6738 static void prepare_gs_input_vgprs(struct nir_to_llvm_context
*ctx
)
6740 for(int i
= 5; i
>= 0; --i
) {
6741 ctx
->gs_vtx_offset
[i
] = ac_build_bfe(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
6742 LLVMConstInt(ctx
->ac
.i32
, (i
& 1) * 16, false),
6743 LLVMConstInt(ctx
->ac
.i32
, 16, false), false);
6746 ctx
->gs_wave_id
= ac_build_bfe(&ctx
->ac
, ctx
->merged_wave_info
,
6747 LLVMConstInt(ctx
->ac
.i32
, 16, false),
6748 LLVMConstInt(ctx
->ac
.i32
, 8, false), false);
6751 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
6752 struct nir_shader
*nir
, struct nir_to_llvm_context
*nctx
)
6754 struct ac_nir_context ctx
= {};
6755 struct nir_function
*func
;
6764 ctx
.stage
= nir
->info
.stage
;
6766 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6768 nir_foreach_variable(variable
, &nir
->outputs
)
6769 handle_shader_output_decl(&ctx
, nir
, variable
);
6771 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6772 _mesa_key_pointer_equal
);
6773 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6774 _mesa_key_pointer_equal
);
6775 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
6776 _mesa_key_pointer_equal
);
6778 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
6780 setup_locals(&ctx
, func
);
6782 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
6783 setup_shared(&ctx
, nir
);
6785 visit_cf_list(&ctx
, &func
->impl
->body
);
6786 phi_post_pass(&ctx
);
6788 ctx
.abi
->emit_outputs(ctx
.abi
, RADEON_LLVM_MAX_OUTPUTS
,
6792 ralloc_free(ctx
.defs
);
6793 ralloc_free(ctx
.phis
);
6794 ralloc_free(ctx
.vars
);
6801 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
6802 struct nir_shader
*const *shaders
,
6804 struct ac_shader_variant_info
*shader_info
,
6805 const struct ac_nir_compiler_options
*options
)
6807 struct nir_to_llvm_context ctx
= {0};
6809 ctx
.options
= options
;
6810 ctx
.shader_info
= shader_info
;
6811 ctx
.context
= LLVMContextCreate();
6812 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6814 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
6816 ctx
.ac
.module
= ctx
.module
;
6817 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
6819 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
6820 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
6821 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
6822 LLVMDisposeTargetData(data_layout
);
6823 LLVMDisposeMessage(data_layout_str
);
6825 enum ac_float_mode float_mode
=
6826 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
6827 AC_FLOAT_MODE_DEFAULT
;
6829 ctx
.builder
= ac_create_builder(ctx
.context
, float_mode
);
6830 ctx
.ac
.builder
= ctx
.builder
;
6832 memset(shader_info
, 0, sizeof(*shader_info
));
6834 for(int i
= 0; i
< shader_count
; ++i
)
6835 ac_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
6837 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
6838 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
6839 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
6840 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
6842 ctx
.max_workgroup_size
= 0;
6843 for (int i
= 0; i
< shader_count
; ++i
) {
6844 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
6845 ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
6849 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
6850 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
6852 ctx
.abi
.inputs
= &ctx
.inputs
[0];
6853 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
6854 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
6855 ctx
.abi
.load_ubo
= radv_load_ubo
;
6856 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
6857 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
6858 ctx
.abi
.clamp_shadow_reference
= false;
6860 if (shader_count
>= 2)
6861 ac_init_exec_full_mask(&ctx
.ac
);
6863 if (ctx
.ac
.chip_class
== GFX9
&&
6864 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
6865 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
6867 for(int i
= 0; i
< shader_count
; ++i
) {
6868 ctx
.stage
= shaders
[i
]->info
.stage
;
6869 ctx
.output_mask
= 0;
6870 ctx
.tess_outputs_written
= 0;
6871 ctx
.num_output_clips
= shaders
[i
]->info
.clip_distance_array_size
;
6872 ctx
.num_output_culls
= shaders
[i
]->info
.cull_distance_array_size
;
6874 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6875 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "gs_next_vertex");
6876 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
6877 ctx
.abi
.load_inputs
= load_gs_input
;
6878 ctx
.abi
.emit_primitive
= visit_end_primitive
;
6879 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6880 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
6881 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
6882 ctx
.abi
.load_tess_varyings
= load_tcs_varyings
;
6883 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
6884 ctx
.abi
.store_tcs_outputs
= store_tcs_output
;
6885 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
6886 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
6887 ctx
.abi
.load_tess_varyings
= load_tes_input
;
6888 ctx
.abi
.load_tess_coord
= load_tess_coord
;
6889 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
6890 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
6891 if (shader_info
->info
.vs
.needs_instance_id
) {
6892 if (ctx
.options
->key
.vs
.as_ls
) {
6893 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6894 MAX2(2, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6896 ctx
.shader_info
->vs
.vgpr_comp_cnt
=
6897 MAX2(1, ctx
.shader_info
->vs
.vgpr_comp_cnt
);
6900 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
6901 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
6905 emit_barrier(&ctx
.ac
, ctx
.stage
);
6907 ac_setup_rings(&ctx
);
6909 LLVMBasicBlockRef merge_block
;
6910 if (shader_count
>= 2) {
6911 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
6912 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6913 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
6915 LLVMValueRef count
= ac_build_bfe(&ctx
.ac
, ctx
.merged_wave_info
,
6916 LLVMConstInt(ctx
.ac
.i32
, 8 * i
, false),
6917 LLVMConstInt(ctx
.ac
.i32
, 8, false), false);
6918 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
6919 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
6920 thread_id
, count
, "");
6921 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
6923 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
6926 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
6927 handle_fs_inputs(&ctx
, shaders
[i
]);
6928 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
6929 handle_vs_inputs(&ctx
, shaders
[i
]);
6930 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
6931 prepare_gs_input_vgprs(&ctx
);
6933 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
6934 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
6936 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
], &ctx
);
6938 if (shader_count
>= 2) {
6939 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
6940 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
6943 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
6944 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
6945 shaders
[i
]->info
.cull_distance_array_size
> 4;
6946 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6947 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6948 shaders
[i
]->info
.gs
.vertices_out
;
6949 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
6950 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6951 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6952 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6953 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6957 LLVMBuildRetVoid(ctx
.builder
);
6959 if (options
->dump_preoptir
)
6960 ac_dump_module(ctx
.module
);
6962 ac_llvm_finalize_module(&ctx
);
6964 if (shader_count
== 1)
6965 ac_nir_eliminate_const_vs_outputs(&ctx
);
6970 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6972 unsigned *retval
= (unsigned *)context
;
6973 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6974 char *description
= LLVMGetDiagInfoDescription(di
);
6976 if (severity
== LLVMDSError
) {
6978 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6982 LLVMDisposeMessage(description
);
6985 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6986 struct ac_shader_binary
*binary
,
6987 LLVMTargetMachineRef tm
)
6989 unsigned retval
= 0;
6991 LLVMContextRef llvm_ctx
;
6992 LLVMMemoryBufferRef out_buffer
;
6993 unsigned buffer_size
;
6994 const char *buffer_data
;
6997 /* Setup Diagnostic Handler*/
6998 llvm_ctx
= LLVMGetModuleContext(M
);
7000 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
7004 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
7007 /* Process Errors/Warnings */
7009 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
7015 /* Extract Shader Code*/
7016 buffer_size
= LLVMGetBufferSize(out_buffer
);
7017 buffer_data
= LLVMGetBufferStart(out_buffer
);
7019 ac_elf_read(buffer_data
, buffer_size
, binary
);
7022 LLVMDisposeMemoryBuffer(out_buffer
);
7028 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
7029 LLVMModuleRef llvm_module
,
7030 struct ac_shader_binary
*binary
,
7031 struct ac_shader_config
*config
,
7032 struct ac_shader_variant_info
*shader_info
,
7033 gl_shader_stage stage
,
7034 bool dump_shader
, bool supports_spill
)
7037 ac_dump_module(llvm_module
);
7039 memset(binary
, 0, sizeof(*binary
));
7040 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
7042 fprintf(stderr
, "compile failed\n");
7046 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
7048 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
7050 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
7051 LLVMDisposeModule(llvm_module
);
7052 LLVMContextDispose(ctx
);
7054 if (stage
== MESA_SHADER_FRAGMENT
) {
7055 shader_info
->num_input_vgprs
= 0;
7056 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
7057 shader_info
->num_input_vgprs
+= 2;
7058 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
7059 shader_info
->num_input_vgprs
+= 2;
7060 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
7061 shader_info
->num_input_vgprs
+= 2;
7062 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
7063 shader_info
->num_input_vgprs
+= 3;
7064 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
7065 shader_info
->num_input_vgprs
+= 2;
7066 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
7067 shader_info
->num_input_vgprs
+= 2;
7068 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
7069 shader_info
->num_input_vgprs
+= 2;
7070 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
7071 shader_info
->num_input_vgprs
+= 1;
7072 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
7073 shader_info
->num_input_vgprs
+= 1;
7074 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
7075 shader_info
->num_input_vgprs
+= 1;
7076 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
7077 shader_info
->num_input_vgprs
+= 1;
7078 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
7079 shader_info
->num_input_vgprs
+= 1;
7080 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
7081 shader_info
->num_input_vgprs
+= 1;
7082 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
7083 shader_info
->num_input_vgprs
+= 1;
7084 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
7085 shader_info
->num_input_vgprs
+= 1;
7086 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
7087 shader_info
->num_input_vgprs
+= 1;
7089 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
7091 /* +3 for scratch wave offset and VCC */
7092 config
->num_sgprs
= MAX2(config
->num_sgprs
,
7093 shader_info
->num_input_sgprs
+ 3);
7095 /* Enable 64-bit and 16-bit denormals, because there is no performance
7098 * If denormals are enabled, all floating-point output modifiers are
7101 * Don't enable denormals for 32-bit floats, because:
7102 * - Floating-point output modifiers would be ignored by the hw.
7103 * - Some opcodes don't support denormals, such as v_mad_f32. We would
7104 * have to stop using those.
7105 * - SI & CI would be very slow.
7107 config
->float_mode
|= V_00B028_FP_64_DENORMS
;
7111 ac_fill_shader_info(struct ac_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct ac_nir_compiler_options
*options
)
7113 switch (nir
->info
.stage
) {
7114 case MESA_SHADER_COMPUTE
:
7115 for (int i
= 0; i
< 3; ++i
)
7116 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
7118 case MESA_SHADER_FRAGMENT
:
7119 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
7121 case MESA_SHADER_GEOMETRY
:
7122 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
7123 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
7124 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
7125 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
7127 case MESA_SHADER_TESS_EVAL
:
7128 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
7129 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
7130 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
7131 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
7132 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
7134 case MESA_SHADER_TESS_CTRL
:
7135 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
7137 case MESA_SHADER_VERTEX
:
7138 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
7139 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
7140 /* in LS mode we need at least 1, invocation id needs 2, handled elsewhere */
7141 if (options
->key
.vs
.as_ls
)
7142 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
7149 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
7150 struct ac_shader_binary
*binary
,
7151 struct ac_shader_config
*config
,
7152 struct ac_shader_variant_info
*shader_info
,
7153 struct nir_shader
*const *nir
,
7155 const struct ac_nir_compiler_options
*options
,
7159 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, nir_count
, shader_info
,
7162 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
[0]->info
.stage
, dump_shader
, options
->supports_spill
);
7163 for (int i
= 0; i
< nir_count
; ++i
)
7164 ac_fill_shader_info(shader_info
, nir
[i
], options
);
7166 /* Determine the ES type (VS or TES) for the GS on GFX9. */
7167 if (options
->chip_class
== GFX9
) {
7168 if (nir_count
== 2 &&
7169 nir
[1]->info
.stage
== MESA_SHADER_GEOMETRY
) {
7170 shader_info
->gs
.es_type
= nir
[0]->info
.stage
;
7176 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
7178 LLVMValueRef args
[9];
7179 args
[0] = ctx
->gsvs_ring
;
7180 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->abi
.vertex_id
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
7181 args
[3] = ctx
->ac
.i32_0
;
7182 args
[4] = ctx
->ac
.i32_1
; /* OFFEN */
7183 args
[5] = ctx
->ac
.i32_0
; /* IDXEN */
7184 args
[6] = ctx
->ac
.i32_1
; /* GLC */
7185 args
[7] = ctx
->ac
.i32_1
; /* SLC */
7186 args
[8] = ctx
->ac
.i32_0
; /* TFE */
7190 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
7194 if (!(ctx
->output_mask
& (1ull << i
)))
7197 if (i
== VARYING_SLOT_CLIP_DIST0
) {
7198 /* unpack clip and cull from a single set of slots */
7199 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
7204 for (unsigned j
= 0; j
< length
; j
++) {
7206 args
[2] = LLVMConstInt(ctx
->ac
.i32
,
7208 ctx
->gs_max_out_vertices
* 16 * 4, false);
7210 value
= ac_build_intrinsic(&ctx
->ac
,
7211 "llvm.SI.buffer.load.dword.i32.i32",
7212 ctx
->ac
.i32
, args
, 9,
7213 AC_FUNC_ATTR_READONLY
|
7214 AC_FUNC_ATTR_LEGACY
);
7216 LLVMBuildStore(ctx
->builder
,
7217 ac_to_float(&ctx
->ac
, value
), ctx
->nir
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
7221 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
7224 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
7225 struct nir_shader
*geom_shader
,
7226 struct ac_shader_binary
*binary
,
7227 struct ac_shader_config
*config
,
7228 struct ac_shader_variant_info
*shader_info
,
7229 const struct ac_nir_compiler_options
*options
,
7232 struct nir_to_llvm_context ctx
= {0};
7233 ctx
.context
= LLVMContextCreate();
7234 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
7235 ctx
.options
= options
;
7236 ctx
.shader_info
= shader_info
;
7238 ac_llvm_context_init(&ctx
.ac
, ctx
.context
, options
->chip_class
,
7240 ctx
.ac
.module
= ctx
.module
;
7242 ctx
.is_gs_copy_shader
= true;
7243 LLVMSetTarget(ctx
.module
, "amdgcn--");
7245 enum ac_float_mode float_mode
=
7246 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
7247 AC_FLOAT_MODE_DEFAULT
;
7249 ctx
.builder
= ac_create_builder(ctx
.context
, float_mode
);
7250 ctx
.ac
.builder
= ctx
.builder
;
7251 ctx
.stage
= MESA_SHADER_VERTEX
;
7253 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
7255 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
7256 ac_setup_rings(&ctx
);
7258 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
7259 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
7261 struct ac_nir_context nir_ctx
= {};
7262 nir_ctx
.ac
= ctx
.ac
;
7263 nir_ctx
.abi
= &ctx
.abi
;
7265 nir_ctx
.nctx
= &ctx
;
7268 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
7269 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
7270 handle_shader_output_decl(&nir_ctx
, geom_shader
, variable
);
7273 ac_gs_copy_shader_emit(&ctx
);
7277 LLVMBuildRetVoid(ctx
.builder
);
7279 ac_llvm_finalize_module(&ctx
);
7281 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
7283 dump_shader
, options
->supports_spill
);