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_info.h"
34 #include "ac_exp_param.h"
36 enum radeon_llvm_calling_convention
{
37 RADEON_LLVM_AMDGPU_VS
= 87,
38 RADEON_LLVM_AMDGPU_GS
= 88,
39 RADEON_LLVM_AMDGPU_PS
= 89,
40 RADEON_LLVM_AMDGPU_CS
= 90,
43 #define CONST_ADDR_SPACE 2
44 #define LOCAL_ADDR_SPACE 3
46 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
47 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
56 struct nir_to_llvm_context
{
57 struct ac_llvm_context ac
;
58 const struct ac_nir_compiler_options
*options
;
59 struct ac_shader_variant_info
*shader_info
;
60 unsigned max_workgroup_size
;
61 LLVMContextRef context
;
63 LLVMBuilderRef builder
;
64 LLVMValueRef main_function
;
66 struct hash_table
*defs
;
67 struct hash_table
*phis
;
69 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
70 LLVMValueRef ring_offsets
;
71 LLVMValueRef push_constants
;
72 LLVMValueRef num_work_groups
;
73 LLVMValueRef workgroup_ids
;
74 LLVMValueRef local_invocation_ids
;
77 LLVMValueRef vertex_buffers
;
78 LLVMValueRef base_vertex
;
79 LLVMValueRef start_instance
;
80 LLVMValueRef draw_index
;
81 LLVMValueRef vertex_id
;
82 LLVMValueRef rel_auto_id
;
83 LLVMValueRef vs_prim_id
;
84 LLVMValueRef instance_id
;
85 LLVMValueRef ls_out_layout
;
86 LLVMValueRef es2gs_offset
;
88 LLVMValueRef tcs_offchip_layout
;
89 LLVMValueRef tcs_out_offsets
;
90 LLVMValueRef tcs_out_layout
;
91 LLVMValueRef tcs_in_layout
;
93 LLVMValueRef tess_factor_offset
;
94 LLVMValueRef tcs_patch_id
;
95 LLVMValueRef tcs_rel_ids
;
96 LLVMValueRef tes_rel_patch_id
;
97 LLVMValueRef tes_patch_id
;
101 LLVMValueRef gsvs_ring_stride
;
102 LLVMValueRef gsvs_num_entries
;
103 LLVMValueRef gs2vs_offset
;
104 LLVMValueRef gs_wave_id
;
105 LLVMValueRef gs_vtx_offset
[6];
106 LLVMValueRef gs_prim_id
, gs_invocation_id
;
108 LLVMValueRef esgs_ring
;
109 LLVMValueRef gsvs_ring
;
110 LLVMValueRef hs_ring_tess_offchip
;
111 LLVMValueRef hs_ring_tess_factor
;
113 LLVMValueRef prim_mask
;
114 LLVMValueRef sample_pos_offset
;
115 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
116 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
117 LLVMValueRef front_face
;
118 LLVMValueRef ancillary
;
119 LLVMValueRef sample_coverage
;
120 LLVMValueRef frag_pos
[4];
122 LLVMBasicBlockRef continue_block
;
123 LLVMBasicBlockRef break_block
;
143 LLVMValueRef i1false
;
144 LLVMValueRef i32zero
;
146 LLVMValueRef f32zero
;
148 LLVMValueRef v4f32empty
;
150 unsigned uniform_md_kind
;
151 LLVMValueRef empty_md
;
152 gl_shader_stage stage
;
155 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
156 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
158 LLVMValueRef shared_memory
;
160 uint64_t output_mask
;
162 LLVMValueRef
*locals
;
164 uint8_t num_output_clips
;
165 uint8_t num_output_culls
;
167 bool has_ds_bpermute
;
169 bool is_gs_copy_shader
;
170 LLVMValueRef gs_next_vertex
;
171 unsigned gs_max_out_vertices
;
173 unsigned tes_primitive_mode
;
174 uint64_t tess_outputs_written
;
175 uint64_t tess_patch_outputs_written
;
178 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
179 nir_deref_var
*deref
,
180 enum desc_type desc_type
);
181 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
183 return (index
* 4) + chan
;
186 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
188 /* handle patch indices separate */
189 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
191 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
193 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
194 return 2 + (slot
- VARYING_SLOT_PATCH0
);
196 if (slot
== VARYING_SLOT_POS
)
198 if (slot
== VARYING_SLOT_PSIZ
)
200 if (slot
== VARYING_SLOT_CLIP_DIST0
)
202 /* 3 is reserved for clip dist as well */
203 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
204 return 4 + (slot
- VARYING_SLOT_VAR0
);
205 unreachable("illegal slot in get unique index\n");
208 static unsigned llvm_get_type_size(LLVMTypeRef type
)
210 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
213 case LLVMIntegerTypeKind
:
214 return LLVMGetIntTypeWidth(type
) / 8;
215 case LLVMFloatTypeKind
:
217 case LLVMPointerTypeKind
:
219 case LLVMVectorTypeKind
:
220 return LLVMGetVectorSize(type
) *
221 llvm_get_type_size(LLVMGetElementType(type
));
228 static void set_llvm_calling_convention(LLVMValueRef func
,
229 gl_shader_stage stage
)
231 enum radeon_llvm_calling_convention calling_conv
;
234 case MESA_SHADER_VERTEX
:
235 case MESA_SHADER_TESS_CTRL
:
236 case MESA_SHADER_TESS_EVAL
:
237 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
239 case MESA_SHADER_GEOMETRY
:
240 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
242 case MESA_SHADER_FRAGMENT
:
243 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
245 case MESA_SHADER_COMPUTE
:
246 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
249 unreachable("Unhandle shader type");
252 LLVMSetFunctionCallConv(func
, calling_conv
);
256 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
257 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
258 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
259 unsigned param_count
, unsigned array_params_mask
,
260 unsigned sgpr_params
, unsigned max_workgroup_size
,
263 LLVMTypeRef main_function_type
, ret_type
;
264 LLVMBasicBlockRef main_function_body
;
266 if (num_return_elems
)
267 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
268 num_return_elems
, true);
270 ret_type
= LLVMVoidTypeInContext(ctx
);
272 /* Setup the function */
274 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
275 LLVMValueRef main_function
=
276 LLVMAddFunction(module
, "main", main_function_type
);
278 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
279 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
281 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
282 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
283 if (array_params_mask
& (1 << i
)) {
284 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
285 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
286 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
289 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
293 if (max_workgroup_size
) {
294 ac_llvm_add_target_dep_function_attr(main_function
,
295 "amdgpu-max-work-group-size",
299 /* These were copied from some LLVM test. */
300 LLVMAddTargetDependentFunctionAttr(main_function
,
301 "less-precise-fpmad",
303 LLVMAddTargetDependentFunctionAttr(main_function
,
306 LLVMAddTargetDependentFunctionAttr(main_function
,
309 LLVMAddTargetDependentFunctionAttr(main_function
,
313 return main_function
;
316 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
318 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
322 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
330 offset
= LLVMConstInt(ctx
->i32
, idx
* 16, false);
332 ptr
= ctx
->shared_memory
;
333 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
334 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
335 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
339 static LLVMTypeRef
to_integer_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
341 if (t
== ctx
->f16
|| t
== ctx
->i16
)
343 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
345 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
348 unreachable("Unhandled integer size");
351 static LLVMTypeRef
to_integer_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
353 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
354 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
355 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
356 LLVMGetVectorSize(t
));
358 return to_integer_type_scalar(ctx
, t
);
361 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
363 LLVMTypeRef type
= LLVMTypeOf(v
);
364 return LLVMBuildBitCast(ctx
->builder
, v
, to_integer_type(ctx
, type
), "");
367 static LLVMTypeRef
to_float_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
369 if (t
== ctx
->i16
|| t
== ctx
->f16
)
371 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
373 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
376 unreachable("Unhandled float size");
379 static LLVMTypeRef
to_float_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
381 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
382 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
383 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
384 LLVMGetVectorSize(t
));
386 return to_float_type_scalar(ctx
, t
);
389 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
391 LLVMTypeRef type
= LLVMTypeOf(v
);
392 return LLVMBuildBitCast(ctx
->builder
, v
, to_float_type(ctx
, type
), "");
395 static int get_elem_bits(struct nir_to_llvm_context
*ctx
, LLVMTypeRef type
)
397 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
398 type
= LLVMGetElementType(type
);
400 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
401 return LLVMGetIntTypeWidth(type
);
403 if (type
== ctx
->f16
)
405 if (type
== ctx
->f32
)
407 if (type
== ctx
->f64
)
410 unreachable("Unhandled type kind in get_elem_bits");
413 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
414 LLVMValueRef param
, unsigned rshift
,
417 LLVMValueRef value
= param
;
419 value
= LLVMBuildLShr(ctx
->builder
, value
,
420 LLVMConstInt(ctx
->i32
, rshift
, false), "");
422 if (rshift
+ bitwidth
< 32) {
423 unsigned mask
= (1 << bitwidth
) - 1;
424 value
= LLVMBuildAnd(ctx
->builder
, value
,
425 LLVMConstInt(ctx
->i32
, mask
, false), "");
430 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
432 switch (ctx
->stage
) {
433 case MESA_SHADER_TESS_CTRL
:
434 return unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
435 case MESA_SHADER_TESS_EVAL
:
436 return ctx
->tes_rel_patch_id
;
439 unreachable("Illegal stage");
443 /* Tessellation shaders pass outputs to the next shader using LDS.
445 * LS outputs = TCS inputs
446 * TCS outputs = TES inputs
449 * - TCS inputs for patch 0
450 * - TCS inputs for patch 1
451 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
453 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
454 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
455 * - TCS outputs for patch 1
456 * - Per-patch TCS outputs for patch 1
457 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
458 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
461 * All three shaders VS(LS), TCS, TES share the same LDS space.
464 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
466 if (ctx
->stage
== MESA_SHADER_VERTEX
)
467 return unpack_param(ctx
, ctx
->ls_out_layout
, 0, 13);
468 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
469 return unpack_param(ctx
, ctx
->tcs_in_layout
, 0, 13);
477 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
479 return unpack_param(ctx
, ctx
->tcs_out_layout
, 0, 13);
483 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
485 return LLVMBuildMul(ctx
->builder
,
486 unpack_param(ctx
, ctx
->tcs_out_offsets
, 0, 16),
487 LLVMConstInt(ctx
->i32
, 4, false), "");
491 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
493 return LLVMBuildMul(ctx
->builder
,
494 unpack_param(ctx
, ctx
->tcs_out_offsets
, 16, 16),
495 LLVMConstInt(ctx
->i32
, 4, false), "");
499 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
501 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
502 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
504 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
508 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
510 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
511 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
512 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
514 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
515 LLVMBuildMul(ctx
->builder
, patch_stride
,
521 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
523 LLVMValueRef patch0_patch_data_offset
=
524 get_tcs_out_patch0_patch_data_offset(ctx
);
525 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
526 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
528 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
529 LLVMBuildMul(ctx
->builder
, patch_stride
,
534 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
536 ud_info
->sgpr_idx
= *sgpr_idx
;
537 ud_info
->num_sgprs
= num_sgprs
;
538 ud_info
->indirect
= false;
539 ud_info
->indirect_offset
= 0;
540 *sgpr_idx
+= num_sgprs
;
543 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
544 int idx
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
546 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
550 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
551 uint32_t indirect_offset
)
553 ud_info
->sgpr_idx
= sgpr_idx
;
554 ud_info
->num_sgprs
= num_sgprs
;
555 ud_info
->indirect
= true;
556 ud_info
->indirect_offset
= indirect_offset
;
559 static void declare_tess_lds(struct nir_to_llvm_context
*ctx
)
561 unsigned lds_size
= ctx
->options
->chip_class
>= CIK
? 65536 : 32768;
562 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32zero
,
563 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
567 struct user_sgpr_info
{
568 bool need_ring_offsets
;
570 bool indirect_all_descriptor_sets
;
573 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
574 struct user_sgpr_info
*user_sgpr_info
)
576 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
578 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
579 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
580 ctx
->stage
== MESA_SHADER_VERTEX
||
581 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
582 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
583 ctx
->is_gs_copy_shader
)
584 user_sgpr_info
->need_ring_offsets
= true;
586 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
587 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
588 user_sgpr_info
->need_ring_offsets
= true;
590 /* 2 user sgprs will nearly always be allocated for scratch/rings */
591 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
592 user_sgpr_info
->sgpr_count
+= 2;
595 switch (ctx
->stage
) {
596 case MESA_SHADER_COMPUTE
:
597 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
599 case MESA_SHADER_FRAGMENT
:
600 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
602 case MESA_SHADER_VERTEX
:
603 if (!ctx
->is_gs_copy_shader
) {
604 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
605 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
606 user_sgpr_info
->sgpr_count
+= 3;
608 user_sgpr_info
->sgpr_count
+= 2;
611 if (ctx
->options
->key
.vs
.as_ls
)
612 user_sgpr_info
->sgpr_count
++;
614 case MESA_SHADER_TESS_CTRL
:
615 user_sgpr_info
->sgpr_count
+= 4;
617 case MESA_SHADER_TESS_EVAL
:
618 user_sgpr_info
->sgpr_count
+= 1;
620 case MESA_SHADER_GEOMETRY
:
621 user_sgpr_info
->sgpr_count
+= 2;
627 if (ctx
->shader_info
->info
.needs_push_constants
)
628 user_sgpr_info
->sgpr_count
+= 2;
630 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
631 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
632 user_sgpr_info
->sgpr_count
+= 2;
633 user_sgpr_info
->indirect_all_descriptor_sets
= true;
635 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
639 static void create_function(struct nir_to_llvm_context
*ctx
)
641 LLVMTypeRef arg_types
[23];
642 unsigned arg_idx
= 0;
643 unsigned array_params_mask
= 0;
644 unsigned sgpr_count
= 0, user_sgpr_count
;
646 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
647 uint8_t user_sgpr_idx
;
648 struct user_sgpr_info user_sgpr_info
;
650 allocate_user_sgprs(ctx
, &user_sgpr_info
);
651 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
652 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* address of rings */
655 /* 1 for each descriptor set */
656 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
657 for (unsigned i
= 0; i
< num_sets
; ++i
) {
658 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
659 array_params_mask
|= (1 << arg_idx
);
660 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
664 array_params_mask
|= (1 << arg_idx
);
665 arg_types
[arg_idx
++] = const_array(const_array(ctx
->i8
, 1024 * 1024), 32);
668 if (ctx
->shader_info
->info
.needs_push_constants
) {
669 /* 1 for push constants and dynamic descriptors */
670 array_params_mask
|= (1 << arg_idx
);
671 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
674 switch (ctx
->stage
) {
675 case MESA_SHADER_COMPUTE
:
676 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
677 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, ctx
->shader_info
->info
.cs
.grid_components_used
); /* grid size */
678 user_sgpr_count
= arg_idx
;
679 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
680 arg_types
[arg_idx
++] = ctx
->i32
;
681 sgpr_count
= arg_idx
;
683 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
685 case MESA_SHADER_VERTEX
:
686 if (!ctx
->is_gs_copy_shader
) {
687 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
688 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
689 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
690 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
691 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
692 arg_types
[arg_idx
++] = ctx
->i32
; // draw index
694 user_sgpr_count
= arg_idx
;
695 if (ctx
->options
->key
.vs
.as_es
)
696 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
697 else if (ctx
->options
->key
.vs
.as_ls
) {
698 arg_types
[arg_idx
++] = ctx
->i32
; //ls out layout
701 sgpr_count
= arg_idx
;
702 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
703 if (!ctx
->is_gs_copy_shader
) {
704 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
705 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
706 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
709 case MESA_SHADER_TESS_CTRL
:
710 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
711 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out offsets
712 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out layout
713 arg_types
[arg_idx
++] = ctx
->i32
; // tcs in layout
714 user_sgpr_count
= arg_idx
;
715 arg_types
[arg_idx
++] = ctx
->i32
; // param oc lds
716 arg_types
[arg_idx
++] = ctx
->i32
; // tess factor offset
717 sgpr_count
= arg_idx
;
718 arg_types
[arg_idx
++] = ctx
->i32
; // patch id
719 arg_types
[arg_idx
++] = ctx
->i32
; // rel ids;
721 case MESA_SHADER_TESS_EVAL
:
722 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
723 user_sgpr_count
= arg_idx
;
724 if (ctx
->options
->key
.tes
.as_es
) {
725 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
726 arg_types
[arg_idx
++] = ctx
->i32
; //
727 arg_types
[arg_idx
++] = ctx
->i32
; // es2gs offset
729 arg_types
[arg_idx
++] = ctx
->i32
; //
730 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
732 sgpr_count
= arg_idx
;
733 arg_types
[arg_idx
++] = ctx
->f32
; // tes_u
734 arg_types
[arg_idx
++] = ctx
->f32
; // tes_v
735 arg_types
[arg_idx
++] = ctx
->i32
; // tes rel patch id
736 arg_types
[arg_idx
++] = ctx
->i32
; // tes patch id
738 case MESA_SHADER_GEOMETRY
:
739 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
740 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
741 user_sgpr_count
= arg_idx
;
742 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
743 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
744 sgpr_count
= arg_idx
;
745 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
746 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
747 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
748 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
749 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
750 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
751 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
752 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
754 case MESA_SHADER_FRAGMENT
:
755 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
756 arg_types
[arg_idx
++] = ctx
->i32
; /* sample position offset */
757 user_sgpr_count
= arg_idx
;
758 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
759 sgpr_count
= arg_idx
;
760 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
761 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
762 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
763 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
764 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
765 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
766 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
767 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
768 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
769 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
770 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
771 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
772 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
773 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
774 arg_types
[arg_idx
++] = ctx
->i32
; /* sample coverage */
775 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
778 unreachable("Shader stage not implemented");
781 ctx
->main_function
= create_llvm_function(
782 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
783 arg_idx
, array_params_mask
, sgpr_count
, ctx
->max_workgroup_size
,
784 ctx
->options
->unsafe_math
);
785 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
787 ctx
->shader_info
->num_input_sgprs
= 0;
788 ctx
->shader_info
->num_input_vgprs
= 0;
790 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
791 for (i
= 0; i
< user_sgpr_count
; i
++)
792 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
794 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
795 for (; i
< sgpr_count
; i
++)
796 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
798 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
799 for (; i
< arg_idx
; ++i
)
800 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
805 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
806 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, &user_sgpr_idx
, 2);
807 if (ctx
->options
->supports_spill
) {
808 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
809 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
810 NULL
, 0, AC_FUNC_ATTR_READNONE
);
811 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
812 const_array(ctx
->v16i8
, 16), "");
814 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
817 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
818 for (unsigned i
= 0; i
< num_sets
; ++i
) {
819 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
820 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], &user_sgpr_idx
, 2);
821 ctx
->descriptor_sets
[i
] =
822 LLVMGetParam(ctx
->main_function
, arg_idx
++);
824 ctx
->descriptor_sets
[i
] = NULL
;
827 uint32_t desc_sgpr_idx
= user_sgpr_idx
;
828 LLVMValueRef desc_sets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
829 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, &user_sgpr_idx
, 2);
831 for (unsigned i
= 0; i
< num_sets
; ++i
) {
832 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
833 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
834 ctx
->descriptor_sets
[i
] = ac_build_indexed_load_const(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->i32
, i
, false));
837 ctx
->descriptor_sets
[i
] = NULL
;
839 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
842 if (ctx
->shader_info
->info
.needs_push_constants
) {
843 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
844 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, &user_sgpr_idx
, 2);
847 switch (ctx
->stage
) {
848 case MESA_SHADER_COMPUTE
:
849 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
850 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, &user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
851 ctx
->num_work_groups
=
852 LLVMGetParam(ctx
->main_function
, arg_idx
++);
855 LLVMGetParam(ctx
->main_function
, arg_idx
++);
857 LLVMGetParam(ctx
->main_function
, arg_idx
++);
858 ctx
->local_invocation_ids
=
859 LLVMGetParam(ctx
->main_function
, arg_idx
++);
861 case MESA_SHADER_VERTEX
:
862 if (!ctx
->is_gs_copy_shader
) {
863 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
864 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, &user_sgpr_idx
, 2);
865 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
868 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
871 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, &user_sgpr_idx
, vs_num
);
873 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
874 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
875 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
876 ctx
->draw_index
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
878 if (ctx
->options
->key
.vs
.as_es
)
879 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
880 else if (ctx
->options
->key
.vs
.as_ls
) {
881 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
882 ctx
->ls_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
884 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
885 if (!ctx
->is_gs_copy_shader
) {
886 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
887 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
888 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
890 if (ctx
->options
->key
.vs
.as_ls
)
891 declare_tess_lds(ctx
);
893 case MESA_SHADER_TESS_CTRL
:
894 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
895 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
896 ctx
->tcs_out_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
897 ctx
->tcs_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
898 ctx
->tcs_in_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
899 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
900 ctx
->tess_factor_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
901 ctx
->tcs_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
902 ctx
->tcs_rel_ids
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
904 declare_tess_lds(ctx
);
906 case MESA_SHADER_TESS_EVAL
:
907 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
908 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
909 if (ctx
->options
->key
.tes
.as_es
) {
910 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
912 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
915 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
917 ctx
->tes_u
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
918 ctx
->tes_v
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
919 ctx
->tes_rel_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
920 ctx
->tes_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
922 case MESA_SHADER_GEOMETRY
:
923 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, &user_sgpr_idx
, 2);
924 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
925 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
926 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
927 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
928 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
929 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
930 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
931 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
932 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
933 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
934 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
935 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
937 case MESA_SHADER_FRAGMENT
:
938 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
939 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, &user_sgpr_idx
, 1);
940 ctx
->sample_pos_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
942 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
943 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
944 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
945 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
947 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
948 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
949 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
950 arg_idx
++; /* line stipple */
951 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
952 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
953 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
954 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
955 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
956 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
957 ctx
->sample_coverage
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
960 unreachable("Shader stage not implemented");
964 static void setup_types(struct nir_to_llvm_context
*ctx
)
966 LLVMValueRef args
[4];
968 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
969 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
970 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
971 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
972 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
973 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
974 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
975 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
976 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
977 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
978 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
979 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
980 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
981 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
982 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
983 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
985 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
986 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
987 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
988 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
989 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
990 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
992 args
[0] = ctx
->f32zero
;
993 args
[1] = ctx
->f32zero
;
994 args
[2] = ctx
->f32zero
;
995 args
[3] = ctx
->f32one
;
996 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
998 ctx
->uniform_md_kind
=
999 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
1000 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
1002 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
1005 static int get_llvm_num_components(LLVMValueRef value
)
1007 LLVMTypeRef type
= LLVMTypeOf(value
);
1008 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1009 ? LLVMGetVectorSize(type
)
1011 return num_components
;
1014 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
1018 int count
= get_llvm_num_components(value
);
1020 assert(index
< count
);
1024 return LLVMBuildExtractElement(ctx
->builder
, value
,
1025 LLVMConstInt(ctx
->i32
, index
, false), "");
1028 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
1029 LLVMValueRef value
, unsigned count
)
1031 unsigned num_components
= get_llvm_num_components(value
);
1032 if (count
== num_components
)
1035 LLVMValueRef masks
[] = {
1036 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1037 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1040 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1043 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1044 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1048 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
1049 LLVMValueRef
*values
,
1050 unsigned value_count
,
1051 unsigned value_stride
,
1054 LLVMBuilderRef builder
= ctx
->builder
;
1057 if (value_count
== 1) {
1058 LLVMBuildStore(builder
, vec
, values
[0]);
1062 for (i
= 0; i
< value_count
; i
++) {
1063 LLVMValueRef ptr
= values
[i
* value_stride
];
1064 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
1065 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1066 LLVMBuildStore(builder
, value
, ptr
);
1070 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
1073 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
1074 if (def
->num_components
> 1) {
1075 type
= LLVMVectorType(type
, def
->num_components
);
1080 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
1083 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
1084 return (LLVMValueRef
)entry
->data
;
1088 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
1089 struct nir_block
*b
)
1091 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
1092 return (LLVMBasicBlockRef
)entry
->data
;
1095 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
1097 unsigned num_components
)
1099 LLVMValueRef value
= get_src(ctx
, src
.src
);
1100 bool need_swizzle
= false;
1103 LLVMTypeRef type
= LLVMTypeOf(value
);
1104 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1105 ? LLVMGetVectorSize(type
)
1108 for (unsigned i
= 0; i
< num_components
; ++i
) {
1109 assert(src
.swizzle
[i
] < src_components
);
1110 if (src
.swizzle
[i
] != i
)
1111 need_swizzle
= true;
1114 if (need_swizzle
|| num_components
!= src_components
) {
1115 LLVMValueRef masks
[] = {
1116 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
1117 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
1118 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
1119 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
1121 if (src_components
> 1 && num_components
== 1) {
1122 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
1124 } else if (src_components
== 1 && num_components
> 1) {
1125 LLVMValueRef values
[] = {value
, value
, value
, value
};
1126 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1128 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1129 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
1133 assert(!src
.negate
);
1138 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
1139 LLVMIntPredicate pred
, LLVMValueRef src0
,
1142 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1143 return LLVMBuildSelect(ctx
->builder
, result
,
1144 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1145 LLVMConstInt(ctx
->i32
, 0, false), "");
1148 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
1149 LLVMRealPredicate pred
, LLVMValueRef src0
,
1152 LLVMValueRef result
;
1153 src0
= to_float(ctx
, src0
);
1154 src1
= to_float(ctx
, src1
);
1155 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1156 return LLVMBuildSelect(ctx
->builder
, result
,
1157 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1158 LLVMConstInt(ctx
->i32
, 0, false), "");
1161 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
1163 LLVMTypeRef result_type
,
1167 LLVMValueRef params
[] = {
1168 to_float(ctx
, src0
),
1171 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1172 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1175 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
1177 LLVMTypeRef result_type
,
1178 LLVMValueRef src0
, LLVMValueRef src1
)
1181 LLVMValueRef params
[] = {
1182 to_float(ctx
, src0
),
1183 to_float(ctx
, src1
),
1186 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1187 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1190 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
1192 LLVMTypeRef result_type
,
1193 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1196 LLVMValueRef params
[] = {
1197 to_float(ctx
, src0
),
1198 to_float(ctx
, src1
),
1199 to_float(ctx
, src2
),
1202 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1203 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1206 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
1207 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1209 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1211 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1214 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
1217 LLVMValueRef params
[2] = {
1220 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1221 * add special code to check for x=0. The reason is that
1222 * the LLVM behavior for x=0 is different from what we
1225 * The hardware already implements the correct behavior.
1227 LLVMConstInt(ctx
->i32
, 1, false),
1229 return ac_build_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
1232 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
1235 return ac_build_imsb(&ctx
->ac
, src0
, ctx
->i32
);
1238 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
1241 return ac_build_umsb(&ctx
->ac
, src0
, ctx
->i32
);
1244 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
1245 LLVMIntPredicate pred
,
1246 LLVMValueRef src0
, LLVMValueRef src1
)
1248 return LLVMBuildSelect(ctx
->builder
,
1249 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1254 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
1257 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1258 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1261 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1264 LLVMValueRef cmp
, val
;
1266 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1267 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1268 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1269 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1273 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1276 LLVMValueRef cmp
, val
;
1278 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1279 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1280 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1281 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1285 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1288 const char *intr
= "llvm.floor.f32";
1289 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
1290 LLVMValueRef params
[] = {
1293 LLVMValueRef floor
= ac_build_intrinsic(&ctx
->ac
, intr
,
1294 ctx
->f32
, params
, 1,
1295 AC_FUNC_ATTR_READNONE
);
1296 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1299 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1301 LLVMValueRef src0
, LLVMValueRef src1
)
1303 LLVMTypeRef ret_type
;
1304 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1306 LLVMValueRef params
[] = { src0
, src1
};
1307 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1310 res
= ac_build_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1311 params
, 2, AC_FUNC_ATTR_READNONE
);
1313 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1314 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1318 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1321 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1324 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1327 LLVMValueRef result
;
1330 src0
= to_float(ctx
, src0
);
1331 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
1333 /* TODO SI/CIK options here */
1334 if (ctx
->options
->chip_class
>= VI
) {
1335 LLVMValueRef args
[2];
1336 /* Check if the result is a denormal - and flush to 0 if so. */
1338 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1339 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1342 /* need to convert back up to f32 */
1343 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1345 if (ctx
->options
->chip_class
>= VI
)
1346 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32zero
, result
, "");
1351 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1352 LLVMValueRef src0
, LLVMValueRef src1
)
1354 LLVMValueRef dst64
, result
;
1355 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1356 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1358 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1359 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1360 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1364 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1365 LLVMValueRef src0
, LLVMValueRef src1
)
1367 LLVMValueRef dst64
, result
;
1368 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1369 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1371 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1372 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1373 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1377 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1379 LLVMValueRef srcs
[3])
1381 LLVMValueRef result
;
1382 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1384 result
= ac_build_bfe(&ctx
->ac
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1385 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1389 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1390 LLVMValueRef src0
, LLVMValueRef src1
,
1391 LLVMValueRef src2
, LLVMValueRef src3
)
1393 LLVMValueRef bfi_args
[3], result
;
1395 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1396 LLVMBuildSub(ctx
->builder
,
1397 LLVMBuildShl(ctx
->builder
,
1402 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1405 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1408 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1409 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1411 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1412 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1413 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1415 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1419 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1422 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1424 LLVMValueRef comp
[2];
1426 src0
= to_float(ctx
, src0
);
1427 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1428 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1429 for (i
= 0; i
< 2; i
++) {
1430 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1431 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1432 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1435 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1436 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1441 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1444 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1445 LLVMValueRef temps
[2], result
, val
;
1448 for (i
= 0; i
< 2; i
++) {
1449 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1450 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1451 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1452 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1455 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1457 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1462 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1468 LLVMValueRef result
;
1469 ctx
->has_ddxy
= true;
1471 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1472 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1473 LLVMArrayType(ctx
->i32
, 64),
1474 "ddxy_lds", LOCAL_ADDR_SPACE
);
1476 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1477 mask
= AC_TID_MASK_LEFT
;
1478 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1479 mask
= AC_TID_MASK_TOP
;
1481 mask
= AC_TID_MASK_TOP_LEFT
;
1483 /* for DDX we want to next X pixel, DDY next Y pixel. */
1484 if (op
== nir_op_fddx_fine
||
1485 op
== nir_op_fddx_coarse
||
1491 result
= ac_build_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1492 mask
, idx
, ctx
->lds
,
1498 * this takes an I,J coordinate pair,
1499 * and works out the X and Y derivatives.
1500 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1502 static LLVMValueRef
emit_ddxy_interp(
1503 struct nir_to_llvm_context
*ctx
,
1504 LLVMValueRef interp_ij
)
1506 LLVMValueRef result
[4], a
;
1509 for (i
= 0; i
< 2; i
++) {
1510 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1511 LLVMConstInt(ctx
->i32
, i
, false), "");
1512 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1513 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1515 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1518 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1520 LLVMValueRef src
[4], result
= NULL
;
1521 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1522 unsigned src_components
;
1523 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1525 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1526 switch (instr
->op
) {
1532 case nir_op_pack_half_2x16
:
1535 case nir_op_unpack_half_2x16
:
1539 src_components
= num_components
;
1542 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1543 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1545 switch (instr
->op
) {
1551 src
[0] = to_float(ctx
, src
[0]);
1552 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1555 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1558 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1561 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1564 src
[0] = to_float(ctx
, src
[0]);
1565 src
[1] = to_float(ctx
, src
[1]);
1566 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1569 src
[0] = to_float(ctx
, src
[0]);
1570 src
[1] = to_float(ctx
, src
[1]);
1571 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1574 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1577 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1580 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1583 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1586 src
[0] = to_float(ctx
, src
[0]);
1587 src
[1] = to_float(ctx
, src
[1]);
1588 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1589 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1590 to_float_type(ctx
, def_type
), result
);
1591 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1592 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1595 src
[0] = to_float(ctx
, src
[0]);
1596 src
[1] = to_float(ctx
, src
[1]);
1597 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1600 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1603 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1606 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1609 src
[0] = to_float(ctx
, src
[0]);
1610 src
[1] = to_float(ctx
, src
[1]);
1611 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1614 src
[0] = to_float(ctx
, src
[0]);
1615 src
[1] = to_float(ctx
, src
[1]);
1616 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1619 src
[0] = to_float(ctx
, src
[0]);
1620 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1623 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1626 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1629 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1632 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1635 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1638 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1641 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1644 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1647 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1650 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1653 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1656 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1659 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1662 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1665 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1668 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1671 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1672 to_float_type(ctx
, def_type
), src
[0]);
1675 result
= emit_iabs(ctx
, src
[0]);
1678 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1681 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1684 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1687 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1690 result
= emit_isign(ctx
, src
[0]);
1693 src
[0] = to_float(ctx
, src
[0]);
1694 result
= emit_fsign(ctx
, src
[0]);
1697 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1698 to_float_type(ctx
, def_type
), src
[0]);
1701 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1702 to_float_type(ctx
, def_type
), src
[0]);
1705 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1706 to_float_type(ctx
, def_type
), src
[0]);
1708 case nir_op_fround_even
:
1709 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1710 to_float_type(ctx
, def_type
),src
[0]);
1713 result
= emit_ffract(ctx
, src
[0]);
1716 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1717 to_float_type(ctx
, def_type
), src
[0]);
1720 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1721 to_float_type(ctx
, def_type
), src
[0]);
1724 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1725 to_float_type(ctx
, def_type
), src
[0]);
1728 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1729 to_float_type(ctx
, def_type
), src
[0]);
1732 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1733 to_float_type(ctx
, def_type
), src
[0]);
1736 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1737 to_float_type(ctx
, def_type
), src
[0]);
1738 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1741 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1742 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1745 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1746 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1747 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1748 result
= emit_intrin_1f_param(ctx
, "llvm.canonicalize",
1749 to_float_type(ctx
, def_type
),
1753 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1754 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1755 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1756 result
= emit_intrin_1f_param(ctx
, "llvm.canonicalize",
1757 to_float_type(ctx
, def_type
),
1761 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1762 to_float_type(ctx
, def_type
), src
[0], src
[1], src
[2]);
1764 case nir_op_ibitfield_extract
:
1765 result
= emit_bitfield_extract(ctx
, true, src
);
1767 case nir_op_ubitfield_extract
:
1768 result
= emit_bitfield_extract(ctx
, false, src
);
1770 case nir_op_bitfield_insert
:
1771 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1773 case nir_op_bitfield_reverse
:
1774 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1776 case nir_op_bit_count
:
1777 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1782 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1783 src
[i
] = to_integer(ctx
, src
[i
]);
1784 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1788 src
[0] = to_float(ctx
, src
[0]);
1789 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1793 src
[0] = to_float(ctx
, src
[0]);
1794 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1798 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1802 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1805 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1808 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1812 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1813 result
= LLVMBuildZExt(ctx
->builder
, src
[0], def_type
, "");
1815 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1819 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1820 result
= LLVMBuildSExt(ctx
->builder
, src
[0], def_type
, "");
1822 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1825 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1827 case nir_op_find_lsb
:
1828 result
= emit_find_lsb(ctx
, src
[0]);
1830 case nir_op_ufind_msb
:
1831 result
= emit_ufind_msb(ctx
, src
[0]);
1833 case nir_op_ifind_msb
:
1834 result
= emit_ifind_msb(ctx
, src
[0]);
1836 case nir_op_uadd_carry
:
1837 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1839 case nir_op_usub_borrow
:
1840 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1843 result
= emit_b2f(ctx
, src
[0]);
1845 case nir_op_fquantize2f16
:
1846 result
= emit_f2f16(ctx
, src
[0]);
1848 case nir_op_umul_high
:
1849 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1851 case nir_op_imul_high
:
1852 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1854 case nir_op_pack_half_2x16
:
1855 result
= emit_pack_half_2x16(ctx
, src
[0]);
1857 case nir_op_unpack_half_2x16
:
1858 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1862 case nir_op_fddx_fine
:
1863 case nir_op_fddy_fine
:
1864 case nir_op_fddx_coarse
:
1865 case nir_op_fddy_coarse
:
1866 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1869 fprintf(stderr
, "Unknown NIR alu instr: ");
1870 nir_print_instr(&instr
->instr
, stderr
);
1871 fprintf(stderr
, "\n");
1876 assert(instr
->dest
.dest
.is_ssa
);
1877 result
= to_integer(ctx
, result
);
1878 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1883 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1884 nir_load_const_instr
*instr
)
1886 LLVMValueRef values
[4], value
= NULL
;
1887 LLVMTypeRef element_type
=
1888 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1890 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1891 switch (instr
->def
.bit_size
) {
1893 values
[i
] = LLVMConstInt(element_type
,
1894 instr
->value
.u32
[i
], false);
1897 values
[i
] = LLVMConstInt(element_type
,
1898 instr
->value
.u64
[i
], false);
1902 "unsupported nir load_const bit_size: %d\n",
1903 instr
->def
.bit_size
);
1907 if (instr
->def
.num_components
> 1) {
1908 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1912 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1915 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1918 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1919 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1920 LLVMPointerType(type
, addr_space
), "");
1924 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1927 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1928 LLVMConstInt(ctx
->i32
, 2, false), "");
1931 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1932 /* On VI, the descriptor contains the size in bytes,
1933 * but TXQ must return the size in elements.
1934 * The stride is always non-zero for resources using TXQ.
1936 LLVMValueRef stride
=
1937 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1938 LLVMConstInt(ctx
->i32
, 1, false), "");
1939 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1940 LLVMConstInt(ctx
->i32
, 16, false), "");
1941 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1942 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1944 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1950 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1953 static void build_int_type_name(
1955 char *buf
, unsigned bufsize
)
1957 assert(bufsize
>= 6);
1959 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1960 snprintf(buf
, bufsize
, "v%ui32",
1961 LLVMGetVectorSize(type
));
1966 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1967 struct ac_image_args
*args
,
1968 nir_tex_instr
*instr
)
1970 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1971 LLVMValueRef coord
= args
->addr
;
1972 LLVMValueRef half_texel
[2];
1973 LLVMValueRef compare_cube_wa
;
1974 LLVMValueRef result
;
1976 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1980 struct ac_image_args txq_args
= { 0 };
1982 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1983 txq_args
.opcode
= ac_image_get_resinfo
;
1984 txq_args
.dmask
= 0xf;
1985 txq_args
.addr
= ctx
->i32zero
;
1986 txq_args
.resource
= args
->resource
;
1987 LLVMValueRef size
= ac_build_image_opcode(&ctx
->ac
, &txq_args
);
1989 for (c
= 0; c
< 2; c
++) {
1990 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1991 LLVMConstInt(ctx
->i32
, c
, false), "");
1992 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1993 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1994 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1995 LLVMConstReal(ctx
->f32
, -0.5), "");
1999 LLVMValueRef orig_coords
= args
->addr
;
2001 for (c
= 0; c
< 2; c
++) {
2003 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2004 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2005 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2006 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2007 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2008 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2013 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2014 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2015 * workaround by sampling using a scaled type and converting.
2016 * This is taken from amdgpu-pro shaders.
2018 /* NOTE this produces some ugly code compared to amdgpu-pro,
2019 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2020 * and then reads them back. -pro generates two selects,
2021 * one s_cmp for the descriptor rewriting
2022 * one v_cmp for the coordinate and result changes.
2024 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2025 LLVMValueRef tmp
, tmp2
;
2027 /* workaround 8/8/8/8 uint/sint cube gather bug */
2028 /* first detect it then change to a scaled read and f2i */
2029 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32one
, "");
2032 /* extract the DATA_FORMAT */
2033 tmp
= ac_build_bfe(&ctx
->ac
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2034 LLVMConstInt(ctx
->i32
, 6, false), false);
2036 /* is the DATA_FORMAT == 8_8_8_8 */
2037 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2039 if (stype
== GLSL_TYPE_UINT
)
2040 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2041 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2042 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2044 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2045 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2046 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2048 /* replace the NUM FORMAT in the descriptor */
2049 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2050 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2052 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32one
, "");
2054 /* don't modify the coordinates for this case */
2055 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2058 result
= ac_build_image_opcode(&ctx
->ac
, args
);
2060 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2061 LLVMValueRef tmp
, tmp2
;
2063 /* if the cube workaround is in place, f2i the result. */
2064 for (c
= 0; c
< 4; c
++) {
2065 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2066 if (stype
== GLSL_TYPE_UINT
)
2067 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2069 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2070 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2071 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2072 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2073 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2074 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2080 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
2081 nir_tex_instr
*instr
,
2083 struct ac_image_args
*args
)
2085 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2086 return ac_build_buffer_load_format(&ctx
->ac
,
2089 LLVMConstInt(ctx
->i32
, 0, false),
2093 args
->opcode
= ac_image_sample
;
2094 args
->compare
= instr
->is_shadow
;
2096 switch (instr
->op
) {
2098 case nir_texop_txf_ms
:
2099 case nir_texop_samples_identical
:
2100 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2101 args
->compare
= false;
2102 args
->offset
= false;
2109 args
->level_zero
= true;
2114 case nir_texop_query_levels
:
2115 args
->opcode
= ac_image_get_resinfo
;
2118 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2119 args
->level_zero
= true;
2125 args
->opcode
= ac_image_gather4
;
2126 args
->level_zero
= true;
2129 args
->opcode
= ac_image_get_lod
;
2130 args
->compare
= false;
2131 args
->offset
= false;
2137 if (instr
->op
== nir_texop_tg4
) {
2138 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2139 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2140 return radv_lower_gather4_integer(ctx
, args
, instr
);
2143 return ac_build_image_opcode(&ctx
->ac
, args
);
2146 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2147 nir_intrinsic_instr
*instr
)
2149 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2150 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2151 unsigned binding
= nir_intrinsic_binding(instr
);
2152 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2153 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2154 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2155 unsigned base_offset
= layout
->binding
[binding
].offset
;
2156 LLVMValueRef offset
, stride
;
2158 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2159 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2160 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2161 layout
->binding
[binding
].dynamic_offset_offset
;
2162 desc_ptr
= ctx
->push_constants
;
2163 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2164 stride
= LLVMConstInt(ctx
->i32
, 16, false);
2166 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
2168 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
2169 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2170 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2172 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2173 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2174 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2176 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2179 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2180 nir_intrinsic_instr
*instr
)
2182 LLVMValueRef ptr
, addr
;
2184 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
2185 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
2187 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2188 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
2190 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2193 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
2194 nir_intrinsic_instr
*instr
)
2196 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2198 return get_buffer_size(ctx
, desc
, false);
2200 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
2201 nir_intrinsic_instr
*instr
)
2203 const char *store_name
;
2204 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2205 LLVMTypeRef data_type
= ctx
->f32
;
2206 int elem_size_mult
= get_elem_bits(ctx
, LLVMTypeOf(src_data
)) / 32;
2207 int components_32bit
= elem_size_mult
* instr
->num_components
;
2208 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2209 LLVMValueRef base_data
, base_offset
;
2210 LLVMValueRef params
[6];
2212 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2213 ctx
->shader_info
->fs
.writes_memory
= true;
2215 params
[1] = get_src(ctx
, instr
->src
[1]);
2216 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2217 params
[4] = ctx
->i1false
; /* glc */
2218 params
[5] = ctx
->i1false
; /* slc */
2220 if (components_32bit
> 1)
2221 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
2223 base_data
= to_float(ctx
, src_data
);
2224 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
2225 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
2227 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2231 LLVMValueRef offset
;
2233 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2235 /* Due to an LLVM limitation, split 3-element writes
2236 * into a 2-element and a 1-element write. */
2238 writemask
|= 1 << (start
+ 2);
2242 start
*= elem_size_mult
;
2243 count
*= elem_size_mult
;
2246 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2251 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2253 } else if (count
== 2) {
2254 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2255 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
2256 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
2259 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2260 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
2261 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
2263 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2267 if (get_llvm_num_components(base_data
) > 1)
2268 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
2269 LLVMConstInt(ctx
->i32
, start
, false), "");
2272 store_name
= "llvm.amdgcn.buffer.store.f32";
2275 offset
= base_offset
;
2277 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
2281 ac_build_intrinsic(&ctx
->ac
, store_name
,
2282 ctx
->voidt
, params
, 6, 0);
2286 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
2287 nir_intrinsic_instr
*instr
)
2290 LLVMValueRef params
[6];
2292 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2293 ctx
->shader_info
->fs
.writes_memory
= true;
2295 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2296 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
2298 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
2299 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
2300 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2301 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2302 params
[arg_count
++] = ctx
->i1false
; /* slc */
2304 switch (instr
->intrinsic
) {
2305 case nir_intrinsic_ssbo_atomic_add
:
2306 name
= "llvm.amdgcn.buffer.atomic.add";
2308 case nir_intrinsic_ssbo_atomic_imin
:
2309 name
= "llvm.amdgcn.buffer.atomic.smin";
2311 case nir_intrinsic_ssbo_atomic_umin
:
2312 name
= "llvm.amdgcn.buffer.atomic.umin";
2314 case nir_intrinsic_ssbo_atomic_imax
:
2315 name
= "llvm.amdgcn.buffer.atomic.smax";
2317 case nir_intrinsic_ssbo_atomic_umax
:
2318 name
= "llvm.amdgcn.buffer.atomic.umax";
2320 case nir_intrinsic_ssbo_atomic_and
:
2321 name
= "llvm.amdgcn.buffer.atomic.and";
2323 case nir_intrinsic_ssbo_atomic_or
:
2324 name
= "llvm.amdgcn.buffer.atomic.or";
2326 case nir_intrinsic_ssbo_atomic_xor
:
2327 name
= "llvm.amdgcn.buffer.atomic.xor";
2329 case nir_intrinsic_ssbo_atomic_exchange
:
2330 name
= "llvm.amdgcn.buffer.atomic.swap";
2332 case nir_intrinsic_ssbo_atomic_comp_swap
:
2333 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2339 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2342 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2343 nir_intrinsic_instr
*instr
)
2345 LLVMValueRef results
[2];
2346 int load_components
;
2347 int num_components
= instr
->num_components
;
2348 if (instr
->dest
.ssa
.bit_size
== 64)
2349 num_components
*= 2;
2351 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2352 load_components
= MIN2(num_components
- i
, 4);
2353 const char *load_name
;
2354 LLVMTypeRef data_type
= ctx
->f32
;
2355 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
2356 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2358 if (load_components
== 3)
2359 data_type
= LLVMVectorType(ctx
->f32
, 4);
2360 else if (load_components
> 1)
2361 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
2363 if (load_components
>= 3)
2364 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2365 else if (load_components
== 2)
2366 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2367 else if (load_components
== 1)
2368 load_name
= "llvm.amdgcn.buffer.load.f32";
2370 unreachable("unhandled number of components");
2372 LLVMValueRef params
[] = {
2373 get_src(ctx
, instr
->src
[0]),
2374 LLVMConstInt(ctx
->i32
, 0, false),
2380 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2384 LLVMValueRef ret
= results
[0];
2385 if (num_components
> 4 || num_components
== 3) {
2386 LLVMValueRef masks
[] = {
2387 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2388 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2389 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2390 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2393 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2394 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2395 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2398 return LLVMBuildBitCast(ctx
->builder
, ret
,
2399 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2402 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2403 nir_intrinsic_instr
*instr
)
2405 LLVMValueRef results
[8], ret
;
2406 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2407 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2408 int num_components
= instr
->num_components
;
2410 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2412 if (instr
->dest
.ssa
.bit_size
== 64)
2413 num_components
*= 2;
2415 for (unsigned i
= 0; i
< num_components
; ++i
) {
2416 LLVMValueRef params
[] = {
2418 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2421 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2423 AC_FUNC_ATTR_READNONE
|
2424 AC_FUNC_ATTR_LEGACY
);
2428 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2429 return LLVMBuildBitCast(ctx
->builder
, ret
,
2430 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2434 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref_var
*deref
,
2435 bool vs_in
, unsigned *vertex_index_out
,
2436 LLVMValueRef
*vertex_index_ref
,
2437 unsigned *const_out
, LLVMValueRef
*indir_out
)
2439 unsigned const_offset
= 0;
2440 nir_deref
*tail
= &deref
->deref
;
2441 LLVMValueRef offset
= NULL
;
2443 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2445 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2446 if (vertex_index_out
)
2447 *vertex_index_out
= deref_array
->base_offset
;
2449 if (vertex_index_ref
) {
2450 LLVMValueRef vtx
= LLVMConstInt(ctx
->i32
, deref_array
->base_offset
, false);
2451 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2452 vtx
= LLVMBuildAdd(ctx
->builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2454 *vertex_index_ref
= vtx
;
2458 if (deref
->var
->data
.compact
) {
2459 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2460 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2461 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2462 /* We always lower indirect dereferences for "compact" array vars. */
2463 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2465 const_offset
= deref_array
->base_offset
;
2469 while (tail
->child
!= NULL
) {
2470 const struct glsl_type
*parent_type
= tail
->type
;
2473 if (tail
->deref_type
== nir_deref_type_array
) {
2474 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2475 LLVMValueRef index
, stride
, local_offset
;
2476 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2478 const_offset
+= size
* deref_array
->base_offset
;
2479 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2482 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2483 index
= get_src(ctx
, deref_array
->indirect
);
2484 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2485 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2488 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2490 offset
= local_offset
;
2491 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2492 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2494 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2495 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2496 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2499 unreachable("unsupported deref type");
2503 if (const_offset
&& offset
)
2504 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2505 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2508 *const_out
= const_offset
;
2509 *indir_out
= offset
;
2513 lds_load(struct nir_to_llvm_context
*ctx
,
2514 LLVMValueRef dw_addr
)
2517 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
2522 lds_store(struct nir_to_llvm_context
*ctx
,
2523 LLVMValueRef dw_addr
, LLVMValueRef value
)
2525 value
= LLVMBuildBitCast(ctx
->builder
, value
, ctx
->i32
, "");
2526 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
2530 /* The offchip buffer layout for TCS->TES is
2532 * - attribute 0 of patch 0 vertex 0
2533 * - attribute 0 of patch 0 vertex 1
2534 * - attribute 0 of patch 0 vertex 2
2536 * - attribute 0 of patch 1 vertex 0
2537 * - attribute 0 of patch 1 vertex 1
2539 * - attribute 1 of patch 0 vertex 0
2540 * - attribute 1 of patch 0 vertex 1
2542 * - per patch attribute 0 of patch 0
2543 * - per patch attribute 0 of patch 1
2546 * Note that every attribute has 4 components.
2548 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2549 LLVMValueRef vertex_index
,
2550 LLVMValueRef param_index
)
2552 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2553 LLVMValueRef param_stride
, constant16
;
2554 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2556 vertices_per_patch
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 9, 6);
2557 num_patches
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 0, 9);
2558 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2561 constant16
= LLVMConstInt(ctx
->i32
, 16, false);
2563 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2564 vertices_per_patch
, "");
2566 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2569 param_stride
= total_vertices
;
2571 base_addr
= rel_patch_id
;
2572 param_stride
= num_patches
;
2575 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2576 LLVMBuildMul(ctx
->builder
, param_index
,
2577 param_stride
, ""), "");
2579 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2581 if (!vertex_index
) {
2582 LLVMValueRef patch_data_offset
=
2583 unpack_param(ctx
, ctx
->tcs_offchip_layout
, 16, 16);
2585 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2586 patch_data_offset
, "");
2591 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2593 unsigned const_index
,
2595 LLVMValueRef vertex_index
,
2596 LLVMValueRef indir_index
)
2598 LLVMValueRef param_index
;
2601 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, param
, false),
2604 if (const_index
&& !is_compact
)
2605 param
+= const_index
;
2606 param_index
= LLVMConstInt(ctx
->i32
, param
, false);
2608 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2612 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2613 bool is_patch
, uint32_t param
)
2617 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2619 ctx
->tess_outputs_written
|= (1ull << param
);
2623 get_dw_address(struct nir_to_llvm_context
*ctx
,
2624 LLVMValueRef dw_addr
,
2626 unsigned const_index
,
2627 bool compact_const_index
,
2628 LLVMValueRef vertex_index
,
2629 LLVMValueRef stride
,
2630 LLVMValueRef indir_index
)
2635 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2636 LLVMBuildMul(ctx
->builder
,
2642 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2643 LLVMBuildMul(ctx
->builder
, indir_index
,
2644 LLVMConstInt(ctx
->i32
, 4, false), ""), "");
2645 else if (const_index
&& !compact_const_index
)
2646 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2647 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2649 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2650 LLVMConstInt(ctx
->i32
, param
* 4, false), "");
2652 if (const_index
&& compact_const_index
)
2653 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2654 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2659 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2660 nir_intrinsic_instr
*instr
)
2662 LLVMValueRef dw_addr
, stride
;
2663 unsigned const_index
;
2664 LLVMValueRef vertex_index
;
2665 LLVMValueRef indir_index
;
2667 LLVMValueRef value
[4], result
;
2668 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2669 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2670 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2671 radv_get_deref_offset(ctx
, instr
->variables
[0],
2672 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2673 &const_index
, &indir_index
);
2675 stride
= unpack_param(ctx
, ctx
->tcs_in_layout
, 13, 8);
2676 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2677 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2680 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2681 value
[i
] = lds_load(ctx
, dw_addr
);
2682 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2685 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2686 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2691 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2692 nir_intrinsic_instr
*instr
)
2694 LLVMValueRef dw_addr
, stride
;
2695 LLVMValueRef value
[4], result
;
2696 LLVMValueRef vertex_index
= NULL
;
2697 LLVMValueRef indir_index
= NULL
;
2698 unsigned const_index
= 0;
2700 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2701 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2702 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2703 radv_get_deref_offset(ctx
, instr
->variables
[0],
2704 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2705 &const_index
, &indir_index
);
2707 if (!instr
->variables
[0]->var
->data
.patch
) {
2708 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2709 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2711 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2714 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2717 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2718 value
[i
] = lds_load(ctx
, dw_addr
);
2719 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2722 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2723 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2728 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2729 nir_intrinsic_instr
*instr
,
2733 LLVMValueRef stride
, dw_addr
;
2734 LLVMValueRef buf_addr
= NULL
;
2735 LLVMValueRef vertex_index
= NULL
;
2736 LLVMValueRef indir_index
= NULL
;
2737 unsigned const_index
= 0;
2739 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2740 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2742 radv_get_deref_offset(ctx
, instr
->variables
[0],
2743 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2744 &const_index
, &indir_index
);
2746 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2747 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2748 is_compact
&& const_index
> 3) {
2753 if (!instr
->variables
[0]->var
->data
.patch
) {
2754 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2755 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2757 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2760 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2762 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2764 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2765 vertex_index
, indir_index
);
2767 unsigned base
= is_compact
? const_index
: 0;
2768 for (unsigned chan
= 0; chan
< 8; chan
++) {
2769 bool is_tess_factor
= false;
2770 if (!(writemask
& (1 << chan
)))
2772 LLVMValueRef value
= llvm_extract_elem(ctx
, src
, chan
);
2774 lds_store(ctx
, dw_addr
, value
);
2776 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2777 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2778 is_tess_factor
= true;
2780 if (!is_tess_factor
&& writemask
!= 0xF)
2781 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2782 buf_addr
, ctx
->oc_lds
,
2783 4 * (base
+ chan
), 1, 0, true, false);
2785 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2789 if (writemask
== 0xF) {
2790 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2791 buf_addr
, ctx
->oc_lds
,
2792 (base
* 4), 1, 0, true, false);
2797 load_tes_input(struct nir_to_llvm_context
*ctx
,
2798 nir_intrinsic_instr
*instr
)
2800 LLVMValueRef buf_addr
;
2801 LLVMValueRef result
;
2802 LLVMValueRef vertex_index
= NULL
;
2803 LLVMValueRef indir_index
= NULL
;
2804 unsigned const_index
= 0;
2806 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2807 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2809 radv_get_deref_offset(ctx
, instr
->variables
[0],
2810 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2811 &const_index
, &indir_index
);
2812 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2813 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2814 is_compact
&& const_index
> 3) {
2818 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2819 is_compact
, vertex_index
, indir_index
);
2821 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2822 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2823 result
= trim_vector(ctx
, result
, instr
->num_components
);
2824 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2829 load_gs_input(struct nir_to_llvm_context
*ctx
,
2830 nir_intrinsic_instr
*instr
)
2832 LLVMValueRef indir_index
, vtx_offset
;
2833 unsigned const_index
;
2834 LLVMValueRef args
[9];
2835 unsigned param
, vtx_offset_param
;
2836 LLVMValueRef value
[4], result
;
2837 unsigned vertex_index
;
2838 radv_get_deref_offset(ctx
, instr
->variables
[0],
2839 false, &vertex_index
, NULL
,
2840 &const_index
, &indir_index
);
2841 vtx_offset_param
= vertex_index
;
2842 assert(vtx_offset_param
< 6);
2843 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2844 LLVMConstInt(ctx
->i32
, 4, false), "");
2846 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2847 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2849 args
[0] = ctx
->esgs_ring
;
2850 args
[1] = vtx_offset
;
2851 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2852 args
[3] = ctx
->i32zero
;
2853 args
[4] = ctx
->i32one
; /* OFFEN */
2854 args
[5] = ctx
->i32zero
; /* IDXEN */
2855 args
[6] = ctx
->i32one
; /* GLC */
2856 args
[7] = ctx
->i32zero
; /* SLC */
2857 args
[8] = ctx
->i32zero
; /* TFE */
2859 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2861 AC_FUNC_ATTR_READONLY
|
2862 AC_FUNC_ATTR_LEGACY
);
2864 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2869 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2870 nir_intrinsic_instr
*instr
)
2872 LLVMValueRef values
[8];
2873 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2874 int ve
= instr
->dest
.ssa
.num_components
;
2875 LLVMValueRef indir_index
;
2877 unsigned const_index
;
2878 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2879 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2880 radv_get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
2881 &const_index
, &indir_index
);
2883 if (instr
->dest
.ssa
.bit_size
== 64)
2886 switch (instr
->variables
[0]->var
->data
.mode
) {
2887 case nir_var_shader_in
:
2888 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2889 return load_tcs_input(ctx
, instr
);
2890 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
2891 return load_tes_input(ctx
, instr
);
2892 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2893 return load_gs_input(ctx
, instr
);
2895 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2897 unsigned count
= glsl_count_attribute_slots(
2898 instr
->variables
[0]->var
->type
,
2899 ctx
->stage
== MESA_SHADER_VERTEX
);
2901 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2902 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2905 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2909 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2913 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2915 unsigned count
= glsl_count_attribute_slots(
2916 instr
->variables
[0]->var
->type
, false);
2918 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2919 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2922 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2926 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2930 case nir_var_shader_out
:
2931 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2932 return load_tcs_output(ctx
, instr
);
2933 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2935 unsigned count
= glsl_count_attribute_slots(
2936 instr
->variables
[0]->var
->type
, false);
2938 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2939 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2942 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2946 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2947 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2952 case nir_var_shared
: {
2953 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2954 LLVMValueRef derived_ptr
;
2957 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2959 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2960 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2962 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2963 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2965 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2970 unreachable("unhandle variable mode");
2972 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2973 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2977 visit_store_var(struct nir_to_llvm_context
*ctx
,
2978 nir_intrinsic_instr
*instr
)
2980 LLVMValueRef temp_ptr
, value
;
2981 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2982 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2983 int writemask
= instr
->const_index
[0];
2984 LLVMValueRef indir_index
;
2985 unsigned const_index
;
2986 radv_get_deref_offset(ctx
, instr
->variables
[0], false,
2987 NULL
, NULL
, &const_index
, &indir_index
);
2989 if (get_elem_bits(ctx
, LLVMTypeOf(src
)) == 64) {
2990 int old_writemask
= writemask
;
2992 src
= LLVMBuildBitCast(ctx
->builder
, src
,
2993 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
2997 for (unsigned chan
= 0; chan
< 4; chan
++) {
2998 if (old_writemask
& (1 << chan
))
2999 writemask
|= 3u << (2 * chan
);
3003 switch (instr
->variables
[0]->var
->data
.mode
) {
3004 case nir_var_shader_out
:
3006 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3007 store_tcs_output(ctx
, instr
, src
, writemask
);
3011 for (unsigned chan
= 0; chan
< 8; chan
++) {
3013 if (!(writemask
& (1 << chan
)))
3016 value
= llvm_extract_elem(ctx
, src
, chan
);
3018 if (instr
->variables
[0]->var
->data
.compact
)
3021 unsigned count
= glsl_count_attribute_slots(
3022 instr
->variables
[0]->var
->type
, false);
3024 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3025 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3028 if (get_llvm_num_components(tmp_vec
) > 1) {
3029 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
3030 value
, indir_index
, "");
3033 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
3034 count
, stride
, tmp_vec
);
3037 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3039 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
3044 for (unsigned chan
= 0; chan
< 8; chan
++) {
3045 if (!(writemask
& (1 << chan
)))
3048 value
= llvm_extract_elem(ctx
, src
, chan
);
3050 unsigned count
= glsl_count_attribute_slots(
3051 instr
->variables
[0]->var
->type
, false);
3053 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3054 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3057 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
3058 value
, indir_index
, "");
3059 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
3062 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3064 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
3068 case nir_var_shared
: {
3069 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3072 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
3074 for (unsigned chan
= 0; chan
< 8; chan
++) {
3075 if (!(writemask
& (1 << chan
)))
3077 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
3078 LLVMValueRef derived_ptr
;
3081 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
3083 value
= llvm_extract_elem(ctx
, src
, chan
);
3084 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
3085 LLVMBuildStore(ctx
->builder
,
3086 to_integer(ctx
, value
), derived_ptr
);
3095 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3098 case GLSL_SAMPLER_DIM_BUF
:
3100 case GLSL_SAMPLER_DIM_1D
:
3101 return array
? 2 : 1;
3102 case GLSL_SAMPLER_DIM_2D
:
3103 return array
? 3 : 2;
3104 case GLSL_SAMPLER_DIM_MS
:
3105 return array
? 4 : 3;
3106 case GLSL_SAMPLER_DIM_3D
:
3107 case GLSL_SAMPLER_DIM_CUBE
:
3109 case GLSL_SAMPLER_DIM_RECT
:
3110 case GLSL_SAMPLER_DIM_SUBPASS
:
3112 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3122 /* Adjust the sample index according to FMASK.
3124 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3125 * which is the identity mapping. Each nibble says which physical sample
3126 * should be fetched to get that sample.
3128 * For example, 0x11111100 means there are only 2 samples stored and
3129 * the second sample covers 3/4 of the pixel. When reading samples 0
3130 * and 1, return physical sample 0 (determined by the first two 0s
3131 * in FMASK), otherwise return physical sample 1.
3133 * The sample index should be adjusted as follows:
3134 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3136 static LLVMValueRef
adjust_sample_index_using_fmask(struct nir_to_llvm_context
*ctx
,
3137 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3138 LLVMValueRef coord_z
,
3139 LLVMValueRef sample_index
,
3140 LLVMValueRef fmask_desc_ptr
)
3142 LLVMValueRef fmask_load_address
[4];
3145 fmask_load_address
[0] = coord_x
;
3146 fmask_load_address
[1] = coord_y
;
3148 fmask_load_address
[2] = coord_z
;
3149 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3152 struct ac_image_args args
= {0};
3154 args
.opcode
= ac_image_load
;
3155 args
.da
= coord_z
? true : false;
3156 args
.resource
= fmask_desc_ptr
;
3158 args
.addr
= ac_build_gather_values(&ctx
->ac
, fmask_load_address
, coord_z
? 4 : 2);
3160 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3162 res
= to_integer(ctx
, res
);
3163 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3164 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3166 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3170 LLVMValueRef sample_index4
=
3171 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3172 LLVMValueRef shifted_fmask
=
3173 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3174 LLVMValueRef final_sample
=
3175 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3177 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3178 * resource descriptor is 0 (invalid),
3180 LLVMValueRef fmask_desc
=
3181 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3184 LLVMValueRef fmask_word1
=
3185 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3188 LLVMValueRef word1_is_nonzero
=
3189 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3190 fmask_word1
, ctx
->i32zero
, "");
3192 /* Replace the MSAA sample index. */
3194 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3195 final_sample
, sample_index
, "");
3196 return sample_index
;
3199 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
3200 nir_intrinsic_instr
*instr
)
3202 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3203 if(instr
->variables
[0]->deref
.child
)
3204 type
= instr
->variables
[0]->deref
.child
->type
;
3206 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3207 LLVMValueRef coords
[4];
3208 LLVMValueRef masks
[] = {
3209 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
3210 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
3213 LLVMValueRef sample_index
= llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
3216 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3217 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3218 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3219 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3220 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3222 count
= image_type_to_components_count(dim
,
3223 glsl_sampler_type_is_array(type
));
3226 LLVMValueRef fmask_load_address
[3];
3229 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3230 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[1], "");
3231 if (glsl_sampler_type_is_array(type
))
3232 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[2], "");
3234 fmask_load_address
[2] = NULL
;
3236 for (chan
= 0; chan
< 2; ++chan
)
3237 fmask_load_address
[chan
] = LLVMBuildAdd(ctx
->builder
, fmask_load_address
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3239 sample_index
= adjust_sample_index_using_fmask(ctx
,
3240 fmask_load_address
[0],
3241 fmask_load_address
[1],
3242 fmask_load_address
[2],
3244 get_sampler_desc(ctx
, instr
->variables
[0], DESC_FMASK
));
3247 if (instr
->src
[0].ssa
->num_components
)
3248 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3255 for (chan
= 0; chan
< count
; ++chan
) {
3256 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
3260 for (chan
= 0; chan
< count
; ++chan
)
3261 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3264 coords
[count
] = sample_index
;
3269 coords
[3] = LLVMGetUndef(ctx
->i32
);
3272 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3277 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
3278 nir_intrinsic_instr
*instr
)
3280 LLVMValueRef params
[7];
3282 char intrinsic_name
[64];
3283 const nir_variable
*var
= instr
->variables
[0]->var
;
3284 const struct glsl_type
*type
= var
->type
;
3285 if(instr
->variables
[0]->deref
.child
)
3286 type
= instr
->variables
[0]->deref
.child
->type
;
3288 type
= glsl_without_array(type
);
3289 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3290 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3291 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3292 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3293 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3294 params
[3] = ctx
->i1false
; /* glc */
3295 params
[4] = ctx
->i1false
; /* slc */
3296 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
3299 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
3300 res
= to_integer(ctx
, res
);
3302 bool is_da
= glsl_sampler_type_is_array(type
) ||
3303 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3304 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3305 LLVMValueRef glc
= ctx
->i1false
;
3306 LLVMValueRef slc
= ctx
->i1false
;
3308 params
[0] = get_image_coords(ctx
, instr
);
3309 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3310 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3311 if (HAVE_LLVM
<= 0x0309) {
3312 params
[3] = ctx
->i1false
; /* r128 */
3317 LLVMValueRef lwe
= ctx
->i1false
;
3324 ac_get_image_intr_name("llvm.amdgcn.image.load",
3325 ctx
->v4f32
, /* vdata */
3326 LLVMTypeOf(params
[0]), /* coords */
3327 LLVMTypeOf(params
[1]), /* rsrc */
3328 intrinsic_name
, sizeof(intrinsic_name
));
3330 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
3331 params
, 7, AC_FUNC_ATTR_READONLY
);
3333 return to_integer(ctx
, res
);
3336 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
3337 nir_intrinsic_instr
*instr
)
3339 LLVMValueRef params
[8];
3340 char intrinsic_name
[64];
3341 const nir_variable
*var
= instr
->variables
[0]->var
;
3342 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3344 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3345 ctx
->shader_info
->fs
.writes_memory
= true;
3347 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3348 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
3349 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3350 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3351 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3352 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3353 params
[4] = ctx
->i1false
; /* glc */
3354 params
[5] = ctx
->i1false
; /* slc */
3355 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
3358 bool is_da
= glsl_sampler_type_is_array(type
) ||
3359 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3360 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3361 LLVMValueRef glc
= ctx
->i1false
;
3362 LLVMValueRef slc
= ctx
->i1false
;
3364 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
3365 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3366 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3367 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3368 if (HAVE_LLVM
<= 0x0309) {
3369 params
[4] = ctx
->i1false
; /* r128 */
3374 LLVMValueRef lwe
= ctx
->i1false
;
3381 ac_get_image_intr_name("llvm.amdgcn.image.store",
3382 LLVMTypeOf(params
[0]), /* vdata */
3383 LLVMTypeOf(params
[1]), /* coords */
3384 LLVMTypeOf(params
[2]), /* rsrc */
3385 intrinsic_name
, sizeof(intrinsic_name
));
3387 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
3393 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
3394 nir_intrinsic_instr
*instr
)
3396 LLVMValueRef params
[6];
3397 int param_count
= 0;
3398 const nir_variable
*var
= instr
->variables
[0]->var
;
3400 const char *base_name
= "llvm.amdgcn.image.atomic";
3401 const char *atomic_name
;
3402 LLVMValueRef coords
;
3403 char intrinsic_name
[32], coords_type
[8];
3404 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3406 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3407 ctx
->shader_info
->fs
.writes_memory
= true;
3409 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3410 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3411 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3413 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3414 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3415 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3416 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3417 params
[param_count
++] = ctx
->i32zero
; /* voffset */
3418 params
[param_count
++] = ctx
->i1false
; /* glc */
3419 params
[param_count
++] = ctx
->i1false
; /* slc */
3421 bool da
= glsl_sampler_type_is_array(type
) ||
3422 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3424 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3425 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3426 params
[param_count
++] = ctx
->i1false
; /* r128 */
3427 params
[param_count
++] = da
? ctx
->i1true
: ctx
->i1false
; /* da */
3428 params
[param_count
++] = ctx
->i1false
; /* slc */
3431 switch (instr
->intrinsic
) {
3432 case nir_intrinsic_image_atomic_add
:
3433 atomic_name
= "add";
3435 case nir_intrinsic_image_atomic_min
:
3436 atomic_name
= "smin";
3438 case nir_intrinsic_image_atomic_max
:
3439 atomic_name
= "smax";
3441 case nir_intrinsic_image_atomic_and
:
3442 atomic_name
= "and";
3444 case nir_intrinsic_image_atomic_or
:
3447 case nir_intrinsic_image_atomic_xor
:
3448 atomic_name
= "xor";
3450 case nir_intrinsic_image_atomic_exchange
:
3451 atomic_name
= "swap";
3453 case nir_intrinsic_image_atomic_comp_swap
:
3454 atomic_name
= "cmpswap";
3459 build_int_type_name(LLVMTypeOf(coords
),
3460 coords_type
, sizeof(coords_type
));
3462 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3463 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
3464 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
3467 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
3468 nir_intrinsic_instr
*instr
)
3471 const nir_variable
*var
= instr
->variables
[0]->var
;
3472 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3473 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3474 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3475 if(instr
->variables
[0]->deref
.child
)
3476 type
= instr
->variables
[0]->deref
.child
->type
;
3478 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3479 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
3481 struct ac_image_args args
= { 0 };
3485 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3486 args
.opcode
= ac_image_get_resinfo
;
3487 args
.addr
= ctx
->i32zero
;
3489 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3491 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3492 glsl_sampler_type_is_array(type
)) {
3493 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3494 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3495 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
3496 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3497 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
3502 #define NOOP_WAITCNT 0xf7f
3503 #define LGKM_CNT 0x07f
3504 #define VM_CNT 0xf70
3506 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3509 LLVMValueRef args
[1] = {
3510 LLVMConstInt(ctx
->i32
, simm16
, false),
3512 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3513 ctx
->voidt
, args
, 1, 0);
3516 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3518 /* SI only (thanks to a hw bug workaround):
3519 * The real barrier instruction isn’t needed, because an entire patch
3520 * always fits into a single wave.
3522 if (ctx
->options
->chip_class
== SI
&&
3523 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3524 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3527 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3528 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3531 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
3532 nir_intrinsic_instr
*instr
)
3535 ctx
->shader_info
->fs
.can_discard
= true;
3537 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3538 get_src(ctx
, instr
->src
[0]),
3541 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
3542 LLVMConstReal(ctx
->f32
, -1.0f
),
3544 ac_build_kill(&ctx
->ac
, cond
);
3548 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3550 LLVMValueRef result
;
3551 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3552 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3553 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
3555 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3558 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3559 nir_intrinsic_instr
*instr
)
3561 LLVMValueRef ptr
, result
;
3562 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3563 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3564 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3566 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3567 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
3568 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3570 LLVMAtomicOrderingSequentiallyConsistent
,
3571 LLVMAtomicOrderingSequentiallyConsistent
,
3574 LLVMAtomicRMWBinOp op
;
3575 switch (instr
->intrinsic
) {
3576 case nir_intrinsic_var_atomic_add
:
3577 op
= LLVMAtomicRMWBinOpAdd
;
3579 case nir_intrinsic_var_atomic_umin
:
3580 op
= LLVMAtomicRMWBinOpUMin
;
3582 case nir_intrinsic_var_atomic_umax
:
3583 op
= LLVMAtomicRMWBinOpUMax
;
3585 case nir_intrinsic_var_atomic_imin
:
3586 op
= LLVMAtomicRMWBinOpMin
;
3588 case nir_intrinsic_var_atomic_imax
:
3589 op
= LLVMAtomicRMWBinOpMax
;
3591 case nir_intrinsic_var_atomic_and
:
3592 op
= LLVMAtomicRMWBinOpAnd
;
3594 case nir_intrinsic_var_atomic_or
:
3595 op
= LLVMAtomicRMWBinOpOr
;
3597 case nir_intrinsic_var_atomic_xor
:
3598 op
= LLVMAtomicRMWBinOpXor
;
3600 case nir_intrinsic_var_atomic_exchange
:
3601 op
= LLVMAtomicRMWBinOpXchg
;
3607 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
3608 LLVMAtomicOrderingSequentiallyConsistent
,
3614 #define INTERP_CENTER 0
3615 #define INTERP_CENTROID 1
3616 #define INTERP_SAMPLE 2
3618 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3619 enum glsl_interp_mode interp
, unsigned location
)
3622 case INTERP_MODE_FLAT
:
3625 case INTERP_MODE_SMOOTH
:
3626 case INTERP_MODE_NONE
:
3627 if (location
== INTERP_CENTER
)
3628 return ctx
->persp_center
;
3629 else if (location
== INTERP_CENTROID
)
3630 return ctx
->persp_centroid
;
3631 else if (location
== INTERP_SAMPLE
)
3632 return ctx
->persp_sample
;
3634 case INTERP_MODE_NOPERSPECTIVE
:
3635 if (location
== INTERP_CENTER
)
3636 return ctx
->linear_center
;
3637 else if (location
== INTERP_CENTROID
)
3638 return ctx
->linear_centroid
;
3639 else if (location
== INTERP_SAMPLE
)
3640 return ctx
->linear_sample
;
3646 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3647 LLVMValueRef sample_id
)
3649 LLVMValueRef result
;
3650 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_PS_SAMPLE_POSITIONS
, false));
3652 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3653 const_array(ctx
->v2f32
, 64), "");
3655 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3656 result
= ac_build_indexed_load(&ctx
->ac
, ptr
, sample_id
, false);
3661 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
3663 LLVMValueRef values
[2];
3665 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
3666 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
3667 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3670 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3671 nir_intrinsic_instr
*instr
)
3673 LLVMValueRef result
[2];
3674 LLVMValueRef interp_param
, attr_number
;
3677 LLVMValueRef src_c0
, src_c1
;
3679 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3680 switch (instr
->intrinsic
) {
3681 case nir_intrinsic_interp_var_at_centroid
:
3682 location
= INTERP_CENTROID
;
3684 case nir_intrinsic_interp_var_at_sample
:
3685 case nir_intrinsic_interp_var_at_offset
:
3686 location
= INTERP_CENTER
;
3687 src0
= get_src(ctx
, instr
->src
[0]);
3693 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3694 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3695 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3696 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3697 LLVMValueRef sample_position
;
3698 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3700 /* fetch sample ID */
3701 sample_position
= load_sample_position(ctx
, src0
);
3703 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3704 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3705 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3706 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3708 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3709 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3711 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3712 LLVMValueRef ij_out
[2];
3713 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3716 * take the I then J parameters, and the DDX/Y for it, and
3717 * calculate the IJ inputs for the interpolator.
3718 * temp1 = ddx * offset/sample.x + I;
3719 * interp_param.I = ddy * offset/sample.y + temp1;
3720 * temp1 = ddx * offset/sample.x + J;
3721 * interp_param.J = ddy * offset/sample.y + temp1;
3723 for (unsigned i
= 0; i
< 2; i
++) {
3724 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3725 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3726 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3727 ddxy_out
, ix_ll
, "");
3728 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3729 ddxy_out
, iy_ll
, "");
3730 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3731 interp_param
, ix_ll
, "");
3732 LLVMValueRef temp1
, temp2
;
3734 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3737 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3738 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3740 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3741 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3743 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3744 temp2
, ctx
->i32
, "");
3746 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3750 for (chan
= 0; chan
< 2; chan
++) {
3751 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3754 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3755 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3756 LLVMValueRef i
= LLVMBuildExtractElement(
3757 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3758 LLVMValueRef j
= LLVMBuildExtractElement(
3759 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3761 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3762 llvm_chan
, attr_number
,
3763 ctx
->prim_mask
, i
, j
);
3765 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3766 LLVMConstInt(ctx
->i32
, 2, false),
3767 llvm_chan
, attr_number
,
3771 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3775 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3776 nir_intrinsic_instr
*instr
)
3778 LLVMValueRef gs_next_vertex
;
3779 LLVMValueRef can_emit
, kill
;
3782 assert(instr
->const_index
[0] == 0);
3783 /* Write vertex attribute values to GSVS ring */
3784 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3785 ctx
->gs_next_vertex
,
3788 /* If this thread has already emitted the declared maximum number of
3789 * vertices, kill it: excessive vertex emissions are not supposed to
3790 * have any effect, and GS threads have no externally observable
3791 * effects other than emitting vertices.
3793 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3794 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3796 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3797 LLVMConstReal(ctx
->f32
, 1.0f
),
3798 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3799 ac_build_kill(&ctx
->ac
, kill
);
3801 /* loop num outputs */
3803 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3804 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3809 if (!(ctx
->output_mask
& (1ull << i
)))
3812 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3813 /* pack clip and cull into a single set of slots */
3814 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3818 for (unsigned j
= 0; j
< length
; j
++) {
3819 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3821 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3822 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3823 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3825 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3827 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3829 voffset
, ctx
->gs2vs_offset
, 0,
3835 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3837 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3839 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3843 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3844 nir_intrinsic_instr
*instr
)
3846 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3850 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
3851 nir_intrinsic_instr
*instr
)
3853 LLVMValueRef coord
[4] = {
3860 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
3861 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->f32one
,
3862 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
3864 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
3865 return LLVMBuildBitCast(ctx
->builder
, result
,
3866 get_def_type(ctx
, &instr
->dest
.ssa
), "");
3869 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3870 nir_intrinsic_instr
*instr
)
3872 LLVMValueRef result
= NULL
;
3874 switch (instr
->intrinsic
) {
3875 case nir_intrinsic_load_work_group_id
: {
3876 result
= ctx
->workgroup_ids
;
3879 case nir_intrinsic_load_base_vertex
: {
3880 result
= ctx
->base_vertex
;
3883 case nir_intrinsic_load_vertex_id_zero_base
: {
3884 result
= ctx
->vertex_id
;
3887 case nir_intrinsic_load_local_invocation_id
: {
3888 result
= ctx
->local_invocation_ids
;
3891 case nir_intrinsic_load_base_instance
:
3892 result
= ctx
->start_instance
;
3894 case nir_intrinsic_load_draw_id
:
3895 result
= ctx
->draw_index
;
3897 case nir_intrinsic_load_invocation_id
:
3898 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3899 result
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
3901 result
= ctx
->gs_invocation_id
;
3903 case nir_intrinsic_load_primitive_id
:
3904 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3905 result
= ctx
->gs_prim_id
;
3906 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3907 result
= ctx
->tcs_patch_id
;
3908 else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3909 result
= ctx
->tes_patch_id
;
3911 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3913 case nir_intrinsic_load_sample_id
:
3914 ctx
->shader_info
->fs
.force_persample
= true;
3915 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3917 case nir_intrinsic_load_sample_pos
:
3918 ctx
->shader_info
->fs
.force_persample
= true;
3919 result
= load_sample_pos(ctx
);
3921 case nir_intrinsic_load_sample_mask_in
:
3922 result
= ctx
->sample_coverage
;
3924 case nir_intrinsic_load_front_face
:
3925 result
= ctx
->front_face
;
3927 case nir_intrinsic_load_instance_id
:
3928 result
= ctx
->instance_id
;
3929 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3930 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3932 case nir_intrinsic_load_num_work_groups
:
3933 result
= ctx
->num_work_groups
;
3935 case nir_intrinsic_load_local_invocation_index
:
3936 result
= visit_load_local_invocation_index(ctx
);
3938 case nir_intrinsic_load_push_constant
:
3939 result
= visit_load_push_constant(ctx
, instr
);
3941 case nir_intrinsic_vulkan_resource_index
:
3942 result
= visit_vulkan_resource_index(ctx
, instr
);
3944 case nir_intrinsic_store_ssbo
:
3945 visit_store_ssbo(ctx
, instr
);
3947 case nir_intrinsic_load_ssbo
:
3948 result
= visit_load_buffer(ctx
, instr
);
3950 case nir_intrinsic_ssbo_atomic_add
:
3951 case nir_intrinsic_ssbo_atomic_imin
:
3952 case nir_intrinsic_ssbo_atomic_umin
:
3953 case nir_intrinsic_ssbo_atomic_imax
:
3954 case nir_intrinsic_ssbo_atomic_umax
:
3955 case nir_intrinsic_ssbo_atomic_and
:
3956 case nir_intrinsic_ssbo_atomic_or
:
3957 case nir_intrinsic_ssbo_atomic_xor
:
3958 case nir_intrinsic_ssbo_atomic_exchange
:
3959 case nir_intrinsic_ssbo_atomic_comp_swap
:
3960 result
= visit_atomic_ssbo(ctx
, instr
);
3962 case nir_intrinsic_load_ubo
:
3963 result
= visit_load_ubo_buffer(ctx
, instr
);
3965 case nir_intrinsic_get_buffer_size
:
3966 result
= visit_get_buffer_size(ctx
, instr
);
3968 case nir_intrinsic_load_var
:
3969 result
= visit_load_var(ctx
, instr
);
3971 case nir_intrinsic_store_var
:
3972 visit_store_var(ctx
, instr
);
3974 case nir_intrinsic_image_load
:
3975 result
= visit_image_load(ctx
, instr
);
3977 case nir_intrinsic_image_store
:
3978 visit_image_store(ctx
, instr
);
3980 case nir_intrinsic_image_atomic_add
:
3981 case nir_intrinsic_image_atomic_min
:
3982 case nir_intrinsic_image_atomic_max
:
3983 case nir_intrinsic_image_atomic_and
:
3984 case nir_intrinsic_image_atomic_or
:
3985 case nir_intrinsic_image_atomic_xor
:
3986 case nir_intrinsic_image_atomic_exchange
:
3987 case nir_intrinsic_image_atomic_comp_swap
:
3988 result
= visit_image_atomic(ctx
, instr
);
3990 case nir_intrinsic_image_size
:
3991 result
= visit_image_size(ctx
, instr
);
3993 case nir_intrinsic_discard
:
3994 ctx
->shader_info
->fs
.can_discard
= true;
3995 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3997 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
3999 case nir_intrinsic_discard_if
:
4000 emit_discard_if(ctx
, instr
);
4002 case nir_intrinsic_memory_barrier
:
4003 emit_waitcnt(ctx
, VM_CNT
);
4005 case nir_intrinsic_barrier
:
4008 case nir_intrinsic_var_atomic_add
:
4009 case nir_intrinsic_var_atomic_imin
:
4010 case nir_intrinsic_var_atomic_umin
:
4011 case nir_intrinsic_var_atomic_imax
:
4012 case nir_intrinsic_var_atomic_umax
:
4013 case nir_intrinsic_var_atomic_and
:
4014 case nir_intrinsic_var_atomic_or
:
4015 case nir_intrinsic_var_atomic_xor
:
4016 case nir_intrinsic_var_atomic_exchange
:
4017 case nir_intrinsic_var_atomic_comp_swap
:
4018 result
= visit_var_atomic(ctx
, instr
);
4020 case nir_intrinsic_interp_var_at_centroid
:
4021 case nir_intrinsic_interp_var_at_sample
:
4022 case nir_intrinsic_interp_var_at_offset
:
4023 result
= visit_interp(ctx
, instr
);
4025 case nir_intrinsic_emit_vertex
:
4026 visit_emit_vertex(ctx
, instr
);
4028 case nir_intrinsic_end_primitive
:
4029 visit_end_primitive(ctx
, instr
);
4031 case nir_intrinsic_load_tess_coord
:
4032 result
= visit_load_tess_coord(ctx
, instr
);
4034 case nir_intrinsic_load_patch_vertices_in
:
4035 result
= LLVMConstInt(ctx
->i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
4038 fprintf(stderr
, "Unknown intrinsic: ");
4039 nir_print_instr(&instr
->instr
, stderr
);
4040 fprintf(stderr
, "\n");
4044 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4048 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
4049 nir_deref_var
*deref
,
4050 enum desc_type desc_type
)
4052 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
4053 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
4054 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
4055 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
4056 unsigned offset
= binding
->offset
;
4057 unsigned stride
= binding
->size
;
4059 LLVMBuilderRef builder
= ctx
->builder
;
4061 LLVMValueRef index
= NULL
;
4062 unsigned constant_index
= 0;
4064 assert(deref
->var
->data
.binding
< layout
->binding_count
);
4066 switch (desc_type
) {
4078 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4088 unreachable("invalid desc_type\n");
4091 if (deref
->deref
.child
) {
4092 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
4094 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4095 offset
+= child
->base_offset
* stride
;
4096 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4097 index
= get_src(ctx
, child
->indirect
);
4100 constant_index
= child
->base_offset
;
4102 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4103 (!index
|| binding
->immutable_samplers_equal
)) {
4104 if (binding
->immutable_samplers_equal
)
4107 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4109 LLVMValueRef constants
[] = {
4110 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 0], 0),
4111 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 1], 0),
4112 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 2], 0),
4113 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 3], 0),
4115 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4118 assert(stride
% type_size
== 0);
4121 index
= ctx
->i32zero
;
4123 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
4125 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
4126 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4128 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4131 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
4132 struct ac_image_args
*args
,
4133 nir_tex_instr
*instr
,
4135 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4136 LLVMValueRef
*param
, unsigned count
,
4139 unsigned is_rect
= 0;
4140 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4142 if (op
== nir_texop_lod
)
4144 /* Pad to power of two vector */
4145 while (count
< util_next_power_of_two(count
))
4146 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4149 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
4151 args
->addr
= param
[0];
4153 args
->resource
= res_ptr
;
4154 args
->sampler
= samp_ptr
;
4156 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4157 args
->addr
= param
[0];
4161 args
->dmask
= dmask
;
4162 args
->unorm
= is_rect
;
4166 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4169 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4170 * filtering manually. The driver sets img7 to a mask clearing
4171 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4172 * s_and_b32 samp0, samp0, img7
4175 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4177 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
4178 LLVMValueRef res
, LLVMValueRef samp
)
4180 LLVMBuilderRef builder
= ctx
->builder
;
4181 LLVMValueRef img7
, samp0
;
4183 if (ctx
->options
->chip_class
>= VI
)
4186 img7
= LLVMBuildExtractElement(builder
, res
,
4187 LLVMConstInt(ctx
->i32
, 7, 0), "");
4188 samp0
= LLVMBuildExtractElement(builder
, samp
,
4189 LLVMConstInt(ctx
->i32
, 0, 0), "");
4190 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4191 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4192 LLVMConstInt(ctx
->i32
, 0, 0), "");
4195 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
4196 nir_tex_instr
*instr
,
4197 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4198 LLVMValueRef
*fmask_ptr
)
4200 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4201 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
4203 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
4206 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
4208 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
4209 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4210 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4212 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4213 instr
->op
== nir_texop_samples_identical
))
4214 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
4217 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
4220 coord
= to_float(ctx
, coord
);
4221 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4222 coord
= to_integer(ctx
, coord
);
4226 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
4228 LLVMValueRef result
= NULL
;
4229 struct ac_image_args args
= { 0 };
4230 unsigned dmask
= 0xf;
4231 LLVMValueRef address
[16];
4232 LLVMValueRef coords
[5];
4233 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4234 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4235 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4236 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4237 LLVMValueRef derivs
[6];
4238 unsigned chan
, count
= 0;
4239 unsigned const_src
= 0, num_deriv_comp
= 0;
4240 bool lod_is_zero
= false;
4241 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4243 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4244 switch (instr
->src
[i
].src_type
) {
4245 case nir_tex_src_coord
:
4246 coord
= get_src(ctx
, instr
->src
[i
].src
);
4248 case nir_tex_src_projector
:
4250 case nir_tex_src_comparator
:
4251 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4253 case nir_tex_src_offset
:
4254 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4257 case nir_tex_src_bias
:
4258 bias
= get_src(ctx
, instr
->src
[i
].src
);
4260 case nir_tex_src_lod
: {
4261 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4263 if (val
&& val
->i32
[0] == 0)
4265 lod
= get_src(ctx
, instr
->src
[i
].src
);
4268 case nir_tex_src_ms_index
:
4269 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4271 case nir_tex_src_ms_mcs
:
4273 case nir_tex_src_ddx
:
4274 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4275 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4277 case nir_tex_src_ddy
:
4278 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4280 case nir_tex_src_texture_offset
:
4281 case nir_tex_src_sampler_offset
:
4282 case nir_tex_src_plane
:
4288 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4289 result
= get_buffer_size(ctx
, res_ptr
, true);
4293 if (instr
->op
== nir_texop_texture_samples
) {
4294 LLVMValueRef res
, samples
, is_msaa
;
4295 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
4296 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
4297 LLVMConstInt(ctx
->i32
, 3, false), "");
4298 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
4299 LLVMConstInt(ctx
->i32
, 28, false), "");
4300 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
4301 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4302 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
4303 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4305 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
4306 LLVMConstInt(ctx
->i32
, 16, false), "");
4307 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
4308 LLVMConstInt(ctx
->i32
, 0xf, false), "");
4309 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
4311 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
4318 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4319 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
4321 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4322 LLVMValueRef offset
[3], pack
;
4323 for (chan
= 0; chan
< 3; ++chan
)
4324 offset
[chan
] = ctx
->i32zero
;
4327 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4328 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
4329 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
4330 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
4332 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
4333 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
4335 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
4336 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
4337 address
[count
++] = pack
;
4340 /* pack LOD bias value */
4341 if (instr
->op
== nir_texop_txb
&& bias
) {
4342 address
[count
++] = bias
;
4345 /* Pack depth comparison value */
4346 if (instr
->is_shadow
&& comparator
) {
4347 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
4350 /* pack derivatives */
4352 switch (instr
->sampler_dim
) {
4353 case GLSL_SAMPLER_DIM_3D
:
4354 case GLSL_SAMPLER_DIM_CUBE
:
4357 case GLSL_SAMPLER_DIM_2D
:
4361 case GLSL_SAMPLER_DIM_1D
:
4366 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
4367 derivs
[i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
4368 derivs
[num_deriv_comp
+ i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
4372 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4373 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
)
4374 coords
[3] = apply_round_slice(ctx
, coords
[3]);
4375 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4376 coords
[chan
] = to_float(ctx
, coords
[chan
]);
4377 if (instr
->coord_components
== 3)
4378 coords
[3] = LLVMGetUndef(ctx
->f32
);
4379 ac_prepare_cube_coords(&ctx
->ac
,
4380 instr
->op
== nir_texop_txd
, instr
->is_array
,
4387 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4388 address
[count
++] = derivs
[i
];
4391 /* Pack texture coordinates */
4393 address
[count
++] = coords
[0];
4394 if (instr
->coord_components
> 1) {
4395 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4396 coords
[1] = apply_round_slice(ctx
, coords
[1]);
4398 address
[count
++] = coords
[1];
4400 if (instr
->coord_components
> 2) {
4401 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4402 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4403 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4404 instr
->op
!= nir_texop_txf
) {
4405 coords
[2] = apply_round_slice(ctx
, coords
[2]);
4407 address
[count
++] = coords
[2];
4412 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4413 instr
->op
== nir_texop_txf
)) {
4414 address
[count
++] = lod
;
4415 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4416 address
[count
++] = sample_index
;
4417 } else if(instr
->op
== nir_texop_txs
) {
4420 address
[count
++] = lod
;
4422 address
[count
++] = ctx
->i32zero
;
4425 for (chan
= 0; chan
< count
; chan
++) {
4426 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
4427 address
[chan
], ctx
->i32
, "");
4430 if (instr
->op
== nir_texop_samples_identical
) {
4431 LLVMValueRef txf_address
[4];
4432 struct ac_image_args txf_args
= { 0 };
4433 unsigned txf_count
= count
;
4434 memcpy(txf_address
, address
, sizeof(txf_address
));
4436 if (!instr
->is_array
)
4437 txf_address
[2] = ctx
->i32zero
;
4438 txf_address
[3] = ctx
->i32zero
;
4440 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
4442 txf_address
, txf_count
, 0xf);
4444 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4446 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4447 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
4451 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4452 instr
->op
!= nir_texop_txs
) {
4453 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4454 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
4457 instr
->is_array
? address
[2] : NULL
,
4458 address
[sample_chan
],
4462 if (offsets
&& instr
->op
== nir_texop_txf
) {
4463 nir_const_value
*const_offset
=
4464 nir_src_as_const_value(instr
->src
[const_src
].src
);
4465 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4466 assert(const_offset
);
4467 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4468 if (num_offsets
> 2)
4469 address
[2] = LLVMBuildAdd(ctx
->builder
,
4470 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
4471 if (num_offsets
> 1)
4472 address
[1] = LLVMBuildAdd(ctx
->builder
,
4473 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
4474 address
[0] = LLVMBuildAdd(ctx
->builder
,
4475 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
4479 /* TODO TG4 support */
4480 if (instr
->op
== nir_texop_tg4
) {
4481 if (instr
->is_shadow
)
4484 dmask
= 1 << instr
->component
;
4486 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
4487 res_ptr
, samp_ptr
, address
, count
, dmask
);
4489 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4491 if (instr
->op
== nir_texop_query_levels
)
4492 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
4493 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
4494 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4495 else if (instr
->op
== nir_texop_txs
&&
4496 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4498 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
4499 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
4500 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
4501 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
4502 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
4503 } else if (instr
->dest
.ssa
.num_components
!= 4)
4504 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
4508 assert(instr
->dest
.is_ssa
);
4509 result
= to_integer(ctx
, result
);
4510 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4515 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
4517 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4518 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
4520 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4521 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4524 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
4525 nir_phi_instr
*instr
,
4526 LLVMValueRef llvm_phi
)
4528 nir_foreach_phi_src(src
, instr
) {
4529 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4530 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4532 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4536 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
4538 struct hash_entry
*entry
;
4539 hash_table_foreach(ctx
->phis
, entry
) {
4540 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4541 (LLVMValueRef
)entry
->data
);
4546 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
4547 nir_ssa_undef_instr
*instr
)
4549 unsigned num_components
= instr
->def
.num_components
;
4552 if (num_components
== 1)
4553 undef
= LLVMGetUndef(ctx
->i32
);
4555 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
4557 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4560 static void visit_jump(struct nir_to_llvm_context
*ctx
,
4561 nir_jump_instr
*instr
)
4563 switch (instr
->type
) {
4564 case nir_jump_break
:
4565 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
4566 LLVMClearInsertionPosition(ctx
->builder
);
4568 case nir_jump_continue
:
4569 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4570 LLVMClearInsertionPosition(ctx
->builder
);
4573 fprintf(stderr
, "Unknown NIR jump instr: ");
4574 nir_print_instr(&instr
->instr
, stderr
);
4575 fprintf(stderr
, "\n");
4580 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4581 struct exec_list
*list
);
4583 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
4585 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
4586 nir_foreach_instr(instr
, block
)
4588 switch (instr
->type
) {
4589 case nir_instr_type_alu
:
4590 visit_alu(ctx
, nir_instr_as_alu(instr
));
4592 case nir_instr_type_load_const
:
4593 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4595 case nir_instr_type_intrinsic
:
4596 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4598 case nir_instr_type_tex
:
4599 visit_tex(ctx
, nir_instr_as_tex(instr
));
4601 case nir_instr_type_phi
:
4602 visit_phi(ctx
, nir_instr_as_phi(instr
));
4604 case nir_instr_type_ssa_undef
:
4605 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4607 case nir_instr_type_jump
:
4608 visit_jump(ctx
, nir_instr_as_jump(instr
));
4611 fprintf(stderr
, "Unknown NIR instr type: ");
4612 nir_print_instr(instr
, stderr
);
4613 fprintf(stderr
, "\n");
4618 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4621 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
4623 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4625 LLVMBasicBlockRef merge_block
=
4626 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4627 LLVMBasicBlockRef if_block
=
4628 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4629 LLVMBasicBlockRef else_block
= merge_block
;
4630 if (!exec_list_is_empty(&if_stmt
->else_list
))
4631 else_block
= LLVMAppendBasicBlockInContext(
4632 ctx
->context
, ctx
->main_function
, "");
4634 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
4635 LLVMConstInt(ctx
->i32
, 0, false), "");
4636 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
4638 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
4639 visit_cf_list(ctx
, &if_stmt
->then_list
);
4640 if (LLVMGetInsertBlock(ctx
->builder
))
4641 LLVMBuildBr(ctx
->builder
, merge_block
);
4643 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4644 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
4645 visit_cf_list(ctx
, &if_stmt
->else_list
);
4646 if (LLVMGetInsertBlock(ctx
->builder
))
4647 LLVMBuildBr(ctx
->builder
, merge_block
);
4650 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
4653 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
4655 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4656 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4658 ctx
->continue_block
=
4659 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4661 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4663 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4664 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
4665 visit_cf_list(ctx
, &loop
->body
);
4667 if (LLVMGetInsertBlock(ctx
->builder
))
4668 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4669 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
4671 ctx
->continue_block
= continue_parent
;
4672 ctx
->break_block
= break_parent
;
4675 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4676 struct exec_list
*list
)
4678 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4680 switch (node
->type
) {
4681 case nir_cf_node_block
:
4682 visit_block(ctx
, nir_cf_node_as_block(node
));
4685 case nir_cf_node_if
:
4686 visit_if(ctx
, nir_cf_node_as_if(node
));
4689 case nir_cf_node_loop
:
4690 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4700 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4701 struct nir_variable
*variable
)
4703 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4704 LLVMValueRef t_offset
;
4705 LLVMValueRef t_list
;
4707 LLVMValueRef buffer_index
;
4708 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4709 int idx
= variable
->data
.location
;
4710 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4712 variable
->data
.driver_location
= idx
* 4;
4714 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4715 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
4716 ctx
->start_instance
, "");
4717 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4718 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4720 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
4721 ctx
->base_vertex
, "");
4723 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4724 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4726 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4728 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
4730 LLVMConstInt(ctx
->i32
, 0, false),
4733 for (unsigned chan
= 0; chan
< 4; chan
++) {
4734 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4735 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4736 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
4737 input
, llvm_chan
, ""));
4742 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4744 LLVMValueRef interp_param
,
4745 LLVMValueRef prim_mask
,
4746 LLVMValueRef result
[4])
4748 LLVMValueRef attr_number
;
4751 bool interp
= interp_param
!= NULL
;
4753 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4755 /* fs.constant returns the param from the middle vertex, so it's not
4756 * really useful for flat shading. It's meant to be used for custom
4757 * interpolation (but the intrinsic can't fetch from the other two
4760 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4761 * to do the right thing. The only reason we use fs.constant is that
4762 * fs.interp cannot be used on integers, because they can be equal
4766 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4767 LLVMVectorType(ctx
->f32
, 2), "");
4769 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4771 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4775 for (chan
= 0; chan
< 4; chan
++) {
4776 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4779 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4784 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4785 LLVMConstInt(ctx
->i32
, 2, false),
4794 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4795 struct nir_variable
*variable
)
4797 int idx
= variable
->data
.location
;
4798 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4799 LLVMValueRef interp
;
4801 variable
->data
.driver_location
= idx
* 4;
4802 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4804 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4805 unsigned interp_type
;
4806 if (variable
->data
.sample
) {
4807 interp_type
= INTERP_SAMPLE
;
4808 ctx
->shader_info
->fs
.force_persample
= true;
4809 } else if (variable
->data
.centroid
)
4810 interp_type
= INTERP_CENTROID
;
4812 interp_type
= INTERP_CENTER
;
4814 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4818 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4819 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4824 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4825 struct nir_variable
*variable
)
4827 switch (ctx
->stage
) {
4828 case MESA_SHADER_VERTEX
:
4829 handle_vs_input_decl(ctx
, variable
);
4831 case MESA_SHADER_FRAGMENT
:
4832 handle_fs_input_decl(ctx
, variable
);
4841 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4842 struct nir_shader
*nir
)
4845 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4846 LLVMValueRef interp_param
;
4847 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4849 if (!(ctx
->input_mask
& (1ull << i
)))
4852 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4853 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4854 interp_param
= *inputs
;
4855 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4859 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4861 } else if (i
== VARYING_SLOT_POS
) {
4862 for(int i
= 0; i
< 3; ++i
)
4863 inputs
[i
] = ctx
->frag_pos
[i
];
4865 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4868 ctx
->shader_info
->fs
.num_interp
= index
;
4869 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4870 ctx
->shader_info
->fs
.has_pcoord
= true;
4871 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4872 ctx
->shader_info
->fs
.prim_id_input
= true;
4873 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4874 ctx
->shader_info
->fs
.layer_input
= true;
4875 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4879 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4883 LLVMBuilderRef builder
= ctx
->builder
;
4884 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4885 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4886 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4887 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4888 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4892 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4894 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4897 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4898 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4900 LLVMDisposeBuilder(first_builder
);
4905 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4909 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4910 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4915 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4916 struct nir_variable
*variable
)
4918 int idx
= variable
->data
.location
+ variable
->data
.index
;
4919 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4920 uint64_t mask_attribs
;
4921 variable
->data
.driver_location
= idx
* 4;
4923 /* tess ctrl has it's own load/store paths for outputs */
4924 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4927 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4928 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4929 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
4930 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4931 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4932 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4933 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4934 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4935 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4937 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4938 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4939 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4946 mask_attribs
= 1ull << idx
;
4950 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4951 for (unsigned chan
= 0; chan
< 4; chan
++) {
4952 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4953 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4956 ctx
->output_mask
|= mask_attribs
;
4960 setup_locals(struct nir_to_llvm_context
*ctx
,
4961 struct nir_function
*func
)
4964 ctx
->num_locals
= 0;
4965 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4966 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4967 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4968 ctx
->num_locals
+= attrib_count
;
4970 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4974 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4975 for (j
= 0; j
< 4; j
++) {
4976 ctx
->locals
[i
* 4 + j
] =
4977 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4983 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4985 v
= to_float(ctx
, v
);
4986 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4987 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4991 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4992 LLVMValueRef src0
, LLVMValueRef src1
)
4994 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4995 LLVMValueRef comp
[2];
4997 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4998 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4999 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5000 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5003 /* Initialize arguments for the shader export intrinsic */
5005 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5006 LLVMValueRef
*values
,
5008 struct ac_export_args
*args
)
5010 /* Default is 0xf. Adjusted below depending on the format. */
5011 args
->enabled_channels
= 0xf;
5013 /* Specify whether the EXEC mask represents the valid mask */
5014 args
->valid_mask
= 0;
5016 /* Specify whether this is the last export */
5019 /* Specify the target we are exporting */
5020 args
->target
= target
;
5022 args
->compr
= false;
5023 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
5024 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
5025 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
5026 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
5031 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5032 LLVMValueRef val
[4];
5033 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5034 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5035 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5037 switch(col_format
) {
5038 case V_028714_SPI_SHADER_ZERO
:
5039 args
->enabled_channels
= 0; /* writemask */
5040 args
->target
= V_008DFC_SQ_EXP_NULL
;
5043 case V_028714_SPI_SHADER_32_R
:
5044 args
->enabled_channels
= 1;
5045 args
->out
[0] = values
[0];
5048 case V_028714_SPI_SHADER_32_GR
:
5049 args
->enabled_channels
= 0x3;
5050 args
->out
[0] = values
[0];
5051 args
->out
[1] = values
[1];
5054 case V_028714_SPI_SHADER_32_AR
:
5055 args
->enabled_channels
= 0x9;
5056 args
->out
[0] = values
[0];
5057 args
->out
[3] = values
[3];
5060 case V_028714_SPI_SHADER_FP16_ABGR
:
5063 for (unsigned chan
= 0; chan
< 2; chan
++) {
5064 LLVMValueRef pack_args
[2] = {
5066 values
[2 * chan
+ 1]
5068 LLVMValueRef packed
;
5070 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5071 args
->out
[chan
] = packed
;
5075 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5076 for (unsigned chan
= 0; chan
< 4; chan
++) {
5077 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5078 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5079 LLVMConstReal(ctx
->f32
, 65535), "");
5080 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5081 LLVMConstReal(ctx
->f32
, 0.5), "");
5082 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5087 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5088 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5091 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5092 for (unsigned chan
= 0; chan
< 4; chan
++) {
5093 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
5094 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5095 LLVMConstReal(ctx
->f32
, 32767), "");
5097 /* If positive, add 0.5, else add -0.5. */
5098 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5099 LLVMBuildSelect(ctx
->builder
,
5100 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5101 val
[chan
], ctx
->f32zero
, ""),
5102 LLVMConstReal(ctx
->f32
, 0.5),
5103 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
5104 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
5108 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5109 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5112 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5113 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
5115 for (unsigned chan
= 0; chan
< 4; chan
++) {
5116 val
[chan
] = to_integer(ctx
, values
[chan
]);
5117 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
5121 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5122 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5126 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5127 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
5128 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
5131 for (unsigned chan
= 0; chan
< 4; chan
++) {
5132 val
[chan
] = to_integer(ctx
, values
[chan
]);
5133 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
5134 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
5138 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5139 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5144 case V_028714_SPI_SHADER_32_ABGR
:
5145 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5149 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5151 for (unsigned i
= 0; i
< 4; ++i
)
5152 args
->out
[i
] = to_float(ctx
, args
->out
[i
]);
5156 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5157 struct ac_vs_output_info
*outinfo
)
5159 uint32_t param_count
= 0;
5161 unsigned pos_idx
, num_pos_exports
= 0;
5162 struct ac_export_args args
, pos_args
[4] = {};
5163 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5166 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5167 sizeof(outinfo
->vs_output_param_offset
));
5169 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5170 LLVMValueRef slots
[8];
5173 if (outinfo
->cull_dist_mask
)
5174 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5176 i
= VARYING_SLOT_CLIP_DIST0
;
5177 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5178 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5179 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5181 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5182 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5184 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5185 target
= V_008DFC_SQ_EXP_POS
+ 3;
5186 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5187 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5188 &args
, sizeof(args
));
5191 target
= V_008DFC_SQ_EXP_POS
+ 2;
5192 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5193 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5194 &args
, sizeof(args
));
5198 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5199 LLVMValueRef values
[4];
5200 if (!(ctx
->output_mask
& (1ull << i
)))
5203 for (unsigned j
= 0; j
< 4; j
++)
5204 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5205 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5207 if (i
== VARYING_SLOT_POS
) {
5208 target
= V_008DFC_SQ_EXP_POS
;
5209 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
5211 } else if (i
== VARYING_SLOT_PSIZ
) {
5212 outinfo
->writes_pointsize
= true;
5213 psize_value
= values
[0];
5215 } else if (i
== VARYING_SLOT_LAYER
) {
5216 outinfo
->writes_layer
= true;
5217 layer_value
= values
[0];
5218 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5219 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5221 } else if (i
== VARYING_SLOT_VIEWPORT
) {
5222 outinfo
->writes_viewport_index
= true;
5223 viewport_index_value
= values
[0];
5225 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5226 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5227 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5229 } else if (i
>= VARYING_SLOT_VAR0
) {
5230 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5231 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5232 outinfo
->vs_output_param_offset
[i
] = param_count
;
5236 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5238 if (target
>= V_008DFC_SQ_EXP_POS
&&
5239 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5240 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5241 &args
, sizeof(args
));
5243 ac_build_export(&ctx
->ac
, &args
);
5247 /* We need to add the position output manually if it's missing. */
5248 if (!pos_args
[0].out
[0]) {
5249 pos_args
[0].enabled_channels
= 0xf;
5250 pos_args
[0].valid_mask
= 0;
5251 pos_args
[0].done
= 0;
5252 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
5253 pos_args
[0].compr
= 0;
5254 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
5255 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
5256 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
5257 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
5260 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5261 (outinfo
->writes_layer
== true ? 4 : 0) |
5262 (outinfo
->writes_viewport_index
== true ? 8 : 0));
5264 pos_args
[1].enabled_channels
= mask
;
5265 pos_args
[1].valid_mask
= 0;
5266 pos_args
[1].done
= 0;
5267 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5268 pos_args
[1].compr
= 0;
5269 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
5270 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
5271 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
5272 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
5274 if (outinfo
->writes_pointsize
== true)
5275 pos_args
[1].out
[0] = psize_value
;
5276 if (outinfo
->writes_layer
== true)
5277 pos_args
[1].out
[2] = layer_value
;
5278 if (outinfo
->writes_viewport_index
== true)
5279 pos_args
[1].out
[3] = viewport_index_value
;
5281 for (i
= 0; i
< 4; i
++) {
5282 if (pos_args
[i
].out
[0])
5287 for (i
= 0; i
< 4; i
++) {
5288 if (!pos_args
[i
].out
[0])
5291 /* Specify the target we are exporting */
5292 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5293 if (pos_idx
== num_pos_exports
)
5294 pos_args
[i
].done
= 1;
5295 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5298 outinfo
->pos_exports
= num_pos_exports
;
5299 outinfo
->param_exports
= param_count
;
5303 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5304 struct ac_es_output_info
*outinfo
)
5307 uint64_t max_output_written
= 0;
5308 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5309 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5313 if (!(ctx
->output_mask
& (1ull << i
)))
5316 if (i
== VARYING_SLOT_CLIP_DIST0
)
5317 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5319 param_index
= shader_io_get_unique_index(i
);
5321 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5323 for (j
= 0; j
< length
; j
++) {
5324 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5325 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
5327 ac_build_buffer_store_dword(&ctx
->ac
,
5330 NULL
, ctx
->es2gs_offset
,
5331 (4 * param_index
+ j
) * 4,
5335 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5339 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5341 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5342 LLVMValueRef vertex_dw_stride
= unpack_param(ctx
, ctx
->ls_out_layout
, 13, 8);
5343 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5344 vertex_dw_stride
, "");
5346 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5347 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5350 if (!(ctx
->output_mask
& (1ull << i
)))
5353 if (i
== VARYING_SLOT_CLIP_DIST0
)
5354 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5355 int param
= shader_io_get_unique_index(i
);
5356 mark_tess_output(ctx
, false, param
);
5358 mark_tess_output(ctx
, false, param
+ 1);
5359 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5360 LLVMConstInt(ctx
->i32
, param
* 4, false),
5362 for (unsigned j
= 0; j
< length
; j
++) {
5363 lds_store(ctx
, dw_addr
,
5364 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5365 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->i32one
, "");
5370 struct ac_build_if_state
5372 struct nir_to_llvm_context
*ctx
;
5373 LLVMValueRef condition
;
5374 LLVMBasicBlockRef entry_block
;
5375 LLVMBasicBlockRef true_block
;
5376 LLVMBasicBlockRef false_block
;
5377 LLVMBasicBlockRef merge_block
;
5380 static LLVMBasicBlockRef
5381 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5383 LLVMBasicBlockRef current_block
;
5384 LLVMBasicBlockRef next_block
;
5385 LLVMBasicBlockRef new_block
;
5387 /* get current basic block */
5388 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5390 /* chqeck if there's another block after this one */
5391 next_block
= LLVMGetNextBasicBlock(current_block
);
5393 /* insert the new block before the next block */
5394 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5397 /* append new block after current block */
5398 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5399 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5405 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5406 struct nir_to_llvm_context
*ctx
,
5407 LLVMValueRef condition
)
5409 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5411 memset(ifthen
, 0, sizeof *ifthen
);
5413 ifthen
->condition
= condition
;
5414 ifthen
->entry_block
= block
;
5416 /* create endif/merge basic block for the phi functions */
5417 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5419 /* create/insert true_block before merge_block */
5420 ifthen
->true_block
=
5421 LLVMInsertBasicBlockInContext(ctx
->context
,
5422 ifthen
->merge_block
,
5425 /* successive code goes into the true block */
5426 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5430 * End a conditional.
5433 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5435 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5437 /* Insert branch to the merge block from current block */
5438 LLVMBuildBr(builder
, ifthen
->merge_block
);
5441 * Now patch in the various branch instructions.
5444 /* Insert the conditional branch instruction at the end of entry_block */
5445 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5446 if (ifthen
->false_block
) {
5447 /* we have an else clause */
5448 LLVMBuildCondBr(builder
, ifthen
->condition
,
5449 ifthen
->true_block
, ifthen
->false_block
);
5452 /* no else clause */
5453 LLVMBuildCondBr(builder
, ifthen
->condition
,
5454 ifthen
->true_block
, ifthen
->merge_block
);
5457 /* Resume building code at end of the ifthen->merge_block */
5458 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5462 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5464 unsigned stride
, outer_comps
, inner_comps
;
5465 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5466 LLVMValueRef invocation_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
5467 LLVMValueRef rel_patch_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
5468 unsigned tess_inner_index
, tess_outer_index
;
5469 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
5470 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
5474 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
5494 ac_nir_build_if(&if_ctx
, ctx
,
5495 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5496 invocation_id
, ctx
->i32zero
, ""));
5498 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5499 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5501 mark_tess_output(ctx
, true, tess_inner_index
);
5502 mark_tess_output(ctx
, true, tess_outer_index
);
5503 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
5504 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5505 LLVMConstInt(ctx
->i32
, tess_inner_index
* 4, false), "");
5506 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5507 LLVMConstInt(ctx
->i32
, tess_outer_index
* 4, false), "");
5509 for (i
= 0; i
< 4; i
++) {
5510 inner
[i
] = LLVMGetUndef(ctx
->i32
);
5511 outer
[i
] = LLVMGetUndef(ctx
->i32
);
5515 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
5516 outer
[0] = out
[1] = lds_load(ctx
, lds_outer
);
5517 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5518 LLVMConstInt(ctx
->i32
, 1, false), "");
5519 outer
[1] = out
[0] = lds_load(ctx
, lds_outer
);
5521 for (i
= 0; i
< outer_comps
; i
++) {
5523 lds_load(ctx
, lds_outer
);
5524 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5525 LLVMConstInt(ctx
->i32
, 1, false), "");
5527 for (i
= 0; i
< inner_comps
; i
++) {
5528 inner
[i
] = out
[outer_comps
+i
] =
5529 lds_load(ctx
, lds_inner
);
5530 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
5531 LLVMConstInt(ctx
->i32
, 1, false), "");
5535 /* Convert the outputs to vectors for stores. */
5536 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
5540 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
5543 buffer
= ctx
->hs_ring_tess_factor
;
5544 tf_base
= ctx
->tess_factor_offset
;
5545 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
5546 LLVMConstInt(ctx
->i32
, 4 * stride
, false), "");
5548 ac_nir_build_if(&inner_if_ctx
, ctx
,
5549 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5550 rel_patch_id
, ctx
->i32zero
, ""));
5552 /* Store the dynamic HS control word. */
5553 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
5554 LLVMConstInt(ctx
->i32
, 0x80000000, false),
5555 1, ctx
->i32zero
, tf_base
,
5556 0, 1, 0, true, false);
5557 ac_nir_build_endif(&inner_if_ctx
);
5559 /* Store the tessellation factors. */
5560 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
5561 MIN2(stride
, 4), byteoffset
, tf_base
,
5562 4, 1, 0, true, false);
5564 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
5565 stride
- 4, byteoffset
, tf_base
,
5566 20, 1, 0, true, false);
5568 //TODO store to offchip for TES to read - only if TES reads them
5570 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
5571 LLVMValueRef tf_inner_offset
;
5572 unsigned param_outer
, param_inner
;
5574 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5575 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5576 LLVMConstInt(ctx
->i32
, param_outer
, 0));
5578 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
5579 util_next_power_of_two(outer_comps
));
5581 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
5582 outer_comps
, tf_outer_offset
,
5583 ctx
->oc_lds
, 0, 1, 0, true, false);
5585 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5586 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5587 LLVMConstInt(ctx
->i32
, param_inner
, 0));
5589 inner_vec
= inner_comps
== 1 ? inner
[0] :
5590 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
5591 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
5592 inner_comps
, tf_inner_offset
,
5593 ctx
->oc_lds
, 0, 1, 0, true, false);
5596 ac_nir_build_endif(&if_ctx
);
5600 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
5602 write_tess_factors(ctx
);
5606 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
5607 LLVMValueRef
*color
, unsigned param
, bool is_last
,
5608 struct ac_export_args
*args
)
5611 si_llvm_init_export_args(ctx
, color
, param
,
5615 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
5616 args
->done
= 1; /* DONE bit */
5617 } else if (!args
->enabled_channels
)
5618 return false; /* unnecessary NULL export */
5624 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
5625 LLVMValueRef depth
, LLVMValueRef stencil
,
5626 LLVMValueRef samplemask
)
5628 struct ac_export_args args
;
5630 args
.enabled_channels
= 0;
5631 args
.valid_mask
= 1;
5633 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
5636 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
5637 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
5638 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
5639 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
5642 args
.out
[0] = depth
;
5643 args
.enabled_channels
|= 0x1;
5647 args
.out
[1] = stencil
;
5648 args
.enabled_channels
|= 0x2;
5652 args
.out
[2] = samplemask
;
5653 args
.enabled_channels
|= 0x4;
5656 /* SI (except OLAND) has a bug that it only looks
5657 * at the X writemask component. */
5658 if (ctx
->options
->chip_class
== SI
&&
5659 ctx
->options
->family
!= CHIP_OLAND
)
5660 args
.enabled_channels
|= 0x1;
5662 ac_build_export(&ctx
->ac
, &args
);
5666 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
5669 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
5670 struct ac_export_args color_args
[8];
5672 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5673 LLVMValueRef values
[4];
5675 if (!(ctx
->output_mask
& (1ull << i
)))
5678 if (i
== FRAG_RESULT_DEPTH
) {
5679 ctx
->shader_info
->fs
.writes_z
= true;
5680 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5681 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5682 } else if (i
== FRAG_RESULT_STENCIL
) {
5683 ctx
->shader_info
->fs
.writes_stencil
= true;
5684 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5685 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5686 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
5687 ctx
->shader_info
->fs
.writes_sample_mask
= true;
5688 samplemask
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5689 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5692 for (unsigned j
= 0; j
< 4; j
++)
5693 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5694 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5696 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
5697 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
5699 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
5705 for (unsigned i
= 0; i
< index
; i
++)
5706 ac_build_export(&ctx
->ac
, &color_args
[i
]);
5707 if (depth
|| stencil
|| samplemask
)
5708 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
5710 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
5711 ac_build_export(&ctx
->ac
, &color_args
[0]);
5714 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
5718 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
5720 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
5724 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
5726 switch (ctx
->stage
) {
5727 case MESA_SHADER_VERTEX
:
5728 if (ctx
->options
->key
.vs
.as_ls
)
5729 handle_ls_outputs_post(ctx
);
5730 else if (ctx
->options
->key
.vs
.as_es
)
5731 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
5733 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
5735 case MESA_SHADER_FRAGMENT
:
5736 handle_fs_outputs_post(ctx
);
5738 case MESA_SHADER_GEOMETRY
:
5739 emit_gs_epilogue(ctx
);
5741 case MESA_SHADER_TESS_CTRL
:
5742 handle_tcs_outputs_post(ctx
);
5744 case MESA_SHADER_TESS_EVAL
:
5745 if (ctx
->options
->key
.tes
.as_es
)
5746 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
5748 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->tes
.outinfo
);
5756 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
5757 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
5759 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
5760 variable
->data
.driver_location
= *offset
;
5764 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
5766 LLVMPassManagerRef passmgr
;
5767 /* Create the pass manager */
5768 passmgr
= LLVMCreateFunctionPassManagerForModule(
5771 /* This pass should eliminate all the load and store instructions */
5772 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
5774 /* Add some optimization passes */
5775 LLVMAddScalarReplAggregatesPass(passmgr
);
5776 LLVMAddLICMPass(passmgr
);
5777 LLVMAddAggressiveDCEPass(passmgr
);
5778 LLVMAddCFGSimplificationPass(passmgr
);
5779 LLVMAddInstructionCombiningPass(passmgr
);
5782 LLVMInitializeFunctionPassManager(passmgr
);
5783 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
5784 LLVMFinalizeFunctionPassManager(passmgr
);
5786 LLVMDisposeBuilder(ctx
->builder
);
5787 LLVMDisposePassManager(passmgr
);
5791 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
5793 struct ac_vs_output_info
*outinfo
;
5795 if (ctx
->stage
== MESA_SHADER_FRAGMENT
||
5796 ctx
->stage
== MESA_SHADER_COMPUTE
||
5797 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5798 ctx
->stage
== MESA_SHADER_GEOMETRY
)
5801 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
5802 if (ctx
->options
->key
.vs
.as_ls
||
5803 ctx
->options
->key
.vs
.as_es
)
5805 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
5808 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5809 if (ctx
->options
->key
.vs
.as_es
)
5811 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
5814 ac_optimize_vs_outputs(&ctx
->ac
,
5816 outinfo
->vs_output_param_offset
,
5818 &outinfo
->param_exports
);
5822 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
5824 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
5825 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
5826 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
5829 if (ctx
->is_gs_copy_shader
) {
5830 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
5832 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5834 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
5835 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
5837 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
5839 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
5840 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
5841 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
5842 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
5844 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
5847 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5848 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5849 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
5850 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
5855 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
5856 struct nir_shader
*nir
)
5858 switch (nir
->stage
) {
5859 case MESA_SHADER_TESS_CTRL
:
5860 return chip_class
>= CIK
? 128 : 64;
5861 case MESA_SHADER_GEOMETRY
:
5863 case MESA_SHADER_COMPUTE
:
5869 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
5870 nir
->info
.cs
.local_size
[1] *
5871 nir
->info
.cs
.local_size
[2];
5872 return max_workgroup_size
;
5876 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
5877 struct nir_shader
*nir
,
5878 struct ac_shader_variant_info
*shader_info
,
5879 const struct ac_nir_compiler_options
*options
)
5881 struct nir_to_llvm_context ctx
= {0};
5882 struct nir_function
*func
;
5884 ctx
.options
= options
;
5885 ctx
.shader_info
= shader_info
;
5886 ctx
.context
= LLVMContextCreate();
5887 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5889 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5890 ctx
.ac
.module
= ctx
.module
;
5892 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
5894 memset(shader_info
, 0, sizeof(*shader_info
));
5896 ac_nir_shader_info_pass(nir
, options
, &shader_info
->info
);
5898 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
5900 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
5901 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
5902 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
5903 LLVMDisposeTargetData(data_layout
);
5904 LLVMDisposeMessage(data_layout_str
);
5908 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5909 ctx
.ac
.builder
= ctx
.builder
;
5910 ctx
.stage
= nir
->stage
;
5911 ctx
.max_workgroup_size
= ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
, nir
);
5913 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
5914 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
5915 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
5916 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
5918 create_function(&ctx
);
5920 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
5922 nir_foreach_variable(variable
, &nir
->shared
)
5926 uint32_t shared_size
= 0;
5928 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
5929 nir_foreach_variable(variable
, &nir
->shared
) {
5930 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
5935 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
5936 LLVMArrayType(ctx
.i8
, shared_size
),
5939 LLVMSetAlignment(var
, 4);
5940 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
5942 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5943 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
5945 ctx
.gs_max_out_vertices
= nir
->info
.gs
.vertices_out
;
5946 } else if (nir
->stage
== MESA_SHADER_TESS_EVAL
) {
5947 ctx
.tes_primitive_mode
= nir
->info
.tess
.primitive_mode
;
5950 ac_setup_rings(&ctx
);
5952 nir_foreach_variable(variable
, &nir
->inputs
)
5953 handle_shader_input_decl(&ctx
, variable
);
5955 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
5956 handle_fs_inputs_pre(&ctx
, nir
);
5958 ctx
.num_output_clips
= nir
->info
.clip_distance_array_size
;
5959 ctx
.num_output_culls
= nir
->info
.cull_distance_array_size
;
5961 nir_foreach_variable(variable
, &nir
->outputs
)
5962 handle_shader_output_decl(&ctx
, variable
);
5964 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5965 _mesa_key_pointer_equal
);
5966 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5967 _mesa_key_pointer_equal
);
5969 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5971 setup_locals(&ctx
, func
);
5973 visit_cf_list(&ctx
, &func
->impl
->body
);
5974 phi_post_pass(&ctx
);
5976 handle_shader_outputs_post(&ctx
);
5977 LLVMBuildRetVoid(ctx
.builder
);
5979 ac_llvm_finalize_module(&ctx
);
5981 ac_nir_eliminate_const_vs_outputs(&ctx
);
5983 ralloc_free(ctx
.defs
);
5984 ralloc_free(ctx
.phis
);
5986 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5987 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
5988 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
5989 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
5990 nir
->info
.gs
.vertices_out
;
5991 } else if (nir
->stage
== MESA_SHADER_TESS_CTRL
) {
5992 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
5993 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
5994 } else if (nir
->stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
5995 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6001 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6003 unsigned *retval
= (unsigned *)context
;
6004 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6005 char *description
= LLVMGetDiagInfoDescription(di
);
6007 if (severity
== LLVMDSError
) {
6009 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6013 LLVMDisposeMessage(description
);
6016 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6017 struct ac_shader_binary
*binary
,
6018 LLVMTargetMachineRef tm
)
6020 unsigned retval
= 0;
6022 LLVMContextRef llvm_ctx
;
6023 LLVMMemoryBufferRef out_buffer
;
6024 unsigned buffer_size
;
6025 const char *buffer_data
;
6028 /* Setup Diagnostic Handler*/
6029 llvm_ctx
= LLVMGetModuleContext(M
);
6031 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6035 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6038 /* Process Errors/Warnings */
6040 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6046 /* Extract Shader Code*/
6047 buffer_size
= LLVMGetBufferSize(out_buffer
);
6048 buffer_data
= LLVMGetBufferStart(out_buffer
);
6050 ac_elf_read(buffer_data
, buffer_size
, binary
);
6053 LLVMDisposeMemoryBuffer(out_buffer
);
6059 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6060 LLVMModuleRef llvm_module
,
6061 struct ac_shader_binary
*binary
,
6062 struct ac_shader_config
*config
,
6063 struct ac_shader_variant_info
*shader_info
,
6064 gl_shader_stage stage
,
6065 bool dump_shader
, bool supports_spill
)
6068 ac_dump_module(llvm_module
);
6070 memset(binary
, 0, sizeof(*binary
));
6071 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6073 fprintf(stderr
, "compile failed\n");
6077 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6079 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6081 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6082 LLVMDisposeModule(llvm_module
);
6083 LLVMContextDispose(ctx
);
6085 if (stage
== MESA_SHADER_FRAGMENT
) {
6086 shader_info
->num_input_vgprs
= 0;
6087 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6088 shader_info
->num_input_vgprs
+= 2;
6089 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6090 shader_info
->num_input_vgprs
+= 2;
6091 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6092 shader_info
->num_input_vgprs
+= 2;
6093 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6094 shader_info
->num_input_vgprs
+= 3;
6095 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6096 shader_info
->num_input_vgprs
+= 2;
6097 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6098 shader_info
->num_input_vgprs
+= 2;
6099 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6100 shader_info
->num_input_vgprs
+= 2;
6101 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6102 shader_info
->num_input_vgprs
+= 1;
6103 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6104 shader_info
->num_input_vgprs
+= 1;
6105 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6106 shader_info
->num_input_vgprs
+= 1;
6107 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6108 shader_info
->num_input_vgprs
+= 1;
6109 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6110 shader_info
->num_input_vgprs
+= 1;
6111 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6112 shader_info
->num_input_vgprs
+= 1;
6113 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6114 shader_info
->num_input_vgprs
+= 1;
6115 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6116 shader_info
->num_input_vgprs
+= 1;
6117 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6118 shader_info
->num_input_vgprs
+= 1;
6120 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6122 /* +3 for scratch wave offset and VCC */
6123 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6124 shader_info
->num_input_sgprs
+ 3);
6127 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6128 struct ac_shader_binary
*binary
,
6129 struct ac_shader_config
*config
,
6130 struct ac_shader_variant_info
*shader_info
,
6131 struct nir_shader
*nir
,
6132 const struct ac_nir_compiler_options
*options
,
6136 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
6139 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
6140 switch (nir
->stage
) {
6141 case MESA_SHADER_COMPUTE
:
6142 for (int i
= 0; i
< 3; ++i
)
6143 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6145 case MESA_SHADER_FRAGMENT
:
6146 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6148 case MESA_SHADER_GEOMETRY
:
6149 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6150 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6151 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6152 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6154 case MESA_SHADER_TESS_EVAL
:
6155 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6156 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6157 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6158 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6159 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6161 case MESA_SHADER_TESS_CTRL
:
6162 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6164 case MESA_SHADER_VERTEX
:
6165 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6166 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6167 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6168 if (options
->key
.vs
.as_ls
)
6169 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6177 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6179 LLVMValueRef args
[9];
6180 args
[0] = ctx
->gsvs_ring
;
6181 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
6182 args
[3] = ctx
->i32zero
;
6183 args
[4] = ctx
->i32one
; /* OFFEN */
6184 args
[5] = ctx
->i32zero
; /* IDXEN */
6185 args
[6] = ctx
->i32one
; /* GLC */
6186 args
[7] = ctx
->i32one
; /* SLC */
6187 args
[8] = ctx
->i32zero
; /* TFE */
6191 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6195 if (!(ctx
->output_mask
& (1ull << i
)))
6198 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6199 /* unpack clip and cull from a single set of slots */
6200 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6205 for (unsigned j
= 0; j
< length
; j
++) {
6207 args
[2] = LLVMConstInt(ctx
->i32
,
6209 ctx
->gs_max_out_vertices
* 16 * 4, false);
6211 value
= ac_build_intrinsic(&ctx
->ac
,
6212 "llvm.SI.buffer.load.dword.i32.i32",
6214 AC_FUNC_ATTR_READONLY
|
6215 AC_FUNC_ATTR_LEGACY
);
6217 LLVMBuildStore(ctx
->builder
,
6218 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6222 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
6225 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6226 struct nir_shader
*geom_shader
,
6227 struct ac_shader_binary
*binary
,
6228 struct ac_shader_config
*config
,
6229 struct ac_shader_variant_info
*shader_info
,
6230 const struct ac_nir_compiler_options
*options
,
6233 struct nir_to_llvm_context ctx
= {0};
6234 ctx
.context
= LLVMContextCreate();
6235 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6236 ctx
.options
= options
;
6237 ctx
.shader_info
= shader_info
;
6239 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
6240 ctx
.ac
.module
= ctx
.module
;
6242 ctx
.is_gs_copy_shader
= true;
6243 LLVMSetTarget(ctx
.module
, "amdgcn--");
6246 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6247 ctx
.ac
.builder
= ctx
.builder
;
6248 ctx
.stage
= MESA_SHADER_VERTEX
;
6250 create_function(&ctx
);
6252 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6253 ac_setup_rings(&ctx
);
6255 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6256 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6258 nir_foreach_variable(variable
, &geom_shader
->outputs
)
6259 handle_shader_output_decl(&ctx
, variable
);
6261 ac_gs_copy_shader_emit(&ctx
);
6263 LLVMBuildRetVoid(ctx
.builder
);
6265 ac_llvm_finalize_module(&ctx
);
6267 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6269 dump_shader
, options
->supports_spill
);