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;
542 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
543 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
545 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
549 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
550 uint32_t indirect_offset
)
552 ud_info
->sgpr_idx
= sgpr_idx
;
553 ud_info
->num_sgprs
= num_sgprs
;
554 ud_info
->indirect
= true;
555 ud_info
->indirect_offset
= indirect_offset
;
558 static void declare_tess_lds(struct nir_to_llvm_context
*ctx
)
560 unsigned lds_size
= ctx
->options
->chip_class
>= CIK
? 65536 : 32768;
561 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32zero
,
562 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
566 struct user_sgpr_info
{
567 bool need_ring_offsets
;
569 bool indirect_all_descriptor_sets
;
572 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
573 struct user_sgpr_info
*user_sgpr_info
)
575 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
577 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
578 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
579 ctx
->stage
== MESA_SHADER_VERTEX
||
580 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
581 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
582 ctx
->is_gs_copy_shader
)
583 user_sgpr_info
->need_ring_offsets
= true;
585 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
586 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
587 user_sgpr_info
->need_ring_offsets
= true;
589 /* 2 user sgprs will nearly always be allocated for scratch/rings */
590 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
591 user_sgpr_info
->sgpr_count
+= 2;
594 switch (ctx
->stage
) {
595 case MESA_SHADER_COMPUTE
:
596 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
598 case MESA_SHADER_FRAGMENT
:
599 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
601 case MESA_SHADER_VERTEX
:
602 if (!ctx
->is_gs_copy_shader
) {
603 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
604 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
605 user_sgpr_info
->sgpr_count
+= 3;
607 user_sgpr_info
->sgpr_count
+= 2;
610 if (ctx
->options
->key
.vs
.as_ls
)
611 user_sgpr_info
->sgpr_count
++;
613 case MESA_SHADER_TESS_CTRL
:
614 user_sgpr_info
->sgpr_count
+= 4;
616 case MESA_SHADER_TESS_EVAL
:
617 user_sgpr_info
->sgpr_count
+= 1;
619 case MESA_SHADER_GEOMETRY
:
620 user_sgpr_info
->sgpr_count
+= 2;
626 if (ctx
->shader_info
->info
.needs_push_constants
)
627 user_sgpr_info
->sgpr_count
+= 2;
629 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
630 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
631 user_sgpr_info
->sgpr_count
+= 2;
632 user_sgpr_info
->indirect_all_descriptor_sets
= true;
634 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
638 static void create_function(struct nir_to_llvm_context
*ctx
)
640 LLVMTypeRef arg_types
[23];
641 unsigned arg_idx
= 0;
642 unsigned array_params_mask
= 0;
643 unsigned sgpr_count
= 0, user_sgpr_count
;
645 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
646 unsigned user_sgpr_idx
;
647 struct user_sgpr_info user_sgpr_info
;
649 allocate_user_sgprs(ctx
, &user_sgpr_info
);
650 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
651 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* address of rings */
654 /* 1 for each descriptor set */
655 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
656 for (unsigned i
= 0; i
< num_sets
; ++i
) {
657 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
658 array_params_mask
|= (1 << arg_idx
);
659 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
663 array_params_mask
|= (1 << arg_idx
);
664 arg_types
[arg_idx
++] = const_array(const_array(ctx
->i8
, 1024 * 1024), 32);
667 if (ctx
->shader_info
->info
.needs_push_constants
) {
668 /* 1 for push constants and dynamic descriptors */
669 array_params_mask
|= (1 << arg_idx
);
670 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
673 switch (ctx
->stage
) {
674 case MESA_SHADER_COMPUTE
:
675 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
676 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, ctx
->shader_info
->info
.cs
.grid_components_used
); /* grid size */
677 user_sgpr_count
= arg_idx
;
678 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
679 arg_types
[arg_idx
++] = ctx
->i32
;
680 sgpr_count
= arg_idx
;
682 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
684 case MESA_SHADER_VERTEX
:
685 if (!ctx
->is_gs_copy_shader
) {
686 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
687 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
688 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
689 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
690 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
691 arg_types
[arg_idx
++] = ctx
->i32
; // draw index
693 user_sgpr_count
= arg_idx
;
694 if (ctx
->options
->key
.vs
.as_es
)
695 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
696 else if (ctx
->options
->key
.vs
.as_ls
) {
697 arg_types
[arg_idx
++] = ctx
->i32
; //ls out layout
700 sgpr_count
= arg_idx
;
701 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
702 if (!ctx
->is_gs_copy_shader
) {
703 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
704 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
705 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
708 case MESA_SHADER_TESS_CTRL
:
709 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
710 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out offsets
711 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out layout
712 arg_types
[arg_idx
++] = ctx
->i32
; // tcs in layout
713 user_sgpr_count
= arg_idx
;
714 arg_types
[arg_idx
++] = ctx
->i32
; // param oc lds
715 arg_types
[arg_idx
++] = ctx
->i32
; // tess factor offset
716 sgpr_count
= arg_idx
;
717 arg_types
[arg_idx
++] = ctx
->i32
; // patch id
718 arg_types
[arg_idx
++] = ctx
->i32
; // rel ids;
720 case MESA_SHADER_TESS_EVAL
:
721 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
722 user_sgpr_count
= arg_idx
;
723 if (ctx
->options
->key
.tes
.as_es
) {
724 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
725 arg_types
[arg_idx
++] = ctx
->i32
; //
726 arg_types
[arg_idx
++] = ctx
->i32
; // es2gs offset
728 arg_types
[arg_idx
++] = ctx
->i32
; //
729 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
731 sgpr_count
= arg_idx
;
732 arg_types
[arg_idx
++] = ctx
->f32
; // tes_u
733 arg_types
[arg_idx
++] = ctx
->f32
; // tes_v
734 arg_types
[arg_idx
++] = ctx
->i32
; // tes rel patch id
735 arg_types
[arg_idx
++] = ctx
->i32
; // tes patch id
737 case MESA_SHADER_GEOMETRY
:
738 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
739 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
740 user_sgpr_count
= arg_idx
;
741 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
742 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
743 sgpr_count
= arg_idx
;
744 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
745 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
746 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
747 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
748 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
749 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
750 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
751 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
753 case MESA_SHADER_FRAGMENT
:
754 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
755 arg_types
[arg_idx
++] = ctx
->i32
; /* sample position offset */
756 user_sgpr_count
= arg_idx
;
757 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
758 sgpr_count
= arg_idx
;
759 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
760 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
761 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
762 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
763 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
764 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
765 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
766 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
767 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
768 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
769 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
770 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
771 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
772 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
773 arg_types
[arg_idx
++] = ctx
->i32
; /* sample coverage */
774 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
777 unreachable("Shader stage not implemented");
780 ctx
->main_function
= create_llvm_function(
781 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
782 arg_idx
, array_params_mask
, sgpr_count
, ctx
->max_workgroup_size
,
783 ctx
->options
->unsafe_math
);
784 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
786 ctx
->shader_info
->num_input_sgprs
= 0;
787 ctx
->shader_info
->num_input_vgprs
= 0;
789 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
790 for (i
= 0; i
< user_sgpr_count
; i
++)
791 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
793 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
794 for (; i
< sgpr_count
; i
++)
795 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
797 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
798 for (; i
< arg_idx
; ++i
)
799 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
804 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
805 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);
822 ctx
->descriptor_sets
[i
] =
823 LLVMGetParam(ctx
->main_function
, arg_idx
++);
825 ctx
->descriptor_sets
[i
] = NULL
;
828 uint32_t desc_sgpr_idx
= user_sgpr_idx
;
829 LLVMValueRef desc_sets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
830 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, user_sgpr_idx
, 2);
833 for (unsigned i
= 0; i
< num_sets
; ++i
) {
834 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
835 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
836 ctx
->descriptor_sets
[i
] = ac_build_indexed_load_const(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->i32
, i
, false));
839 ctx
->descriptor_sets
[i
] = NULL
;
841 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
844 if (ctx
->shader_info
->info
.needs_push_constants
) {
845 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
846 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
850 switch (ctx
->stage
) {
851 case MESA_SHADER_COMPUTE
:
852 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
853 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
854 user_sgpr_idx
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
855 ctx
->num_work_groups
=
856 LLVMGetParam(ctx
->main_function
, arg_idx
++);
859 LLVMGetParam(ctx
->main_function
, arg_idx
++);
861 LLVMGetParam(ctx
->main_function
, arg_idx
++);
862 ctx
->local_invocation_ids
=
863 LLVMGetParam(ctx
->main_function
, arg_idx
++);
865 case MESA_SHADER_VERTEX
:
866 if (!ctx
->is_gs_copy_shader
) {
867 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
868 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
870 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
873 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
876 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, vs_num
);
877 user_sgpr_idx
+= vs_num
;
879 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
880 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
881 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
882 ctx
->draw_index
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
884 if (ctx
->options
->key
.vs
.as_es
)
885 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
886 else if (ctx
->options
->key
.vs
.as_ls
) {
887 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, user_sgpr_idx
, 1);
889 ctx
->ls_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
891 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
892 if (!ctx
->is_gs_copy_shader
) {
893 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
894 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
895 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
897 if (ctx
->options
->key
.vs
.as_ls
)
898 declare_tess_lds(ctx
);
900 case MESA_SHADER_TESS_CTRL
:
901 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, user_sgpr_idx
, 4);
903 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
904 ctx
->tcs_out_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
905 ctx
->tcs_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
906 ctx
->tcs_in_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
907 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
908 ctx
->tess_factor_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
909 ctx
->tcs_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
910 ctx
->tcs_rel_ids
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
912 declare_tess_lds(ctx
);
914 case MESA_SHADER_TESS_EVAL
:
915 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, user_sgpr_idx
, 1);
917 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
918 if (ctx
->options
->key
.tes
.as_es
) {
919 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
921 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
924 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
926 ctx
->tes_u
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
927 ctx
->tes_v
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
928 ctx
->tes_rel_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
929 ctx
->tes_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
931 case MESA_SHADER_GEOMETRY
:
932 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
934 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
935 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
936 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
937 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
938 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
939 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
940 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
941 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
942 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
943 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
944 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
945 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
947 case MESA_SHADER_FRAGMENT
:
948 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
949 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, user_sgpr_idx
, 1);
951 ctx
->sample_pos_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
953 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
954 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
955 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
956 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
958 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
959 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
960 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
961 arg_idx
++; /* line stipple */
962 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
963 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
964 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
965 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
966 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
967 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
968 ctx
->sample_coverage
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
971 unreachable("Shader stage not implemented");
975 static void setup_types(struct nir_to_llvm_context
*ctx
)
977 LLVMValueRef args
[4];
979 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
980 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
981 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
982 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
983 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
984 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
985 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
986 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
987 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
988 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
989 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
990 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
991 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
992 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
993 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
994 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
996 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
997 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
998 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
999 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
1000 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
1001 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
1003 args
[0] = ctx
->f32zero
;
1004 args
[1] = ctx
->f32zero
;
1005 args
[2] = ctx
->f32zero
;
1006 args
[3] = ctx
->f32one
;
1007 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
1009 ctx
->uniform_md_kind
=
1010 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
1011 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
1013 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
1016 static int get_llvm_num_components(LLVMValueRef value
)
1018 LLVMTypeRef type
= LLVMTypeOf(value
);
1019 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1020 ? LLVMGetVectorSize(type
)
1022 return num_components
;
1025 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
1029 int count
= get_llvm_num_components(value
);
1031 assert(index
< count
);
1035 return LLVMBuildExtractElement(ctx
->builder
, value
,
1036 LLVMConstInt(ctx
->i32
, index
, false), "");
1039 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
1040 LLVMValueRef value
, unsigned count
)
1042 unsigned num_components
= get_llvm_num_components(value
);
1043 if (count
== num_components
)
1046 LLVMValueRef masks
[] = {
1047 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1048 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1051 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1054 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1055 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1059 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
1060 LLVMValueRef
*values
,
1061 unsigned value_count
,
1062 unsigned value_stride
,
1065 LLVMBuilderRef builder
= ctx
->builder
;
1068 if (value_count
== 1) {
1069 LLVMBuildStore(builder
, vec
, values
[0]);
1073 for (i
= 0; i
< value_count
; i
++) {
1074 LLVMValueRef ptr
= values
[i
* value_stride
];
1075 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
1076 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1077 LLVMBuildStore(builder
, value
, ptr
);
1081 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
1084 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
1085 if (def
->num_components
> 1) {
1086 type
= LLVMVectorType(type
, def
->num_components
);
1091 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
1094 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
1095 return (LLVMValueRef
)entry
->data
;
1099 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
1100 struct nir_block
*b
)
1102 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
1103 return (LLVMBasicBlockRef
)entry
->data
;
1106 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
1108 unsigned num_components
)
1110 LLVMValueRef value
= get_src(ctx
, src
.src
);
1111 bool need_swizzle
= false;
1114 LLVMTypeRef type
= LLVMTypeOf(value
);
1115 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1116 ? LLVMGetVectorSize(type
)
1119 for (unsigned i
= 0; i
< num_components
; ++i
) {
1120 assert(src
.swizzle
[i
] < src_components
);
1121 if (src
.swizzle
[i
] != i
)
1122 need_swizzle
= true;
1125 if (need_swizzle
|| num_components
!= src_components
) {
1126 LLVMValueRef masks
[] = {
1127 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
1128 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
1129 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
1130 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
1132 if (src_components
> 1 && num_components
== 1) {
1133 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
1135 } else if (src_components
== 1 && num_components
> 1) {
1136 LLVMValueRef values
[] = {value
, value
, value
, value
};
1137 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1139 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1140 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
1144 assert(!src
.negate
);
1149 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
1150 LLVMIntPredicate pred
, LLVMValueRef src0
,
1153 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1154 return LLVMBuildSelect(ctx
->builder
, result
,
1155 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1156 LLVMConstInt(ctx
->i32
, 0, false), "");
1159 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
1160 LLVMRealPredicate pred
, LLVMValueRef src0
,
1163 LLVMValueRef result
;
1164 src0
= to_float(ctx
, src0
);
1165 src1
= to_float(ctx
, src1
);
1166 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1167 return LLVMBuildSelect(ctx
->builder
, result
,
1168 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1169 LLVMConstInt(ctx
->i32
, 0, false), "");
1172 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
1174 LLVMTypeRef result_type
,
1178 LLVMValueRef params
[] = {
1179 to_float(ctx
, src0
),
1182 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1183 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1186 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
1188 LLVMTypeRef result_type
,
1189 LLVMValueRef src0
, LLVMValueRef src1
)
1192 LLVMValueRef params
[] = {
1193 to_float(ctx
, src0
),
1194 to_float(ctx
, src1
),
1197 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1198 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1201 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
1203 LLVMTypeRef result_type
,
1204 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1207 LLVMValueRef params
[] = {
1208 to_float(ctx
, src0
),
1209 to_float(ctx
, src1
),
1210 to_float(ctx
, src2
),
1213 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1214 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1217 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
1218 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1220 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1222 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1225 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
1228 LLVMValueRef params
[2] = {
1231 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1232 * add special code to check for x=0. The reason is that
1233 * the LLVM behavior for x=0 is different from what we
1236 * The hardware already implements the correct behavior.
1238 LLVMConstInt(ctx
->i32
, 1, false),
1240 return ac_build_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
1243 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
1246 return ac_build_imsb(&ctx
->ac
, src0
, ctx
->i32
);
1249 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
1252 return ac_build_umsb(&ctx
->ac
, src0
, ctx
->i32
);
1255 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
1256 LLVMIntPredicate pred
,
1257 LLVMValueRef src0
, LLVMValueRef src1
)
1259 return LLVMBuildSelect(ctx
->builder
,
1260 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1265 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
1268 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1269 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1272 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1275 LLVMValueRef cmp
, val
;
1277 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1278 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1279 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1280 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1284 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1287 LLVMValueRef cmp
, val
;
1289 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1290 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1291 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1292 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1296 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1299 const char *intr
= "llvm.floor.f32";
1300 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
1301 LLVMValueRef params
[] = {
1304 LLVMValueRef floor
= ac_build_intrinsic(&ctx
->ac
, intr
,
1305 ctx
->f32
, params
, 1,
1306 AC_FUNC_ATTR_READNONE
);
1307 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1310 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1312 LLVMValueRef src0
, LLVMValueRef src1
)
1314 LLVMTypeRef ret_type
;
1315 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1317 LLVMValueRef params
[] = { src0
, src1
};
1318 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1321 res
= ac_build_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1322 params
, 2, AC_FUNC_ATTR_READNONE
);
1324 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1325 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1329 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1332 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1335 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1338 LLVMValueRef result
;
1341 src0
= to_float(ctx
, src0
);
1342 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
1344 /* TODO SI/CIK options here */
1345 if (ctx
->options
->chip_class
>= VI
) {
1346 LLVMValueRef args
[2];
1347 /* Check if the result is a denormal - and flush to 0 if so. */
1349 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1350 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1353 /* need to convert back up to f32 */
1354 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1356 if (ctx
->options
->chip_class
>= VI
)
1357 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32zero
, result
, "");
1362 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1363 LLVMValueRef src0
, LLVMValueRef src1
)
1365 LLVMValueRef dst64
, result
;
1366 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1367 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1369 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1370 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1371 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1375 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1376 LLVMValueRef src0
, LLVMValueRef src1
)
1378 LLVMValueRef dst64
, result
;
1379 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1380 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1382 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1383 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1384 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1388 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1390 LLVMValueRef srcs
[3])
1392 LLVMValueRef result
;
1393 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1395 result
= ac_build_bfe(&ctx
->ac
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1396 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1400 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1401 LLVMValueRef src0
, LLVMValueRef src1
,
1402 LLVMValueRef src2
, LLVMValueRef src3
)
1404 LLVMValueRef bfi_args
[3], result
;
1406 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1407 LLVMBuildSub(ctx
->builder
,
1408 LLVMBuildShl(ctx
->builder
,
1413 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1416 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1419 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1420 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1422 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1423 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1424 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1426 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1430 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1433 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1435 LLVMValueRef comp
[2];
1437 src0
= to_float(ctx
, src0
);
1438 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1439 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1440 for (i
= 0; i
< 2; i
++) {
1441 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1442 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1443 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1446 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1447 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1452 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1455 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1456 LLVMValueRef temps
[2], result
, val
;
1459 for (i
= 0; i
< 2; i
++) {
1460 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1461 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1462 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1463 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1466 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1468 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1473 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1479 LLVMValueRef result
;
1480 ctx
->has_ddxy
= true;
1482 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1483 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1484 LLVMArrayType(ctx
->i32
, 64),
1485 "ddxy_lds", LOCAL_ADDR_SPACE
);
1487 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1488 mask
= AC_TID_MASK_LEFT
;
1489 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1490 mask
= AC_TID_MASK_TOP
;
1492 mask
= AC_TID_MASK_TOP_LEFT
;
1494 /* for DDX we want to next X pixel, DDY next Y pixel. */
1495 if (op
== nir_op_fddx_fine
||
1496 op
== nir_op_fddx_coarse
||
1502 result
= ac_build_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1503 mask
, idx
, ctx
->lds
,
1509 * this takes an I,J coordinate pair,
1510 * and works out the X and Y derivatives.
1511 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1513 static LLVMValueRef
emit_ddxy_interp(
1514 struct nir_to_llvm_context
*ctx
,
1515 LLVMValueRef interp_ij
)
1517 LLVMValueRef result
[4], a
;
1520 for (i
= 0; i
< 2; i
++) {
1521 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1522 LLVMConstInt(ctx
->i32
, i
, false), "");
1523 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1524 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1526 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1529 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1531 LLVMValueRef src
[4], result
= NULL
;
1532 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1533 unsigned src_components
;
1534 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1536 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1537 switch (instr
->op
) {
1543 case nir_op_pack_half_2x16
:
1546 case nir_op_unpack_half_2x16
:
1550 src_components
= num_components
;
1553 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1554 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1556 switch (instr
->op
) {
1562 src
[0] = to_float(ctx
, src
[0]);
1563 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1566 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1569 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1572 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1575 src
[0] = to_float(ctx
, src
[0]);
1576 src
[1] = to_float(ctx
, src
[1]);
1577 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1580 src
[0] = to_float(ctx
, src
[0]);
1581 src
[1] = to_float(ctx
, src
[1]);
1582 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1585 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1588 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1591 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1594 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1597 src
[0] = to_float(ctx
, src
[0]);
1598 src
[1] = to_float(ctx
, src
[1]);
1599 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1600 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1601 to_float_type(ctx
, def_type
), result
);
1602 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1603 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1606 src
[0] = to_float(ctx
, src
[0]);
1607 src
[1] = to_float(ctx
, src
[1]);
1608 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1611 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1614 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1617 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1620 src
[0] = to_float(ctx
, src
[0]);
1621 src
[1] = to_float(ctx
, src
[1]);
1622 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1625 src
[0] = to_float(ctx
, src
[0]);
1626 src
[1] = to_float(ctx
, src
[1]);
1627 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1630 src
[0] = to_float(ctx
, src
[0]);
1631 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1634 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1637 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1640 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1643 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1646 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1649 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1652 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1655 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1658 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1661 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1664 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1667 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1670 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1673 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1676 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1679 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1682 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1683 to_float_type(ctx
, def_type
), src
[0]);
1686 result
= emit_iabs(ctx
, src
[0]);
1689 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1692 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1695 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1698 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1701 result
= emit_isign(ctx
, src
[0]);
1704 src
[0] = to_float(ctx
, src
[0]);
1705 result
= emit_fsign(ctx
, src
[0]);
1708 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1709 to_float_type(ctx
, def_type
), src
[0]);
1712 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1713 to_float_type(ctx
, def_type
), src
[0]);
1716 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1717 to_float_type(ctx
, def_type
), src
[0]);
1719 case nir_op_fround_even
:
1720 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1721 to_float_type(ctx
, def_type
),src
[0]);
1724 result
= emit_ffract(ctx
, src
[0]);
1727 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1728 to_float_type(ctx
, def_type
), src
[0]);
1731 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1732 to_float_type(ctx
, def_type
), src
[0]);
1735 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1736 to_float_type(ctx
, def_type
), src
[0]);
1739 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1740 to_float_type(ctx
, def_type
), src
[0]);
1743 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1744 to_float_type(ctx
, def_type
), src
[0]);
1747 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1748 to_float_type(ctx
, def_type
), src
[0]);
1749 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1752 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1753 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1756 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1757 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1758 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1759 result
= emit_intrin_1f_param(ctx
, "llvm.canonicalize",
1760 to_float_type(ctx
, def_type
),
1764 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1765 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1766 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1767 result
= emit_intrin_1f_param(ctx
, "llvm.canonicalize",
1768 to_float_type(ctx
, def_type
),
1772 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1773 to_float_type(ctx
, def_type
), src
[0], src
[1], src
[2]);
1775 case nir_op_ibitfield_extract
:
1776 result
= emit_bitfield_extract(ctx
, true, src
);
1778 case nir_op_ubitfield_extract
:
1779 result
= emit_bitfield_extract(ctx
, false, src
);
1781 case nir_op_bitfield_insert
:
1782 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1784 case nir_op_bitfield_reverse
:
1785 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1787 case nir_op_bit_count
:
1788 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1793 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1794 src
[i
] = to_integer(ctx
, src
[i
]);
1795 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1799 src
[0] = to_float(ctx
, src
[0]);
1800 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1804 src
[0] = to_float(ctx
, src
[0]);
1805 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1809 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1813 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1816 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1819 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1823 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1824 result
= LLVMBuildZExt(ctx
->builder
, src
[0], def_type
, "");
1826 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1830 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1831 result
= LLVMBuildSExt(ctx
->builder
, src
[0], def_type
, "");
1833 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1836 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1838 case nir_op_find_lsb
:
1839 result
= emit_find_lsb(ctx
, src
[0]);
1841 case nir_op_ufind_msb
:
1842 result
= emit_ufind_msb(ctx
, src
[0]);
1844 case nir_op_ifind_msb
:
1845 result
= emit_ifind_msb(ctx
, src
[0]);
1847 case nir_op_uadd_carry
:
1848 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1850 case nir_op_usub_borrow
:
1851 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1854 result
= emit_b2f(ctx
, src
[0]);
1856 case nir_op_fquantize2f16
:
1857 result
= emit_f2f16(ctx
, src
[0]);
1859 case nir_op_umul_high
:
1860 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1862 case nir_op_imul_high
:
1863 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1865 case nir_op_pack_half_2x16
:
1866 result
= emit_pack_half_2x16(ctx
, src
[0]);
1868 case nir_op_unpack_half_2x16
:
1869 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1873 case nir_op_fddx_fine
:
1874 case nir_op_fddy_fine
:
1875 case nir_op_fddx_coarse
:
1876 case nir_op_fddy_coarse
:
1877 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1880 fprintf(stderr
, "Unknown NIR alu instr: ");
1881 nir_print_instr(&instr
->instr
, stderr
);
1882 fprintf(stderr
, "\n");
1887 assert(instr
->dest
.dest
.is_ssa
);
1888 result
= to_integer(ctx
, result
);
1889 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1894 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1895 nir_load_const_instr
*instr
)
1897 LLVMValueRef values
[4], value
= NULL
;
1898 LLVMTypeRef element_type
=
1899 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1901 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1902 switch (instr
->def
.bit_size
) {
1904 values
[i
] = LLVMConstInt(element_type
,
1905 instr
->value
.u32
[i
], false);
1908 values
[i
] = LLVMConstInt(element_type
,
1909 instr
->value
.u64
[i
], false);
1913 "unsupported nir load_const bit_size: %d\n",
1914 instr
->def
.bit_size
);
1918 if (instr
->def
.num_components
> 1) {
1919 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1923 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1926 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1929 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1930 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1931 LLVMPointerType(type
, addr_space
), "");
1935 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1938 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1939 LLVMConstInt(ctx
->i32
, 2, false), "");
1942 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1943 /* On VI, the descriptor contains the size in bytes,
1944 * but TXQ must return the size in elements.
1945 * The stride is always non-zero for resources using TXQ.
1947 LLVMValueRef stride
=
1948 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1949 LLVMConstInt(ctx
->i32
, 1, false), "");
1950 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1951 LLVMConstInt(ctx
->i32
, 16, false), "");
1952 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1953 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1955 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1961 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1964 static void build_int_type_name(
1966 char *buf
, unsigned bufsize
)
1968 assert(bufsize
>= 6);
1970 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1971 snprintf(buf
, bufsize
, "v%ui32",
1972 LLVMGetVectorSize(type
));
1977 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1978 struct ac_image_args
*args
,
1979 nir_tex_instr
*instr
)
1981 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1982 LLVMValueRef coord
= args
->addr
;
1983 LLVMValueRef half_texel
[2];
1984 LLVMValueRef compare_cube_wa
;
1985 LLVMValueRef result
;
1987 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1991 struct ac_image_args txq_args
= { 0 };
1993 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1994 txq_args
.opcode
= ac_image_get_resinfo
;
1995 txq_args
.dmask
= 0xf;
1996 txq_args
.addr
= ctx
->i32zero
;
1997 txq_args
.resource
= args
->resource
;
1998 LLVMValueRef size
= ac_build_image_opcode(&ctx
->ac
, &txq_args
);
2000 for (c
= 0; c
< 2; c
++) {
2001 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
2002 LLVMConstInt(ctx
->i32
, c
, false), "");
2003 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
2004 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
2005 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
2006 LLVMConstReal(ctx
->f32
, -0.5), "");
2010 LLVMValueRef orig_coords
= args
->addr
;
2012 for (c
= 0; c
< 2; c
++) {
2014 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2015 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2016 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2017 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2018 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2019 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2024 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2025 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2026 * workaround by sampling using a scaled type and converting.
2027 * This is taken from amdgpu-pro shaders.
2029 /* NOTE this produces some ugly code compared to amdgpu-pro,
2030 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2031 * and then reads them back. -pro generates two selects,
2032 * one s_cmp for the descriptor rewriting
2033 * one v_cmp for the coordinate and result changes.
2035 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2036 LLVMValueRef tmp
, tmp2
;
2038 /* workaround 8/8/8/8 uint/sint cube gather bug */
2039 /* first detect it then change to a scaled read and f2i */
2040 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32one
, "");
2043 /* extract the DATA_FORMAT */
2044 tmp
= ac_build_bfe(&ctx
->ac
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2045 LLVMConstInt(ctx
->i32
, 6, false), false);
2047 /* is the DATA_FORMAT == 8_8_8_8 */
2048 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2050 if (stype
== GLSL_TYPE_UINT
)
2051 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2052 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2053 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2055 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2056 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2057 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2059 /* replace the NUM FORMAT in the descriptor */
2060 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2061 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2063 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32one
, "");
2065 /* don't modify the coordinates for this case */
2066 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2069 result
= ac_build_image_opcode(&ctx
->ac
, args
);
2071 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2072 LLVMValueRef tmp
, tmp2
;
2074 /* if the cube workaround is in place, f2i the result. */
2075 for (c
= 0; c
< 4; c
++) {
2076 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2077 if (stype
== GLSL_TYPE_UINT
)
2078 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2080 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2081 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2082 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2083 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2084 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2085 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2091 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
2092 nir_tex_instr
*instr
,
2094 struct ac_image_args
*args
)
2096 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2097 return ac_build_buffer_load_format(&ctx
->ac
,
2100 LLVMConstInt(ctx
->i32
, 0, false),
2104 args
->opcode
= ac_image_sample
;
2105 args
->compare
= instr
->is_shadow
;
2107 switch (instr
->op
) {
2109 case nir_texop_txf_ms
:
2110 case nir_texop_samples_identical
:
2111 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2112 args
->compare
= false;
2113 args
->offset
= false;
2120 args
->level_zero
= true;
2125 case nir_texop_query_levels
:
2126 args
->opcode
= ac_image_get_resinfo
;
2129 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2130 args
->level_zero
= true;
2136 args
->opcode
= ac_image_gather4
;
2137 args
->level_zero
= true;
2140 args
->opcode
= ac_image_get_lod
;
2141 args
->compare
= false;
2142 args
->offset
= false;
2148 if (instr
->op
== nir_texop_tg4
) {
2149 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2150 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2151 return radv_lower_gather4_integer(ctx
, args
, instr
);
2154 return ac_build_image_opcode(&ctx
->ac
, args
);
2157 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2158 nir_intrinsic_instr
*instr
)
2160 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2161 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2162 unsigned binding
= nir_intrinsic_binding(instr
);
2163 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2164 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2165 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2166 unsigned base_offset
= layout
->binding
[binding
].offset
;
2167 LLVMValueRef offset
, stride
;
2169 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2170 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2171 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2172 layout
->binding
[binding
].dynamic_offset_offset
;
2173 desc_ptr
= ctx
->push_constants
;
2174 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2175 stride
= LLVMConstInt(ctx
->i32
, 16, false);
2177 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
2179 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
2180 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2181 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2183 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2184 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2185 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2187 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2190 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2191 nir_intrinsic_instr
*instr
)
2193 LLVMValueRef ptr
, addr
;
2195 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
2196 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
2198 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2199 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
2201 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2204 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
2205 nir_intrinsic_instr
*instr
)
2207 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2209 return get_buffer_size(ctx
, desc
, false);
2211 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
2212 nir_intrinsic_instr
*instr
)
2214 const char *store_name
;
2215 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2216 LLVMTypeRef data_type
= ctx
->f32
;
2217 int elem_size_mult
= get_elem_bits(ctx
, LLVMTypeOf(src_data
)) / 32;
2218 int components_32bit
= elem_size_mult
* instr
->num_components
;
2219 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2220 LLVMValueRef base_data
, base_offset
;
2221 LLVMValueRef params
[6];
2223 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2224 ctx
->shader_info
->fs
.writes_memory
= true;
2226 params
[1] = get_src(ctx
, instr
->src
[1]);
2227 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2228 params
[4] = ctx
->i1false
; /* glc */
2229 params
[5] = ctx
->i1false
; /* slc */
2231 if (components_32bit
> 1)
2232 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
2234 base_data
= to_float(ctx
, src_data
);
2235 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
2236 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
2238 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2242 LLVMValueRef offset
;
2244 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2246 /* Due to an LLVM limitation, split 3-element writes
2247 * into a 2-element and a 1-element write. */
2249 writemask
|= 1 << (start
+ 2);
2253 start
*= elem_size_mult
;
2254 count
*= elem_size_mult
;
2257 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2262 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2264 } else if (count
== 2) {
2265 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2266 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
2267 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
2270 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2271 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
2272 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
2274 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2278 if (get_llvm_num_components(base_data
) > 1)
2279 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
2280 LLVMConstInt(ctx
->i32
, start
, false), "");
2283 store_name
= "llvm.amdgcn.buffer.store.f32";
2286 offset
= base_offset
;
2288 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
2292 ac_build_intrinsic(&ctx
->ac
, store_name
,
2293 ctx
->voidt
, params
, 6, 0);
2297 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
2298 nir_intrinsic_instr
*instr
)
2301 LLVMValueRef params
[6];
2303 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2304 ctx
->shader_info
->fs
.writes_memory
= true;
2306 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2307 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
2309 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
2310 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
2311 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2312 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2313 params
[arg_count
++] = ctx
->i1false
; /* slc */
2315 switch (instr
->intrinsic
) {
2316 case nir_intrinsic_ssbo_atomic_add
:
2317 name
= "llvm.amdgcn.buffer.atomic.add";
2319 case nir_intrinsic_ssbo_atomic_imin
:
2320 name
= "llvm.amdgcn.buffer.atomic.smin";
2322 case nir_intrinsic_ssbo_atomic_umin
:
2323 name
= "llvm.amdgcn.buffer.atomic.umin";
2325 case nir_intrinsic_ssbo_atomic_imax
:
2326 name
= "llvm.amdgcn.buffer.atomic.smax";
2328 case nir_intrinsic_ssbo_atomic_umax
:
2329 name
= "llvm.amdgcn.buffer.atomic.umax";
2331 case nir_intrinsic_ssbo_atomic_and
:
2332 name
= "llvm.amdgcn.buffer.atomic.and";
2334 case nir_intrinsic_ssbo_atomic_or
:
2335 name
= "llvm.amdgcn.buffer.atomic.or";
2337 case nir_intrinsic_ssbo_atomic_xor
:
2338 name
= "llvm.amdgcn.buffer.atomic.xor";
2340 case nir_intrinsic_ssbo_atomic_exchange
:
2341 name
= "llvm.amdgcn.buffer.atomic.swap";
2343 case nir_intrinsic_ssbo_atomic_comp_swap
:
2344 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2350 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2353 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2354 nir_intrinsic_instr
*instr
)
2356 LLVMValueRef results
[2];
2357 int load_components
;
2358 int num_components
= instr
->num_components
;
2359 if (instr
->dest
.ssa
.bit_size
== 64)
2360 num_components
*= 2;
2362 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2363 load_components
= MIN2(num_components
- i
, 4);
2364 const char *load_name
;
2365 LLVMTypeRef data_type
= ctx
->f32
;
2366 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
2367 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2369 if (load_components
== 3)
2370 data_type
= LLVMVectorType(ctx
->f32
, 4);
2371 else if (load_components
> 1)
2372 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
2374 if (load_components
>= 3)
2375 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2376 else if (load_components
== 2)
2377 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2378 else if (load_components
== 1)
2379 load_name
= "llvm.amdgcn.buffer.load.f32";
2381 unreachable("unhandled number of components");
2383 LLVMValueRef params
[] = {
2384 get_src(ctx
, instr
->src
[0]),
2385 LLVMConstInt(ctx
->i32
, 0, false),
2391 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2395 LLVMValueRef ret
= results
[0];
2396 if (num_components
> 4 || num_components
== 3) {
2397 LLVMValueRef masks
[] = {
2398 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2399 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2400 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2401 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2404 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2405 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2406 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2409 return LLVMBuildBitCast(ctx
->builder
, ret
,
2410 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2413 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2414 nir_intrinsic_instr
*instr
)
2416 LLVMValueRef results
[8], ret
;
2417 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2418 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2419 int num_components
= instr
->num_components
;
2421 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2423 if (instr
->dest
.ssa
.bit_size
== 64)
2424 num_components
*= 2;
2426 for (unsigned i
= 0; i
< num_components
; ++i
) {
2427 LLVMValueRef params
[] = {
2429 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2432 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2434 AC_FUNC_ATTR_READNONE
|
2435 AC_FUNC_ATTR_LEGACY
);
2439 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2440 return LLVMBuildBitCast(ctx
->builder
, ret
,
2441 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2445 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref_var
*deref
,
2446 bool vs_in
, unsigned *vertex_index_out
,
2447 LLVMValueRef
*vertex_index_ref
,
2448 unsigned *const_out
, LLVMValueRef
*indir_out
)
2450 unsigned const_offset
= 0;
2451 nir_deref
*tail
= &deref
->deref
;
2452 LLVMValueRef offset
= NULL
;
2454 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2456 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2457 if (vertex_index_out
)
2458 *vertex_index_out
= deref_array
->base_offset
;
2460 if (vertex_index_ref
) {
2461 LLVMValueRef vtx
= LLVMConstInt(ctx
->i32
, deref_array
->base_offset
, false);
2462 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2463 vtx
= LLVMBuildAdd(ctx
->builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2465 *vertex_index_ref
= vtx
;
2469 if (deref
->var
->data
.compact
) {
2470 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2471 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2472 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2473 /* We always lower indirect dereferences for "compact" array vars. */
2474 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2476 const_offset
= deref_array
->base_offset
;
2480 while (tail
->child
!= NULL
) {
2481 const struct glsl_type
*parent_type
= tail
->type
;
2484 if (tail
->deref_type
== nir_deref_type_array
) {
2485 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2486 LLVMValueRef index
, stride
, local_offset
;
2487 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2489 const_offset
+= size
* deref_array
->base_offset
;
2490 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2493 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2494 index
= get_src(ctx
, deref_array
->indirect
);
2495 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2496 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2499 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2501 offset
= local_offset
;
2502 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2503 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2505 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2506 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2507 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2510 unreachable("unsupported deref type");
2514 if (const_offset
&& offset
)
2515 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2516 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2519 *const_out
= const_offset
;
2520 *indir_out
= offset
;
2524 lds_load(struct nir_to_llvm_context
*ctx
,
2525 LLVMValueRef dw_addr
)
2528 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
2533 lds_store(struct nir_to_llvm_context
*ctx
,
2534 LLVMValueRef dw_addr
, LLVMValueRef value
)
2536 value
= LLVMBuildBitCast(ctx
->builder
, value
, ctx
->i32
, "");
2537 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
2541 /* The offchip buffer layout for TCS->TES is
2543 * - attribute 0 of patch 0 vertex 0
2544 * - attribute 0 of patch 0 vertex 1
2545 * - attribute 0 of patch 0 vertex 2
2547 * - attribute 0 of patch 1 vertex 0
2548 * - attribute 0 of patch 1 vertex 1
2550 * - attribute 1 of patch 0 vertex 0
2551 * - attribute 1 of patch 0 vertex 1
2553 * - per patch attribute 0 of patch 0
2554 * - per patch attribute 0 of patch 1
2557 * Note that every attribute has 4 components.
2559 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2560 LLVMValueRef vertex_index
,
2561 LLVMValueRef param_index
)
2563 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2564 LLVMValueRef param_stride
, constant16
;
2565 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2567 vertices_per_patch
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 9, 6);
2568 num_patches
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 0, 9);
2569 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2572 constant16
= LLVMConstInt(ctx
->i32
, 16, false);
2574 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2575 vertices_per_patch
, "");
2577 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2580 param_stride
= total_vertices
;
2582 base_addr
= rel_patch_id
;
2583 param_stride
= num_patches
;
2586 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2587 LLVMBuildMul(ctx
->builder
, param_index
,
2588 param_stride
, ""), "");
2590 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2592 if (!vertex_index
) {
2593 LLVMValueRef patch_data_offset
=
2594 unpack_param(ctx
, ctx
->tcs_offchip_layout
, 16, 16);
2596 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2597 patch_data_offset
, "");
2602 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2604 unsigned const_index
,
2606 LLVMValueRef vertex_index
,
2607 LLVMValueRef indir_index
)
2609 LLVMValueRef param_index
;
2612 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, param
, false),
2615 if (const_index
&& !is_compact
)
2616 param
+= const_index
;
2617 param_index
= LLVMConstInt(ctx
->i32
, param
, false);
2619 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2623 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2624 bool is_patch
, uint32_t param
)
2628 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2630 ctx
->tess_outputs_written
|= (1ull << param
);
2634 get_dw_address(struct nir_to_llvm_context
*ctx
,
2635 LLVMValueRef dw_addr
,
2637 unsigned const_index
,
2638 bool compact_const_index
,
2639 LLVMValueRef vertex_index
,
2640 LLVMValueRef stride
,
2641 LLVMValueRef indir_index
)
2646 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2647 LLVMBuildMul(ctx
->builder
,
2653 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2654 LLVMBuildMul(ctx
->builder
, indir_index
,
2655 LLVMConstInt(ctx
->i32
, 4, false), ""), "");
2656 else if (const_index
&& !compact_const_index
)
2657 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2658 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2660 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2661 LLVMConstInt(ctx
->i32
, param
* 4, false), "");
2663 if (const_index
&& compact_const_index
)
2664 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2665 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2670 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2671 nir_intrinsic_instr
*instr
)
2673 LLVMValueRef dw_addr
, stride
;
2674 unsigned const_index
;
2675 LLVMValueRef vertex_index
;
2676 LLVMValueRef indir_index
;
2678 LLVMValueRef value
[4], result
;
2679 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2680 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2681 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2682 radv_get_deref_offset(ctx
, instr
->variables
[0],
2683 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2684 &const_index
, &indir_index
);
2686 stride
= unpack_param(ctx
, ctx
->tcs_in_layout
, 13, 8);
2687 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2688 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2691 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2692 value
[i
] = lds_load(ctx
, dw_addr
);
2693 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2696 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2697 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2702 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2703 nir_intrinsic_instr
*instr
)
2705 LLVMValueRef dw_addr
, stride
;
2706 LLVMValueRef value
[4], result
;
2707 LLVMValueRef vertex_index
= NULL
;
2708 LLVMValueRef indir_index
= NULL
;
2709 unsigned const_index
= 0;
2711 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2712 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2713 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2714 radv_get_deref_offset(ctx
, instr
->variables
[0],
2715 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2716 &const_index
, &indir_index
);
2718 if (!instr
->variables
[0]->var
->data
.patch
) {
2719 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2720 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2722 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2725 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2728 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2729 value
[i
] = lds_load(ctx
, dw_addr
);
2730 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2733 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2734 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2739 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2740 nir_intrinsic_instr
*instr
,
2744 LLVMValueRef stride
, dw_addr
;
2745 LLVMValueRef buf_addr
= NULL
;
2746 LLVMValueRef vertex_index
= NULL
;
2747 LLVMValueRef indir_index
= NULL
;
2748 unsigned const_index
= 0;
2750 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2751 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2753 radv_get_deref_offset(ctx
, instr
->variables
[0],
2754 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2755 &const_index
, &indir_index
);
2757 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2758 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2759 is_compact
&& const_index
> 3) {
2764 if (!instr
->variables
[0]->var
->data
.patch
) {
2765 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2766 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2768 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2771 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2773 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2775 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2776 vertex_index
, indir_index
);
2778 unsigned base
= is_compact
? const_index
: 0;
2779 for (unsigned chan
= 0; chan
< 8; chan
++) {
2780 bool is_tess_factor
= false;
2781 if (!(writemask
& (1 << chan
)))
2783 LLVMValueRef value
= llvm_extract_elem(ctx
, src
, chan
);
2785 lds_store(ctx
, dw_addr
, value
);
2787 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2788 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2789 is_tess_factor
= true;
2791 if (!is_tess_factor
&& writemask
!= 0xF)
2792 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2793 buf_addr
, ctx
->oc_lds
,
2794 4 * (base
+ chan
), 1, 0, true, false);
2796 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2800 if (writemask
== 0xF) {
2801 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2802 buf_addr
, ctx
->oc_lds
,
2803 (base
* 4), 1, 0, true, false);
2808 load_tes_input(struct nir_to_llvm_context
*ctx
,
2809 nir_intrinsic_instr
*instr
)
2811 LLVMValueRef buf_addr
;
2812 LLVMValueRef result
;
2813 LLVMValueRef vertex_index
= NULL
;
2814 LLVMValueRef indir_index
= NULL
;
2815 unsigned const_index
= 0;
2817 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2818 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2820 radv_get_deref_offset(ctx
, instr
->variables
[0],
2821 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2822 &const_index
, &indir_index
);
2823 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2824 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2825 is_compact
&& const_index
> 3) {
2829 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2830 is_compact
, vertex_index
, indir_index
);
2832 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2833 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2834 result
= trim_vector(ctx
, result
, instr
->num_components
);
2835 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2840 load_gs_input(struct nir_to_llvm_context
*ctx
,
2841 nir_intrinsic_instr
*instr
)
2843 LLVMValueRef indir_index
, vtx_offset
;
2844 unsigned const_index
;
2845 LLVMValueRef args
[9];
2846 unsigned param
, vtx_offset_param
;
2847 LLVMValueRef value
[4], result
;
2848 unsigned vertex_index
;
2849 radv_get_deref_offset(ctx
, instr
->variables
[0],
2850 false, &vertex_index
, NULL
,
2851 &const_index
, &indir_index
);
2852 vtx_offset_param
= vertex_index
;
2853 assert(vtx_offset_param
< 6);
2854 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2855 LLVMConstInt(ctx
->i32
, 4, false), "");
2857 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2858 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2860 args
[0] = ctx
->esgs_ring
;
2861 args
[1] = vtx_offset
;
2862 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2863 args
[3] = ctx
->i32zero
;
2864 args
[4] = ctx
->i32one
; /* OFFEN */
2865 args
[5] = ctx
->i32zero
; /* IDXEN */
2866 args
[6] = ctx
->i32one
; /* GLC */
2867 args
[7] = ctx
->i32zero
; /* SLC */
2868 args
[8] = ctx
->i32zero
; /* TFE */
2870 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2872 AC_FUNC_ATTR_READONLY
|
2873 AC_FUNC_ATTR_LEGACY
);
2875 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2880 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2881 nir_intrinsic_instr
*instr
)
2883 LLVMValueRef values
[8];
2884 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2885 int ve
= instr
->dest
.ssa
.num_components
;
2886 LLVMValueRef indir_index
;
2888 unsigned const_index
;
2889 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2890 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2891 radv_get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
2892 &const_index
, &indir_index
);
2894 if (instr
->dest
.ssa
.bit_size
== 64)
2897 switch (instr
->variables
[0]->var
->data
.mode
) {
2898 case nir_var_shader_in
:
2899 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2900 return load_tcs_input(ctx
, instr
);
2901 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
2902 return load_tes_input(ctx
, instr
);
2903 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2904 return load_gs_input(ctx
, instr
);
2906 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2908 unsigned count
= glsl_count_attribute_slots(
2909 instr
->variables
[0]->var
->type
,
2910 ctx
->stage
== MESA_SHADER_VERTEX
);
2912 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2913 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2916 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2920 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2924 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2926 unsigned count
= glsl_count_attribute_slots(
2927 instr
->variables
[0]->var
->type
, false);
2929 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2930 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2933 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2937 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2941 case nir_var_shader_out
:
2942 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2943 return load_tcs_output(ctx
, instr
);
2944 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2946 unsigned count
= glsl_count_attribute_slots(
2947 instr
->variables
[0]->var
->type
, false);
2949 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2950 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2953 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2957 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2958 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2963 case nir_var_shared
: {
2964 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2965 LLVMValueRef derived_ptr
;
2968 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2970 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2971 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2973 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2974 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2976 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2981 unreachable("unhandle variable mode");
2983 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2984 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2988 visit_store_var(struct nir_to_llvm_context
*ctx
,
2989 nir_intrinsic_instr
*instr
)
2991 LLVMValueRef temp_ptr
, value
;
2992 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2993 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2994 int writemask
= instr
->const_index
[0];
2995 LLVMValueRef indir_index
;
2996 unsigned const_index
;
2997 radv_get_deref_offset(ctx
, instr
->variables
[0], false,
2998 NULL
, NULL
, &const_index
, &indir_index
);
3000 if (get_elem_bits(ctx
, LLVMTypeOf(src
)) == 64) {
3001 int old_writemask
= writemask
;
3003 src
= LLVMBuildBitCast(ctx
->builder
, src
,
3004 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
3008 for (unsigned chan
= 0; chan
< 4; chan
++) {
3009 if (old_writemask
& (1 << chan
))
3010 writemask
|= 3u << (2 * chan
);
3014 switch (instr
->variables
[0]->var
->data
.mode
) {
3015 case nir_var_shader_out
:
3017 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3018 store_tcs_output(ctx
, instr
, src
, writemask
);
3022 for (unsigned chan
= 0; chan
< 8; chan
++) {
3024 if (!(writemask
& (1 << chan
)))
3027 value
= llvm_extract_elem(ctx
, src
, chan
);
3029 if (instr
->variables
[0]->var
->data
.compact
)
3032 unsigned count
= glsl_count_attribute_slots(
3033 instr
->variables
[0]->var
->type
, false);
3035 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3036 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3039 if (get_llvm_num_components(tmp_vec
) > 1) {
3040 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
3041 value
, indir_index
, "");
3044 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
3045 count
, stride
, tmp_vec
);
3048 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3050 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
3055 for (unsigned chan
= 0; chan
< 8; chan
++) {
3056 if (!(writemask
& (1 << chan
)))
3059 value
= llvm_extract_elem(ctx
, src
, chan
);
3061 unsigned count
= glsl_count_attribute_slots(
3062 instr
->variables
[0]->var
->type
, false);
3064 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3065 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3068 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
3069 value
, indir_index
, "");
3070 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
3073 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3075 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
3079 case nir_var_shared
: {
3080 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3083 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
3085 for (unsigned chan
= 0; chan
< 8; chan
++) {
3086 if (!(writemask
& (1 << chan
)))
3088 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
3089 LLVMValueRef derived_ptr
;
3092 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
3094 value
= llvm_extract_elem(ctx
, src
, chan
);
3095 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
3096 LLVMBuildStore(ctx
->builder
,
3097 to_integer(ctx
, value
), derived_ptr
);
3106 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3109 case GLSL_SAMPLER_DIM_BUF
:
3111 case GLSL_SAMPLER_DIM_1D
:
3112 return array
? 2 : 1;
3113 case GLSL_SAMPLER_DIM_2D
:
3114 return array
? 3 : 2;
3115 case GLSL_SAMPLER_DIM_MS
:
3116 return array
? 4 : 3;
3117 case GLSL_SAMPLER_DIM_3D
:
3118 case GLSL_SAMPLER_DIM_CUBE
:
3120 case GLSL_SAMPLER_DIM_RECT
:
3121 case GLSL_SAMPLER_DIM_SUBPASS
:
3123 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3133 /* Adjust the sample index according to FMASK.
3135 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3136 * which is the identity mapping. Each nibble says which physical sample
3137 * should be fetched to get that sample.
3139 * For example, 0x11111100 means there are only 2 samples stored and
3140 * the second sample covers 3/4 of the pixel. When reading samples 0
3141 * and 1, return physical sample 0 (determined by the first two 0s
3142 * in FMASK), otherwise return physical sample 1.
3144 * The sample index should be adjusted as follows:
3145 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3147 static LLVMValueRef
adjust_sample_index_using_fmask(struct nir_to_llvm_context
*ctx
,
3148 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3149 LLVMValueRef coord_z
,
3150 LLVMValueRef sample_index
,
3151 LLVMValueRef fmask_desc_ptr
)
3153 LLVMValueRef fmask_load_address
[4];
3156 fmask_load_address
[0] = coord_x
;
3157 fmask_load_address
[1] = coord_y
;
3159 fmask_load_address
[2] = coord_z
;
3160 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3163 struct ac_image_args args
= {0};
3165 args
.opcode
= ac_image_load
;
3166 args
.da
= coord_z
? true : false;
3167 args
.resource
= fmask_desc_ptr
;
3169 args
.addr
= ac_build_gather_values(&ctx
->ac
, fmask_load_address
, coord_z
? 4 : 2);
3171 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3173 res
= to_integer(ctx
, res
);
3174 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3175 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3177 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3181 LLVMValueRef sample_index4
=
3182 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3183 LLVMValueRef shifted_fmask
=
3184 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3185 LLVMValueRef final_sample
=
3186 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3188 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3189 * resource descriptor is 0 (invalid),
3191 LLVMValueRef fmask_desc
=
3192 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3195 LLVMValueRef fmask_word1
=
3196 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3199 LLVMValueRef word1_is_nonzero
=
3200 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3201 fmask_word1
, ctx
->i32zero
, "");
3203 /* Replace the MSAA sample index. */
3205 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3206 final_sample
, sample_index
, "");
3207 return sample_index
;
3210 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
3211 nir_intrinsic_instr
*instr
)
3213 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3214 if(instr
->variables
[0]->deref
.child
)
3215 type
= instr
->variables
[0]->deref
.child
->type
;
3217 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3218 LLVMValueRef coords
[4];
3219 LLVMValueRef masks
[] = {
3220 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
3221 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
3224 LLVMValueRef sample_index
= llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
3227 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3228 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3229 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3230 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3231 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3233 count
= image_type_to_components_count(dim
,
3234 glsl_sampler_type_is_array(type
));
3237 LLVMValueRef fmask_load_address
[3];
3240 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3241 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[1], "");
3242 if (glsl_sampler_type_is_array(type
))
3243 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[2], "");
3245 fmask_load_address
[2] = NULL
;
3247 for (chan
= 0; chan
< 2; ++chan
)
3248 fmask_load_address
[chan
] = LLVMBuildAdd(ctx
->builder
, fmask_load_address
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3250 sample_index
= adjust_sample_index_using_fmask(ctx
,
3251 fmask_load_address
[0],
3252 fmask_load_address
[1],
3253 fmask_load_address
[2],
3255 get_sampler_desc(ctx
, instr
->variables
[0], DESC_FMASK
));
3258 if (instr
->src
[0].ssa
->num_components
)
3259 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3266 for (chan
= 0; chan
< count
; ++chan
) {
3267 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
3271 for (chan
= 0; chan
< count
; ++chan
)
3272 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3275 coords
[count
] = sample_index
;
3280 coords
[3] = LLVMGetUndef(ctx
->i32
);
3283 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3288 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
3289 nir_intrinsic_instr
*instr
)
3291 LLVMValueRef params
[7];
3293 char intrinsic_name
[64];
3294 const nir_variable
*var
= instr
->variables
[0]->var
;
3295 const struct glsl_type
*type
= var
->type
;
3296 if(instr
->variables
[0]->deref
.child
)
3297 type
= instr
->variables
[0]->deref
.child
->type
;
3299 type
= glsl_without_array(type
);
3300 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3301 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3302 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3303 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3304 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3305 params
[3] = ctx
->i1false
; /* glc */
3306 params
[4] = ctx
->i1false
; /* slc */
3307 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
3310 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
3311 res
= to_integer(ctx
, res
);
3313 bool is_da
= glsl_sampler_type_is_array(type
) ||
3314 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3315 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3316 LLVMValueRef glc
= ctx
->i1false
;
3317 LLVMValueRef slc
= ctx
->i1false
;
3319 params
[0] = get_image_coords(ctx
, instr
);
3320 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3321 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3322 if (HAVE_LLVM
<= 0x0309) {
3323 params
[3] = ctx
->i1false
; /* r128 */
3328 LLVMValueRef lwe
= ctx
->i1false
;
3335 ac_get_image_intr_name("llvm.amdgcn.image.load",
3336 ctx
->v4f32
, /* vdata */
3337 LLVMTypeOf(params
[0]), /* coords */
3338 LLVMTypeOf(params
[1]), /* rsrc */
3339 intrinsic_name
, sizeof(intrinsic_name
));
3341 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
3342 params
, 7, AC_FUNC_ATTR_READONLY
);
3344 return to_integer(ctx
, res
);
3347 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
3348 nir_intrinsic_instr
*instr
)
3350 LLVMValueRef params
[8];
3351 char intrinsic_name
[64];
3352 const nir_variable
*var
= instr
->variables
[0]->var
;
3353 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3355 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3356 ctx
->shader_info
->fs
.writes_memory
= true;
3358 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3359 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
3360 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3361 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3362 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3363 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3364 params
[4] = ctx
->i1false
; /* glc */
3365 params
[5] = ctx
->i1false
; /* slc */
3366 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
3369 bool is_da
= glsl_sampler_type_is_array(type
) ||
3370 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3371 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3372 LLVMValueRef glc
= ctx
->i1false
;
3373 LLVMValueRef slc
= ctx
->i1false
;
3375 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
3376 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3377 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3378 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3379 if (HAVE_LLVM
<= 0x0309) {
3380 params
[4] = ctx
->i1false
; /* r128 */
3385 LLVMValueRef lwe
= ctx
->i1false
;
3392 ac_get_image_intr_name("llvm.amdgcn.image.store",
3393 LLVMTypeOf(params
[0]), /* vdata */
3394 LLVMTypeOf(params
[1]), /* coords */
3395 LLVMTypeOf(params
[2]), /* rsrc */
3396 intrinsic_name
, sizeof(intrinsic_name
));
3398 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
3404 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
3405 nir_intrinsic_instr
*instr
)
3407 LLVMValueRef params
[6];
3408 int param_count
= 0;
3409 const nir_variable
*var
= instr
->variables
[0]->var
;
3411 const char *base_name
= "llvm.amdgcn.image.atomic";
3412 const char *atomic_name
;
3413 LLVMValueRef coords
;
3414 char intrinsic_name
[32], coords_type
[8];
3415 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3417 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3418 ctx
->shader_info
->fs
.writes_memory
= true;
3420 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3421 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3422 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3424 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3425 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3426 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3427 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3428 params
[param_count
++] = ctx
->i32zero
; /* voffset */
3429 params
[param_count
++] = ctx
->i1false
; /* glc */
3430 params
[param_count
++] = ctx
->i1false
; /* slc */
3432 bool da
= glsl_sampler_type_is_array(type
) ||
3433 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3435 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3436 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3437 params
[param_count
++] = ctx
->i1false
; /* r128 */
3438 params
[param_count
++] = da
? ctx
->i1true
: ctx
->i1false
; /* da */
3439 params
[param_count
++] = ctx
->i1false
; /* slc */
3442 switch (instr
->intrinsic
) {
3443 case nir_intrinsic_image_atomic_add
:
3444 atomic_name
= "add";
3446 case nir_intrinsic_image_atomic_min
:
3447 atomic_name
= "smin";
3449 case nir_intrinsic_image_atomic_max
:
3450 atomic_name
= "smax";
3452 case nir_intrinsic_image_atomic_and
:
3453 atomic_name
= "and";
3455 case nir_intrinsic_image_atomic_or
:
3458 case nir_intrinsic_image_atomic_xor
:
3459 atomic_name
= "xor";
3461 case nir_intrinsic_image_atomic_exchange
:
3462 atomic_name
= "swap";
3464 case nir_intrinsic_image_atomic_comp_swap
:
3465 atomic_name
= "cmpswap";
3470 build_int_type_name(LLVMTypeOf(coords
),
3471 coords_type
, sizeof(coords_type
));
3473 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3474 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
3475 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
3478 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
3479 nir_intrinsic_instr
*instr
)
3482 const nir_variable
*var
= instr
->variables
[0]->var
;
3483 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3484 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3485 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3486 if(instr
->variables
[0]->deref
.child
)
3487 type
= instr
->variables
[0]->deref
.child
->type
;
3489 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3490 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
3492 struct ac_image_args args
= { 0 };
3496 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3497 args
.opcode
= ac_image_get_resinfo
;
3498 args
.addr
= ctx
->i32zero
;
3500 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3502 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3503 glsl_sampler_type_is_array(type
)) {
3504 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3505 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3506 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
3507 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3508 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
3513 #define NOOP_WAITCNT 0xf7f
3514 #define LGKM_CNT 0x07f
3515 #define VM_CNT 0xf70
3517 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3520 LLVMValueRef args
[1] = {
3521 LLVMConstInt(ctx
->i32
, simm16
, false),
3523 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3524 ctx
->voidt
, args
, 1, 0);
3527 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3529 /* SI only (thanks to a hw bug workaround):
3530 * The real barrier instruction isn’t needed, because an entire patch
3531 * always fits into a single wave.
3533 if (ctx
->options
->chip_class
== SI
&&
3534 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3535 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3538 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3539 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3542 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
3543 nir_intrinsic_instr
*instr
)
3546 ctx
->shader_info
->fs
.can_discard
= true;
3548 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3549 get_src(ctx
, instr
->src
[0]),
3552 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
3553 LLVMConstReal(ctx
->f32
, -1.0f
),
3555 ac_build_kill(&ctx
->ac
, cond
);
3559 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3561 LLVMValueRef result
;
3562 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3563 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3564 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
3566 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3569 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3570 nir_intrinsic_instr
*instr
)
3572 LLVMValueRef ptr
, result
;
3573 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3574 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3575 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3577 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3578 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
3579 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3581 LLVMAtomicOrderingSequentiallyConsistent
,
3582 LLVMAtomicOrderingSequentiallyConsistent
,
3585 LLVMAtomicRMWBinOp op
;
3586 switch (instr
->intrinsic
) {
3587 case nir_intrinsic_var_atomic_add
:
3588 op
= LLVMAtomicRMWBinOpAdd
;
3590 case nir_intrinsic_var_atomic_umin
:
3591 op
= LLVMAtomicRMWBinOpUMin
;
3593 case nir_intrinsic_var_atomic_umax
:
3594 op
= LLVMAtomicRMWBinOpUMax
;
3596 case nir_intrinsic_var_atomic_imin
:
3597 op
= LLVMAtomicRMWBinOpMin
;
3599 case nir_intrinsic_var_atomic_imax
:
3600 op
= LLVMAtomicRMWBinOpMax
;
3602 case nir_intrinsic_var_atomic_and
:
3603 op
= LLVMAtomicRMWBinOpAnd
;
3605 case nir_intrinsic_var_atomic_or
:
3606 op
= LLVMAtomicRMWBinOpOr
;
3608 case nir_intrinsic_var_atomic_xor
:
3609 op
= LLVMAtomicRMWBinOpXor
;
3611 case nir_intrinsic_var_atomic_exchange
:
3612 op
= LLVMAtomicRMWBinOpXchg
;
3618 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
3619 LLVMAtomicOrderingSequentiallyConsistent
,
3625 #define INTERP_CENTER 0
3626 #define INTERP_CENTROID 1
3627 #define INTERP_SAMPLE 2
3629 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3630 enum glsl_interp_mode interp
, unsigned location
)
3633 case INTERP_MODE_FLAT
:
3636 case INTERP_MODE_SMOOTH
:
3637 case INTERP_MODE_NONE
:
3638 if (location
== INTERP_CENTER
)
3639 return ctx
->persp_center
;
3640 else if (location
== INTERP_CENTROID
)
3641 return ctx
->persp_centroid
;
3642 else if (location
== INTERP_SAMPLE
)
3643 return ctx
->persp_sample
;
3645 case INTERP_MODE_NOPERSPECTIVE
:
3646 if (location
== INTERP_CENTER
)
3647 return ctx
->linear_center
;
3648 else if (location
== INTERP_CENTROID
)
3649 return ctx
->linear_centroid
;
3650 else if (location
== INTERP_SAMPLE
)
3651 return ctx
->linear_sample
;
3657 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3658 LLVMValueRef sample_id
)
3660 LLVMValueRef result
;
3661 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_PS_SAMPLE_POSITIONS
, false));
3663 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3664 const_array(ctx
->v2f32
, 64), "");
3666 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3667 result
= ac_build_indexed_load(&ctx
->ac
, ptr
, sample_id
, false);
3672 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
3674 LLVMValueRef values
[2];
3676 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
3677 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
3678 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3681 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3682 nir_intrinsic_instr
*instr
)
3684 LLVMValueRef result
[2];
3685 LLVMValueRef interp_param
, attr_number
;
3688 LLVMValueRef src_c0
, src_c1
;
3690 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3691 switch (instr
->intrinsic
) {
3692 case nir_intrinsic_interp_var_at_centroid
:
3693 location
= INTERP_CENTROID
;
3695 case nir_intrinsic_interp_var_at_sample
:
3696 case nir_intrinsic_interp_var_at_offset
:
3697 location
= INTERP_CENTER
;
3698 src0
= get_src(ctx
, instr
->src
[0]);
3704 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3705 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3706 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3707 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3708 LLVMValueRef sample_position
;
3709 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3711 /* fetch sample ID */
3712 sample_position
= load_sample_position(ctx
, src0
);
3714 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3715 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3716 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3717 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3719 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3720 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3722 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3723 LLVMValueRef ij_out
[2];
3724 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3727 * take the I then J parameters, and the DDX/Y for it, and
3728 * calculate the IJ inputs for the interpolator.
3729 * temp1 = ddx * offset/sample.x + I;
3730 * interp_param.I = ddy * offset/sample.y + temp1;
3731 * temp1 = ddx * offset/sample.x + J;
3732 * interp_param.J = ddy * offset/sample.y + temp1;
3734 for (unsigned i
= 0; i
< 2; i
++) {
3735 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3736 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3737 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3738 ddxy_out
, ix_ll
, "");
3739 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3740 ddxy_out
, iy_ll
, "");
3741 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3742 interp_param
, ix_ll
, "");
3743 LLVMValueRef temp1
, temp2
;
3745 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3748 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3749 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3751 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3752 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3754 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3755 temp2
, ctx
->i32
, "");
3757 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3761 for (chan
= 0; chan
< 2; chan
++) {
3762 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3765 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3766 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3767 LLVMValueRef i
= LLVMBuildExtractElement(
3768 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3769 LLVMValueRef j
= LLVMBuildExtractElement(
3770 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3772 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3773 llvm_chan
, attr_number
,
3774 ctx
->prim_mask
, i
, j
);
3776 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3777 LLVMConstInt(ctx
->i32
, 2, false),
3778 llvm_chan
, attr_number
,
3782 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3786 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3787 nir_intrinsic_instr
*instr
)
3789 LLVMValueRef gs_next_vertex
;
3790 LLVMValueRef can_emit
, kill
;
3793 assert(instr
->const_index
[0] == 0);
3794 /* Write vertex attribute values to GSVS ring */
3795 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3796 ctx
->gs_next_vertex
,
3799 /* If this thread has already emitted the declared maximum number of
3800 * vertices, kill it: excessive vertex emissions are not supposed to
3801 * have any effect, and GS threads have no externally observable
3802 * effects other than emitting vertices.
3804 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3805 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3807 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3808 LLVMConstReal(ctx
->f32
, 1.0f
),
3809 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3810 ac_build_kill(&ctx
->ac
, kill
);
3812 /* loop num outputs */
3814 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3815 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3820 if (!(ctx
->output_mask
& (1ull << i
)))
3823 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3824 /* pack clip and cull into a single set of slots */
3825 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3829 for (unsigned j
= 0; j
< length
; j
++) {
3830 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3832 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3833 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3834 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3836 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3838 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3840 voffset
, ctx
->gs2vs_offset
, 0,
3846 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3848 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3850 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3854 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3855 nir_intrinsic_instr
*instr
)
3857 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3861 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
3862 nir_intrinsic_instr
*instr
)
3864 LLVMValueRef coord
[4] = {
3871 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
3872 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->f32one
,
3873 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
3875 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
3876 return LLVMBuildBitCast(ctx
->builder
, result
,
3877 get_def_type(ctx
, &instr
->dest
.ssa
), "");
3880 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3881 nir_intrinsic_instr
*instr
)
3883 LLVMValueRef result
= NULL
;
3885 switch (instr
->intrinsic
) {
3886 case nir_intrinsic_load_work_group_id
: {
3887 result
= ctx
->workgroup_ids
;
3890 case nir_intrinsic_load_base_vertex
: {
3891 result
= ctx
->base_vertex
;
3894 case nir_intrinsic_load_vertex_id_zero_base
: {
3895 result
= ctx
->vertex_id
;
3898 case nir_intrinsic_load_local_invocation_id
: {
3899 result
= ctx
->local_invocation_ids
;
3902 case nir_intrinsic_load_base_instance
:
3903 result
= ctx
->start_instance
;
3905 case nir_intrinsic_load_draw_id
:
3906 result
= ctx
->draw_index
;
3908 case nir_intrinsic_load_invocation_id
:
3909 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3910 result
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
3912 result
= ctx
->gs_invocation_id
;
3914 case nir_intrinsic_load_primitive_id
:
3915 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3916 result
= ctx
->gs_prim_id
;
3917 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3918 result
= ctx
->tcs_patch_id
;
3919 else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3920 result
= ctx
->tes_patch_id
;
3922 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3924 case nir_intrinsic_load_sample_id
:
3925 ctx
->shader_info
->fs
.force_persample
= true;
3926 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3928 case nir_intrinsic_load_sample_pos
:
3929 ctx
->shader_info
->fs
.force_persample
= true;
3930 result
= load_sample_pos(ctx
);
3932 case nir_intrinsic_load_sample_mask_in
:
3933 result
= ctx
->sample_coverage
;
3935 case nir_intrinsic_load_front_face
:
3936 result
= ctx
->front_face
;
3938 case nir_intrinsic_load_instance_id
:
3939 result
= ctx
->instance_id
;
3940 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3941 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3943 case nir_intrinsic_load_num_work_groups
:
3944 result
= ctx
->num_work_groups
;
3946 case nir_intrinsic_load_local_invocation_index
:
3947 result
= visit_load_local_invocation_index(ctx
);
3949 case nir_intrinsic_load_push_constant
:
3950 result
= visit_load_push_constant(ctx
, instr
);
3952 case nir_intrinsic_vulkan_resource_index
:
3953 result
= visit_vulkan_resource_index(ctx
, instr
);
3955 case nir_intrinsic_store_ssbo
:
3956 visit_store_ssbo(ctx
, instr
);
3958 case nir_intrinsic_load_ssbo
:
3959 result
= visit_load_buffer(ctx
, instr
);
3961 case nir_intrinsic_ssbo_atomic_add
:
3962 case nir_intrinsic_ssbo_atomic_imin
:
3963 case nir_intrinsic_ssbo_atomic_umin
:
3964 case nir_intrinsic_ssbo_atomic_imax
:
3965 case nir_intrinsic_ssbo_atomic_umax
:
3966 case nir_intrinsic_ssbo_atomic_and
:
3967 case nir_intrinsic_ssbo_atomic_or
:
3968 case nir_intrinsic_ssbo_atomic_xor
:
3969 case nir_intrinsic_ssbo_atomic_exchange
:
3970 case nir_intrinsic_ssbo_atomic_comp_swap
:
3971 result
= visit_atomic_ssbo(ctx
, instr
);
3973 case nir_intrinsic_load_ubo
:
3974 result
= visit_load_ubo_buffer(ctx
, instr
);
3976 case nir_intrinsic_get_buffer_size
:
3977 result
= visit_get_buffer_size(ctx
, instr
);
3979 case nir_intrinsic_load_var
:
3980 result
= visit_load_var(ctx
, instr
);
3982 case nir_intrinsic_store_var
:
3983 visit_store_var(ctx
, instr
);
3985 case nir_intrinsic_image_load
:
3986 result
= visit_image_load(ctx
, instr
);
3988 case nir_intrinsic_image_store
:
3989 visit_image_store(ctx
, instr
);
3991 case nir_intrinsic_image_atomic_add
:
3992 case nir_intrinsic_image_atomic_min
:
3993 case nir_intrinsic_image_atomic_max
:
3994 case nir_intrinsic_image_atomic_and
:
3995 case nir_intrinsic_image_atomic_or
:
3996 case nir_intrinsic_image_atomic_xor
:
3997 case nir_intrinsic_image_atomic_exchange
:
3998 case nir_intrinsic_image_atomic_comp_swap
:
3999 result
= visit_image_atomic(ctx
, instr
);
4001 case nir_intrinsic_image_size
:
4002 result
= visit_image_size(ctx
, instr
);
4004 case nir_intrinsic_discard
:
4005 ctx
->shader_info
->fs
.can_discard
= true;
4006 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4008 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4010 case nir_intrinsic_discard_if
:
4011 emit_discard_if(ctx
, instr
);
4013 case nir_intrinsic_memory_barrier
:
4014 emit_waitcnt(ctx
, VM_CNT
);
4016 case nir_intrinsic_barrier
:
4019 case nir_intrinsic_var_atomic_add
:
4020 case nir_intrinsic_var_atomic_imin
:
4021 case nir_intrinsic_var_atomic_umin
:
4022 case nir_intrinsic_var_atomic_imax
:
4023 case nir_intrinsic_var_atomic_umax
:
4024 case nir_intrinsic_var_atomic_and
:
4025 case nir_intrinsic_var_atomic_or
:
4026 case nir_intrinsic_var_atomic_xor
:
4027 case nir_intrinsic_var_atomic_exchange
:
4028 case nir_intrinsic_var_atomic_comp_swap
:
4029 result
= visit_var_atomic(ctx
, instr
);
4031 case nir_intrinsic_interp_var_at_centroid
:
4032 case nir_intrinsic_interp_var_at_sample
:
4033 case nir_intrinsic_interp_var_at_offset
:
4034 result
= visit_interp(ctx
, instr
);
4036 case nir_intrinsic_emit_vertex
:
4037 visit_emit_vertex(ctx
, instr
);
4039 case nir_intrinsic_end_primitive
:
4040 visit_end_primitive(ctx
, instr
);
4042 case nir_intrinsic_load_tess_coord
:
4043 result
= visit_load_tess_coord(ctx
, instr
);
4045 case nir_intrinsic_load_patch_vertices_in
:
4046 result
= LLVMConstInt(ctx
->i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
4049 fprintf(stderr
, "Unknown intrinsic: ");
4050 nir_print_instr(&instr
->instr
, stderr
);
4051 fprintf(stderr
, "\n");
4055 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4059 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
4060 nir_deref_var
*deref
,
4061 enum desc_type desc_type
)
4063 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
4064 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
4065 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
4066 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
4067 unsigned offset
= binding
->offset
;
4068 unsigned stride
= binding
->size
;
4070 LLVMBuilderRef builder
= ctx
->builder
;
4072 LLVMValueRef index
= NULL
;
4073 unsigned constant_index
= 0;
4075 assert(deref
->var
->data
.binding
< layout
->binding_count
);
4077 switch (desc_type
) {
4089 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4099 unreachable("invalid desc_type\n");
4102 if (deref
->deref
.child
) {
4103 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
4105 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4106 offset
+= child
->base_offset
* stride
;
4107 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4108 index
= get_src(ctx
, child
->indirect
);
4111 constant_index
= child
->base_offset
;
4113 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4114 (!index
|| binding
->immutable_samplers_equal
)) {
4115 if (binding
->immutable_samplers_equal
)
4118 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4120 LLVMValueRef constants
[] = {
4121 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 0], 0),
4122 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 1], 0),
4123 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 2], 0),
4124 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 3], 0),
4126 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4129 assert(stride
% type_size
== 0);
4132 index
= ctx
->i32zero
;
4134 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
4136 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
4137 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4139 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4142 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
4143 struct ac_image_args
*args
,
4144 nir_tex_instr
*instr
,
4146 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4147 LLVMValueRef
*param
, unsigned count
,
4150 unsigned is_rect
= 0;
4151 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4153 if (op
== nir_texop_lod
)
4155 /* Pad to power of two vector */
4156 while (count
< util_next_power_of_two(count
))
4157 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4160 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
4162 args
->addr
= param
[0];
4164 args
->resource
= res_ptr
;
4165 args
->sampler
= samp_ptr
;
4167 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4168 args
->addr
= param
[0];
4172 args
->dmask
= dmask
;
4173 args
->unorm
= is_rect
;
4177 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4180 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4181 * filtering manually. The driver sets img7 to a mask clearing
4182 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4183 * s_and_b32 samp0, samp0, img7
4186 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4188 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
4189 LLVMValueRef res
, LLVMValueRef samp
)
4191 LLVMBuilderRef builder
= ctx
->builder
;
4192 LLVMValueRef img7
, samp0
;
4194 if (ctx
->options
->chip_class
>= VI
)
4197 img7
= LLVMBuildExtractElement(builder
, res
,
4198 LLVMConstInt(ctx
->i32
, 7, 0), "");
4199 samp0
= LLVMBuildExtractElement(builder
, samp
,
4200 LLVMConstInt(ctx
->i32
, 0, 0), "");
4201 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4202 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4203 LLVMConstInt(ctx
->i32
, 0, 0), "");
4206 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
4207 nir_tex_instr
*instr
,
4208 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4209 LLVMValueRef
*fmask_ptr
)
4211 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4212 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
4214 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
4217 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
4219 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
4220 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4221 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4223 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4224 instr
->op
== nir_texop_samples_identical
))
4225 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
4228 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
4231 coord
= to_float(ctx
, coord
);
4232 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4233 coord
= to_integer(ctx
, coord
);
4237 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
4239 LLVMValueRef result
= NULL
;
4240 struct ac_image_args args
= { 0 };
4241 unsigned dmask
= 0xf;
4242 LLVMValueRef address
[16];
4243 LLVMValueRef coords
[5];
4244 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4245 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4246 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4247 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4248 LLVMValueRef derivs
[6];
4249 unsigned chan
, count
= 0;
4250 unsigned const_src
= 0, num_deriv_comp
= 0;
4251 bool lod_is_zero
= false;
4252 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4254 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4255 switch (instr
->src
[i
].src_type
) {
4256 case nir_tex_src_coord
:
4257 coord
= get_src(ctx
, instr
->src
[i
].src
);
4259 case nir_tex_src_projector
:
4261 case nir_tex_src_comparator
:
4262 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4264 case nir_tex_src_offset
:
4265 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4268 case nir_tex_src_bias
:
4269 bias
= get_src(ctx
, instr
->src
[i
].src
);
4271 case nir_tex_src_lod
: {
4272 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4274 if (val
&& val
->i32
[0] == 0)
4276 lod
= get_src(ctx
, instr
->src
[i
].src
);
4279 case nir_tex_src_ms_index
:
4280 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4282 case nir_tex_src_ms_mcs
:
4284 case nir_tex_src_ddx
:
4285 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4286 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4288 case nir_tex_src_ddy
:
4289 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4291 case nir_tex_src_texture_offset
:
4292 case nir_tex_src_sampler_offset
:
4293 case nir_tex_src_plane
:
4299 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4300 result
= get_buffer_size(ctx
, res_ptr
, true);
4304 if (instr
->op
== nir_texop_texture_samples
) {
4305 LLVMValueRef res
, samples
, is_msaa
;
4306 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
4307 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
4308 LLVMConstInt(ctx
->i32
, 3, false), "");
4309 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
4310 LLVMConstInt(ctx
->i32
, 28, false), "");
4311 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
4312 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4313 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
4314 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4316 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
4317 LLVMConstInt(ctx
->i32
, 16, false), "");
4318 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
4319 LLVMConstInt(ctx
->i32
, 0xf, false), "");
4320 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
4322 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
4329 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4330 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
4332 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4333 LLVMValueRef offset
[3], pack
;
4334 for (chan
= 0; chan
< 3; ++chan
)
4335 offset
[chan
] = ctx
->i32zero
;
4338 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4339 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
4340 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
4341 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
4343 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
4344 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
4346 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
4347 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
4348 address
[count
++] = pack
;
4351 /* pack LOD bias value */
4352 if (instr
->op
== nir_texop_txb
&& bias
) {
4353 address
[count
++] = bias
;
4356 /* Pack depth comparison value */
4357 if (instr
->is_shadow
&& comparator
) {
4358 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
4361 /* pack derivatives */
4363 switch (instr
->sampler_dim
) {
4364 case GLSL_SAMPLER_DIM_3D
:
4365 case GLSL_SAMPLER_DIM_CUBE
:
4368 case GLSL_SAMPLER_DIM_2D
:
4372 case GLSL_SAMPLER_DIM_1D
:
4377 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
4378 derivs
[i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
4379 derivs
[num_deriv_comp
+ i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
4383 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4384 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
)
4385 coords
[3] = apply_round_slice(ctx
, coords
[3]);
4386 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4387 coords
[chan
] = to_float(ctx
, coords
[chan
]);
4388 if (instr
->coord_components
== 3)
4389 coords
[3] = LLVMGetUndef(ctx
->f32
);
4390 ac_prepare_cube_coords(&ctx
->ac
,
4391 instr
->op
== nir_texop_txd
, instr
->is_array
,
4398 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4399 address
[count
++] = derivs
[i
];
4402 /* Pack texture coordinates */
4404 address
[count
++] = coords
[0];
4405 if (instr
->coord_components
> 1) {
4406 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4407 coords
[1] = apply_round_slice(ctx
, coords
[1]);
4409 address
[count
++] = coords
[1];
4411 if (instr
->coord_components
> 2) {
4412 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4413 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4414 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4415 instr
->op
!= nir_texop_txf
) {
4416 coords
[2] = apply_round_slice(ctx
, coords
[2]);
4418 address
[count
++] = coords
[2];
4423 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4424 instr
->op
== nir_texop_txf
)) {
4425 address
[count
++] = lod
;
4426 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4427 address
[count
++] = sample_index
;
4428 } else if(instr
->op
== nir_texop_txs
) {
4431 address
[count
++] = lod
;
4433 address
[count
++] = ctx
->i32zero
;
4436 for (chan
= 0; chan
< count
; chan
++) {
4437 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
4438 address
[chan
], ctx
->i32
, "");
4441 if (instr
->op
== nir_texop_samples_identical
) {
4442 LLVMValueRef txf_address
[4];
4443 struct ac_image_args txf_args
= { 0 };
4444 unsigned txf_count
= count
;
4445 memcpy(txf_address
, address
, sizeof(txf_address
));
4447 if (!instr
->is_array
)
4448 txf_address
[2] = ctx
->i32zero
;
4449 txf_address
[3] = ctx
->i32zero
;
4451 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
4453 txf_address
, txf_count
, 0xf);
4455 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4457 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4458 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
4462 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4463 instr
->op
!= nir_texop_txs
) {
4464 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4465 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
4468 instr
->is_array
? address
[2] : NULL
,
4469 address
[sample_chan
],
4473 if (offsets
&& instr
->op
== nir_texop_txf
) {
4474 nir_const_value
*const_offset
=
4475 nir_src_as_const_value(instr
->src
[const_src
].src
);
4476 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4477 assert(const_offset
);
4478 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4479 if (num_offsets
> 2)
4480 address
[2] = LLVMBuildAdd(ctx
->builder
,
4481 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
4482 if (num_offsets
> 1)
4483 address
[1] = LLVMBuildAdd(ctx
->builder
,
4484 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
4485 address
[0] = LLVMBuildAdd(ctx
->builder
,
4486 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
4490 /* TODO TG4 support */
4491 if (instr
->op
== nir_texop_tg4
) {
4492 if (instr
->is_shadow
)
4495 dmask
= 1 << instr
->component
;
4497 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
4498 res_ptr
, samp_ptr
, address
, count
, dmask
);
4500 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4502 if (instr
->op
== nir_texop_query_levels
)
4503 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
4504 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
4505 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4506 else if (instr
->op
== nir_texop_txs
&&
4507 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4509 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
4510 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
4511 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
4512 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
4513 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
4514 } else if (instr
->dest
.ssa
.num_components
!= 4)
4515 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
4519 assert(instr
->dest
.is_ssa
);
4520 result
= to_integer(ctx
, result
);
4521 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4526 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
4528 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4529 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
4531 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4532 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4535 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
4536 nir_phi_instr
*instr
,
4537 LLVMValueRef llvm_phi
)
4539 nir_foreach_phi_src(src
, instr
) {
4540 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4541 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4543 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4547 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
4549 struct hash_entry
*entry
;
4550 hash_table_foreach(ctx
->phis
, entry
) {
4551 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4552 (LLVMValueRef
)entry
->data
);
4557 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
4558 nir_ssa_undef_instr
*instr
)
4560 unsigned num_components
= instr
->def
.num_components
;
4563 if (num_components
== 1)
4564 undef
= LLVMGetUndef(ctx
->i32
);
4566 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
4568 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4571 static void visit_jump(struct nir_to_llvm_context
*ctx
,
4572 nir_jump_instr
*instr
)
4574 switch (instr
->type
) {
4575 case nir_jump_break
:
4576 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
4577 LLVMClearInsertionPosition(ctx
->builder
);
4579 case nir_jump_continue
:
4580 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4581 LLVMClearInsertionPosition(ctx
->builder
);
4584 fprintf(stderr
, "Unknown NIR jump instr: ");
4585 nir_print_instr(&instr
->instr
, stderr
);
4586 fprintf(stderr
, "\n");
4591 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4592 struct exec_list
*list
);
4594 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
4596 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
4597 nir_foreach_instr(instr
, block
)
4599 switch (instr
->type
) {
4600 case nir_instr_type_alu
:
4601 visit_alu(ctx
, nir_instr_as_alu(instr
));
4603 case nir_instr_type_load_const
:
4604 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4606 case nir_instr_type_intrinsic
:
4607 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4609 case nir_instr_type_tex
:
4610 visit_tex(ctx
, nir_instr_as_tex(instr
));
4612 case nir_instr_type_phi
:
4613 visit_phi(ctx
, nir_instr_as_phi(instr
));
4615 case nir_instr_type_ssa_undef
:
4616 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4618 case nir_instr_type_jump
:
4619 visit_jump(ctx
, nir_instr_as_jump(instr
));
4622 fprintf(stderr
, "Unknown NIR instr type: ");
4623 nir_print_instr(instr
, stderr
);
4624 fprintf(stderr
, "\n");
4629 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4632 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
4634 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4636 LLVMBasicBlockRef merge_block
=
4637 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4638 LLVMBasicBlockRef if_block
=
4639 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4640 LLVMBasicBlockRef else_block
= merge_block
;
4641 if (!exec_list_is_empty(&if_stmt
->else_list
))
4642 else_block
= LLVMAppendBasicBlockInContext(
4643 ctx
->context
, ctx
->main_function
, "");
4645 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
4646 LLVMConstInt(ctx
->i32
, 0, false), "");
4647 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
4649 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
4650 visit_cf_list(ctx
, &if_stmt
->then_list
);
4651 if (LLVMGetInsertBlock(ctx
->builder
))
4652 LLVMBuildBr(ctx
->builder
, merge_block
);
4654 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4655 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
4656 visit_cf_list(ctx
, &if_stmt
->else_list
);
4657 if (LLVMGetInsertBlock(ctx
->builder
))
4658 LLVMBuildBr(ctx
->builder
, merge_block
);
4661 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
4664 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
4666 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4667 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4669 ctx
->continue_block
=
4670 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4672 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4674 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4675 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
4676 visit_cf_list(ctx
, &loop
->body
);
4678 if (LLVMGetInsertBlock(ctx
->builder
))
4679 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4680 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
4682 ctx
->continue_block
= continue_parent
;
4683 ctx
->break_block
= break_parent
;
4686 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4687 struct exec_list
*list
)
4689 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4691 switch (node
->type
) {
4692 case nir_cf_node_block
:
4693 visit_block(ctx
, nir_cf_node_as_block(node
));
4696 case nir_cf_node_if
:
4697 visit_if(ctx
, nir_cf_node_as_if(node
));
4700 case nir_cf_node_loop
:
4701 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4711 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4712 struct nir_variable
*variable
)
4714 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4715 LLVMValueRef t_offset
;
4716 LLVMValueRef t_list
;
4718 LLVMValueRef buffer_index
;
4719 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4720 int idx
= variable
->data
.location
;
4721 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4723 variable
->data
.driver_location
= idx
* 4;
4725 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4726 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
4727 ctx
->start_instance
, "");
4728 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4729 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4731 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
4732 ctx
->base_vertex
, "");
4734 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4735 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4737 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4739 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
4741 LLVMConstInt(ctx
->i32
, 0, false),
4744 for (unsigned chan
= 0; chan
< 4; chan
++) {
4745 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4746 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4747 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
4748 input
, llvm_chan
, ""));
4753 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4755 LLVMValueRef interp_param
,
4756 LLVMValueRef prim_mask
,
4757 LLVMValueRef result
[4])
4759 LLVMValueRef attr_number
;
4762 bool interp
= interp_param
!= NULL
;
4764 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4766 /* fs.constant returns the param from the middle vertex, so it's not
4767 * really useful for flat shading. It's meant to be used for custom
4768 * interpolation (but the intrinsic can't fetch from the other two
4771 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4772 * to do the right thing. The only reason we use fs.constant is that
4773 * fs.interp cannot be used on integers, because they can be equal
4777 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4778 LLVMVectorType(ctx
->f32
, 2), "");
4780 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4782 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4786 for (chan
= 0; chan
< 4; chan
++) {
4787 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4790 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4795 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4796 LLVMConstInt(ctx
->i32
, 2, false),
4805 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4806 struct nir_variable
*variable
)
4808 int idx
= variable
->data
.location
;
4809 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4810 LLVMValueRef interp
;
4812 variable
->data
.driver_location
= idx
* 4;
4813 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4815 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4816 unsigned interp_type
;
4817 if (variable
->data
.sample
) {
4818 interp_type
= INTERP_SAMPLE
;
4819 ctx
->shader_info
->fs
.force_persample
= true;
4820 } else if (variable
->data
.centroid
)
4821 interp_type
= INTERP_CENTROID
;
4823 interp_type
= INTERP_CENTER
;
4825 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4829 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4830 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4835 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4836 struct nir_variable
*variable
)
4838 switch (ctx
->stage
) {
4839 case MESA_SHADER_VERTEX
:
4840 handle_vs_input_decl(ctx
, variable
);
4842 case MESA_SHADER_FRAGMENT
:
4843 handle_fs_input_decl(ctx
, variable
);
4852 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4853 struct nir_shader
*nir
)
4856 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4857 LLVMValueRef interp_param
;
4858 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4860 if (!(ctx
->input_mask
& (1ull << i
)))
4863 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4864 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4865 interp_param
= *inputs
;
4866 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4870 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4872 } else if (i
== VARYING_SLOT_POS
) {
4873 for(int i
= 0; i
< 3; ++i
)
4874 inputs
[i
] = ctx
->frag_pos
[i
];
4876 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4879 ctx
->shader_info
->fs
.num_interp
= index
;
4880 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4881 ctx
->shader_info
->fs
.has_pcoord
= true;
4882 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4883 ctx
->shader_info
->fs
.prim_id_input
= true;
4884 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4885 ctx
->shader_info
->fs
.layer_input
= true;
4886 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4890 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4894 LLVMBuilderRef builder
= ctx
->builder
;
4895 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4896 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4897 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4898 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4899 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4903 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4905 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4908 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4909 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4911 LLVMDisposeBuilder(first_builder
);
4916 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4920 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4921 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4926 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4927 struct nir_variable
*variable
)
4929 int idx
= variable
->data
.location
+ variable
->data
.index
;
4930 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4931 uint64_t mask_attribs
;
4932 variable
->data
.driver_location
= idx
* 4;
4934 /* tess ctrl has it's own load/store paths for outputs */
4935 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4938 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4939 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4940 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
4941 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4942 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4943 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4944 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4945 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4946 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4948 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4949 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4950 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4957 mask_attribs
= 1ull << idx
;
4961 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4962 for (unsigned chan
= 0; chan
< 4; chan
++) {
4963 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4964 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4967 ctx
->output_mask
|= mask_attribs
;
4971 setup_locals(struct nir_to_llvm_context
*ctx
,
4972 struct nir_function
*func
)
4975 ctx
->num_locals
= 0;
4976 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4977 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4978 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4979 ctx
->num_locals
+= attrib_count
;
4981 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4985 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4986 for (j
= 0; j
< 4; j
++) {
4987 ctx
->locals
[i
* 4 + j
] =
4988 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4994 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4996 v
= to_float(ctx
, v
);
4997 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4998 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
5002 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
5003 LLVMValueRef src0
, LLVMValueRef src1
)
5005 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
5006 LLVMValueRef comp
[2];
5008 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5009 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5010 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5011 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5014 /* Initialize arguments for the shader export intrinsic */
5016 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5017 LLVMValueRef
*values
,
5019 struct ac_export_args
*args
)
5021 /* Default is 0xf. Adjusted below depending on the format. */
5022 args
->enabled_channels
= 0xf;
5024 /* Specify whether the EXEC mask represents the valid mask */
5025 args
->valid_mask
= 0;
5027 /* Specify whether this is the last export */
5030 /* Specify the target we are exporting */
5031 args
->target
= target
;
5033 args
->compr
= false;
5034 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
5035 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
5036 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
5037 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
5042 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5043 LLVMValueRef val
[4];
5044 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5045 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5046 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5048 switch(col_format
) {
5049 case V_028714_SPI_SHADER_ZERO
:
5050 args
->enabled_channels
= 0; /* writemask */
5051 args
->target
= V_008DFC_SQ_EXP_NULL
;
5054 case V_028714_SPI_SHADER_32_R
:
5055 args
->enabled_channels
= 1;
5056 args
->out
[0] = values
[0];
5059 case V_028714_SPI_SHADER_32_GR
:
5060 args
->enabled_channels
= 0x3;
5061 args
->out
[0] = values
[0];
5062 args
->out
[1] = values
[1];
5065 case V_028714_SPI_SHADER_32_AR
:
5066 args
->enabled_channels
= 0x9;
5067 args
->out
[0] = values
[0];
5068 args
->out
[3] = values
[3];
5071 case V_028714_SPI_SHADER_FP16_ABGR
:
5074 for (unsigned chan
= 0; chan
< 2; chan
++) {
5075 LLVMValueRef pack_args
[2] = {
5077 values
[2 * chan
+ 1]
5079 LLVMValueRef packed
;
5081 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5082 args
->out
[chan
] = packed
;
5086 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5087 for (unsigned chan
= 0; chan
< 4; chan
++) {
5088 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5089 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5090 LLVMConstReal(ctx
->f32
, 65535), "");
5091 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5092 LLVMConstReal(ctx
->f32
, 0.5), "");
5093 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5098 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5099 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5102 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5103 for (unsigned chan
= 0; chan
< 4; chan
++) {
5104 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
5105 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5106 LLVMConstReal(ctx
->f32
, 32767), "");
5108 /* If positive, add 0.5, else add -0.5. */
5109 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5110 LLVMBuildSelect(ctx
->builder
,
5111 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5112 val
[chan
], ctx
->f32zero
, ""),
5113 LLVMConstReal(ctx
->f32
, 0.5),
5114 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
5115 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
5119 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5120 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5123 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5124 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
5126 for (unsigned chan
= 0; chan
< 4; chan
++) {
5127 val
[chan
] = to_integer(ctx
, values
[chan
]);
5128 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
5132 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5133 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5137 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5138 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
5139 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
5142 for (unsigned chan
= 0; chan
< 4; chan
++) {
5143 val
[chan
] = to_integer(ctx
, values
[chan
]);
5144 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
5145 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
5149 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5150 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5155 case V_028714_SPI_SHADER_32_ABGR
:
5156 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5160 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5162 for (unsigned i
= 0; i
< 4; ++i
)
5163 args
->out
[i
] = to_float(ctx
, args
->out
[i
]);
5167 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5168 struct ac_vs_output_info
*outinfo
)
5170 uint32_t param_count
= 0;
5172 unsigned pos_idx
, num_pos_exports
= 0;
5173 struct ac_export_args args
, pos_args
[4] = {};
5174 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5177 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5178 sizeof(outinfo
->vs_output_param_offset
));
5180 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5181 LLVMValueRef slots
[8];
5184 if (outinfo
->cull_dist_mask
)
5185 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5187 i
= VARYING_SLOT_CLIP_DIST0
;
5188 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5189 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5190 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5192 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5193 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5195 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5196 target
= V_008DFC_SQ_EXP_POS
+ 3;
5197 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5198 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5199 &args
, sizeof(args
));
5202 target
= V_008DFC_SQ_EXP_POS
+ 2;
5203 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5204 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5205 &args
, sizeof(args
));
5209 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5210 LLVMValueRef values
[4];
5211 if (!(ctx
->output_mask
& (1ull << i
)))
5214 for (unsigned j
= 0; j
< 4; j
++)
5215 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5216 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5218 if (i
== VARYING_SLOT_POS
) {
5219 target
= V_008DFC_SQ_EXP_POS
;
5220 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
5222 } else if (i
== VARYING_SLOT_PSIZ
) {
5223 outinfo
->writes_pointsize
= true;
5224 psize_value
= values
[0];
5226 } else if (i
== VARYING_SLOT_LAYER
) {
5227 outinfo
->writes_layer
= true;
5228 layer_value
= values
[0];
5229 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5230 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5232 } else if (i
== VARYING_SLOT_VIEWPORT
) {
5233 outinfo
->writes_viewport_index
= true;
5234 viewport_index_value
= values
[0];
5236 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5237 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5238 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5240 } else if (i
>= VARYING_SLOT_VAR0
) {
5241 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5242 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5243 outinfo
->vs_output_param_offset
[i
] = param_count
;
5247 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5249 if (target
>= V_008DFC_SQ_EXP_POS
&&
5250 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5251 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5252 &args
, sizeof(args
));
5254 ac_build_export(&ctx
->ac
, &args
);
5258 /* We need to add the position output manually if it's missing. */
5259 if (!pos_args
[0].out
[0]) {
5260 pos_args
[0].enabled_channels
= 0xf;
5261 pos_args
[0].valid_mask
= 0;
5262 pos_args
[0].done
= 0;
5263 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
5264 pos_args
[0].compr
= 0;
5265 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
5266 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
5267 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
5268 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
5271 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5272 (outinfo
->writes_layer
== true ? 4 : 0) |
5273 (outinfo
->writes_viewport_index
== true ? 8 : 0));
5275 pos_args
[1].enabled_channels
= mask
;
5276 pos_args
[1].valid_mask
= 0;
5277 pos_args
[1].done
= 0;
5278 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5279 pos_args
[1].compr
= 0;
5280 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
5281 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
5282 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
5283 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
5285 if (outinfo
->writes_pointsize
== true)
5286 pos_args
[1].out
[0] = psize_value
;
5287 if (outinfo
->writes_layer
== true)
5288 pos_args
[1].out
[2] = layer_value
;
5289 if (outinfo
->writes_viewport_index
== true)
5290 pos_args
[1].out
[3] = viewport_index_value
;
5292 for (i
= 0; i
< 4; i
++) {
5293 if (pos_args
[i
].out
[0])
5298 for (i
= 0; i
< 4; i
++) {
5299 if (!pos_args
[i
].out
[0])
5302 /* Specify the target we are exporting */
5303 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5304 if (pos_idx
== num_pos_exports
)
5305 pos_args
[i
].done
= 1;
5306 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5309 outinfo
->pos_exports
= num_pos_exports
;
5310 outinfo
->param_exports
= param_count
;
5314 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5315 struct ac_es_output_info
*outinfo
)
5318 uint64_t max_output_written
= 0;
5319 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5320 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5324 if (!(ctx
->output_mask
& (1ull << i
)))
5327 if (i
== VARYING_SLOT_CLIP_DIST0
)
5328 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5330 param_index
= shader_io_get_unique_index(i
);
5332 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5334 for (j
= 0; j
< length
; j
++) {
5335 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5336 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
5338 ac_build_buffer_store_dword(&ctx
->ac
,
5341 NULL
, ctx
->es2gs_offset
,
5342 (4 * param_index
+ j
) * 4,
5346 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5350 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5352 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5353 LLVMValueRef vertex_dw_stride
= unpack_param(ctx
, ctx
->ls_out_layout
, 13, 8);
5354 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5355 vertex_dw_stride
, "");
5357 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5358 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5361 if (!(ctx
->output_mask
& (1ull << i
)))
5364 if (i
== VARYING_SLOT_CLIP_DIST0
)
5365 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5366 int param
= shader_io_get_unique_index(i
);
5367 mark_tess_output(ctx
, false, param
);
5369 mark_tess_output(ctx
, false, param
+ 1);
5370 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5371 LLVMConstInt(ctx
->i32
, param
* 4, false),
5373 for (unsigned j
= 0; j
< length
; j
++) {
5374 lds_store(ctx
, dw_addr
,
5375 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5376 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->i32one
, "");
5381 struct ac_build_if_state
5383 struct nir_to_llvm_context
*ctx
;
5384 LLVMValueRef condition
;
5385 LLVMBasicBlockRef entry_block
;
5386 LLVMBasicBlockRef true_block
;
5387 LLVMBasicBlockRef false_block
;
5388 LLVMBasicBlockRef merge_block
;
5391 static LLVMBasicBlockRef
5392 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5394 LLVMBasicBlockRef current_block
;
5395 LLVMBasicBlockRef next_block
;
5396 LLVMBasicBlockRef new_block
;
5398 /* get current basic block */
5399 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5401 /* chqeck if there's another block after this one */
5402 next_block
= LLVMGetNextBasicBlock(current_block
);
5404 /* insert the new block before the next block */
5405 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5408 /* append new block after current block */
5409 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5410 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5416 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5417 struct nir_to_llvm_context
*ctx
,
5418 LLVMValueRef condition
)
5420 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5422 memset(ifthen
, 0, sizeof *ifthen
);
5424 ifthen
->condition
= condition
;
5425 ifthen
->entry_block
= block
;
5427 /* create endif/merge basic block for the phi functions */
5428 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5430 /* create/insert true_block before merge_block */
5431 ifthen
->true_block
=
5432 LLVMInsertBasicBlockInContext(ctx
->context
,
5433 ifthen
->merge_block
,
5436 /* successive code goes into the true block */
5437 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5441 * End a conditional.
5444 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5446 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5448 /* Insert branch to the merge block from current block */
5449 LLVMBuildBr(builder
, ifthen
->merge_block
);
5452 * Now patch in the various branch instructions.
5455 /* Insert the conditional branch instruction at the end of entry_block */
5456 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5457 if (ifthen
->false_block
) {
5458 /* we have an else clause */
5459 LLVMBuildCondBr(builder
, ifthen
->condition
,
5460 ifthen
->true_block
, ifthen
->false_block
);
5463 /* no else clause */
5464 LLVMBuildCondBr(builder
, ifthen
->condition
,
5465 ifthen
->true_block
, ifthen
->merge_block
);
5468 /* Resume building code at end of the ifthen->merge_block */
5469 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5473 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5475 unsigned stride
, outer_comps
, inner_comps
;
5476 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5477 LLVMValueRef invocation_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
5478 LLVMValueRef rel_patch_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
5479 unsigned tess_inner_index
, tess_outer_index
;
5480 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
5481 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
5485 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
5505 ac_nir_build_if(&if_ctx
, ctx
,
5506 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5507 invocation_id
, ctx
->i32zero
, ""));
5509 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5510 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5512 mark_tess_output(ctx
, true, tess_inner_index
);
5513 mark_tess_output(ctx
, true, tess_outer_index
);
5514 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
5515 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5516 LLVMConstInt(ctx
->i32
, tess_inner_index
* 4, false), "");
5517 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5518 LLVMConstInt(ctx
->i32
, tess_outer_index
* 4, false), "");
5520 for (i
= 0; i
< 4; i
++) {
5521 inner
[i
] = LLVMGetUndef(ctx
->i32
);
5522 outer
[i
] = LLVMGetUndef(ctx
->i32
);
5526 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
5527 outer
[0] = out
[1] = lds_load(ctx
, lds_outer
);
5528 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5529 LLVMConstInt(ctx
->i32
, 1, false), "");
5530 outer
[1] = out
[0] = lds_load(ctx
, lds_outer
);
5532 for (i
= 0; i
< outer_comps
; i
++) {
5534 lds_load(ctx
, lds_outer
);
5535 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5536 LLVMConstInt(ctx
->i32
, 1, false), "");
5538 for (i
= 0; i
< inner_comps
; i
++) {
5539 inner
[i
] = out
[outer_comps
+i
] =
5540 lds_load(ctx
, lds_inner
);
5541 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
5542 LLVMConstInt(ctx
->i32
, 1, false), "");
5546 /* Convert the outputs to vectors for stores. */
5547 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
5551 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
5554 buffer
= ctx
->hs_ring_tess_factor
;
5555 tf_base
= ctx
->tess_factor_offset
;
5556 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
5557 LLVMConstInt(ctx
->i32
, 4 * stride
, false), "");
5559 ac_nir_build_if(&inner_if_ctx
, ctx
,
5560 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5561 rel_patch_id
, ctx
->i32zero
, ""));
5563 /* Store the dynamic HS control word. */
5564 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
5565 LLVMConstInt(ctx
->i32
, 0x80000000, false),
5566 1, ctx
->i32zero
, tf_base
,
5567 0, 1, 0, true, false);
5568 ac_nir_build_endif(&inner_if_ctx
);
5570 /* Store the tessellation factors. */
5571 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
5572 MIN2(stride
, 4), byteoffset
, tf_base
,
5573 4, 1, 0, true, false);
5575 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
5576 stride
- 4, byteoffset
, tf_base
,
5577 20, 1, 0, true, false);
5579 //TODO store to offchip for TES to read - only if TES reads them
5581 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
5582 LLVMValueRef tf_inner_offset
;
5583 unsigned param_outer
, param_inner
;
5585 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5586 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5587 LLVMConstInt(ctx
->i32
, param_outer
, 0));
5589 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
5590 util_next_power_of_two(outer_comps
));
5592 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
5593 outer_comps
, tf_outer_offset
,
5594 ctx
->oc_lds
, 0, 1, 0, true, false);
5596 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5597 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5598 LLVMConstInt(ctx
->i32
, param_inner
, 0));
5600 inner_vec
= inner_comps
== 1 ? inner
[0] :
5601 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
5602 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
5603 inner_comps
, tf_inner_offset
,
5604 ctx
->oc_lds
, 0, 1, 0, true, false);
5607 ac_nir_build_endif(&if_ctx
);
5611 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
5613 write_tess_factors(ctx
);
5617 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
5618 LLVMValueRef
*color
, unsigned param
, bool is_last
,
5619 struct ac_export_args
*args
)
5622 si_llvm_init_export_args(ctx
, color
, param
,
5626 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
5627 args
->done
= 1; /* DONE bit */
5628 } else if (!args
->enabled_channels
)
5629 return false; /* unnecessary NULL export */
5635 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
5636 LLVMValueRef depth
, LLVMValueRef stencil
,
5637 LLVMValueRef samplemask
)
5639 struct ac_export_args args
;
5641 args
.enabled_channels
= 0;
5642 args
.valid_mask
= 1;
5644 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
5647 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
5648 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
5649 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
5650 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
5653 args
.out
[0] = depth
;
5654 args
.enabled_channels
|= 0x1;
5658 args
.out
[1] = stencil
;
5659 args
.enabled_channels
|= 0x2;
5663 args
.out
[2] = samplemask
;
5664 args
.enabled_channels
|= 0x4;
5667 /* SI (except OLAND) has a bug that it only looks
5668 * at the X writemask component. */
5669 if (ctx
->options
->chip_class
== SI
&&
5670 ctx
->options
->family
!= CHIP_OLAND
)
5671 args
.enabled_channels
|= 0x1;
5673 ac_build_export(&ctx
->ac
, &args
);
5677 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
5680 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
5681 struct ac_export_args color_args
[8];
5683 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5684 LLVMValueRef values
[4];
5686 if (!(ctx
->output_mask
& (1ull << i
)))
5689 if (i
== FRAG_RESULT_DEPTH
) {
5690 ctx
->shader_info
->fs
.writes_z
= true;
5691 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5692 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5693 } else if (i
== FRAG_RESULT_STENCIL
) {
5694 ctx
->shader_info
->fs
.writes_stencil
= true;
5695 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5696 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5697 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
5698 ctx
->shader_info
->fs
.writes_sample_mask
= true;
5699 samplemask
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5700 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5703 for (unsigned j
= 0; j
< 4; j
++)
5704 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5705 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5707 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
5708 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
5710 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
5716 for (unsigned i
= 0; i
< index
; i
++)
5717 ac_build_export(&ctx
->ac
, &color_args
[i
]);
5718 if (depth
|| stencil
|| samplemask
)
5719 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
5721 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
5722 ac_build_export(&ctx
->ac
, &color_args
[0]);
5725 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
5729 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
5731 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
5735 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
5737 switch (ctx
->stage
) {
5738 case MESA_SHADER_VERTEX
:
5739 if (ctx
->options
->key
.vs
.as_ls
)
5740 handle_ls_outputs_post(ctx
);
5741 else if (ctx
->options
->key
.vs
.as_es
)
5742 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
5744 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
5746 case MESA_SHADER_FRAGMENT
:
5747 handle_fs_outputs_post(ctx
);
5749 case MESA_SHADER_GEOMETRY
:
5750 emit_gs_epilogue(ctx
);
5752 case MESA_SHADER_TESS_CTRL
:
5753 handle_tcs_outputs_post(ctx
);
5755 case MESA_SHADER_TESS_EVAL
:
5756 if (ctx
->options
->key
.tes
.as_es
)
5757 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
5759 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->tes
.outinfo
);
5767 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
5768 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
5770 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
5771 variable
->data
.driver_location
= *offset
;
5775 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
5777 LLVMPassManagerRef passmgr
;
5778 /* Create the pass manager */
5779 passmgr
= LLVMCreateFunctionPassManagerForModule(
5782 /* This pass should eliminate all the load and store instructions */
5783 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
5785 /* Add some optimization passes */
5786 LLVMAddScalarReplAggregatesPass(passmgr
);
5787 LLVMAddLICMPass(passmgr
);
5788 LLVMAddAggressiveDCEPass(passmgr
);
5789 LLVMAddCFGSimplificationPass(passmgr
);
5790 LLVMAddInstructionCombiningPass(passmgr
);
5793 LLVMInitializeFunctionPassManager(passmgr
);
5794 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
5795 LLVMFinalizeFunctionPassManager(passmgr
);
5797 LLVMDisposeBuilder(ctx
->builder
);
5798 LLVMDisposePassManager(passmgr
);
5802 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
5804 struct ac_vs_output_info
*outinfo
;
5806 if (ctx
->stage
== MESA_SHADER_FRAGMENT
||
5807 ctx
->stage
== MESA_SHADER_COMPUTE
||
5808 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5809 ctx
->stage
== MESA_SHADER_GEOMETRY
)
5812 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
5813 if (ctx
->options
->key
.vs
.as_ls
||
5814 ctx
->options
->key
.vs
.as_es
)
5816 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
5819 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5820 if (ctx
->options
->key
.vs
.as_es
)
5822 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
5825 ac_optimize_vs_outputs(&ctx
->ac
,
5827 outinfo
->vs_output_param_offset
,
5829 &outinfo
->param_exports
);
5833 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
5835 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
5836 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
5837 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
5840 if (ctx
->is_gs_copy_shader
) {
5841 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
5843 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5845 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
5846 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
5848 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
5850 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
5851 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
5852 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
5853 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
5855 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
5858 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5859 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5860 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
5861 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
5866 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
5867 struct nir_shader
*nir
)
5869 switch (nir
->stage
) {
5870 case MESA_SHADER_TESS_CTRL
:
5871 return chip_class
>= CIK
? 128 : 64;
5872 case MESA_SHADER_GEOMETRY
:
5874 case MESA_SHADER_COMPUTE
:
5880 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
5881 nir
->info
.cs
.local_size
[1] *
5882 nir
->info
.cs
.local_size
[2];
5883 return max_workgroup_size
;
5887 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
5888 struct nir_shader
*nir
,
5889 struct ac_shader_variant_info
*shader_info
,
5890 const struct ac_nir_compiler_options
*options
)
5892 struct nir_to_llvm_context ctx
= {0};
5893 struct nir_function
*func
;
5895 ctx
.options
= options
;
5896 ctx
.shader_info
= shader_info
;
5897 ctx
.context
= LLVMContextCreate();
5898 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5900 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5901 ctx
.ac
.module
= ctx
.module
;
5903 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
5905 memset(shader_info
, 0, sizeof(*shader_info
));
5907 ac_nir_shader_info_pass(nir
, options
, &shader_info
->info
);
5909 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
5911 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
5912 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
5913 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
5914 LLVMDisposeTargetData(data_layout
);
5915 LLVMDisposeMessage(data_layout_str
);
5919 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5920 ctx
.ac
.builder
= ctx
.builder
;
5921 ctx
.stage
= nir
->stage
;
5922 ctx
.max_workgroup_size
= ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
, nir
);
5924 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
5925 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
5926 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
5927 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
5929 create_function(&ctx
);
5931 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
5933 nir_foreach_variable(variable
, &nir
->shared
)
5937 uint32_t shared_size
= 0;
5939 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
5940 nir_foreach_variable(variable
, &nir
->shared
) {
5941 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
5946 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
5947 LLVMArrayType(ctx
.i8
, shared_size
),
5950 LLVMSetAlignment(var
, 4);
5951 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
5953 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5954 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
5956 ctx
.gs_max_out_vertices
= nir
->info
.gs
.vertices_out
;
5957 } else if (nir
->stage
== MESA_SHADER_TESS_EVAL
) {
5958 ctx
.tes_primitive_mode
= nir
->info
.tess
.primitive_mode
;
5961 ac_setup_rings(&ctx
);
5963 nir_foreach_variable(variable
, &nir
->inputs
)
5964 handle_shader_input_decl(&ctx
, variable
);
5966 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
5967 handle_fs_inputs_pre(&ctx
, nir
);
5969 ctx
.num_output_clips
= nir
->info
.clip_distance_array_size
;
5970 ctx
.num_output_culls
= nir
->info
.cull_distance_array_size
;
5972 nir_foreach_variable(variable
, &nir
->outputs
)
5973 handle_shader_output_decl(&ctx
, variable
);
5975 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5976 _mesa_key_pointer_equal
);
5977 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5978 _mesa_key_pointer_equal
);
5980 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5982 setup_locals(&ctx
, func
);
5984 visit_cf_list(&ctx
, &func
->impl
->body
);
5985 phi_post_pass(&ctx
);
5987 handle_shader_outputs_post(&ctx
);
5988 LLVMBuildRetVoid(ctx
.builder
);
5990 ac_llvm_finalize_module(&ctx
);
5992 ac_nir_eliminate_const_vs_outputs(&ctx
);
5994 ralloc_free(ctx
.defs
);
5995 ralloc_free(ctx
.phis
);
5997 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5998 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
5999 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6000 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6001 nir
->info
.gs
.vertices_out
;
6002 } else if (nir
->stage
== MESA_SHADER_TESS_CTRL
) {
6003 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6004 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6005 } else if (nir
->stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6006 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6012 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6014 unsigned *retval
= (unsigned *)context
;
6015 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6016 char *description
= LLVMGetDiagInfoDescription(di
);
6018 if (severity
== LLVMDSError
) {
6020 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6024 LLVMDisposeMessage(description
);
6027 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6028 struct ac_shader_binary
*binary
,
6029 LLVMTargetMachineRef tm
)
6031 unsigned retval
= 0;
6033 LLVMContextRef llvm_ctx
;
6034 LLVMMemoryBufferRef out_buffer
;
6035 unsigned buffer_size
;
6036 const char *buffer_data
;
6039 /* Setup Diagnostic Handler*/
6040 llvm_ctx
= LLVMGetModuleContext(M
);
6042 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6046 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6049 /* Process Errors/Warnings */
6051 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6057 /* Extract Shader Code*/
6058 buffer_size
= LLVMGetBufferSize(out_buffer
);
6059 buffer_data
= LLVMGetBufferStart(out_buffer
);
6061 ac_elf_read(buffer_data
, buffer_size
, binary
);
6064 LLVMDisposeMemoryBuffer(out_buffer
);
6070 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6071 LLVMModuleRef llvm_module
,
6072 struct ac_shader_binary
*binary
,
6073 struct ac_shader_config
*config
,
6074 struct ac_shader_variant_info
*shader_info
,
6075 gl_shader_stage stage
,
6076 bool dump_shader
, bool supports_spill
)
6079 ac_dump_module(llvm_module
);
6081 memset(binary
, 0, sizeof(*binary
));
6082 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6084 fprintf(stderr
, "compile failed\n");
6088 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6090 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6092 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6093 LLVMDisposeModule(llvm_module
);
6094 LLVMContextDispose(ctx
);
6096 if (stage
== MESA_SHADER_FRAGMENT
) {
6097 shader_info
->num_input_vgprs
= 0;
6098 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6099 shader_info
->num_input_vgprs
+= 2;
6100 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6101 shader_info
->num_input_vgprs
+= 2;
6102 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6103 shader_info
->num_input_vgprs
+= 2;
6104 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6105 shader_info
->num_input_vgprs
+= 3;
6106 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6107 shader_info
->num_input_vgprs
+= 2;
6108 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6109 shader_info
->num_input_vgprs
+= 2;
6110 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6111 shader_info
->num_input_vgprs
+= 2;
6112 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6113 shader_info
->num_input_vgprs
+= 1;
6114 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6115 shader_info
->num_input_vgprs
+= 1;
6116 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6117 shader_info
->num_input_vgprs
+= 1;
6118 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6119 shader_info
->num_input_vgprs
+= 1;
6120 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6121 shader_info
->num_input_vgprs
+= 1;
6122 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6123 shader_info
->num_input_vgprs
+= 1;
6124 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6125 shader_info
->num_input_vgprs
+= 1;
6126 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6127 shader_info
->num_input_vgprs
+= 1;
6128 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6129 shader_info
->num_input_vgprs
+= 1;
6131 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6133 /* +3 for scratch wave offset and VCC */
6134 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6135 shader_info
->num_input_sgprs
+ 3);
6138 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6139 struct ac_shader_binary
*binary
,
6140 struct ac_shader_config
*config
,
6141 struct ac_shader_variant_info
*shader_info
,
6142 struct nir_shader
*nir
,
6143 const struct ac_nir_compiler_options
*options
,
6147 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
6150 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
6151 switch (nir
->stage
) {
6152 case MESA_SHADER_COMPUTE
:
6153 for (int i
= 0; i
< 3; ++i
)
6154 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6156 case MESA_SHADER_FRAGMENT
:
6157 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6159 case MESA_SHADER_GEOMETRY
:
6160 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6161 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6162 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6163 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6165 case MESA_SHADER_TESS_EVAL
:
6166 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6167 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6168 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6169 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6170 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6172 case MESA_SHADER_TESS_CTRL
:
6173 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6175 case MESA_SHADER_VERTEX
:
6176 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6177 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6178 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6179 if (options
->key
.vs
.as_ls
)
6180 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6188 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6190 LLVMValueRef args
[9];
6191 args
[0] = ctx
->gsvs_ring
;
6192 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
6193 args
[3] = ctx
->i32zero
;
6194 args
[4] = ctx
->i32one
; /* OFFEN */
6195 args
[5] = ctx
->i32zero
; /* IDXEN */
6196 args
[6] = ctx
->i32one
; /* GLC */
6197 args
[7] = ctx
->i32one
; /* SLC */
6198 args
[8] = ctx
->i32zero
; /* TFE */
6202 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6206 if (!(ctx
->output_mask
& (1ull << i
)))
6209 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6210 /* unpack clip and cull from a single set of slots */
6211 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6216 for (unsigned j
= 0; j
< length
; j
++) {
6218 args
[2] = LLVMConstInt(ctx
->i32
,
6220 ctx
->gs_max_out_vertices
* 16 * 4, false);
6222 value
= ac_build_intrinsic(&ctx
->ac
,
6223 "llvm.SI.buffer.load.dword.i32.i32",
6225 AC_FUNC_ATTR_READONLY
|
6226 AC_FUNC_ATTR_LEGACY
);
6228 LLVMBuildStore(ctx
->builder
,
6229 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6233 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
6236 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6237 struct nir_shader
*geom_shader
,
6238 struct ac_shader_binary
*binary
,
6239 struct ac_shader_config
*config
,
6240 struct ac_shader_variant_info
*shader_info
,
6241 const struct ac_nir_compiler_options
*options
,
6244 struct nir_to_llvm_context ctx
= {0};
6245 ctx
.context
= LLVMContextCreate();
6246 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6247 ctx
.options
= options
;
6248 ctx
.shader_info
= shader_info
;
6250 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
6251 ctx
.ac
.module
= ctx
.module
;
6253 ctx
.is_gs_copy_shader
= true;
6254 LLVMSetTarget(ctx
.module
, "amdgcn--");
6257 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6258 ctx
.ac
.builder
= ctx
.builder
;
6259 ctx
.stage
= MESA_SHADER_VERTEX
;
6261 create_function(&ctx
);
6263 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6264 ac_setup_rings(&ctx
);
6266 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6267 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6269 nir_foreach_variable(variable
, &geom_shader
->outputs
)
6270 handle_shader_output_decl(&ctx
, variable
);
6272 ac_gs_copy_shader_emit(&ctx
);
6274 LLVMBuildRetVoid(ctx
.builder
);
6276 ac_llvm_finalize_module(&ctx
);
6278 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6280 dump_shader
, options
->supports_spill
);